結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 👑 H20H20
提出日時 2021-07-31 00:15:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 200 ms / 2,000 ms
コード長 1,799 bytes
コンパイル時間 681 ms
コンパイル使用メモリ 87,224 KB
実行使用メモリ 99,232 KB
最終ジャッジ日時 2023-10-14 09:23:37
合計ジャッジ時間 5,438 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,548 KB
testcase_01 AC 96 ms
71,580 KB
testcase_02 AC 94 ms
71,744 KB
testcase_03 AC 94 ms
71,832 KB
testcase_04 AC 94 ms
71,612 KB
testcase_05 AC 99 ms
71,620 KB
testcase_06 AC 97 ms
71,624 KB
testcase_07 AC 95 ms
71,656 KB
testcase_08 AC 96 ms
71,620 KB
testcase_09 AC 95 ms
71,644 KB
testcase_10 AC 97 ms
71,792 KB
testcase_11 AC 98 ms
71,396 KB
testcase_12 AC 97 ms
71,912 KB
testcase_13 AC 97 ms
71,772 KB
testcase_14 AC 95 ms
71,828 KB
testcase_15 AC 96 ms
71,408 KB
testcase_16 AC 200 ms
95,848 KB
testcase_17 AC 188 ms
98,336 KB
testcase_18 AC 149 ms
93,380 KB
testcase_19 AC 152 ms
93,140 KB
testcase_20 AC 170 ms
93,324 KB
testcase_21 AC 168 ms
93,176 KB
testcase_22 AC 173 ms
93,220 KB
testcase_23 AC 99 ms
73,724 KB
testcase_24 AC 192 ms
99,232 KB
testcase_25 AC 131 ms
83,512 KB
testcase_26 AC 138 ms
84,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
N,K = input().split()
N = int(N)
if len(K)>N:
    print(-1)
    exit()
K = str(K).zfill(N)
A = [0] + list(map(int, input().split())) 

ANS = collections.deque()
for i in range(N):
    v = int(K[i])
    if A[v]>0:
        ANS.append(v)
        A[v]-=1
    else:
        #より大きい値がある
        for j in range(int(K[i])+1,10):
            if A[j]>0:
                ANS.append(j)                
                A[j]-=1
                for k in range(len(ANS),N):
                    for j in range(10):
                        if A[j]>0:
                            ANS.append(j)
                            A[j]-=1
                            break
                print(''.join(map(str, ANS)))
                exit()
        #ないから遡る
        while len(ANS)>0:
            v = ANS.pop()
            A[v]+=1
            for j in range(v+1,10):
                if A[j]>0:                 
                    ANS.append(j)
                    A[j]-=1
                    for k in range(len(ANS),N):
                        for j in range(10):
                            if A[j]>0:
                                ANS.append(j)
                                A[j]-=1
                                break
                    print(''.join(map(str, ANS)))
                    exit()
        print(-1)
        exit()
while len(ANS)>0:
    v = ANS.pop()
    A[v]+=1
    for j in range(v+1,10):
        if A[j]>0:                 
            ANS.append(j)
            A[j]-=1
            for k in range(len(ANS),N):
                for j in range(10):
                    if A[j]>0:
                        ANS.append(j)
                        A[j]-=1
                        break
            print(''.join(map(str, ANS)))
            exit()
print(-1)
exit()
0