#include <bits/stdc++.h>
using namespace std;
int dp[33333][222][2][2][2];
int a[33333];
int n, k;
int go(int p, int k, int s1, int s2, int s) {
  if (p == n) return k == 0 ? 0 : -(int)1e9;
  if (k == 0) return 0;
  int& res = dp[p][k][s1][s2][s];
  if (res != -(int)1e9) return res;
  if (!s) {
    res = max(go(p, k, 1 - s2, 0, 1), go(p, k, 1 - s2, 1, 1));
    res = max(res, go(p + 1, k, s1, s2, 0));
  } else {
    if (k == 1) {
      int add = (s1 ? a[p] : -a[p]);
      res = max(res, go(p + 1, k, s1, s2, 1) + add);
      res = max(res, go(p + 1, k - 1, s1, s2, 0) + add);
    } else {
      int add = (s1 ? a[p] : -a[p]) + (s2 ? a[p] : -a[p]);
      if (k == ::k) add = (s2 ? a[p] : -a[p]);
      res = max(res, go(p + 1, k, s1, s2, 1) + add);
      res = max(res, go(p + 1, k - 1, s1, s2, 0) + add);
    }
  }
  return res;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  int res = 0;
  int s = 0;
  for (int i = 0; i < 33333; ++i)
    for (int j = 0; j < 222; ++j)
      for (int ii = 0; ii < 2; ++ii)
        for (int jj = 0; jj < 2; ++jj)
          for (int kk = 0; kk < 2; ++kk) dp[i][j][ii][jj][kk] = -(int)1e9;
  printf("%d\n", max(go(0, k, 1, 1, 0), go(0, k, 0, 0, 1)));
  return 0;
}
