結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー titiatitia
提出日時 2021-07-30 23:20:39
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,264 ms / 2,000 ms
コード長 1,262 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 10,912 KB
実行使用メモリ 48,572 KB
最終ジャッジ日時 2023-10-14 08:22:04
合計ジャッジ時間 7,889 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,172 KB
testcase_01 AC 16 ms
8,276 KB
testcase_02 AC 17 ms
8,196 KB
testcase_03 AC 16 ms
8,276 KB
testcase_04 AC 16 ms
8,236 KB
testcase_05 AC 17 ms
8,168 KB
testcase_06 AC 16 ms
8,128 KB
testcase_07 AC 17 ms
8,284 KB
testcase_08 AC 16 ms
8,140 KB
testcase_09 AC 17 ms
8,176 KB
testcase_10 AC 17 ms
8,168 KB
testcase_11 AC 17 ms
8,216 KB
testcase_12 AC 16 ms
8,120 KB
testcase_13 AC 16 ms
8,172 KB
testcase_14 AC 17 ms
8,288 KB
testcase_15 AC 17 ms
8,168 KB
testcase_16 AC 1,050 ms
48,520 KB
testcase_17 AC 1,264 ms
48,500 KB
testcase_18 AC 565 ms
48,568 KB
testcase_19 AC 551 ms
48,572 KB
testcase_20 AC 600 ms
48,460 KB
testcase_21 AC 606 ms
48,428 KB
testcase_22 AC 20 ms
9,884 KB
testcase_23 AC 20 ms
9,536 KB
testcase_24 AC 1,255 ms
40,284 KB
testcase_25 AC 20 ms
9,564 KB
testcase_26 AC 20 ms
9,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

ANS=""
for i in range(9):
    ANS+=str(i+1)*C[i]

if (ANS>K and len(ANS)==len(K)) or len(ANS)>len(K):
    print(ANS)
    exit()

if (ANS[::-1]<=K and len(ANS)==len(K)) or len(ANS)<len(K):
    print(-1)
    exit()

S=K
ANS=[-1]*len(S)

for i in range(len(S)):
    x=int(S[i])


    if x!=0 and C[x-1]>0 and i!=len(S)-1:
        ANS[i]=x
        C[x-1]-=1

    else:        
        for j in range(x,9):
            if C[j]>0:
                C[j]-=1
                ANS[i]=j+1
                break
            
        else:
            for j in range(i-1,-1,-1):
                C[ANS[j]-1]+=1
                ANS[j]=-1

                x=int(S[j])
                flag=0

                for k in range(x,9):
                    if C[k]>0:
                        C[k]-=1
                        ANS[j]=k+1
                        flag=1
                        break

                if flag:
                    break

        break

#print(ANS,C)

for i in range(len(S)):
    if ANS[i]==-1:
        for j in range(9):
            if C[j]>0:
                C[j]-=1
                ANS[i]=j+1
                break

print("".join(map(str,ANS)))
                
                    
                    
0