#include <bits/stdc++.h>
using namespace std;
long long stonks[100005][2], sol[100005];
int len = 0, l = 0;
void afla(long long v1, long long v2) {
  for (int j = len - 2; j >= 0; j--) {
    if (v1 < stonks[j][0]) {
      sol[l++] = stonks[j][1] - stonks[j][0];
      stonks[j][0] = v1;
      stonks[j][1] = v2;
      len--;
    } else {
      if (v2 <= stonks[j][1]) {
        stonks[j + 1][0] = v1;
        stonks[j + 1][1] = v2;
        break;
      } else {
        sol[l++] = stonks[j][1] - v1;
        stonks[j][1] = v2;
        v1 = stonks[j][0];
        len--;
      }
    }
  }
}
int main() {
  int n, k, sir = 0;
  long long st1, st2, ans = 0;
  scanf("%d%d%lld", &n, &k, &st1);
  for (int i = 1; i < n; i++) {
    scanf("%lld", &st2);
    if (st1 <= st2) {
      if (sir == 0) {
        sir = 1;
        stonks[len][0] = st1;
      }
    } else {
      if (sir == 1) {
        sir = 0;
        stonks[len++][1] = st1;
        afla(stonks[len - 1][0], stonks[len - 1][1]);
      }
    }
    st1 = st2;
  }
  if (stonks[len][0] != 0 && sir == 1) {
    stonks[len++][1] = st2;
    afla(stonks[len - 1][0], stonks[len - 1][1]);
  }
  for (int i = 0; i < len; i++) {
    sol[l++] = stonks[i][1] - stonks[i][0];
  }
  sort(sol, sol + l);
  for (int i = 0; i < min(l, k); i++) {
    ans += sol[l - i - 1];
  }
  printf("%lld\n", ans);
  return 0;
}
