zoj 3211 Dream city dp+贪心

来源:互联网 发布:九阴真经捏脸数据 编辑:程序博客网 时间:2024/05/22 16:46

JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are ntrees 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 growbi 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.

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 npositive integers separated by a space, indicating bi. (0 < bi <= 100, i=1, 2, 3...n)

<b< dd="">

Output

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

<b< dd="">

Sample Input

22 110 101 12 28 102 3

<b< dd="">

Sample Output

1021

<b< dd="">

Hint
s:
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.

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespace std;struct xx{    int a,b;}x[1005];int d[1005];bool cmp(xx A,xx B){    return A.b<B.b;}int main(){    int t;    while(~scanf("%d",&t))    {        while(t--)        {            memset(d,0,sizeof(d));            int n,m,i,j;            scanf("%d %d",&n,&m);            for(i=1;i<=n;i++)scanf("%d",&x[i].a);            for(i=1;i<=n;i++)scanf("%d",&x[i].b);            sort(x+1,x+n+1,cmp);            for(i=1;i<=n;i++)            {                for(j=m;j>=1;j--)                {                    d[j]=max(d[j],d[j-1]+x[i].a+x[i].b*(j-1));                }            }            printf("%d\n",d[m]);        }    }    return 0;}

原创粉丝点击