結果

問題 No.2866 yuusaan's Knapsack
コンテスト
ユーザー Tatsu_mr
提出日時 2024-08-30 23:18:29
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,554 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,741 ms
コンパイル使用メモリ 282,176 KB
実行使用メモリ 27,648 KB
最終ジャッジ日時 2026-07-05 14:06:25
合計ジャッジ時間 4,699 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/vector:68,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/functional:66,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:55,
                 from main.cpp:1:
/home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_vector.h:1261:7: note: declared here
 1261 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~

ソースコード

diff #
raw source code

#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