soj 2222 Health Power

2222 Health Power

xy likes playing TV games,these days there comes up a problem,that is,as you know,in the game,he can get plenty of foods,which can add to the HP (Health Power) of the role, only when the HP grows full,it adds to the Score of the role, now he doesn't how to maxium the Score of when facing kinds of food, please help him.

Input
There are multiple test cases.
The first line contains an integer N(0&ltN&lt1000),followed by N cases of input, each case of input consists of: a nonnegative integer K (0<=K <= 1000) on a single line,representing the HP needed to grow full,then an integer F (0<= F<= 10000) on a single line,representing there would be F foods,then F lines follow,each line contains 2 integers,the first one H ( 0<=H<=10000) is the HP of the food, the second one S (0<= S <= 10000) is the Score of the food.

Output
Output on a single line the Maxium Score can get for each case.

Sample Input

2

100 3
20 80
80 90
100 23
1000 3
0 23
900 290
 99 555

Sample Output

170
0


//题目的意思是先把人物血加满的情况下时得分最大
//思路:既然是求先把人物血加满的情况下时得分最大,则可以把它转换为把血加满的情况使得分最少,因为
//得分总和是一定的,
//例第一个数据  加满血时得分最少为23(即取加血量100的水果)而得分总和为80+90+23=193
//  得分总和减去加满血花费的23即得到最大得分170
#include<stdio.h>

#define INF (1<<30)

int f[10010];

void memset(int m);

int main()

{

   int T,n,m,sum,a,b,i;

   scanf("%d",&T);

   while(T--)

   {

       scanf("%d%d",&m,&n);

       sum=0;

       memset(m);

       f[0]=0;

       while(n--)

       {

           scanf("%d%d",&a,&b);

                 sum+=b;

           if(a>=m&&f[m]>b)f[m]=b;

           else

           for(i=2*m;i>=a;i--)

              {

                if((f[i]>f[i-a]+b)&&i-a<m)

                    f[i]=f[i-a]+b;

                if(i>m&&f[m]>f[i])f[m]=f[i];

              }

       }

       if(f[m]==INF)

          printf("0\n");

       else

          printf("%d\n",sum-f[m]);

   }

}



void memset(int m)

{

    int i;

    for(i=0;i<=2*m+1;i++)

       f[i]=INF;

}

你可能感兴趣的:(health)