#include <bits/stdc++.h>
using namespace std;
int n, m, dp[30010][210][4], num[30010];
int main() {
  int i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++) scanf("%d", &num[i]);
  memset(dp[0], -0x3f, sizeof(dp[0]));
  for (i = 0; i < 4; i++) dp[0][0][i] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      int tmp = 1 + (j != 1 && j != m);
      dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j - 1][3]) - tmp * num[i];
      dp[i][j][1] = max(dp[i - 1][j][1], dp[i][j][0]);
      dp[i][j][2] = max(dp[i - 1][j][2], dp[i - 1][j - 1][1]) + tmp * num[i];
      dp[i][j][3] = max(dp[i - 1][j][3], dp[i][j][2]);
      if (tmp == 2) {
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j - 1][1]);
        dp[i][j][3] = max(dp[i][j][3], dp[i - 1][j - 1][3]);
      }
    }
  }
  cout << max(max(dp[n][m][0], dp[n][m][1]), max(dp[n][m][2], dp[n][m][3]));
}
