#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1000100;
ll dp[2][maxn];
ll a[maxn];
int rf(int flag) { return 1 - flag; }
void cf(int& flag) { flag = 1 - flag; }
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  int flag = 0;
  for (int i = 1; i <= k; ++i) {
    cf(flag);
    ll _max = -INF;
    for (int j = 1; j <= n; ++j) {
      _max = max(_max, dp[rf(flag)][j - 1] - a[j]);
      dp[flag][j] = max(dp[flag][j - 1], _max + a[j]);
    }
  }
  cout << dp[flag][n] << endl;
}
