#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, u;
int a[30010], f[30010], g[30010], h[30010], w[30010];
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] += a[i - 1];
  int ma = -1000000000, mi = 1000000000;
  for (i = 0; i <= n; i++) {
    if (!i)
      f[i] = g[i] = -1000000000;
    else {
      int zhi = a[i];
      f[i] = -(zhi - ma);
      g[i] = zhi - mi;
    }
    ma = max(ma, a[i]);
    mi = min(mi, a[i]);
  }
  for (i = 2; i <= m - 1; i++) {
    for (j = 0; j <= n; j++) h[j] = w[j] = -1000000000;
    int ma0, ma1, Ma0, Ma1;
    ma0 = ma1 = Ma0 = Ma1 = -1000000000;
    for (j = 1; j <= n; j++) {
      int zhi = a[j];
      h[j] = max(ma0, ma1 - 2 * zhi);
      w[j] = max(Ma0, Ma1 + 2 * zhi);
      ma0 = max(ma0, f[j]);
      Ma0 = max(Ma0, g[j]);
      ma1 = max(ma1, Ma0 + 2 * zhi);
      Ma1 = max(Ma1, ma0 - 2 * zhi);
    }
    for (j = 0; j <= n; j++) f[j] = h[j], g[j] = w[j];
  }
  {
    int an = -1000000000;
    int ma0, ma1, Ma0, Ma1;
    ma0 = ma1 = Ma0 = Ma1 = -1000000000;
    for (j = 1; j <= n; j++) {
      int zhi = a[j];
      an = max(an, ma1 + zhi);
      an = max(an, Ma1 - zhi);
      ma0 = max(ma0, f[j]);
      Ma0 = max(Ma0, g[j]);
      ma1 = max(ma1, ma0 - zhi);
      Ma1 = max(Ma1, Ma0 + zhi);
    }
    printf("%d\n", an);
  }
  return 0;
}
