#include <bits/stdc++.h>

using namespace std;

long long N, K, a[200005], br, br2;

int main () {
    cin >> N >> K;
    for (int i = 1; i <= N; i++) cin >> a[i];

    br = 1;
    br2 = K;
    while (br < br2){
         if (a[br] >= a[br2]){
             a[br] -= a[br2];
             br2--;
         }
         else {
             a[br2] -= a[br];
             br++;
         }
    }
    cout << a[br] << endl;

return 0;
}