답안 #976579

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
976579 2024-05-06T18:38:50 Z vjudge1 Knapsack (NOI18_knapsack) C++17
0 / 100
1 ms 348 KB
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define pll pair<long long, long long>
using namespace std;


void solve(){
	ll s,n;
	cin >> s >> n;
	vector<pll> arr[s+5];
	for(int i=1;i<=n;i++){
		ll a,b,c;
		cin >> a >> b >> c;
		arr[b].pb({a,c});
	}
	vector<pll> v;
	for(int i=1;i<=s;i++){
		if(arr[i].size()==0) continue;
		ll now = s/i;
		sort(arr[i].begin(),arr[i].end(),greater<pll>());
		ll sz = arr[i].size();
		for(int j=0;j<sz;j++){
			if(now==0) break;
			while(arr[i][j].se>0&&now>0){
				ll val = arr[i][j].fi;
				v.pb({i,val});
				arr[i][j].se--;
				now--;
			}
		}
	}
	ll dp[s+5] = {0};
	ll INF = -1;
	for(int i=0;i<=s+1;i++){
		dp[i] = INF;
	}
	dp[0] = 0;
	for(auto i:v){
		for(int j=s;j>=i.fi;j--){
			dp[j] = max(dp[j],dp[j-1]);
			if(dp[j-i.fi]==INF) continue;
			dp[j] = max(dp[j],dp[j-i.fi]+i.se);
		}
	}
	cout << dp[s] << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tc = 1;
//	cin >> tc;
	while(tc--){
	   solve();
	}
}

# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -