1、B站视频链接:E09【模板】背包DP 完全背包_哔哩哔哩_bilibili
#include <bits/stdc++.h>
using namespace std;
const int N=1010;
int n,m;
int v[N],w[N],f[N][N];int main(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d%d",&v[i],&w[i]);}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(j<v[i])f[i][j]=f[i-1][j];else f[i][j]=max(f[i-1][j],f[i][j-v[i]]+w[i]);}}printf("%d\n",f[n][m]);return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N=1010;
int n,m;
int v[N],w[N],f[N];
int main(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)scanf("%d%d",&v[i],&w[i]);for(int i=1;i<=n;i++){for(int j=v[i];j<=m;j++){f[j]=max(f[j],f[j-v[i]]+w[i]);}}printf("%d\n",f[m]);return 0;
}
题目链接:疯狂的采药 - 洛谷
#include <bits/stdc++.h>
using namespace std;
int t,m;
const int N=10005,M=1e7+5;
long long a[N],b[N],f[M];//不开longlong见祖宗int main(){scanf("%d%d",&t,&m);for(int i=1;i<=m;i++){scanf("%d%d",&a[i],&b[i]);}for(int i=1;i<=m;i++){for(int j=a[i];j<=t;j++){f[j]=max(f[j],f[j-a[i]]+b[i]);}}printf("%lld\n",f[t]);//注意用%lldreturn 0;
}