cswork / OAOpen practice workspace

AmazonOA #332

Reduce Memory Usage

EasyJudge-ready

Topics (AI-labelled)Two PointersPrefix & Ranges

A data center has N processes, and processes[i] is the memory used by process i. You must delete exactly m processes at consecutive positions from the list so that the total memory used by the remaining processes is as small as possible. Output this minimum total.

Input

The first line contains two integers N and m. The second line contains N integers processes[0..N-1], separated by whitespace. 2≤N≤3×10^6, 2≤m≤99999 and m≤N, 2≤processes[i]≤999999999.

Output

Output one integer: the minimum total remaining memory after deleting a contiguous segment of m processes. The answer may exceed the 32-bit integer range.

Example 1

Input:

5 2
10 4 8 13 20

Output:

22

Example 2

Input:

4 3
5 9 2 7

Output:

5

Example 3

Input:

2 2
3 3

Output:

0

Example notes

Example 1: delete 13 and 20, leaving 10+4+8=22. Example 2: delete a contiguous segment of length 3 from [5,9,2,7]; deleting 5,9,2 leaves 7 and deleting 9,2,7 leaves 5, so the answer is 5. Example 3: when N=m=2, everything must be deleted, leaving 0.

Editorial

Loading solution…