結果
問題 | No.2840 RGB Plates |
ユーザー |
![]() |
提出日時 | 2024-08-09 22:18:09 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,588 ms / 2,000 ms |
コード長 | 1,402 bytes |
コンパイル時間 | 3,166 ms |
コンパイル使用メモリ | 256,396 KB |
実行使用メモリ | 11,392 KB |
最終ジャッジ日時 | 2024-09-17 13:59:35 |
合計ジャッジ時間 | 28,099 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; using tll=tuple<ll,ll,ll>; using ld=long double; const ll INF=(1ll<<60); #define rep(i,n) for (ll i=0;i<(ll)(n);i++) #define replr(i,l,r) for (ll i=(ll)(l);i<(ll)(r);i++) #define all(v) v.begin(),v.end() #define len(v) ((ll)v.size()) template<class T> inline bool chmin(T &a,T b){ if(a>b){ a=b; return true; } return false; } template<class T> inline bool chmax(T &a,T b){ if(a<b){ a=b; return true; } return false; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; vector<ll> a(n); ll mx=0; rep(i,n){ cin >> a[i]; chmax(mx,a[i]); } mt19937 mt; shuffle(all(a),mt); if(n<=500) mx=2e4; else mx=1e4; vector<vector<ll>> dp(mx*2+1,vector<ll>(8,-INF)),ndp=dp; dp[mx][0]=0; rep(i,n){ rep(j,mx*2+1){ rep(k,8){ if(dp[j][k]==-INF) continue; chmax(ndp[j][k|1],dp[j][k]+a[i]); if(j+a[i]<=mx*2) chmax(ndp[j+a[i]][k|2],dp[j][k]); if(0<=j-a[i]) chmax(ndp[j-a[i]][k|4],dp[j][k]); } } swap(dp,ndp); ndp.assign(mx*2+1,vector<ll>(8,-INF)); } if(dp[mx][7]==-INF) cout << "-1\n"; else cout << dp[mx][7] << '\n'; }