結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー brthyyjp
提出日時 2022-03-09 10:33:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 801 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 124,676 KB
最終ジャッジ日時 2024-09-13 09:54:19
合計ジャッジ時間 4,131 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

n, k = map(str, input().split())
n = int(n)
C = list(map(int, input().split()))
import copy
D = copy.copy(C)
if n < len(k):
    print(-1)
    exit()

if n > len(k):
    ans = []
    for i, c in enumerate(C):
        ans += [str(i+1)]*c
    print(''.join(ans))
    exit()

l = len(k)
k = list(k)
npd, p = -1, -1
for i in range(l):
    d = int(k[i])
    nd = -1
    for j in range(d+1, 10):
        if C[j-1] > 0:
            nd = j
            break
    if nd != -1:
        p = i
        npd = nd
    if d == 0:
        break
    if not C[d-1]:
        break
    else:
        C[d-1] -= 1
if p == -1:
    print(-1)
    exit()
ans = k[0:p]+[str(npd)]
for d in k[0:p]:
    d = int(d)
    if d != 0:
        D[d-1] -= 1
D[npd-1] -= 1
for i, c in enumerate(D):
    ans += [str(i+1)]*c
print(''.join(ans))
0