結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,668 KB
testcase_01 AC 98 ms
71,540 KB
testcase_02 AC 96 ms
71,672 KB
testcase_03 AC 95 ms
71,496 KB
testcase_04 AC 94 ms
71,576 KB
testcase_05 AC 94 ms
71,676 KB
testcase_06 AC 94 ms
71,576 KB
testcase_07 AC 94 ms
71,500 KB
testcase_08 AC 95 ms
71,792 KB
testcase_09 AC 96 ms
71,636 KB
testcase_10 AC 95 ms
71,732 KB
testcase_11 AC 97 ms
71,788 KB
testcase_12 AC 96 ms
71,760 KB
testcase_13 AC 96 ms
71,752 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 182 ms
94,408 KB
testcase_17 AC 198 ms
98,088 KB
testcase_18 WA -
testcase_19 AC 152 ms
93,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 102 ms
74,048 KB
testcase_24 AC 193 ms
99,196 KB
testcase_25 RE -
testcase_26 AC 142 ms
84,764 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
                print(''.join(map(str, ANS)))
                exit()
        #ないから遡る
        while len(ANS)>0:
            v = ANS.pop()
            A[v]+=1
            for j in range(ANS[-1]+1,10):
                if A[j]>0:
                    temp = ANS.pop()
                    A[temp]+=1                    
                    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(ANS[-1]+1,10):
        if A[j]>0:
            temp = ANS.pop()
            A[temp]+=1                    
            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