結果

問題 No.1457 ツブ消ししとるなHard
ユーザー lam6er
提出日時 2025-03-20 21:05:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,679 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 82,604 KB
実行使用メモリ 66,080 KB
最終ジャッジ日時 2025-03-20 21:05:42
合計ジャッジ時間 2,006 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def main():
    N, M, X, Y, Z = map(int, sys.stdin.readline().split())
    A = list(map(int, sys.stdin.readline().split()))
    
    must_keep = []
    must_remove = []
    optional = []
    
    for a in A:
        if a >= X:
            must_keep.append(a)
        elif a <= Y:
            must_remove.append(a)
        else:
            optional.append(a)
    
    m_keep = len(must_keep)
    sum_m_keep = sum(must_keep)
    k_opt = len(optional)
    
    if m_keep > M:
        print("Handicapped")
        return
    
    if k_opt == 0:
        possible = 0
        if m_keep >=1 and m_keep <= M:
            if sum_m_keep == Z * m_keep:
                possible = 1
        print(possible)
        return
    
    max_sum = sum(optional)
    max_t = k_opt
    dp = [[0] * (max_sum + 1) for _ in range(max_t + 1)]
    dp[0][0] = 1
    
    for a in optional:
        for t in range(max_t, 0, -1):
            for s in range(max_sum, -1, -1):
                if dp[t-1][s]:
                    new_s = s + a
                    if new_s <= max_sum:
                        dp[t][new_s] += dp[t-1][s]
    
    answer = 0
    t_max = min(k_opt, M - m_keep)
    
    for t in range(0, t_max + 1):
        total_t = m_keep + t
        if total_t < 1 or total_t > M:
            continue
        required_s = Z * total_t - sum_m_keep
        if required_s < 0:
            continue
        if t == 0:
            if required_s == 0 and sum_m_keep == Z * m_keep:
                answer += 1
        else:
            if required_s <= max_sum:
                answer += dp[t][required_s]
    
    print(answer if answer > 0 else 0)

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