結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 👑 rin204rin204
提出日時 2021-07-30 20:48:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,440 bytes
コンパイル時間 430 ms
コンパイル使用メモリ 86,956 KB
実行使用メモリ 118,728 KB
最終ジャッジ日時 2023-10-14 03:05:49
合計ジャッジ時間 4,724 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,020 KB
testcase_01 AC 74 ms
71,008 KB
testcase_02 AC 75 ms
71,288 KB
testcase_03 AC 76 ms
71,284 KB
testcase_04 AC 75 ms
71,332 KB
testcase_05 AC 73 ms
71,176 KB
testcase_06 AC 74 ms
71,224 KB
testcase_07 AC 73 ms
71,320 KB
testcase_08 AC 72 ms
71,176 KB
testcase_09 AC 75 ms
71,524 KB
testcase_10 AC 74 ms
71,192 KB
testcase_11 AC 74 ms
71,416 KB
testcase_12 AC 72 ms
71,140 KB
testcase_13 AC 77 ms
71,152 KB
testcase_14 AC 74 ms
71,268 KB
testcase_15 AC 75 ms
71,432 KB
testcase_16 AC 186 ms
118,728 KB
testcase_17 AC 192 ms
116,244 KB
testcase_18 AC 154 ms
115,220 KB
testcase_19 AC 148 ms
115,220 KB
testcase_20 AC 101 ms
95,128 KB
testcase_21 AC 103 ms
99,416 KB
testcase_22 AC 106 ms
100,512 KB
testcase_23 WA -
testcase_24 AC 181 ms
116,664 KB
testcase_25 AC 155 ms
115,176 KB
testcase_26 AC 145 ms
115,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n, K = input().split()
n = int(n)
K = K.zfill(n)
cnt = [0] + list(map(int, input().split()))
ans = []
ok = False
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
            ok = True
            break
    if flg:
        for i, c in enumerate(cnt):
            ans += [str(i)] * c
        ok = True
        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
        ok = True
        break
    else:
        print(-1)
        exit()
if ok:
    print("".join(ans))
else:
    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
        print("".join(ans))
    else:
        print(-1)
        
0