結果

問題 No.1247 ブロック登り
ユーザー 👑 rin204rin204
提出日時 2022-07-06 22:31:11
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,384 bytes
コンパイル時間 797 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 505,960 KB
最終ジャッジ日時 2023-08-24 22:28:34
合計ジャッジ時間 39,221 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,408 KB
testcase_01 AC 69 ms
71,284 KB
testcase_02 AC 74 ms
75,004 KB
testcase_03 AC 69 ms
71,288 KB
testcase_04 AC 86 ms
76,120 KB
testcase_05 AC 79 ms
75,116 KB
testcase_06 AC 72 ms
71,488 KB
testcase_07 AC 305 ms
83,300 KB
testcase_08 AC 306 ms
83,720 KB
testcase_09 AC 235 ms
80,312 KB
testcase_10 AC 259 ms
81,308 KB
testcase_11 TLE -
testcase_12 AC 2,828 ms
486,132 KB
testcase_13 AC 2,946 ms
505,600 KB
testcase_14 AC 2,679 ms
505,960 KB
testcase_15 AC 2,505 ms
505,252 KB
testcase_16 AC 2,493 ms
505,360 KB
testcase_17 AC 2,861 ms
505,496 KB
testcase_18 AC 2,593 ms
467,268 KB
testcase_19 AC 727 ms
147,160 KB
testcase_20 AC 535 ms
107,028 KB
testcase_21 AC 174 ms
80,192 KB
testcase_22 AC 322 ms
87,676 KB
testcase_23 AC 320 ms
85,992 KB
testcase_24 AC 77 ms
74,932 KB
testcase_25 AC 2,414 ms
503,568 KB
testcase_26 AC 2,380 ms
503,780 KB
testcase_27 AC 2,548 ms
504,788 KB
testcase_28 AC 2,599 ms
504,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
k, k - 1, ..., 1 の順番に割り当てていく
既に割り当ててるところに上書きはできない

dp[l][r][x][t]
    区間[l, r]に数字を割り当て
    x 以上の数字を割り当て済み
    t = 0 -> 現在左端
    t = 1 -> 現在右端

初期値は
dp[i][i][k][0] = dp[i][i][k][1] = A[i] * k
dp[l][r][x][t] からの遷移
    dp[l][r + 1][x - 1][t] (t == 1)
        r + 1 に x - 1 を割り当てる
    dp[l - 1][r][x - 1][t] (t == 0)
        l - 1 に x - 1 を割り当てる
    dp[l][r][x - 2][t] (l != r)
        2手消費する
    dp[l][r][x - (r - l)][1 - t] (l != r)
        反対側に移動する

i 番目の答えは
    l <= i <= r
    dp[l][r][r - i][1]
    dp[l][r][i - l][0]
    の中の最小値
"""
n, K = map(int, input().split())
A = list(map(int, input().split()))
inf =  1 << 30
dp = [-inf] * (n * n * (K + 1) * 2)
f = lambda i, j, k, l: ((i * n + j) * (K + 1) + k) * 2 + l
for i, a in enumerate(A):
    dp[f(i, i, K, 0)] = a * K
    dp[f(i, i, K, 1)] = a * K

for x in range(K, 0, -1):
    for l in range(n):
        for r in range(l, n):
            p = f(l, r, x, 0)
            if l != 0:
                p2 = f(l - 1, r, x - 1, 0)
                dp[p2] = max(dp[p2], dp[p] + A[l - 1] * (x - 1))
            if l != r:
                if x >= 2:
                    p2 = f(l, r, x - 2, 0)
                    dp[p2] = max(dp[p2], dp[p])
                if x - (r - l) >= 0:
                    p2 = f(l, r, x - (r - l), 1)
                    dp[p2] = max(dp[p2], dp[p])

            p = f(l, r, x, 1)
            if r != n - 1:
                p2 = f(l, r + 1, x - 1, 1)
                dp[p2] = max(dp[p2], dp[p] + A[r + 1] * (x - 1))
            if l != r:
                if x >= 2:
                    p2 = f(l, r, x - 2, 1)
                    dp[p2] = max(dp[p2], dp[p])
                if x - (r - l) >= 0:
                    p2 = f(l, r, x - (r - l), 0)
                    dp[p2] = max(dp[p2], dp[p])

for i in range(n):
    ans = -1 << 30
    for l in range(min(i + 2, n)):
        for r in range(max(0, i - 1), n):
            if l > r:
                continue
            if abs(r - i) <= K:
                p = f(l, r, abs(r - i), 1)
                ans = max(ans, dp[p])
            if abs(i - l) <= K:
                p = f(l, r, abs(i - l), 0)
                ans = max(ans, dp[p])
    print(ans)

0