#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long n,k,rez=-100000000000000000;
	vector <long long> a;
	vector <long long> b;
	cin>>n>>k;
	for(long long i=0;i<n;i++) 
	{
		long long pomocna;
		cin>>pomocna;
		if(i<k-1) b.push_back(pomocna);
		a.push_back(pomocna);
	}
	if(n==k)
	{
		long long rez;
		for(long long i=0;i<n;i++) rez+=a[i];
		rez = rez - 2 * (min (a[0],a[n-1]));
		cout<<rez;
	}
	else
	{
		sort(b.begin(),b.end());
		long long prvi=a[0];
		long long sum=0;
		long long najmanji=b[0];
		for(int i=0;i<k-1;i++) sum+=b[i];
		long long DokleSMostigli=0;
		for(int i=k-1;i<n;i++)
		{
			long long cnt;
			cnt = sum -  min (a[i],prvi);
			rez = max(rez,cnt);
			if(a[i]>najmanji)
			{
				if(prvi==najmanji)
				{
					b[0]=a[i];
					najmanji = min(b[0],b[1]);
					while(prvi<najmanji)
					{
						DokleSMostigli++;
						prvi = a[DokleSMostigli];
					}
					
				}
				b[0]=a[i];
				najmanji = min(b[0],b[1]);
			}
		}
		cout<<rez;
	}
}