The
Sorts the number of hooks from large to small, and then f[i] [j] represents the maximum value of the remaining J hooks for the first I decoration, and the transfer is very simple
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
int read () {char ch=getchar (); int f=0,x=1; while (ch< ' 0 ' | |
Ch> ' 9 ') {if (ch== '-') X=-1;ch=getchar ();}
while (ch>= ' 0 ' &&ch<= ' 9 ') {f= (f<<1) + (f<<3) +ch-' 0 '; Ch=getchar ();}
return f*x;
} int f[2005][4005],n,a[2005],b[2005];
struct data {int A;
int b;
}Q[2005];
BOOL CMP (const data &A,CONST data &b) {return a.a>b.a;} int main () {memset (f,-0x7f7f7f7f,sizeof (f));
N=read ();
for (int i=0;i<=n;i++) {for (int j=0;j<=2*n;j++) f[i][j]=-2100000000;
}f[0][1]=0;
for (int i=1;i<=n;i++) {q[i].a=read (); Q[i].b=read ();
}sort (Q+1,Q+N+1,CMP);
for (int i=1;i<=n;i++) {for (int j=0;j<=2*n;j++) {if (j) {
F[i][min (j+q[i].a-1,2*n)]=max (F[i-1][j]+q[i].b,f[i][min (j+q[i].a-1,2*n)]); F[I][J]=max (F[i][j],f[i-1][j]);
} else {F[i][j]=max (f[i][j],f[i-1][j]);
}}} int ans=0;
for (int i=0;i<=2*n;i++) Ans=max (Ans,f[n][i]);
cout<<ans;
}