結果

問題 No.2866 yuusaan's Knapsack
ユーザー haihamabossuhaihamabossu
提出日時 2024-08-30 22:15:20
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,271 bytes
コンパイル時間 2,157 ms
コンパイル使用メモリ 211,784 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-26 14:36:23
合計ジャッジ時間 2,968 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/modint>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using mint = atcoder::modint998244353;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const ll INF = 1e18, MX = 20020;
void solve() {
  ll n, W;
  cin >> n >> W;
  vector<pair<ll, ll>> wv(n);
  rep(i, n) cin >> wv[i].second >> wv[i].first;
  sort(wv.begin(), wv.end());
  vector<pair<ll, mint>> dp(MX + 10, {-INF, -INF});
  dp[MX / 2] = {0, 1};
  for (const auto [w, v] : wv) {
    vector<pair<ll, mint>> nex = dp;
    rep(pw, MX) {
      ll nw = pw + w;
      if (0 <= nw && nw <= MX) {
        if (nex[nw].first < dp[pw].first + v) {
          nex[nw] = {dp[pw].first + v, dp[pw].second};
        } else if (nex[nw].first == dp[pw].first + v) {
          nex[nw].second += dp[pw].second;
        }
      }
    }
    swap(dp, nex);
  }
  ll maxv = -INF;
  mint ans = 0;
  for (ll i = 0; i <= W + MX / 2; i++) {
    if (maxv < dp[i].first) {
      maxv = dp[i].first;
      ans = dp[i].second;
    } else if (maxv == dp[i].first) {
      ans += dp[i].second;
    }
  }
  cout << maxv << ' ' << ans.val() << '\n';
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  int T = 1;
  for (int t = 0; t < T; t++) {
    solve();
  }
  return 0;
}
0