#import <iostream>
#import <cmath>
using namespace std;

int main(){
    int n, k;
    cin >> n >> k;
    int niz[n] = {};
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        niz[i] = x;
    }
    int l = 0;
    int r = 0;
    for(int i = 0; i < k - 1; i++){
        if(niz[l] > niz[k - r - 1]){
            niz[k - r - 1]  = niz[k - r - 1] - niz[l];
            l++;
        }else{
            niz[l] = niz[l] - niz[k - r - 1];
            r++;
        }
    }
    cout << abs(niz[l]);
    return 0;
}