11错误
//错误代码,无法保证容量最大
#include <bits/stdc++.h>
using namespace std;
const int N=502,M=2002;
typedef pair<int,int> pii;
struct wenjian{
string name;
int w,v;
}wen[N];
int dp[N][M];
int main(){
int n,m;
cin>>n>>m;
int sum=0;
for(int i=1;i<=n;i++){
cin>>wen[i].name>>wen[i].w>>wen[i].v;
sum+=wen[i].v;
}
if(sum<m){
cout<<"-1\n";
return 0;
}
memset(dp,0x3f,sizeof dp);
//memset(path,-1,sizeof path);
dp[0][0]=0;
vector<string> path;
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
dp[i][j]=min(dp[i-1][j],dp[i-1][max(0,j-wen[i].w)]+wen[i].v);
}
}
int ret=dp[n][m];
int mm=m;
for(int i=n;i>=1;i--){
if(ret==dp[i-1][max(0,mm-wen[i].w)]+wen[i].v){
mm=max(0,mm-wen[i].w);
ret-=wen[i].v;
path.push_back(wen[i].name);
}
}
int size=path.size();
for(int i=size-1;i>=0;i--)
cout<<path[i]<<"\n";
}
@xiao_shaui_ge
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define INF 2e18
ll dp[505][2005]={0};
ll w[505],v[505];
string name[505];
vector<vector<int>>res;
vector<int>node;
int main()
{
int n,m;
cin>>n>>m;
for (int i=0;i<505;i++) for (int j=0;j<2005;j++) dp[i][j]=INF;
dp[0][0]=0;
for (int i=1;i<=n;i++) {
cin>>name[i]>>w[i]>>v[i];
for (int j=0;j<=2000;j++) {
dp[i][j]=dp[i-1][j];
if (j>=w[i]) dp[i][j]=min(dp[i][j],dp[i-1][j-w[i]]+v[i]);
}
}
ll x=INF,ind=-1;
for (int i=2000;i>=m;i--) {
if (dp[n][i]<x) {
x=dp[n][i];
ind=i;
}
}
if (ind==-1) cout<<-1<<endl;
else {
stack<string>s;
for (int i=n;i>0;i--) {
if (dp[i][ind]!=dp[i-1][ind]) {
s.push(name[i]);
ind-=w[i];
}
}
while (!s.empty()) {
cout<<s.top()<<endl;
s.pop();
}
}
return 0;
}