#!/usr/bin/env python3 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # %% import numpy as np INF = 10 ** 9 # %% N, M, *W = map(int, read().split()) # %% dp00 = np.full(N + 2, -INF, np.int64) dp01 = np.full(N + 2, -INF, np.int64) dp10 = np.full(N + 2, -INF, np.int64) dp11 = np.full(N + 2, -INF, np.int64) dp00[0] = 0 dp11[1] = 0 for x in W: newdp00 = np.full_like(dp00, -INF) newdp01 = np.full_like(dp00, -INF) newdp10 = np.full_like(dp00, -INF) newdp11 = np.full_like(dp00, -INF) np.maximum(newdp00, dp00, out=newdp00) np.maximum(newdp00, dp01, out=newdp00) np.maximum(newdp01[1:], dp00[:-1], out=newdp01[1:]) np.maximum(newdp01[1:], dp01[:-1] + x, out=newdp01[1:]) np.maximum(newdp10, dp10, out=newdp10) np.maximum(newdp10, dp11, out=newdp10) np.maximum(newdp11[1:], dp10[:-1], out=newdp11[1:]) np.maximum(newdp11[1:], dp11[:-1] + x, out=newdp11[1:]) dp00 = newdp00 dp01 = newdp01 dp10 = newdp10 dp11 = newdp11 # %% answer = max(dp00[M], dp11[M + 1]) print(answer)