按照挂件数量排序,然后做一个 DP 就好了.
code:
#include <bits/stdc++.h> #define ll long long #define N 2003 #define setIO(s) freopen(s".in","r",stdin) using namespace std; ll f[N]; struct node { int a,b; }s[N]; bool cmp(node x,node y) { return x.a>y.a; } int main() { // setIO("input"); int i,j,n; ll ans=0; memset(f,-0x3f,sizeof(f)),f[0]=f[1]=0; scanf("%d",&n); for(i=1;i<=n;++i) { scanf("%d%d",&s[i].a,&s[i].b); } sort(s+1,s+1+n,cmp); for(i=1;i<=n;++i) { if(s[i].a!=0) { for(j=n;j>=1;--j) { int v=min(j+s[i].a-1,n); f[v]=max(f[v],f[j]+s[i].b); ans=max(ans,f[v]); } } else { for(j=1;j<=n;++j) f[j-1]=max(f[j-1],f[j]+s[i].b),ans=max(ans,f[j-1]); } } printf("%lld ",ans); return 0; }