이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int limit, type_num;
cin >> limit >> type_num;
map<int, vector<pair<int, int>>> by_weight;
for (int i = 1; i <= type_num; ++i) {
int value, weight, amount;
cin >> value >> weight >> amount;
by_weight[weight].emplace_back(value, amount);
}
const i64 inf = 1e18;
vector best(by_weight.size() + 1, vector<i64>(limit + 1, -inf));
best[0][0] = 0;
int at = 1;
for (auto &[weight, items] : by_weight) {
sort(items.begin(), items.end(), greater<pair<int, int>>());
for (int sum = 0; sum <= limit; ++sum) {
best[at][sum] = best[at - 1][sum];
int copies = 0, type_at = 0, curr_used = 0;
i64 profit = 0;
while ((copies + 1) * weight <= sum && type_at < (int) items.size()) {
copies += 1;
profit += items[type_at].first;
best[at][sum] = max(best[at][sum], best[at - 1][sum - copies * weight] + profit);
curr_used += 1;
if (curr_used == items[type_at].second) {
curr_used = 0;
type_at += 1;
}
}
}
at += 1;
}
cout << *max_element(best.back().begin(), best.back().end()) << '\n';
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# | 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... |