#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int N = 4e6 + 10;
int stal[N], star[N];
long long a[N], A[N];
inline long long read() {
  long long x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-') ch = getchar();
  if (ch == '-') flag = -1, ch = getchar();
  while (isdigit(ch)) x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
int main() {
  int n = read(), k = read();
  for (int i = (1), iend = (n); i <= iend; i++) a[i] = read();
  int topl = 0, topr = 0, cnt = 0;
  for (int i = 1, j = 1; i < n; j++, i = j) {
    while (j <= n && a[j + 1] >= a[j]) j++;
    if (i == j) continue;
    while (topl && a[stal[topl]] >= a[i])
      A[++cnt] = a[stal[topl]] - a[star[topr]], topl--, topr--;
    stal[++topl] = i;
    while (topr && a[star[topr]] <= a[j])
      A[++cnt] = a[stal[topl]] - a[star[topr]], topl--, topr--;
    star[++topr] = j;
  }
  while (topl) A[++cnt] = a[stal[topl]] - a[star[topr]], topl--, topr--;
  if (cnt > k) nth_element(A + 1, A + k, A + cnt + 1);
  long long ans = 0;
  for (int i = (1), iend = (min(k, cnt)); i <= iend; i++) ans -= A[i];
  printf("%lld\n", ans);
}
