結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー ThetaTheta
提出日時 2024-04-16 19:24:35
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,997 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 82,408 KB
実行使用メモリ 848,856 KB
最終ジャッジ日時 2024-04-16 19:24:42
合計ジャッジ時間 5,290 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,552 KB
testcase_01 AC 39 ms
52,096 KB
testcase_02 AC 38 ms
52,352 KB
testcase_03 AC 37 ms
52,096 KB
testcase_04 AC 38 ms
52,224 KB
testcase_05 AC 39 ms
52,480 KB
testcase_06 AC 41 ms
51,840 KB
testcase_07 AC 37 ms
51,840 KB
testcase_08 AC 38 ms
52,096 KB
testcase_09 AC 38 ms
52,352 KB
testcase_10 AC 41 ms
52,736 KB
testcase_11 AC 38 ms
52,608 KB
testcase_12 AC 37 ms
52,096 KB
testcase_13 AC 38 ms
52,992 KB
testcase_14 AC 39 ms
52,812 KB
testcase_15 AC 39 ms
52,864 KB
testcase_16 MLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(600000)


def main():
    N, K = input().split()
    N = int(N)
    K = list(map(int, K))
    c = list(map(int, input().split()))

    # M_cand = inf
    # M_cand_list = []
    # # 最上位の桁の数値が違う中での最小をまず求める
    # for idx in range(int(K[0]), 9):
    #     if c[idx] > 0:
    #         M_cand_list.append(idx+1)
    # else:
    #     pass

    # if M_cand_list:
    #     c_copy = c.copy()
    #     c_copy[M_cand_list[0]-1] -= 1
    #     for num, ctr in enumerate(c, 1):
    #         for _ in range(ctr):
    #             M_cand_list.append(num)

    # 最上位の桁が同じ範囲での最小を求める
    def calc_min_M(digit_idx: int, rest: tuple[int, ...]) -> list[int]:
        if digit_idx == N - 1:
            for n, v in enumerate(rest, 1):
                if n <= K[-1]:
                    continue
                if v == 0:
                    continue
                return [n]
            return [-1]

        if rest[K[digit_idx] - 1] > 0:
            new_rest = list(rest)
            new_rest[K[digit_idx] - 1] -= 1
            res = calc_min_M(digit_idx + 1, tuple(new_rest))
            if res != [-1]:
                res.append(K[digit_idx])
                return res
        min_over_n = -1
        for n, v in enumerate(rest, 1):
            if n <= K[digit_idx]:
                continue
            if v > 0:
                min_over_n = n
                break
        else:
            return [-1]

        res = []
        for n, v in reversed(list(enumerate(rest, 1))):
            if n == min_over_n:
                for _ in range(v - 1):
                    res.append(n)
            else:
                for _ in range(v):
                    res.append(n)
        res.append(min_over_n)
        return res

    ans = calc_min_M(0, tuple(c))
    if ans == [-1]:
        print(-1)
    else:
        print("".join(map(str, reversed(ans))))


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