#include <bits/stdc++.h>
using namespace std;
long long int max1(long long int x, long long int y) {
  if (x > y)
    return x;
  else
    return y;
}
int main() {
  long long int n, k;
  cin >> n >> k;
  long long int i, j;
  vector<vector<long long int> > profit(k + 1, vector<long long int>(n, 0));
  vector<long long int> price;
  i = n;
  while (i--) {
    cin >> j;
    price.push_back(j);
  }
  for (i = 1; i <= k; i++) {
    long long int maxdiff;
    for (j = 1; j < n; j++) {
      if (j == 1) {
        if (price[j] > price[0]) profit[i][j] = price[j] - price[0];
        maxdiff = -price[0];
      } else {
        profit[i][j] =
            max1(profit[i][j - 1],
                 price[j] + max1(maxdiff, profit[i - 1][j - 2] - price[j - 1]));
        maxdiff = max1(maxdiff, profit[i - 1][j - 2] - price[j - 1]);
      }
    }
  }
  cout << profit[k][n - 1];
}
