Garlic king went shopping, he has a capacity of VV shopping bag, and he wanted to buy nn items, each item known volume v_ivi And the importance of p_ipi. Garlic king wanted to know what the selection of articles into the shopping bag, can make the items available and the degree of importance of the largest, and the volume of goods and does not exceed the capacity of shopping bags.
Input Format
The first line of the input two integers VV (1 \ leq V \ leq 1000. 1 ≤ V ≤ . 1 0 0 0 ) and nn (1 \ leq n \ leq 100. 1 ≤ n- ≤ . 1 0 0 ). Representative of the total volume of shopping bags VV , garlic Jun want to buy a total of nn items.
Next input nn lines of input two integers v_ivi And p_ipi( 1 \ leq v_i, p_i \ leq 1001≤vi,pi≤ . 1 0 0 ), respectively, and the volume of each item importance degree.
Output Format
Output line, output an integer representing the garlic king can buy a maximum degree of importance of the items and.
Sample input
50 4 1 5 60 99 49 8 33 7
Sample Output
13
#include <stdio.h>
#include <stdlib.h>
int v[110];
int p[110];
int dp[110][20020];
int main()
{
memset(dp,0,sizeof(dp));
int sum ,n ;
scanf("%d%d",&sum,&n);
int i,j;
for(i=1;i<=n;i++)
{
scanf("%d%d",&v[i],&p[i]);
}
for( i=1;i<=n;i++){
for( j=1;j<=sum;j++)
{
if(j<v[i])dp[i][j]=dp[i-1][j];
else dp[i][j]=dp[i-1][j]>(dp[i-1][j-v[i]]+p[i])? dp[i-1][j]:(dp[i-1][j-v[i]]+p[i]);
}
}
printf("%d",dp[n][sum]);
return 0;
}