#include <bits/stdc++.h>
using namespace std;
const int N_ = 100010;
struct node {
  node() {}
  node(int mx, int mn, pair<int, int> rl, pair<int, int> lr)
      : mx(mx), mn(mn), rl(rl), lr(lr) {}
  long long mx, mn;
  pair<int, int> rl, lr;
} T[1 << 18];
struct str {
  str() {}
  str(long long x, int l, int r, int p, int m) : x(x), l(l), r(r), p(p), m(m) {}
  long long x;
  int l, r, p, m;
  bool operator<(const str &l) const { return x < l.x; }
};
priority_queue<str> pq;
int N, K;
long long inp[N_];
inline long long Get(pair<int, int> x) { return inp[x.first] - inp[x.second]; }
node Merge(node a, node b) {
  node ret;
  ret.mx = (inp[a.mx] > inp[b.mx] ? a.mx : b.mx);
  ret.mn = (inp[a.mn] < inp[b.mn] ? a.mn : b.mn);
  long long tmp = max(max(Get(a.rl), Get(b.rl)), inp[b.mx] - inp[a.mn]);
  if (tmp == inp[b.mx] - inp[a.mn])
    ret.rl = pair<int, int>(b.mx, a.mn);
  else if (tmp == Get(a.rl))
    ret.rl = a.rl;
  else
    ret.rl = b.rl;
  tmp = max(max(Get(a.lr), Get(b.lr)), inp[a.mx] - inp[b.mn]);
  if (tmp == inp[a.mx] - inp[b.mn])
    ret.lr = pair<int, int>(a.mx, b.mn);
  else if (tmp == Get(a.lr))
    ret.lr = a.lr;
  else
    ret.lr = b.lr;
  return ret;
}
void build(int s, int d, int rt) {
  if (s == d) {
    T[rt] = node(s, s, pair<int, int>(0, 0), pair<int, int>(0, 0));
    return;
  }
  int m = (s + d) >> 1;
  build(s, m, rt << 1);
  build(m + 1, d, rt << 1 | 1);
  T[rt] = Merge(T[rt << 1], T[rt << 1 | 1]);
}
node Query(int s, int d, int l, int r, int rt) {
  if (s <= l && r <= d) return T[rt];
  int m = (l + r) >> 1;
  if (s <= m && m < d)
    return Merge(Query(s, d, l, m, rt << 1),
                 Query(s, d, m + 1, r, rt << 1 | 1));
  if (s <= m) return Query(s, d, l, m, rt << 1);
  return Query(s, d, m + 1, r, rt << 1 | 1);
}
node Query(int s, int d) { return Query(s, d, 1, N, 1); }
int main() {
  scanf("%d%d", &N, &K);
  int i;
  for (i = 1; i <= N; i++) scanf("%lld", inp + i);
  build(1, N, 1);
  node tm = Query(1, N);
  pq.push(str(Get(tm.rl), 1, N, tm.rl.first, tm.rl.second));
  long long ans = 0;
  for (i = 1; i <= K; i++) {
    if (pq.empty()) break;
    str tmp = pq.top();
    pq.pop();
    if (tmp.x <= 0) break;
    ans += tmp.x;
    if (tmp.m < tmp.p) {
      if (tmp.m - tmp.l >= 2) {
        node u = Query(tmp.l, tmp.m - 1);
        pq.push(str(Get(u.rl), tmp.l, tmp.m - 1, u.rl.first, u.rl.second));
      }
      if (tmp.p - tmp.m >= 3) {
        node u = Query(tmp.m + 1, tmp.p - 1);
        pq.push(str(Get(u.lr), tmp.m + 1, tmp.p - 1, u.lr.first, u.lr.second));
      }
      if (tmp.r - tmp.p >= 2) {
        node u = Query(tmp.p + 1, tmp.r);
        pq.push(str(Get(u.rl), tmp.p + 1, tmp.r, u.rl.first, u.rl.second));
      }
    } else {
      if (tmp.p - tmp.l >= 2) {
        node u = Query(tmp.l, tmp.p - 1);
        pq.push(str(Get(u.lr), tmp.l, tmp.p - 1, u.lr.first, u.lr.second));
      }
      if (tmp.m - tmp.p >= 3) {
        node u = Query(tmp.p + 1, tmp.m - 1);
        pq.push(str(Get(u.rl), tmp.p + 1, tmp.m - 1, u.rl.first, u.rl.second));
      }
      if (tmp.r - tmp.m >= 2) {
        node u = Query(tmp.m + 1, tmp.r);
        pq.push(str(Get(u.lr), tmp.m + 1, tmp.r, u.lr.first, u.lr.second));
      }
    }
  }
  printf("%lld", ans);
  return 0;
}
