PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000031. Làm việc theo nhóm (Teamwork)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Làm việc theo nhóm (Teamwork)

Teamwork

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN cows in a fixed order, and cow ii has skill sis_i. A farmer wants to divide them into teams.

Each team is a consecutive segment containing at most KK cows. Every cow in a team receives the maximum skill of any member of that team. A team of size tt with maximum skill vv contributes t×vt\times v points.

Partition all cows to maximize the total score.

Input

The first line contains NN and KK.

The next NN integers, possibly on multiple lines, describe the cows’ skills in order.

Output

Print the maximum total score after partitioning the cows into teams.

Subtasks

  • Subtask 1 (20 points): 1≤N≤201\le N\le 20; 1≤K≤1031\le K\le 10^3; 1≤si≤1051\le s_i\le 10^5.
  • Subtask 2 (30 points): 1≤N≤5001\le N\le 500; 1≤K≤1031\le K\le 10^3; 1≤si≤1051\le s_i\le 10^5.
  • Subtask 3 (50 points): 1≤N≤1041\le N\le 10^4; 1≤K≤1031\le K\le 10^3; 1≤si≤1051\le s_i\le 10^5.

Examples

Input

7 3
1
15
7
9
2
5
10

Output

84

Explanation

Partition into (1,15,7)(1,15,7), (9)(9), and (2,5,10)(2,5,10). Their sizes are 33, 11, and 33, all at most K=3K=3. Their contributions are 3×15=453\times15=45, 1×9=91\times9=9, and 3×10=303\times10=30, for an optimal total of 8484.