結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー chineristAC
提出日時 2021-07-30 20:51:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 269 ms / 2,000 ms
コード長 1,119 bytes
コンパイル時間 475 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 104,512 KB
最終ジャッジ日時 2024-09-15 22:43:04
合計ジャッジ時間 4,183 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd,log

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

def solve(N,K,c):
    if len(K) > N:
        return -1

    if N > len(K):
        ans = []
        for j in range(10):
            ans += [str(j)] * c[j]
        return "".join(ans)

    check_c = [0 for i in range(10)]
    for i in range(len(K)):
        check_c[int(K[i])] += 1

    for d in range(len(K)-1,-1,-1):
        check_c[int(K[d])] -= 1
        if any(c[i] < check_c[i] for i in range(10)):
            continue

        for i in range(int(K[d])+1,10):
            if c[i] > check_c[i]:
                ans = [K[:d],str(i)]
                #print(ans,check_c,c)
                c[i] -= 1
                for j in range(10):
                    c[j] -= check_c[j]
                    ans += [str(j)] * c[j]
                return "".join(ans)

    return -1

N,K = input().split()
N = int(N)
c = [0] + li()
print(solve(N,K,c))
0