結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー sotanishysotanishy
提出日時 2021-07-30 21:47:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 1,046 bytes
コンパイル時間 2,461 ms
コンパイル使用メモリ 86,512 KB
実行使用メモリ 190,084 KB
最終ジャッジ日時 2023-10-14 04:10:58
合計ジャッジ時間 7,251 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,228 KB
testcase_01 AC 75 ms
71,184 KB
testcase_02 AC 73 ms
71,236 KB
testcase_03 AC 70 ms
71,144 KB
testcase_04 AC 73 ms
71,320 KB
testcase_05 AC 72 ms
71,104 KB
testcase_06 AC 72 ms
71,336 KB
testcase_07 AC 73 ms
71,168 KB
testcase_08 AC 73 ms
71,140 KB
testcase_09 AC 75 ms
71,252 KB
testcase_10 AC 73 ms
71,388 KB
testcase_11 AC 75 ms
70,904 KB
testcase_12 AC 74 ms
71,016 KB
testcase_13 AC 74 ms
71,252 KB
testcase_14 AC 89 ms
76,248 KB
testcase_15 AC 78 ms
76,252 KB
testcase_16 AC 400 ms
175,808 KB
testcase_17 AC 389 ms
190,084 KB
testcase_18 AC 412 ms
178,384 KB
testcase_19 AC 400 ms
175,280 KB
testcase_20 AC 378 ms
190,040 KB
testcase_21 AC 386 ms
190,024 KB
testcase_22 AC 378 ms
190,004 KB
testcase_23 AC 76 ms
72,864 KB
testcase_24 AC 323 ms
167,864 KB
testcase_25 AC 336 ms
153,192 KB
testcase_26 AC 399 ms
175,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def check(i, d):
    s = 0
    for x in range(10)[::-1]:
        s += c[x]
        if x == d:
            s -= 1
        if cnt[i+1][x] > s:
            return False
        if cnt[i+1][x] < s:
            return True
    return False

N, K = input().rstrip().split()
N = int(N)
K = '0' * (N - len(K)) + K
if len(K) > N:
    print(-1)
    exit()
c = [0] + list(map(int, input().split()))
ans = []
cnt = [[0] * 10 for _ in range(N+1)]
for i in range(N)[::-1]:
    for d in range(10):
        if int(K[i]) >= d:
            cnt[i][d] = cnt[i+1][d] + 1
        else:
            cnt[i][d] = 0
for i in range(N):
    d = int(K[i])
    ok = False
    if c[d] > 0 and check(i, d):
        ans.append(d)
        c[d] -= 1
    else:
        d += 1
        while d < 10 and c[d] == 0:
            d += 1
        if d == 10:
            print(-1)
            exit()
        ans.append(d)
        c[d] -= 1
        break
for d in range(10):
    for _ in range(c[d]):
        ans.append(d)
print(''.join(map(str, ans)))
0