#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    vector<pair<int, int>> b(n);
    vector<pair<int, int>> c(k);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) {
        b[i].first = a[i];
        b[i].second = i;
    } sort (b.begin(), b.end());
    for (int i = 0; i < k - 1; i++) {
        c[i].first = b[i].second;
        c[i].second = b[i].first;
    } c[k-1].first = b[n-1].second;
    c[k-1].second = b[n-1].first;
    sort (c.begin(), c.end());

    int i = 0, j = k-1;
    for (int m = 0; m < k-1; m++) {
        if (c[i].second >= c[j].second) {
            c[i].second -= c[j].second;
            j--;
        } else {
            c[j].second -= c[i].second;
            i++; 
        }
    }
    cout << c[i].second;
}