#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, oo = inf;
inline long long read() {
  register long long x = 0;
  char f = 0;
  register char c = getchar();
  for (; !isdigit(c); c = getchar()) f |= (c == '-');
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  return f ? -x : x;
}
inline void write(long long x) {
  if (x < 0) x = -x, putchar('-');
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(long long x) {
  write(x);
  puts("");
}
inline void writeln(long long x, char c, long long y) {
  write(x);
  putchar(c);
  writeln(y);
}
inline void writeln(long long x, char c, long long y, char d, long long z) {
  write(x);
  putchar(c);
  write(y);
  putchar(d);
  writeln(z);
}
const int maxn = 4e5 + 233;
int n, k, m;
long long a[maxn], b[maxn];
long long sl[maxn], sr[maxn];
int tl, tr;
int main() {
  n = read(), k = read();
  for (register int i = (1); i <= (n); ++i) a[i] = read();
  tl = tr = 0;
  for (int i = 1, j; i < n; i = j + 1) {
    for (j = i; j <= n && a[j + 1] > a[j]; ++j)
      ;
    if (i == j) continue;
    assert(i < j);
    while (tl && sl[tl] > a[i]) b[++m] = sr[tr--] - sl[tl--];
    sl[++tl] = a[i];
    while (tr && sr[tr] < a[j]) b[++m] = sr[tr--] - sl[tl--];
    sr[++tr] = a[j];
  }
  while (tr) b[++m] = sr[tr--] - sl[tl--];
  k = min(k, m);
  nth_element(b + 1, b + k, b + 1 + m, greater<long long>());
  long long ans = 0;
  for (register int i = (1); i <= (k); ++i) ans += b[i];
  writeln(ans);
  return 0;
  return 0;
}
