結果

問題 No.1017 Reiwa Sequence
コンテスト
ユーザー Shinya Fujita
提出日時 2024-10-08 23:30:45
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,078 ms / 2,000 ms
コード長 946 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 133 ms
コンパイル使用メモリ 85,376 KB
実行使用メモリ 431,640 KB
最終ジャッジ日時 2026-04-25 04:05:29
合計ジャッジ時間 53,016 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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