描述

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. normal曲线绘制

      <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...

  2. python_10(模块与包)

    第1章 模块 1.1 模块的种类 1.2 定义 1.3 作用 1.4 导入及使用 1.4.1 import 1.4.2 测试一: 1.4.3 测试二: 1.4.4 测试三: 1.4.5 小结 1.4. ...

  3. servlet基础概念

    一.servlet是什么? 运行在Web服务器上(如:tomcat),作为浏览器请求与数据库或其他应用程序之间的中间层 二.servlet主要任务: 1.读取浏览器发送的显式数据(如:html表单)隐 ...

  4. Apache下禁止使用IP直接访问本站的配置方法

    现在管的严啊,上面要求不能使用IP直接访问服务器,把apache配置做下调整就行了.方法如下: 打开apache的配置文件 # vi /usr/local/apache2/conf/extra/htt ...

  5. PropTypes 和组件参数验证

    我们来了到了一个非常尴尬的章节,很多初学的朋友可能对这一章的知识点不屑一顾,觉得用不用对程序功能也没什么影响.但其实这一章节的知识在你构建多人协作.大型的应用程序的时候也是非常重要的,不可忽视. 都说 ...

  6. vue_resource 使用说明

    前几天用vue-resource调用接口,用post方式给后端,发现后端php接受不到数据,这好奇怪,最后发现提交给后端的时候 需要加一个参数 就是:emulateJSON : true 这句话的意思 ...

  7. 代码审查的艺术:Dropbox 的故事

    Dropbox 的 iOS 应用中的每一行代码,都是开始于被添加到 Maniphest 中的一个 bug 或者功能任务,Maniphest 是我们的任务管理系统.当一位工程师在上面接受一个任务,那么在 ...

  8. java实现排序的几种方法

    package com.ywx.count; import java.util.Scanner; /** * 题目:排序的几种方式(汇总及重构) * @author Vashon(yangwenxue ...

  9. Idea导入tomcat源码

    1.下载资源 下载主要包含两个包,已经编译的包和源码包,如图所示. 链接地址为: http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.93/bin ...

  10. std::map插入已存在的key时,key对应的内容不会被更新

    std::map插入已存在的key时,key对应的内容不会被更新,如果不知道这一点,可能会造成运行结果与预期的不一致 “Because element keys in a map are unique ...