Under construction

Still being built. Things may change

Tourism

Time limit1sMemory limit512 MB

Summary
Split the N attractions, in order, into the fewest days with at most K attractions per day, maximizing the sum of each day's highest score.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Sliding window
Solved
No attempts yet

Problem

You are planning a trip to visit NN tourist attractions. The attractions are numbered from 11 to NN and must be visited in this order. You can visit at most KK attractions per day, and you want to finish the trip in as few days as possible.

Each attraction ii has a score aia_i. For a given schedule, each day scores the maximum score among the attractions visited that day. The total score of the schedule is the sum of the daily scores. Find the maximum total score among the schedules that use the fewest possible days.

Input

The first line contains two space-separated integers NN and KK (1≤K≤N≤1061 \le K \le N \le 10^6).

The next line contains NN space-separated integers aia_i (1≤ai≤1091 \le a_i \le 10^9).

For 3 of the 15 available marks, 2K≥N2K \ge N. For an additional 3 of the 15 available marks, K≤100K \le 100 and N≤105N \le 10^5.

Output

Output a single integer, the maximum possible total score.

Examples1

  1. Example 1

    Input
    5 3
    2 5 7 1 4
    
    Expected output
    12