#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  long long int ans = 0;
  if (n < k) {
    printf("0");
    return 0;
  }
  long long int a[n];
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < n - k; ++i) {
    if (a[i] > 0) ans += a[i];
  }
  long long int cnt = 0, sum1 = 0, sum2 = 0;
  for (int i = n - k - 1; a[i] <= 0; --i) ++cnt;
  for (int i = n - 1; i >= n - k; --i) {
    if (a[i] > 0 && cnt > 0) sum1 += a[i];
    sum2 += a[i];
    --cnt;
  }
  if (sum1 > sum2)
    ans += sum1;
  else
    ans += sum2;
  printf("%lld", ans);
  return 0;
}
