結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー Kude
提出日時 2021-07-30 20:58:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 1,044 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 139,108 KB
最終ジャッジ日時 2024-09-15 22:48:11
合計ジャッジ時間 3,684 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

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