結果

問題 No.1861 Required Number
コンテスト
ユーザー LyricalMaestro
提出日時 2026-07-22 22:06:48
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 257 ms / 2,500 ms
+ 899µs
コード長 1,026 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 243 ms
コンパイル使用メモリ 95,980 KB
実行使用メモリ 163,712 KB
最終ジャッジ日時 2026-07-22 22:06:59
合計ジャッジ時間 10,518 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42 MLE * 2 -- * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

## https://yukicoder.me/problems/no/1861


def main():
    N, K = map(int, input().split())
    A = list(map(int ,input().split()))

    # そもそも存在する?
    dp = [[0] * (K + 1) for _ in range(N + 1)]
    dp[0][0] = 1
    for i in range(N):
        a = A[i]        
        for x in range(K + 1):
            if x + a <= K:
                dp[i + 1][x + a] |= dp[i][x]
            dp[i + 1][x] |= dp[i][x]
    
    if dp[-1][K] == 0:
        print(-1)
        return
    
    answer = 0
    dp0 = [0] * (K + 1)
    dp0[0] = 1
    for i in reversed(range(N)):
        is_ok = True
        for k in range(K + 1):
            k0 = K - k
            if dp0[k] == 1 and dp[i][k0] == 1:
                is_ok = False

        if is_ok:
            answer += 1

        a = A[i]
        new_dp = dp0.copy()
        for x in range(K + 1):
            if x + a <= K:
                new_dp[x + a] |= dp0[x]
        dp0 = new_dp
    print(answer)

                        


    






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