結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー rin204
提出日時 2021-07-30 20:47:05
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 920 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 117,848 KB
最終ジャッジ日時 2024-09-15 22:40:14
合計ジャッジ時間 3,288 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4 WA * 1
other AC * 13 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

n, K = input().split()
n = int(n)
K = K.zfill(n)
cnt = [0] + list(map(int, input().split()))
ans = []
for k in K:
    k = int(k)
    if cnt[k] > 0:
        ans.append(str(k))
        cnt[k] -= 1
        continue
    flg = False
    for i in range(k + 1, 10):
        if cnt[i] > 0:
            flg = True
            ans.append(str(i))
            cnt[i] -= 1
            break
    if flg:
        for i, c in enumerate(cnt):
            ans += [str(i)] * c
        break
    while ans:
        p = int(ans.pop())
        cnt[p] += 1
        flg = False
        for i in range(p + 1, 10):
            if cnt[i] > 0:
                flg = True
                ans.append(str(i))
                cnt[i] -= 1
                break
        if flg:
            break
    if ans:
        for i, c in enumerate(cnt):
            ans += [str(i)] * c
        break
    else:
        print(-1)
        exit()
print("".join(ans))

0