This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, s;
int dp[2005][2005];
vector<pair<int, int> > items[2005];
int32_t main(){
cin >> s >> n;
memset(dp, -1, sizeof dp);
dp[0][0] = 0;
for(int i = 0; i < n; i++){
int v, w, k; cin >> v >> w >> k;
items[w].push_back(make_pair(v, k));
}
for(int i = 1; i <= s; i++){
sort(items[i].rbegin(), items[i].rend());
for(int j = 0; j <= s; j++){
dp[i][j] = dp[i-1][j];
int ptr = 0;
int used = 0;
int cursum = 0;
int tmpused = 0;
while(ptr < items[i].size() && i*(used+1) <= j){
cursum += items[i][ptr].first;
used++;
// printf("%d %d %d %d\n", cursum, j+i*used, dp[i][j]+cursum, dp[i+1][j+i*used]);
if(dp[i-1][j-i*used] != -1){
dp[i][j] = max(dp[i-1][j-i*used]+cursum, dp[i][j]);
}
tmpused++;
if(tmpused == items[i][ptr].second){
tmpused = 0;
ptr++;
}
}
}
}
int ans = -1;
for(int i = 0; i <= s+1; i++){
ans = max(ans, dp[s][i]);
}
cout << ans << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:27:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | while(ptr < items[i].size() && i*(used+1) <= j){
| ~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |