描述

JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has ai coins on it (i=1, 2, 3...n). Surprisingly, each tree i can grow bi new coins each day if it is not cut down. From the first day, JAVAMAN can choose to cut down one tree each day to get all the coins on it. Since he can stay in the Dream City for at most m days, he can cut down at most m trees in all and if he decides not to cut one day, he cannot cut any trees later. (In other words, he can only cut down trees for consecutive m or less days from the first day!)

Given nmai and bi (i=1, 2, 3...n), calculate the maximum number of gold coins JAVAMAN can get.

输入

There are multiple test cases. The first line of input contains an integer T (T <= 200) indicates the number of test cases. Then T test cases follow.

Each test case contains 3 lines: The first line of each test case contains 2 positive integers n and m (0 < m <= n <= 250) separated by a space. The second line of each test case contains n positive integers separated by a space, indicating ai. (0 < ai <= 100, i=1, 2, 3...n) The third line of each test case also contains n positive integers separated by a space, indicating bi. (0 < bi <= 100, i=1, 2, 3...n)

输出

For each test case, output the result in a single line.

样例输入

2
2 1
10 10
1 1
2 2
8 10
2 3

样例输出

10
21

提示

Test case 1: JAVAMAN just cut tree 1 to get 10 gold coins at the first day.
Test case 2: JAVAMAN cut tree 1 at the first day and tree 2 at the second day to get 8 + 10 + 3 = 21 gold coins in all.
题目大意:
输入n,m分别代表n个数,和能砍m棵树,接下来n个数字ai代表第i棵树原有的金币,再接下来n个数字bi代表第i棵树每天增长的硬币。
将树按b升序排序,然后循环更新dp值即可。
#include <bits/stdc++.h>
using namespace std;
struct p
{
int a,b;
}x[];
int dp[];
bool cmp(p a,p b)
{
if(a.b!=b.b) return a.b<b.b;
return a.a<b.a;
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--)
{
memset(dp,,sizeof dp);
int n,m;
cin>>n>>m;
for(int i=;i<n;i++)
cin>>x[i].a;
for(int i=;i<n;i++)
cin>>x[i].b;
sort(x,x+n,cmp);
for(int i=;i<n;i++)
for(int j=m;j>;j--)
dp[j]=max(dp[j],dp[j-]+x[i].a+x[i].b*(j-));
cout<<dp[m]<<'\n';
}
return ;
}

Dream City(线性DP)的更多相关文章

  1. ZOJ 3211 Dream City(线性DP)

    Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he se ...

  2. ZOJ 3211 Dream City(DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3374 题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上 ...

  3. POJ-2346 Lucky tickets(线性DP)

    Lucky tickets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3298 Accepted: 2174 Descrip ...

  4. Hills——一道转移方程很“有趣”的线性DP

    题目描述 Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlastin ...

  5. LightOJ1044 Palindrome Partitioning(区间DP+线性DP)

    问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...

  6. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  7. hdu1712 线性dp

    //Accepted 400 KB 109 ms //dp线性 //dp[i][j]=max(dp[i-1][k]+a[i][j-k]) //在前i门课上花j天得到的最大分数,等于max(在前i-1门 ...

  8. 动态规划——线性dp

    我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...

  9. POJ 2479-Maximum sum(线性dp)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33918   Accepted: 10504 Des ...

随机推荐

  1. 不通过ecplise,只通过文件目录 创建最简单的JSP文件

    手动创建最简单的JSP 文件   1.在Tomcat 6.0的安装目录的webapps目录下新建一个目录,起名叫myapp. 2.在myapp目录下新建一个目录WEB-INF,注意,目录名称是区分大小 ...

  2. adb的含义

    ADB全名Andorid Debug Bridge. 是一个Debug工具.为何称之为Bridge呢?因为adb是一个标准的C/S结构的工具, 是要连接开发电脑和调试手机的.包含如下几个部分: 1.C ...

  3. Hash环/一致性Hash原理

    当前,Memcached.Redis这类分布式kv缓存已经非常普遍.从本篇开始,本系列将分析分布式缓存相关的原理.使用策略和最佳实践. 我们知道Memcached的分布式其实是一种“伪分布式”,也就是 ...

  4. JVM内存配置参数-XMX,-XMS,-XMN的例子

    转载:http://www.nowcoder.com/questionTerminal/093bfa948d144ce3b0a68b938ae8b4ec 对于JVM内存配置参数: -Xmx10240m ...

  5. Java迭代器的用法【转】

    迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的I ...

  6. ABP教程(四)- 开始一个简单的任务管理系统 - 实现UI端的增删改查

    接上一篇:ABP教程(三)- 开始一个简单的任务管理系统 – 后端编码 1.实现UI端的增删改查 1.1添加增删改查代码 打开SimpleTaskSystem.sln解决方案,添加一个“包含视图的MV ...

  7. Haproxy+Rabbitmq中的问题

    问题一.Rabbitmq集群搭建完成 某个集群节宕机后 无法添加失败 解决办法:停掉所有Rabbitmq服务 并删除集群文件C\Users\Administrator\AppData\Roaming\ ...

  8. android xml中使用include标签

    在一个项目中,我们可能会在xml中局部用到相同的布局,如果每次都在xml中重写这些布局,代码显得很冗余.重复的复制黏贴也很烦恼,所以,我们把这些相同的局部布局写成一个单独的xml模块,需要用到这些布局 ...

  9. html调用js提示方法名 is not defined处理方法

    解决办法(方法名 is not defined): dosave=function(){ alert("方法名在前"); } 下面这种写法有时候会出现错误: function do ...

  10. HTML5应用 + Cordova = 平台相关的混合应用

    Jerry之前的一篇文章 SAP Fiori应用的三种部署方式 曾经提到SAP Fiori应用的三种部署方式: On Premise环境下以ABAP BSP应用作为Fiori应用部署和运行的载体 部署 ...