【洛谷】P3985 不开心的金明
#include<bits/stdc++.h> using namespace std; struct node{ int v,p; }a[105]; int n,w,ans; int f[10000005]; bool cmp(node a,node b) { return a.v<b.v; } int main() { scanf("%d %d",&n,&w); for (int i = 1; i <= n ; i ++) scanf("%d %d",&a[i].v,&a[i].p); sort(a+1,a+1+n,cmp); int l = 1, r = 1; while (l <= r) { if (l>n) break; while (r+1<=n && a[l].v+3 >= a[r+1].v) r ++; for (int i = 1 ; i <= w ; i ++) f[i] = 0; for (int i = 1 ; i <= r-l+1 ; i ++) for (int j = w ; j >= a[i].v ; j --) { f[j] = max(f[j],f[j-a[i].v]+a[i].p); } ans = max(ans,f[w]); l ++; r = l; } cout<<ans<<endl; return 0; }