結果

問題 No.10 +か×か
ユーザー lam6er
提出日時 2025-03-26 15:45:56
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 889 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 76,688 KB
最終ジャッジ日時 2025-03-26 15:46:17
合計ジャッジ時間 7,248 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 TLE * 1 -- * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve():
    N = int(input())
    Total = int(input())
    A = list(map(int, input().split()))
    
    def backtrack(index, current_val, ops):
        if index == N - 1:
            return ops if current_val == Total else None
        
        next_num = A[index + 1]
        
        # Try '+' first to maintain lex order
        new_plus = current_val + next_num
        if new_plus <= Total:
            result = backtrack(index + 1, new_plus, ops + ['+'])
            if result is not None:
                return result
        
        # Try '*' if '+' doesn't work
        new_mul = current_val * next_num
        if new_mul <= Total:
            result = backtrack(index + 1, new_mul, ops + ['*'])
            if result is not None:
                return result
        
        return None
    
    result_ops = backtrack(0, A[0], [])
    print(''.join(result_ops))

solve()
0