#include <bits/stdc++.h>
using namespace std;

int N, K;
long long res, sub;

int main() {
  cin >> N >> K;
  for(int i = 0; i < N; ++i) {
    int a; cin >> a;
    if(i < K && (N - i) <= K) sub += a;
    else res += min(0, a);
  }
  res += min(0, sub);
  cout << res;
}
