#include <bits/stdc++.h>
using namespace std;
const long long maxn = 40000;
long long n, k, a[maxn], f[31000][210][4], ans, tmp;
inline long long read() {
  char c = getchar();
  long long x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
signed main() {
  n = read();
  k = read();
  for (long long i = 1; i <= n; i++) a[i] = read();
  memset(f, -0x3f, sizeof(f));
  for (long long i = 0; i <= n; i++)
    f[i][0][0] = f[i][0][1] = f[i][0][2] = f[i][0][3] = 0;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= k; j++) {
      if (j == 1 || j == k)
        tmp = 1;
      else
        tmp = 2;
      f[i][j][0] = (f[i - 1][j][0] > f[i - 1][j - 1][1] ? f[i - 1][j][0]
                                                        : f[i - 1][j - 1][1]) -
                   tmp * a[i];
      f[i][j][2] = (f[i - 1][j][2] > f[i - 1][j - 1][3] ? f[i - 1][j][2]
                                                        : f[i - 1][j - 1][3]) +
                   tmp * a[i];
      f[i][j][3] = (f[i - 1][j][3] > f[i][j][0] ? f[i - 1][j][3] : f[i][j][0]);
      f[i][j][1] = (f[i - 1][j][1] > f[i][j][2] ? f[i - 1][j][1] : f[i][j][2]);
      if (tmp == 2) {
        f[i][j][1] =
            (f[i][j][1] > f[i - 1][j - 1][1] ? f[i][j][1] : f[i - 1][j - 1][1]);
        f[i][j][3] =
            (f[i][j][3] > f[i - 1][j - 1][3] ? f[i][j][3] : f[i - 1][j - 1][3]);
      }
    }
  cout << ((f[n][k][1] > f[n][k][3] ? f[n][k][1] : f[n][k][3]) >
                   (f[n][k][2] > f[n][k][0] ? f[n][k][2] : f[n][k][0])
               ? (f[n][k][1] > f[n][k][3] ? f[n][k][1] : f[n][k][3])
               : (f[n][k][2] > f[n][k][0] ? f[n][k][2] : f[n][k][0]))
       << endl;
}
