結果

問題 No.1017 Reiwa Sequence
ユーザー Shinya Fujita
提出日時 2024-10-08 23:30:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,537 ms / 2,000 ms
コード長 946 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 403,912 KB
最終ジャッジ日時 2024-10-08 23:31:40
合計ジャッジ時間 49,136 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
A = list(map(int, input().split()))


exist = [-1] * (max(A) + 1)
for i, ai in enumerate(A):
    if exist[ai] != -1:
        ans = [0] * N
        ans[i] = -ai
        ans[exist[ai]] = ai
        print('Yes')
        print(*ans)
        quit()
    
    exist[ai] = i


order = sorted(range(N), key=lambda i:A[i])
dp = [{0}]
for pos, i in enumerate(order):
    ai = A[i]
    if ai in dp[-1]:
        ans = [0] * N
        ans[i] = -ai
        now = ai
        for ii in order[:pos][::-1]:
            dp.pop()
            aii = A[ii]
            if abs(now-aii) in dp[-1]:
                now -= aii
                ans[ii] = aii
            elif abs(now+aii) in dp[-1]:
                now += aii
                ans[ii] = -aii
        print('Yes')
        print(*ans)
        quit()
    nex = set()
    for s in dp[-1]:
        nex.add(s)
        nex.add(abs(s - ai))
        nex.add(s + ai)
    
    dp.append(nex)

print('No')
0