# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
959830 | susanthenerd | Knapsack (NOI18_knapsack) | C++98 | 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>
#include <algorithm>
#include <map>
#include <vector>
#include <climits>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int s, n, m = 0;
cin >> s >> n;
vector v(s + 1, vector<pair<int, int>>());
for (int i = 0; i < n; ++i) {
int pret, greu, cnt;
cin >> pret >> greu >> cnt;
if (greu <= s && cnt > 0) {
if (v[greu].empty())
++m;
v[greu].emplace_back(pret, cnt);
}
}
vector dp(m + 1, vector<ll>(s + 1, LLONG_MIN));
dp[0][0] = 0;
for (int a = 0, i = 0; a <= s; ++a) {
if (!v[a].empty()) {
++i;
sort(v[a].begin(), v[a].end(), greater<>());
for (int j = 0; j <= s; ++j) {
dp[i][j] = dp[i - 1][j];
int cnt = 0, tip = 0, used = 0;
ll profit = 0;
while ((cnt + 1) * a <= j && tip < v[a].size()) {
++cnt;
profit += v[a][tip].first;
if (dp[i - 1][j - cnt * a] != LLONG_MIN)
dp[i][j] = max(dp[i][j], dp[i - 1][j - cnt * a] + profit);
++used;
if (used == v[a][tip].second) {
used = 0;
++tip;
}
}
}
}
}
cout << *max_element(dp.back().begin(), dp.back().end()) << '\n';
}