이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int W, n;
cin >> W >> n;
vector<array<long long, 2>> a; // { (value, weight) }
for (int i = 0; i < n; i++) {
long long v, w;
int cnt;
cin >> v >> w >> cnt;
int hbp = 31 - __builtin_clz(cnt);
for (int k = 0; k < hbp; k++) {
a.push_back({ v * (1 << k), w * (1 << k) });
}
cnt -= (1 << hbp) - 1;
for (int k = 0; k < 31; k++) {
if (cnt >> k & 1) {
a.push_back({ v * (1 << k), w * (1 << k) });
}
}
}
n = a.size();
vector<vector<long long>> dp(n + 1, vector<long long>(W + 1, 0));
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1];
for (int w = 0; w <= W; w++) {
if (w - a[i - 1][1] >= 0) {
dp[i][w] = max( dp[i][w], dp[i - 1][w - a[i - 1][1]] + a[i - 1][0] );
}
}
}
cout << *max_element(dp[n].begin(), dp[n].end()) << "\n";
}
# | 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... |