結果

問題 No.1606 Stuffed Animals Keeper
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-06-10 20:17:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,096 bytes
コンパイル時間 1,424 ms
コンパイル使用メモリ 111,224 KB
最終ジャッジ日時 2025-02-14 01:20:44
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25 WA * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;
using ll = long long;

int main(){

    int N, A, K, x=0, y=0, S=0;
    cin >> N;
    vector<int> L, M;

    for (int i=0; i<N; i++){
        cin >> A;
        if (A == 0) x++;
        else if (A == 1) x++, y++;
        else{
            if (x == 0) continue;
            L.push_back(x); M.push_back(y);
            x = 0; y = 0;
        }
    }
    if (x > 0) L.push_back(x), M.push_back(y);
    K = L.size();
    for (int i=0; i<K; i++) S += L[i]-M[i];
    if (K == 0){
        cout << "Yes" << endl;
        return 0;
    }

    vector<vector<int>> dp(K+1, vector<int>(S+1, 1e9));
    dp[0][0] = 0;
    
    for (int i=1; i<=K; i++){
        for (int j=0; j<=S; j++){
            if (j-L[i-1]>=0) dp[i][j] = min(dp[i-1][j], dp[i-1][j-L[i-1]]+M[i-1]);
        }
    }

    if (dp[K][S] == 1e9) dp[K][S] = -1;
    cout << dp[K][S] << endl;

    return 0;
}
0