import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] inputs = br.readLine().split(" ");
        int n = Integer.parseInt(inputs[0]);
        int k = Integer.parseInt(inputs[1]);
        String[] parseS = br.readLine().split(" ");
        int[] nums = new int[parseS.length];
        for(int i = 0; i < nums.length;i++) {
            String s = parseS[i];
            nums[i] = Integer.parseInt(s);
        }
        long oldms = System.currentTimeMillis();
        List<int[]> subsets = new ArrayList<>();
        int[] s = new int[k];

        if (k <= nums.length) {
            for (int i = 0; (s[i] = i) < k - 1; i++);
            subsets.add(getSubset(nums, s));
            for(;;) {
                int i;
                for (i = k - 1; i >= 0 && s[i] == nums.length - k + i; i--);
                if (i < 0) {
                    break;
                }
                s[i]++;
                for (++i; i < k; i++) {
                    s[i] = s[i - 1] + 1;
                }
                subsets.add(getSubset(nums, s));
            }
        }
        int[] results = new int[subsets.size()];
        for(int i = 0; i < subsets.size();i++)
        {
            int[] array = subsets.get(i);
            int res = 0;
            for(int j = 1;j < array.length-1;j++)
            {
                res += array[j];
            }
            res += Math.abs(array[0]-array[array.length-1]);
            results[i] = res;
            //System.out.println(results[i]);
            //System.out.println(Arrays.toString(array));
        }
        int max = 0;
        int index = 0;
        for(int i = 0; i < results.length;i++)
        {
            if(results[i] > max)
            {
                index = i;
                max = results[i];
            }
        }
        //.out.println(Arrays.toString(subsets.get(index)));
        System.out.println(max);
        //System.out.println(System.currentTimeMillis()-oldms + " ms");
    }

    public static int[] getSubset(int[] input, int[] subset) {
        int[] result = new int[subset.length];
        for (int i = 0; i < subset.length; i++)
            result[i] = input[subset[i]];
        return result;
    }
}