#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
using namespace std;

int main()
{
	int n, k; cin >> n >> k;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	if (n == k)
	{
		vector<long long> prefix(n);
		prefix[0] = a[0];
		for (int i = 0; i < n; i++)
			prefix[i] = prefix[i - 1] + a[i];

		long long max_prefix = INT_MIN;
		for (int i = 0; i < n - 1; i++)
		{
			long long curr_prefix = abs(prefix[i] - (prefix[n - 1] - prefix[i]));
			max_prefix = max(max_prefix, curr_prefix);
		}
		cout << max_prefix << endl;
	}

	else
	{
		vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(k + 1, vector<int>(2)));


		for (int i = n - 1; i >= 0; i--)
		{
			for (int j = k - 1; j >= 0; j--)
			{
				for (int p = 1; p >= 0; p--)
				{
					if (j == k - 1 && i == n - 1)
						dp[i][j][p] = -a[i] + dp[i + 1][j + 1][!p];
					else if (j == k - 1 && p == 0)
						dp[i][j][p] = max(dp[i + 1][j][p], -a[i] + dp[i + 1][j + 1][!p]);
					else if (p == 0)
					{
						dp[i][j][p] = max(a[i] + dp[i + 1][j + 1][p], max(dp[i + 1][j][p], -a[i] + dp[i + 1][j + 1][!p]));
					}
					else
					{
						dp[i][j][p] = max(dp[i + 1][j][p], -a[i] + dp[i + 1][j + 1][p]);
					}
				}
			}
		}

		cout << dp[0][0][0] << endl;
	}
}