結果

問題 No.1594 Three Classes
ユーザー lam6er
提出日時 2025-03-31 17:18:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 1,007 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 82,100 KB
実行使用メモリ 67,940 KB
最終ジャッジ日時 2025-03-31 17:19:09
合計ジャッジ時間 1,777 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve():
    import sys
    input = sys.stdin.read().split()
    N = int(input[0])
    E = list(map(int, input[1:N+1]))
    sum_total = sum(E)
    
    if sum_total % 3 != 0:
        print("No")
        return
    
    T = sum_total // 3
    for e in E:
        if e > T:
            print("No")
            return
    
    E.sort(reverse=True)
    
    def backtrack(index, a, b, c):
        if index == N:
            return a == b == c == T
        current = E[index]
        # Try adding to a
        if a + current <= T:
            if backtrack(index + 1, a + current, b, c):
                return True
        # Try adding to b
        if b + current <= T:
            if backtrack(index + 1, a, b + current, c):
                return True
        # Try adding to c
        if c + current <= T:
            if backtrack(index + 1, a, b, c + current):
                return True
        return False
    
    if backtrack(0, 0, 0, 0):
        print("Yes")
    else:
        print("No")

solve()
0