#include <bits/stdc++.h>
using namespace std;
const bool DBG = 1;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vpdd = vector<pdd>;
using vvpdd = vector<vpdd>;
template <typename T>
void ckmin(T& a, const T& b) {
  a = min(a, b);
}
template <typename T>
void ckmax(T& a, const T& b) {
  a = max(a, b);
}
void solve() {}
typedef struct node {
  ll gl;
  bool invalid;
  node* left = NULL;
  node* right = NULL;
} node;
class NodeCompare {
 public:
  bool operator()(const node* lhs, const node* rhs) {
    return lhs->gl > rhs->gl;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(15);
  int N, k;
  cin >> N >> k;
  vll p(N);
  for (int i = 0; i < (N); i++) cin >> p[i];
  ll z = 0;
  vll second(1, 5e18);
  second.push_back(0);
  bool up = 1;
  for (int i = (1); i < (N); i++) {
    if (up) {
      if (p[i] - p[i - 1] >= 0) {
        second.back() += (p[i] - p[i - 1]);
        z += (p[i] - p[i - 1]);
      } else {
        if (second[1] > 0) {
          second.push_back(p[i - 1] - p[i]);
          up = 0;
        }
      }
    } else {
      if (p[i] - p[i - 1] < 0) {
        second.back() += (p[i - 1] - p[i]);
      } else {
        second.push_back(p[i] - p[i - 1]);
        z += (p[i] - p[i - 1]);
        up = 1;
      }
    }
  }
  if (!up) second.pop_back();
  second.push_back(5e18);
  int t = (second.size() - 1) / 2;
  vector<node> sn(second.size());
  priority_queue<node*, vector<node*>, NodeCompare> pq;
  for (int i = 0; i < (second.size()); i++) {
    sn[i].gl = second[i];
    if (i > 0) {
      sn[i].left = &sn[i - 1];
      sn[i - 1].right = &sn[i];
    }
    pq.push(&sn[i]);
  }
  while (t > k) {
    auto mnode = pq.top();
    pq.pop();
    if (mnode->invalid) continue;
    z -= mnode->gl;
    t--;
    mnode->gl = mnode->left->gl - mnode->gl + mnode->right->gl;
    mnode->left->invalid = true;
    mnode->right->invalid = true;
    mnode->left = mnode->left->left;
    mnode->right = mnode->right->right;
    if (mnode->left != NULL) mnode->left->right = mnode;
    if (mnode->right != NULL) mnode->right->left = mnode;
    pq.push(mnode);
  }
  cout << z << endl;
  return 0;
}
