結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー ayaoniayaoni
提出日時 2021-07-30 23:30:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 1,622 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 215,088 KB
最終ジャッジ日時 2023-10-14 08:34:28
合計ジャッジ時間 5,784 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,276 KB
testcase_01 AC 65 ms
71,128 KB
testcase_02 AC 65 ms
71,452 KB
testcase_03 AC 67 ms
71,404 KB
testcase_04 AC 66 ms
71,512 KB
testcase_05 AC 68 ms
71,268 KB
testcase_06 AC 65 ms
71,404 KB
testcase_07 AC 65 ms
71,240 KB
testcase_08 AC 64 ms
71,440 KB
testcase_09 AC 63 ms
71,464 KB
testcase_10 AC 62 ms
71,052 KB
testcase_11 AC 67 ms
71,084 KB
testcase_12 AC 67 ms
71,344 KB
testcase_13 AC 66 ms
71,080 KB
testcase_14 AC 70 ms
76,336 KB
testcase_15 AC 68 ms
76,336 KB
testcase_16 AC 332 ms
193,092 KB
testcase_17 AC 340 ms
196,388 KB
testcase_18 AC 307 ms
190,312 KB
testcase_19 AC 308 ms
188,952 KB
testcase_20 AC 334 ms
215,088 KB
testcase_21 AC 319 ms
211,984 KB
testcase_22 AC 94 ms
90,464 KB
testcase_23 AC 78 ms
85,496 KB
testcase_24 AC 310 ms
174,804 KB
testcase_25 AC 300 ms
161,496 KB
testcase_26 AC 311 ms
192,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


N,K = S().split()
N = int(N)
C = [0]+LI()

digit_K = len(K)  # Kの桁数
int_K = [0]+[int(s) for s in K]

if N < digit_K:
    print(-1)
    exit()

if N > digit_K:
    ANS = ['']*N
    idx = 0
    for i in range(1,10):
        c = C[i]
        s = str(i)
        for j in range(c):
            ANS[idx+j] = s
        idx += c
    ans = ''.join(ANS)
    print(ans)
    exit()

S_count_K = [[0]*10 for _ in range(N+1)]
for i in range(1,N+1):
    for j in range(10):
        S_count_K[i][j] += S_count_K[i-1][j]
    k = int_K[i]
    S_count_K[i][k] += 1

for i in range(N,0,-1):  # Kと上i-1桁が一致
    if S_count_K[i-1][0]:
        continue
    for j in range(1,10):
        if S_count_K[i-1][j] > C[j]:
            break
    else:
        x = int_K[i]
        for k in range(x+1,10):
            if C[k] > S_count_K[i-1][k]:
                C[k] -= 1
                ANS = int_K[1:i]+[k]
                for l in range(1,10):
                    c = C[l]-S_count_K[i-1][l]
                    for _ in range(c):
                        ANS.append(l)
                ans = ''.join([str(a) for a in ANS])
                print(ans)
                exit()

print(-1)
0