n,m = map(int,input().split()) w = list(map(int,input().split())) inf = -10 ** 8 """ dp1 = [[[inf] * 2 for _ in range(m+1)] for _ in range(n)] dp1[0][1][1] = 0 dp2 = [[[inf] * 2 for _ in range(m+1)] for _ in range(n)] dp2[0][0][0] = 0 for i in range(n-1): for j in range(m+1): dp1[i+1][j][0] = max(dp1[i][j][0],dp1[i][j][1]) dp2[i+1][j][0] = max(dp2[i][j][0],dp2[i][j][1]) if j > 0: dp1[i+1][j][1] = max(dp1[i][j-1][1] + w[i],dp1[i][j-1][0]) dp2[i+1][j][1] = max(dp2[i][j-1][1] + w[i],dp2[i][j-1][0]) dp1[-1][m][1] += w[-1] ans = max(dp1[-1][m][1],dp1[-1][m][0],dp2[-1][m][0],dp2[-1][m][1]) print(ans) """ dp1 = [[inf] * 2 for _ in range(m+1)] dp2 = [[inf] * 2 for _ in range(m+1)] dp1[1][1] = 0 dp2[0][0] = 0 for i in range(n-1): n1 = [[inf] * 2 for _ in range(m+1)] n2 = [[inf] * 2 for _ in range(m+1)] for j in range(m+1): n1[j][0] = max(dp1[j][0],dp1[j][1]) n2[j][0] = max(dp2[j][0],dp2[j][1]) if j > 0: n1[j][1] = max(dp1[j-1][1] + w[i],dp1[j-1][0]) n2[j][1] = max(dp2[j-1][1] + w[i],dp2[j-1][0]) dp1 = n1 dp2 = n2 dp1[m][1] += w[-1] ans = max(dp1[m][0],dp1[m][1],dp2[m][0],dp2[m][1]) print(ans)