結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー ThetaTheta
提出日時 2024-04-16 19:43:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 361 ms / 2,000 ms
コード長 1,274 bytes
コンパイル時間 557 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 143,780 KB
最終ジャッジ日時 2024-04-16 19:43:20
合計ジャッジ時間 5,104 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
53,376 KB
testcase_01 AC 49 ms
53,376 KB
testcase_02 AC 47 ms
53,504 KB
testcase_03 AC 48 ms
53,760 KB
testcase_04 AC 47 ms
54,144 KB
testcase_05 AC 47 ms
53,632 KB
testcase_06 AC 46 ms
53,760 KB
testcase_07 AC 50 ms
53,888 KB
testcase_08 AC 48 ms
53,888 KB
testcase_09 AC 47 ms
53,504 KB
testcase_10 AC 48 ms
53,504 KB
testcase_11 AC 47 ms
54,400 KB
testcase_12 AC 48 ms
53,888 KB
testcase_13 AC 47 ms
53,504 KB
testcase_14 AC 47 ms
53,888 KB
testcase_15 AC 48 ms
54,144 KB
testcase_16 AC 296 ms
128,948 KB
testcase_17 AC 343 ms
138,280 KB
testcase_18 AC 174 ms
128,880 KB
testcase_19 AC 186 ms
129,280 KB
testcase_20 AC 328 ms
143,780 KB
testcase_21 AC 329 ms
140,200 KB
testcase_22 AC 148 ms
128,180 KB
testcase_23 AC 101 ms
101,888 KB
testcase_24 AC 344 ms
122,360 KB
testcase_25 AC 361 ms
129,212 KB
testcase_26 AC 169 ms
129,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
import sys
sys.setrecursionlimit(600000)


def main():
    N, K = input().split()
    N = int(N)
    K = list(map(int, K))
    c = list(map(int, input().split()))
    if len(K) < N:
        ans = []
        for n, v in enumerate(c, 1):
            for _ in range(v):
                ans.append(n)
        print("".join(map(str, ans)))
        return

    if len(K) > N:
        print(-1)
        return

    ctr = Counter(dict(enumerate(c, 1)))
    ctr.subtract(K)

    for idx, digit in reversed(list(enumerate(K))):
        ctr[digit] += 1
        if any(ctr_elm < 0 for ctr_elm in ctr.values()):
            continue
        min_over_n = -1
        for n in range(10):
            if n <= digit:
                continue
            if ctr[n] > 0:
                min_over_n = n
                break
        else:
            continue

        ans = K[:idx]
        ans.append(min_over_n)
        for n in range(10):
            if n == min_over_n:
                for _ in range(ctr[n] - 1):
                    ans.append(n)
            else:
                for _ in range(ctr[n]):
                    ans.append(n)
        print("".join(map(str, ans)))
        return
    print(-1)
    return


if __name__ == "__main__":
    main()
0