結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー ayaoniayaoni
提出日時 2021-07-30 22:11:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,725 bytes
コンパイル時間 1,277 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 78,768 KB
最終ジャッジ日時 2023-10-14 05:07:54
合計ジャッジ時間 5,189 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,328 KB
testcase_01 AC 66 ms
71,064 KB
testcase_02 AC 67 ms
71,368 KB
testcase_03 AC 67 ms
71,432 KB
testcase_04 AC 66 ms
71,508 KB
testcase_05 AC 70 ms
71,404 KB
testcase_06 AC 70 ms
71,244 KB
testcase_07 AC 68 ms
71,544 KB
testcase_08 AC 71 ms
71,140 KB
testcase_09 AC 69 ms
71,328 KB
testcase_10 AC 72 ms
71,372 KB
testcase_11 AC 69 ms
71,588 KB
testcase_12 AC 72 ms
71,188 KB
testcase_13 AC 72 ms
71,504 KB
testcase_14 WA -
testcase_15 AC 74 ms
76,164 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
権限があれば一括ダウンロードができます

ソースコード

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 = MI()
C = [0]+LI()
str_K = [0]+[int(s) for s in str(K)]
digit_K = len(str_K)-1

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

if N > digit_K:
    ANS = [0]*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 = str_K[i]
    S_count_K[i][k] += 1

ok = 0
ng = N+1
while ok+1 < ng:
    mid = (ok+ng)//2
    if S_count_K[mid-1][0]:
        ng = mid
        continue
    for j in range(1,10):
        if S_count_K[mid-1][j] > C[j]:
            ng = mid
            break
    else:
        x = str_K[mid]
        for k in range(x+1,10):
            if C[k] > S_count_K[mid-1][k]:
                ok = mid
                break
        else:
            ng = mid

if ok == 0:
    print(-1)
    exit()

ANS = str_K[1:ok]
for i in range(str_K[ok]+1,10):
    if C[i] > S_count_K[ok-1][i]:
        ANS.append(i)
        C[i] -= 1
        break
for i in range(1,10):
    for _ in range(C[i]-S_count_K[ok-1][i]):
        ANS.append(i)

str_ANS = [str(s) for s in ANS]
ans = ''.join(str_ANS)
print(ans)
0