# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
976643 | vjudge1 | Knapsack (NOI18_knapsack) | C++17 | 0 ms | 0 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>
using namespace std;
struct Kue {
long long value, weight, stock;
};
int main() {
int s, n;
long long ans = 0;
cin >> s >> n;
Kue kue[n];
for (int i = 0; i < n; i++) {
cin >> kue[i].value >> kue[i].weight >> kue[i].stock;
}
sort(kue, kue + n, [](const Kue &a, const Kue &b) {
return a.value * b.weight > b.value * a.weight;
});
int k;
for (int i = 0; i < n; i++) {
if (kue[i].weight > s) continue;
k = min(s / kue[i].weight, kue[i].stock);
ans += k * kue[i].value;
s -= k * kue[i].weight;
}
cout << ans;
}