結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー KudeKude
提出日時 2021-07-30 20:58:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 327 ms / 2,000 ms
コード長 1,044 bytes
コンパイル時間 269 ms
コンパイル使用メモリ 86,656 KB
実行使用メモリ 143,096 KB
最終ジャッジ日時 2023-10-14 03:13:52
合計ジャッジ時間 4,799 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,132 KB
testcase_01 AC 71 ms
71,000 KB
testcase_02 AC 71 ms
71,036 KB
testcase_03 AC 72 ms
71,032 KB
testcase_04 AC 74 ms
71,000 KB
testcase_05 AC 74 ms
71,028 KB
testcase_06 AC 73 ms
70,848 KB
testcase_07 AC 73 ms
71,156 KB
testcase_08 AC 72 ms
70,948 KB
testcase_09 AC 72 ms
71,088 KB
testcase_10 AC 71 ms
71,164 KB
testcase_11 AC 72 ms
71,152 KB
testcase_12 AC 70 ms
70,840 KB
testcase_13 AC 71 ms
71,040 KB
testcase_14 AC 71 ms
70,716 KB
testcase_15 AC 72 ms
71,036 KB
testcase_16 AC 327 ms
127,012 KB
testcase_17 AC 196 ms
127,168 KB
testcase_18 AC 180 ms
143,096 KB
testcase_19 AC 177 ms
142,908 KB
testcase_20 AC 149 ms
126,620 KB
testcase_21 AC 149 ms
128,464 KB
testcase_22 AC 148 ms
128,588 KB
testcase_23 AC 112 ms
102,360 KB
testcase_24 AC 168 ms
114,128 KB
testcase_25 AC 146 ms
131,348 KB
testcase_26 AC 151 ms
128,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n_, k_ = input().split()
n = int(n_)
k = list(map(int, k_))
c = [0] + list(map(int, input().split()))
if n < len(k):
    print(-1)
    exit()

def fill_rest(s, c):
    for i, cnt in enumerate(c):
        for _ in range(cnt):
            s.append(str(i))
    return

s = []
if n > len(k):
    fill_rest(s, c)
    ans = ''.join(map(str, s))
    print(ans)
    exit()


def dame(i):
    while i:
        i -= 1
        c[s.pop()] += 1
        x = k[i]
        for d in range(x + 1, 10):
            if c[d]:
                s.append(d)
                c[d] -= 1
                fill_rest(s, c)
                ans = ''.join(map(str, s))
                print(ans)
                exit(0)
    print(-1)
    exit()

for i in range(n):
    x = k[i]
    if c[x]:
        s.append(x)
        c[x] -= 1
        continue
    for d in range(x + 1, 10):
        if c[d]:
            s.append(d)
            c[d] -= 1
            fill_rest(s, c)
            ans = ''.join(map(str, s))
            print(ans)
            exit(0)
    dame(i)
else:
    dame(n)
0