結果

問題 No.1606 Stuffed Animals Keeper
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-04-15 21:43:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 119 ms / 3,000 ms
コード長 1,028 bytes
コンパイル時間 338 ms
コンパイル使用メモリ 87,048 KB
実行使用メモリ 77,144 KB
最終ジャッジ日時 2023-09-20 12:29:12
合計ジャッジ時間 7,085 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,280 KB
testcase_01 AC 74 ms
71,276 KB
testcase_02 AC 75 ms
71,288 KB
testcase_03 AC 113 ms
76,732 KB
testcase_04 AC 118 ms
76,872 KB
testcase_05 AC 97 ms
76,556 KB
testcase_06 AC 101 ms
76,884 KB
testcase_07 AC 109 ms
76,728 KB
testcase_08 AC 104 ms
76,780 KB
testcase_09 AC 95 ms
77,024 KB
testcase_10 AC 92 ms
76,696 KB
testcase_11 AC 90 ms
76,916 KB
testcase_12 AC 98 ms
76,680 KB
testcase_13 AC 80 ms
75,996 KB
testcase_14 AC 80 ms
76,136 KB
testcase_15 AC 79 ms
76,116 KB
testcase_16 AC 113 ms
76,796 KB
testcase_17 AC 114 ms
76,692 KB
testcase_18 AC 119 ms
77,072 KB
testcase_19 AC 119 ms
76,932 KB
testcase_20 AC 119 ms
77,000 KB
testcase_21 AC 96 ms
76,524 KB
testcase_22 AC 97 ms
76,760 KB
testcase_23 AC 102 ms
76,828 KB
testcase_24 AC 99 ms
76,656 KB
testcase_25 AC 99 ms
76,764 KB
testcase_26 AC 97 ms
76,892 KB
testcase_27 AC 99 ms
76,600 KB
testcase_28 AC 97 ms
76,808 KB
testcase_29 AC 95 ms
76,592 KB
testcase_30 AC 94 ms
76,844 KB
testcase_31 AC 94 ms
76,628 KB
testcase_32 AC 84 ms
76,536 KB
testcase_33 AC 91 ms
76,640 KB
testcase_34 AC 86 ms
76,628 KB
testcase_35 AC 94 ms
76,776 KB
testcase_36 AC 93 ms
76,740 KB
testcase_37 AC 90 ms
76,784 KB
testcase_38 AC 92 ms
76,588 KB
testcase_39 AC 86 ms
76,652 KB
testcase_40 AC 86 ms
76,792 KB
testcase_41 AC 82 ms
76,068 KB
testcase_42 AC 81 ms
76,376 KB
testcase_43 AC 119 ms
77,144 KB
testcase_44 AC 113 ms
76,644 KB
testcase_45 AC 111 ms
77,004 KB
testcase_46 AC 112 ms
77,024 KB
testcase_47 AC 113 ms
77,020 KB
testcase_48 AC 75 ms
71,204 KB
testcase_49 AC 74 ms
71,360 KB
testcase_50 AC 76 ms
71,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))

#制約チェック
#assert 2 <= N <= 3000
#assert len(A) == N
#for i in A:
#    assert 0 <= i <= 2


lis = [] #2で区切られた各区間に関して (0の数,1の数) を入れていく

allzero = 0 #0の総数
allone = 0  #1の総数

zero = 0
one = 0

for i in A:
    if i == 0:
        zero += 1
        allzero += 1
    elif i == 1:
        one += 1
        allone += 1
    elif zero + one > 0:
        lis.append( (zero,one) )
        zero = 0
        one = 0

if zero + one > 0:
    lis.append( (zero,one) )

M = allzero + allone

#ちょうど allzero 個入る区間の組み合わせのうち、
#oneの数の総和が最小の区間の組み合わせを求めればよい

dp = [float("inf")] * (allzero + 1)
dp[0] = 0

for nz,no in lis:

    for i in range(allzero-(nz+no),-1,-1):
        dp[i + (nz+no)] = min(dp[i + (nz+no)] , dp[i] + no)

if dp[-1] == float("inf"):
    print (-1)
else:
    print (dp[-1])
0