結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 👑 rin204rin204
提出日時 2021-07-30 20:49:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 1,480 bytes
コンパイル時間 2,532 ms
コンパイル使用メモリ 86,916 KB
実行使用メモリ 118,556 KB
最終ジャッジ日時 2023-10-14 03:06:12
合計ジャッジ時間 5,217 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,380 KB
testcase_01 AC 75 ms
70,996 KB
testcase_02 AC 76 ms
71,152 KB
testcase_03 AC 75 ms
71,148 KB
testcase_04 AC 77 ms
71,344 KB
testcase_05 AC 76 ms
71,448 KB
testcase_06 AC 77 ms
71,164 KB
testcase_07 AC 75 ms
71,456 KB
testcase_08 AC 76 ms
71,256 KB
testcase_09 AC 78 ms
71,256 KB
testcase_10 AC 76 ms
71,508 KB
testcase_11 AC 79 ms
71,504 KB
testcase_12 AC 74 ms
71,388 KB
testcase_13 AC 74 ms
71,164 KB
testcase_14 AC 77 ms
71,260 KB
testcase_15 AC 75 ms
70,992 KB
testcase_16 AC 190 ms
118,556 KB
testcase_17 AC 190 ms
116,544 KB
testcase_18 AC 150 ms
115,448 KB
testcase_19 AC 148 ms
115,464 KB
testcase_20 AC 104 ms
95,256 KB
testcase_21 AC 108 ms
99,856 KB
testcase_22 AC 109 ms
100,744 KB
testcase_23 AC 78 ms
72,472 KB
testcase_24 AC 185 ms
116,484 KB
testcase_25 AC 158 ms
115,264 KB
testcase_26 AC 151 ms
115,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n, K = input().split()
n = int(n)
if len(K) > n:
    print(-1)
    exit()
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