CF BUDDY
← Problems·

1862E · Kolya and Movie Theatre

1600 · constructive algorithms, data structures, greedy

Problem: Recently, Kolya found out that a new movie theatre is going to be opened in his city soon, which will show a new movie every day for nn days. So, on the day with the number 1in1 \le i \le n, the movie theatre will show the premiere of the ii-th movie. Also, Kolya found out the schedule of the movies and assigned the entertainment value to each movie, denoted by aia_i.

However, the longer Kolya stays without visiting a movie theatre, the larger the decrease in entertainment value of the next movie. That decrease is equivalent to dcntd \cdot cnt, where dd is a predetermined value and cntcnt is the number of days since the last visit to the movie theatre. It is also known that Kolya managed to visit another movie theatre a day before the new one opened — the day with the number 00. So if we visit the movie theatre the first time on the day with the number ii, then cntcnt — the number of days since the last visit to the movie theatre will be equal to ii.

For example, if d=2d = 2 and a=[3,2,5,4,6]a = [3, 2, 5, 4, 6], then by visiting movies with indices 11 and 33, cntcnt value for the day 11 will be equal to 10=11 - 0 = 1 and cntcnt value for the day 33 will be 31=23 - 1 = 2, so the total entertainment value of the movies will be a1d1+a3d2=321+522=2a_1 - d \cdot 1 + a_3 - d \cdot 2 = 3 - 2 \cdot 1 + 5 - 2 \cdot 2 = 2.

Unfortunately, Kolya only has time to visit at most mm movies. Help him create a plan to visit the cinema in such a way that the total entertainment value of all the movies he visits is maximized.

Input Format: Each test consists of multiple test cases. The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains three integers nn, mm, and dd (1n21051 \le n \le 2 \cdot 10^5, 1mn1 \le m \le n, 1d1091 \le d \le 10^9).

The second line of each set of input data contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (109ai109-10^9 \le a_i \le 10^9) — the entertainment values of the movies.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output a single integer — the maximum total entertainment value that Kolya can get.

Note: The first test case is explained in the problem statement.

In the second test case, it is optimal not to visit any movies.

In the third test case, it is optimal to visit movies with numbers 22, 33, 55, 66, so the total entertainment value of the visited movies will be 4562+161+3962+1161=6045 - 6 \cdot 2 + 1 - 6 \cdot 1 + 39 - 6 \cdot 2 + 11 - 6 \cdot 1 = 60.

Sample Cases

Case 1

Input

6
5 2 2
3 2 5 4 6
4 3 2
1 1 1 1
6 6 6
-82 45 1 -77 39 11
5 2 2
3 2 5 4 8
2 1 1
-1 2
6 3 2
-8 8 -2 -1 9 0

Output

2
0
60
3
0
7

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback