#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;
}
Compilation message
knapsack.cpp: In function 'int main()':
knapsack.cpp:32:19: warning: unused variable 'weight' [-Wunused-variable]
32 | long long weight = entry.first;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |