#include <bits/stdc++.h>
using namespace std;
struct torg {
  bool lose;
  long long cost;
  long long l, r;
};
torg a[100003];
long long p[3003];
int n, k;
int coun;
int trade;
int main() {
  scanf("%d %d", &n, &k);
  int i, j;
  for (i = 0; i < n; i++) scanf("%I64d", &p[i]);
  long long ans = 0;
  i = 0;
  while (i < n) {
    j = i + 1;
    while (j < n && p[j] >= p[j - 1]) j++;
    j--;
    if (i != j) {
      if (coun > 0) {
        a[coun].lose = true;
        a[coun].cost = a[coun - 1].r - p[i];
        coun++;
      }
      a[coun].l = p[i];
      a[coun].r = p[j];
      a[coun++].lose = false;
      trade++;
      ans += p[j] - p[i];
    }
    i = j + 1;
  }
  for (i = 0; i < trade - k; i++) {
    long long mini = (long long)1000000000 * 1000000000;
    int ind;
    for (j = 0; j < coun; j++) {
      if (a[j].lose == true && a[j].cost < mini) {
        ind = j;
        mini = a[j].cost;
      } else if (a[j].lose == false && a[j].r - a[j].l < mini) {
        ind = j;
        mini = a[j].r - a[j].l;
      }
    }
    ans -= mini;
    if (a[ind].lose == true) {
      a[ind - 1].r = a[ind + 1].r;
      for (j = ind + 2; j < coun; j++) a[j - 2] = a[j];
      coun -= 2;
    } else {
      if (ind == 0) {
        for (j = 2; j < coun; j++) a[j - 2] = a[j];
        coun -= 2;
      } else {
        a[ind - 1].cost = a[ind - 2].r - a[ind + 2].l;
        for (j = ind + 2; j < coun; j++) a[j - 2] = a[j];
        coun -= 2;
      }
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
