이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
struct item {
size_t weight = 0;
size_t value = 0;
size_t count = 0;
};
size_t dp[100001];
std::vector<item> filter_items(const std::vector<item>& items, size_t capacity) noexcept {
std::vector<item> filtered;
filtered.reserve(items.size());
std::unordered_map<size_t, size_t> counts;
for (const item& item : items) {
if (counts[item.weight] * item.weight > capacity) {
continue;
}
counts[item.weight] += item.count;
filtered.push_back(item);
}
return filtered;
}
std::vector<item> prepare_items(std::vector<item>& items, size_t capacity) noexcept {
const auto cmp = [](const item& lhs, const item& rhs) {
if (lhs.weight == rhs.weight) {
return lhs.value > rhs.value;
}
return lhs.weight < rhs.weight;
};
std::sort(items.begin(), items.end(), cmp);
return filter_items(items, capacity);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
size_t amount_of_types = 0, capacity = 0;
std::cin >> capacity >> amount_of_types;
std::vector<item> items(amount_of_types);
for (size_t i = 0; i < amount_of_types; ++i) {
size_t weight = 0, value = 0, count = 0;
std::cin >> value >> weight >> count;
items[i] = {weight, value, count};
}
items = prepare_items(items, capacity);
size_t max = 0;
for (const auto& item : items) {
for (size_t cap = capacity; cap > 0; --cap) {
size_t limit = std::min(item.count, cap / item.weight);
for (size_t count = 1; count <= limit; ++count) {
const auto alt = dp[cap - count * item.weight] + count * item.value;
dp[cap] = std::max(dp[cap], alt);
}
max = std::max(max, dp[cap]);
}
}
std::cout << max << '\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... |