#include <bits/stdc++.h>
using namespace std;
const int N = 3005;
long long dp[N][N];
long long cost[N];
int n, k;
long long best[N];
inline void load() {
  scanf("%d", &n);
  scanf("%d", &k);
  for (int i = 1; i <= n; i++) cin >> cost[i];
}
inline long long maks(long long a, long long b) { return a > b ? a : b; }
inline void solve() {
  for (int i = 0; i <= k; i++) best[i] = -cost[1];
  long long ans = 0;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j <= k; j++) {
      if (j > 0) dp[i][j] = maks(dp[i][j], best[j - 1] + cost[i]);
      dp[i][j] = maks(dp[i][j], dp[i - 1][j]);
      ans = maks(ans, dp[i][j]);
    }
    for (int j = 0; j <= k; j++) best[j] = maks(best[j], dp[i][j] - cost[i]);
  }
  cout << ans << endl;
}
int main(void) {
  load();
  solve();
  return 0;
}
