#include <bits/stdc++.h>
using namespace std;
int n, k, cnt = 0;
long long a[4000000];
int nx[4000000], pr[4000000];
struct Cmp {
  bool operator()(const int &lhs, const int &rhs) const {
    long long lval = abs(a[nx[lhs]] - a[lhs]);
    long long rval = abs(a[nx[rhs]] - a[rhs]);
    if (lval != rval) return lval < rval;
    return lhs < rhs;
  }
};
set<int, Cmp> se;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < (int)(n); ++i) {
    long long x;
    scanf("%I64d", &x);
    if ((cnt == 1 && x <= a[0]) ||
        (cnt > 1 &&
         (x == a[cnt - 1] || (x > a[cnt - 1] && a[cnt - 1] > a[cnt - 2]) ||
          (x < a[cnt - 1] && a[cnt - 1] < a[cnt - 2]))))
      --cnt;
    a[cnt++] = x;
  }
  if (cnt & 1) --cnt;
  long long ans = 0;
  for (int i = 0; i < cnt; i += 2) ans += a[i + 1] - a[i];
  for (int i = 0; i < (int)(cnt); ++i) {
    nx[i] = i + 1;
    pr[i] = i - 1;
  }
  nx[cnt - 1] = -1;
  pr[0] = -1;
  for (int i = 0; i < (int)(cnt - 1); ++i) se.insert(i);
  k = cnt / 2 - k;
  for (int kk = 0; kk < (int)(k); ++kk) {
    int ind = *se.begin();
    if (pr[ind] != -1) se.erase(pr[ind]);
    se.erase(ind);
    se.erase(nx[ind]);
    ans -= abs(a[nx[ind]] - a[ind]);
    if (pr[ind] == -1) {
      if (nx[nx[ind]] != -1) {
        pr[nx[nx[ind]]] = -1;
      }
    } else {
      nx[pr[ind]] = nx[nx[ind]];
      if (nx[nx[ind]] != -1) {
        pr[nx[nx[ind]]] = pr[ind];
        se.insert(pr[ind]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
