#include <bits/stdc++.h>
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define popcount __builtin_popcount
#define x first
#define y second
#define pb push_back
#define re real()
#define im imag()

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef vector<int> VI;

const int MAXN = 1000010;
const int INF = 1000*1000*1000;

int best_hi[410][53]; 	// value of pref + last_group (last group as high as possible)
int best_lo[410][53];	// value of pref - last_group (last group as small as possible)
int n, k;
int S[410];
int res;

int main(){
	scanf("%d %d", &n, &k);
	--k;
	FWD(i,0,n) scanf("%d", &S[i]);
	FWD(r,0,k+1) best_hi[0][r] = best_lo[0][r] = -INF;
	FWD(i,1,n+1){
		FWD(r,0,k+1) best_hi[i][r] = best_lo[i][r] = -INF;
		int sum = 0;
		BCK(fst,i-1,-1){
			sum += S[fst];
			best_hi[i][0] = max(best_hi[i][0], sum);
			best_lo[i][0] = max(best_lo[i][0], -sum);
			FWD(j,0,fst+1)
				FWD(r,0,k){
					best_hi[i][r+1] = max(best_hi[i][r+1], best_lo[j][r] + 2*sum);
					best_hi[i][r+1] = max(best_hi[i][r+1], best_hi[j][r]);
					if(r+1 == k) res = max(res, best_lo[j][r] + sum);

					best_lo[i][r+1] = max(best_lo[i][r+1], best_hi[j][r] - 2*sum);
					best_lo[i][r+1] = max(best_lo[i][r+1], best_lo[j][r]);
					if(r+1 == k) res = max(res, best_hi[j][r] - sum);
				}
		}
	}
	printf("%d\n", res);
	return 0;
}


#include <bits/stdc++.h>
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define popcount __builtin_popcount
#define x first
#define y second
#define pb push_back
#define re real()
#define im imag()
#define max3(a,b,c) max((a), max((b),(c)))

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef vector<int> VI;

const int MAXN = 1000010;
const int INF = 1000*1000*1000;

int n, k;
int res;
int seq[30010];
int dp[4][30010][203];

/*
	0 - v
	1 - /
	2 - ^
	3 - \
*/

int main(){
	scanf("%d %d", &n, &k);
	FWD(i,1,n+1) scanf("%d", &seq[i]);
	// add - last group was maximal
	// sub - last group was minimal
	// ign - last group was non-extremal
	int max_suf, min_suf;
	FWD(d,0,4) FWD(i,0,n+1) FWD(r,0,k+1) dp[d][i][r] = -INF;
	max_suf = -INF, min_suf = INF;
	FWD(i,1,n+1){
		int a = seq[i];
		max_suf = max(a, max_suf+a);
		min_suf = min(a, min_suf+a);
		dp[0][i][1] = -min_suf;
		dp[2][i][1] = max_suf;
		FWD(r,2,k+1){
			dp[0][i][r] = max3(dp[0][i-1][r], dp[2][i-1][r-1], dp[3][i-1][r-1]) - 2*a;
			dp[1][i][r] = max(dp[1][i-1][r], dp[0][i-1][r-1]);
			dp[2][i][r] = max3(dp[2][i-1][r], dp[0][i-1][r-1], dp[1][i-1][r-1]) + 2*a;
			dp[3][i][r] = max(dp[3][i-1][r], dp[2][i-1][r-1]);
		}
	}
	max_suf = -INF, min_suf = INF;
	BCK(i,n,0){
		int a = seq[i];
		max_suf = max(a, max_suf+a);
		min_suf = min(a, min_suf+a);
		res = max(res, max(dp[2][i-1][k-1], dp[3][i-1][k-1]) - min_suf);
		res = max(res, max(dp[0][i-1][k-1], dp[1][i-1][k-1]) + max_suf);
	}
	printf("%d\n", res);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>

#define NDEBUG
#include <cassert>

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <array>
#include <type_traits>

using namespace std;

#define int64 long long
#define repeat(x) for(auto repeat_var=0;repeat_var<x;++repeat_var)
#define fill0(x) memset(x, 0, sizeof(x))

#define MAXN 500
#define MAXK 60

int n, k;
int a[MAXN];
int f[MAXK][MAXN][MAXN];
int sumPart[MAXN];

void testGen() {
  freopen("biginput1.txt", "w", stdout);
  fclose(stdout);
}

int getSum(int l, int r) {
  return sumPart[r] - sumPart[l - 1];
}

int main() {
  //testGen();
  //freopen("input2.txt", "r", stdin);

  cin >> n >> k;
  sumPart[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sumPart[i] = a[i] + sumPart[i - 1];
  }

  fill0(f);
  for (int last = 1; last <= n; ++last) {
    for (int prev = 1; prev <= last; ++prev) {
      for (int numSeq = 1; numSeq <= k; ++numSeq) {
        if (numSeq > last) {
          continue;
        }
        if (numSeq - 1 > prev - 1) {
          continue;
        }
        int sumCur = getSum(prev, last);
        if (numSeq == 1) {
          f[last][prev][numSeq] = 0;
          continue;
        }
        int best = 0;
        for (int before = 1; before < prev; ++before) {
          int sumPrev = getSum(before, prev - 1);
          if (numSeq - 2 > before - 1) {
            continue;
          }
          int cand = abs(sumCur - sumPrev) + f[prev - 1][before][numSeq - 1];
          if (cand > best) {
            best = cand;
          }
        }
        f[last][prev][numSeq] = best;
      }
    }
  }

  int best = 0;
  for (int i = 1; i <= n; ++i) {
    for (int prev = 1; prev <= n; ++prev) {
      if (k <= i && f[i][prev][k] > best) {
        best = f[i][prev][k];
      }
    }
  }
  cout << best << endl;
  return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>

#define NDEBUG
#include <cassert>

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <array>
#include <type_traits>

using namespace std;

#define int64 long long
#define repeat(x) for(auto repeat_var=0;repeat_var<x;++repeat_var)
#define fill0(x) memset(x, 0, sizeof(x))

#define MAXN 500
#define MAXK 60

int n, k;
int a[MAXN];
int f[MAXN][MAXN][MAXK];
int sumPart[MAXN];

void testGen() {
  freopen("biginput1.txt", "w", stdout);
  cout << 400 << " " << 50 << endl;
  for (int i = 1; i <= 400; ++i) {
    cout << 10000 << " ";
  }
  fclose(stdout);
}

int getSum(int l, int r) {
  return sumPart[r] - sumPart[l - 1];
}

int main() {
  //testGen();
  //freopen("biginput1.txt", "r", stdin);

  cin >> n >> k;
  sumPart[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sumPart[i] = a[i] + sumPart[i - 1];
  }

  fill0(f);
  for (int last = 1; last <= n; ++last) {
    for (int prev = 1; prev <= last; ++prev) {
      for (int numSeq = 1; numSeq <= k; ++numSeq) {
        if (numSeq > last) {
          continue;
        }
        if (numSeq - 1 > prev - 1) {
          continue;
        }
        int sumCur = getSum(prev, last);
        if (numSeq == 1) {
          f[last][prev][numSeq] = 0;
          continue;
        }
        int best = 0;
        for (int before = 1; before < prev; ++before) {
          int sumPrev = getSum(before, prev - 1);
          if (numSeq - 2 > before - 1) {
            continue;
          }
          int cand = abs(sumCur - sumPrev) + f[prev - 1][before][numSeq - 1];
          if (cand > best) {
            best = cand;
          }
        }
        f[last][prev][numSeq] = best;
      }
    }
  }

  int best = 0;
  for (int i = 1; i <= n; ++i) {
    for (int prev = 1; prev <= n; ++prev) {
      if (k <= i && f[i][prev][k] > best) {
        best = f[i][prev][k];
      }
    }
  }
  cout << best << endl;
  return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>

#define NDEBUG
#include <cassert>

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <array>
#include <type_traits>

using namespace std;

#define int64 long long
#define repeat(x) for(auto repeat_var=0;repeat_var<x;++repeat_var)
#define fill0(x) memset(x, 0, sizeof(x))

#define MAXN 500
#define MAXK 60

int n, k;
int a[MAXN];
int f[MAXN][MAXN][MAXK];
int sumPart[MAXN];

void testGen() {
  freopen("biginput1.txt", "w", stdout);
  cout << 400 << " " << 50 << endl;
  for (int i = 1; i <= 400; ++i) {
    cout << 10000 << " ";
  }
  fclose(stdout);
}

int getSum(int l, int r) {
  return sumPart[r] - sumPart[l - 1];
}

int main() {
  //testGen();
  freopen("biginput1.txt", "r", stdin);

  cin >> n >> k;
  sumPart[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sumPart[i] = a[i] + sumPart[i - 1];
  }

  fill0(f);
  for (int last = 1; last <= n; ++last) {
    for (int prev = 1; prev <= last; ++prev) {
      for (int numSeq = 1; numSeq <= k; ++numSeq) {
        if (numSeq > last) {
          continue;
        }
        if (numSeq - 1 > prev - 1) {
          continue;
        }
        int sumCur = getSum(prev, last);
        if (numSeq == 1) {
          f[last][prev][numSeq] = 0;
          continue;
        }
        int best = 0;
        for (int before = 1; before < prev; ++before) {
          int sumPrev = getSum(before, prev - 1);
          if (numSeq - 2 > before - 1) {
            continue;
          }
          int cand = abs(sumCur - sumPrev) + f[prev - 1][before][numSeq - 1];
          if (cand > best) {
            best = cand;
          }
        }
        f[last][prev][numSeq] = best;
      }
    }
  }

  int best = 0;
  for (int i = 1; i <= n; ++i) {
    for (int prev = 1; prev <= n; ++prev) {
      if (k <= i && f[i][prev][k] > best) {
        best = f[i][prev][k];
      }
    }
  }
  cout << best << endl;
  return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>

#define NDEBUG
#include <cassert>

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <array>
#include <type_traits>

using namespace std;

#define int64 long long
#define repeat(x) for(auto repeat_var=0;repeat_var<x;++repeat_var)
#define fill0(x) memset(x, 0, sizeof(x))

#define MAXN 500
#define MAXK 60

int n, k;
int a[MAXN];
int f[MAXN][MAXK];
int sumPart[MAXN];

void testGen() {
  freopen("biginput1.txt", "w", stdout);
  cout << 400 << " " << 50 << endl;
  for (int i = 1; i <= 400; ++i) {
    cout << 10000 << " ";
  }
  fclose(stdout);
}

int getSum(int l, int r) {
  return sumPart[r] - sumPart[l - 1];
}

int main() {
  //testGen();
  //freopen("input3.txt", "r", stdin);

  cin >> n >> k;
  sumPart[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sumPart[i] = a[i] + sumPart[i - 1];
  }

  fill0(f);

  // Obs: no two consecutive segments are necessarily to have more than 1 element
  for (int i = 1; i <= n; ++i) {
    f[i][1] = 0;
    for (int j = 2; j <= k; ++j) {
      if (j > i) continue;
      // Last seg has one element
      f[i][j] = f[i - 1][j - 1] + abs(a[i - 1] - a[i]);

      // Last seq has more than one elements
      for (int prevI = 0; prevI < i - 1; ++prevI) {
        int sum = getSum(prevI + 1, i - 1);
        if (j == 2) {
          f[i][j] = max(f[i][j], abs(sum - a[i]));
        } else {
          if (1 <= prevI && j - 2 <= prevI) {
            f[i][j] = max(f[i][j], abs(sum - a[i]) + abs(sum - a[prevI]) + f[prevI][j - 2]);
          }
        }
      }

      //cout << i << " " << j << " " << f[i][j] << endl;
    }
  }

  int best = 0;
  for (int i = 1; i <= n; ++i) {
    best = max(best, f[i][k]);
    if (i < n) {
      best = max(best, f[i][k - 1] + abs(a[i] - getSum(i + 1, n)));
    }
  }

  cout << best << endl;
  return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>

#define NDEBUG
#include <cassert>

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <array>
#include <type_traits>

using namespace std;

#define int64 long long
#define repeat(x) for(auto repeat_var=0;repeat_var<x;++repeat_var)
#define fill0(x) memset(x, 0, sizeof(x))

#define MAXN 500
#define MAXK 60

int n, k;
int a[MAXN];
int f[MAXN][MAXN][MAXK];
int sumPart[MAXN];

void testGen() {
  freopen("biginput1.txt", "w", stdout);
  cout << 400 << " " << 50 << endl;
  for (int i = 1; i <= 400; ++i) {
    cout << 10000 << " ";
  }
  fclose(stdout);
}

int getSum(int l, int r) {
  return sumPart[r] - sumPart[l - 1];
}

int main() {
  //testGen();
  //freopen("input2.txt", "r", stdin);

  cin >> n >> k;
  sumPart[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sumPart[i] = a[i] + sumPart[i - 1];
  }

  fill0(f);
  for (int last = 1; last <= n; ++last) {
    for (int prev = 1; prev <= last; ++prev) {
      for (int numSeq = 1; numSeq <= k; ++numSeq) {
        if (numSeq > last) {
          continue;
        }
        if (numSeq - 1 > prev - 1) {
          continue;
        }
        int sumCur = getSum(prev, last);
        if (numSeq == 1) {
          f[last][prev][numSeq] = 0;
          continue;
        }
        int best = 0;
        for (int before = 1; before < prev; ++before) {
          int sumPrev = getSum(before, prev - 1);
          if (sumPrev > sumCur && prev < last) {
            // Guaranteed to have another state that is maximum
            break;
          }
          if (numSeq - 2 > before - 1) {
            continue;
          }
          int cand = abs(sumCur - sumPrev) + f[prev - 1][before][numSeq - 1];
          if (cand > best) {
            best = cand;
          }
        }
        f[last][prev][numSeq] = best;
      }
    }
  }

  int best = 0;
  for (int i = 1; i <= n; ++i) {
    for (int prev = 1; prev <= n; ++prev) {
      if (k <= i && f[i][prev][k] > best) {
        best = f[i][prev][k];
      }
    }
  }
  cout << best << endl;
  return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>

#define NDEBUG
#include <cassert>

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <array>
#include <type_traits>

using namespace std;

#define int64 long long
#define repeat(x) for(auto repeat_var=0;repeat_var<x;++repeat_var)
#define fill0(x) memset(x, 0, sizeof(x))
#define INF_INT 2E9L

#define MAXN 30100
#define MAXK 210

void testGen() {
  freopen("biginput1.txt", "w", stdout);
  cout << 400 << " " << 50 << endl;
  for (int i = 1; i <= 400; ++i) {
    cout << 10000 << " ";
  }
  fclose(stdout);
}

int n, k;

int a[MAXN];
int f[2][2][MAXN][MAXK];
int g[MAXN][MAXK];

int main() {
  //testGen();
  //freopen("input2.txt", "r", stdin);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }

  // Let s1, s2, ..., sk be the sum of k subarrays
  // Relax the problem to optimize
  // g(s) = (qi + pi) * si, such that pi, qi are -1 or 1, and:
  // q1 = 0
  // pk = 0
  // pi = - q(i + 1), for i < k
  // We can easily shown that g(s) is also equal to |s1 - s2| + ... + |s(k -1) - sk|
  fill0(f);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= k; ++j) {
      for (int p = 0; p < 2; ++p) {
        for (int q = 0; q < 2; ++q) {
          f[p][q][i][j] = -INF_INT;
          if (i < j) continue;
          int qq = j == 1 ? 0 : (2 * q - 1);
          int pp = j == k ? 0 : (2 * p  - 1);
          if (j == 1) {
            f[p][q][i][j] = (pp + qq) * a[i] + max(f[p][q][i - 1][j], 0);
            continue;
          }
          f[p][q][i][j] = (pp + qq) * a[i] + max(f[p][q][i - 1][j], max(f[1 - q][0][i - 1][j - 1], f[1 - q][1][i - 1][j - 1]));
        }
      }

      g[i][j] = -INF_INT;
      for (int p = 0; p < 2; ++p) {
        for (int q = 0; q < 2; ++q) {
          g[i][j] = max(g[i][j], f[p][q][i][j]);
        }
      }
    }
  }

  int ret = 0;
  for (int i = 1; i <= n; ++i) {
    ret = max(ret, g[i][k]);
  }
  cout << ret << endl;

  return 0;
}


#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>

typedef long long ll;
typedef long double ld;

using namespace std;

const int INF = 1000000000;

const int MAXK = 210;
const int MAXN = 31000;

int mx[MAXK][MAXN];
int mn[MAXK][MAXN];
int dp[MAXK][MAXN];

int a[MAXN];

int n, k;

int main()
{
	cin >> n >> k;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	for (int i = 0; i < n; ++i)
	{
		dp[1][i] = 0;
		int now = a[i];
		mx[1][i] = a[i];
		mn[1][i] = -a[i];
		for (int j = i - 1; j >= 0; --j)
		{
			now += a[j];
			mx[1][i] = max(mx[1][i], now);
			mn[1][i] = max(mn[1][i], -now);
		}
	}

	for (int i = 2; i <= k; ++i)
	{
		int nm = mn[i - 1][i - 2];
		int xm = mx[i - 1][i - 2];
		int aspl1 = nm;
		int aspl2 = xm;
		int asmn1 = nm;
		int asmn2 = xm;
		for (int j = i - 1; j < n; ++j)
		{
			nm += a[j];
			xm -= a[j];
			aspl1 += 2 * a[j];
			asmn2 -= 2 * a[j];

			mx[i][j] = max(aspl1, aspl2);
			mn[i][j] = max(asmn1, asmn2);
			dp[i][j] = max(nm, xm);
			/*int now = 0;
			mx[i][j] = -INF;
			mn[i][j] = -INF;
			dp[i][j] = 0;*/
			/*for (int t = j; t > i - 2; --t)
			{
				now += a[t];
				int ans = max(mx[i - 1][t - 1] - now, mn[i - 1][t - 1] + now);
				mx[i][j] = max(mx[i][j], ans + now);
				mn[i][j] = max(mn[i][j], ans - now);
				dp[i][j] = max(dp[i][j], ans);
			}*/
			nm = max(nm, mn[i - 1][j]);
			xm = max(xm, mx[i - 1][j]);
			aspl1 = max(aspl1, mn[i - 1][j]);
			aspl2 = max(aspl2, mx[i - 1][j]);
			asmn1 = max(asmn1, mn[i - 1][j]);
			asmn2 = max(asmn2, mx[i - 1][j]);
		}
	}

	int ans = 0;

	for (int i = 0; i < n; ++i)
		ans = max(ans, dp[k][i]);

	cout << ans;

	return 0;
}


#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>

typedef long long ll;
typedef long double ld;

using namespace std;

const int INF = 1000000000;

const int MAXK = 60;
const int MAXN = 500;

int mx[MAXK][MAXN];
int mn[MAXK][MAXN];
int dp[MAXK][MAXN];

int a[MAXN];

int n, k;

int main()
{
	cin >> n >> k;

	for (int i = 0; i < n; ++i)
		cin >> a[i];

	for (int i = 0; i < n; ++i)
	{
		dp[1][i] = 0;
		int now = a[i];
		mx[1][i] = a[i];
		mn[1][i] = -a[i];
		for (int j = i - 1; j >= 0; --j)
		{
			now += a[j];
			mx[1][i] = max(mx[1][i], now);
			mn[1][i] = max(mn[1][i], -now);
		}
	}

	for (int i = 2; i <= k; ++i)
	{
		for (int j = i - 1; j < n; ++j)
		{
			int now = 0;
			mx[i][j] = -INF;
			mn[i][j] = -INF;
			dp[i][j] = 0;
			for (int t = j; t > i - 2; --t)
			{
				now += a[t];
				int ans = max(mx[i - 1][t - 1] - now, mn[i - 1][t - 1] + now);
				mx[i][j] = max(mx[i][j], ans + now);
				mn[i][j] = max(mn[i][j], ans - now);
				dp[i][j] = max(dp[i][j], ans);
			}
		}
	}

	int ans = 0;

	for (int i = 0; i < n; ++i)
		ans = max(ans, dp[k][i]);

	cout << ans;

	return 0;
}


#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define F first
#define S second
typedef long long LL;
using namespace std;
const int MOD = 1e9+7;
const int SIZE = 30001;
int a[SIZE];
int sum[SIZE];
int dp[SIZE][201];
int get(int x,int y){
    return sum[y]-sum[x-1];
}
int main(){
    DRII(N,K);
    REP(i,N)RI(a[i+1]);
    REPP(i,1,N+1)sum[i]=sum[i-1]+a[i];
    MS1(dp);
    int an=0;
    REPP(i,1,N+1){
        dp[i][1]=0;
        if(i>1){
            dp[i][2]=abs(get(1,i-1)-a[i]);
            REPP(j,2,K+1){
                if(dp[i-1][j-1]!=-1)dp[i][j]=max(dp[i][j],dp[i-1][j-1]+abs(a[i]-a[i-1]));
            }
        }
        REPP(j,3,K+1){
            REPP(k,1,i-1){
                if(dp[k][j-2]!=-1)dp[i][j]=max(dp[i][j],dp[k][j-2]+abs(a[k]-get(k+1,i-1))+abs(a[i]-get(k+1,i-1)));
            }
        }
        an=max(an,dp[i][K]);
        if(dp[i][K-1]!=-1&&i<N)an=max(an,dp[i][K-1]+abs(a[i]-get(i+1,N)));
    }
    printf("%d\n",an);
    return 0;
}


#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define F first
#define S second
typedef long long LL;
using namespace std;
const int MOD = 1e9+7;
const int SIZE = 30001;
int a[SIZE];
int sum[SIZE];
int dp[SIZE][201];
int get(int x,int y){
    return sum[y]-sum[x-1];
}
int main(){
    DRII(N,K);
    REP(i,N)RI(a[i+1]);
    REPP(i,1,N+1)sum[i]=sum[i-1]+a[i];
    MS1(dp);
    int an=0;
    REPP(i,1,N+1){
        dp[i][1]=0;
        if(i>1){
            dp[i][2]=abs(get(1,i-1)-a[i]);
            REPP(j,2,K+1){
                REPP(k,1,i){
                    if(dp[k][j-1]!=-1)dp[i][j]=max(dp[i][j],dp[k][j-1]+abs(a[i]-a[k]));
                }
            }
        }
        REPP(j,3,K+1){
            REPP(k,1,i-1){
                if(dp[k][j-2]!=-1)dp[i][j]=max(dp[i][j],dp[k][j-2]+abs(a[k]-get(k+1,i-1))+abs(a[i]-get(k+1,i-1)));
            }
        }
        an=max(an,dp[i][K]);
        if(dp[i][K-1]!=-1&&i<N)an=max(an,dp[i][K-1]+abs(a[i]-get(i+1,N)));
    }
    printf("%d\n",an);
    return 0;
}


#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define F first
#define S second
typedef long long LL;
using namespace std;
const int MOD = 1e9+7;
const int SIZE = 30001;
int a[SIZE];
int sum[SIZE];
int dp[401][401][50];
int get(int x,int y){
    return sum[y]-sum[x-1];
}
int main(){
    DRII(N,K);
    REP(i,N)RI(a[i+1]);
    REPP(i,1,N+1)sum[i]=sum[i-1]+a[i];
    MS1(dp);
    int an=0;
    REPP(i,1,N+1){
        REPP(j,i,N+1)
            dp[i][j][1]=0;
    }
    REPP(k,1,K){
        REPP(i,1,N)REPP(j,i,N){
            if(dp[i][j][k]!=-1){
                REPP(k2,j+1,N+1)dp[j+1][k2][k+1]=max(dp[j+1][k2][k+1],dp[i][j][k]+abs(get(i,j)-get(j+1,k2)));
            }
        }
    }
    REPP(i,1,N+1)REPP(j,i,N+1)an=max(an,dp[i][j][K]);
    printf("%d\n",an);
    return 0;
}


#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define F first
#define S second
typedef long long LL;
using namespace std;
const int MOD = 1e9+7;
const int SIZE = 30001;
int a[SIZE];
int sum[SIZE];
int dp[SIZE][201];
int get(int x,int y){
    return sum[y]-sum[x-1];
}
int main(){
    DRII(N,K);
    REP(i,N)RI(a[i+1]);
    REPP(i,1,N+1)sum[i]=sum[i-1]+a[i];
    MS1(dp);
    int an=0;
    REPP(i,1,N+1){
        dp[i][1]=0;
        if(i>1)dp[i][2]=abs(get(1,i-1)-a[i]);
        REPP(j,3,K+1){
            REPP(k,1,i-1){
                if(dp[k][j-2]!=-1)dp[i][j]=max(dp[i][j],dp[k][j-2]+abs(a[k]-get(k+1,i-1))+abs(a[i]-get(k+1,i-1)));
            }
        }
        an=max(an,dp[i][K]);
        if(dp[i][K-1]!=-1&&i<N)an=max(an,dp[i][K-1]+abs(a[i]-get(i+1,N)));
    }
    printf("%d\n",an);
    return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 30005, MAXK = 205, INF = 0x3f3f3f3f;

int p[MAXN], sum[MAXN];
int dp[2][2][MAXK][MAXN]; //left/right, 1/-1
int sn[2] = {1, -1};
int n, k;

int Rek(int lr, int znak, int kol, int poz)
{
  if (dp[lr][znak][kol][poz] != -INF)
    return dp[lr][znak][kol][poz];

  if (poz > n) {
    if (kol == k)
      return 0;
    return -INF;
  }

  int ret = -INF;
  ret = max(ret, Rek(lr, znak, kol, poz +1));

  if (kol < k) {
    int nlr, nzn, nkol, npoz, f;

    if (!kol || kol == k - 1) f = 1; else f = 2;
    if (!lr) {
      nzn = znak ^ 1;
      npoz = poz + 1;
      nkol = kol;
    }
    else {
      nzn = znak;
      npoz = poz;
      nkol = kol + 1;
    }
    nlr = lr ^ 1;

    ret = max(ret, sum[poz] * sn[znak] * f + Rek(nlr, nzn, nkol, npoz));
  }

  return dp[lr][znak][kol][poz] = ret;
}

int main()
{
  scanf("%d%d", &n, &k);

  for (int i=1; i<=n; i++) {
    scanf("%d", &p[i]);
    sum[i] = sum[i-1] + p[i];
  }

  for (int i=0; i<2; i++)
    for (int j=0; j<2; j++)
      for (int ii=0; ii<MAXK; ii++)
        for (int jj=0; jj<MAXN; jj++)
          dp[i][j][ii][jj] = -INF;

  printf("%d\n", max(Rek(0, 0, 0, 0), Rek(0, 1, 0, 0)));

  return 0;
}


#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 30005, MAXK = 205, INF = 1000000000;

int p[MAXN], sum[MAXN];
int dp[2][4][MAXK][MAXN]; //left/right, 1/-1
int n, k;
int sn[2] = {1, -1};
int sn2[4] = {2, 0, 0, -2};

int Rek(int lr, int st, int kol, int poz)
{
  if (dp[lr][st][kol][poz] != -INF)
    return dp[lr][st][kol][poz];

  if (poz > n) {
    if (kol == k)
      return 0;
    return -INF;
  }

  int ret = Rek(lr, st, kol, poz + 1);

  if (kol < k) {
    int um = 0, zad = st & 1;

    if (!kol) um = sn[st&1];
    else if (kol == k - 1) um = sn[(st>>1)&1];
    else um = sn2[st];

    if (lr) {
      ret = max(ret, sum[poz] * um + Rek(0, (zad ^ 1) << 1, kol + 1, poz));
      ret = max(ret, sum[poz] * um + Rek(0, ((zad ^ 1) << 1) + 1, kol + 1, poz));
    }
    else
      ret = max(ret, sum[poz] * (-um) + Rek(1, st, kol, poz + 1));
  }

  return dp[lr][st][kol][poz] = ret;
}

int main()
{
  scanf("%d%d", &n, &k);

  for (int i=1; i<=n; i++) {
    scanf("%d", &p[i]);
    sum[i] = sum[i-1] + p[i];
  }

  for (int i=0; i<2; i++)
    for (int j=0; j<4; j++)
      for (int ii=0; ii<MAXK; ii++)
        for (int jj=0; jj<MAXN; jj++)
          dp[i][j][ii][jj] = -INF;

  printf("%d\n", max(Rek(0, 0, 0, 0), Rek(0, 3, 0, 0)));

  return 0;
}


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <time.h>
#define int_bit_count __builtin_popcount
#define ll_bit_count  __builtin_popcountll
#define gcd  __gcd
#define pb push_back
#define mp make_pair
#define REP(i, n) for( (i) = 0; (i) < (int)(n);(i) ++)
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")

typedef long long ll;
const int N = 30005;
const int K = 205;
const int INF = -1000000000;

int dp[N][K][2][2][2];
int a[N];
int n, k;

int sign(int i, int s) {
    return (1 < i && i <= k) ? (s ? -1 : 1) : 0;
}

void update(int &a, int b) {
    a = max(a, b);
}

int MAIN()
{
    cin >> n >> k;
    for (int i = 0; i < n; ++ i) {
        cin >> a[i];
    }
    for (int i = 0; i <= n; ++ i) {
        for (int j = 0; j <= k; ++ j) {
            for (int s0 = 0; s0 < 2; ++ s0) {
                for (int s1 = 0; s1 < 2; ++ s1) {
                    for (int t = 0; t < 2; ++ t) {
                        dp[i][j][s0][s1][t] = INF;
                    }
                }
            }
        }
    }
    dp[0][0][0][0][1] = 0;
    for (int i = 0; i < n; ++ i) {
        for (int j = 0; j <= k; ++ j) {
            for (int s0 = 0; s0 < 2; ++ s0) {
                for (int s1 = 0; s1 < 2; ++ s1) {
                    for (int t = 0; t < 2; ++ t) {
                        if (dp[i][j][s0][s1][t] == INF) continue;
                        if (t == 0) {
                            int coef = sign(j, s0) + sign(j + 1, s1);
                            update(dp[i + 1][j][s0][s1][t], dp[i][j][s0][s1][t] + coef * a[i]);
                            update(dp[i + 1][j][s0][s1][1], dp[i][j][s0][s1][t]);
                            if (j < k) {
                                for (int ns = 0; ns < 2; ++ ns) {
                                    coef = sign(j + 1, 1 ^ s1) + sign(j + 2, ns);
                                    update(dp[i + 1][j + 1][1 ^ s1][ns][0], dp[i][j][s0][s1][t] + coef * a[i]);
                                }
                            }
                        }
                        else {
                            update(dp[i + 1][j][s0][s1][t],dp[i][j][s0][s1][t]);
                            if (j < k) {
                                for (int ns = 0; ns < 2; ++ ns) {
                                    int coef = sign(j + 1, 1 ^ s1) + sign(j + 2, ns);
                                    update(dp[i + 1][j + 1][1 ^ s1][ns][0], dp[i][j][s0][s1][t] + coef * a[i]);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    int ans = INF;
    for (int s0 = 0; s0 < 2; ++ s0) {
        for (int s1 = 0; s1 < 2; ++ s1) {
            for (int t = 0; t < 2; ++ t) {
                ans = max(ans, dp[n][k][s0][s1][t]);
            }
        }
    }
    cout << ans << endl;
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    cout << fixed << setprecision(16);
    return MAIN();
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;
#define forn(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
const int INF = 1000000000;
int n, k;
int a[1005];
int mn[405][405];
int mx[405][405];
int s[405];
int dp[55][405][405];
int get(int l, int r) {
    int ret = s[r] - (l ? s[l - 1] : 0);
    return ret;
}
int main() {
//    freopen("input.txt", "r", stdin);
    int n, k;
    cin >> n >> k;
    forn(i, 0, n) {
        cin >> a[i];
        if (i) s[i] = s[i - 1];
        s[i] += a[i];
    }
    forn(i, 0, 404)
        forn(j, 0, 404)
            forn(q, 0, 55)
                dp[q][i][j] = -INF;
    forn(i, 0, n)
        forn(j, i, n)
            dp[1][i][j] = 0;
    forn(i, 0, n)
        forn(j, i, n) {
            mn[i][j] = mx[i][j] = i;
            forn(t, i, j + 1) {
                if (get(mn[i][j], j) > get(t, j)) mn[i][j] = t;
                if (get(mx[i][j], j) < get(t, j)) mx[i][j] = t;
            }
        }
    forn(c, 1, k)
        forn(i, 0, n)
            forn(j, i, n) {
                if (dp[c][i][j] < 0) continue;
                int cur;
                forn(e, j + 1, n) {
                    cur = mn[j + 1][e];
                    dp[c + 1][cur][e] = max(dp[c + 1][cur][e], dp[c][i][j] + abs(get(cur, e) - get(i, j)));
                    cur = mx[j + 1][e];
                    dp[c + 1][cur][e] = max(dp[c + 1][cur][e], dp[c][i][j] + abs(get(cur, e) - get(i, j)));
                }
            }
    int ans = 0;
    forn(i, 0, n)
        forn(j, i, n)
            ans = max(ans, dp[k][i][j]);
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;
#define forn(i, a, b) for (int i = a; i < b; ++i)
const int INF = 1000000000;
int n, k;
int a[1005];
int mn[405][405];
int mx[405][405];
int s[405];
int dp[405][405][55];
int get(int l, int r) {
    int ret = s[r];
    if (l) ret -= s[l - 1];
    return ret;
}
int main() {
//    freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    forn(i, 0, n) {
        scanf("%d", &a[i]);
        if (i) s[i] = s[i - 1];
        s[i] += a[i];
    }
    forn(i, 0, n + 1)
        forn(j, 0, n + 1)
            forn(q, 0, k + 1)
                dp[q][i][j] = -INF;
    forn(i, 0, n)
        forn(j, i, n) {
            mn[i][j] = mx[i][j] = i;
            forn(E, i, j + 1) {
                if (get(mn[i][j], j) > get(E, j)) mn[i][j] = E;
                if (get(mx[i][j], j) < get(E, j)) mx[i][j] = E;
            }
        }
    forn(i, 0, n)
        forn(j, i, n)
            dp[1][i][j] = 0;
    forn(c, 1, k)
        forn(i, 0, n)
            forn(j, i, n) {
                if (dp[c][i][j] == -INF) continue;
                forn(e, j + 1, n) {
                    int cx = mn[j + 1][e];
                    dp[c + 1][cx][e] = max(dp[c + 1][cx][e], dp[c][i][j] + abs(get(cx, e) - get(i, j)));
                    cx = mx[j + 1][e];
                    dp[c + 1][cx][e] = max(dp[c + 1][cx][e], dp[c][i][j] + abs(get(cx, e) - get(i, j)));
                }
            }
    int ans = 0;
    forn(i, 0, n)
        forn(j, i, n) {
            ans = max(ans, dp[k][i][j]);
        }
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define forn(i, a, b) for (int i = a; i < b; ++i)
const int INF = 1000000000;
int n, k;
int a[1005];
int mn[405][405];
int mx[405][405];
int s[405];
int dp[405][405][55];
int get(int l, int r) {
    int ret = s[r];
    if (l) ret -= s[l - 1];
    return ret;
}
int main() {
// freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    forn(i, 0, n) {
        scanf("%d", &a[i]);
        if (i) s[i] = s[i - 1];
        s[i] += a[i];
    }
    forn(i, 0, n + 1)
        forn(j, 0, n + 1)
            forn(q, 0, k + 1)
                dp[q][i][j] = -INF;
    forn(i, 0, n)
        forn(j, i, n) {
            mn[i][j] = mx[i][j] = i;
            forn(E, i, j + 1) {
                int cur = mn[i][j];
                if (get(cur, j) > get(E, j)) mn[i][j] = E;
                if (get(mx[i][j], j) < get(E, j)) mx[i][j] = E;
            }
        }
    forn(i, 0, n)
        forn(j, i, n)
            dp[1][i][j] = 0;
    forn(c, 1, k)
        forn(i, 0, n)
            forn(j, i, n) {
                if (dp[c][i][j] == -INF) continue;
                forn(e, j + 1, n) {
                    int cx = mn[j + 1][e];
                    dp[c + 1][cx][e] = max(dp[c + 1][cx][e], dp[c][i][j] + abs(get(cx, e) - get(i, j)));
                    cx = mx[j + 1][e];
                    dp[c + 1][cx][e] = max(dp[c + 1][cx][e], dp[c][i][j] + abs(get(cx, e) - get(i, j)));
                }
            }
    int ans = 0;
    forn(i, 0, n)
        forn(j, i, n) {
            ans = max(ans, dp[k][i][j]);
        }
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define forn(i, a, b) for (int i = a; i < b; ++i)
const int INF = 1000000000;
int n, k;
int a[1005];
int dp[405][405][55];

int main() {
// freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    rep(i, n) scanf("%d", &a[i]);
    rep(i, n + 4)
        rep(j, n + 4)
            rep(q, k + 5)
                dp[i][j][q] = -INF;
    rep(i, n) {
        dp[0][i][1] = 0;
    }

    rep(q, k)
        rep(i, n) {
            int val = 0;
            forn(j, i, n) {
                val += a[j];
                if (dp[i][j][q] == -INF) continue;
                int curS = 0;
                forn(cur, j + 1, n) {
                    curS += a[cur];
                    dp[j + 1][cur][q + 1] = max(dp[j + 1][cur][q + 1], dp[i][j][q] + abs(curS - val));
                }
            }
        }
    int ans = 0;
    rep(i, n)
        ans = max(ans, dp[i][n - 1][k]);
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;
#define forn(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
const int INF = 1000000000;
int n, k;
int a[1005];
int mn[405][405];
int mx[405][405];
int s[405];
int dp[405][405][55];
int get(int l, int r) {
    int ret = s[r];
    if (l) ret -= s[l - 1];
    return ret;
}
int main() {
//    freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    forn(i, 0, n) {
        scanf("%d", &a[i]);
        if (i) s[i] = s[i - 1];
        s[i] += a[i];
    }
    forn(i, 0, 404)
        forn(j, 0, 404)
            forn(q, 0, 55)
                dp[q][i][j] = -INF;
    forn(i, 0, n)
        forn(j, i, n) {
            mn[i][j] = mx[i][j] = i;
            forn(E, i, j + 1) {
                if (get(mn[i][j], j) > get(E, j)) mn[i][j] = E;
                if (get(mx[i][j], j) < get(E, j)) mx[i][j] = E;
            }
        }
    forn(i, 0, n)
        forn(j, i, n)
            dp[1][i][j] = 0;
    forn(c, 1, k)
        forn(i, 0, n)
            forn(j, i, n) {
                if (dp[c][i][j] == -INF) continue;
                forn(e, j + 1, n) {
                    int cx = mn[j + 1][e];
                    dp[c + 1][cx][e] = max(dp[c + 1][cx][e], dp[c][i][j] + abs(get(cx, e) - get(i, j)));
                    cx = mx[j + 1][e];
                    dp[c + 1][cx][e] = max(dp[c + 1][cx][e], dp[c][i][j] + abs(get(cx, e) - get(i, j)));
                }
            }
    int ans = 0;
    forn(i, 0, n)
        forn(j, i, n) {
            ans = max(ans, dp[k][i][j]);
        }
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;
#define forn(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
const int INF = 1000000000;
int n, k;
int a[1005];
int mn[405][405];
int mx[405][405];
int s[405];
int dp[405][405][55];
int get(int l, int r) {
    int ret = s[r] - (l ? s[l - 1] : 0);
    return ret;
}
int main() {
//    freopen("input.txt", "r", stdin);
    int n, k;
    cin >> n >> k;
    forn(i, 0, n) {
        cin >> a[i];
        if (i) s[i] = s[i - 1];
        s[i] += a[i];
    }
    forn(i, 0, 404)
        forn(j, 0, 404)
            forn(q, 0, 55)
                dp[q][i][j] = -INF;
    forn(i, 0, n)
        forn(j, i, n)
            dp[1][i][j] = 0;
    forn(i, 0, n)
        forn(j, i, n) {
            mn[i][j] = mx[i][j] = i;
            forn(t, i, j + 1) {
                if (get(mn[i][j], j) > get(t, j)) mn[i][j] = t;
                if (get(mx[i][j], j) < get(t, j)) mx[i][j] = t;
            }
        }
    forn(c, 1, k)
        forn(i, 0, n)
            forn(j, i, n) {
                if (dp[c][i][j] < 0) continue;
                int cur;
                forn(e, j + 1, n) {
                    cur = mn[j + 1][e];
                    dp[c + 1][cur][e] = max(dp[c + 1][cur][e], dp[c][i][j] + abs(get(cur, e) - get(i, j)));
                    cur = mx[j + 1][e];
                    dp[c + 1][cur][e] = max(dp[c + 1][cur][e], dp[c][i][j] + abs(get(cur, e) - get(i, j)));
                }
            }
    int ans = 0;
    forn(i, 0, n)
        forn(j, i, n)
            ans = max(ans, dp[k][i][j]);
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define forn(i, a, b) for (int i = a; i < b; ++i)
const int INF = 1000000000;
int n, k;
int a[1005];
int dp[405][405][55];

int main() {
// freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    rep(i, n) scanf("%d", &a[i]);
    rep(i, n + 4)
        rep(j, n + 4)
            rep(q, k + 5)
                dp[i][j][q] = -INF;
    rep(i, n) {
        dp[i][i][1] = 0;
    }

    rep(q, k)
        rep(i, n) {
            int val = 0;
            forn(j, i, n) {
                val += a[j];
                if (dp[i][j][q] == -INF) continue;
                int curS = 0;
                forn(cur, j + 1, n) {
                    curS += a[cur];
                    dp[j + 1][cur][q + 1] = max(dp[j + 1][cur][q + 1], dp[i][j][q] + abs(curS - val));
                }
            }
        }
    int ans = 0;
    rep(i, n)
        ans = max(ans, dp[i][n - 1][k]);
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;
#define forn(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
const int INF = 1000000000;
int n, k;
int a[1005];
int mn[405][405];
int mx[405][405];
int s[405];
int dp[405][405][55];
int get(int l, int r) {
    int ret = s[r];
    if (l) ret -= s[l - 1];
    return ret;
}
int main() {
//    freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    forn(i, 0, n) {
        scanf("%d", &a[i]);
        if (i) s[i] = s[i - 1];
        s[i] += a[i];
    }
    forn(i, 0, n + 1)
        forn(j, 0, n + 1)
            forn(q, 0, k + 1)
                dp[q][i][j] = -INF;
    forn(i, 0, n)
        forn(j, i, n) {
            mn[i][j] = mx[i][j] = i;
            forn(E, i, j + 1) {
                if (get(mn[i][j], j) > get(E, j)) mn[i][j] = E;
                if (get(mx[i][j], j) < get(E, j)) mx[i][j] = E;
            }
        }
    forn(i, 0, n)
        forn(j, i, n)
            dp[1][i][j] = 0;
    forn(c, 1, k)
        forn(i, 0, n)
            forn(j, i, n) {
                if (dp[c][i][j] == -INF) continue;
                forn(e, j + 1, n) {
                    int cx = mn[j + 1][e];
                    dp[c + 1][cx][e] = max(dp[c + 1][cx][e], dp[c][i][j] + abs(get(cx, e) - get(i, j)));
                    cx = mx[j + 1][e];
                    dp[c + 1][cx][e] = max(dp[c + 1][cx][e], dp[c][i][j] + abs(get(cx, e) - get(i, j)));
                }
            }
    int ans = 0;
    forn(i, 0, n)
        forn(j, i, n) {
            ans = max(ans, dp[k][i][j]);
        }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define fr(a,b,c) for(int a = b; a < c; a++)
#define fr_(a,b,c) for(int a = c-1; a >= b; a--)
#define rp(a,b) fr(a,0,b)
#define rp_(a,b) rf(a,0,b)
#define sc(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d%d", &a, &b)
#define sc3(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define fre(a,b) for(int a = adj[b]; ~a; a = ant[a])
#define cl(a,b) memset(a,b,sizeof a)
#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<
#define pri(x) printf("%d", x)
#define oo 0x3f3f3f3f

#define mp make_pair
#define pb push_back

#define F first
#define S second

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;

int pd[30010][202][4][4];
bool mark[30010][202][4][4];
int x[30010];
int n, k;

int op[] = {0,2,1};

int go(int i, int g, int s1, int s2) {
    int &ret = pd[i][g][s1][s2];
    if (mark[i][g][s1][s2]) return ret;
    mark[i][g][s1][s2] = 1;
    if (g == 0) return ret = 0;
    else if (i >= n) return ret = -oo;

    int sig1 = (s1 == 2 ? -1 : 1);
    int sig2 = (s2 == 2 ? -1 : 1);
    int mulg = (g == 1 ? 0 : 1);

    ret = -oo;
    if (s1 == 0 && s2 == 0) {
        if (go(i+1, g, 1, 0) != -oo)
            ret = max(ret, x[i] + go(i+1, g, 1, 0));
        if (go(i+1, g, 2, 0) != -oo)
            ret = max(ret, -x[i] + go(i+1, g, 2, 0));
        if (go(i+1, g-1, 0, op[1]) != -oo)
            ret = max(ret, x[i] + go(i+1, g-1, 0, op[1]));
        if (go(i+1, g-1, 0, op[2]) != -oo)
            ret = max(ret, -x[i] + go(i+1, g-1, 0, op[2]));
        if (go(i+1, g, 0, 0) != -oo)
            ret = max(ret, go(i+1, g, 0, 0));
    } else if (s1 == 0) {
        if (go(i+1, g, 1, s2) != -oo)
            ret = max(ret, sig2*x[i] + mulg*x[i] + go(i+1, g, 1, s2));
        if (go(i+1, g, 2, s2) != -oo)
            ret = max(ret, sig2*x[i] - mulg*x[i] + go(i+1, g, 2, s2));
        if (go(i+1, g-1, 0, op[1]) != -oo)
            ret = max(ret, sig2*x[i] + mulg*x[i] + go(i+1, g-1, 0, op[1]));
        if (go(i+1, g-1, 0, op[2]) != -oo)
            ret = max(ret, sig2*x[i] - mulg*x[i] + go(i+1, g-1, 0, op[2]));
        if (go(i+1, g, 0, s2) != -oo)
            ret = max(ret, go(i+1, g, 0, s2));
    } else if (s2 == 0) {
        if (go(i+1, g, s1, 0) != -oo)
            ret = max(ret, sig1*x[i] + go(i+1, g, s1, 0));
        if (go(i+1, g-1, 0, op[s1]) != -oo)
            ret = max(ret, sig1*x[i] + go(i+1, g-1, 0, op[s1]));
    } else {
        if (go(i+1, g, s1, s2) != -oo)
            ret = max(ret, mulg*sig1*x[i] + sig2*x[i] + go(i+1, g, s1, s2));
        if (go(i+1, g-1, 0, op[s1]) != -oo)
            ret = max(ret, mulg*sig1*x[i] + sig2*x[i] + go(i+1, g-1, 0, op[s1]));
    }
    //dbs(i _ g _ sig1 _ (sig2*x[i] - mulg*x[i]) _ ret);
    return ret;
}

int main() {
    sc2(n,k);
    rp(i,n) sc(x[i]);
    cl(mark,0);
    printf("%d\n", go(0,k,0,0));
}


#include <bits/stdc++.h>
using namespace std;

const char lend = '\n';
const int N = 400, M = 51;

int vet[N], n, m, dp1[N][N][M], dp2[N][M], sum[N][N];

int solve2(int, int);

int solve1(int a, int b, int c)
{
    if (!c) return 0;
    if (b == n-1) return -1e9;
    int& res = dp1[a][b][c];
    if (res != -1) return res;

    res = abs(sum[a][b]-vet[b+1])+solve2(b+1, c-1);

    return res;
}

int solve2(int a, int c)
{
    if (!c) return 0;
    if (a == n-1) return -1e9;

    int& res = dp2[a][c];
    if (res != -1) return res;
    res = 0;

    for (int x = a+1; x < n; ++x)
        res = max(res, abs(vet[a]-sum[a+1][x])+solve1(a+1, x, c-1));

    return res;
}

int main()
{
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	    cin >> vet[i];

	memset(dp1, -1, sizeof dp1);
	memset(dp2, -1, sizeof dp2);
	for (int i = 0; i < n; ++i)
	    for (int j = i; j < n; ++j)
	        for (int k = i; k <= j; ++k)
	            sum[i][j] += vet[k];

	int best = 0;
	for (int i = 0; i < n; ++i)
	{
	    best = max(best, solve2(i, m-1));
	    for (int j = i; j < n; ++j)
	        best = max(best, solve1(i, j, m-1));
	}

	cout << best << endl;
}


#include <bits/stdc++.h>
using namespace std;

const char lend = '\n';
const int N = 400, M = 51;

int vet[N], n, m, dp1[N][N][M], dp2[N][M], sum[N][N];

int solve2(int, int);

int solve1(int a, int b, int c)
{
    if (!c) return 0;
    if (b == n-1) return -1e9;
    int& res = dp1[a][b][c];
    if (res != -1) return res;
    res = 0;

    for (int x = b+1; x < n; ++x)
        res = max(res, abs(sum[a][b]-vet[x])+solve2(x, c-1));

    return res;
}

int solve2(int a, int c)
{
    if (!c) return 0;
    if (a == n-1) return -1e9;

    int& res = dp2[a][c];
    if (res != -1) return res;
    res = 0;

    for (int x = a+1; x < n; ++x)
        res = max(res, abs(vet[a]-sum[a+1][x])+solve1(a+1, x, c-1));

    return res;
}

int main()
{
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	    cin >> vet[i];

	memset(dp1, -1, sizeof dp1);
	memset(dp2, -1, sizeof dp2);
	for (int i = 0; i < n; ++i)
	    for (int j = i; j < n; ++j)
	        for (int k = i; k <= j; ++k)
	            sum[i][j] += vet[k];

	int best = 0;
	for (int i = 0; i < n; ++i)
	{
	    best = max(best, solve2(i, m-1));
	    for (int j = i; j < n; ++j)
	        best = max(best, solve1(i, j, m-1));
	}

	cout << best << endl;
}


#include <bits/stdc++.h>
using namespace std;

const char lend = '\n';
const int N = 400, M = 51;

int vet[N], n, m, dp[M][N][N], sum[N][N], up[N][N], down[N][N], upaux[N][N][2], downaux[N][N][2];

void pre()
{
    for (int i = 0; i < n; ++i)
    {
        up[i][i] = vet[i];
        upaux[i][i][0] = upaux[i][i][1] = i;
        for (int j = i+1; j < n; ++j)
        {
            up[i][j] = up[i][j-1];
            upaux[i][j][0] = upaux[i][j-1][0];
            upaux[i][j][1] = upaux[i][j-1][1];
            int now = 0;
            for (int k = j; k >= i; --k)
            {
                now += vet[k];
                if (now > up[i][j])
                {
                    up[i][j] = now;
                    upaux[i][j][0] = k;
                    upaux[i][j][1] = j;
                }
            }
        }
    }
    for (int i = 0; i < n; ++i)
    {
        down[i][i] = vet[i];
        downaux[i][i][0] = downaux[i][i][1] = i;
        for (int j = i+1; j < n; ++j)
        {
            down[i][j] = down[i][j-1];
            downaux[i][j][0] = downaux[i][j-1][0];
            downaux[i][j][1] = downaux[i][j-1][1];
            int now = 0;
            for (int k = j; k >= i; --k)
            {
                now += vet[k];
                if (now < down[i][j])
                {
                    down[i][j] = now;
                    downaux[i][j][0] = k;
                    downaux[i][j][1] = j;
                }
            }
        }
    }
}

const int INF = -1e9;

void solve()
{
    --m;
    for (int k = 0; k < m; ++k)
    {
        for (int i = 0; i < n; ++i)
            for (int j = i; j < n; ++j)
                dp[k+1][i][j] = INF;
        for (int i = 0; i < n; ++i)
        {
            for (int j = i; j < n; ++j)
            {
                if (dp[k][i][j] == INF) continue;
                for (int x = j+1; x < n; ++x)
                {
                    int& resa = dp[k+1][upaux[j+1][x][0]][upaux[j+1][x][1]];
                    resa = max(resa, dp[k][i][j]+abs(sum[i][j]-up[j+1][x]));
                    int& resb = dp[k+1][downaux[j+1][x][0]][downaux[j+1][x][1]];
                    resb = max(resb, dp[k][i][j]+abs(sum[i][j]-down[j+1][x]));
                }
            }
        }
    }

    int best = INF;
    for (int i = 0; i < n; ++i)
        for (int j = i; j < n; ++j)
            best = max(best, dp[m][i][j]);
    cout << best << endl;
}

int solve(int a, int b, int c)
{
    if (c == m-1) return 0;
    int& res = dp[a][b][c];
    if (res != -1) return res;

    assert(a<=b);

    res = INF;
    for (int i = b+1; i < n; ++i)
    {
        res = max(res, abs(sum[a][b]-up[b+1][i])+solve(upaux[b+1][i][0], upaux[b+1][i][1], c+1));
        res = max(res, abs(sum[a][b]-down[b+1][i])+solve(downaux[b+1][i][0], downaux[b+1][i][1], c+1));
    }

    return res;
}

int main()
{
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	    cin >> vet[i];

	for (int i = 0; i < n; ++i)
	    for (int j = i; j < n; ++j)
	        for (int k = i; k <= j; ++k)
	            sum[i][j] += vet[k];
	pre();

	solve();
}


#include <bits/stdc++.h>
using namespace std;

const char lend = '\n';
const int N = 400, M = 51;

int vet[N], n, m, dp[N][N][M][2], sum[N][N], up[N][N], down[N][N], upaux[N][N][2], downaux[N][N][2];

void pre()
{
    for (int i = 0; i < n; ++i)
    {
        up[i][i] = vet[i];
        upaux[i][i][0] = upaux[i][i][1] = i;
        for (int j = i+1; j < n; ++j)
        {
            up[i][j] = up[i][j-1];
            upaux[i][j][0] = upaux[i][j-1][0];
            upaux[i][j][1] = upaux[i][j-1][1];
            int now = 0;
            for (int k = j; k >= i; --k)
            {
                now += vet[k];
                if (now > up[i][j])
                {
                    up[i][j] = now;
                    upaux[i][j][0] = k;
                    upaux[i][j][1] = j;
                }
            }
        }
    }
    for (int i = 0; i < n; ++i)
    {
        down[i][i] = vet[i];
        downaux[i][i][0] = downaux[i][i][1] = i;
        for (int j = i+1; j < n; ++j)
        {
            down[i][j] = down[i][j-1];
            downaux[i][j][0] = downaux[i][j-1][0];
            downaux[i][j][1] = downaux[i][j-1][1];
            int now = 0;
            for (int k = j; k >= i; --k)
            {
                now += vet[k];
                if (now < down[i][j])
                {
                    down[i][j] = now;
                    downaux[i][j][0] = k;
                    downaux[i][j][1] = j;
                }
            }
        }
    }
}

int solve(int a, int b, int c, int d)
{
    if (c == m-1) return 0;
    int& res = dp[a][b][c][d];
    if (res != -1) return res;

    res = -1e9;
    for (int i = b+1; i < n; ++i)
    {
        res = max(res, abs(sum[a][b]-up[b+1][i])+solve(upaux[b+1][i][0], upaux[b+1][i][1], c+1, 0));
        res = max(res, abs(sum[a][b]-down[b+1][i])+solve(downaux[b+1][i][0], downaux[b+1][i][1], c+1, 0));
    }

    return res;
}

int main()
{
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	    cin >> vet[i];

	memset(dp, -1, sizeof dp);
	for (int i = 0; i < n; ++i)
	    for (int j = i; j < n; ++j)
	        for (int k = i; k <= j; ++k)
	            sum[i][j] += vet[k];
	pre();

	int best = 0;
	for (int i = 0; i < n; ++i)
	{
	    for (int j = i; j < n; ++j)
	        best = max(best, max(solve(i, j, 0, 0), solve(i, j, 0, 1)));
	}

	cout << best << endl;
}


#include <bits/stdc++.h>
using namespace std;

const char lend = '\n';
const int N = 400, M = 51;

int vet[N], n, m, dp1[N][N][M], dp2[N][M], sum[N][N];

int solve2(int, int);

int solve1(int a, int b, int c)
{
    if (!c) return 0;
    if (b == n-1) return -1e9;
    int& res = dp1[a][b][c];
    if (res != -1) return res;
    res = 0;

    for (int x = b+1; x < n; ++x)
        res = max(res, abs(sum[a][b]-vet[x])+solve2(x, c-1));

    return res;
}

int solve2(int a, int c)
{
    if (!c) return 0;
    if (a == n-1) return -1e9;

    int& res = dp2[a][c];
    if (res != -1) return res;
    res = 0;

    for (int x = a+1; x < n; ++x)
        for (int y = x; y < n; ++y)
            res = max(res, abs(vet[a]-sum[x][y])+solve1(x, y, c-1));

    return res;
}

int main()
{
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	    cin >> vet[i];

	memset(dp1, -1, sizeof dp1);
	memset(dp2, -1, sizeof dp2);
	for (int i = 0; i < n; ++i)
	    for (int j = i; j < n; ++j)
	        for (int k = i; k <= j; ++k)
	            sum[i][j] += vet[k];

	int best = 0;
	for (int i = 0; i < n; ++i)
	{
	    best = max(best, solve2(i, m-1));
	    for (int j = i; j < n; ++j)
	        best = max(best, solve1(i, j, m-1));
	}

	cout << best << endl;
}


#include <bits/stdc++.h>
using namespace std;

const char lend = '\n';
const int N = 400, M = 51;

int vet[N], n, m, dp1[N][N][M], dp2[N][M], sum[N][N];

int solve2(int, int);

int solve1(int a, int b, int c)
{
    if (!c) return 0;
    if (b == n-1) return -1e9;
    int& res = dp1[a][b][c];
    if (res != -1) return res;

    res = abs(sum[a][b]-vet[b+1])+solve2(b+1, c-1);

    return res;
}

int solve2(int a, int c)
{
    if (!c) return 0;
    if (a == n-1) return -1e9;

    int& res = dp2[a][c];
    if (res != -1) return res;
    res = -1e9;

    for (int x = a+1; x < n; ++x)
        res = max(res, abs(vet[a]-sum[a+1][x])+solve1(a+1, x, c-1));

    return res;
}

int main()
{
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	    cin >> vet[i];

	memset(dp1, -1, sizeof dp1);
	memset(dp2, -1, sizeof dp2);
	for (int i = 0; i < n; ++i)
	    for (int j = i; j < n; ++j)
	        for (int k = i; k <= j; ++k)
	            sum[i][j] += vet[k];

	int best = 0;
	for (int i = 0; i < n; ++i)
	{
	    best = max(best, solve2(i, m-1));
	    for (int j = i; j < n; ++j)
	        best = max(best, solve1(i, j, m-1));
	}

	cout << best << endl;
}


#include <bits/stdc++.h>
using namespace std;

const char lend = '\n';
const int N = 400, M = 51;

int vet[N], n, m, dp[N][N][M][2], sum[N][N], up[N][N], down[N][N], upaux[N][N][2], downaux[N][N][2];

void pre()
{
    for (int i = 0; i < n; ++i)
    {
        up[i][i] = vet[i];
        upaux[i][i][0] = upaux[i][i][1] = i;
        for (int j = i+1; j < n; ++j)
        {
            up[i][j] = up[i][j-1];
            upaux[i][j][0] = upaux[i][j-1][0];
            upaux[i][j][1] = upaux[i][j-1][1];
            int now = 0;
            for (int k = j; k >= i; --k)
            {
                now += vet[k];
                if (now > up[i][j])
                {
                    up[i][j] = now;
                    upaux[i][j][0] = k;
                    upaux[i][j][1] = j;
                }
            }
        }
    }
    for (int i = 0; i < n; ++i)
    {
        down[i][i] = vet[i];
        downaux[i][i][0] = downaux[i][i][1] = i;
        for (int j = i+1; j < n; ++j)
        {
            down[i][j] = down[i][j-1];
            downaux[i][j][0] = downaux[i][j-1][0];
            downaux[i][j][1] = downaux[i][j-1][1];
            int now = 0;
            for (int k = j; k >= i; --k)
            {
                now += vet[k];
                if (now < down[i][j])
                {
                    down[i][j] = now;
                    downaux[i][j][0] = k;
                    downaux[i][j][1] = j;
                }
            }
        }
    }
}

int solve(int a, int b, int c, int d)
{
    if (c == m-1) return 0;
    int& res = dp[a][b][c][d];
    if (res != -1) return res;

    res = -1e9;
    for (int i = b+1; i < n; ++i)
    {
        if (d == 0) res = max(res, abs(sum[a][b]-up[b+1][i])+solve(upaux[b+1][i][0], upaux[b+1][i][1], c+1, 1));
        else res = max(res, abs(sum[a][b]-down[b+1][i])+solve(downaux[b+1][i][0], downaux[b+1][i][1], c+1, 0));
    }

    return res;
}

int main()
{
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	    cin >> vet[i];

	memset(dp, -1, sizeof dp);
	for (int i = 0; i < n; ++i)
	    for (int j = i; j < n; ++j)
	        for (int k = i; k <= j; ++k)
	            sum[i][j] += vet[k];
	pre();

	int best = 0;
	for (int i = 0; i < n; ++i)
	{
	    for (int j = i; j < n; ++j)
	        best = max(best, max(solve(i, j, 0, 0), solve(i, j, 0, 1)));
	}

	cout << best << endl;
}


#include <bits/stdc++.h>
using namespace std;

const char lend = '\n';
const int N = 400, M = 51;

int vet[N], n, m, dp[N][N][M], sum[N][N], up[N][N], down[N][N], upaux[N][N][2], downaux[N][N][2];

void pre()
{
    for (int i = 0; i < n; ++i)
    {
        up[i][i] = vet[i];
        upaux[i][i][0] = upaux[i][i][1] = i;
        for (int j = i+1; j < n; ++j)
        {
            up[i][j] = up[i][j-1];
            upaux[i][j][0] = upaux[i][j-1][0];
            upaux[i][j][1] = upaux[i][j-1][1];
            int now = 0;
            for (int k = j; k >= i; --k)
            {
                now += vet[k];
                if (now > up[i][j])
                {
                    up[i][j] = now;
                    upaux[i][j][0] = k;
                    upaux[i][j][1] = j;
                }
            }
        }
    }
    for (int i = 0; i < n; ++i)
    {
        down[i][i] = vet[i];
        downaux[i][i][0] = downaux[i][i][1] = i;
        for (int j = i+1; j < n; ++j)
        {
            down[i][j] = down[i][j-1];
            downaux[i][j][0] = downaux[i][j-1][0];
            downaux[i][j][1] = downaux[i][j-1][1];
            int now = 0;
            for (int k = j; k >= i; --k)
            {
                now += vet[k];
                if (now < down[i][j])
                {
                    down[i][j] = now;
                    downaux[i][j][0] = k;
                    downaux[i][j][1] = j;
                }
            }
        }
    }
}

int solve(int a, int b, int c)
{
    if (c == m-1) return 0;
    int& res = dp[a][b][c];
    if (res != -1) return res;

    assert(a<=b);

    res = -1e9;
    for (int i = b+1; i < n; ++i)
    {
        res = max(res, abs(sum[a][b]-up[b+1][i])+solve(upaux[b+1][i][0], upaux[b+1][i][1], c+1));
        res = max(res, abs(sum[a][b]-down[b+1][i])+solve(downaux[b+1][i][0], downaux[b+1][i][1], c+1));
    }

    return res;
}

int main()
{
	ios::sync_with_stdio(0);

	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	    cin >> vet[i];

	memset(dp, -1, sizeof dp);
	for (int i = 0; i < n; ++i)
	    for (int j = i; j < n; ++j)
	        for (int k = i; k <= j; ++k)
	            sum[i][j] += vet[k];
	pre();

	int best = 0;
	for (int i = 0; i < n; ++i)
	{
	    for (int j = i; j < n; ++j)
	        best = max(best, solve(i, j, 0));
	}

	cout << best << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb push_back
#define f(i,x,y) for(int i = x; i<y; i++ )
#define FORV(it,A) for(vector<int>::iterator it = A.begin(); it!= A.end(); it++)
#define FORS(it,A) for(set<int>::iterator it = A.begin(); it!= A.end(); it++)
#define quad(x) (x) * (x)
#define mp make_pair
#define clr(x, y) memset(x, y, sizeof x)
#define fst first
#define snd second
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
#define INF 100000000000LL
#define KK 210
#define N 31000

int K, n;

ll pd[N][KK][2];
ll v[N];

ll solve (int pos, int k, int mx){
	if (k == 0) return 0;
	if (pos == n) return -INF;
	ll& ret = pd[pos][k][mx];
	//printf("%d %d %d %lld\n", pos, k, mx, ret);
	if (ret != INF) return ret;

	if (mx){
		if (k == K || k == 1) return ret = max(solve(pos+1, k, mx)+v[pos], solve(pos+1, k-1, mx^1)+v[pos]);
		return ret = max(solve(pos+1, k, mx)+2*v[pos], solve(pos+1, k-1, mx^1)+2*v[pos]);
	}
	if (k == K || k == 1) return ret = max(solve(pos+1, k, mx)-v[pos], solve(pos+1, k-1, mx^1)-v[pos]);
	return ret = max(solve(pos+1, k, mx)-2*v[pos], solve(pos+1, k-1, mx^1)-2*v[pos]);
}

int main (){
	cin >> n >> K;
	f (i, 0, n) cin >> v[i];
	f (i, 0, N) f (j, 0, KK) f (l, 0, 2) pd[i][j][l] = INF;
	ll ans = -INF;
	f (i, 0, n) f (l, 0, 2) ans = max(ans, solve(i, K, l));
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb push_back
#define f(i,x,y) for(int i = x; i<y; i++ )
#define FORV(it,A) for(vector<int>::iterator it = A.begin(); it!= A.end(); it++)
#define FORS(it,A) for(set<int>::iterator it = A.begin(); it!= A.end(); it++)
#define quad(x) (x) * (x)
#define mp make_pair
#define clr(x, y) memset(x, y, sizeof x)
#define fst first
#define snd second
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
#define INF 100000000000LL
#define KK 51
#define N 1000

int K, n;

ll pd[N][KK][KK][2][2][2];
ll v[N];

//peg eh o falta nao pegar
ll solve (int pos, int k, int peg, int mx, int pri, int ja){
	peg = max(peg, 0);
	if (k == 0 && peg == 0) return 0;
	if (pos == n && k == 1 && ja && peg == 0) return 0;
	if (k == 0) return -INF;
	if (pos == n) return -INF;
	ll& ret = pd[pos][k][peg][mx][pri][ja];
	//printf("%d %d %d %lld\n", pos, k, mx, ret);
	if (ret != INF) return ret;

	ll sum = 0;
	if (mx){
		if (pri || k == 1)  sum = v[pos];
		else sum = 2*v[pos];
	}
	else{
		if (pri || k == 1)  sum = -v[pos];
		else sum = -2*v[pos];
	}

	ret = solve(pos+1, k, peg-1, mx, pri, ja);
	ret = max(ret, solve(pos+1, k, peg, mx, pri, 1)+sum);
   	if (ja) ret = max(ret, solve(pos, k-1, peg, mx^1, 0, 0));

	return ret;
}

int main (){
	cin >> n >> K;
	f (i, 0, n) cin >> v[i];
	f (i, 0, N) f (j, 0, KK) f (l, 0, 2) f (peg, 0, KK) f (pri, 0, 2) f (ja, 0, 2) pd[i][j][peg][l][pri][ja] = INF;
	ll ans = -INF;
	f (k, 2, K+1) f (l, 0, 2) ans = max(ans, solve(0, k, K-k, l, 1, 0));

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb push_back
#define f(i,x,y) for(int i = x; i<y; i++ )
#define FORV(it,A) for(vector<int>::iterator it = A.begin(); it!= A.end(); it++)
#define FORS(it,A) for(set<int>::iterator it = A.begin(); it!= A.end(); it++)
#define quad(x) (x) * (x)
#define mp make_pair
#define clr(x, y) memset(x, y, sizeof x)
#define fst first
#define snd second
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
#define INF 100000000000LL
#define KK 55
#define N 1000

int K, n;

ll pd[N][KK][KK][2][2][2];
ll v[N];

//peg eh o falta nao pegar
ll solve (int pos, int k, int peg, int mx, int pri, int ja){
	peg = max(peg, 0);
	if (k == 0 && peg == 0) return 0;
	if (pos == n && k == 1 && ja && peg == 0) return 0;
	if (k == 0) return -INF;
	if (pos == n) return -INF;
	ll& ret = pd[pos][k][peg][mx][pri][ja];
	//printf("%d %d %d %lld\n", pos, k, mx, ret);
	if (ret != INF) return ret;

	ll sum = 0;
	if (mx){
		if (pri || k == 1)  sum = v[pos];
		else sum = 2*v[pos];
	}
	else{
		if (pri || k == 1)  sum = -v[pos];
		else sum = -2*v[pos];
	}
	ret = -INF;
	if (ja == 0) ret = max(ret, solve(pos+1, k, peg-1, mx, pri, ja));
	ret = max(ret, solve(pos+1, k, peg, mx, pri, 1)+sum);
   	if (ja) ret = max(ret, solve(pos, k-1, peg, mx^1, 0, 0));

	return ret;
}

int main (){
	cin >> n >> K;
	f (i, 0, n) cin >> v[i];
	f (i, 0, N) f (j, 0, KK) f (l, 0, 2) f (peg, 0, KK) f (pri, 0, 2) f (ja, 0, 2) pd[i][j][peg][l][pri][ja] = INF;
	ll ans = -INF;
	f (k, 2, K+1) f (l, 0, 2) ans = max(ans, solve(0, k, K-k, l, 1, 0));

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

const int X = 410, Y = 55, INF = 1e9;

int dp[Y][X][X], mxdp1[Y][X], mxdp2[Y][X], lst[X], pr[X];

int sum(int l, int r)
{
    if(r == 0) return pr[r];
    return pr[r]  - pr[l - 1];
}

int main()
{
    int n, k, i, j;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
    {
        cin >> lst[i];
    }
    pr[0] = lst[0];
    for (int i = 0; i < n; i++) pr[i] = pr[i - 1] + lst[i];

    for (int i = 0; i < Y; i++)
    {
        for (int j = 0; j < X; j++)
        {
            for (int z = 0; z < X; z++)
            {
                dp[i][j][z] = -INF;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        dp[1][0][i] = 0;
        mxdp1[1][i] = max(((i == 0) ? -INF : mxdp1[1][i - 1]), sum(0, i));
        mxdp2[1][i] = max(((i == 0) ? -INF : mxdp2[1][i - 1]), -sum(0, i));
    }
    for (int i = 2; i <= k; i++)
    {
        for (int j = i - 1; j < n; j++)
        {
            for (int z = j; z < n; z++)
            {
                dp[i][j][z] = max(dp[i][j][z], max(mxdp1[i - 1][j - 1] - sum(j, z), mxdp2[i - 1][j - 1] + sum(j, z)));
                mxdp1[i][z] = max(mxdp1[i][z],  dp[i][j][z] + sum(j, z));
                mxdp1[i][z] = max(mxdp1[i][z],  dp[i][j][z] - sum(j, z));
            }
        }
    }

    int ans = -INF;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            ans = max(ans, dp[k][i][j]);
        }
    }

    cout << ans;

    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

const long long X = 410, Y = 55, INF = 1e15;

long long dp[Y][X][X], mxdp1[Y][X], mxdp2[Y][X], lst[X], pr[X];

long long sum(long long l, long long r)
{
    if(r == 0) return pr[r];
    return pr[r]  - pr[l - 1];
}

int main()
{
    long long n, k, i, j;
    cin >> n >> k;

    for (long long i = 0; i < n; i++)
    {
        cin >> lst[i];
    }

    pr[0] = lst[0];
    for (long long i = 0; i < n; i++) pr[i] = pr[i - 1] + lst[i];

    for (long long i = 0; i < Y; i++)
    {
        for (long long j = 0; j < X; j++)
        {
            for (long long z = 0; z < X; z++)
            {
                dp[i][j][z] = -INF;
                mxdp1[i][z] = -INF;
                mxdp2[i][z] = -INF;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
            dp[1][i][j] = 0;
            mxdp1[1][j] = max(mxdp1[1][j], sum(i, j));
            mxdp2[1][j] = max(mxdp2[1][j], -sum(i, j));
        }
    }
    for (int i = 1; i < n; i++)
    {
        mxdp1[1][i] = max(mxdp1[1][i], mxdp1[1][i - 1]);
        mxdp2[1][i] = max(mxdp2[1][i], mxdp2[1][i - 1]);
    }
    for (long long i = 2; i <= k; i++)
    {
        for (long long j = i - 1; j < n; j++)
        {
            for (long long z = j; z < n; z++)
            {
                dp[i][j][z] = max(dp[i][j][z], max(mxdp1[i - 1][j - 1] - sum(j, z), mxdp2[i - 1][j - 1] + sum(j, z)));
                mxdp1[i][z] = max(mxdp1[i][z],  dp[i][j][z] + sum(j, z));
                mxdp2[i][z] = max(mxdp2[i][z],  dp[i][j][z] - sum(j, z));
            }
        }
    }

    long long ans = -INF;

    for (long long i = 0; i < n; i++)
    {
        for (long long j = 0; j < n; j++)
        {
            ans = max(ans, dp[k][i][j]);
        }
    }

    cout << ans;

    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

const long long X = 410, Y = 55, INF = 1e15;

long long dp[Y][X][X], mxdp1[Y][X], mxdp2[Y][X], lst[X], pr[X];

long long sum(long long l, long long r)
{
    if(r == 0) return pr[r];
    return pr[r]  - pr[l - 1];
}

int main()
{
    long long n, k, i, j;
    cin >> n >> k;

    for (long long i = 0; i < n; i++)
    {
        cin >> lst[i];
    }
    pr[0] = lst[0];
    for (long long i = 0; i < n; i++) pr[i] = pr[i - 1] + lst[i];

    for (long long i = 0; i < Y; i++)
    {
        for (long long j = 0; j < X; j++)
        {
            for (long long z = 0; z < X; z++)
            {
                dp[i][j][z] = -INF;
            }
        }
    }
    for (long long i = 0; i < n; i++)
    {
        dp[1][0][i] = 0;
        mxdp1[1][i] = max(((i == 0) ? -INF : mxdp1[1][i - 1]), sum(0, i));
        mxdp2[1][i] = max(((i == 0) ? -INF : mxdp2[1][i - 1]), -sum(0, i));
    }
    for (long long i = 2; i <= k; i++)
    {
        for (long long j = i - 1; j < n; j++)
        {
            for (long long z = j; z < n; z++)
            {
                dp[i][j][z] = max(dp[i][j][z], max(mxdp1[i - 1][j - 1] - sum(j, z), mxdp2[i - 1][j - 1] + sum(j, z)));
                mxdp1[i][z] = max(mxdp1[i][z],  dp[i][j][z] + sum(j, z));
                mxdp2[i][z] = max(mxdp2[i][z],  dp[i][j][z] - sum(j, z));
            }
        }
    }

    long long ans = -INF;

    for (long long i = 0; i < n; i++)
    {
        for (long long j = 0; j < n; j++)
        {
            ans = max(ans, dp[k][i][j]);
        }
    }

    cout << ans;

    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

const int X = 410, Y = 55, INF = 1e9;

int dp[Y][X][X], mxdp1[Y][X], mxdp2[Y][X], lst[X], pr[X];

int sum(int l, int r)
{
    if(r == 0) return pr[r];
    return pr[r]  - pr[l - 1];
}

int main()
{
    int n, k, i, j;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
    {
        cin >> lst[i];
    }
    pr[0] = lst[0];
    for (int i = 0; i < n; i++) pr[i] = pr[i - 1] + lst[i];

    for (int i = 0; i < Y; i++)
    {
        for (int j = 0; j < X; j++)
        {
            for (int z = 0; z < X; z++)
            {
                dp[i][j][z] = -INF;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        dp[1][0][i] = 0;
        mxdp1[1][i] = max(((i == 0) ? -INF : mxdp1[1][i - 1]), sum(0, i));
        mxdp2[1][i] = max(((i == 0) ? -INF : mxdp2[1][i - 1]), -sum(0, i));
    }
    for (int i = 2; i <= k; i++)
    {
        for (int j = i - 1; j < n; j++)
        {
            for (int z = j; z < n; z++)
            {
                dp[i][j][z] = max(dp[i][j][z], max(mxdp1[i - 1][j - 1] - sum(j, z), mxdp2[i - 1][j - 1] + sum(j, z)));
                mxdp1[i][z] = max(mxdp1[i][z],  dp[i][j][z] + sum(j, z));
                mxdp2[i][z] = max(mxdp2[i][z],  dp[i][j][z] - sum(j, z));
            }
        }
    }

    int ans = -INF;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            ans = max(ans, dp[k][i][j]);
        }
    }

    cout << ans;

    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

const long long X = 410, Y = 55, INF = 1e15;

long long dp[Y][X][X], mxdp1[Y][X], mxdp2[Y][X], lst[X], pr[X];

long long sum(long long l, long long r)
{
    if(r == 0) return pr[r];
    return pr[r]  - pr[l - 1];
}

int main()
{
    long long n, k, i, j;
    cin >> n >> k;

    for (long long i = 0; i < n; i++)
    {
        cin >> lst[i];
    }

    pr[0] = lst[0];
    for (long long i = 0; i < n; i++) pr[i] = pr[i - 1] + lst[i];

    for (long long i = 0; i < Y; i++)
    {
        for (long long j = 0; j < X; j++)
        {
            for (long long z = 0; z < X; z++)
            {
                dp[i][j][z] = -INF;
                mxdp1[i][z] = -INF;
                mxdp2[i][z] = -INF;
            }
        }
    }
    for (long long i = 0; i < n; i++)
    {
        dp[1][0][i] = 0;
        mxdp1[1][i] = max(((i == 0) ? -INF : mxdp1[1][i - 1]), sum(0, i));
        mxdp2[1][i] = max(((i == 0) ? -INF : mxdp2[1][i - 1]), -sum(0, i));
    }
    for (long long i = 2; i <= k; i++)
    {
        for (long long j = i - 1; j < n; j++)
        {
            for (long long z = j; z < n; z++)
            {
                dp[i][j][z] = max(dp[i][j][z], max(mxdp1[i - 1][j - 1] - sum(j, z), mxdp2[i - 1][j - 1] + sum(j, z)));
                mxdp1[i][z] = max(mxdp1[i][z],  dp[i][j][z] + sum(j, z));
                mxdp2[i][z] = max(mxdp2[i][z],  dp[i][j][z] - sum(j, z));
            }
        }
    }

    long long ans = -INF;

    for (long long i = 0; i < n; i++)
    {
        for (long long j = 0; j < n; j++)
        {
            ans = max(ans, dp[k][i][j]);
        }
    }

    cout << ans;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <ctime>
#pragma comment (linker, "/STACK:256000000")

using namespace std;

const int maxN = 500;
const int maxK = 60;
int d[2][maxN][maxN];
int s[maxN];
int a[maxN], b[maxN];
int n, k;

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		s[i] = s[i - 1] + a[i];
	}

	int u = 0, v = 1;
	for (int steps = 1; steps < k; ++steps, swap(u, v)) {
		for (int i = 1; i <= n; ++i) {
			for (int j = i; j <= n; ++j) {
				d[v][i][j] = -1000000000;
			}
		}
		for (int i = 1; i <= n; ++i) {
			for (int j = i; j <= n; ++j) {
				int A = s[j] - s[i - 1];
				int a = j + 1;
				for (int b = j + 1; b <= n; ++b) {
					int B = s[b] - s[a - 1];
					d[v][a][b] = max(d[v][a][b], d[u][i][j] + abs(B - A));
				}
			}
		}
	}
	int res = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = i; j <= n; ++j) {
			res = max(res, d[u][i][j]);
		}
	}
	cout << res << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <ctime>
#pragma comment (linker, "/STACK:256000000")

using namespace std;

const int maxN = 31000;
const int maxD = 400;
int d[2][maxD][maxN];
int s[maxN];
int a[maxN];
int n, k;

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		s[i] = s[i - 1] + a[i];
	}

	int u = 0, v = 1;
	for (int steps = 1; steps < k; ++steps, swap(u, v)) {
		for (int i = 0; i < maxD; ++i) {
			for (int j = 1; j <= n; ++j) {
				d[v][i][j] = -1000000000;
			}
		}
		for (int i = 0; i < maxD; ++i) {
			for (int j = 1 + i; j <= n; ++j) {
				int A = s[j] - s[j - i - 1];
				int a = j + 1;
				for (int b = j + 1; b <= j + maxD && b <= n; ++b) {
					int B = s[b] - s[a - 1];
					d[v][b - a][b] = max(d[v][b - a][b], d[u][i][j] + abs(B - A));
				}
			}
		}
	}
	int res = 0;
	for (int i = 0; i < maxD; ++i) {
		for (int j = 1 + i; j <= n; ++j) {
			res = max(res, d[u][i][j]);
		}
	}
	cout << res << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <ctime>
#pragma comment (linker, "/STACK:256000000")

using namespace std;

const int maxN = 31000;
const int maxD = 400;
int d[2][maxD][maxN];
int s[maxN];
int a[maxN];
int n, k;

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		s[i] = s[i - 1] + a[i];
	}

	int step = maxD;
	if (n > 1000) {
		step = 50;
	}
	int u = 0, v = 1;
	for (int steps = 1; steps < k; ++steps, swap(u, v)) {
		for (int i = 0; i < step; ++i) {
			for (int j = 1; j <= n; ++j) {
				d[v][i][j] = -1000000000;
			}
		}
		for (int i = 0; i < step; ++i) {
			for (int j = 1 + i; j <= n; ++j) {
				int A = s[j] - s[j - i - 1];
				int a = j + 1;
				for (int b = j + 1; b <= j + step && b <= n; ++b) {
					int B = s[b] - s[a - 1];
					d[v][b - a][b] = max(d[v][b - a][b], d[u][i][j] + abs(B - A));
				}
			}
		}
	}
	int res = 0;
	for (int i = 0; i < maxD; ++i) {
		for (int j = 1 + i; j <= n; ++j) {
			res = max(res, d[u][i][j]);
		}
	}
	cout << res << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <ctime>
#pragma comment (linker, "/STACK:256000000")

using namespace std;

const int maxN = 31000;
const int maxD = 100;
const int maxK = 60;
int d[2][maxD][maxN];
int s[maxN];
int a[maxN];
int n, k;

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		s[i] = s[i - 1] + a[i];
	}

	int u = 0, v = 1;
	for (int steps = 1; steps < k; ++steps, swap(u, v)) {
		for (int i = 0; i < maxD; ++i) {
			for (int j = 1; j <= n; ++j) {
				d[v][i][j] = -1000000000;
			}
		}
		for (int i = 0; i < maxD; ++i) {
			for (int j = 1 + i; j <= n; ++j) {
				int A = s[j] - s[j - i - 1];
				int a = j + 1;
				for (int b = j + 1; b <= j + maxD && b <= n; ++b) {
					int B = s[b] - s[a - 1];
					d[v][b - a][b] = max(d[v][b - a][b], d[u][i][j] + abs(B - A));
				}
			}
		}
	}
	int res = 0;
	for (int i = 0; i < maxD; ++i) {
		for (int j = 1 + i; j <= n; ++j) {
			res = max(res, d[u][i][j]);
		}
	}
	cout << res << endl;

	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <ctime>
#include <numeric>
#include <vector>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <complex>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <string>
#include <sstream>
#include <set>
#include <stack>
#include <queue>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<short, short> pss;
typedef pair<LL, LL> pll;
typedef pair<ULL, ULL> puu;
typedef pair<ld, ld> pdd;
template<class T> inline T sqr(T x) { return x * x; }
template<class T> inline string tostr(const T & x) { stringstream ss; ss << x; return ss.str(); }
inline LL parse(const string & s) { stringstream ss(s); LL x; ss >> x; return x; }
#define left asdleft
#define right asdright
#define link asdlink
#define unlink asdunlink
#define next asdnext
#define prev asdprev
#define y0 asdy0
#define y1 asdy1
#define mp make_pair
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define clr(ar,val) memset(ar, val, sizeof(ar))
#define istr stringstream
#define forn(i,n) for(int i=0;i<(n);++i)
#define forv(i,v) forn(i,sz(v))
#define X first
#define Y second
#define hash asdhash
#define move asdmove
const ld EPS = 1e-9;
const int inf = 1000*1000*1000;
const char cinf = 102;
const LL linf = inf * 1ll * inf;
const ld dinf = 1e200;
const ld PI = 3.1415926535897932384626433832795l;
int gcd(int a,int b){return a?gcd(b%a,a):b;}
LL gcd(LL a,LL b){return a?gcd(b%a,a):b;}
LL powmod(LL a,LL p,LL m){LL r=1;while(p){if(p&1)r=r*a%m;p>>=1;a=a*a%m;}return r;}
#define FAIL { cerr<<"assertion failed on line "<<__LINE__<<endl; exit(123);}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);

  int n,k;
  cin>>n>>k;
  vector<LL> A(n+1);
  forn(i,n){
    int a;
    cin>>a;
    A[i+1]=A[i]+a;
  }
  ++n;

  vector<vector<LL>> dp(n,vector<LL>(n,-inf));
  auto ndp=dp;
  forn(j,n){
    forn(i,j){
      dp[i][j]=0;
    }
  }
  for(int q=3;q<=k+1;++q){
    forn(c,n){
      forn(b,c){
        ndp[b][c]=-inf;
        forn(a,b){
          if(dp[a][b]==-inf)
            continue;
          ndp[b][c]=max(ndp[b][c],dp[a][b]+std::abs(A[a]+A[c]-2*A[b]));
        }
      }
    }
    swap(ndp,dp);
  }

  LL r=-inf;
  forn(j,n){
    forn(i,j){
      r=max(r,dp[i][j]);
    }
  }
  cout<<r;

  return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <ctime>
#include <numeric>
#include <vector>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <complex>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <string>
#include <sstream>
#include <set>
#include <stack>
#include <queue>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<short, short> pss;
typedef pair<LL, LL> pll;
typedef pair<ULL, ULL> puu;
typedef pair<ld, ld> pdd;
template<class T> inline T sqr(T x) { return x * x; }
template<class T> inline string tostr(const T & x) { stringstream ss; ss << x; return ss.str(); }
inline LL parse(const string & s) { stringstream ss(s); LL x; ss >> x; return x; }
#define left asdleft
#define right asdright
#define link asdlink
#define unlink asdunlink
#define next asdnext
#define prev asdprev
#define y0 asdy0
#define y1 asdy1
#define mp make_pair
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define clr(ar,val) memset(ar, val, sizeof(ar))
#define istr stringstream
#define forn(i,n) for(int i=0;i<(n);++i)
#define forv(i,v) forn(i,sz(v))
#define X first
#define Y second
#define hash asdhash
#define move asdmove
const ld EPS = 1e-9;
const int inf = 1000*1000*1000;
const char cinf = 102;
const LL linf = inf * 1ll * inf;
const ld dinf = 1e200;
const ld PI = 3.1415926535897932384626433832795l;
int gcd(int a,int b){return a?gcd(b%a,a):b;}
LL gcd(LL a,LL b){return a?gcd(b%a,a):b;}
LL powmod(LL a,LL p,LL m){LL r=1;while(p){if(p&1)r=r*a%m;p>>=1;a=a*a%m;}return r;}
#define FAIL { cerr<<"assertion failed on line "<<__LINE__<<endl; exit(123);}

void gen(){
  int n,k;
  n=400;
  k=50;
  cout<<n<<' '<<k<<endl;
  forn(i,n){
    cout<<rand()%20001-10000<<' ';
  }
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);

//  gen();return 0;

  int n,k;
  cin>>n>>k;
  vector<int> A(n+1);
  forn(i,n){
    int a;
    cin>>a;
    A[i+1]=A[i]+a;
  }
  ++n;

  vector<vector<int>> dp(n,vector<int>(n,-inf));
  auto ndp=dp;
  forn(j,n){
    forn(i,j){
      dp[i][j]=0;
    }
  }
  for(int q=3;q<=k+1;++q){
    forn(c,n){
      forn(b,c){
        ndp[b][c]=-inf;
        forn(a,b){
          if(dp[a][b]==-inf)
            continue;
          ndp[b][c]=max(ndp[b][c],dp[a][b]+abs(A[a]+A[c]-2*A[b]));
        }
      }
    }
    swap(ndp,dp);
  }

  int r=-inf;
  forn(j,n){
    forn(i,j){
      r=max(r,dp[i][j]);
    }
  }
  cout<<r;

  return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 444, max_k = 55, inf = 1000000000;

int n, k, a[max_n];
pair<int, int> dp[max_n][max_k][2];

int myabs(int a) {
    if (a < 0) {
        return -a;
    }
    return a;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    dp[0][1][0] = make_pair(0, a[0]);
    dp[0][1][1] = make_pair(0, a[0]);
    for (int i = 2; i <= k; ++i) {
        dp[0][i][0] = make_pair(-inf, inf);
        dp[0][i][1] = make_pair(-inf, -inf);
    }
    int ans = -inf;
    for (int i = 1; i < n; ++i) {
        for (int j = 2; j < k; ++j) {
            dp[i][j][0] = dp[i - 1][j][0];
            int sum = 0;
            for (int x = 1; x <= i; ++x) {
                sum += a[i - x + 1];
                if (dp[i - x][j - 1][0].first > -inf) {
                    if (dp[i][j][0].first - dp[i][j][0].second < dp[i - x][j - 1][0].first + myabs(sum - dp[i - x][j - 1][0].second) - sum) {
                        //cout << "!" << i << " " << j << endl;
                        dp[i][j][0].first = dp[i - x][j - 1][0].first + myabs(sum - dp[i - x][j - 1][0].second);
                        dp[i][j][0].second = sum;
                    }
                }
                if (dp[i - x][j - 1][1].first > -inf) {
                    if (dp[i][j][0].first - dp[i][j][0].second < dp[i - x][j - 1][1].first + myabs(sum - dp[i - x][j - 1][1].second) - sum) {
                        //cout << "!" << i << " " << j << endl;
                        dp[i][j][0].first = dp[i - x][j - 1][1].first + myabs(sum - dp[i - x][j - 1][1].second);
                        dp[i][j][0].second = sum;
                    }
                }
            }
            dp[i][j][1] = dp[i - 1][j][1];
            sum = 0;
            for (int x = 1; x <= i; ++x) {
                sum += a[i - x + 1];
                if (dp[i - x][j - 1][1].first > -inf) {
                    if (dp[i][j][1].first + dp[i][j][1].second < dp[i - x][j - 1][1].first + myabs(sum - dp[i - x][j - 1][1].second) + sum) {
                        dp[i][j][1].first = dp[i - x][j - 1][1].first + myabs(sum - dp[i - x][j - 1][1].second);
                        dp[i][j][1].second = sum;
                    }
                }
                if (dp[i - x][j - 1][0].first > -inf) {
                    if (dp[i][j][1].first + dp[i][j][1].second < dp[i - x][j - 1][0].first + myabs(sum - dp[i - x][j - 1][0].second) + sum) {
                        dp[i][j][1].first = dp[i - x][j - 1][0].first + myabs(sum - dp[i - x][j - 1][0].second);
                        dp[i][j][1].second = sum;
                    }
                }
            }
        }
        dp[i][1][0] = dp[i - 1][1][0];
        int sum = 0;
        for (int x = 1; x <= i + 1; ++x) {
            sum += a[i - x + 1];
            dp[i][1][0] = min(dp[i][1][0], make_pair(0, sum));
        }
        dp[i][1][1] = dp[i - 1][1][1];
        sum = 0;
        for (int x = 1; x <= i + 1; ++x) {
            sum += a[i - x + 1];
            dp[i][1][1] = max(dp[i][1][1], make_pair(0, sum));
        }

        dp[i][k][0] = dp[i - 1][k][0];
        sum = 0;
        for (int x = 1; x <= i; ++x) {
            sum += a[i - x + 1];
            if (dp[i - x][k - 1][0].first > -inf) {
                if (dp[i][k][0].first < dp[i - x][k - 1][0].first + myabs(sum - dp[i - x][k - 1][0].second)) {
                    //cout << "!" << i << " " << k << endl;
                    dp[i][k][0].first = dp[i - x][k - 1][0].first + myabs(sum - dp[i - x][k - 1][0].second);
                    dp[i][k][0].second = sum;
                }
            }
            if (dp[i - x][k - 1][1].first > -inf) {
                if (dp[i][k][0].first < dp[i - x][k - 1][1].first + myabs(sum - dp[i - x][k - 1][1].second)) {
                    //cout << "!" << i << " " << k << endl;
                    dp[i][k][0].first = dp[i - x][k - 1][1].first + myabs(sum - dp[i - x][k - 1][1].second);
                    dp[i][k][0].second = sum;
                }
            }
        }
        dp[i][k][1] = dp[i - 1][k][1];
        sum = 0;
        for (int x = 1; x <= i; ++x) {
            sum += a[i - x + 1];
            if (dp[i - x][k - 1][1].first > -inf) {
                if (dp[i][k][1].first < dp[i - x][k - 1][1].first + myabs(sum - dp[i - x][k - 1][1].second)) {
                    dp[i][k][1].first = dp[i - x][k - 1][1].first + myabs(sum - dp[i - x][k - 1][1].second);
                    dp[i][k][1].second = sum;
                }
            }
            if (dp[i - x][k - 1][0].first > -inf) {
                if (dp[i][k][1].first < dp[i - x][k - 1][0].first + myabs(sum - dp[i - x][k - 1][0].second)) {
                    dp[i][k][1].first = dp[i - x][k - 1][0].first + myabs(sum - dp[i - x][k - 1][0].second);
                    dp[i][k][1].second = sum;
                }
            }
        }

        /*cout << endl;
        for (int j = 1; j <= k; ++j) {
            cout << i << " " << j << " (" << dp[i][j][0].first << " " << dp[i][j][0].second << ") " << "  (" << dp[i][j][1].first << " " << dp[i][j][1].second << ")\n";
        }
        cout << endl;*/
        ans = max(ans, dp[i][k][0].first);
        ans = max(ans, dp[i][k][1].first);
    }
    cout << ans << endl;
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int max_n = 33333, max_k = 222, inf = 1000000000;

int n, k, a[max_n];
pair<int, int> dp[max_n][max_k][2];

int myabs(int a) {
    if (a < 0) {
        return -a;
    }
    return a;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    dp[0][1][0] = make_pair(0, a[0]);
    dp[0][1][1] = make_pair(0, a[0]);
    for (int i = 2; i <= k; ++i) {
        dp[0][i][0] = make_pair(-inf, inf);
        dp[0][i][1] = make_pair(-inf, -inf);
    }
    int ans = -inf;
    for (int i = 1; i < n; ++i) {
        for (int j = 2; j < k; ++j) {
            dp[i][j][0] = dp[i - 1][j][0];
            int sum = 0;
            for (int x = 1; x <= i; ++x) {
                sum += a[i - x + 1];
                if (dp[i - x][j - 1][0].first > -inf) {
                    if (dp[i][j][0].first - dp[i][j][0].second < dp[i - x][j - 1][0].first + myabs(sum - dp[i - x][j - 1][0].second) - sum) {
                        //cout << "!" << i << " " << j << endl;
                        dp[i][j][0].first = dp[i - x][j - 1][0].first + myabs(sum - dp[i - x][j - 1][0].second);
                        dp[i][j][0].second = sum;
                    }
                }
                if (dp[i - x][j - 1][1].first > -inf) {
                    if (dp[i][j][0].first - dp[i][j][0].second < dp[i - x][j - 1][1].first + myabs(sum - dp[i - x][j - 1][1].second) - sum) {
                        //cout << "!" << i << " " << j << endl;
                        dp[i][j][0].first = dp[i - x][j - 1][1].first + myabs(sum - dp[i - x][j - 1][1].second);
                        dp[i][j][0].second = sum;
                    }
                }
            }
            dp[i][j][1] = dp[i - 1][j][1];
            sum = 0;
            for (int x = 1; x <= i; ++x) {
                sum += a[i - x + 1];
                if (dp[i - x][j - 1][1].first > -inf) {
                    if (dp[i][j][1].first + dp[i][j][1].second < dp[i - x][j - 1][1].first + myabs(sum - dp[i - x][j - 1][1].second) + sum) {
                        dp[i][j][1].first = dp[i - x][j - 1][1].first + myabs(sum - dp[i - x][j - 1][1].second);
                        dp[i][j][1].second = sum;
                    }
                }
                if (dp[i - x][j - 1][0].first > -inf) {
                    if (dp[i][j][1].first + dp[i][j][1].second < dp[i - x][j - 1][0].first + myabs(sum - dp[i - x][j - 1][0].second) + sum) {
                        dp[i][j][1].first = dp[i - x][j - 1][0].first + myabs(sum - dp[i - x][j - 1][0].second);
                        dp[i][j][1].second = sum;
                    }
                }
            }
        }
        dp[i][1][0] = dp[i - 1][1][0];
        int sum = 0;
        for (int x = 1; x <= i + 1; ++x) {
            sum += a[i - x + 1];
            dp[i][1][0] = min(dp[i][1][0], make_pair(0, sum));
        }
        dp[i][1][1] = dp[i - 1][1][1];
        sum = 0;
        for (int x = 1; x <= i + 1; ++x) {
            sum += a[i - x + 1];
            dp[i][1][1] = max(dp[i][1][1], make_pair(0, sum));
        }

        dp[i][k][0] = dp[i - 1][k][0];
        sum = 0;
        for (int x = 1; x <= i; ++x) {
            sum += a[i - x + 1];
            if (dp[i - x][k - 1][0].first > -inf) {
                if (dp[i][k][0].first < dp[i - x][k - 1][0].first + myabs(sum - dp[i - x][k - 1][0].second)) {
                    //cout << "!" << i << " " << k << endl;
                    dp[i][k][0].first = dp[i - x][k - 1][0].first + myabs(sum - dp[i - x][k - 1][0].second);
                    dp[i][k][0].second = sum;
                }
            }
            if (dp[i - x][k - 1][1].first > -inf) {
                if (dp[i][k][0].first < dp[i - x][k - 1][1].first + myabs(sum - dp[i - x][k - 1][1].second)) {
                    //cout << "!" << i << " " << k << endl;
                    dp[i][k][0].first = dp[i - x][k - 1][1].first + myabs(sum - dp[i - x][k - 1][1].second);
                    dp[i][k][0].second = sum;
                }
            }
        }
        dp[i][k][1] = dp[i - 1][k][1];
        sum = 0;
        for (int x = 1; x <= i; ++x) {
            sum += a[i - x + 1];
            if (dp[i - x][k - 1][1].first > -inf) {
                if (dp[i][k][1].first < dp[i - x][k - 1][1].first + myabs(sum - dp[i - x][k - 1][1].second)) {
                    dp[i][k][1].first = dp[i - x][k - 1][1].first + myabs(sum - dp[i - x][k - 1][1].second);
                    dp[i][k][1].second = sum;
                }
            }
            if (dp[i - x][k - 1][0].first > -inf) {
                if (dp[i][k][1].first < dp[i - x][k - 1][0].first + myabs(sum - dp[i - x][k - 1][0].second)) {
                    dp[i][k][1].first = dp[i - x][k - 1][0].first + myabs(sum - dp[i - x][k - 1][0].second);
                    dp[i][k][1].second = sum;
                }
            }
        }

        /*cout << endl;
        for (int j = 1; j <= k; ++j) {
            cout << i << " " << j << " (" << dp[i][j][0].first << " " << dp[i][j][0].second << ") " << "  (" << dp[i][j][1].first << " " << dp[i][j][1].second << ")\n";
        }
        cout << endl;*/
        ans = max(ans, dp[i][k][0].first);
        ans = max(ans, dp[i][k][1].first);
    }
    cout << ans << endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <string>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef complex < double > cd;

const bool db = false;

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define sd second
#define fs first

#define inf 1000000007
#define nmax 100010
#define mmax 100010
#define eps 1e-9

int n, k;
int a[nmax], sum[nmax];
int dp[444][444][55];
set < pair < int, int > > s;

int F(int l, int r) {
	return sum[r] - sum[l - 1];
}

inline void relax(int &a, int b) {
	if(a < b) a = b;
}

inline int A(int a) {
	return a < 0 ? -a : a;
}

int main() {
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	//freopen("metro.in", "r", stdin); freopen("metro.out", "w", stdout);
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		sum[i] = a[i] + sum[i - 1];
	}
	for(int i = 0; i < 444; ++i)
		for(int j = 0; j < 444; ++j)
			for(int k = 0; k < 55; ++k)
				dp[i][j][k] =-inf;
	for(int i = 1; i <= n; ++i)
		for(int j = i; j <= n; ++j)
			dp[i][j][1] = 0;
	int ans = -inf;
	for(int it = 1; it < k; ++it) {
		for(int R = 1; R <= n; ++R) {
			for(int L = 1; L <= n; ++L) {
				int S = F(L, R);
				if(dp[L][R][it] > -inf) {
					int mn = R, mx = R;
					for(int N = R + 1; N <= n; ++N) {
						relax(dp[mn + 1][N][it + 1], dp[L][R][it] + A(S - F(mn + 1, N)));
						relax(dp[mx + 1][N][it + 1], dp[L][R][it] + A(S - F(mx + 1, N)));
						if(sum[N] < sum[mn]) mn = N;
						if(sum[N] > sum[mx]) mx = N;
					}
					for(int NL = R + 1; NL <= R + 20; ++NL)
						for(int NR = NL; NR <= R + 20; ++NR)
							relax(dp[NL][NR][it + 1], dp[L][R][it] + A(S - F(NL, NR)));
				}
			}
		}
	}
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			ans = max(ans, dp[i][j][k]);
	cout << ans << "\n";
	getchar(); getchar();
	return 0;
}