# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1060865 | manhlinh1501 | Knapsack (NOI18_knapsack) | C++17 | 50 ms | 76068 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 <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 105;
const int MAXM = 1e5 + 5;
const int MAXV = 1e3 + 5;
#define ALL(a) (a).begin(), (a).end()
struct event {
int w, v, x;
event(int w = 0, int v = 0, int x = 0) : w(w), v(v), x(x) {}
};
int N, K;
event a[MAXN];
i64 dp[MAXM];
vector<int> g[MAXV];
signed main() {
#define TASK "P1"
if (fopen(TASK ".inp", "r")) {
freopen(TASK ".inp", "r", stdin);
freopen(TASK ".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> K;
for(int i = 1; i <= N; i++) {
auto &[w, v, x] = a[i];
cin >> w >> v >> x;
}
for(int i = 1; i <= N; i++) {
auto [w, v, x] = a[i];
for(int j = 1; j <= x; j++)
g[w].emplace_back(v);
}
for(int i = 1; i < MAXV; i++) {
sort(ALL(g[i]));
reverse(ALL(g[i]));
while(!g[i].empty() and (g[i].size() - 1) * i > K)
g[i].pop_back();
}
memset(dp, -0x3f, sizeof dp);
dp[0] = 0;
for(int i = 1; i < MAXV; i++) {
for(int x : g[i]) {
for(int j = K; j >= i; j--)
dp[j] = max(dp[j], dp[j - i] + x);
}
}
i64 ans = *max_element(dp, dp + K + 1);
cout << ans;
return (0 ^ 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... |