Algorithm improves the maximum demand
Problem Description
To n ordered pairs of integers ai bi, you need to make you choose some integer number of all selected ai + bi and maximum. And ask you to select the number of pairs ai and non-negative, bi's and non-negative.
Input Format
The first act input n, the logarithm of the number of
each row of the two integers n rows ai bi
Output Format
Export your selected number of the ai + bi sum
Sample input
5
-403 -625
-847 901
-624 -708
-293 413
886 709
Sample Output
1715
Scale data and conventions
1<=n<=100
-1000<=ai,bi<=1000
Analysis: ai as weight, as the value of the bi seeking 01 backpack can, but is negative weight
Coordinate translation method, since the maximum range is 1e5, 1e5 we will zero right translation units,
01 We are all normal backpack to scroll backwards when a negative value when we can be a positive, it is conceivable to think close to zero,
we first addition to the "origin" are set outside -INF, then you can do 01 backpack Finally, go to the most value
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e7 + 10,INF = 2000000000;
typedef long long ll;
int dp[222222];
int main(){
ios_base::sync_with_stdio(0);
int n;
cin>>n;
int res = 0;
for ( int i = 0; i <= 200000; i++) dp[i] = -INF;
dp[100000] = 0;
for ( int i = 0; i < n; i++) {
int x,y; cin>>x>>y;
if( x >= 0) {
for (int j = 200000; j >= x; j--) {
if( dp[j - x] > -INF)
dp[j] = max(dp[j],dp[j - x] + y);
}
} else {
for ( int j = 0; j <= 200000 + x; j++) {
if( dp[j - x] > -INF)
dp[j] = max(dp[j],dp[j - x] + y);
}
}
}
for ( int i = 100000; i <= 200000; i++) {
if( dp[i] >= 0 ) {
res = max(res,i - 100000 + dp[i]);
}
}
cout<<res<<endl;
return 0;
}