結果
| 問題 | No.2741 Balanced Choice |
| コンテスト | |
| ユーザー |
umezo
|
| 提出日時 | 2024-04-20 11:09:26 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 364 ms / 2,000 ms |
| コード長 | 1,326 bytes |
| コンパイル時間 | 3,280 ms |
| コンパイル使用メモリ | 251,816 KB |
| 実行使用メモリ | 402,176 KB |
| 最終ジャッジ日時 | 2024-10-12 06:48:39 |
| 合計ジャッジ時間 | 6,144 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 10 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
template <class T> using V=vector<T>;
template <class T> using VV=V<V<T>>; //B(n,V<int>(n))
const ll INF=1e18;
ll dp0[5050][5050],dp1[5050][5050];
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
rep(i,5050) rep(j,5050) dp0[i][j]=-INF,dp1[i][j]=-INF;
ll n,ww,d;
cin>>n>>ww>>d;
VV<ll> W(2),V(2);
int c0=0,c1=0;
rep(i,n){
ll t,w,v;
cin>>t>>w>>v;
if(t==0) c0++;
else c1++;
W[t].push_back(w),V[t].push_back(v);
}
dp0[0][0]=0,dp1[0][0]=0;
rep(i,c0){
rep(j,ww+1){
dp0[i+1][j]=max(dp0[i+1][j],dp0[i][j]);
if(j+W[0][i]<=ww) dp0[i+1][j+W[0][i]]=max(dp0[i+1][j+W[0][i]],dp0[i][j]+V[0][i]);
}
}
rep(i,c1){
rep(j,ww+1){
dp1[i+1][j]=max(dp1[i+1][j],dp1[i][j]);
if(j+W[1][i]<=ww) dp1[i+1][j+W[1][i]]=max(dp1[i+1][j+W[1][i]],dp1[i][j]+V[1][i]);
}
}
vector<ll> A(ww+1),B(ww+1);
rep(i,ww+1){
A[i]=dp0[c0][i];
B[i]=dp1[c1][i];
}
ll ans=0;
rep(i,ww+1){
if(A[i]==-INF) continue;
ll ma=-INF;
for(int j=max(0ll,i-d);j<=min(ww,i+d);j++){
if(i+j>ww) continue;
ma=max(ma,B[j]);
}
ans=max(ans,A[i]+ma);
}
cout<<ans<<endl;
return 0;
}
umezo