#include <bits/stdc++.h>
using namespace std;
long long n, k, ar[4500000];
long long p;
vector<pair<long long, long long> > tv;
vector<long long> vec, nvec;
vector<pair<long long, long long> > tvec;
long long p1, m;
long long todel, lwr, temp, temp1, temp2;
long long ans;
long long ttl;
pair<long long, long long> tp;
long cd[8500000];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  p = 1;
  for (int i = 2; i <= n; i++) {
    if (ar[i] > ar[i - 1]) continue;
    if (i != p + 1) {
      tv.push_back(make_pair(p, i - 1));
    }
    p = i;
  }
  if (ar[n] > ar[p]) tv.push_back(make_pair(p, n));
  vec.push_back(1e17);
  for (int i = 0; i < tv.size(); i++) {
    if (i > 0) vec.push_back(ar[tv[i - 1].second] - ar[tv[i].first]);
    vec.push_back(ar[tv[i].second] - ar[tv[i].first]);
  }
  for (int i = 0; i < tv.size(); i++) ans += ar[tv[i].second] - ar[tv[i].first];
  vec.push_back(1e17);
  ttl = tv.size();
  if (k < ttl)
    m = ttl - k;
  else
    m = 0;
  while (m > 0) {
    tvec.clear();
    for (int i = 0; i < vec.size(); i++) tvec.push_back(make_pair(vec[i], i));
    p1 = m;
    if (p1 >= vec.size()) p1 = vec.size();
    random_shuffle(tvec.begin(), tvec.end());
    nth_element(tvec.begin(), tvec.begin() + p1 - 1, tvec.end());
    for (int i = 0; i < vec.size(); i++) cd[i] = 0;
    for (int i = 0; i < p1; i++) cd[tvec[i].second] = 1;
    p1 = m * 3;
    if (p1 >= vec.size()) p1 = vec.size();
    nth_element(tvec.begin(), tvec.begin() + p1 - 1, tvec.end());
    lwr = tvec[p1 - 1].first;
    nvec.clear();
    for (int i = 0; i < vec.size(); i++) {
      if (i > 0 && i + 1 < vec.size() && vec[i] <= vec[i - 1] &&
          vec[i] <= vec[i + 1])
        continue;
      else
        cd[i] = 0;
    }
    while (vec.size()) {
      temp = vec.back();
      vec.pop_back();
      if (m > 0 && cd[vec.size()] == 1) {
        temp1 = vec.back();
        vec.pop_back();
        temp2 = nvec.back();
        nvec.pop_back();
        temp2 = temp2 + temp1 - temp;
        ans -= temp;
        nvec.push_back(temp2);
        if (vec.size()) {
          temp = vec.back();
          vec.pop_back();
          nvec.push_back(temp);
        }
        --m;
      } else if (nvec.size() > 1 && nvec[nvec.size() - 1] > lwr && temp > lwr) {
        nvec[nvec.size() - 1] += temp;
      } else
        nvec.push_back(temp);
    }
    vec = nvec;
  }
  cout << ans << endl;
  cin.get();
  cin.get();
  return 0;
}
