結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー lam6er
提出日時 2025-04-15 23:55:05
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,493 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 81,812 KB
実行使用メモリ 64,448 KB
最終ジャッジ日時 2025-04-15 23:56:22
合計ジャッジ時間 5,185 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 11 TLE * 1 -- * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    N = int(input[0])
    K = input[1]
    counts = list(map(int, input[2:11]))
    
    if len(K) != N:
        if len(K) < N:
            res = []
            for d in range(1, 10):
                res.extend([str(d)] * counts[d-1])
            print(''.join(res))
        else:
            print(-1)
        return
    
    max_num = []
    for d in range(9, 0, -1):
        max_num.extend([str(d)] * counts[d-1])
    max_num_str = ''.join(max_num)
    if max_num_str <= K:
        print(-1)
        return
    
    current_counts = counts.copy()
    prefix = []
    best_candidate = None
    
    for i in range(N):
        current_digit = int(K[i])
        for d in range(current_digit + 1, 10):
            if current_counts[d-1] > 0:
                temp_counts = current_counts.copy()
                temp_counts[d-1] -= 1
                suffix = []
                for digit in range(1, 10):
                    suffix.extend([str(digit)] * temp_counts[digit-1])
                candidate = ''.join(prefix) + str(d) + ''.join(suffix)
                if (best_candidate is None) or (candidate < best_candidate):
                    best_candidate = candidate
        if current_counts[current_digit-1] == 0:
            break
        current_counts[current_digit-1] -= 1
        prefix.append(str(current_digit))
    
    print(best_candidate if best_candidate is not None else -1)

if __name__ == "__main__":
    main()
0