#include <bits/stdc++.h>
using namespace std;
const int N = 4000010;
long long a[N], ans = 0, b[N], l = 0;
int sl[N], sr[N], tl = 0, tr = 0;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  for (int i = 1, j; i < n; i = j + 1) {
    j = i;
    while (j <= n && a[j + 1] > a[j]) j++;
    if (i == j) continue;
    while (tl && a[sl[tl]] > a[i]) b[++l] = a[sl[tl--]] - a[sr[tr--]];
    sl[++tl] = i;
    while (tr && a[sr[tr]] < a[j]) b[++l] = a[sl[tl--]] - a[sr[tr--]];
    sr[++tr] = j;
  }
  while (tr) b[++l] = a[sl[tl--]] - a[sr[tr--]];
  if (k > l) k = l;
  nth_element(b + 1, b + k, b + l + 1);
  for (int i = 1; i <= k; i++) ans -= b[i];
  printf("%I64d\n", ans);
  return 0;
}
