#include <bits/stdc++.h>
using namespace std;
int op[] = {-1, 1};
int a[30005], f[30005][205][2][2][2];
void upd(int &x, int y) { x = max(x, y); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  memset(f, -69, sizeof f);
  for (int i = 0; i <= n; ++i) f[i][0][1][0][0] = f[i][0][1][0][1] = 0;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int pos = 1; pos <= n; ++pos)
    for (int num = 1; num <= k; ++num)
      for (int prev = 0; prev <= 1; ++prev)
        for (int nxt = 0; nxt <= 1; ++nxt) {
          int opprev = op[prev], opnxt = op[nxt];
          if (num == 1) opprev = 0;
          if (num == k) opnxt = 0;
          f[pos][num][0][prev][nxt] =
              max(f[pos][num][0][prev][nxt],
                  f[pos - 1][num][0][prev][nxt] + a[pos] * (opprev + opnxt));
          f[pos][num][0][prev][nxt] = max(
              f[pos][num][0][prev][nxt],
              f[pos - 1][num - 1][1][0][1 - prev] + a[pos] * (opprev + opnxt));
          f[pos][num][0][prev][nxt] = max(
              f[pos][num][0][prev][nxt],
              f[pos - 1][num - 1][1][1][1 - prev] + a[pos] * (opprev + opnxt));
          f[pos][num][1][prev][nxt] =
              max(f[pos][num][1][prev][nxt], f[pos][num][0][prev][nxt]);
          f[pos][num][1][prev][nxt] =
              max(f[pos][num][1][prev][nxt], f[pos - 1][num][1][prev][nxt]);
        }
  cout << max(f[n][k][1][0][0], f[n][k][1][1][0]);
}
