結果

問題 No.2866 yuusaan's Knapsack
ユーザー Tatsu_mr
提出日時 2024-08-30 23:18:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,554 bytes
コンパイル時間 4,502 ms
コンパイル使用メモリ 256,708 KB
最終ジャッジ日時 2025-02-24 03:24:04
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 WA * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:16:12: warning: ignoring return value of ‘std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = long long int; _Alloc = std::allocator<long long int>; reference = long long int&; size_type = long unsigned int]’, declared with attribute ‘nodiscard’ [-Wunused-result]
   16 |         w[i];
      |            ^
In file included from /usr/include/c++/13/vector:66,
                 from /usr/include/c++/13/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:53,
                 from main.cpp:1:
/usr/include/c++/13/bits/stl_vector.h:1126:7: note: declared here
 1126 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;

using lint = long long;
using mint = atcoder::modint998244353;

lint INF = 2000000000;

int main() {
    int n, W;
    cin >> n >> W;
    vector<lint> v(n), w(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i] >> w[i];
        w[i];
    }
    vector<vector<lint>> dp(n + 1, vector<lint>(20010, -INF));
    dp[0][10000] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 20010; j++) {
            dp[i][j] = max(dp[i][j], dp[i - 1][j]);
            if (0 <= j - w[i - 1] && j - w[i - 1] < 20010) {
                dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
            }
        }
    }
    lint ans = -INF;
    for (int j = 0; j <= W + 10000; j++) {
        ans = max(ans, dp[n][j]);
    }
    
    vector<vector<mint>> cnt(n + 1, vector<mint>(20010));
    for (int j = 0; j < 20010; j++) {
        if (dp[n][j] == ans) {
            cnt[n][j] = 1;
        }
    }
    for (int i = n; i >= 1; i--) {
        for (int j = 0; j < 20010; j++) {
            if (0 <= j - w[i - 1] && j - w[i - 1] < 20010) {
                if (dp[i - 1][j] == dp[i][j]) {
                    cnt[i - 1][j] += cnt[i][j];
                }
                if (dp[i - 1][j - w[i - 1]] == dp[i][j] - v[i - 1]) {
                    cnt[i - 1][j - w[i - 1]] += cnt[i][j];
                }
            }
        }
    }
    
    mint ans2 = 0;
    for (int j = 0; j < 20010; j++) {
        ans2 += cnt[0][j];
    }
    
    cout << ans << " " << ans2.val() << endl;
}
0