#include <bits/stdc++.h>
using namespace std;
const int N = 411, M = 53;
int sum[N];
int f[N][N][M];
inline void updMax(int &a, int b) {
  if (a < b) a = b;
}
int main() {
  ios ::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  sum[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> sum[i];
    sum[i] += sum[i - 1];
  }
  memset(f, 0x80, sizeof(f));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= i; j++) f[i][j][1] = 0;
  int ans = 0;
  for (int k = 1; k < m; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= i; j++)
        if (f[i][j][k] >= 0) {
          int maxPos = i;
          int minPos = i;
          for (int t = i + 1; t <= n; t++) {
            updMax(f[t][minPos + 1][k + 1],
                   f[i][j][k] +
                       abs((sum[i] - sum[j - 1]) - (sum[t] - sum[minPos])));
            updMax(f[t][maxPos + 1][k + 1],
                   f[i][j][k] +
                       abs((sum[i] - sum[j - 1]) - (sum[t] - sum[maxPos])));
            if (sum[t] > sum[maxPos]) maxPos = t;
            if (sum[t] < sum[minPos]) minPos = t;
          }
        }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      ans = max(ans, f[i][j][m]);
    }
  }
  cout << ans << '\n';
  return 0;
}
