結果
問題 |
No.1630 Sorting Integers (Greater than K)
|
ユーザー |
![]() |
提出日時 | 2025-04-15 23:52:11 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,493 bytes |
コンパイル時間 | 259 ms |
コンパイル使用メモリ | 81,908 KB |
実行使用メモリ | 291,072 KB |
最終ジャッジ日時 | 2025-04-15 23:53:33 |
合計ジャッジ時間 | 4,755 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 11 TLE * 1 -- * 10 |
ソースコード
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()