이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
int main() {
int m, n;
cin >> m >> n;
vector<long long> a(n);
vector<long long> w(n);
vector<long long> freq(n);
unordered_map<long long, vector<pair<long long, long long>>> pairs; // (weight, {{value, frequency}})
for (int i = 0; i < n; i++) {
cin >> a[i] >> w[i] >> freq[i];
}
for (int i = 0; i < n; i++) {
if (pairs.find(w[i]) != pairs.end()) {
pairs[w[i]].push_back({a[i], freq[i]});
}
else {
pairs[w[i]] = {{a[i], freq[i]}};
}
}
vector<long long> dp(m + 1, -1);
dp[0] = 0;
for (auto& entry : pairs) {
long long weight = entry.first;
auto& curr = entry.second;
sort(curr.begin(), curr.end(), [](const pair<long long, long long>& x, const pair<long long, long long>& y) {
if (x.first > y.first) {
return 1;
}
else if (x.first < y.first) {
return -1;
}
else {
return 0;
}
});
reverse(curr.begin(), curr.end());
}
for (auto& entry : pairs) {
long long weight = entry.first;
auto& curr = entry.second;
long long sum = 0;
for (const auto& j : curr) {
sum += j.second;
}
for (long long i = m; i >= weight; i--) {
long long f = i / weight;
if (dp[i - min(f, sum) * weight] != -1) {
long long c = dp[i - min(f, sum) * weight];
for (const auto& j : curr) {
long long fr = j.second, val = j.first;
if (fr >= f) {
c += f * val;
f = 0;
break;
}
else {
c += fr * val;
f -= fr;
}
}
dp[i] = max(dp[i], c);
}
}
}
long long max_val = 0;
for (int i = 1; i <= m; i++) {
max_val = max(max_val, dp[i]);
}
cout << max_val << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:32:19: warning: unused variable 'weight' [-Wunused-variable]
32 | long long weight = entry.first;
| ^~~~~~
# | 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... |