#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3100;
long long DP[MAX_N];
long long DP_new[MAX_N];
long long HOLD[MAX_N];
long long HOLD_new[MAX_N];
long long PK[MAX_N];
long long PK_pre[MAX_N];
long long PRICE[MAX_N];
int main() {
  memset(DP, 0, sizeof(DP));
  memset(DP_new, 0, sizeof(DP_new));
  memset(HOLD, 0, sizeof(HOLD));
  memset(HOLD_new, 0, sizeof(HOLD_new));
  memset(PK, 0, sizeof(PK));
  memset(PK_pre, 0, sizeof(PK_pre));
  long long i, j, k, N, K;
  cin >> N >> K;
  for (i = 0; i < N; i++) cin >> PRICE[i];
  for (i = 0; i < N; i++) {
    memset(HOLD_new, 0, sizeof(HOLD_new));
    memset(DP_new, 0, sizeof(DP_new));
    memset(PK, 0, sizeof(PK));
    for (j = 0; j < K; j++) {
      k = DP[j] - PRICE[i];
      if (PK[j] == 0) {
        HOLD_new[j] = k;
        PK[j] = 1;
      } else {
        PK[j] = 1;
        HOLD_new[j] = max(k, HOLD_new[j]);
      }
    }
    for (j = 0; j < K; j++) {
      if (PK_pre[j] == 0) continue;
      k = HOLD[j] + PRICE[i];
      DP_new[j + 1] = max(DP_new[j + 1], k);
    }
    for (j = 0; j <= K; j++) {
      DP[j] = max(DP[j], DP_new[j]);
      if (PK_pre[j] == 0 && PK[j] == 1) {
        HOLD[j] = HOLD_new[j];
        PK_pre[j] = 1;
      } else if (PK_pre[j] == 1 && PK[j] == 1) {
        HOLD[j] = max(HOLD[j], HOLD_new[j]);
      }
    }
  }
  long long res = 0;
  for (i = 0; i <= K; i++) res = max(res, DP[i]);
  cout << res << endl;
  return 0;
}
