01背包
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 LL;
//typedef unsigned long long ull;
//#define F first
//#define S second
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
const ld PI=acos(-1);
const ld eps=1e-9;
//unordered_map<int,int>mp;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const int seed=131;
const int M = 1e3+7;
int f[M];
int w[M],v[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,V;
cin>>n>>V;
for(int i=1;i<=n;i++)
cin>>v[i]>>w[i];
for(int i=1;i<=n;i++)
for(int j=V;j>=v[i];j--)
f[j]=max(f[j-v[i]]+w[i],f[j]);
//第i个物品选或不选
cout<<f[V]<<endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 LL;
//typedef unsigned long long ull;
//#define F first
//#define S second
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
const ld PI=acos(-1);
const ld eps=1e-9;
//unordered_map<int,int>mp;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const int seed=131;
const int M = 1e3+7;
int f[M];
int a[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,V;
cin>>n>>V;
for(int i=1;i<=n;i++)
cin>>a[i];
f[a[1]]=1;
f[0]=1;
for(int i=2;i<=n;i++)
for(int j=V;j>=a[i];j--)
f[j]=f[j]+f[j-a[i]];
//第i个数选或不选
cout<<f[V]<<endl;
return 0;
}
完全背包
把01背包第二重 倒序改正序即可。(01背包倒叙循环就是为了不变成完全背包)