結果

問題 No.2866 yuusaan's Knapsack
ユーザー cho435cho435
提出日時 2024-08-30 22:30:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 329 ms / 2,000 ms
コード長 1,622 bytes
コンパイル時間 4,073 ms
コンパイル使用メモリ 267,900 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-08-31 01:28:58
合計ジャッジ時間 10,776 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
6,816 KB
testcase_01 AC 115 ms
6,940 KB
testcase_02 AC 116 ms
6,944 KB
testcase_03 AC 116 ms
6,944 KB
testcase_04 AC 112 ms
6,944 KB
testcase_05 AC 112 ms
6,940 KB
testcase_06 AC 168 ms
6,944 KB
testcase_07 AC 152 ms
6,940 KB
testcase_08 AC 237 ms
6,944 KB
testcase_09 AC 266 ms
6,944 KB
testcase_10 AC 129 ms
6,944 KB
testcase_11 AC 329 ms
6,940 KB
testcase_12 AC 268 ms
6,944 KB
testcase_13 AC 323 ms
6,940 KB
testcase_14 AC 178 ms
6,940 KB
testcase_15 AC 142 ms
6,944 KB
testcase_16 AC 198 ms
6,944 KB
testcase_17 AC 329 ms
6,944 KB
testcase_18 AC 320 ms
6,940 KB
testcase_19 AC 183 ms
6,944 KB
testcase_20 AC 308 ms
6,940 KB
testcase_21 AC 259 ms
6,940 KB
testcase_22 AC 214 ms
6,944 KB
testcase_23 AC 324 ms
6,940 KB
testcase_24 AC 117 ms
6,944 KB
testcase_25 AC 252 ms
6,944 KB
testcase_26 AC 115 ms
6,944 KB
testcase_27 AC 327 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)

template<typename T>
bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
template<typename T>
bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }

struct io_setup {
	io_setup() {
		ios::sync_with_stdio(false);
		std::cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} io_setup;

using mint=atcoder::modint998244353;

int main(){
	int n,w;
	cin>>n>>w;
	vector<pair<int,int>> vw(n);
	for(auto&[a,b]:vw) cin>>b>>a;
	int sz=1e4+10;
	vector<pair<ll,mint>> dpm(sz,{-1e18,0}),dp2(sz+w,{-1e18,0});
	dpm.at(0)={0,1};
	dp2.at(0)={0,1};
	for(auto[a,b]:vw){
		if(a<0){
			auto ndp=dpm;
			rep(i,0,sz){
				if(i-a>=sz) break;
				if(chmax(ndp.at(i-a).first,dpm.at(i).first+b)){
					ndp.at(i-a).second=dpm.at(i).second;
				}else if(ndp.at(i-a).first==dpm.at(i).first+b){
					ndp.at(i-a).second+=dpm.at(i).second;
				}
			}
			swap(dpm,ndp);
		}else{
			auto ndp=dp2;
			rep(i,0,sz+w){
				if(i+a>=sz+w) break;
				if(chmax(ndp.at(i+a).first,dp2.at(i).first+b)){
					ndp.at(i+a).second=dp2.at(i).second;
				}else if(ndp.at(i+a).first==dp2.at(i).first+b){
					ndp.at(i+a).second+=dp2.at(i).second;
				}
			}
			swap(dp2,ndp);
		}
	}
	ll a=-1e18;
	mint b=0;
	rep(i,0,sz) rep(j,0,sz+w){
		if(j-i>w) break;
		if(dpm.at(i).first+dp2.at(j).first>a){
			a=dpm.at(i).first+dp2.at(j).first;
			b=dpm.at(i).second*dp2.at(j).second;
		}else if(dpm.at(i).first+dp2.at(j).first==a){
			b+=dpm.at(i).second*dp2.at(j).second;
		}
	}
	cout<<a<<" "<<b.val()<<endl;
}
0