結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー chineristACchineristAC
提出日時 2021-07-30 20:51:44
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 311 ms / 2,000 ms
コード長 1,119 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 86,988 KB
実行使用メモリ 108,216 KB
最終ジャッジ日時 2023-10-14 03:08:20
合計ジャッジ時間 5,877 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
74,376 KB
testcase_01 AC 117 ms
74,412 KB
testcase_02 AC 112 ms
74,544 KB
testcase_03 AC 113 ms
74,340 KB
testcase_04 AC 111 ms
74,584 KB
testcase_05 AC 111 ms
74,380 KB
testcase_06 AC 112 ms
74,264 KB
testcase_07 AC 113 ms
74,268 KB
testcase_08 AC 111 ms
74,268 KB
testcase_09 AC 113 ms
74,320 KB
testcase_10 AC 114 ms
74,244 KB
testcase_11 AC 116 ms
74,316 KB
testcase_12 AC 114 ms
74,104 KB
testcase_13 AC 114 ms
74,576 KB
testcase_14 AC 113 ms
74,384 KB
testcase_15 AC 114 ms
74,628 KB
testcase_16 AC 257 ms
95,072 KB
testcase_17 AC 295 ms
99,392 KB
testcase_18 AC 142 ms
82,900 KB
testcase_19 AC 142 ms
82,620 KB
testcase_20 AC 198 ms
104,172 KB
testcase_21 AC 199 ms
108,216 KB
testcase_22 AC 141 ms
102,460 KB
testcase_23 AC 118 ms
75,816 KB
testcase_24 AC 311 ms
101,720 KB
testcase_25 AC 301 ms
81,436 KB
testcase_26 AC 131 ms
82,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd,log

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

def solve(N,K,c):
    if len(K) > N:
        return -1

    if N > len(K):
        ans = []
        for j in range(10):
            ans += [str(j)] * c[j]
        return "".join(ans)

    check_c = [0 for i in range(10)]
    for i in range(len(K)):
        check_c[int(K[i])] += 1

    for d in range(len(K)-1,-1,-1):
        check_c[int(K[d])] -= 1
        if any(c[i] < check_c[i] for i in range(10)):
            continue

        for i in range(int(K[d])+1,10):
            if c[i] > check_c[i]:
                ans = [K[:d],str(i)]
                #print(ans,check_c,c)
                c[i] -= 1
                for j in range(10):
                    c[j] -= check_c[j]
                    ans += [str(j)] * c[j]
                return "".join(ans)

    return -1

N,K = input().split()
N = int(N)
c = [0] + li()
print(solve(N,K,c))
0