超 dark van♂全背包
ref1
ref2
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, s, v[1005], w[1005], tmp[35], cnt ;
ll dp[4005], f[35][4005];
int main(){
while(scanf("%d %d", &n, &s)!=EOF){
memset(dp, 0, sizeof(dp));
memset(f, 0, sizeof(f));
cnt = 0;
int maxv=0;
for(int i=1; i<=n; i++){
scanf("%d %d", &v[i], &w[i]);
maxv = max(maxv, v[i]);
}
for(int i=1; i<=n; i++)
for(int j=v[i]; j<=4*maxv; j++)
dp[j] = max(dp[j], dp[j-v[i]]+w[i]);
while(s>0){
tmp[++cnt] = s;
s = (s - maxv) >> 1;
}
for(int i=cnt; i; i--)
for(int j=tmp[i]; j<=tmp[i]+2*maxv; j++)
if(j<=4*maxv)
f[i][j-tmp[i]] = dp[j];
else{
for(int k=(j-maxv)>>1; k<=j>>1; k++)
f[i][j-tmp[i]] = max(f[i][j-tmp[i]], f[i+1][k-tmp[i+1]]+f[i+1][j-k-tmp[i+1]]);
}
printf("%lld
", f[1][0]);
}
return 0;
}