이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define mp make_pair
#define F first
#define S second
constexpr int maxs = 2e3 + 5;
int n, s;
vector<pii> items[maxs]; // items[i] = all items (val, ct) with weight i, sort by val
ll vadd[maxs][maxs]; // vadd[i][j] = value added by taking j items of weight i
ll dp[maxs][maxs]; // dp[i][j] = best value from i-sized basket w/ only items of weight <= j
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
cin >> s >> n;
for (int i = 1; i <= n; i++) {
int v, w, k;
cin >> v >> w >> k;
items[w].push_back(mp(v, k));
}
for (int i = 1; i <= s; i++) {
sort(items[i].rbegin(), items[i].rend());
int ct = 1;
for (pii it : items[i]) {
for (int j = 0; j < it.S && ct <= s / i; j++, ct++) {
vadd[i][ct] = vadd[i][ct - 1] + it.F;
}
}
// cout << i << " : ";
// for (int j = 0; j <= ct; j++) {
// cout << vadd[i][j] << ' ';
// }
// cout << '\n';
}
for (int i = 1; i <= s; i++) {
for (int j = 1; j <= s; j++) {
dp[i][j] = dp[i][j - 1];
for (int k = 1; j * k <= i && vadd[j][k] != 0; k++) {
dp[i][j] = max(dp[i][j], dp[i - j * k][j - 1] + vadd[j][k]);
}
// cout << dp[i][j] << ' ';
}
// cout << '\n';
}
cout << dp[s][s] << '\n';
return 0;
}
# | 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... |