結果

問題 No.332 数列をプレゼントに
ユーザー lam6er
提出日時 2025-04-09 21:02:54
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,044 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 82,644 KB
実行使用メモリ 96,692 KB
最終ジャッジ日時 2025-04-09 21:04:41
合計ジャッジ時間 4,275 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 1 TLE * 1 -- * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

n, X = map(int, input().split())
A = list(map(int, input().split()))
sum_total = sum(A)

if sum_total < X:
    print("No")
else:
    # Precompute suffix sums for pruning
    suffix_sum = [0] * (n + 1)
    for i in range(n-1, -1, -1):
        suffix_sum[i] = A[i] + suffix_sum[i+1]
    
    result = None
    path = ['x'] * n  # Initialize all as not selected
    
    def dfs(i, current_sum):
        global result
        if current_sum == X:
            result = path.copy()
            return True
        if i >= n:
            return False
        if current_sum > X:
            return False
        if current_sum + suffix_sum[i] < X:
            return False
        
        # Try not selecting A[i]
        if dfs(i + 1, current_sum):
            return True
        
        # Try selecting A[i]
        path[i] = 'o'
        if dfs(i + 1, current_sum + A[i]):
            return True
        path[i] = 'x'  # Backtrack
        
        return False
    
    if dfs(0, 0):
        print(''.join(result))
    else:
        print("No")
0