P1616 疯狂的采药(完全背包基础)

注意到 \(O(nV)\) 可过。

#include<bits/stdc++.h>
using namespace std;
const int N=10000009;
typedef long long ll;
int V,n;
ll f[N];
int main(){
    scanf("%d%d",&V,&n);
    for(int i=1,v,w;i<=n;i++){
        scanf("%d%d",&w,&v);
        for(int j=w;j<=V;j++)
            f[j]=max(f[j],f[j-w]+v);
    }
    printf("%lld\n",f[V]);
    return 0;
}
posted @ 2026-02-13 17:11  2025ing  阅读(0)  评论(0)    收藏  举报