#include <bits/stdc++.h>
using namespace std;
int a[30005], dp[30005][205][4], ans, n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  memset(dp, 0x80, sizeof(dp));
  for (int i = 0; i <= n; i++)
    for (int k = 0; k <= 3; k++) dp[i][0][k] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int s = (j == 1 || j == m) ? a[i] : a[i] * 2;
      dp[i][j][0] = max(dp[i - 1][j - 1][2], dp[i - 1][j][0]) + s;
      dp[i][j][1] = max(dp[i - 1][j - 1][3], dp[i - 1][j][1]) - s;
      dp[i][j][2] = max(dp[i][j][1], dp[i - 1][j][2]);
      dp[i][j][3] = max(dp[i][j][0], dp[i - 1][j][3]);
      if (j != 1 && j != m) {
        dp[i][j][2] = max(dp[i - 1][j - 1][2], dp[i][j][2]);
        dp[i][j][3] = max(dp[i - 1][j - 1][3], dp[i][j][3]);
      }
    }
  }
  printf("%d\n", max(dp[n][m][2], dp[n][m][3]));
  return 0;
}
