Apple time limit: theMs | Memory Limit:65535KB Difficulty:3
-
-
Describe
-
CTest has n apples to put into a backpack with a capacity of V. Give the size and price of the first Apple, and find out the total price of the apples that can be put into the backpack.
-
-
Input
-
-
There are several sets of test data, the first behavior of each group of test data 2 positive integers, respectively, the number of apples is n and the capacity of the backpack V,n, v at the same time to end the test at 0 o'clock, not output. The next n lines, 2 positive integers per line, are separated by a space, representing the size C and the price W of the apples, respectively. The range of all input numbers is greater than or equal to 0, less than or equal to 1000.
-
-
Output
-
-
output An integer for each set of test data, representing the total value of the apple that can be put into the backpack.
-
-
Sample input
-
-
3 31 12 13 10 0
-
-
Sample output
-
-
2
-
-
#include <cstdio> #include <cstring> #include <cstdlib> #define MAX (b) a>b?a:busing namespace Std ; int Dp[1010];int main () {int n,v,c,w,i,j;while (scanf ("%d%d", &n,&v) ==2) {if (n==0&&v==0) break;memset (Dp,0,sizeof (DP)), for (I=0;i<n;++i) {scanf ("%d%d", &c,&w), and for (J=V;J>=C;--J) {Dp[j]=max (dp[j],dp[j-c] +W);}} printf ("%d\n", Dp[v]);} return 0;}
Nyoj Apple "Dynamic planning"