14628 - KuoTA Restaurant 2   

Description

You might need to use long long int to avoid overflow.

Elephants love to eat KuoTA (鍋貼), so Elfnt decided to open a restaurant called "八方來財", serving delicious KuoTA to satisfy their cravings.

After solving the queueing problem, Elfnt now hopes to close the restaurant as early as possible to get some rest. He already knows the dining time of all $n$ elephants today. Please calculate how long the restaurant needs to stay open, from opening to closing.

There are $k$ seats in the restaurant. As long as there is an available seat, the elephant at the front of the queue will immediately be seated and start dining. Once the $i$-th elephant is seated, they will occupy the seat for $x_i$ units of time, after which they will leave the restaurant, freeing up the seat. (At the very beginning, the first $k$ elephants will be seated immediately.)

Input

The first line contains two integers $n$ and $k$, representing the number of elephant and the number of seats.

The second line contains $n$ integers $x_1, x_2, ..., x_n$, where $x_i$ is the amount of time the $i$-th elephant will take to dine.

Constraints

  • $1 \leq n, k \leq 2 \times 10^5$
  • $1 \leq x_j \leq 10^9$

Subtask

  1. (Testcases 1-3) $1 \leq n, k \leq 1000$
  2. (Testcases 4-6) No additional constraints.

Output

Output how long the restaurant needs to stay open. (NO space and '\n' at the end)

Sample Input  Download

Sample Output  Download




Discuss