이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int 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<pair<int, int>, int> comp;
for (int i = 0; i < n; ++i) {
comp[{v[i], w[i]}] += k[i];
}
for (auto& x : comp) {
if (x.second >= 3) {
comp[{2 * x.first.first, 2*x.first.second}] += (x.second - 1) / 2;
if (x.second % 2) comp[x.first] = 1;
else comp[x.first] = 2;
}
}
map<int, priority_queue<int>> comp2;
for (auto& x : comp) {
if (x.first.second * ((int)comp2[x.first.second].size()+1) > s) continue;
comp2[x.first.second].push(x.first.first);
if (x.second == 2)
comp2[x.first.second].push(x.first.first);
}
vector<pair<int, priority_queue<int>>> products;
for (auto& x : comp2) {
products.pb(mp(x.first, x.second));
}
return products;
}
signed 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<int> prev_dp(s+1, 0), curr_dp(s+1, 0);
for (int i = 0; i < N; ++i) {
curr_dp = vector<int>(s+1, 0);
for (int j = 1; j <= s; ++j) {
curr_dp[j] = prev_dp[j];
int cnt = 1, 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... |