#include <bits/stdc++.h>
using namespace std;
int n, k, a[30010], pre[30010];
int now, f[2][30010], g[2][30010], s, p, c, ans;
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", a + i), pre[i] = pre[i - 1] + a[i];
  memset(f, 0xc0, sizeof(f));
  memset(g, 0xc0, sizeof(g));
  for (int i = 1; i <= n; i++)
    f[now][i] = max(f[now][i - 1], -pre[i - 1]),
    g[now][i] = max(g[now][i - 1], pre[i - 1]);
  for (int i = 1; i <= n; i++) f[now][i] += pre[i], g[now][i] -= pre[i];
  ans = 0;
  for (int t = 2; t <= k; t++) {
    c = s = p = g[now][0];
    for (int i = 1; i <= n; i++) {
      if (t == k) ans = max(c + pre[i], ans);
      f[now ^ 1][i] = max(s + pre[i] * 2, f[now][i - 1]);
      p = max(g[now][i], p);
      s = max(p - pre[i] * 2, s);
      c = max(p - pre[i], c);
    }
    c = s = p = f[now][0];
    for (int i = 1; i <= n; i++) {
      if (t == k) ans = max(c - pre[i], ans);
      g[now ^ 1][i] = max(s - pre[i] * 2, g[now][i - 1]);
      p = max(f[now][i], p);
      s = max(p + pre[i] * 2, s);
      c = max(p + pre[i], c);
    }
    memset(f[now], 0xc0, sizeof(f[now]));
    memset(g[now], 0xc0, sizeof(g[now]));
    now ^= 1;
  }
  printf("%d", ans);
  return 0;
}
