#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4000000 + 10;
const long long INF = (long long)1e18;
long long first;
inline long long Read() {
  first = 0;
  char ch = getchar();
  while (!((ch >= '0' && ch <= '9'))) ch = getchar();
  while ((ch >= '0' && ch <= '9'))
    first = first * 10 + ch - '0', ch = getchar();
  return first;
}
int n, k, id[MAXN], pos[MAXN], tot, nex[MAXN], pre[MAXN];
long long a[MAXN], b[MAXN], Tohka;
inline void Up(int first) {
  for (; first != 1 && b[first] < b[first / 2]; first /= 2)
    swap(id[first], id[first / 2]), swap(pos[id[first]], pos[id[first / 2]]),
        swap(b[first], b[first / 2]);
}
int second, TT, tid;
long long tb;
inline void Down(int first) {
  tid = id[first], tb = b[first];
  for (; first * 2 <= tot;) {
    second = first * 2;
    if (b[second + 1] < b[second]) ++second;
    if (b[second] >= tb) break;
    b[first] = b[second];
    id[first] = id[second];
    pos[id[first]] = first;
    first = second;
  }
  b[first] = tb, id[first] = tid, pos[id[first]] = first;
}
inline void Erase(int first) {
  b[first] = b[tot], id[first] = id[tot];
  pos[id[first]] = first;
  --tot;
  b[tot + 1] = INF;
  if (first != 1 && b[first] < b[first / 2]) Up(first);
  Down(first);
}
int main() {
  scanf("%d%d", &tot, &k);
  a[n = 1] = -Read();
  long long first;
  for (int i = 2, f = 1; i <= tot; ++i) {
    first = -Read();
    if (f == 1)
      if (a[n] > first)
        a[++n] = first, f = 2;
      else
        a[n] = first;
    else if (a[n] > first)
      a[n] = first;
    else
      a[++n] = first, f = 1;
  }
  if (a[n - 1] < a[n]) --n;
  tot = 0;
  for (int i = 1; i < n; ++i) {
    id[++tot] = i, pos[i] = tot, b[i] = abs(a[i] - a[i + 1]), Up(tot);
    nex[i] = i + 1;
    pre[i + 1] = i;
    if (i & 1) Tohka += a[i] - a[i + 1];
  }
  b[tot + 1] = INF;
  k = n / 2 - k;
  if (k <= 0) cout << Tohka << endl, exit(0);
  int i, j;
  for (int tt = 1; tt <= k; ++tt) {
    i = id[1], j = nex[i];
    Tohka -= b[1];
    nex[pre[i]] = nex[j];
    pre[nex[j]] = pre[i];
    if (pre[i]) Erase(pos[pre[i]]);
    if (nex[j]) Erase(pos[j]);
    if (pre[i] && nex[j])
      pos[pre[i]] = 1, id[1] = pre[i], b[1] = (a[pre[i]] - a[nex[j]]),
      b[1] = (b[1] > 0) ? b[1] : -b[1], Down(1);
    else
      Erase(1);
  }
  cout << Tohka << endl;
  fclose(stdin);
  fclose(stdout);
  return 0;
}
