#include <bits/stdc++.h>
using namespace std;
int N, K;
long long DP[3005][3005], P[3005], T[2] = {1, -1};
int main() {
  cin >> N >> K;
  K *= 2;
  for (int i = 1; i <= N; i++) cin >> P[i];
  for (int ops = 1; ops <= K; ops++) DP[0][ops] = -9999999999999999ll;
  for (int d = 1; d <= N; d++)
    for (int ops = 1; ops <= K; ops++)
      DP[d][ops] = max(DP[d - 1][ops], DP[d - 1][ops - 1] + P[d] * T[ops % 2]);
  long long ans = 0;
  for (int i = 1; i <= K; i++) ans = max(ans, DP[N][i]);
  cout << ans;
}
