noip模拟66
A. 接力比赛
背包+剪枝.
A_code
#include<bits/stdc++.h>
using namespace std;
namespace BSS {
#define ll long long int
#define ull unsigned ll
#define lf double
#define lbt(x) (x&(-x))
#define mp(x,y) make_pair(x,y)
#define lb lower_bound
#define ub upper_bound
#define Fill(x,y) memset(x,y,sizeof x)
#define Copy(x,y) memcpy(x,y,sizeof x)
#define File(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
inline ll read() {
ll res=0; bool cit=1; char ch;
while(!isdigit(ch=getchar())) if(ch=='-') cit=0;
while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
return cit?res:-res;
}
} using namespace BSS;
const ll N=1e3+21,inf=1e15;
ll n,m,sum,suma,sumb,ans;
ll f[N*N],g[N*N],pref[N*N],preg[N*N];
struct I { ll x,y; } a[N],b[N];
signed main(){
File(game);
n=read(),m=read(),Fill(f,-0x3f),Fill(g,-0x3f);
for(int i=1;i<=n;i++){
pref[i]=pref[i-1]+(a[i].x=read()),a[i].y=read();
}
for(int i=1;i<=m;i++){
preg[i]=preg[i-1]+(b[i].x=read()),b[i].y=read();
}
sum=min(pref[n],preg[m]),f[0]=0,g[0]=0;
for(int i=1;i<=n;i++){
for(int j=pref[i];j>=a[i].x;j--)
f[j]=max(f[j],f[j-a[i].x]+a[i].y);
}
for(int i=1;i<=m;i++){
for(int j=preg[i];j>=b[i].x;j--)
g[j]=max(g[j],g[j-b[i].x]+b[i].y);
}
for(int i=0;i<=sum;i++) ans=max(ans,f[i]+g[i]);
printf("%lld\n",ans),exit(0);
}