結果

問題 No.1861 Required Number
コンテスト
ユーザー roaris
提出日時 2022-03-04 21:58:41
言語 PyPy3
(8.0.0 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 385 ms / 2,500 ms
+ 462µs
コード長 700 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 380 ms
コンパイル使用メモリ 64,508 KB
実行使用メモリ 138,596 KB
最終ジャッジ日時 2026-10-17 11:10:04
合計ジャッジ時間 14,630 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43 MLE * 2 -- * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline
from collections import *

N, K = map(int, input().split())
A = list(map(int, input().split()))
dp1 = [False]*(K+1)
dp1[0] = True
dp2 = [[False]*(K+1) for _ in range(N+1)]
dp2[0][0] = True

for i in range(N):
    for j in range(K+1):
        if dp2[i][j]:
            dp2[i+1][j] = True
            
            if j+A[N-1-i]<=K:
                dp2[i+1][j+A[N-1-i]] = True

if not dp2[N][K]:
    exit(print(-1))

ans = 0

for i in range(N):
    for j in range(K+1):
        if dp1[j] and dp2[N-1-i][K-j]:
            break
    else:
        ans += 1
    
    for j in range(K, -1, -1):
        if dp1[j] and j+A[i]<=K:
            dp1[j+A[i]] = True

print(ans)
0