#pragma comment(linker, "/stack:20000000")
#define _CRT_SECURE_NO_WARNINGS
# include "stdio.h"
#ifndef DEBUG
# include <iostream>
# include <cmath>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <deque>
# include <functional>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_map>
# else
# include <tr1/unordered_map>
using namespace std::tr1;
# endif
# else
# include <unordered_map>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,N,...) N

#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs

#define DBN1(a)			std::cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"

#define DEB(x) std::cerr<<#x<<"="<<(x)<<"  "

#ifdef DEBUG
#define DB(x) DEB(x)
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
#else
#define DB(x) ((void)42)
#define DBN(...) ((void)42)
#define DA(a,n) ((void)42)
#define DAR(a,n,s) ((void)42)
#endif

double __begin;
#define DTIME(ccc) __begin = clock(); ccc; std::cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<std::endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;

template<typename T1,typename T2,typename T3>
struct triple{T1 a;T2 b;T3 c;triple(){};triple(T1 _a,T2 _b, T3 _c):a(_a),b(_b),c(_c){}};
#define tri triple<int,int,int>
#define trl triple<ll,ll,ll>
template<typename T1,typename T2,typename T3>
bool operator<(const triple<T1,T2,T3> &t1,const triple<T1,T2,T3> &t2){if (t1.a!=t2.a) return t1.a<t2.a; else return t1.b<t2.b;}
template<typename T1, typename T2, typename T3>
inline std::ostream& operator << (std::ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }

#define FI(n) for(int i=0;i<n;i++)
#define FI1(n) for(int i=1;i<n;i++)
#define FJ(n) for(int j=0;j<n;j++)
#define FJ1(n) for(int j=1;j<n;j++)
#define FJI for(int j=0;j<i;j++)
#define FJI1 for(int j=1;j<i;j++)
#define FJIE for(int j=0;j<=i;j++)
#define FJIE1 for(int j=1;j<=i;j++)
#define For(i,a,b) for(int i=a;i<b;i++)

// Input macros
#define S(n)                        scanf("%d",&n)
#define S2(a,b)						scanf("%d%d",&a,&b)
#define Sc(n)                       scanf("%c",&n)
#define Sl(n)                       scanf("%I64d",&n)
#define Sl2(a,b)					scanf("%I64d%I63d",&a,&b)
#define Sf(n)                       scanf("%lf",&n)
#define Sf2(a,b)                    scanf("%lf%lf",&a,&b)

//Output macros
#define Pr(n)                       printf("%d ",n)
#define Prn(n)                      printf("%d\n",n)
#define Prc(n)                      printf("%c",n)
#define Prcn(n)                     printf("%c\n",n)
#define Prl(n)                      printf("%I64d ",n)
#define Prln(n)                     printf("%I64d\n",n)
#define Prf(n)                      printf("%lf ",n)
#define Prfn(n)                     printf("%lf\n",n)
#define Prs(n)                      printf("%s ",n)
#define Prsn(n)                     printf("%s\n",n)

//STL containers
#define foreach(v, c)               for( auto v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ((b).find(a) != (b).end())
#define in2(a,b)                    (find(all(b),a) != (b).end())
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define svi(vec,n) FI(n) S(vec[y])
#define svl(vec,n) FI(n) Sl(vec[y])
#define IT ::iterator

//int some_primes[10] = {100271, 500179, 1000003, 2000227, 5000321}
//fill char arrays
#define fill(a,v)                    memset(a, v, sizeof (a))

inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){ int t = v >> 32; int p = (v & ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));return((x >> 16) | (x << 16));}
inline int sign(int x){return (x >> 31) | (-x >> 31);}
inline bool ispow2(int x){return (x!=0 && (x&(x-1))==0);}
#define checkbit(n,b)        ( (n >> b) & 1)
#define INDEX(arr,ind)       (lower_bound(all(arr),ind)-arr.begin())
#define sor(a)  sort(all(a))
#define rsor(a) sort(all(a)); reverse(all(a));

//STL output ********************************
template<typename T1,typename T2>inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p){return os << "(" << p.first << ", " << p.second << ")";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::vector<T>& v){bool first=true;os<<"[";for(unsigned int i=0;i<v.size();i++){if(!first)os<<", ";os<<v[i];first=false;}return os<<"]";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::set<T>&v){bool first=true;os << "[";for(typename std::set<T>::const_iterator ii=v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T1, typename T2>inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v){bool first = true; os << "[";for (typename std::map<T1,T2>::const_iterator ii =v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T,typename T2>void printarray(T  a[],T2 sz,T2 beg=0){for(T2 i=beg;i<sz;i++) cout<<a[i]<<" ";cout<<endl;}

#define FREIN(FILE) freopen(FILE,"rt",stdin)
#define FREOUT(FILE) freopen(FILE,"wt",stdout)

#define sqr(x) ((x)*(x))
#define sqrt(x) sqrt(1.0*(x))
#define pow(x,n) pow(1.0*(x),n)

inline ll binpow(ll x,ll n){ll res=1;while(n){if(n&1)res*=x;x*=x;n>>=1;}return res;}
inline ll powmod(ll x,ll n, ll _mod){ll res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
inline ll gcd(ll a,ll b){ll t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline int gcd(int a,int b){int t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline ll lcm(int a,int b){return a/gcd(a,b)*(ll)b;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll gcd(ll a,ll b,ll c){return gcd(gcd(a,b),c);}
inline int gcd(int a,int b,int c){return gcd(gcd(a,b),c);}
inline ll lcm(ll a,ll b,ll c){return lcm(lcm(a,b),c);}
inline ll lcm(int a,int b,int c){return lcm(lcm(a,b),(ll)c);}

inline ll max(ll a,ll b){return (a>b)?a:b;}
inline int max(int a,int b){return (a>b)?a:b;}
inline double max(double a,double b){return (a>b)?a:b;}
inline ll max(ll a,ll b,ll c){return max(a,max(b,c));}
inline int max(int a,int b,int c){return max(a,max(b,c));}
inline double max(double a,double b,double c){return max(a,max(b,c));}
inline ll min(ll a,ll b){return (a<b)?a:b;}
inline int min(int a,int b){return (a<b)?a:b;}
inline double min(double a,double b){return (a<b)?a:b;}
inline ll min(ll a,ll b,ll c){return min(a,min(b,c));}
inline int min(int a,int b,int c){return min(a,min(b,c));}
inline double min(double a,double b,double c){return min(a,min(b,c));}

template<class T>
inline void getar(T a, int n, int m){ for (int i = 0; i < n; i++) for (int j = 0; j < m;++j) { scanf("%d", &a[i][j]); } }
inline void getar(int *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d", a + ii); } }
inline void getar(pii *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d%d", &a[ii].first, &a[ii].second); } }
inline void getar(ll *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%I64d", a + ii); } }
template<class T>
inline void cinarr(T a, int n){ for (int i=0;i<n;++i) cin >> a[i];}
// Useful constants

#define INF			1011111111
#define EPS			(double)1e-8
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif

pii dmin[1000][400];
pii dmax[1000][400];
int a[1000];
int s[1000];
void upd1(pii p1, pii &p2){
	if (p2.first + p2.second < p1.first + p1.second){
		p2 = p1;
	}
}
void upd2(pii p1, pii &p2){
	if (p2.first - p2.second < p1.first - p1.second){
		p2 = p1;
	}
}
int main(){
	int n, k;
	cin >> n >> k;
	FI(400)
		FJ(100){
		dmax[i][j] = { -INF, -INF };
		dmin[i][j] = { -INF, +INF };
	}
	int maxs = 0;
	for (int i = 0; i < n; ++i){
		cin >> a[i];
		if (s[i] > maxs) maxs = s[i];
		s[i + 1] = s[i] + a[i];
		dmin[i][1] = dmax[i][1] = { 0, s[i + 1] };
		dmin[i][1].second = s[i + 1] - maxs;
	}
	for (int st = 2; st < k; ++st){
		for (int i = 1; i < n; ++i){
			if (i != 1) upd1(dmax[i - 2][st - 1], dmax[i - 1][st - 1]);
			if (i != 1) upd2(dmin[i - 2][st - 1], dmin[i - 1][st - 1]);
			pii pmin = dmin[i - 1][st - 1];
			pii pmax = dmax[i - 1][st - 1];
			int sum = 0;
			//DBN(i, st, pmin, pmax);
			for (int j = i; j < n; ++j){
				sum += a[j];
				pii& rmin = dmin[j][st];
				pii& rmax = dmax[j][st];
				int res = pmin.first + max(sum - pmin.second, pmax.second - sum);
				if (rmin.first - rmin.second < res - sum){
					rmin = { res, sum };
				}
				if (rmax.first + rmax.second < res + sum){
					rmax = { res, sum };
				}
// 				if (rmin.first < res || rmin.first == res && rmin.second > sum){
// 					rmin = { res, sum };
// 				}
// 				if (rmax.first < res || rmax.first == res && rmax.second < sum){
// 					rmax = { res, sum };
// 				}
			}
		}
	}
	int ans = -INF;
	int sum = s[n];
	for (int i = 1; i < n; ++i){
		for (int j = i; j < n; ++j){
			sum = s[j + 1] - s[i];
			ans = max(ans, dmin[i - 1][k - 1].first + sum - dmin[i - 1][k - 1].second, dmax[i - 1][k - 1].first - sum + dmax[i - 1][k - 1].second);
		}
	}
	cout << ans << endl;
	return 0;
}

// int a[100];
// int l[100];
// int r[100];
// bool used[100];
// int main()
// {
// 	if (false)
// 	{
// 		int n;
// 		cin >> n;
// 		FI(n){
// 			cin >> l[i] >> r[i];
// 			a[i];
// 		}
// 		do{
//
// 		} while (next_permutation(a, a + n));
//
//
// 	}
// 	return 0;
// }
//


#pragma comment(linker, "/stack:20000000")
#define _CRT_SECURE_NO_WARNINGS
# include "stdio.h"
#ifndef DEBUG
# include <iostream>
# include <cmath>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <deque>
# include <functional>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_map>
# else
# include <tr1/unordered_map>
using namespace std::tr1;
# endif
# else
# include <unordered_map>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,N,...) N

#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs

#define DBN1(a)			std::cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"

#define DEB(x) std::cerr<<#x<<"="<<(x)<<"  "

#ifdef DEBUG
#define DB(x) DEB(x)
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
#else
#define DB(x) ((void)42)
#define DBN(...) ((void)42)
#define DA(a,n) ((void)42)
#define DAR(a,n,s) ((void)42)
#endif

double __begin;
#define DTIME(ccc) __begin = clock(); ccc; std::cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<std::endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;

template<typename T1,typename T2,typename T3>
struct triple{T1 a;T2 b;T3 c;triple(){};triple(T1 _a,T2 _b, T3 _c):a(_a),b(_b),c(_c){}};
#define tri triple<int,int,int>
#define trl triple<ll,ll,ll>
template<typename T1,typename T2,typename T3>
bool operator<(const triple<T1,T2,T3> &t1,const triple<T1,T2,T3> &t2){if (t1.a!=t2.a) return t1.a<t2.a; else return t1.b<t2.b;}
template<typename T1, typename T2, typename T3>
inline std::ostream& operator << (std::ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }

#define FI(n) for(int i=0;i<n;i++)
#define FI1(n) for(int i=1;i<n;i++)
#define FJ(n) for(int j=0;j<n;j++)
#define FJ1(n) for(int j=1;j<n;j++)
#define FJI for(int j=0;j<i;j++)
#define FJI1 for(int j=1;j<i;j++)
#define FJIE for(int j=0;j<=i;j++)
#define FJIE1 for(int j=1;j<=i;j++)
#define For(i,a,b) for(int i=a;i<b;i++)

// Input macros
#define S(n)                        scanf("%d",&n)
#define S2(a,b)						scanf("%d%d",&a,&b)
#define Sc(n)                       scanf("%c",&n)
#define Sl(n)                       scanf("%I64d",&n)
#define Sl2(a,b)					scanf("%I64d%I63d",&a,&b)
#define Sf(n)                       scanf("%lf",&n)
#define Sf2(a,b)                    scanf("%lf%lf",&a,&b)

//Output macros
#define Pr(n)                       printf("%d ",n)
#define Prn(n)                      printf("%d\n",n)
#define Prc(n)                      printf("%c",n)
#define Prcn(n)                     printf("%c\n",n)
#define Prl(n)                      printf("%I64d ",n)
#define Prln(n)                     printf("%I64d\n",n)
#define Prf(n)                      printf("%lf ",n)
#define Prfn(n)                     printf("%lf\n",n)
#define Prs(n)                      printf("%s ",n)
#define Prsn(n)                     printf("%s\n",n)

//STL containers
#define foreach(v, c)               for( auto v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ((b).find(a) != (b).end())
#define in2(a,b)                    (find(all(b),a) != (b).end())
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define svi(vec,n) FI(n) S(vec[y])
#define svl(vec,n) FI(n) Sl(vec[y])
#define IT ::iterator

//int some_primes[10] = {100271, 500179, 1000003, 2000227, 5000321}
//fill char arrays
#define fill(a,v)                    memset(a, v, sizeof (a))

inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){ int t = v >> 32; int p = (v & ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));return((x >> 16) | (x << 16));}
inline int sign(int x){return (x >> 31) | (-x >> 31);}
inline bool ispow2(int x){return (x!=0 && (x&(x-1))==0);}
#define checkbit(n,b)        ( (n >> b) & 1)
#define INDEX(arr,ind)       (lower_bound(all(arr),ind)-arr.begin())
#define sor(a)  sort(all(a))
#define rsor(a) sort(all(a)); reverse(all(a));

//STL output ********************************
template<typename T1,typename T2>inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p){return os << "(" << p.first << ", " << p.second << ")";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::vector<T>& v){bool first=true;os<<"[";for(unsigned int i=0;i<v.size();i++){if(!first)os<<", ";os<<v[i];first=false;}return os<<"]";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::set<T>&v){bool first=true;os << "[";for(typename std::set<T>::const_iterator ii=v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T1, typename T2>inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v){bool first = true; os << "[";for (typename std::map<T1,T2>::const_iterator ii =v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T,typename T2>void printarray(T  a[],T2 sz,T2 beg=0){for(T2 i=beg;i<sz;i++) cout<<a[i]<<" ";cout<<endl;}

#define FREIN(FILE) freopen(FILE,"rt",stdin)
#define FREOUT(FILE) freopen(FILE,"wt",stdout)

#define sqr(x) ((x)*(x))
#define sqrt(x) sqrt(1.0*(x))
#define pow(x,n) pow(1.0*(x),n)

inline ll binpow(ll x,ll n){ll res=1;while(n){if(n&1)res*=x;x*=x;n>>=1;}return res;}
inline ll powmod(ll x,ll n, ll _mod){ll res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
inline ll gcd(ll a,ll b){ll t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline int gcd(int a,int b){int t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline ll lcm(int a,int b){return a/gcd(a,b)*(ll)b;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll gcd(ll a,ll b,ll c){return gcd(gcd(a,b),c);}
inline int gcd(int a,int b,int c){return gcd(gcd(a,b),c);}
inline ll lcm(ll a,ll b,ll c){return lcm(lcm(a,b),c);}
inline ll lcm(int a,int b,int c){return lcm(lcm(a,b),(ll)c);}

inline ll max(ll a,ll b){return (a>b)?a:b;}
inline int max(int a,int b){return (a>b)?a:b;}
inline double max(double a,double b){return (a>b)?a:b;}
inline ll max(ll a,ll b,ll c){return max(a,max(b,c));}
inline int max(int a,int b,int c){return max(a,max(b,c));}
inline double max(double a,double b,double c){return max(a,max(b,c));}
inline ll min(ll a,ll b){return (a<b)?a:b;}
inline int min(int a,int b){return (a<b)?a:b;}
inline double min(double a,double b){return (a<b)?a:b;}
inline ll min(ll a,ll b,ll c){return min(a,min(b,c));}
inline int min(int a,int b,int c){return min(a,min(b,c));}
inline double min(double a,double b,double c){return min(a,min(b,c));}

template<class T>
inline void getar(T a, int n, int m){ for (int i = 0; i < n; i++) for (int j = 0; j < m;++j) { scanf("%d", &a[i][j]); } }
inline void getar(int *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d", a + ii); } }
inline void getar(pii *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d%d", &a[ii].first, &a[ii].second); } }
inline void getar(ll *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%I64d", a + ii); } }
template<class T>
inline void cinarr(T a, int n){ for (int i=0;i<n;++i) cin >> a[i];}
// Useful constants

#define INF			1011111111
#define EPS			(double)1e-8
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif

int dmin[1000][400];
int dmax[1000][400];
int a[1000];
int s[1000];
void upd(int x, int &y){
	if (y < x) y = x;
}
int main(){
	int n, k;
	cin >> n >> k;
	FI(400)
		FJ(100){
		dmax[i][j] = -INF;
		dmin[i][j] = -INF;
	}
	int maxs = 0;
	int mins = 0;
	for (int i = 0; i < n; ++i){
		cin >> a[i];
		if (s[i] > maxs) maxs = s[i];
		if (s[i] < mins) mins = s[i];
		s[i + 1] = s[i] + a[i];
		dmax[i][1] = s[i + 1] - mins;
		dmin[i][1] = - s[i + 1] + maxs;
		DBN(i, dmin[i][1], dmax[i][1]);
	}
	for (int st = 2; st < k; ++st){
		for (int i = st-1; i < n; ++i){
			if (i != 1) upd(dmax[i - 2][st - 1], dmax[i - 1][st - 1]);
			if (i != 1) upd(dmin[i - 2][st - 1], dmin[i - 1][st - 1]);
			int pmin = dmin[i - 1][st - 1];
			int pmax = dmax[i - 1][st - 1];
			int sum = 0;
			DBN(i, st, pmin, pmax);
			for (int j = i; j < n; ++j){
				sum += a[j];
				int& rmin = dmin[j][st];
				int& rmax = dmax[j][st];
				int res = max(pmin + sum, pmax - sum);
				if (rmin < res - sum){
					rmin = res - sum;
				}
				if (rmax < res + sum){
					rmax = res + sum;
				}
			}
		}
	}
	int ans = -INF;
	for (int i = 1; i < n; ++i){
		if (i != 1) upd(dmax[i - 2][k - 1], dmax[i - 1][k - 1]);
		if (i != 1) upd(dmin[i - 2][k - 1], dmin[i - 1][k - 1]);
		for (int j = i; j < n; ++j){
			int sum = s[j + 1] - s[i];
			ans = max(ans, dmin[i - 1][k - 1] + sum, dmax[i - 1][k - 1] - sum);
		}
	}
	cout << ans << endl;
	return 0;
}

// int a[100];
// int l[100];
// int r[100];
// bool used[100];
// int main()
// {
// 	if (false)
// 	{
// 		int n;
// 		cin >> n;
// 		FI(n){
// 			cin >> l[i] >> r[i];
// 			a[i];
// 		}
// 		do{
//
// 		} while (next_permutation(a, a + n));
//
//
// 	}
// 	return 0;
// }
//


#pragma comment(linker, "/stack:20000000")
#define _CRT_SECURE_NO_WARNINGS
# include "stdio.h"
#ifndef DEBUG
# include <iostream>
# include <cmath>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <deque>
# include <functional>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_map>
# else
# include <tr1/unordered_map>
using namespace std::tr1;
# endif
# else
# include <unordered_map>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,N,...) N

#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs

#define DBN1(a)			std::cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"

#define DEB(x) std::cerr<<#x<<"="<<(x)<<"  "

#ifdef DEBUG
#define DB(x) DEB(x)
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
#else
#define DB(x) ((void)42)
#define DBN(...) ((void)42)
#define DA(a,n) ((void)42)
#define DAR(a,n,s) ((void)42)
#endif

double __begin;
#define DTIME(ccc) __begin = clock(); ccc; std::cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<std::endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;

template<typename T1, typename T2, typename T3>
struct triple{ T1 a; T2 b; T3 c; triple(){}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
#define tri triple<int,int,int>
#define trl triple<ll,ll,ll>
template<typename T1, typename T2, typename T3>
bool operator<(const triple<T1, T2, T3> &t1, const triple<T1, T2, T3> &t2){ if (t1.a != t2.a) return t1.a < t2.a; else return t1.b < t2.b; }
template<typename T1, typename T2, typename T3>
inline std::ostream& operator << (std::ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }

#define FI(n) for(int i=0;i<n;i++)
#define FI1(n) for(int i=1;i<n;i++)
#define FJ(n) for(int j=0;j<n;j++)
#define FJ1(n) for(int j=1;j<n;j++)
#define FJI for(int j=0;j<i;j++)
#define FJI1 for(int j=1;j<i;j++)
#define FJIE for(int j=0;j<=i;j++)
#define FJIE1 for(int j=1;j<=i;j++)
#define For(i,a,b) for(int i=a;i<b;i++)

// Input macros
#define S(n)                        scanf("%d",&n)
#define S2(a,b)						scanf("%d%d",&a,&b)
#define Sc(n)                       scanf("%c",&n)
#define Sl(n)                       scanf("%I64d",&n)
#define Sl2(a,b)					scanf("%I64d%I63d",&a,&b)
#define Sf(n)                       scanf("%lf",&n)
#define Sf2(a,b)                    scanf("%lf%lf",&a,&b)

//Output macros
#define Pr(n)                       printf("%d ",n)
#define Prn(n)                      printf("%d\n",n)
#define Prc(n)                      printf("%c",n)
#define Prcn(n)                     printf("%c\n",n)
#define Prl(n)                      printf("%I64d ",n)
#define Prln(n)                     printf("%I64d\n",n)
#define Prf(n)                      printf("%lf ",n)
#define Prfn(n)                     printf("%lf\n",n)
#define Prs(n)                      printf("%s ",n)
#define Prsn(n)                     printf("%s\n",n)

//STL containers
#define foreach(v, c)               for( auto v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ((b).find(a) != (b).end())
#define in2(a,b)                    (find(all(b),a) != (b).end())
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define svi(vec,n) FI(n) S(vec[y])
#define svl(vec,n) FI(n) Sl(vec[y])
#define IT ::iterator

//int some_primes[10] = {100271, 500179, 1000003, 2000227, 5000321}
//fill char arrays
#define fill(a,v)                    memset(a, v, sizeof (a))

inline int bits_count(int v){ v = v - ((v >> 1) & 0x55555555); v = (v & 0x33333333) + ((v >> 2) & 0x33333333); return((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; }
inline int bits_count(ll v){ int t = v >> 32; int p = (v & ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
inline int sign(int x){ return (x >> 31) | (-x >> 31); }
inline bool ispow2(int x){ return (x != 0 && (x&(x - 1)) == 0); }
#define checkbit(n,b)        ( (n >> b) & 1)
#define INDEX(arr,ind)       (lower_bound(all(arr),ind)-arr.begin())
#define sor(a)  sort(all(a))
#define rsor(a) sort(all(a)); reverse(all(a));

//STL output ********************************
template<typename T1, typename T2>inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p){ return os << "(" << p.first << ", " << p.second << ")"; }
template<typename T>inline std::ostream &operator<<(std::ostream &os, const std::vector<T>& v){ bool first = true; os << "["; for (unsigned int i = 0; i < v.size(); i++){ if (!first)os << ", "; os << v[i]; first = false; }return os << "]"; }
template<typename T>inline std::ostream &operator<<(std::ostream &os, const std::set<T>&v){ bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii){ if (!first)os << ", "; os << *ii; first = false; }return os << "]"; }
template<typename T1, typename T2>inline std::ostream &operator << (std::ostream & os, const std::map<T1, T2>& v){ bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii){ if (!first)os << ", "; os << *ii; first = false; }return os << "]"; }
template<typename T, typename T2>void printarray(T  a[], T2 sz, T2 beg = 0){ for (T2 i = beg; i < sz; i++) cout << a[i] << " "; cout << endl; }

#define FREIN(FILE) freopen(FILE,"rt",stdin)
#define FREOUT(FILE) freopen(FILE,"wt",stdout)

#define sqr(x) ((x)*(x))
#define sqrt(x) sqrt(1.0*(x))
#define pow(x,n) pow(1.0*(x),n)

inline ll binpow(ll x, ll n){ ll res = 1; while (n){ if (n & 1)res *= x; x *= x; n >>= 1; }return res; }
inline ll powmod(ll x, ll n, ll _mod){ ll res = 1; while (n){ if (n & 1)res = (res*x) % _mod; x = (x*x) % _mod; n >>= 1; }return res; }
inline ll gcd(ll a, ll b){ ll t; while (b){ a = a%b; t = a; a = b; b = t; }return a; }
inline int gcd(int a, int b){ int t; while (b){ a = a%b; t = a; a = b; b = t; }return a; }
inline ll lcm(int a, int b){ return a / gcd(a, b)*(ll)b; }
inline ll lcm(ll a, ll b){ return a / gcd(a, b)*b; }
inline ll gcd(ll a, ll b, ll c){ return gcd(gcd(a, b), c); }
inline int gcd(int a, int b, int c){ return gcd(gcd(a, b), c); }
inline ll lcm(ll a, ll b, ll c){ return lcm(lcm(a, b), c); }
inline ll lcm(int a, int b, int c){ return lcm(lcm(a, b), (ll)c); }

inline ll max(ll a, ll b){ return (a > b) ? a : b; }
inline int max(int a, int b){ return (a > b) ? a : b; }
inline double max(double a, double b){ return (a > b) ? a : b; }
inline ll max(ll a, ll b, ll c){ return max(a, max(b, c)); }
inline int max(int a, int b, int c){ return max(a, max(b, c)); }
inline double max(double a, double b, double c){ return max(a, max(b, c)); }
inline ll min(ll a, ll b){ return (a < b) ? a : b; }
inline int min(int a, int b){ return (a < b) ? a : b; }
inline double min(double a, double b){ return (a < b) ? a : b; }
inline ll min(ll a, ll b, ll c){ return min(a, min(b, c)); }
inline int min(int a, int b, int c){ return min(a, min(b, c)); }
inline double min(double a, double b, double c){ return min(a, min(b, c)); }

template<class T>
inline void getar(T a, int n, int m){ for (int i = 0; i < n; i++) for (int j = 0; j < m; ++j) { scanf("%d", &a[i][j]); } }
inline void getar(int *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d", a + ii); } }
inline void getar(pii *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d%d", &a[ii].first, &a[ii].second); } }
inline void getar(ll *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%I64d", a + ii); } }
template<class T>
inline void cinarr(T a, int n){ for (int i = 0; i < n; ++i) cin >> a[i]; }
// Useful constants

#define INF			1011111111
#define EPS			(double)1e-8
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif

int dmin[1000][400];
int dmax[1000][400];
int a[1000];
int s[1000];
void upd(int x, int &y){
	if (y < x) y = x;
}
int main(){
	int n, k;
	cin >> n >> k;
	FI(400)
		FJ(100){
		dmax[i][j] = -INF;
		dmin[i][j] = -INF;
	}
	int maxs = 0;
	int mins = 0;
	for (int i = 0; i < n; ++i){
		cin >> a[i];
		if (s[i] > maxs) maxs = s[i];
		if (s[i] < mins) mins = s[i];
		s[i + 1] = s[i] + a[i];
		dmax[i][1] = s[i + 1] - mins;
		dmin[i][1] = -s[i + 1] + maxs;
	}
	for (int st = 2; st < k; ++st){
		for (int i = st - 1; i < n; ++i){
			if (i != 1) upd(dmax[i - 2][st - 1], dmax[i - 1][st - 1]);
			if (i != 1) upd(dmin[i - 2][st - 1], dmin[i - 1][st - 1]);
			if (st != 2) upd(dmin[i - 2][st - 2], dmax[i - 1][st - 1]);
			if (st != 2) upd(dmin[i - 2][st - 2], dmin[i - 1][st - 1]);
			int pmin = dmin[i - 1][st - 1];
			int pmax = dmax[i - 1][st - 1];
			int sum = 0;
			for (int j = i; j < n; ++j){
				sum += a[j];
				int& rmin = dmin[j][st];
				int& rmax = dmax[j][st];
				int res = max(pmin + sum, pmax - sum);
				if (rmin < res - sum){
					rmin = res - sum;
				}
				if (rmax < res + sum){
					rmax = res + sum;
				}
			}
		}
	}
	int ans = -INF;
	for (int i = 1; i < n; ++i){
		if (i != 1) upd(dmax[i - 2][k - 1], dmax[i - 1][k - 1]);
		if (i != 1) upd(dmin[i - 2][k - 1], dmin[i - 1][k - 1]);
		for (int j = i; j < n; ++j){
			int sum = s[j + 1] - s[i];
			ans = max(ans, dmin[i - 1][k - 1] + sum, dmax[i - 1][k - 1] - sum);
		}
	}
	cout << ans << endl;
	return 0;
}


#pragma comment(linker, "/stack:20000000")
#define _CRT_SECURE_NO_WARNINGS
# include "stdio.h"
#ifndef DEBUG
# include <iostream>
# include <cmath>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <deque>
# include <functional>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_map>
# else
# include <tr1/unordered_map>
using namespace std::tr1;
# endif
# else
# include <unordered_map>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,N,...) N

#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs

#define DBN1(a)			std::cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"

#define DEB(x) std::cerr<<#x<<"="<<(x)<<"  "

#ifdef DEBUG
#define DB(x) DEB(x)
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
#else
#define DB(x) ((void)42)
#define DBN(...) ((void)42)
#define DA(a,n) ((void)42)
#define DAR(a,n,s) ((void)42)
#endif

double __begin;
#define DTIME(ccc) __begin = clock(); ccc; std::cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<std::endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;

template<typename T1,typename T2,typename T3>
struct triple{T1 a;T2 b;T3 c;triple(){};triple(T1 _a,T2 _b, T3 _c):a(_a),b(_b),c(_c){}};
#define tri triple<int,int,int>
#define trl triple<ll,ll,ll>
template<typename T1,typename T2,typename T3>
bool operator<(const triple<T1,T2,T3> &t1,const triple<T1,T2,T3> &t2){if (t1.a!=t2.a) return t1.a<t2.a; else return t1.b<t2.b;}
template<typename T1, typename T2, typename T3>
inline std::ostream& operator << (std::ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }

#define FI(n) for(int i=0;i<n;i++)
#define FI1(n) for(int i=1;i<n;i++)
#define FJ(n) for(int j=0;j<n;j++)
#define FJ1(n) for(int j=1;j<n;j++)
#define FJI for(int j=0;j<i;j++)
#define FJI1 for(int j=1;j<i;j++)
#define FJIE for(int j=0;j<=i;j++)
#define FJIE1 for(int j=1;j<=i;j++)
#define For(i,a,b) for(int i=a;i<b;i++)

// Input macros
#define S(n)                        scanf("%d",&n)
#define S2(a,b)						scanf("%d%d",&a,&b)
#define Sc(n)                       scanf("%c",&n)
#define Sl(n)                       scanf("%I64d",&n)
#define Sl2(a,b)					scanf("%I64d%I63d",&a,&b)
#define Sf(n)                       scanf("%lf",&n)
#define Sf2(a,b)                    scanf("%lf%lf",&a,&b)

//Output macros
#define Pr(n)                       printf("%d ",n)
#define Prn(n)                      printf("%d\n",n)
#define Prc(n)                      printf("%c",n)
#define Prcn(n)                     printf("%c\n",n)
#define Prl(n)                      printf("%I64d ",n)
#define Prln(n)                     printf("%I64d\n",n)
#define Prf(n)                      printf("%lf ",n)
#define Prfn(n)                     printf("%lf\n",n)
#define Prs(n)                      printf("%s ",n)
#define Prsn(n)                     printf("%s\n",n)

//STL containers
#define foreach(v, c)               for( auto v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ((b).find(a) != (b).end())
#define in2(a,b)                    (find(all(b),a) != (b).end())
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define svi(vec,n) FI(n) S(vec[y])
#define svl(vec,n) FI(n) Sl(vec[y])
#define IT ::iterator

//int some_primes[10] = {100271, 500179, 1000003, 2000227, 5000321}
//fill char arrays
#define fill(a,v)                    memset(a, v, sizeof (a))

inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){ int t = v >> 32; int p = (v & ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));return((x >> 16) | (x << 16));}
inline int sign(int x){return (x >> 31) | (-x >> 31);}
inline bool ispow2(int x){return (x!=0 && (x&(x-1))==0);}
#define checkbit(n,b)        ( (n >> b) & 1)
#define INDEX(arr,ind)       (lower_bound(all(arr),ind)-arr.begin())
#define sor(a)  sort(all(a))
#define rsor(a) sort(all(a)); reverse(all(a));

//STL output ********************************
template<typename T1,typename T2>inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p){return os << "(" << p.first << ", " << p.second << ")";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::vector<T>& v){bool first=true;os<<"[";for(unsigned int i=0;i<v.size();i++){if(!first)os<<", ";os<<v[i];first=false;}return os<<"]";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::set<T>&v){bool first=true;os << "[";for(typename std::set<T>::const_iterator ii=v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T1, typename T2>inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v){bool first = true; os << "[";for (typename std::map<T1,T2>::const_iterator ii =v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T,typename T2>void printarray(T  a[],T2 sz,T2 beg=0){for(T2 i=beg;i<sz;i++) cout<<a[i]<<" ";cout<<endl;}

#define FREIN(FILE) freopen(FILE,"rt",stdin)
#define FREOUT(FILE) freopen(FILE,"wt",stdout)

#define sqr(x) ((x)*(x))
#define sqrt(x) sqrt(1.0*(x))
#define pow(x,n) pow(1.0*(x),n)

inline ll binpow(ll x,ll n){ll res=1;while(n){if(n&1)res*=x;x*=x;n>>=1;}return res;}
inline ll powmod(ll x,ll n, ll _mod){ll res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
inline ll gcd(ll a,ll b){ll t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline int gcd(int a,int b){int t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline ll lcm(int a,int b){return a/gcd(a,b)*(ll)b;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll gcd(ll a,ll b,ll c){return gcd(gcd(a,b),c);}
inline int gcd(int a,int b,int c){return gcd(gcd(a,b),c);}
inline ll lcm(ll a,ll b,ll c){return lcm(lcm(a,b),c);}
inline ll lcm(int a,int b,int c){return lcm(lcm(a,b),(ll)c);}

inline ll max(ll a,ll b){return (a>b)?a:b;}
inline int max(int a,int b){return (a>b)?a:b;}
inline double max(double a,double b){return (a>b)?a:b;}
inline ll max(ll a,ll b,ll c){return max(a,max(b,c));}
inline int max(int a,int b,int c){return max(a,max(b,c));}
inline double max(double a,double b,double c){return max(a,max(b,c));}
inline ll min(ll a,ll b){return (a<b)?a:b;}
inline int min(int a,int b){return (a<b)?a:b;}
inline double min(double a,double b){return (a<b)?a:b;}
inline ll min(ll a,ll b,ll c){return min(a,min(b,c));}
inline int min(int a,int b,int c){return min(a,min(b,c));}
inline double min(double a,double b,double c){return min(a,min(b,c));}

template<class T>
inline void getar(T a, int n, int m){ for (int i = 0; i < n; i++) for (int j = 0; j < m;++j) { scanf("%d", &a[i][j]); } }
inline void getar(int *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d", a + ii); } }
inline void getar(pii *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d%d", &a[ii].first, &a[ii].second); } }
inline void getar(ll *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%I64d", a + ii); } }
template<class T>
inline void cinarr(T a, int n){ for (int i=0;i<n;++i) cin >> a[i];}
// Useful constants

#define INF			1011111111
#define EPS			(double)1e-8
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif

pii dmin[1000][400];
pii dmax[1000][400];
int a[1000];
int s[1000];
void upd1(pii p1, pii &p2){
	if (p2.first + p2.second < p1.first + p1.second){
		p2 = p1;
	}
}
void upd2(pii p1, pii &p2){
	if (p2.first - p2.second < p1.first - p1.second){
		p2 = p1;
	}
}
int main(){
	int n, k;
	cin >> n >> k;
	FI(400)
		FJ(100){
		dmax[i][j] = { -INF, -INF };
		dmin[i][j] = { -INF, +INF };
	}
	int maxs = 0;
	int mins = 0;
	for (int i = 0; i < n; ++i){
		cin >> a[i];
		if (s[i] > maxs) maxs = s[i];
		if (s[i] < mins) mins = s[i];
		s[i + 1] = s[i] + a[i];
		dmax[i][1] = { 0, s[i + 1] - mins };
		dmin[i][1] = { 0, s[i + 1] - maxs };
	}
	for (int st = 2; st < k; ++st){
		for (int i = 1; i < n; ++i){
			if (i != 1) upd1(dmax[i - 2][st - 1], dmax[i - 1][st - 1]);
			if (i != 1) upd2(dmin[i - 2][st - 1], dmin[i - 1][st - 1]);
			pii pmin = dmin[i - 1][st - 1];
			pii pmax = dmax[i - 1][st - 1];
			int sum = 0;
			//DBN(i, st, pmin, pmax);
			for (int j = i; j < n; ++j){
				sum += a[j];
				pii& rmin = dmin[j][st];
				pii& rmax = dmax[j][st];
				int res = pmin.first + max(sum - pmin.second, pmax.second - sum);
				if (rmin.first - rmin.second < res - sum){
					rmin = { res, sum };
				}
				if (rmax.first + rmax.second < res + sum){
					rmax = { res, sum };
				}
// 				if (rmin.first < res || rmin.first == res && rmin.second > sum){
// 					rmin = { res, sum };
// 				}
// 				if (rmax.first < res || rmax.first == res && rmax.second < sum){
// 					rmax = { res, sum };
// 				}
			}
		}
	}
	int ans = -INF;
	int sum = s[n];
	for (int i = 1; i < n; ++i){
		for (int j = i; j < n; ++j){
			sum = s[j + 1] - s[i];
			ans = max(ans, dmin[i - 1][k - 1].first + sum - dmin[i - 1][k - 1].second, dmax[i - 1][k - 1].first - sum + dmax[i - 1][k - 1].second);
		}
	}
	cout << ans << endl;
	return 0;
}

// int a[100];
// int l[100];
// int r[100];
// bool used[100];
// int main()
// {
// 	if (false)
// 	{
// 		int n;
// 		cin >> n;
// 		FI(n){
// 			cin >> l[i] >> r[i];
// 			a[i];
// 		}
// 		do{
//
// 		} while (next_permutation(a, a + n));
//
//
// 	}
// 	return 0;
// }
//


#pragma comment(linker, "/stack:20000000")
#define _CRT_SECURE_NO_WARNINGS
# include "stdio.h"
#ifndef DEBUG
# include <iostream>
# include <cmath>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <deque>
# include <functional>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_map>
# else
# include <tr1/unordered_map>
using namespace std::tr1;
# endif
# else
# include <unordered_map>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,N,...) N

#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs

#define DBN1(a)			std::cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"

#define DEB(x) std::cerr<<#x<<"="<<(x)<<"  "

#ifdef DEBUG
#define DB(x) DEB(x)
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
#else
#define DB(x) ((void)42)
#define DBN(...) ((void)42)
#define DA(a,n) ((void)42)
#define DAR(a,n,s) ((void)42)
#endif

double __begin;
#define DTIME(ccc) __begin = clock(); ccc; std::cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<std::endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;

template<typename T1,typename T2,typename T3>
struct triple{T1 a;T2 b;T3 c;triple(){};triple(T1 _a,T2 _b, T3 _c):a(_a),b(_b),c(_c){}};
#define tri triple<int,int,int>
#define trl triple<ll,ll,ll>
template<typename T1,typename T2,typename T3>
bool operator<(const triple<T1,T2,T3> &t1,const triple<T1,T2,T3> &t2){if (t1.a!=t2.a) return t1.a<t2.a; else return t1.b<t2.b;}
template<typename T1, typename T2, typename T3>
inline std::ostream& operator << (std::ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }

#define FI(n) for(int i=0;i<n;i++)
#define FI1(n) for(int i=1;i<n;i++)
#define FJ(n) for(int j=0;j<n;j++)
#define FJ1(n) for(int j=1;j<n;j++)
#define FJI for(int j=0;j<i;j++)
#define FJI1 for(int j=1;j<i;j++)
#define FJIE for(int j=0;j<=i;j++)
#define FJIE1 for(int j=1;j<=i;j++)
#define For(i,a,b) for(int i=a;i<b;i++)

// Input macros
#define S(n)                        scanf("%d",&n)
#define S2(a,b)						scanf("%d%d",&a,&b)
#define Sc(n)                       scanf("%c",&n)
#define Sl(n)                       scanf("%I64d",&n)
#define Sl2(a,b)					scanf("%I64d%I63d",&a,&b)
#define Sf(n)                       scanf("%lf",&n)
#define Sf2(a,b)                    scanf("%lf%lf",&a,&b)

//Output macros
#define Pr(n)                       printf("%d ",n)
#define Prn(n)                      printf("%d\n",n)
#define Prc(n)                      printf("%c",n)
#define Prcn(n)                     printf("%c\n",n)
#define Prl(n)                      printf("%I64d ",n)
#define Prln(n)                     printf("%I64d\n",n)
#define Prf(n)                      printf("%lf ",n)
#define Prfn(n)                     printf("%lf\n",n)
#define Prs(n)                      printf("%s ",n)
#define Prsn(n)                     printf("%s\n",n)

//STL containers
#define foreach(v, c)               for( auto v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ((b).find(a) != (b).end())
#define in2(a,b)                    (find(all(b),a) != (b).end())
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define svi(vec,n) FI(n) S(vec[y])
#define svl(vec,n) FI(n) Sl(vec[y])
#define IT ::iterator

//int some_primes[10] = {100271, 500179, 1000003, 2000227, 5000321}
//fill char arrays
#define fill(a,v)                    memset(a, v, sizeof (a))

inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){ int t = v >> 32; int p = (v & ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));return((x >> 16) | (x << 16));}
inline int sign(int x){return (x >> 31) | (-x >> 31);}
inline bool ispow2(int x){return (x!=0 && (x&(x-1))==0);}
#define checkbit(n,b)        ( (n >> b) & 1)
#define INDEX(arr,ind)       (lower_bound(all(arr),ind)-arr.begin())
#define sor(a)  sort(all(a))
#define rsor(a) sort(all(a)); reverse(all(a));

//STL output ********************************
template<typename T1,typename T2>inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p){return os << "(" << p.first << ", " << p.second << ")";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::vector<T>& v){bool first=true;os<<"[";for(unsigned int i=0;i<v.size();i++){if(!first)os<<", ";os<<v[i];first=false;}return os<<"]";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::set<T>&v){bool first=true;os << "[";for(typename std::set<T>::const_iterator ii=v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T1, typename T2>inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v){bool first = true; os << "[";for (typename std::map<T1,T2>::const_iterator ii =v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T,typename T2>void printarray(T  a[],T2 sz,T2 beg=0){for(T2 i=beg;i<sz;i++) cout<<a[i]<<" ";cout<<endl;}

#define FREIN(FILE) freopen(FILE,"rt",stdin)
#define FREOUT(FILE) freopen(FILE,"wt",stdout)

#define sqr(x) ((x)*(x))
#define sqrt(x) sqrt(1.0*(x))
#define pow(x,n) pow(1.0*(x),n)

inline ll binpow(ll x,ll n){ll res=1;while(n){if(n&1)res*=x;x*=x;n>>=1;}return res;}
inline ll powmod(ll x,ll n, ll _mod){ll res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
inline ll gcd(ll a,ll b){ll t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline int gcd(int a,int b){int t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline ll lcm(int a,int b){return a/gcd(a,b)*(ll)b;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll gcd(ll a,ll b,ll c){return gcd(gcd(a,b),c);}
inline int gcd(int a,int b,int c){return gcd(gcd(a,b),c);}
inline ll lcm(ll a,ll b,ll c){return lcm(lcm(a,b),c);}
inline ll lcm(int a,int b,int c){return lcm(lcm(a,b),(ll)c);}

inline ll max(ll a,ll b){return (a>b)?a:b;}
inline int max(int a,int b){return (a>b)?a:b;}
inline double max(double a,double b){return (a>b)?a:b;}
inline ll max(ll a,ll b,ll c){return max(a,max(b,c));}
inline int max(int a,int b,int c){return max(a,max(b,c));}
inline double max(double a,double b,double c){return max(a,max(b,c));}
inline ll min(ll a,ll b){return (a<b)?a:b;}
inline int min(int a,int b){return (a<b)?a:b;}
inline double min(double a,double b){return (a<b)?a:b;}
inline ll min(ll a,ll b,ll c){return min(a,min(b,c));}
inline int min(int a,int b,int c){return min(a,min(b,c));}
inline double min(double a,double b,double c){return min(a,min(b,c));}

template<class T>
inline void getar(T a, int n, int m){ for (int i = 0; i < n; i++) for (int j = 0; j < m;++j) { scanf("%d", &a[i][j]); } }
inline void getar(int *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d", a + ii); } }
inline void getar(pii *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d%d", &a[ii].first, &a[ii].second); } }
inline void getar(ll *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%I64d", a + ii); } }
template<class T>
inline void cinarr(T a, int n){ for (int i=0;i<n;++i) cin >> a[i];}
// Useful constants

#define INF			1011111111
#define EPS			(double)1e-8
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif

pii dmin[1000][400];
pii dmax[1000][400];
int a[1000];
int s[1000];
void upd1(pii p1, pii &p2){
	if (p2.first + p2.second < p1.first + p1.second){
		p2 = p1;
	}
}
void upd2(pii p1, pii &p2){
	if (p2.first - p2.second < p1.first - p1.second){
		p2 = p1;
	}
}
int main(){
	int n, k;
	cin >> n >> k;
	FI(400)
		FJ(100){
		dmin[i][j] = dmax[i][j] = { -INF, -INF };
	}
	int maxs = 0;
	for (int i = 0; i < n; ++i){
		cin >> a[i];
		if (s[i] > maxs) maxs = s[i];
		s[i + 1] = s[i] + a[i];
		dmin[i][1] = dmax[i][1] = { 0, s[i + 1] };
		dmin[i][1].second = s[i + 1] - maxs;
	}
	for (int st = 2; st < k; ++st){
		for (int i = 1; i < n; ++i){
			upd1(dmax[i - 2][st - 1], dmax[i - 1][st - 1]);
			upd2(dmin[i - 2][st - 1], dmin[i - 1][st - 1]);
			pii pmin = dmin[i - 1][st - 1];
			pii pmax = dmax[i - 1][st - 1];
			int sum = 0;
			//DBN(i, st, pmin, pmax);
			for (int j = i; j < n; ++j){
				sum += a[j];
				pii& rmin = dmin[j][st];
				pii& rmax = dmax[j][st];
				int res = pmin.first + max(sum - pmin.second, pmax.second - sum);
				if (rmin.first - rmin.second < res - sum){
					rmin = { res, sum };
				}
				if (rmax.first + rmax.second < res + sum){
					rmax = { res, sum };
				}
// 				if (rmin.first < res || rmin.first == res && rmin.second > sum){
// 					rmin = { res, sum };
// 				}
// 				if (rmax.first < res || rmax.first == res && rmax.second < sum){
// 					rmax = { res, sum };
// 				}
			}
		}
	}
	int ans = -INF;
	int sum = s[n];
	for (int i = 1; i < n; ++i){
		for (int j = i; j < n; ++j){
			sum = s[j + 1] - s[i];
			ans = max(ans, dmin[i - 1][k - 1].first + sum - dmin[i - 1][k - 1].second, dmax[i - 1][k - 1].first - sum + dmax[i - 1][k - 1].second);
		}
	}
	cout << ans << endl;
	return 0;
}

// int a[100];
// int l[100];
// int r[100];
// bool used[100];
// int main()
// {
// 	if (false)
// 	{
// 		int n;
// 		cin >> n;
// 		FI(n){
// 			cin >> l[i] >> r[i];
// 			a[i];
// 		}
// 		do{
//
// 		} while (next_permutation(a, a + n));
//
//
// 	}
// 	return 0;
// }
//


#pragma comment(linker, "/stack:20000000")
#define _CRT_SECURE_NO_WARNINGS
# include "stdio.h"
#ifndef DEBUG
# include <iostream>
# include <cmath>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <deque>
# include <functional>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_map>
# else
# include <tr1/unordered_map>
using namespace std::tr1;
# endif
# else
# include <unordered_map>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,N,...) N

#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs

#define DBN1(a)			std::cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)		std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)	std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"

#define DEB(x) std::cerr<<#x<<"="<<(x)<<"  "

#ifdef DEBUG
#define DB(x) DEB(x)
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
#else
#define DB(x) ((void)42)
#define DBN(...) ((void)42)
#define DA(a,n) ((void)42)
#define DAR(a,n,s) ((void)42)
#endif

double __begin;
#define DTIME(ccc) __begin = clock(); ccc; std::cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<std::endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;

template<typename T1,typename T2,typename T3>
struct triple{T1 a;T2 b;T3 c;triple(){};triple(T1 _a,T2 _b, T3 _c):a(_a),b(_b),c(_c){}};
#define tri triple<int,int,int>
#define trl triple<ll,ll,ll>
template<typename T1,typename T2,typename T3>
bool operator<(const triple<T1,T2,T3> &t1,const triple<T1,T2,T3> &t2){if (t1.a!=t2.a) return t1.a<t2.a; else return t1.b<t2.b;}
template<typename T1, typename T2, typename T3>
inline std::ostream& operator << (std::ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }

#define FI(n) for(int i=0;i<n;i++)
#define FI1(n) for(int i=1;i<n;i++)
#define FJ(n) for(int j=0;j<n;j++)
#define FJ1(n) for(int j=1;j<n;j++)
#define FJI for(int j=0;j<i;j++)
#define FJI1 for(int j=1;j<i;j++)
#define FJIE for(int j=0;j<=i;j++)
#define FJIE1 for(int j=1;j<=i;j++)
#define For(i,a,b) for(int i=a;i<b;i++)

// Input macros
#define S(n)                        scanf("%d",&n)
#define S2(a,b)						scanf("%d%d",&a,&b)
#define Sc(n)                       scanf("%c",&n)
#define Sl(n)                       scanf("%I64d",&n)
#define Sl2(a,b)					scanf("%I64d%I63d",&a,&b)
#define Sf(n)                       scanf("%lf",&n)
#define Sf2(a,b)                    scanf("%lf%lf",&a,&b)

//Output macros
#define Pr(n)                       printf("%d ",n)
#define Prn(n)                      printf("%d\n",n)
#define Prc(n)                      printf("%c",n)
#define Prcn(n)                     printf("%c\n",n)
#define Prl(n)                      printf("%I64d ",n)
#define Prln(n)                     printf("%I64d\n",n)
#define Prf(n)                      printf("%lf ",n)
#define Prfn(n)                     printf("%lf\n",n)
#define Prs(n)                      printf("%s ",n)
#define Prsn(n)                     printf("%s\n",n)

//STL containers
#define foreach(v, c)               for( auto v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ((b).find(a) != (b).end())
#define in2(a,b)                    (find(all(b),a) != (b).end())
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define svi(vec,n) FI(n) S(vec[y])
#define svl(vec,n) FI(n) Sl(vec[y])
#define IT ::iterator

//int some_primes[10] = {100271, 500179, 1000003, 2000227, 5000321}
//fill char arrays
#define fill(a,v)                    memset(a, v, sizeof (a))

inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){ int t = v >> 32; int p = (v & ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));return((x >> 16) | (x << 16));}
inline int sign(int x){return (x >> 31) | (-x >> 31);}
inline bool ispow2(int x){return (x!=0 && (x&(x-1))==0);}
#define checkbit(n,b)        ( (n >> b) & 1)
#define INDEX(arr,ind)       (lower_bound(all(arr),ind)-arr.begin())
#define sor(a)  sort(all(a))
#define rsor(a) sort(all(a)); reverse(all(a));

//STL output ********************************
template<typename T1,typename T2>inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p){return os << "(" << p.first << ", " << p.second << ")";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::vector<T>& v){bool first=true;os<<"[";for(unsigned int i=0;i<v.size();i++){if(!first)os<<", ";os<<v[i];first=false;}return os<<"]";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::set<T>&v){bool first=true;os << "[";for(typename std::set<T>::const_iterator ii=v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T1, typename T2>inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v){bool first = true; os << "[";for (typename std::map<T1,T2>::const_iterator ii =v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T,typename T2>void printarray(T  a[],T2 sz,T2 beg=0){for(T2 i=beg;i<sz;i++) cout<<a[i]<<" ";cout<<endl;}

#define FREIN(FILE) freopen(FILE,"rt",stdin)
#define FREOUT(FILE) freopen(FILE,"wt",stdout)

#define sqr(x) ((x)*(x))
#define sqrt(x) sqrt(1.0*(x))
#define pow(x,n) pow(1.0*(x),n)

inline ll binpow(ll x,ll n){ll res=1;while(n){if(n&1)res*=x;x*=x;n>>=1;}return res;}
inline ll powmod(ll x,ll n, ll _mod){ll res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
inline ll gcd(ll a,ll b){ll t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline int gcd(int a,int b){int t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline ll lcm(int a,int b){return a/gcd(a,b)*(ll)b;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll gcd(ll a,ll b,ll c){return gcd(gcd(a,b),c);}
inline int gcd(int a,int b,int c){return gcd(gcd(a,b),c);}
inline ll lcm(ll a,ll b,ll c){return lcm(lcm(a,b),c);}
inline ll lcm(int a,int b,int c){return lcm(lcm(a,b),(ll)c);}

inline ll max(ll a,ll b){return (a>b)?a:b;}
inline int max(int a,int b){return (a>b)?a:b;}
inline double max(double a,double b){return (a>b)?a:b;}
inline ll max(ll a,ll b,ll c){return max(a,max(b,c));}
inline int max(int a,int b,int c){return max(a,max(b,c));}
inline double max(double a,double b,double c){return max(a,max(b,c));}
inline ll min(ll a,ll b){return (a<b)?a:b;}
inline int min(int a,int b){return (a<b)?a:b;}
inline double min(double a,double b){return (a<b)?a:b;}
inline ll min(ll a,ll b,ll c){return min(a,min(b,c));}
inline int min(int a,int b,int c){return min(a,min(b,c));}
inline double min(double a,double b,double c){return min(a,min(b,c));}

template<class T>
inline void getar(T a, int n, int m){ for (int i = 0; i < n; i++) for (int j = 0; j < m;++j) { scanf("%d", &a[i][j]); } }
inline void getar(int *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d", a + ii); } }
inline void getar(pii *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d%d", &a[ii].first, &a[ii].second); } }
inline void getar(ll *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%I64d", a + ii); } }
template<class T>
inline void cinarr(T a, int n){ for (int i=0;i<n;++i) cin >> a[i];}
// Useful constants

#define INF			1011111111
#define EPS			(double)1e-8
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif

int dmin[1000][400];
int dmax[1000][400];
int a[1000];
int s[1000];
void upd(int x, int &y){
	if (y < x) y = x;
}
int main(){
	int n, k;
	cin >> n >> k;
	FI(400)
		FJ(100){
		dmax[i][j] = -INF;
		dmin[i][j] = -INF;
	}
	int maxs = 0;
	int mins = 0;
	for (int i = 0; i < n; ++i){
		cin >> a[i];
		if (s[i] > maxs) maxs = s[i];
		if (s[i] < mins) mins = s[i];
		s[i + 1] = s[i] + a[i];
		dmax[i][1] = s[i + 1] - mins;
		dmin[i][1] = s[i + 1] - maxs;
	}
	for (int st = 2; st < k; ++st){
		for (int i = 1; i < n; ++i){
			if (i != 1) upd(dmax[i - 2][st - 1], dmax[i - 1][st - 1]);
			if (i != 1) upd(dmin[i - 2][st - 1], dmin[i - 1][st - 1]);
			int pmin = dmin[i - 1][st - 1];
			int pmax = dmax[i - 1][st - 1];
			int sum = 0;
			//DBN(i, st, pmin, pmax);
			for (int j = i; j < n; ++j){
				sum += a[j];
				int& rmin = dmin[j][st];
				int& rmax = dmax[j][st];
				int res = max(pmin + sum, pmax - sum);
				if (rmin < pmax - 2*sum){
					rmin = pmax - 2*sum;
				}
				if (rmax < pmin + 2*sum){
					rmax = pmin + 2*sum;
				}
			}
		}
	}
	int ans = -INF;
	for (int i = 1; i < n; ++i){
		if (i != 1) upd(dmax[i - 2][k - 1], dmax[i - 1][k - 1]);
		if (i != 1) upd(dmin[i - 2][k - 1], dmin[i - 1][k - 1]);
		for (int j = i; j < n; ++j){
			int sum = s[j + 1] - s[i];
			ans = max(ans, dmin[i - 1][k - 1] + sum, dmax[i - 1][k - 1] - sum);
		}
	}
	cout << ans << endl;
	return 0;
}

// int a[100];
// int l[100];
// int r[100];
// bool used[100];
// int main()
// {
// 	if (false)
// 	{
// 		int n;
// 		cin >> n;
// 		FI(n){
// 			cin >> l[i] >> r[i];
// 			a[i];
// 		}
// 		do{
//
// 		} while (next_permutation(a, a + n));
//
//
// 	}
// 	return 0;
// }
//


// @author cebrusfs
// headers {{{
#include<bits/stdc++.h>
using namespace std;
// }}}
// macros {{{
#ifdef WIN32
    #define LLD "%I64d"
#else
    #define LLD "%lld"
#endif

#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

#define MS0(x) memset(x,0,sizeof(x))
#define MS1(x) memset(x,-1,sizeof(x))

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

#define IOS ios_base::sync_with_stdio(false)

#define REP(i,n) for(int i=0;i<(n);i++)
#define REP1(i,a,b) for(int i=(a);i<=(b);i++)

#define PER(i,n) for(int i=(n)-1;i>=0;i--)
#define PER1(i,a,b) for(int i=(a);i>=(b);i--)

#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

typedef pair<int, int> PII;
#define MP make_pair
#define F first
#define S second

typedef vector<int> VI;
#define PB push_back
#define PF push_front

#define PPB pop_back
#define PPF pop_front

// C++11
#if __cplusplus >= 201103L
#define MT make_tuple
typedef tuple<int, int> TII;
#endif

#define runtime() ((double)clock() / CLOCKS_PER_SEC)

const double eps = 1e-7;
// }}}

#define MAX 30005
#define VMAX 205
#define INF (1e15)

int ary[MAX];
long long sum[MAX];

long long cal(int l, int r) // [l, r]
{
    return sum[r] - sum[l - 1];
}

long long dp[2][MAX][VMAX], ma[2][MAX][VMAX];
long long dp2[MAX][VMAX];

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

    for (int i = 1; i <= n; ++i)
        scanf("%d", &ary[i]);

    for (int i = 1; i <= n; ++i)
        sum[i] = ary[i] + sum[i - 1];

    for (int k = 0; k < m; ++k)
    {
        ma[0][0][k] = -INF;
        ma[1][0][k] = -INF;

        for (int j = 0; j < 2; ++j)
            for (int i = 1; i <= n; ++i)
                ma[j][i][k] = max(dp[j][i][k], ma[j][i - 1][k]);

        //for (int i = 1; i <= n; ++i)
        //    printf("i=%d: %lld %lld\n", i, ma[0][i][k], ma[1][i][k]);

        for (int i = 1; i <= n; ++i)
        {
            long long& pos = dp[0][i][k + 1];
            long long& neg = dp[1][i][k + 1];
            dp2[i][k] = -INF;
            pos = -INF;
            neg = -INF;

            for (int j = i; j > 0; --j)
            {
                // ... [j, i]
                long long s = cal(j, i);

                long long ans =
                    k == 0 ?
                    0 :
                    max(-s + ma[0][j - 1][k], s + ma[1][j - 1][k]);

                dp2[i][k] = max(dp2[i][k], ans);
                pos = max(ans + s, pos);
                neg = max(ans - s, neg);

                //printf("k=%d i=%d j=%d: %lld %lld, ans=%lld\n", k, i, j, pos, neg, ans);
            }
        }
    }
    long long ans = 0;
    for (int i = 1; i <= n; ++i)
        ans = max(ans, dp2[i][m - 1]);

    printf("%lld\n", ans);
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>
#include <unordered_map>
#include <ctime>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left
#define right _right

const ld pi = 3.14159265359;

template < typename T > T abs(T x)
{
    return x > 0 ? x : -x;
}

template < typename T > T sqr(T x)
{
	return x * x;
}

const int maxn = 400;
const int maxk = 200;

ll s[maxn];
ll p[maxn];
ll dp[maxn][maxk][2];

int main()
{
//	srand(time(NULL));
//	gen();
	//freopen("a.in", "r", stdin);

	int n, k;
	cin >> n >> k;

	for (int i = 1; i <= n; i++)
		cin >> p[i];

	const ll inf = 1e17;

	for (int i = 0; i < maxn; i++)
		for (int j = 0; j < maxk; j++)
			dp[i][j][0] = dp[i][j][1] = -inf;

	dp[0][0][0] = 0;
	dp[0][0][1] = 0;

	for (int i = 1; i <= n; i++)
		s[i] = s[i - 1] + p[i];

	ll ans = 0;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= k; j++)
		{

			dp[i][j][0] = dp[i - 1][j][0];
			dp[i][j][1] = dp[i - 1][j][1];

			if (j > 0)
			{
				for (int h = i - 1; h >= 0; h--)
				{

					if (j == k)
					{
						ans = max(ans, dp[h][j - 1][0]  + s[i]);
						ans = max(ans, dp[h][j - 1][1]  - s[i]);
					}
					dp[i][j][0] = max(dp[i][j][0], dp[h][j - 1][0]  + (s[i] - s[h]) - ((s[i] - s[h])));
					dp[i][j][1] = max(dp[i][j][1], dp[h][j - 1][0]  + (s[i] - s[h]) + ((s[i] - s[h])));
					dp[i][j][0] = max(dp[i][j][0], dp[h][j - 1][1]  - (s[i] - s[h]) - ((s[i] - s[h])));
					dp[i][j][1] = max(dp[i][j][1], dp[h][j - 1][1]  - (s[i] - s[h]) + ((s[i] - s[h])));
				}
			}

		}
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>
#include <unordered_map>
#include <ctime>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left
#define right _right

const ld pi = 3.14159265359;

template < typename T > T abs(T x)
{
    return x > 0 ? x : -x;
}

template < typename T > T sqr(T x)
{
	return x * x;
}

const int maxn = 400;
const int maxk = 200;

ll s[maxn];
ll p[maxn];
ll dp[maxn][maxk][2];

int main()
{
//	srand(time(NULL));
//	gen();
	//freopen("a.in", "r", stdin);

	int n, k;
	cin >> n >> k;

	for (int i = 1; i <= n; i++)
		cin >> p[i];

	const ll inf = 1e17;

	for (int i = 0; i < maxn; i++)
		for (int j = 0; j < maxk; j++)
			dp[i][j][0] = dp[i][j][1] = -inf;

	dp[0][0][0] = 0;
	dp[0][0][1] = 0;

	for (int i = 1; i <= n; i++)
		s[i] = s[i - 1] + p[i];

	ll ans = 0;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= k; j++)
		{

			dp[i][j][0] = dp[i - 1][j][0];
			dp[i][j][1] = dp[i - 1][j][1];

			if (j > 0)
			{
				for (int h = i - 1; h >= 0; h--)
				{
					if (j == k)
					{
						ans = max(ans, dp[h][j - 1][0]  +  (s[i] - s[h]));
						ans = max(ans, dp[h][j - 1][1]  -  (s[i] - s[h]));
					}

					ll add = (s[i] - s[h]);
					if (j == 1)
						add = 0;

					dp[i][j][0] = max(dp[i][j][0], dp[h][j - 1][0]  + add - ((s[i] - s[h])));
					dp[i][j][1] = max(dp[i][j][1], dp[h][j - 1][0]  + add + ((s[i] - s[h])));

					dp[i][j][0] = max(dp[i][j][0], dp[h][j - 1][1]  - add - ((s[i] - s[h])));
					dp[i][j][1] = max(dp[i][j][1], dp[h][j - 1][1]  - add + ((s[i] - s[h])));
				}
			}

		}
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>
#include <unordered_map>
#include <ctime>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left
#define right _right

const ld pi = 3.14159265359;

template < typename T > T abs(T x)
{
    return x > 0 ? x : -x;
}

template < typename T > T sqr(T x)
{
	return x * x;
}

const int maxn = 400;
const int maxk = 200;

ll s[maxn];
ll p[maxn];
ll dp[maxn][maxk][2];

int main()
{
//	srand(time(NULL));
//	gen();
	//freopen("a.in", "r", stdin);

	int n, k;
	cin >> n >> k;

	for (int i = 1; i <= n; i++)
		cin >> p[i];

	const ll inf = 1e17;

	for (int i = 0; i < maxn; i++)
		for (int j = 0; j < maxk; j++)
			dp[i][j][0] = dp[i][j][1] = -inf;

	dp[0][0][0] = 0;
	dp[0][0][1] = 0;

	for (int i = 1; i <= n; i++)
		s[i] = s[i - 1] + p[i];

	ll ans = 0;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= k; j++)
		{

			dp[i][j][0] = dp[i - 1][j][0];
			dp[i][j][1] = dp[i - 1][j][1];

			if (j > 0)
			{
				for (int h = i - 1; h >= 0; h--)
				{

					if (j == k)
					{
						ans = max(ans, dp[h][j - 1][0]  +  (s[i] - s[h]));
						ans = max(ans, dp[h][j - 1][1]  -  (s[i] - s[h]));
					}
					dp[i][j][0] = max(dp[i][j][0], dp[h][j - 1][0]  + (s[i] - s[h]) - ((s[i] - s[h])));
					dp[i][j][1] = max(dp[i][j][1], dp[h][j - 1][0]  + (s[i] - s[h]) + ((s[i] - s[h])));
					dp[i][j][0] = max(dp[i][j][0], dp[h][j - 1][1]  - (s[i] - s[h]) - ((s[i] - s[h])));
					dp[i][j][1] = max(dp[i][j][1], dp[h][j - 1][1]  - (s[i] - s[h]) + ((s[i] - s[h])));
				}
			}

		}
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>
#include <unordered_map>
#include <ctime>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left
#define right _right

const ld pi = 3.14159265359;

template < typename T > T abs(T x)
{
    return x > 0 ? x : -x;
}

template < typename T > T sqr(T x)
{
	return x * x;
}

const int maxn = 400;
const int maxk = 200;

ll s[maxn];
ll p[maxn];
ll dp[maxn][maxk][2];

int main()
{
//	srand(time(NULL));
//	gen();
	//freopen("a.in", "r", stdin);

	int n, k;
	cin >> n >> k;

	for (int i = 1; i <= n; i++)
		cin >> p[i];

	const ll inf = 1e17;

	for (int i = 0; i < maxn; i++)
		for (int j = 0; j < maxk; j++)
			dp[i][j][0] = dp[i][j][1] = -inf;

	dp[0][0][0] = 0;
	dp[0][0][1] = 0;

	for (int i = 1; i <= n; i++)
		s[i] = s[i - 1] + p[i];

	ll ans = 0;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= k; j++)
		{

			dp[i][j][0] = dp[i - 1][j][0];
			dp[i][j][1] = dp[i - 1][j][1];

			if (j > 0)
			{
				for (int h = i - 1; h >= 0; h--)
				{

					if (j == k)
					{
						ans = max(ans, dp[h][j - 1][0]  + s[i]);
						ans = max(ans, dp[h][j - 1][1]  - s[i]);
					}
					dp[i][j][0] = max(dp[i][j][0], dp[h][j - 1][0]  + s[i] - ((s[i] - s[h]) - s[i]));
					dp[i][j][1] = max(dp[i][j][1], dp[h][j - 1][0]  + s[i] + ((s[i] - s[h]) + s[i]));
					dp[i][j][0] = max(dp[i][j][0], dp[h][j - 1][1]  - s[i] - ((s[i] - s[h]) - s[i]));
					dp[i][j][1] = max(dp[i][j][1], dp[h][j - 1][1]  - s[i] + ((s[i] - s[h]) + s[i]));
				}
			}

		}
	}

	cout << ans << endl;

	return 0;
}


/*
ID: keyvank2
TASK: combo
LANG: C++
*/

#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define FOR(i,j,k) for(int i = j; i < (int)(k); i++)
#define FORV(i, v) FOR(i, 0, ((v).size()))
#define REP(i,j,k) for(int i = j; i >= (int)(k); i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()
#define autodef(x,v) typeof(v) x = (v)
#define autoref(x,v) typeof(v)& x = (v)
#define forit(it, c) for (autodef(it, ((c).begin())); it != ((c).end()); ++it)

//typedef complex<double> Point;
//#define X real()
//#define Y imag()

using namespace std;

//ifstream fin("");
//ofstream fout("");
//#define cin fin
//#define cout fout

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef pair<pii,int> ppi;
typedef pair<pll,ll> ppl;
typedef pair<int,pii> pip;
typedef pair<ll,pll> plp;
typedef pair<pii,pii> ppp;

const int INF = (int) 1e9;
const ll INFL = (ll) 9e18;
const int MAXINT = ((~0) ^ (1 << 31));
const ll MAXLL = ((~0) ^ ((ll)1 << 63));

template<class T> inline T pow2(T a) { return a*a;}
template<class T> inline bool mineq(T& a, T b){return (a > b) ? (a=b, true) : false;}
template<class T> inline bool maxeq(T& a, T b){return (a < b) ? (a=b, true) : false;}

//srand (time(NULL));

setmax(444);
const int maxk = 55;

int dp[maxn][maxn][maxk], num[maxn],n,k, sum[maxn][maxn];

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

    cin >> n >> k;
    FOR(i,0,n) cin >> num[i];

    FOR(i,0,n) sum[i][1] = num[i];

    FOR(i,0,n)
	FOR(len,2,n)
	    if(i - len + 1 >= 0)
		sum[i][len] = sum[i][len-1] + num[i-len+1];

    FOR(i,0,maxn)
    {
	FOR(j,0,maxn)
	{
	    FOR(kk,0,maxk)
	    {
		dp[i][j][kk] = -INF;
	    }
	}
    }

    FOR(i,0,n)
	FOR(j,0,i+2)
	    dp[i][j][1] = 0;

   /* FOR(i,0,n)
    {
	FOR(j,0,n) cout << sum[i][j] << " ";
	cout << endl;
    }*/

  //  cout << dp[1][1][2] << endl;

    FOR(i,1,n)
    {
	REP(len,i,1)
	{
	    FOR(kk,2,k+1)
	    {
		FOR(len2,1,i-len+2)
		{
		    maxeq(dp[i][len][kk], dp[i-len][len2][kk-1] + abs(sum[i][len]-sum[i-len][len2]));
		}
	    }
	}
    }

    int ans = 0;
    FOR(i,0,n)
	FOR(len,1,i+1)
	    maxeq(ans,dp[i][len][k]);
    cout << ans;
}


/*
ID: keyvank2
TASK: combo
LANG: C++
*/

#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define FOR(i,j,k) for(int i = j; i < (int)(k); i++)
#define FORV(i, v) FOR(i, 0, ((v).size()))
#define REP(i,j,k) for(int i = j; i >= (int)(k); i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()
#define autodef(x,v) typeof(v) x = (v)
#define autoref(x,v) typeof(v)& x = (v)
#define forit(it, c) for (autodef(it, ((c).begin())); it != ((c).end()); ++it)

//typedef complex<double> Point;
//#define X real()
//#define Y imag()

using namespace std;

//ifstream fin("");
//ofstream fout("");
//#define cin fin
//#define cout fout

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef pair<pii,int> ppi;
typedef pair<pll,ll> ppl;
typedef pair<int,pii> pip;
typedef pair<ll,pll> plp;
typedef pair<pii,pii> ppp;

const int INF = (int) 1e9;
const ll INFL = (ll) 9e18;
const int MAXINT = ((~0) ^ (1 << 31));
const ll MAXLL = ((~0) ^ ((ll)1 << 63));

template<class T> inline T pow2(T a) { return a*a;}
template<class T> inline bool mineq(T& a, T b){return (a > b) ? (a=b, true) : false;}
template<class T> inline bool maxeq(T& a, T b){return (a < b) ? (a=b, true) : false;}

//srand (time(NULL));

setmax(444);
const int maxk = 55;

int dp[maxn][maxn][maxk], num[maxn],n,k, sum[maxn][maxn];

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

    cin >> n >> k;
    FOR(i,0,n) cin >> num[i];

    FOR(i,0,n) sum[i][1] = num[i];

    FOR(i,0,n)
	FOR(len,2,n)
	    if(i - len + 1 >= 0)
		sum[i][len] = sum[i][len-1] + num[i-len+1];

    FOR(i,0,maxn)
    {
	FOR(j,0,maxn)
	{
	    FOR(kk,0,maxk)
	    {
		dp[i][j][kk] = -INF;
	    }
	}
    }

    FOR(i,0,n)
	dp[i][i+1][1] = 0;

   /* FOR(i,0,n)
    {
	FOR(j,0,n) cout << sum[i][j] << " ";
	cout << endl;
    }*/

  //  cout << dp[1][1][2] << endl;

    FOR(i,1,n)
    {
	REP(len,i,1)
	{
	    FOR(kk,2,k+1)
	    {
		FOR(len2,1,i-len+2)
		{
		  //  cout << dp[i][len][kk] << endl;
		    maxeq(dp[i][len][kk], dp[i-len][len2][kk-1] + abs(sum[i][len]-sum[i-len][len2]));
		//    cout << i << " " << len << " " << kk << " " << len2 << " " << dp[i][len][kk] << " " << dp[i-len][len2][kk-1]<< endl;
		}
	    }
	}
    }

    int ans = 0;
    FOR(i,0,n)
	FOR(len,1,i+1)
	    maxeq(ans,dp[i][len][k]);
    cout << ans;
}


//Problem E from Codeforces Round rockethon2015
// "We are drowning in information and starved for knowledge."
#include <cassert>
#include <cmath>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define int64 long long

const int inf = 0x3f3f3f3f;

int sum[500];
int dp[55][555][555];

int main() {
	int n, K;
    cin >> n >> K;
	for (int i = 1; i <= n; ++i) {
		cin >> sum[i];
		sum[i] += sum[i - 1];
	}

	int mx = 0;

	for (int k = 2; k <= K; ++k) {
		int mx0 = -inf;
		int mx1 = -inf;

		for (int st = 1; st <= n; ++st) {
			for (int dr = st; dr <= n; ++dr) {
				dp[k][st][dr] = sum[dr] - sum[st - 1] + mx0;
				dp[k][st][dr] = max(dp[k][st][dr], - (sum[dr] - sum[st - 1]) + mx1);
				if (dp[k][st][dr] > mx and k == K)
					mx = dp[k][st][dr];
			}
			for (int x = 1; x <= st; ++x) {
				mx0 = max(mx0, dp[k - 1][x][st] - (sum[st] - sum[x - 1]));
				mx1 = max(mx1, dp[k - 1][x][st] + (sum[st] - sum[x - 1]));
			}
		}
	}
	cout << mx << '\n';
	return 0;
}


#include <bits/stdc++.h>

#define jjs(i, s, x) for (int i = (s); i < (x); i++)
#define jjl(i, x) jjs(i, 0, x)
#define ji(x) jjl(i, x)
#define jj(x) jjl(j, x)
#define jk(x) jjl(k, x)
#define jij(a, b) ji(a) jj(b)
#define INF ((ll) 1e15+10)
#define pb push_back
#define mp make_pair
#define rint(x) scanf("%lld", &(x))
#define rlong(x) scanf("%lld", &(x))
#define rfloat(x) scanf("%lf", &(x))
#define MOD 1000000007
#define int ll

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;

int N, K;
int arr[30000];
int prefix[30000];
pi dp[30001][201][2];

#undef int
int main()
#define int ll
{
	rint(N); rint(K);
	ji(N) rint(arr[i]);
	prefix[0] = arr[0];
	jjs(i, 1, N) prefix[i] = prefix[i-1] + arr[i];
	dp[N][0][0] = dp[N][0][1] = {0, 0};
	for (int i = N-1; i >= 0; i--)
	{
		for (int j = 0; j <= K; j++)
		{
			if (j == 0)
			{
				dp[i][j][0] = dp[i][j][1] = {0, 0};
				continue;
			}
			dp[i][j][0] = dp[i][j][1] = {-INF, 0};
			for (int k = i; k < N; k++)
			{
				if (j - 1 - (N - k - 1) > 0) continue;
				int sum = i == 0 ? prefix[k] : prefix[k] - prefix[i-1];
				if (j == 1)
				{
					dp[i][j][1] = max(dp[i][j][1], {sum, -sum});
					dp[i][j][0] = max(dp[i][j][0], {-sum, sum});
					continue;
				}
				if (sum <= -dp[k+1][j-1][1].second)
				{
					dp[i][j][1] = max(dp[i][j][1], {dp[k+1][j-1][1].first, -sum});
					dp[i][j][0] = max(dp[i][j][0], {dp[k+1][j-1][1].first - sum * 2, sum});
				}
				if (sum >= dp[k+1][j-1][0].second)
				{
					dp[i][j][1] = max(dp[i][j][1], {dp[k+1][j-1][0].first + sum * 2, -sum});
					dp[i][j][0] = max(dp[i][j][0], {dp[k+1][j-1][0].first, sum});
				}
			}
			// printf("dp[%d][%d][%d] = {%d, %d}\n", i, j, 0, dp[i][j][0].first, dp[i][j][0].second);
			// printf("dp[%d][%d][%d] = {%d, %d}\n", i, j, 1, dp[i][j][1].first, dp[i][j][1].second);
			// printf("\n");
		}
	}
	int ans = 0;
	ji(N)
	{
		if (N - i - 1 < K-1) continue;
		int sum = prefix[i];
		if (sum <= -dp[i+1][K-1][1].second)
		{
			ans = max(ans, dp[i+1][K-1][1].first - sum);
		}
		if (sum >= dp[i+1][K-1][0].second)
		{
			ans = max(ans, dp[i+1][K-1][0].first + sum);
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

#define jjs(i, s, x) for (int i = (s); i < (x); i++)
#define jjl(i, x) jjs(i, 0, x)
#define ji(x) jjl(i, x)
#define jj(x) jjl(j, x)
#define jk(x) jjl(k, x)
#define jij(a, b) ji(a) jj(b)
#define INF ((ll) 1e15+10)
#define pb push_back
#define mp make_pair
#define rint(x) scanf("%lld", &(x))
#define rlong(x) scanf("%lld", &(x))
#define rfloat(x) scanf("%lf", &(x))
#define MOD 1000000007
#define int ll

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;

int N, K;
int arr[30000];
int prefix[30000];
pi dp[30001][201][2];

#undef int
int main()
#define int ll
{
	rint(N); rint(K);
	ji(N) rint(arr[i]);
	prefix[0] = arr[0];
	jjs(i, 1, N) prefix[i] = prefix[i-1] + arr[i];
	dp[N][0][0] = dp[N][0][1] = {0, 0};
	for (int i = N-1; i >= 0; i--)
	{
		for (int j = 0; j <= K; j++)
		{
			if (j == 0)
			{
				dp[i][j][0] = dp[i][j][1] = {0, 0};
				continue;
			}
			dp[i][j][0] = dp[i][j][1] = {-INF, 0};
			if (i < N-1)
			{
				dp[i][j][0] = dp[i+1][j][0];
				dp[i][j][1] = dp[i+1][j][1];
			}
			for (int k = i; k < N; k++)
			{
				if (j - 1 - (N - k - 1) > 0) continue;
				int sum = i == 0 ? prefix[k] : prefix[k] - prefix[i-1];
				if (j == 1)
				{
					dp[i][j][1] = max(dp[i][j][1], {sum, -sum});
					dp[i][j][0] = max(dp[i][j][0], {-sum, sum});
					continue;
				}
				if (sum <= -dp[k+1][j-1][1].second)
				{
					dp[i][j][1] = max(dp[i][j][1], {dp[k+1][j-1][1].first, -sum});
					dp[i][j][0] = max(dp[i][j][0], {dp[k+1][j-1][1].first - sum * 2, sum});
				}
				if (sum >= dp[k+1][j-1][0].second)
				{
					dp[i][j][1] = max(dp[i][j][1], {dp[k+1][j-1][0].first + sum * 2, -sum});
					dp[i][j][0] = max(dp[i][j][0], {dp[k+1][j-1][0].first, sum});
				}
			}
			// printf("dp[%d][%d][%d] = {%d, %d}\n", i, j, 0, dp[i][j][0].first, dp[i][j][0].second);
			// printf("dp[%d][%d][%d] = {%d, %d}\n", i, j, 1, dp[i][j][1].first, dp[i][j][1].second);
			// printf("\n");
		}
	}
	int ans = 0;
	jj(N) jjs(i, j, N)
	{
		if (N - i - 1 < K-1) continue;
		int sum = prefix[i];
		if (j > 0) sum -= prefix[j-1];
		if (sum <= -dp[i+1][K-1][1].second)
		{
			ans = max(ans, dp[i+1][K-1][1].first - sum);
		}
		if (sum >= dp[i+1][K-1][0].second)
		{
			ans = max(ans, dp[i+1][K-1][0].first + sum);
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

#define jjs(i, s, x) for (int i = (s); i < (x); i++)
#define jjl(i, x) jjs(i, 0, x)
#define ji(x) jjl(i, x)
#define jj(x) jjl(j, x)
#define jk(x) jjl(k, x)
#define jij(a, b) ji(a) jj(b)
#define INF ((ll) 1e15+10)
#define pb push_back
#define mp make_pair
#define rint(x) scanf("%lld", &(x))
#define rlong(x) scanf("%lld", &(x))
#define rfloat(x) scanf("%lf", &(x))
#define MOD 1000000007
#define int ll

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;

int N, K;
int arr[30000];
int prefix[30000];
pi dp[30001][201][2];

#undef int
int main()
#define int ll
{
	rint(N); rint(K);
	ji(N) rint(arr[i]);
	prefix[0] = arr[0];
	jjs(i, 1, N) prefix[i] = prefix[i-1] + arr[i];
	dp[N][0][0] = dp[N][0][1] = {0, 0};
	for (int i = N-1; i >= 0; i--)
	{
		for (int j = 0; j <= K; j++)
		{
			if (j == 0)
			{
				dp[i][j][0] = dp[i][j][1] = {0, 0};
				continue;
			}
			dp[i][j][0] = dp[i][j][1] = {-INF, 0};
			for (int k = i; k < N; k++)
			{
				if (j - 1 - (N - k - 1) > 0) continue;
				int sum = i == 0 ? prefix[k] : prefix[k] - prefix[i-1];
				if (j == 1)
				{
					dp[i][j][1] = max(dp[i][j][1], {sum, sum});
					dp[i][j][0] = max(dp[i][j][0], {-sum, sum});
					continue;
				}
				if (sum <= dp[k+1][j-1][1].second)
				{
					dp[i][j][1] = max(dp[i][j][1], {dp[k+1][j-1][1].first, sum});
					dp[i][j][0] = max(dp[i][j][0], {dp[k+1][j-1][1].first - sum * 2, sum});
				}
				if (sum >= dp[k+1][j-1][0].second)
				{
					dp[i][j][1] = max(dp[i][j][1], {dp[k+1][j-1][0].first + sum * 2, sum});
					dp[i][j][0] = max(dp[i][j][0], {dp[k+1][j-1][0].first, sum});
				}
			}
			// printf("dp[%d][%d][%d] = {%d, %d}\n", i, j, 0, dp[i][j][0].first, dp[i][j][0].second);
			// printf("dp[%d][%d][%d] = {%d, %d}\n", i, j, 1, dp[i][j][1].first, dp[i][j][1].second);
			// printf("\n");
		}
	}
	int ans = 0;
	ji(N)
	{
		if (N - i - 1 < K-1) continue;
		int sum = prefix[i];
		if (sum <= dp[i+1][K-1][1].second)
		{
			ans = max(ans, dp[i+1][K-1][1].first - sum);
		}
		if (sum >= dp[i+1][K-1][0].second)
		{
			ans = max(ans, dp[i+1][K-1][0].first + sum);
		}
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

#define jjs(i, s, x) for (int i = (s); i < (x); i++)
#define jjl(i, x) jjs(i, 0, x)
#define ji(x) jjl(i, x)
#define jj(x) jjl(j, x)
#define jk(x) jjl(k, x)
#define jij(a, b) ji(a) jj(b)
#define INF ((int) 1e9+10)
#define pb push_back
#define mp make_pair
#define rint(x) scanf("%d", &(x))
#define rlong(x) scanf("%lld", &(x))
#define rfloat(x) scanf("%lf", &(x))
#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;

int N, K;
int arr[30000];
int prefix[30000];
pi dp[30001][201][2];

int main()
{
	rint(N); rint(K);
	ji(N) rint(arr[i]);
	prefix[0] = arr[0];
	jjs(i, 1, N) prefix[i] = prefix[i-1] + arr[i];
	dp[N][0][0] = dp[N][0][1] = {0, 0};
	for (int i = N-1; i >= 0; i--)
	{
		for (int j = 0; j <= K; j++)
		{
			if (j == 0)
			{
				dp[i][j][0] = dp[i][j][1] = {0, 0};
				continue;
			}
			dp[i][j][0] = dp[i][j][1] = {-INF, 0};
			for (int k = i; k < N; k++)
			{
				if (j - 1 - (N - k - 1) > 0) continue;
				int sum = i == 0 ? prefix[k] : prefix[k] - prefix[i-1];
				if (j == 1)
				{
					dp[i][j][1] = max(dp[i][j][1], {sum, sum});
					dp[i][j][0] = max(dp[i][j][0], {-sum, sum});
					continue;
				}
				if (sum <= dp[k+1][j-1][1].second)
				{
					dp[i][j][1] = max(dp[i][j][1], {dp[k+1][j-1][1].first, sum});
					dp[i][j][0] = max(dp[i][j][0], {dp[k+1][j-1][1].first - sum * 2, sum});
				}
				if (sum >= dp[k+1][j-1][0].second)
				{
					dp[i][j][1] = max(dp[i][j][1], {dp[k+1][j-1][0].first + sum * 2, sum});
					dp[i][j][0] = max(dp[i][j][0], {dp[k+1][j-1][0].first, sum});
				}
			}
			// printf("dp[%d][%d][%d] = {%d, %d}\n", i, j, 0, dp[i][j][0].first, dp[i][j][0].second);
			// printf("dp[%d][%d][%d] = {%d, %d}\n", i, j, 1, dp[i][j][1].first, dp[i][j][1].second);
			// printf("\n");
		}
	}
	int ans = 0;
	ji(N)
	{
		if (N - i - 1 < K-1) continue;
		int sum = prefix[i];
		if (sum <= dp[i+1][K-1][1].second)
		{
			ans = max(ans, dp[i+1][K-1][1].first - sum);
		}
		if (sum >= dp[i+1][K-1][0].second)
		{
			ans = max(ans, dp[i+1][K-1][0].first + sum);
		}
	}
	printf("%d\n", ans);
}


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

#ifdef SG
	#include <debug.h>
#else
	#define show(...)
	#define debug(...)
	#define deepen(...)
	#define timer(...)
#endif

#define ARG4(_1,_2,_3,_4,...) _4

#define forn3(i,l,r) for (int i = int(l); i < int(r); ++i)
#define forn2(i,n) forn3 (i, 0, n)
#define forn(...) ARG4(__VA_ARGS__, forn3, forn2) (__VA_ARGS__)

#define ford3(i,l,r) for (int i = int(r) - 1; i >= int(l); --i)
#define ford2(i,n) ford3 (i, 0, n)
#define ford(...) ARG4(__VA_ARGS__, ford3, ford2) (__VA_ARGS__)

#define ve vector
#define pa pair
#define tu tuple
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fs first
#define sc second
#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)(a).size())

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef unsigned char uc;
typedef pa<int, int> pii;
typedef pa<int, ll> pil;
typedef pa<ll, int> pli;
typedef pa<ll, ll> pll;
typedef ve<int> vi;

const ld pi = 3.1415926535897932384626433832795l;

template<typename T> inline auto sqr (T x) -> decltype(x * x) {return x * x;}
template<typename T1, typename T2> inline bool umx (T1& a, T2 b) {if (a < b) {a = b; return 1;} return 0;}
template<typename T1, typename T2> inline bool umn (T1& a, T2 b) {if (b < a) {a = b; return 1;} return 0;}

const int N = 30000;
const int K = 200;
const int X = 10000;

struct Input {
	int n, k;
	int a[N];

	bool read () {
		if (!(cin >> n >> k)) {
			return 0;
		}
		forn (i, n) {
			scanf("%d", &a[i]);
		}
		return 1;
	}

	void init (const Input &input) {
		*this = input;
	}
};

struct Data: Input {
	int ans;

	void write () {
		cout << ans << endl;
	}

	virtual void solve () {}

	virtual void clear () {
		*this = Data();
	}
};

struct Solution: Data {
	static const int inf = 1000000000;

	int s[N + 1];
	int d[2][2 * K + 1][3][3];

	void solve () {
		s[0] = 0;
		forn (i, n) {
			s[i + 1] = s[i] + a[i];
		}
		forn (i, 2 * k + 1) {
			forn (l1, 3) {
				forn (l2, 3) {
					d[0][i][l1][l2] = -inf;
				}
			}
		}
		d[0][0][1][1] = 0;
		forn (i, n + 1) {
			int i0 = i & 1;
			int i1 = i0 ^ 1;
			memcpy(d[i1], d[i0], sizeof d[i1]);
			//debug(i + 1);
			forn (j, 2 * k) {
				if (j & 1) {
					forn (l1, 3) {
						forn (l2, 3) {
							umx(d[i1][j + 1][l1][l2], d[i0][j][l1][l2] + (l2 - l1) * s[i]);
						}
					}
				} else {
					forn (l0, 3) {
						forn (l1, 3) {
							forn (l2, 3) {
								umx(d[i1][j + 1][l1][l2], d[i1][j][l0][l1] - (l2 - l1) * s[i]);
							}
						}
					}
				}
				//forn (l1, 3) {
				//	debug(d[i1][j + 1][l1], d[i1][j + 1][l1] + 3);
				//}
				//debug();
			}
		}
		ans = -inf;
		forn (l, 3) {
			umx(ans, d[(n + 1) & 1][2 * k][l][1]);
		}
	}

	void clear () {
		*this = Solution();
	}
};

Solution sol;

int main () {
	cout.setf(ios::showpoint | ios::fixed);
	cout.precision(20);
#ifdef SG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	while (sol.read()) {
		sol.solve();
		sol.write();
		sol.clear();
	}
#else
	sol.read();
	sol.solve();
	sol.write();
#endif
	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 add[30010][203], sub[30010][203], ign[30010][203];

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;
	max_suf = -INF, min_suf = INF;
	FWD(i,0,n+1) FWD(r,0,k+1) add[i][r] = sub[i][r] = ign[i][r] = -INF;
	FWD(i,1,n+1){
		int a = seq[i];
		max_suf = max(a, max_suf+a);
		min_suf = min(a, min_suf+a);
		add[i][1] = max_suf;
		sub[i][1] = -min_suf;
		FWD(r,2,k+1){
			add[i][r] = max3(add[i-1][r], ign[i-1][r-1], sub[i-1][r-1]) + 2*a;
			ign[i][r] = max3(add[i-1][r-1], ign[i-1][r], sub[i-1][r-1]);
			sub[i][r] = max3(add[i-1][r-1], ign[i-1][r-1], sub[i-1][r]) - 2*a;
		}
	}
	//FWD(i,1,n+1){ FWD(r,0,k+1) printf("%d ", add[i][r]); printf("\n"); } printf("\n");
	//FWD(i,1,n+1){ FWD(r,0,k+1) printf("%d ", ign[i][r]); printf("\n"); } printf("\n");
	//FWD(i,1,n+1){ FWD(r,0,k+1) printf("%d ", sub[i][r]); printf("\n"); } printf("\n");
	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, add[i-1][k-1] - min_suf);
		res = max(res, sub[i-1][k-1] + max_suf);
	}
	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 INF = 1000*1000*1000;

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

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

int main(){
	scanf("%d %d", &n, &k);
	FWD(i,1,n+1) scanf("%d", &seq[i]);
	FWD(d,0,4) FWD(i,0,n+1) FWD(r,0,k+1) dp[d][i][r] = -INF;
	min_suf = INF, max_suf = -INF;
	FWD(i,1,n+1){
		int a = seq[i];
		min_suf = min(a, min_suf+a);
		max_suf = max(a, max_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] = max3(dp[1][i-1][r], dp[0][i-1][r-1], dp[1][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] = max3(dp[3][i-1][r], dp[2][i-1][r-1], dp[3][i-1][r-1]);
		}
	}
	max_suf = -INF, min_suf = INF;
	BCK(i,n,0){
		int a = seq[i];
		min_suf = min(a, min_suf+a);
		max_suf = max(a, max_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 <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);
					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);
					if(r+1 == k) res = max(res, best_hi[j][r] - sum);
				}
		}
	}
	printf("%d\n", res);
	return 0;
}