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 = N-1 for i in reversed(range(1, K)): cnt = A[cur] cur -= 1 while A[cur] >= 0 and i <= cur: cnt += A[cur] cur -= 1 ans += cnt*(i+1) ans += cum[cur+1] print(ans)