# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1047923 | vjudge1 | Knapsack (NOI18_knapsack) | C++17 | 31 ms | 35012 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 pii = pair<int, int>;
using i64 = long long;
const int MAXN = 1e5 + 5;
const int MAXV = 2e3 + 5;
#define ALL(a) (a).begin(), (a).end()
struct PRODUCT {
int c, w, x;
PRODUCT(int c = 0, int w = 0, int x = 0) : c(c), w(w), x(x) {}
};
int N, S;
PRODUCT a[MAXN];
vector<pii> g[MAXV];
vector<int> b[MAXV];
i64 dp[MAXV][MAXV];
namespace subtask {
const int MAXN = 105;
const int MAXV = 2e3 + 5;
i64 dp[MAXN][MAXV];
void solution() {
for(int i = 1; i <= N; i++) {
auto [c, w, x] = a[i];
for(int j = 0; j <= S; j++) dp[i][j] = dp[i - 1][j];
for(int z = 0; z <= x and z * w <= S; z++) {
for(int j = S; j >= w * z; j--)
dp[i][j] = max(dp[i][j], dp[i - 1][j - w * z] + 1LL * c * z);
}
}
i64 ans = 0;
for(int i = 1; i <= S; i++)
ans = max(ans, dp[N][i]);
cout << ans;
}
}
signed main() {
#define TASK "code"
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 >> S >> N;
for(int i = 1; i <= N; i++) cin >> a[i].c >> a[i].w >> a[i].x;
for(int i = 1; i <= N; i++) {
auto [c, w, x] = a[i];
g[w].emplace_back(c, x);
}
for(int i = 1; i <= S; i++) sort(ALL(g[i]), [&](const pii x, const pii y) {
if(x.first != y.first) return x.first > y.first;
return x.second > y.second;
});
for(int i = 1; i <= S; i++) {
if(g[i].empty()) continue;
int j = 0;
for(int z = 1; z <= S; z++) {
if(g[i][j].second == 0)
j++;
if(j == g[i].size()) break;
b[i].emplace_back(g[i][j].first);
g[i][j].second--;
if(b[i].size() * i > S) break;
}
}
// for(int i = 1; i <= S; i++) {
// if(b[i].empty()) continue;
// cout << i << ": ";
// for(int x : b[i]) cout << x << " ";
// cout << "\n";
// }
for(int i = 1; i <= S; i++) {
i64 cur = 0;
for(int &x : b[i]) {
cur += x;
x = cur;
}
}
for(int w = 1; w <= S; w++) {
for(int j = 0; j <= S; j++) dp[w][j] = dp[w - 1][j];
for(int i = 0; i < b[w].size(); i++) {
for(int j = S; j >= (i + 1) * w; j--)
dp[w][j] = max(dp[w][j], dp[w - 1][j - (i + 1) * w] + b[w][i]);
}
}
i64 ans = 0;
for(int j = 0; j <= S; j++) ans = max(ans, dp[S][j]);
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... |