http://lx.lanqiao.cn/problem.page?gpid=T368
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=105;
const int INF=0x3f3f3f3f;
const int t=100000;
int n;
int a[maxn],b[maxn];
int dp[maxn][200005];
// dp[i][j]前i对数中,当ai之和为j时,bi之和的最大值
//dp[i][j]=max(dp[i][j],dp[k][sumb]+Node[i].b)(0<=k<=i-1)
int main(){
cin >> n;
for (int i=0;i<n;i++){
cin >> a[i] >> b[i];
}
for (int i=0;i<n;i++){
for (int j=-t;j<=t;j++){
dp[i][j+t]=-INF;
}
}
for (int i=0;i<n;i++){
dp[i][a[i]+t]=b[i];
}
for (int i=1;i<n;i++){
for (int j=-t;j<=t;j++){
dp[i][j+t]=max(dp[i-1][j+t],dp[i][j+t]);
if (j-a[i]+t<0||j-a[i]+t>200000) continue;
dp[i][j+t]=max(dp[i][j+t],dp[i-1][j-a[i]+t]+b[i]);
}
}
int ans=0;
for (int j=0;j<=t;j++){
ans=max(ans,dp[n-1][j+t]>=0?j+dp[n-1][j+t]:-INF);
}
cout << ans << endl;
}