#include int n, m, w[3000]; int dp[3001][4]; int max(int a, int b) { return (a > b ? a : b); } int main(void) { scanf("%d%d", &n, &m); if(m < 2) { printf("0\n"); return 0; } int i, j; for(i = 0; i < n; i++) { scanf("%d", &w[i]); } dp[0][3] = w[0]; for(i = 2; i < n; i++) { for(j = i - 1; 0 <= j; j--) { int a = dp[j - 1][3], b = dp[j - 1][1]; int w1 = w[i - 1], w2 = (i == n - 1 ? w[i] : 0); if(j == i - 1) { dp[j][3] = a + w1 + w2; dp[j][2] = a; dp[j][1] = b + w1; dp[j][0] = b; } else if(j == 0) { dp[0][3] = w2; } else { dp[j][3] = max(dp[j][2], a + w1) + w2; dp[j][2] = max(dp[j][2], a); dp[j][1] = max(dp[j][0], b + w1); dp[j][0] = max(dp[j][0], b); } } } printf("%d\n", max( max(dp[m][0], dp[m - 1][1]), max(dp[m - 1][2], dp[m - 2][3]) ) ); return 0; }