#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>

using namespace std;


bool inSet[100'001];

bool pair_sort_comp(const pair<int, int> &p1, const pair<int, int> &p2) {
	return p1.first > p2.first;
}

int main() {


	int n, k;
	cin >> n >> k;
	vector<int> arr1(n);
	vector<pair<int, int>> arr(n);

	for (int i = 0; i < n; i++) {
		cin >> arr1[i];
		arr[i].first = arr1[i];
		arr[i].second = i;
	}


	if (n == k) {
		int s = 0;
		for (auto x : arr1)
			s += x;

		cout << max(s - 2*arr1[0], s - 2*arr1[n-1]);
		return 0;
	} else if (n <= 1000) { //(((((((((((((((((((((((())))))))))))))))))))))))
		int ans = 0;

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

			int curr = -arr1[i];

			vector<int> arr2 = arr1;
			sort(arr2.begin() + i + 1, arr2.end(), greater<int>());
			for (int j = 1; j < k; j++) {
				curr += arr2[j+i];
			}

			ans = max(curr, ans);

		}


		for (int i = n-1; i >= k-1; i--) {

			int curr = -arr1[i];

			vector<int> arr2 = arr1;
			sort(arr2.begin(), arr2.end() - (n - i));

			// cout << endl << " | ";
			// for (auto x : arr2)
			// 	cout << x << " ";
			// cout << "| " << endl;

			for (int j = 1; j < k; j++) {
				curr += arr2[i - j];
			}

			ans = max(curr, ans);

		}


		cout << ans << endl;
		return 0;

	}


	int ans = 0;

	vector<int> resLeft(n);

	sort(arr.begin(), arr.end(), pair_sort_comp);

	// add first K to set
	int cr = 0;
	int currSum = 0;

	set<int> ms;
	for (int i = 0; i < k-1; i++) {
		ms.insert(arr[i].second); // {0, 1}
		currSum += arr[i].first; // 9
	}
	int si = k - 1; // 2

	// loop from 1 to n-k

	for (int i = 0; i < n - k + 1; i++) {
		if (ms.count(i)) {
			// cout << "i = " << i << endl;
			// cout << "currSum = " << currSum << endl;
			ms.erase(ms.find(i)); // ms = {2}
			currSum -= arr1[i]; // = 3

			ms.insert(arr[si].second); // ms = {2, 4}
			currSum += arr[si].first; // = 6 
			// cout << "currSum = " << currSum << endl;
			// cout << "--------------" << endl;
			si++; // 4
			resLeft[cr] = max(resLeft[cr], currSum - arr1[i]); // res[cr] = 2
		} else {
			resLeft[cr] = max(resLeft[cr], currSum - arr1[i]);
		}
		cr++;
	}

	for (auto x : resLeft) {
		ans = max(ans, x);
	}

	// check if a[i] is in set
	// if it is, remove it from the set, and add second greatest to set
	// if it is also in the set, do the same again with third greatest...


	// right to left


	vector<int> resRight(n);
	cr = 0;
	currSum = 0;
	set<int> ms2;
	for (int i = 0; i < k-1; i++) {
		ms2.insert(arr[i].second); // {0}
		currSum += arr[i].first; // 5
	}
	si = k - 1; // 2

	for (int i = n-1; i >= k-1; i--) {
		//cout << i << " s " << currSum <<  endl;
		if (ms2.count(i)) {
			// cout << "i = " << i << endl;
			// cout << "currSum = " << currSum << endl;
			ms2.erase(ms2.find(i)); // ms = {2}
			currSum -= arr1[i]; // = 3

			ms2.insert(arr[si].second); // ms = {2, 4}
			currSum += arr[si].first; // = 6 
			// cout << "currSum = " << currSum << endl;
			// cout << "--------------" << endl;
			si++; // 4
			resRight[cr] = max(resRight[cr], currSum - arr1[i]); // res[cr] = 2
		} else {
			resRight[cr] = max(resRight[cr], currSum - arr1[i]);
			//cout << " _ " << arr1[i] << endl;
		}
		cr++;
	}

	for (auto x : resRight) {
		ans = max(ans, x);
	}


	cout << ans << endl;


	return 0;
}