#include <bits/stdc++.h>
using namespace std;
const int Maxn = 30010;
const int Maxk = 210;
const int inf = 2147483647;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
int a[Maxn], n, k, f[Maxn][Maxk][4];
int main() {
  memset(f, -63, sizeof(f));
  n = read(), k = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  f[0][0][0] = f[0][0][1] = f[0][0][2] = f[0][0][3] = 0;
  for (int i = 1; i <= n; i++) {
    f[i][0][0] = f[i][0][1] = f[i][0][2] = f[i][0][3] = 0;
    for (int j = 1; j <= min(i, k); j++) {
      int t = 2;
      if (j == k || j == 1) t = 1;
      f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j - 1][3]) - t * a[i];
      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]) + t * a[i];
      f[i][j][3] = max(f[i - 1][j][3], f[i][j][2]);
      if (t == 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]);
      }
    }
  }
  printf("%d", max(max(f[n][k][0], f[n][k][1]), max(f[n][k][2], f[n][k][3])));
}
