""" 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)