#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int NS = 4000010;
const int MS = 19;
const int MOD = 1000000007;
const double PI = acos(-1.0);
const long long INF = (1LL << 60);
long long arr[NS];
stack<pair<int, int> > vp;
vector<long long> g;
long long maxProfit(int k, int len, long long* a) {
  int n = len;
  while (!vp.empty()) vp.pop();
  g.clear();
  for (int v = 0, p; v < n; v = p + 1) {
    for (; v + 1 < n && a[v] >= a[v + 1]; v++)
      ;
    for (p = v + 1; p + 1 < n && a[p] <= a[p + 1]; p++)
      ;
    if (p >= n) break;
    for (; !vp.empty() && a[vp.top().first] >= a[v]; vp.pop())
      g.push_back(a[vp.top().second] - a[vp.top().first]);
    for (; !vp.empty() && a[vp.top().second] <= a[p]; vp.pop()) {
      long long x = a[vp.top().second] - a[v];
      if (x > 0) g.push_back(x);
      v = vp.top().first;
    }
    vp.push(make_pair(v, p));
  }
  for (; !vp.empty(); vp.pop())
    g.push_back(a[vp.top().second] - a[vp.top().first]);
  k = min(k, (int)g.size());
  if (k > 0)
    nth_element(g.begin(), g.begin() + k - 1, g.end(), greater<long long>());
  long long ans = 0;
  for (int i = 0; i < k; i++) {
    ans += g[i];
  }
  return ans;
}
int main() {
  int n, k;
  while (~scanf("%d %d", &n, &k)) {
    long long vt;
    for (int i = 0; i < n; i++) {
      scanf("%I64d", &arr[i]);
    }
    long long ans = maxProfit(k, n, arr);
    printf("%I64d\n", ans);
  }
  return 0;
}
