//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iomanip> 
#include <map>
#include <string>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <deque>

#define f(n) for (int i = 0; i < n; i++)
#define f0(n) for (int j = 0; j < n; j++)
#define co(n) cout << n << "\n";

using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    f(n) cin >> a[i];
    long long r = 0;
    if (n == k)
    {
        long r0 = -a[0];
        long r1 = -a[n - 1];
        f(n - 1)
        {
            r0 += a[i + 1];
            r1 += a[i];
        }
        r = max(r0, r1);
    }
    else
    {
        vector<int> b = a;
        long long rk = 0; 
        int br = 0;
        int l = n - 1, d = 0;
        sort(b.begin(), b.end());
        for(int i = 0; i < n && br < k - 1; i++)
        {
            if (binary_search(b.begin(), b.begin() + k - 1, a[i]) != -1)
            {
                rk += a[i];
                d = max(d, i);
                l = min(l, i);
                br++;
            }
        }
        int m0 = 100000, m1 = 100000;
        for (int i = 0; i < l; i++) m0 = min(m0, a[i]);
        for (int i = d + 1; i < n; i++) m1 = min(m1, a[i]);
        rk -= min(m0, m1);
        r = rk;
    }
    cout << r;
}