import java.io.*;
import java.util.*;

public class Solution {

    StreamTokenizer in;
    PrintWriter out;

    public static void main(String[] args) throws Exception {
        new Solution().run();
    }

    public void run() throws Exception {
        //in = new BufferedReader(new InputStreamReader(System.in));
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(new OutputStreamWriter(System.out));
        solve();
        out.flush();
    }

    int nextInt() throws Exception {
        in.nextToken();
        return (int) in.nval;
    }

    public void solve() throws Exception {
        int n=nextInt();
        LinkedList a [] = new LinkedList [210];
        int y[] = new int [210];
        for (int i=0;i<210;i++)
            a[i] =new LinkedList <Integer> ();
        boolean [] b = new boolean [210];
        for (int i=0; i<n*(n-1)/2;i++)
        {
            int k=nextInt();
            LinkedList <Integer> l = new LinkedList <Integer>();
            for (int j=0; j<k;j++)
            {
                int s = nextInt();
                b[s]=true;
                l.add(s);
                y[s]++;
            }
            for (int j=0; j<k;j++)
            {
                for (int q=0;q<k;q++)
                {
                    if (q!=j&&!a[l.get(j)].contains(l.get(q))&&y[l.get(q)]==1&&y[l.get(j)]==1)
                        a[l.get(j)].add(l.get(q));
                }
            }
            for (int j=0; j<k;j++)
            {
                for (int q=0; q<a[l.get(j)].size();q++)
                    if (!l.contains(a[l.get(j)].get(q))&&a[l.get(j)].get(q)!=l.get(j))
                    {
                        a[l.get(j)].remove(a[l.get(j)].get(q));
                        q--;
                    }
            }
        }
        for (int i=0; i<210;i++)
        {
            if (a[i].size()==0&&b[i])
                {
                out.println("1 "+i);
                b[i]=false;
                }
            else
            {
                if (b[i])
                {
                out.print((a[i].size()+1)+" "+(i)+" ");
                b[i]=false;
                for (int j=0; j<a[i].size();j++)
                {
                //  if (b[j])
                out.print(a[i].get(j)+" ");
                b[(Integer) a[i].get(j)]=false;
                }
                out.println();
                }
            }
        }
    }

}


import java.io.*;
import java.util.*;

public class Solution {

    StreamTokenizer in;
    PrintWriter out;

    public static void main(String[] args) throws Exception {
        new Solution().run();
    }

    public void run() throws Exception {
        //in = new BufferedReader(new InputStreamReader(System.in));
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(new OutputStreamWriter(System.out));
        solve();
        out.flush();
    }

    int nextInt() throws Exception {
        in.nextToken();
        return (int) in.nval;
    }

    public void solve() throws Exception {
        int n=nextInt();
        LinkedList a [] = new LinkedList [210];
        int y[] = new int [210];
        for (int i=0;i<210;i++)
            a[i] =new LinkedList <Integer> ();
        boolean [] b = new boolean [210];
        for (int i=0; i<n*(n-1)/2;i++)
        {
            int k=nextInt();
            LinkedList <Integer> l = new LinkedList <Integer>();
            for (int j=0; j<k;j++)
            {
                int s = nextInt();
                b[s]=true;
                l.add(s);
                y[s]++;
            }
            for (int j=0; j<k;j++)
            {
                for (int q=0;q<k;q++)
                {
                    if (q!=j&&!a[l.get(j)].contains(l.get(q))&&y[l.get(q)]==1&&y[l.get(j)]==1)
                        a[l.get(j)].add(l.get(q));
                }
            }
            for (int j=0; j<k;j++)
            {
                for (int q=0; q<a[l.get(j)].size();q++)
                    if (!l.contains(a[l.get(j)].get(q))&&a[l.get(j)].get(q)!=l.get(j))
                    {
                        a[l.get(j)].remove(a[l.get(j)].get(q));
                        q--;
                    }
            }
        }
        if (n==2)
        {
            for (int j=0; j<210;j++)
            {
                if (a[j].size()!=0)
                {
                    out.println("1 "+j);
                    int count=0;
                    for (int i=0; i<n-1;i++)
                    {
                        out.print((a[j].size())+" "+a[j].get(count++)+" ");
                    }
                    for (int i=count;i<a[i].size();i++)
                        out.print(a[j].get(i)+" ");
                    out.flush();
                    return;
                }
            }
        }
        for (int i=0; i<210;i++)
        {
            if (a[i].size()==0&&b[i])
                {
                out.println("1 "+i);
                b[i]=false;
                }
            else
            {
                if (b[i])
                {
                out.print((a[i].size()+1)+" "+(i)+" ");
                b[i]=false;
                for (int j=0; j<a[i].size();j++)
                {
                //  if (b[j])
                out.print(a[i].get(j)+" ");
                b[(Integer) a[i].get(j)]=false;
                }
                out.println();
                }
            }
        }
    }

}


import java.io.*;
import java.util.*;

public class SolB implements Runnable {
	public static void main(String[] args) {
		new Thread(new SolB()).start();
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	boolean eof;

	@Override
	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return "0";
			}
		}
		return st.nextToken();
	}

	int nextInt() {
		return Integer.parseInt(nextToken());
	}

	long nextLong() {
		return Long.parseLong(nextToken());
	}

	double nextDouble() {
		return Double.parseDouble(nextToken());
	}

	void solve() throws IOException {
		int n = nextInt();
		int[][] tab = new int[200][n - 1];
		int[] c = new int[200];
		if (n == 2) {
			int k = nextInt();
			int[] a = new int[k];
			for (int i = 0; i < k; i++) {
				a[i] = nextInt();
			}
			out.println("1 " + a[0]);
			out.print((k - 1));
			for (int i = 1; i < k; i++) {
				out.print(" " + a[i]);
			}
			out.println();
			return;
		}
		for (int i = 0; i < n * (n - 1) / 2; i++) {
			int k = nextInt();
			for (int j = 0; j < k; j++) {
				int t = nextInt() - 1;
				tab[t][c[t]++] = i;
			}
		}
		for (int i = 0; i < c.length; i++) {
			if (c[i] > 0) {
				Arrays.sort(tab[i]);
			}
		}
		for (int i = 0; i < c.length; i++) {
			if (c[i] > 0) {
				ArrayList<Integer> ans = new ArrayList<Integer>();
				for (int j = i; j < tab.length; j++) {
					if (c[j] > 0 && Arrays.equals(tab[i], tab[j])) {
						ans.add(j + 1);
						c[j] = 0;
					}
				}
				out.print(ans.size());
				for (int j : ans) {
					out.print(" " + j);
				}
				out.println();
			}
		}
	}
}


import java.io.*;
import java.util.*;

public class SolB implements Runnable {
	public static void main(String[] args) {
		new Thread(new SolB()).start();
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	boolean eof;

	@Override
	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			solve();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return "0";
			}
		}
		return st.nextToken();
	}

	int nextInt() {
		return Integer.parseInt(nextToken());
	}

	long nextLong() {
		return Long.parseLong(nextToken());
	}

	double nextDouble() {
		return Double.parseDouble(nextToken());
	}

	void solve() throws IOException {
		int n = nextInt();
		int[][] tab = new int[200][n - 1];
		int[] c = new int[200];
		for (int i = 0; i < n * (n - 1) / 2; i++) {
			int k = nextInt();
			for (int j = 0; j < k; j++) {
				int t = nextInt() - 1;
				tab[t][c[t]++] = i;
			}
		}
		for (int i = 0; i < c.length; i++) {
			if (c[i] > 0) {
				Arrays.sort(tab[i]);
			}
		}
		for (int i = 0; i < c.length; i++) {
			if (c[i] > 0) {
				ArrayList<Integer> ans = new ArrayList<Integer>();
				for (int j = i; j < tab.length; j++) {
					if (c[j] > 0 && Arrays.equals(tab[i], tab[j])) {
						ans.add(j + 1);
						c[j] = 0;
					}
				}
				out.print(ans.size());
				for (int j : ans) {
					out.print(" " + j);
				}
				out.println();
			}
		}
	}
}


import java.io.*;
import java.util.*;

public class Sets {
    public static class A extends ArrayList<Integer> {}
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader br = new BufferedReader(new FileReader("A.txt"));
        PrintWriter out = new PrintWriter(System.out);
        StringTokenizer tokenizer = null;

        A[] lists = new A[201];
        for(int i = 0; i < lists.length; ++i) lists[i] = new A();
        int n = Integer.parseInt(br.readLine());
        if(n == 2) {
            String[] result = br.readLine().split(" ");
            out.println("1 " + Integer.parseInt(result[1]));
            out.print(Integer.parseInt(result[0]) - 1);
            for(int i = 2; i < result.length; ++i)
                out.print(" " + result[i]);
        }
        else {
            for(int i = 0; i < n * (n - 1) / 2; ++i) {
                tokenizer = new StringTokenizer(br.readLine());
                int count = Integer.parseInt(tokenizer.nextToken());
                for(int j = 0; j < count; ++j) {
                    int next = Integer.parseInt(tokenizer.nextToken());
                    lists[next].add(i);
                }
            }
            boolean[] used = new boolean[lists.length];
            for(int i = 0; i < lists.length; ++i) {
                if(lists[i].isEmpty() || used[i]) { used[i] = true; continue; }
                A result = new A();
                for(int j = i; j < lists.length; ++j) {
                    if(lists[i].equals(lists[j])) {
                        result.add(j);
                        used[j] = true;
                    }
                }
                out.print(result.size());
                for(int k = 0; k < result.size(); ++k)
                    out.print(" " + result.get(k));
                out.println();
            }
        }
        br.close();
        out.close();
    }
}


import java.io.*;
import java.util.*;

public class Sets {
    public static class A extends ArrayList<Integer> {}
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader br = new BufferedReader(new FileReader("A.txt"));
        PrintWriter out = new PrintWriter(System.out);
        StringTokenizer tokenizer = null;

        A[] lists = new A[201];
        for(int i = 0; i < lists.length; ++i) lists[i] = new A();
        int n = Integer.parseInt(br.readLine());
        if(n == 2) {
            String[] result = br.readLine().split(" ");
            out.println("1 " + Integer.parseInt(result[1]));
            out.println("1 " + Integer.parseInt(result[2]));
        }
        else {
            for(int i = 0; i < n * (n - 1) / 2; ++i) {
                tokenizer = new StringTokenizer(br.readLine());
                int count = Integer.parseInt(tokenizer.nextToken());
                for(int j = 0; j < count; ++j) {
                    int next = Integer.parseInt(tokenizer.nextToken());
                    lists[next].add(i);
                }
            }
            boolean[] used = new boolean[lists.length];
            for(int i = 0; i < lists.length; ++i) {
                if(lists[i].isEmpty() || used[i]) { used[i] = true; continue; }
                A result = new A();
                for(int j = i; j < lists.length; ++j) {
                    if(lists[i].equals(lists[j])) {
                        result.add(j);
                        used[j] = true;
                    }
                }
                out.print(result.size());
                for(int k = 0; k < result.size(); ++k)
                    out.print(" " + result.get(k));
                out.println();
            }
        }
        br.close();
        out.close();
    }
}


import java.io.*;
import java.util.*;

public class Main{

     static void process(InputStream is, OutputStream os) {
        Scanner sc = new Scanner(is);
        PrintWriter pw = new PrintWriter(os);

        int n = sc.nextInt();

        int[][] match = new int[200][];
        for (int i = 0; i < 200; i++) {
            match[i] = new int[200];
        }

        int pairs = n * (n - 1) / 2;
        for (int i = 0; i < pairs; i++) {

            int l = sc.nextInt();
            int[] line = new int[l];

            //System.out.println("LINE:" + l);

            for (int j = 0; j < l; j++) {
                line[j] = sc.nextInt() - 1;
            }

            for (int j = 0; j < l; j++) {

                match[line[j]][line[j]]++;
                //System.out.println("MATCH:" + line[j] + ":" + line[j]);

                for (int k = j + 1; k < l; k++) {
                    if (line[j] <= line[k]) {
                        match[line[j]][line[k]]++;
                        //System.out.println("MATCH:" + line[j] + ":" + line[k]);
                    } else {
                        match[line[k]][line[j]]++;
                        //System.out.println("MATCH:" + line[k] + ":" + line[j]);
                    }
                }
            }
        }

        int[] use = new int[200];

        int criterion = n - 1;

        if (n == 2) {

            int z = 0;

            for (int i = 0; i < 200; i++) {
                if (match[i][i] == criterion) {
                    pw.write(String.format("%d %d%n", 1, i + 1));
                    z = i;
                    break;
                }
            }

            List<Integer> list = new ArrayList<Integer>();
            for (int i = z + 1; i < 200; i++) {
                if (match[i][i] == criterion) {
                    list.add(i);
                }
            }

            pw.write(String.format("%d", list.size()));
            for (int m : list) {
                pw.write(String.format(" %d", m + 1));
            }
            pw.write(String.format("%n"));

        } else {

            for (int i = 0; i < 200; i++) {
                if (use[i] == 0 && match[i][i] == criterion) {
                    use[i] = 1;
                    List<Integer> list = new ArrayList<Integer>();
                    list.add(i);
                    for (int j = i + 1; j < 200; j++) {
                        if (match[i][j] == criterion) {
                            use[j] = 1;
                            list.add(j);
                        }
                    }
                    pw.write(String.format("%d", list.size()));
                    for (int m : list) {
                        pw.write(String.format(" %d", m + 1));
                    }
                    pw.write(String.format("%n"));
                }
            }

        }

        pw.flush();
    }

    public static void main(String[] args) {
        process(System.in, System.out);

    }

}


import java.io.*;
import java.util.*;

public class Main{

    static void process(InputStream is, OutputStream os) {
        Scanner sc = new Scanner(is);
        PrintWriter pw = new PrintWriter(os);

        int n = sc.nextInt();

        int[][] match = new int[200][];
        for(int i = 0; i < 200; i++) {
            match[i] = new int[200];
        }

        int pairs = n * (n - 1) / 2;
        for(int i = 0; i < pairs; i++) {

            int l = sc.nextInt();
            int[] line = new int[l];

            //System.out.println("LINE:" + l);

            for(int j = 0; j < l; j++) {
                line[j] = sc.nextInt() - 1;
            }

            for(int j = 0; j < l; j++) {

                match[line[j]][line[j]]++;
                //System.out.println("MATCH:" + line[j] + ":" + line[j]);

                for(int k = j + 1; k < l; k++) {
                    if(line[j] <= line[k]) {
                        match[line[j]][line[k]]++;
                        //System.out.println("MATCH:" + line[j] + ":" + line[k]);
                    } else {
                        match[line[k]][line[j]]++;
                        //System.out.println("MATCH:" + line[k] + ":" + line[j]);
                    }
                }
            }
        }

        int[] use = new int[200];

        int criterion = n - 1;

        for(int i = 0; i < 200; i++) {
            if(use[i] == 0 && match[i][i] == criterion) {
                use[i] = 1;
                List<Integer> list = new ArrayList<Integer>();
                list.add(i);
                for(int j = i + 1; j < 200 ; j ++) {
                    if(match[i][j] == criterion) {
                        use[j] = 1;
                        list.add(j);
                    }
                }
                pw.write(String.format("%d", list.size()));
                for(int m : list) {
                    pw.write(String.format(" %d", m + 1));
                }
                pw.write(String.format("%n"));
            }
        }

        pw.flush();
    }

    public static void main(String[] args) {
        process(System.in, System.out);

    }

}


import java.io.*;
import java.util.*;

public class Main{

    static void process(InputStream is, OutputStream os) {
        Scanner sc = new Scanner(is);
        PrintWriter pw = new PrintWriter(os);

        int n = sc.nextInt();

        int[][] match = new int[20][];
        for(int i = 0; i < 20; i++) {
            match[i] = new int[20];
        }

        int pairs = n * (n - 1) / 2;
        for(int i = 0; i < pairs; i++) {

            int l = sc.nextInt();
            int[] line = new int[l];

            //System.out.println("LINE:" + l);

            for(int j = 0; j < l; j++) {
                line[j] = sc.nextInt() - 1;
            }

            for(int j = 0; j < l; j++) {

                match[line[j]][line[j]]++;
                //System.out.println("MATCH:" + line[j] + ":" + line[j]);

                for(int k = j + 1; k < l; k++) {
                    if(line[j] <= line[k]) {
                        match[line[j]][line[k]]++;
                        //System.out.println("MATCH:" + line[j] + ":" + line[k]);
                    } else {
                        match[line[k]][line[j]]++;
                        //System.out.println("MATCH:" + line[k] + ":" + line[j]);
                    }
                }
            }
        }

        int[] use = new int[20];

        int criterion = n - 1;

        for(int i = 0; i < 20; i++) {
            if(use[i] == 0 && match[i][i] == criterion) {
                use[i] = 1;
                List<Integer> list = new ArrayList<Integer>();
                list.add(i);
                for(int j = i + 1; j < 20 ; j ++) {
                    if(match[i][j] == criterion) {
                        use[j] = 1;
                        list.add(j);
                    }
                }
                pw.write(String.format("%d", list.size()));
                for(int m : list) {
                    pw.write(String.format(" %d", m + 1));
                }
                pw.write(String.format("%n"));
            }
        }

        pw.flush();
    }

    public static void main(String[] args) {
        process(System.in, System.out);

    }

}


// eddy1021
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long long ll;
typedef int TYPE;
typedef pair<int,int> PII;
#define mod9 1000000009ll
#define mod7 1000000007ll
#define INF 1023456789000000ll
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define eps 1e-7
int getint(){
    int _x=0,_tmp=1; char _tc=getchar();
    while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
    if( _tc == '-' ) _tc=getchar() , _tmp = -1;
    while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
    return _x*_tmp;
}
bool equal( D _x ,  D _y ){
    return _x > _y - eps && _x < _y + eps;
}
int __ = 1 , cs;
/*********default*********/
#define N 30010
#define K 210
ll n , k , a[ N ];
ll bstu[ N ][ K ];
ll bstd[ N ][ K ];
bool tagu[ N ][ K ];
bool tagd[ N ][ K ];
void build(){

}
void update_u( ll tn , ll tk , ll ta ){
    if( ta > bstu[ tn ][ tk ] ){
        bstu[ tn ][ tk ] = ta;
        tagu[ tn ][ tk ] = true;
    }
}
void update_d( ll tn , ll tk , ll ta ){
    if( ta > bstd[ tn ][ tk ] ){
        bstd[ tn ][ tk ] = ta;
        tagd[ tn ][ tk ] = true;
    }
}
void init(){
    scanf( "%I64d%I64d" , &n , &k );
    for( ll i = 0 ; i <= n ; i ++ )
        for( ll j = 0 ; j <= k ; j ++ )
            bstu[ i ][ j ] = bstd[ i ][ j ] = -INF;
    for( ll i = 1 ; i <= n ; i ++ )
        scanf( "%I64d" , &a[ i ] );
}
ll ans;
void solve(){
    for( ll i = 1 ; i <= n ; i ++ ){
        if( i > 1 ){
            for( ll j = 1 ; j <= k ; j ++ ){
                update_u( i , j , bstu[ i - 1 ][ j ] );
                update_d( i , j , bstd[ i - 1 ][ j ] );
            }
        }
        ll sum = 0;
        for( ll j = i ; j >= 1 ; j -- ){
            sum += a[ j ];
            for( ll tk = 1 ; tk < k ; tk ++ ){
                ll tmpu = -INF , tmpd = -INF , tb;
                if( tagu[ j - 1 ][ tk ] )
                    tmpu = bstu[ j - 1 ][ tk ] - sum;
                if( tagd[ j - 1 ][ tk ] )
                    tmpd = bstd[ j - 1 ][ tk ] + sum;
                tb = max( tmpu , tmpd );
                if( tb == -INF ) continue;
                if( tk + 1 == k ) ans = max( ans , tb );
                update_u( i , tk + 1 , tb + sum );
                update_d( i , tk + 1 , tb - sum );
            }
        }
        update_u( i , 1 , sum );
        update_d( i , 1 , -sum );
    }
    printf( "%I64d\n" , ans );
}
int main(){
    build();
//    __ = getint();
    while( __ -- ){
        init();
        solve();
    }
}


// eddy1021
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long long ll;
typedef int TYPE;
typedef pair<int,int> PII;
#define mod9 1000000009ll
#define mod7 1000000007ll
#define INF 1023456789
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define eps 1e-7
int getint(){
    int _x=0,_tmp=1; char _tc=getchar();
    while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
    if( _tc == '-' ) _tc=getchar() , _tmp = -1;
    while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
    return _x*_tmp;
}
bool equal( D _x ,  D _y ){
    return _x > _y - eps && _x < _y + eps;
}
int __ = 1 , cs;
/*********default*********/
#define N 410
#define K 52
int n , k , a[ N ];
//map<int,int> dp[ N ][ K ];
int bstu[ N ][ K ];
int bstd[ N ][ K ];
//typedef map<int,int>::iterator mi;
void build(){

}
void update_u( int tn , int tk , int ta ){
    if( ta > bstu[ tn ][ tk ] )
        bstu[ tn ][ tk ] = ta;
}
void update_d( int tn , int tk , int ta ){
    if( ta > bstd[ tn ][ tk ] )
        bstd[ tn ][ tk ] = ta;
}
void init(){
    n = getint(); k = getint();
    for( int i = 0 ; i <= n ; i ++ )
        for( int j = 0 ; j <= k ; j ++ )
            bstu[ i ][ j ] = bstd[ i ][ j ] = -INF;
    for( int i = 1 ; i <= n ; i ++ )
        a[ i ] = getint();
}
int ans;
void solve(){
    for( int i = 1 ; i <= n ; i ++ ){
        for( int j = 1 ; j <= k ; j ++ ){
            update_u( i , j , bstu[ i - 1 ][ j ] );
            update_d( i , j , bstd[ i - 1 ][ j ] );
        }
//            dp[ i ][ j ] = dp[ i - 1 ][ j ];
        int sum = 0;
        for( int j = i ; j >= 1 ; j -- ){
            sum += a[ j ];
            for( int tk = 1 ; tk <= min( k - 1 , j - 1 ) ; tk ++ ){
                int tmpu = -INF , tmpd = -INF , tb;
                if( bstu[ j - 1 ][ tk ] != -INF )
                    tmpu = max( tmpu , bstu[ j - 1 ][ tk ] - sum );
                if( bstd[ j - 1 ][ tk ] != -INF )
                    tmpd = max( tmpd , bstd[ j - 1 ][ tk ] + sum );
                tb = max( tmpu , tmpd );
                if( tb == -INF ) continue;
                if( tk + 1 == k )
                    ans = max( ans , tb );
                update_u( i , tk + 1 , tb + sum );
                update_d( i , tk + 1 , tb - sum );
            }
/*                for( mi it = dp[ j - 1 ][ tk ].begin() ; it != dp[ j - 1 ][ tk ].end() ; it ++ ){
                    dp[ i ][ tk + 1 ][ sum ] = max( dp[ i ][ tk + 1 ][ sum ] , abs( sum - it->FI ) + it->SE );
                    if( tk + 1 == k )
                        ans = max( ans , dp[ i ][ tk + 1 ][ sum ] );
                }*/
        }
        update_u( i , 1 , sum );
        update_d( i , 1 , -sum );
//        dp[ i ][ 1 ][ sum ] = 0;
    }
//    for( int i = 1 ; i <= n ; i ++ )
//        for( int j = 1 ; j <= k ; j ++ )
//            printf( "%d %d : %I64d %I64d\n" , i , j , bstu[ i ][ j ] , bstd[ i ][ j ] );
    printf( "%d\n" , ans );
}
int main(){
    build();
//    __ = getint();
    while( __ -- ){
        init();
        solve();
    }
}


// eddy1021
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long long ll;
typedef int TYPE;
typedef pair<int,int> PII;
#define mod9 1000000009ll
#define mod7 1000000007ll
#define INF 1023456789000000ll
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define eps 1e-7
int getint(){
    int _x=0,_tmp=1; char _tc=getchar();
    while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
    if( _tc == '-' ) _tc=getchar() , _tmp = -1;
    while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
    return _x*_tmp;
}
bool equal( D _x ,  D _y ){
    return _x > _y - eps && _x < _y + eps;
}
int __ = 1 , cs;
/*********default*********/
#define N 410
#define K 52
ll n , k , a[ N ];
//map<int,int> dp[ N ][ K ];
ll bstu[ N ][ K ];
ll bstd[ N ][ K ];
//typedef map<int,int>::iterator mi;
void build(){

}
void update_u( ll tn , ll tk , ll ta ){
    if( ta > bstu[ tn ][ tk ] )
        bstu[ tn ][ tk ] = ta;
}
void update_d( ll tn , ll tk , ll ta ){
    if( ta > bstd[ tn ][ tk ] )
        bstd[ tn ][ tk ] = ta;
}
void init(){
    n = getint(); k = getint();
    for( int i = 0 ; i <= n ; i ++ )
        for( int j = 1 ; j <= k ; j ++ )
            bstu[ i ][ j ] = bstd[ i ][ j ] = -INF;
    for( int i = 1 ; i <= n ; i ++ )
        a[ i ] = getint();
}
ll ans;
void solve(){
    for( int i = 1 ; i <= n ; i ++ ){
        for( int j = 1 ; j <= k ; j ++ ){
            update_u( i , j , bstu[ i - 1 ][ j ] );
            update_d( i , j , bstd[ i - 1 ][ j ] );
        }
//            dp[ i ][ j ] = dp[ i - 1 ][ j ];
        ll sum = 0;
        for( ll j = i ; j >= 1 ; j -- ){
            sum += a[ j ];
            for( int tk = 1 ; tk <= min( k - 1 , j - 1 ) ; tk ++ ){
                ll tmpu = -INF , tmpd = -INF , tb;
                if( bstu[ j - 1 ][ tk ] != -INF )
                    tmpu = max( tmpu , bstu[ j - 1 ][ tk ] - sum );
                if( bstd[ j - 1 ][ tk ] != -INF )
                    tmpd = max( tmpd , bstd[ j - 1 ][ tk ] + sum );
                tb = max( tmpu , tmpd );
                if( tb == -INF ) continue;
                if( tk + 1 == k )
                    ans = max( ans , tb );
                update_u( i , tk + 1 , tb + sum );
                update_d( i , tk + 1 , sum - tb );
            }
/*                for( mi it = dp[ j - 1 ][ tk ].begin() ; it != dp[ j - 1 ][ tk ].end() ; it ++ ){
                    dp[ i ][ tk + 1 ][ sum ] = max( dp[ i ][ tk + 1 ][ sum ] , abs( sum - it->FI ) + it->SE );
                    if( tk + 1 == k )
                        ans = max( ans , dp[ i ][ tk + 1 ][ sum ] );
                }*/
        }
        update_u( i , 1 , sum );
        update_d( i , 1 , -sum );
//        dp[ i ][ 1 ][ sum ] = 0;
    }
//    for( int i = 1 ; i <= n ; i ++ )
//        for( int j = 1 ; j <= k ; j ++ )
//            printf( "%d %d : %I64d %I64d\n" , i , j , bstu[ i ][ j ] , bstd[ i ][ j ] );
    printf( "%I64d\n" , ans );
}
int main(){
    build();
//    __ = getint();
    while( __ -- ){
        init();
        solve();
    }
}


#include <cmath>
#include <cstdio>
#include <algorithm>

using namespace std;

const int K = 55;
const int N = 444;

int n, m, a[N], s[N][N], dp[K][N][N];

int main() {

    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }

    for (int i = 0; i < n; i++) {
        s[i][i] = a[i];
        for (int j = i + 1; j < n; j++) {
            s[i][j] = s[i][j - 1] + a[j];
        }
    }

    for (int k = 1; k < m; k++) {
        for (int x = k - 1; x < n; x++) {
            for (int y = x; y < n; y++) {
                for (int z = y + 1; z < n; z++) {
                    dp[k + 1][y + 1][z] = max(dp[k + 1][y + 1][z], dp[k][x][y] + abs(s[x][y] - s[y + 1][z]));
                }
            }
        }
    }

    int ans = 0;

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

    printf("%d\n", ans);

    return 0;

}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>

using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)
typedef long long LL;
typedef pair<int, int> PII;

int n, k;
int a[401];
int mx[401][51], mn[401][51];
const int INF = 1e9;
int sum[401];

int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> k, --k;
    REP(i, n) cin >> a[i];
    sum[0] = 0;
    REP(i, n) sum[i + 1] = sum[i] + a[i];
    REP(i, n + 1) REP(j, k + 1) mn[i][j] = mx[i][j] = -INF;
    for (int i = 1; i < n + 1; ++i) mn[i][0] = -sum[i], mx[i][0] = sum[i];
    int ans = 0;
    REP(i, n) for (int j = 0; j < k; ++j) {
        if (i) {
            mn[i][j] = max(mn[i][j], mn[i - 1][j]);
            mx[i][j] = max(mx[i][j], mx[i - 1][j]);
        }
        if (mn[i][j] != -INF) {
            for (int r = i; r < n; ++r) {
                mn[r + 1][j + 1] = max(mn[r + 1][j + 1], mn[i][j]);
                mx[r + 1][j + 1] = max(mx[r + 1][j + 1], mn[i][j] + 2 * (sum[r + 1] - sum[i]));
            }
            if (j == k - 1) {
                for (int r = i; r < n; ++r) {
                    ans = max(ans, mn[i][j] + sum[r + 1] - sum[i]);
                }
            }
        }
        if (mx[i][j] != -INF) {
            for (int r = i; r < n; ++r) {
                mx[r + 1][j + 1] = max(mx[r + 1][j + 1], mx[i][j]);
                mn[r + 1][j + 1] = max(mn[r + 1][j + 1], mx[i][j] - 2 * (sum[r + 1] - sum[i]));
            }
            if (j == k - 1) {
                for (int r = i; r < n; ++r) {
                    ans = max(ans, mx[i][j] - sum[r + 1] + sum[i]);
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>

using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)
typedef long long LL;
typedef pair<int, int> PII;

int n, k;
int a[405];
int mx[405][55], mn[405][55];
const int INF = 1e9;
int sum[405];

int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> k, --k;
    REP(i, n) cin >> a[i];
    sum[0] = 0;
    REP(i, n) sum[i + 1] = sum[i] + a[i];
    REP(i, n + 1) REP(j, k + 1) mn[i][j] = mx[i][j] = -INF;
    for (int i = 1; i < n + 1; ++i) {
        mn[i][0] = -sum[i], mx[i][0] = sum[i];
        REP(j, i) mn[i][0] = max(mn[i][0], -sum[i] + sum[j]);
        REP(j, i) mx[i][0] = max(mx[i][0], sum[i] - sum[j]);
    }
    int ans = 0;
    REP(i, n) for (int j = 0; j < k; ++j) {
        if (i) {
            mn[i][j] = max(mn[i][j], mn[i - 1][j]);
            mx[i][j] = max(mx[i][j], mx[i - 1][j]);
        }
        if (mn[i][j] != -INF) {
            for (int r = i; r < n; ++r) {
                mn[r + 1][j + 1] = max(mn[r + 1][j + 1], mn[i][j]);
                mx[r + 1][j + 1] = max(mx[r + 1][j + 1], mn[i][j] + 2 * (sum[r + 1] - sum[i]));
            }
            if (j == k - 1) {
                for (int r = i; r < n; ++r) {
                    ans = max(ans, mn[i][j] + sum[r + 1] - sum[i]);
                }
            }
        }
        if (mx[i][j] != -INF) {
            for (int r = i; r < n; ++r) {
                mx[r + 1][j + 1] = max(mx[r + 1][j + 1], mx[i][j]);
                mn[r + 1][j + 1] = max(mn[r + 1][j + 1], mx[i][j] - 2 * (sum[r + 1] - sum[i]));
            }
            if (j == k - 1) {
                for (int r = i; r < n; ++r) {
                    ans = max(ans, mx[i][j] - sum[r + 1] + sum[i]);
                }
            }
        }
    }
    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

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];
int main(){
	int n, k;
	cin >> n >> k;
	FI(400)
		FJ(100){
		dmin[i][j] = dmax[i][j] = { -INF, -INF };
	}
	for (int i = 0; i < n; ++i){
		cin >> a[i];
		s[i + 1] = s[i] + a[i];
		dmin[i][1] = dmax[i][1] = { 0, s[i + 1] };
	}
	for (int st = 2; st < k; ++st){
		for (int i = 1; i < n; ++i){
			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;
}

//
// vector<vi> v;
// int a[100];
// int l[100];
// int r[100];
// int main()
// {
//
// 	{
// 		int n;
// 		cin >> n;
// 		FI(n){
// 			cin >> l[i] >> r[i];
// 			a[i];
// 		}
// 		do{
//
// 		} while (next_permutation(a, a + n));
//
//
// 	}
// 	int n, m;
// 	cin >> n >> m;
// 	FI(n){
// 		a[i] = i + 1;
// 	}
// 	int max_res = 0;
// 	do{
// 		int res = 0;
// 		for (int i = 0; i < n; ++i){
// 			int minel = a[i];
// 			for (int j = i; j < n; ++j){
// 				if (a[j] < a[i])
// 					minel = a[j];
// 				res += minel;
// 			}
// 		}
// 		if (res > max_res){
// 			max_res = res;
// 			v.clear();
// 		}
// 		if (res == max_res){
// 			v.push_back({a, a + n});
// 		}
// 	} while (next_permutation(a, a + n));
// 	for (auto x : v[m - 1]){
// 		cout << x << ' ';
// 	}
// 	cout << 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 = max(pmin.first + sum - pmin.second, pmax.first + 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;
	for (int i = 1; i < n; ++i){
		if (i != 1) upd1(dmax[i - 2][k - 1], dmax[i - 1][k - 1]);
		if (i != 1) upd2(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].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 = 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 < 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;
// }
//


#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;
			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;
// }
//