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>
using namespace std;
typedef pair<int, int> ii;
typedef vector<pair<int, int>> vii;
typedef vector<int> vi;
typedef long long ll;
#define PB push_back
#define MP make_pair
#define FOR(i, x, y) for (int i = x; i < y ; i ++)
int main(){
int s, n;
cin >> s >> n;
vector<pair<ll, ll>> weights[s + 1];
FOR(i, 0, n){
ll v, w, k;
cin >> v >> w >> k;
weights[w].PB(MP(v, k));
}
FOR(i, 0, s){
sort(weights[i].rbegin(), weights[i].rend());
}
ll dp[s + 1] = { 0 };
ll ans = 0;
FOR(j, 1, s + 1){
int indx = 0;
ll prev = 0;
FOR(cnt, 1, s/j + 1){
for(int i = s; i >= 1; i --){
//cout << dp[s] << '\n';
if (j > i || indx >= weights[j].size()){
break;
}
if (cnt - prev > weights[j][indx].second){
indx ++;
prev += weights[j][indx].second;
}
if (indx >= weights[j].size()){
break;
}
dp[i] = max(dp[i], dp[i - j] + weights[j][indx].first);
}
}
}
FOR(i, 1, s + 1){
//cout << i << " : " << dp[i] << '\n';
ans = max(ans, dp[i]);
}
cout << ans << '\n';
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:31:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if (j > i || indx >= weights[j].size()){
| ~~~~~^~~~~~~~~~~~~~~~~~~~
knapsack.cpp:38:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if (indx >= weights[j].size()){
| ~~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |