#include <bits/stdc++.h>
using namespace std;
const int N = 4e6 + 50;
int n, m;
long long tmp[N], a[N];
int L[N], R[N];
bool del[N];
pair<long long, int> t[N], mx;
queue<long long> q;
bool inQue[N];
void Init() {
  scanf("%d%d", &n, &m);
  n--;
  long long pre;
  scanf("%I64d", &pre);
  for (int i = 1; i <= n; i++) {
    long long cur;
    scanf("%I64d", &cur);
    tmp[i] = cur - pre;
    pre = cur;
  }
  int top = 0;
  for (int i = 1; i <= n; i++) {
    long long r = tmp[i];
    if (!r) continue;
    if (!top && r < 0) continue;
    if (top && a[top] > 0 == r > 0)
      a[top] += r;
    else
      a[++top] = r;
  }
  if (top && a[top] < 0) top--;
  for (int i = 0; i <= top; i++) {
    R[i] = (i + 1) % (top + 1);
    L[i] = (i + top) % (top + 1);
  }
  n = top;
}
void AddQue(int u) {
  if (u && !inQue[u] && pair<long long, int>(abs(a[u]), u) <= mx)
    q.push(u), inQue[u] = true;
}
void Del(int u) {
  if (!u || del[u]) return;
  int l = L[u], r = R[u];
  R[l] = r;
  L[r] = l;
  del[u] = true;
}
void Merge(int u) {
  if (del[u]) return;
  int l = L[u], r = R[u];
  if (l && abs(a[l]) < abs(a[u])) return;
  if (r && abs(a[r]) < abs(a[u])) return;
  Del(l);
  Del(r);
  a[u] += a[l] + a[r];
  l&& r ? AddQue(u) : Del(u);
  AddQue(L[u]);
  AddQue(R[u]);
}
void Work() {
  while (true) {
    n = 0;
    for (int i = R[0]; i; i = R[i])
      if (!del[i]) t[++n] = pair<long long, int>(abs(a[i]), i);
    if (n <= m * 2 - 1) break;
    int mid = (n - (m * 2 - 1)) / 2;
    nth_element(t + 1, t + mid, t + n + 1);
    mx = t[mid];
    for (int i = R[0]; i; i = R[i]) AddQue(i);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      inQue[u] = false;
      Merge(u);
    }
  }
  long long ans = 0;
  for (int i = R[0]; i; i = R[i])
    if (a[i] > 0) ans += a[i];
  printf("%I64d\n", ans);
}
int main() {
  Init();
  Work();
  return 0;
}
