結果

問題 No.1861 Required Number
ユーザー 👑 H20H20
提出日時 2022-03-04 23:29:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 139 ms / 2,500 ms
コード長 1,242 bytes
コンパイル時間 621 ms
コンパイル使用メモリ 87,220 KB
実行使用メモリ 170,384 KB
最終ジャッジ日時 2023-09-26 03:58:42
合計ジャッジ時間 21,077 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
75,992 KB
testcase_01 AC 94 ms
81,272 KB
testcase_02 AC 88 ms
76,232 KB
testcase_03 AC 103 ms
170,108 KB
testcase_04 AC 98 ms
82,056 KB
testcase_05 AC 91 ms
170,384 KB
testcase_06 AC 89 ms
76,092 KB
testcase_07 AC 97 ms
76,808 KB
testcase_08 AC 105 ms
77,116 KB
testcase_09 AC 99 ms
76,960 KB
testcase_10 AC 108 ms
77,500 KB
testcase_11 AC 101 ms
77,272 KB
testcase_12 AC 96 ms
76,712 KB
testcase_13 AC 104 ms
77,068 KB
testcase_14 AC 107 ms
77,308 KB
testcase_15 AC 101 ms
77,048 KB
testcase_16 AC 95 ms
76,928 KB
testcase_17 AC 105 ms
77,268 KB
testcase_18 AC 104 ms
77,160 KB
testcase_19 AC 104 ms
76,836 KB
testcase_20 AC 106 ms
76,860 KB
testcase_21 AC 108 ms
76,816 KB
testcase_22 AC 107 ms
77,356 KB
testcase_23 AC 101 ms
77,400 KB
testcase_24 AC 121 ms
78,304 KB
testcase_25 AC 136 ms
78,348 KB
testcase_26 AC 111 ms
77,708 KB
testcase_27 AC 117 ms
78,304 KB
testcase_28 AC 120 ms
77,932 KB
testcase_29 AC 115 ms
77,760 KB
testcase_30 AC 101 ms
77,264 KB
testcase_31 AC 137 ms
78,340 KB
testcase_32 AC 98 ms
77,268 KB
testcase_33 AC 128 ms
78,100 KB
testcase_34 AC 127 ms
78,112 KB
testcase_35 AC 129 ms
78,396 KB
testcase_36 AC 114 ms
78,296 KB
testcase_37 AC 121 ms
78,152 KB
testcase_38 AC 131 ms
78,340 KB
testcase_39 AC 124 ms
78,176 KB
testcase_40 AC 135 ms
78,224 KB
testcase_41 AC 139 ms
78,320 KB
testcase_42 AC 131 ms
78,424 KB
testcase_43 AC 116 ms
78,516 KB
testcase_44 AC 91 ms
71,768 KB
04_evil_1.txt TLE -
04_evil_2.txt TLE -
04_evil_3.txt TLE -
04_evil_4.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections

N,K = map(int,input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
CA = collections.Counter(A)
if CA[0]==1 and K==0:
    print(1)
    exit()
if CA[0]>1 and K==0:
    print(0)
    exit()
if K==0:
    print(-1)
    exit()
if sum(A)==K:
    print(N-CA[0])
    exit()
if CA[1]>K:
    print(0)
    exit()

DP = [-1]*(K+1)
for i in range(N):
    for j in reversed(range(K)):
        if DP[j]!=-1 and j+A[i]<=K and DP[j+A[i]]==-1:
            DP[j+A[i]]=i
        else:
            pass
    if DP[K]!=-1:
        break
    if DP[A[i]]==-1:
        DP[A[i]]=i
if DP[K]==-1:
    print(-1)
    exit()
v = K
L = []
B = []
while v>0:
    L.append(DP[v])
    B.append(A[DP[v]])
    v-=A[DP[v]]
ans = 0
CB = collections.Counter(B)
for k,v in CB.items():
    if CB[k]==CA[k]:
        DP = [-1]*(K+1)
        for i in range(N):
            if A[i]==k:
                continue
            for j in reversed(range(K)):
                if DP[j]!=-1 and j+A[i]<=K and DP[j+A[i]]==-1:
                    DP[j+A[i]]=i
                else:
                    pass
            if DP[K]!=-1:
                break
            if DP[A[i]]==-1:
                DP[A[i]]=i
        if DP[K]==-1:
            ans+=v

print(ans)
0