#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3005;
int N, K;
long long P[MAXN];
long long dp[MAXN][MAXN];
int type[2] = {1, -1};
int main() {
  cin >> N >> K;
  K *= 2;
  for (int i = 1; i <= N; i++) cin >> P[i];
  for (int i = 1; i <= K; i++) dp[0][i] = (long long)-1E18;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= K; j++)
      dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + P[i] * type[j % 2]);
  long long profit = 0;
  for (int i = 1; i <= K; i++) profit = max(profit, dp[N][i]);
  cout << profit << "\n";
  return 0;
}
