結果

問題 No.2741 Balanced Choice
ユーザー umezoumezo
提出日時 2024-04-20 11:09:26
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 1,326 bytes
コンパイル時間 4,521 ms
コンパイル使用メモリ 280,716 KB
実行使用メモリ 402,304 KB
最終ジャッジ日時 2024-04-20 11:09:37
合計ジャッジ時間 6,972 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 234 ms
402,008 KB
testcase_01 AC 266 ms
402,048 KB
testcase_02 AC 266 ms
402,096 KB
testcase_03 AC 224 ms
402,012 KB
testcase_04 AC 223 ms
402,208 KB
testcase_05 AC 157 ms
402,048 KB
testcase_06 AC 181 ms
401,988 KB
testcase_07 AC 187 ms
402,304 KB
testcase_08 AC 199 ms
402,176 KB
testcase_09 AC 183 ms
402,176 KB
testcase_10 AC 189 ms
402,164 KB
testcase_11 AC 199 ms
402,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0