結果
問題 |
No.1606 Stuffed Animals Keeper
|
ユーザー |
![]() |
提出日時 | 2021-07-16 22:42:14 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,951 bytes |
コンパイル時間 | 1,225 ms |
コンパイル使用メモリ | 121,080 KB |
最終ジャッジ日時 | 2025-01-23 02:26:26 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 WA * 24 |
ソースコード
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<bitset> #include<set> #include<map> #include<stack> #include<queue> #include<deque> #include<list> #include<iomanip> #include<cmath> #include<cstring> #include<functional> #include<cstdio> #include<cstdlib> #include<numeric> #include<ctime> //#include<atcoder/all> using namespace std; //using namespace atcoder; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define INF 2e9 //#define MOD 1000000007 #define MOD 998244353 #define LINF (long long)4e18 #define jck 3.141592 #define PI acos(-1.0) const double EPS = 1e-10; using ll = long long; using Pi = pair<int,int>; using Pl = pair<ll,ll>; //using mint = modint998244353; int dh[] = {1,0,-1,0}; int dw[] = {0,1,0,-1}; ll dp1[5010][5010]; ll dp2[5010][5010]; int main(){ int n; cin >> n; vector<int> a(n); rep(i,n) cin >> a[i]; int cnt0 = 0,cnt1 = 0; vector<Pi> v; rep(i,n){ if(a[i] == 2){ v.emplace_back(cnt0,cnt1); cnt0 = 0; cnt1 = 0; } else if(a[i] == 0) cnt0++; else cnt1++; } v.emplace_back(cnt0,cnt1); int m = v.size(); rep(i,5010)rep(j,5010){ dp1[i][j] = LINF; dp2[i][j] = LINF; } dp1[0][0] = 0; dp2[0][0] = 0; rep(i,m){ int u = v[i].first; int w = v[i].second; rep(j,n+1){ if(dp1[i][j] == LINF) continue; dp1[i+1][j+u] = min(dp1[i+1][j+u],dp1[i][j]+u); if(j >= w) dp1[i+1][j-w] = min(dp1[i+1][j-w],dp1[i][j]); } rep(j,n+1){ if(dp2[i][j] == LINF) continue; dp2[i+1][j+w] = min(dp2[i+1][j+w],dp2[i][j]+w); if(j >= u) dp2[i+1][j-u] = min(dp2[i+1][j-u],dp2[i][j]); } } if(dp1[m][0] == LINF and dp2[m][0] == LINF) cout << -1 << endl; else cout << min(dp1[m][0],dp2[m][0]) << endl; }