Increase Subarray Sums solution codeforces – What is the maximum total area the two posters can cover together if you make the optimal moves?

Increase Subarray Sums solution codeforces

You are given an array 𝑎1,𝑎2,,𝑎𝑛a1,a2,…,an, consisting of 𝑛n integers. You are also given an integer value 𝑥x.

Let 𝑓(𝑘)f(k) be the maximum sum of a contiguous subarray of 𝑎a after applying the following operation: add 𝑥x to the elements on exactly 𝑘k distinct positions. An empty subarray should also be considered, it has sum 00.

Note that the subarray doesn’t have to include all of the increased elements.

Calculate 𝑓(𝑘)f(k) for all 𝑘k from 00 t𝑛n.

Initially, all panels hang down from the bar (their top edges lie on it), but before placing the two posters, you are allowed to move each panel up by any integer length, as long as it is still connected to the bar (its bottom edge lies below or on it).

After the moves are done, you will place two posters: one below the bar and one above it. They are not allowed to go over the bar and they must be positioned completely inside of the panels.

What is the maximum total area the two posters can cover together if you make the optimal moves? Note that you can also place a poster of 00 area. This case is equivalent to placing a single poster.

Increase Subarray Sums solution codeforces

The first line of input contains one integer 𝑛n (1𝑛1041≤n≤104) — the number of vertical panels.

The second line of input contains 𝑛n integers 1,2,...,𝑛h1,h2,…,hn (1𝑖10121≤hi≤1012) — the heights of the 𝑛n vertical panels.

Output

Print a single integer — the maximum total area the two posters can cover together.

Input

The first line contains a single integer 𝑡t (1𝑡50001≤t≤5000) — the number of testcases.

The first line of the testcase contains two integers 𝑛n and 𝑥x (1𝑛50001≤n≤50000𝑥1050≤x≤105) — the number of elements in the array and the value to add.

The second line contains 𝑛n integers 𝑎1,𝑎2,,𝑎𝑛a1,a2,…,an (105𝑎𝑖105−105≤ai≤105).

The sum of 𝑛n over all testcases doesn’t exceed 50005000.

Output

For each testcase, print 𝑛+1n+1 integers — the values of 𝑓(𝑘)f(k) for all 𝑘k from 00 to 𝑛n.

Increase Subarray Sums solution codeforces

3
4 2
4 1 3 2
3 5
-2 -7 -1
10 2
-6 -1 -2 4 -6 -1 -4 4 -5 -4

output

Copy
10 12 14 16 18
0 4 4 5
4 6 6 7 7 7 7 8 8 8 8

Increase Subarray Sums solution codeforces

In the first testcase, it doesn’t matter which elements you add 𝑥x to. The subarray with the maximum sum will always be the entire array. If you increase 𝑘k elements by 𝑥x𝑘𝑥k⋅x will be added to the sum.

In the second testcase:

  • For 𝑘=0k=0, the empty subarray is the best option.
  • For 𝑘=1k=1, it’s optimal to increase the element at position 33. The best sum becomes 1+5=4−1+5=4 for a subarray [3,3][3,3].
  • For 𝑘=2k=2, it’s optimal to increase the element at position 33 and any other element. The best sum is still 44 for a subarray [3,3][3,3].
  • For 𝑘=3k=3, you have to increase all elements. The best sum becomes (2+5)+(7+5)+(1+5)=5(−2+5)+(−7+5)+(−1+5)=5 for a subarray [1,3][1,3].

Leave a Comment