結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-07-30 21:53:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 1,831 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 87,132 KB
実行使用メモリ 177,936 KB
最終ジャッジ日時 2023-10-14 04:27:34
合計ジャッジ時間 5,298 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,164 KB
testcase_01 AC 76 ms
71,096 KB
testcase_02 AC 71 ms
71,244 KB
testcase_03 AC 72 ms
71,304 KB
testcase_04 AC 72 ms
71,340 KB
testcase_05 AC 71 ms
71,420 KB
testcase_06 AC 72 ms
71,336 KB
testcase_07 AC 73 ms
71,372 KB
testcase_08 AC 71 ms
71,312 KB
testcase_09 AC 72 ms
71,276 KB
testcase_10 AC 72 ms
71,232 KB
testcase_11 AC 73 ms
71,140 KB
testcase_12 AC 73 ms
71,376 KB
testcase_13 AC 76 ms
71,220 KB
testcase_14 AC 72 ms
71,268 KB
testcase_15 AC 73 ms
71,412 KB
testcase_16 AC 262 ms
177,936 KB
testcase_17 AC 230 ms
176,072 KB
testcase_18 AC 184 ms
174,232 KB
testcase_19 AC 187 ms
174,208 KB
testcase_20 AC 208 ms
165,216 KB
testcase_21 AC 207 ms
165,296 KB
testcase_22 AC 207 ms
169,792 KB
testcase_23 AC 98 ms
94,496 KB
testcase_24 AC 203 ms
152,708 KB
testcase_25 AC 175 ms
176,412 KB
testcase_26 AC 176 ms
173,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

上の桁から決めていく
なるべく合わせていく
超えちゃうんだったら、置いちゃって後は昇順

絶対下回る場合、戻って回収していく。
大きくできるところで大きくして、


"""

import sys
from sys import stdin

N,K = input().split()
N = int(N)
K = list(K)

if len(K) > N:
    print (-1)
    sys.exit()

K.reverse()
while len(K) < N:
    K.append('0')
K.reverse()
K = list(map(int,K))
    

c = [0] + list(map(int,stdin.readline().split()))

#print (N,K)

ans = []
for i in range(N):

    if c[K[i]] > 0:
        c[K[i]] -= 1
        ans.append(K[i])
    else:
        put = None
        for j in range(K[i]+1,10):
            if c[j] > 0:
                put = j
                break

        if put != None: #置いちゃって、後は貪欲
            c[put] -= 1
            ans.append(put)

            while len(ans) < N:
                put = None
                for j in range(10):
                    if c[j] > 0:
                        put = j
                        break
                c[put] -= 1
                ans.append(put)
            print ("".join(map(str,ans)))
            sys.exit()
        else:
            break

#大きいのが置けるまでロールバック

while len(ans) > 0:

    c[ans[-1]] += 1
    del ans[-1]
    i = len(ans)

    put = None
    for j in range(K[i]+1,10):
        if c[j] > 0:
            put = j
            break

    if put != None: #置いちゃって、後は貪欲
        c[put] -= 1
        ans.append(put)

        while len(ans) < N:
            put = None
            for j in range(10):
                if c[j] > 0:
                    put = j
                    break
            c[put] -= 1
            ans.append(put)
        print ("".join(map(str,ans)))
        sys.exit()

print (-1)
0