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