結果
| 問題 |
No.2866 yuusaan's Knapsack
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-08-30 22:15:20 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 7 ms / 2,000 ms |
| コード長 | 1,271 bytes |
| コンパイル時間 | 2,237 ms |
| コンパイル使用メモリ | 204,548 KB |
| 最終ジャッジ日時 | 2025-02-24 03:07:01 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
#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;
}