# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
744624 | tibiCoder | Knapsack (NOI18_knapsack) | C++17 | 125 ms | 6176 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <set>
#include <vector>
#include <unordered_map>
#include <climits>
#include <fstream>
#include <algorithm>
using namespace std;
#define int long long
#define MOD 1000000007
#define MAX_S 2001
#define loop3(x, vec) for (auto (x):(vec))
#define loop2(x, s, n) for (int (x) = (s); (x) < (n); ++(x))
#define loop(x, n) for (int (x) = 0; (x) < (n); ++(x))
#define inp(n, p) for (int (x) = 0; (x) < (n); ++(x)) cin >> (p)[(x)];
struct Item {
int value;
int weight;
int k;
bool operator<(const Item& other) const {
return value > other.value;
}
};
signed main() {
int w, n;
cin >> w >> n;
vector<vector<Item>> itemsForWeight(MAX_S);
loop(i, n) {
Item item;
cin >> item.value >> item.weight >> item.k;
itemsForWeight[item.weight].emplace_back(item);
}
loop(i, MAX_S) {
std::sort(itemsForWeight[i].begin(), itemsForWeight[i].end());
}
vector<int> dp(w+1, 0);
vector<int> tempDp(w+1, 0);
for (auto& items : itemsForWeight) {
tempDp = dp;
loop2(targetSum, 1, w+1) {
int index = 0;
int currentBucket = 0;
int currentSum = 0;
int currentValue = 0;
while (currentBucket < items.size()) {
currentSum += items[currentBucket].weight;
if (currentSum > targetSum) break;
currentValue += items[currentBucket].value;
tempDp[targetSum] = max(tempDp[targetSum], dp[targetSum-currentSum]+currentValue);
if (++index >= items[currentBucket].k) {
index = 0;
++currentBucket;
}
}
}
dp = tempDp;
}
cout << *std::max_element(dp.begin(), dp.end()) << endl;
return 0;
}
Compilation message (stderr)
# | 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... |