D - 买糖果
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 #define FOR(i,a,b) for(int i=a;i<=b;i++) 8 #define MST(a,b) memset(a,b,sizeof(a)) 9 #define MAXN 105 10 #define MAXM 1050 11 int n,m; 12 int totx,toty,x[MAXN],y[MAXN]; 13 int f[2][MAXN][MAXM]; 14 int a,b; 15 int main() 16 { 17 18 while (scanf("%d%d",&n,&m)!=EOF) 19 { 20 FOR(i,1,n)scanf("%d%d",&x[i],&y[i]); 21 scanf("%d%d",&a,&b); 22 MST(f[0],128); 23 totx=0; 24 toty=0; 25 f[0][0][0]=0; 26 FOR(i,1,n) 27 { 28 MST(f[i%2],128); 29 totx+=x[i]; 30 FOR(j,0,i) 31 FOR(k,0,totx) 32 { 33 if(j-1>=0 && k-x[i]>=0 && f[(i+1)%2][j-1][k-x[i]]>=0)f[i%2][j][k]=f[(i+1)%2][j-1][k-x[i]]+y[i]; 34 if(f[(i+1)%2][j][k]>=f[i%2][j][k])f[i%2][j][k]=f[(i+1)%2][j][k]; 35 } 36 } 37 int ans=-1; 38 FOR(k,a,totx) 39 if(f[n%2][m][k]>=b && f[n%2][m][k]-b+k-a>ans)ans=f[n%2][m][k]-b+k-a; 40 printf("%d\n",ans); 41 } 42 }