# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
831258 | jmao142857 | Knapsack (NOI18_knapsack) | C++14 | 365 ms | 8876 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 <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <unordered_set>
#include <set>
#include <queue>
using namespace std;
using ll = long long;
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int S, N; cin >> S >> N;
map<int, vector<vector<int>>> nums;
for (int i = 0; i < N; i++) {
int a, b, c; cin >> a >> b >> c;
nums[b].push_back({a, c});
}
for (int j = 1; j <= S; j++) sort(nums[j].begin(), nums[j].end());
vector<ll> dp(S + 1);
for (int j = 1; j <= S; j++) {
int pointer = nums[j].size() - 1;
int count = 0;
while (pointer >= 0 && count <= (S / j)) {
for (int i = S; i >= 0; i--) {
if (i >= j) dp[i] = max(dp[i - j] + nums[j][pointer][0], dp[i]);
}
nums[j][pointer][1]--; count++;
if (nums[j][pointer][1] == 0) pointer--;
}
}
ll ans = 0;
for (int i = 0; i <= S; i++) ans = max(ans, dp[i]);
cout << ans;
}
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... |