#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<long long> a(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  long long ans = 0, sum = 0;
  bool flag = false;
  for (int i = 0; i < n - k; ++i) {
    if (a[i] > 0) {
      ans += a[i];
      if (i == n - k - 1) flag = true;
    }
  }
  for (int i = n - k; i < n; ++i) {
    sum += a[i];
  }
  if (flag) {
    ans += max(0LL, max(sum, sum - a[n - 1]));
  } else {
    ans += max(0LL, max(sum, a[n - 1]));
  }
  cout << ans << endl;
  return 0;
}
