from collections import deque, defaultdict, Counter from bisect import bisect_left, bisect_right from itertools import permutations, combinations from heapq import heappop, heappush import math, sys # input = sys.stdin.readline _int = lambda x: int(x)-1 MOD = 998244353 #10**9+7 INF = 1<<60 Yes, No = "Yes", "No" N, K = map(int, input().split()) A = list(map(int, input().split())) while A and len(A) > K: if A[-1] < 0: A.pop() else: break N = len(A) cum = [0] for a in A: cum.append(cum[-1]+a) ans = 0 cur = 0 for i in range(K-1): cnt = A[cur] cur += 1 while A[cur] < 0 and K-i-1 < N-cur-1: cnt += A[cur] cur += 1 ans += cnt*(i+1) ans += (max(cum[cur+1:N+1])-cum[cur])*K print(ans)