ZOJ - 3211 Dream City

<center> Dream City </center>
<center> Time Limit: 1 Second      Memory Limit: 32768 KB </center>

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 n, m, ai and bi (i=1, 2, 3...n), calculate the maximum number of gold coins JAVAMAN can get.

Input

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)

Output

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

Sample Input

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

Sample Output

10
21

Hints:
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.


Author: CAO, Peng

Source: The 6th Zhejiang Provincial Collegiate Programming Contest

                 这其实就是一道比较简单的01背包dp题目加了一层贪心的装饰~~因为假设选出来了x个树要砍~~他的增加斜率越大的当然要越往后放了~~加一个cmp的排序就好了~~;

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
struct ***
{
	int a, b;
}k[300];
bool cmp(*** a, *** b)
{
	return a.b < b.b;
}
int main()
{
	int te;
	scanf("%d", &te);
	while (te--)
	{
		int m, n;
		scanf("%d%d", &m, &n);
		for (int s = 0; s < m; s++)
		{
			scanf("%d", &k[s].a);
		}
		for (int s = 0; s < m; s++)
		{
			scanf("%d", &k[s].b);
		}
		sort(k, k+ m,cmp);
		int dp[300];
		memset(dp, 0, sizeof(dp));
		for (int s = 0; s < m; s++)
		{
			for (int w = n; w >0; w--)
			{
				dp[w] = max(dp[w], dp[w - 1] + k[s].a + (w - 1)*k[s].b);
			}
		}
		cout << dp[n] << endl;
	}
	return 0;
}

全部评论

相关推荐

10-11 17:45
门头沟学院 Java
走吗:别怕 我以前也是这么认为 虽然一面就挂 但是颇有收获!
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务