結果

問題 No.2866 yuusaan's Knapsack
ユーザー eve__fuyukieve__fuyuki
提出日時 2024-08-30 23:09:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,233 bytes
コンパイル時間 2,138 ms
コンパイル使用メモリ 213,076 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-08-31 01:29:13
合計ジャッジ時間 3,215 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,812 KB
testcase_01 AC 3 ms
6,944 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 14 ms
6,940 KB
testcase_07 AC 11 ms
6,940 KB
testcase_08 AC 13 ms
6,944 KB
testcase_09 AC 13 ms
6,944 KB
testcase_10 AC 14 ms
6,944 KB
testcase_11 AC 10 ms
6,940 KB
testcase_12 AC 12 ms
6,944 KB
testcase_13 AC 11 ms
6,944 KB
testcase_14 AC 13 ms
6,940 KB
testcase_15 AC 14 ms
6,944 KB
testcase_16 AC 11 ms
6,940 KB
testcase_17 AC 14 ms
6,940 KB
testcase_18 AC 10 ms
6,944 KB
testcase_19 AC 15 ms
6,940 KB
testcase_20 AC 12 ms
6,940 KB
testcase_21 AC 12 ms
6,940 KB
testcase_22 AC 12 ms
6,940 KB
testcase_23 AC 14 ms
6,940 KB
testcase_24 AC 12 ms
6,944 KB
testcase_25 AC 13 ms
6,944 KB
testcase_26 AC 11 ms
6,940 KB
testcase_27 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void fast_io() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
}

#include <atcoder/modint>
using mint = atcoder::modint998244353;
int main() {
	fast_io();
	int n, W;
	cin >> n >> W;
	vector<int> v(n), w(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i] >> w[i];
	}
	int offset = 20000;
	int ma = 50000;
	const long long INF = 1e18;
	vector<long long> dp(ma + 1, -INF);
	dp[offset] = 0;
	vector<mint> cnt(ma + 1, 0);
	cnt[offset] = 1;
	for (int i = 0; i < n; i++) {
		vector<long long> dp_new = dp;
		vector<mint> cnt_new = cnt;
		for (int j = 0; j <= ma; j++) {
			if (dp[j] == -INF) {
				continue;
			}
			if (j + w[i] >= 0 && j + w[i] <= ma) {
				if (dp_new[j + w[i]] < dp[j] + v[i]) {
					dp_new[j + w[i]] = dp[j] + v[i];
					cnt_new[j + w[i]] = cnt[j];
				} else if (dp_new[j + w[i]] == dp[j] + v[i]) {
					cnt_new[j + w[i]] += cnt[j];
				}
			}
		}
		dp = dp_new;
		cnt = cnt_new;
	}
	long long v_max = -INF;
	for (int i = 0; i <= ma; i++) {
		if (i - offset <= W) {
			v_max = max(v_max, dp[i]);
		}
	}
	mint ans = 0;
	for (int i = 0; i <= ma; i++) {
		if (i - offset <= W && dp[i] == v_max) {
			ans += cnt[i];
		}
	}
	cout << v_max << " " << ans.val() << endl;
}
0