跳到正文

个人记录

yzy

_yanny_约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年7月26日
更新
2026年7月26日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int t,n,m;
int w[5005],c[5005];
ll v[5005],dp[5005];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n>>m;
		vector<int> a;
		for(int i=1;i<=n;i++){
			cin>>w[i]>>v[i]>>c[i];
			if(c[i]==-1){
				a.push_back(i);
			}
		}
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++){
			if(c[i]==1){
				for(int j=m;j>=w[i];j--){
					dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
				}
			}
		}
		ll ans=0;
		for(int i=0;i<=m;i++){
			ans=max(ans,dp[i]);
		}
		for(int i:a){
			for(int j=0;j*w[i]<=m;j++){
				ans=max(ans,dp[m-j*w[i]]+1LL*j*v[i]);
			}
		}
		cout<<ans<<'\n';
	}
	return 0;
}
原文