結果

問題 No.1606 Stuffed Animals Keeper
ユーザー shotoyooshotoyoo
提出日時 2021-07-16 22:20:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 315 ms / 3,000 ms
コード長 1,088 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 80,484 KB
最終ジャッジ日時 2023-09-20 14:32:36
合計ジャッジ時間 10,370 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,332 KB
testcase_01 AC 77 ms
71,516 KB
testcase_02 AC 76 ms
71,244 KB
testcase_03 AC 238 ms
79,240 KB
testcase_04 AC 270 ms
79,332 KB
testcase_05 AC 129 ms
77,472 KB
testcase_06 AC 161 ms
77,976 KB
testcase_07 AC 218 ms
78,512 KB
testcase_08 AC 197 ms
78,256 KB
testcase_09 AC 151 ms
77,876 KB
testcase_10 AC 126 ms
77,504 KB
testcase_11 AC 108 ms
77,284 KB
testcase_12 AC 162 ms
77,928 KB
testcase_13 AC 83 ms
76,160 KB
testcase_14 AC 81 ms
75,972 KB
testcase_15 AC 81 ms
75,908 KB
testcase_16 AC 295 ms
80,080 KB
testcase_17 AC 298 ms
80,244 KB
testcase_18 AC 312 ms
80,448 KB
testcase_19 AC 315 ms
80,484 KB
testcase_20 AC 303 ms
80,456 KB
testcase_21 AC 160 ms
78,680 KB
testcase_22 AC 153 ms
78,392 KB
testcase_23 AC 151 ms
78,288 KB
testcase_24 AC 153 ms
78,328 KB
testcase_25 AC 154 ms
77,840 KB
testcase_26 AC 122 ms
77,760 KB
testcase_27 AC 123 ms
77,704 KB
testcase_28 AC 122 ms
77,660 KB
testcase_29 AC 127 ms
77,876 KB
testcase_30 AC 121 ms
77,668 KB
testcase_31 AC 124 ms
77,720 KB
testcase_32 AC 85 ms
76,576 KB
testcase_33 AC 116 ms
77,868 KB
testcase_34 AC 88 ms
76,368 KB
testcase_35 AC 118 ms
77,984 KB
testcase_36 AC 118 ms
77,816 KB
testcase_37 AC 118 ms
77,728 KB
testcase_38 AC 114 ms
77,764 KB
testcase_39 AC 87 ms
76,548 KB
testcase_40 AC 87 ms
76,428 KB
testcase_41 AC 88 ms
76,608 KB
testcase_42 AC 87 ms
76,340 KB
testcase_43 AC 256 ms
78,488 KB
testcase_44 AC 254 ms
78,568 KB
testcase_45 AC 246 ms
78,520 KB
testcase_46 AC 244 ms
78,588 KB
testcase_47 AC 246 ms
78,552 KB
testcase_48 AC 77 ms
71,236 KB
testcase_49 AC 80 ms
71,356 KB
testcase_50 AC 81 ms
71,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))

def runlength(s):
    if not s:
        return []
    c = s[0]
    v = 1
    n = len(s)
    l = []
    for i in range(1, n):
        if c==s[i]:
            v += 1
        else:
            l.append((c,v))
            c = s[i]
            v = 1
    l.append((c,v))
    return l

n = int(input())
a = list(map(int, input().split()))
res = runlength(a)
l = []
tmp = [0,0]
for c,v in res:
    if c==2:
        if sum(tmp)>0:
            l.append(tmp)
        tmp = [0,0]
    else:
        tmp[c] += v
if sum(tmp)>0:
    l.append(tmp)
d = {0: 0}
for a,b in l:
    nd = {}
    for k,v in d.items():
        nk = k+a
        nv = v
        nd.setdefault(nk,10**12)    
        nd[nk] = min(nd[nk], nv)
        nk = k-b
        nv = v+b
        nd.setdefault(nk,10**12)
        nd[nk] = min(nd[nk], nv)
    d = nd
if 0 in d:
    ans = d[0]
else:
    ans = -1
print(ans)
0