結果
問題 | No.2026 Yet Another Knapsack Problem |
ユーザー |
|
提出日時 | 2022-07-30 00:29:14 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 376 ms / 10,000 ms |
コード長 | 1,082 bytes |
コンパイル時間 | 949 ms |
コンパイル使用メモリ | 79,228 KB |
最終ジャッジ日時 | 2025-01-30 16:23:09 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
#include <iostream>#include <limits>#include <vector>constexpr long long inf = std::numeric_limits<long long>::max() / 2;int main() {std::ios::sync_with_stdio(false);std::cin.tie(nullptr);int n;std::cin >> n;std::vector<int> c(n + 1);std::vector<long long> v(n + 1);for (int i = 1; i <= n; ++i) {std::cin >> c[i] >> v[i];}std::vector dp(n + 1, std::vector<long long>(n + 1, -inf));std::fill(std::begin(dp[0]), std::end(dp[0]), 0);for (int w = n; w >= 1; --w) {for (int pw = 1; c[w] > 0; pw <<= 1) {const int k = std::min(pw, c[w]);c[w] -= k;const int dw = k * w;const long long dv = k * v[w];for (int num = n / w; num >= k; --num) {for (int wsum = n; wsum >= dw; --wsum) {dp[num][wsum] = std::max(dp[num][wsum], dp[num - k][wsum - dw] + dv);}}}}for (int num = 1; num <= n; ++num) {std::cout << dp[num][n] << '\n';}return 0;}