結果

問題 No.1247 ブロック登り
ユーザー rin204rin204
提出日時 2022-07-06 22:33:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,587 ms / 3,000 ms
コード長 2,806 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 82,484 KB
実行使用メモリ 504,228 KB
最終ジャッジ日時 2024-06-02 11:43:05
合計ジャッジ時間 30,426 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,120 KB
testcase_01 AC 33 ms
52,224 KB
testcase_02 AC 38 ms
58,112 KB
testcase_03 AC 33 ms
52,616 KB
testcase_04 AC 51 ms
67,712 KB
testcase_05 AC 42 ms
62,208 KB
testcase_06 AC 33 ms
52,736 KB
testcase_07 AC 241 ms
80,824 KB
testcase_08 AC 237 ms
81,888 KB
testcase_09 AC 177 ms
79,080 KB
testcase_10 AC 201 ms
79,760 KB
testcase_11 AC 2,587 ms
503,452 KB
testcase_12 AC 2,285 ms
484,540 KB
testcase_13 AC 2,581 ms
503,584 KB
testcase_14 AC 2,241 ms
504,092 KB
testcase_15 AC 2,150 ms
504,228 KB
testcase_16 AC 2,087 ms
503,708 KB
testcase_17 AC 2,201 ms
503,964 KB
testcase_18 AC 1,952 ms
465,284 KB
testcase_19 AC 633 ms
145,208 KB
testcase_20 AC 443 ms
105,300 KB
testcase_21 AC 128 ms
79,068 KB
testcase_22 AC 260 ms
86,072 KB
testcase_23 AC 258 ms
84,420 KB
testcase_24 AC 42 ms
59,648 KB
testcase_25 AC 2,045 ms
502,284 KB
testcase_26 AC 1,935 ms
502,156 KB
testcase_27 AC 2,153 ms
502,772 KB
testcase_28 AC 2,184 ms
503,288 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))
            else:
                pass
            if l != r:
                if x >= 2:
                    p2 = f(l, r, x - 2, 0)
                    dp[p2] = max(dp[p2], dp[p])
                else:
                    pass
                if x - (r - l) >= 0:
                    p2 = f(l, r, x - (r - l), 1)
                    dp[p2] = max(dp[p2], dp[p])
                else:
                    pass
            else:
                pass

            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))
            else:
                pass
            if l != r:
                if x >= 2:
                    p2 = f(l, r, x - 2, 1)
                    dp[p2] = max(dp[p2], dp[p])
                else:
                    pass
                if x - (r - l) >= 0:
                    p2 = f(l, r, x - (r - l), 0)
                    dp[p2] = max(dp[p2], dp[p])
                else:
                    pass

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
            else:
                pass
            if abs(r - i) <= K:
                p = f(l, r, abs(r - i), 1)
                ans = max(ans, dp[p])
            else:
                pass
            if abs(i - l) <= K:
                p = f(l, r, abs(i - l), 0)
                ans = max(ans, dp[p])
            else:
                pass
    print(ans)

0