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 pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
struct Item { int v, w, k; };
int32_t main() {
setIO();
int s, n;
cin >> s >> n;
vector<Item> v(n);
map<int, vector<pii> > mp;
for(Item &it : v) {
cin >> it.v >> it.w >> it.k;
mp[it.w].push_back({ it.v, it.k });
}
// for(auto &x : mp) {
// cout << x.first << '\n';
// for(auto &el : x.second) {
// cout << "{ " << el.first << ", " << el.second << " } \n";
// }
// }
vector<vector<ll> > dp(mp.size()+1, vector<ll>(s+1, -1));
dp[0][0] = 0;
int id = 1;
for(auto &currVec : mp) {
int currW = currVec.first;
vector<pii> items = currVec.second;
sort(all(items));
reverse(all(items));
for(int j=0; j<=s; j++) {
dp[id][j] = dp[id-1][j];
int currProd = 0;
int totalProducts = 0;
ll gain = 0;
int currBuy = 0;
while(currProd < sz(items) && (totalProducts + 1) * currW <= j) {
gain += items[currProd].first;
totalProducts++;
if(j >= totalProducts * currW && dp[id-1][j-totalProducts*currW] != -1) {
dp[id][j] = max(
dp[id][j],
dp[id-1][j-totalProducts*currW] + gain
);
}
if(totalProducts == items[currProd].second) {
currProd++;
totalProducts = 0;
}
}
}
id++;
}
ll ans = 0;
for(int i=0; i<=s; i++) ans = max(ans, dp[mp.size()][i]);
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:69:17: warning: unused variable 'currBuy' [-Wunused-variable]
69 | int currBuy = 0;
| ^~~~~~~
# | 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... |