#include <bits/stdc++.h>
using namespace std;
long long n, k, p, answ, dp[4100][2220][2];
long long c[20000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= k; j++) dp[i][j][0] = dp[i][j][1] = -1e18;
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= k; j++) {
      dp[i][j][1] = max(dp[i - 1][j][1], dp[i][j][1]);
      dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][0]);
      dp[i][j + 1][1] = max(dp[i][j + 1][1], dp[i - 1][j][0] - c[i]);
      dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][1] + c[i]);
    }
  answ = 0;
  for (int i = 0; i <= k; i++) answ = max(answ, max(dp[n][i][0], dp[n][i][1]));
  cout << answ << endl;
  cin.get();
  cin.get();
  return 0;
}
