結果

問題 No.1606 Stuffed Animals Keeper
ユーザー tamatotamato
提出日時 2021-07-16 22:52:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 124 ms / 3,000 ms
コード長 850 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 87,128 KB
実行使用メモリ 77,772 KB
最終ジャッジ日時 2023-09-20 15:16:52
合計ジャッジ時間 7,136 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,244 KB
testcase_01 AC 73 ms
71,264 KB
testcase_02 AC 72 ms
71,528 KB
testcase_03 AC 124 ms
77,572 KB
testcase_04 AC 109 ms
77,328 KB
testcase_05 AC 92 ms
76,316 KB
testcase_06 AC 97 ms
76,840 KB
testcase_07 AC 105 ms
77,600 KB
testcase_08 AC 104 ms
77,312 KB
testcase_09 AC 95 ms
76,796 KB
testcase_10 AC 95 ms
76,584 KB
testcase_11 AC 87 ms
76,232 KB
testcase_12 AC 99 ms
77,212 KB
testcase_13 AC 74 ms
75,684 KB
testcase_14 AC 75 ms
76,152 KB
testcase_15 AC 80 ms
76,232 KB
testcase_16 AC 115 ms
77,772 KB
testcase_17 AC 117 ms
77,644 KB
testcase_18 AC 119 ms
77,436 KB
testcase_19 AC 115 ms
77,416 KB
testcase_20 AC 116 ms
77,404 KB
testcase_21 AC 90 ms
76,208 KB
testcase_22 AC 88 ms
76,364 KB
testcase_23 AC 90 ms
76,528 KB
testcase_24 AC 90 ms
76,028 KB
testcase_25 AC 90 ms
76,096 KB
testcase_26 AC 86 ms
76,304 KB
testcase_27 AC 86 ms
76,064 KB
testcase_28 AC 84 ms
76,068 KB
testcase_29 AC 87 ms
76,060 KB
testcase_30 AC 85 ms
76,092 KB
testcase_31 AC 83 ms
76,076 KB
testcase_32 AC 79 ms
76,176 KB
testcase_33 AC 84 ms
76,072 KB
testcase_34 AC 80 ms
75,980 KB
testcase_35 AC 86 ms
76,200 KB
testcase_36 AC 85 ms
76,172 KB
testcase_37 AC 85 ms
76,200 KB
testcase_38 AC 85 ms
76,208 KB
testcase_39 AC 81 ms
76,160 KB
testcase_40 AC 80 ms
76,160 KB
testcase_41 AC 78 ms
76,248 KB
testcase_42 AC 78 ms
76,296 KB
testcase_43 AC 114 ms
77,572 KB
testcase_44 AC 113 ms
77,492 KB
testcase_45 AC 120 ms
77,500 KB
testcase_46 AC 114 ms
77,772 KB
testcase_47 AC 112 ms
77,444 KB
testcase_48 AC 71 ms
71,384 KB
testcase_49 AC 71 ms
71,528 KB
testcase_50 AC 71 ms
71,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9
inf = 10**15


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    A = list(map(int, input().split()))
    A.append(2)

    B = []
    cnt = [0, 0]
    for a in A:
        if a == 2:
            if sum(cnt):
                B.append(cnt)
                cnt = [0, 0]
        else:
            cnt[a] += 1

    S = 0
    for a, b in B:
        S += b

    dp = [inf] * (S+1)
    dp[S] = 0
    for i in range(len(B)):
        n0, n1 = B[i]
        dp_new = [inf] * (S+1)
        for j in range(S+1):
            dp_new[j] = min(dp_new[j], dp[j])
            if j - (n0 + n1) >= 0:
                dp_new[j - (n0 + n1)] = min(dp_new[j - (n0 + n1)], dp[j] + n0)
        dp = dp_new
    if dp[0] != inf:
        print(dp[0])
    else:
        print(-1)


if __name__ == '__main__':
    main()
0