#include <bits/stdc++.h>
using namespace std;
int nxt[4000000], pre[4000000], pos[4000000];
int heap[4000000], n, k, sh, flag, size, u;
long long vl[4000000], ans;
bool cmp(int id1, int id2) {
  int ip1, ip2;
  ip1 = nxt[id1];
  ip2 = nxt[id2];
  return abs(vl[id1] - vl[ip1]) < abs(vl[id2] - vl[ip2]);
}
void heap_up(int id) {
  int ii = (id - 1) / 2;
  if (id > 0 && cmp(heap[id], heap[ii])) {
    swap(heap[id], heap[ii]);
    pos[heap[id]] = id;
    pos[heap[ii]] = ii;
    heap_up(ii);
  }
}
void heap_down(int id) {
  int l = 2 * id + 1, r = 2 * id + 2, ii = id;
  if (l < size && cmp(heap[l], heap[ii])) ii = l;
  if (r < size && cmp(heap[r], heap[ii])) ii = r;
  if (ii != id) {
    swap(heap[ii], heap[id]);
    pos[heap[ii]] = ii;
    pos[heap[id]] = id;
    heap_down(ii);
  }
}
void ins(int id) {
  double v1, v2;
  if (sh >= 0) {
    nxt[sh] = id;
    pre[id] = sh;
    heap[size++] = sh;
    heap_up(size - 1);
  }
}
void del(int id1, int id2) {
  int id, ip1, ip2;
  if (pos[id1] >= 0) {
    id = pos[id1];
    pos[id1] = -1;
    if (id != size - 1) {
      heap[id] = heap[size - 1];
      pos[heap[id]] = id;
    }
    size--;
    heap_up(id);
    heap_down(id);
  }
  if (pos[id2] >= 0) {
    id = pos[id2];
    pos[id2] = -1;
    if (id != size - 1) {
      heap[id] = heap[size - 1];
      pos[heap[id]] = id;
    }
    size--;
    heap_up(id);
    heap_down(id);
  }
  ip1 = pre[id1];
  ip2 = nxt[id2];
  if (ip1 >= 0) nxt[ip1] = ip2;
  if (ip2 >= 0) pre[ip2] = ip1;
  if (ip1 >= 0 && ip2 >= 0) {
    int id;
    if (pos[ip1] >= 0)
      id = pos[ip1];
    else {
      while (true) puts("orz");
      pos[ip1] = size;
      heap[size++] = ip1;
      id = size - 1;
    }
    heap_up(id);
    heap_down(id);
  } else if (pos[ip1] >= 0) {
    id = pos[ip1];
    pos[ip1] = -1;
    if (id != size - 1) {
      heap[id] = heap[size - 1];
      if (id != size - 1) pos[heap[id]] = id;
      size--;
    }
    heap_up(id);
    heap_down(id);
  }
}
int main() {
  int i, j, s, p, q, id;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; i++) scanf("%I64d", &vl[i]);
  memset(nxt, -1, sizeof(nxt));
  memset(pre, -1, sizeof(pre));
  flag = 1;
  sh = -1;
  size = 0;
  for (i = 0; i < n; i++) {
    if (flag == 1 && (i < n - 1 && vl[i] < vl[i + 1]) &&
        (i == 0 || vl[i] <= vl[i - 1])) {
      ins(i);
      sh = i;
      flag ^= 1;
    } else if (flag == 0 && (i > 0 && vl[i] >= vl[i - 1]) &&
               (i == n - 1 || vl[i] > vl[i + 1])) {
      ins(i);
      sh = i;
      flag ^= 1;
    }
  }
  sort(heap, heap + size, cmp);
  memset(pos, -1, sizeof(pos));
  for (i = 0; i < size; i++) pos[heap[i]] = i;
  while (size > 2 * k) {
    u = heap[0];
    del(u, nxt[u]);
  }
  for (i = 0; i < n; i++) {
    if (pos[i] >= 0) break;
  }
  flag = 0;
  ans = 0;
  for (; nxt[i] >= 0; i = nxt[i]) {
    id = nxt[i];
    if (flag == 0) ans += abs(vl[id] - vl[i]);
    flag ^= 1;
  }
  printf("%I64d\n", ans);
}
