This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: 3omar_ahmed
* date: 09-06-2023
* ya rb awsl CM :)
*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std ;
#define int long long
#define endl '\n'
#define all(a) a.begin() , a.end()
#define alr(a) a.rbegin() , a.rend()
int x, n;
vector < vector < int >> dp;
vector < vector < pair < int , int >>> a;
int DP(int idx, int still) {
if(still == 0 || idx == n) return 0;
if(dp[idx][still] != -1) return dp[idx][still];
int op1 = DP(idx + 1, still);
int op2 = 0, sum = 0, val = 0;
for(int i = 0 ; i < a[idx].size() ; i++) {
for(int j = 0 ; j < a[idx][i].second ; j++) {
sum += idx + 1, val += a[idx][i].first;
if(sum > still) break;
op2 = max(op2, DP(idx + 1, still - sum) + val);
}
}
return dp[idx][still] = max(op1, op2);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
cin >> x >> n;
a = vector < vector < pair < int , int >>> (x + 1);
for(int i = 0 ; i < n ; i++) {
int b, c, d;
cin >> b >> c >> d;
a[c - 1].push_back({b, d});
}
dp = vector < vector < int >> (x + 2, vector < int > (x + 2, -1));
for(auto &i : a) sort(alr(i));
cout << DP(0, x) << endl;
return 0 ;
}
Compilation message (stderr)
knapsack.cpp: In function 'long long int DP(long long int, long long int)':
knapsack.cpp:21:23: 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]
21 | for(int i = 0 ; i < a[idx].size() ; i++) {
| ~~^~~~~~~~~~~~~~~
# | 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... |