#include <bits/stdc++.h>
using namespace std;
const long long maxN = 3001;
long long N, K;
long long A[maxN];
long long dp[maxN][maxN][2];
long long F(long long, long long, bool);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(dp, -1, sizeof dp);
  cin >> N >> K;
  for (long long i = 0; i < N; i++) cin >> A[i];
  cout << F(0, K, 0) << '\n';
}
long long F(long long idx, long long tradesLeft, bool holdingStock) {
  if (idx == N || (!tradesLeft && !holdingStock)) return 0;
  if (dp[idx][tradesLeft][holdingStock] != -1)
    return dp[idx][tradesLeft][holdingStock];
  if (!holdingStock)
    return dp[idx][tradesLeft][holdingStock] =
               max(F(idx + 1, tradesLeft, false),
                   -A[idx] + F(idx + 1, tradesLeft - 1, true));
  return dp[idx][tradesLeft][holdingStock] =
             max(F(idx + 1, tradesLeft, true),
                 F(idx + 1, tradesLeft, false) + A[idx]);
}
