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 mp make_pair
#define ll long long
using namespace std;
vector<pair<int, priority_queue<int>>> compression(int n, int s, vector<int>& v, vector<int>& w, vector<int>& k) {
map<int, priority_queue<int>> comp;
for (int i = 0; i < n; ++i) {
while ((int)comp[w[i]].size() < s/w[i] && k[i] > 0) {
comp[w[i]].push(-v[i]);
--k[i];
}
while (k[i] > 0 && -comp[w[i]].top() < v[i]) {
comp[w[i]].pop();
comp[w[i]].push(-v[i]);
--k[i];
}
}
vector<pair<int, priority_queue<int>>> products;
for (auto& x : comp) {
priority_queue<int> pq;
while (!x.second.empty()) {
pq.push(-x.second.top());
x.second.pop();
}
products.pb(mp(x.first, pq));
}
return products;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int s, n;
cin >> s >> n;
vector<int> v(n), w(n), k(n);
for (int i = 0; i < n; ++i) {
cin >> v[i] >> w[i] >> k[i];
}
vector<pair<int, priority_queue<int>>> products = compression(n, s, v, w, k);
int N = (int)products.size();
vector<ll> prev_dp(s+1, 0), curr_dp(s+1, 0);
for (int i = 0; i < N; ++i) {
curr_dp = vector<ll>(s+1, 0);
for (int j = 1; j <= s; ++j) {
curr_dp[j] = prev_dp[j];
int cnt = 1;
ll value = 0;
priority_queue<int> pq = products[i].second;
while (cnt*products[i].first <= j && !pq.empty()) {
value += pq.top();
pq.pop();
curr_dp[j] = max(curr_dp[j], prev_dp[j-cnt*products[i].first] + value);
++cnt;
}
}
prev_dp = curr_dp;
}
cout << curr_dp[s] << '\n';
return 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... |