#include <bits/stdc++.h>
using namespace std;
const int N = 30010;
int f[N][205][4], a[N];
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  memset(f, -0x3f, sizeof f);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= 3; j++) f[i][0][j] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      int xi = 1 + (j != 1 && j != k);
      f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j - 1][3]) + a[i] * xi;
      f[i][j][1] = max(f[i - 1][j][1], f[i][j][0]);
      f[i][j][2] = max(f[i - 1][j][2], f[i - 1][j - 1][1]) - a[i] * xi;
      f[i][j][3] = max(f[i - 1][j][3], f[i][j][2]);
      if (xi == 2) {
        f[i][j][1] = max(f[i][j][1], f[i - 1][j - 1][1]);
        f[i][j][3] = max(f[i][j][3], f[i - 1][j - 1][3]);
      }
    }
  }
  int ans = -2e9;
  for (int i = 0; i < 4; i++) ans = max(ans, f[n][k][i]);
  cout << ans;
  return 0;
}
