#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &n) {
  n = 0;
  T f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (isdigit(c)) n = n * 10 + c - '0', c = getchar();
  n *= f;
}
int n, m, cnt, tot;
int pre[4000005], nxt[4000005], vis[4000005], in[4000005];
long long a[4000005], b[4000005];
queue<int> q;
pair<long long, int> st[4000005], tmp;
void push(int x) {
  if (!in[x] && (make_pair(abs(a[x]), x)) <= tmp) q.push(x), in[x] = 1;
}
void init() {
  for (int i = 2; i <= n; ++i) {
    if (a[i] != a[i - 1]) {
      b[++cnt] = a[i] - a[i - 1];
      if (cnt <= 1 && b[cnt] <= 0) cnt--;
      if (cnt > 1 && ((b[cnt] > 0) == (b[cnt - 1] > 0))) {
        cnt--;
        b[cnt] += b[cnt + 1];
      }
    }
  }
  while (cnt && b[cnt] <= 0) cnt--;
  n = cnt;
  for (int i = 1; i <= n; ++i) a[i] = b[i];
  for (int i = 0; i < n; ++i) nxt[i] = (i + 1) % (n + 1);
  for (int i = 1; i <= n; ++i) pre[i] = (i - 1);
  pre[0] = n, nxt[n] = 0;
  a[0] = 0;
}
void merge(int x) {
  int l = pre[x], r = nxt[x];
  if (vis[x]) return;
  if (l && abs(a[l]) < abs(a[x])) return;
  if (r && abs(a[r]) < abs(a[x])) return;
  if (l) pre[x] = pre[l];
  if (r) nxt[x] = nxt[r];
  nxt[pre[x]] = x, pre[nxt[x]] = x;
  vis[l] = vis[r] = 1;
  a[x] += a[l] + a[r];
  if (!l) {
    nxt[0] = nxt[x];
    pre[nxt[x]] = 0;
    vis[x] = 1;
  }
  if (!r) {
    pre[0] = pre[x];
    nxt[pre[x]] = 0;
    vis[x] = 1;
  }
  if (pre[x]) push(pre[x]);
  if (nxt[x]) push(nxt[x]);
  push(x);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) read(a[i]);
  init();
  while (1) {
    tot = 0;
    for (int i = nxt[0]; i; i = nxt[i]) {
      if (!vis[i]) st[++tot] = (make_pair(abs(a[i]), i));
    }
    if (tot < m * 2) break;
    int mid = (tot - m * 2 + 1) / 2;
    nth_element(st + 1, st + mid, st + tot + 1);
    tmp = st[mid];
    while (!q.empty()) q.pop();
    for (int i = nxt[0]; i; i = nxt[i]) push(i);
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      in[x] = 0;
      merge(x);
    }
  }
  long long ans = 0;
  for (int i = nxt[0]; i; i = nxt[i]) {
    if (a[i] > 0) ans += a[i];
  }
  cout << ans << endl;
  return 0;
}
