Sequence Cost

 MoEngage has given you an array 

A of N integers and a positive integer C.

You can apply the following operation on the array any number of times. In one operation:

  • Choose a strictly increasing array B of length M such that:
    • B1=1,BM=N, and 1<Bi<N for all (1<i<M);
    • ABi>0 for all (1≤i≤M).
  • Subtract 1 from ABi for all (1≤i≤M).
    The cost of this operation is ∑i=1(M−1)(Bi+1−Bi)2.

Note that, M can be different for different operations.

Let K be the total cost after performing X number of operations on the array. Your goal is to maximize the value of (C⋅X−K).

Input Format

  • First line contains a single integer T, denoting the number of test cases.
  • For each of the T test cases, the first line contains two integers N and C.
  • The next line contains N space-separated integers denoting A1,A2,…,AN.

Output Format

For each test case, output a single integer - the maximum value of (C⋅X−K).

Constraints

  • 1≤T≤104
  • 2≤N≤2⋅105
  • 1≤C,Ai≤109
  • Sum of N over all test cases does not exceed 2⋅105.

Sample Input 1 

3
3 3
3 1 5
7 5
5 5 5 4 1 2 1
2 10
3 3

Sample Output 1 

1
0
27

Explanation

  • Test case 1: Given N=3, A=[3,1,5], C=3.
    It is optimal to apply only 1 operation with B=[1,2,3]. The cost for the operation would be (2−1)2+(3−2)2=2. The value of X=1 and the total cost K=2. Thus, (C⋅X−K)=(3⋅1−2)=1.
    After the operation, the array becomes A=[2,0,4].

  • Test case 2: Given N=7, A=[5,5,5,4,1,2,1], C=5.
    It can be proven that it is never optimal to apply any operation. Hence, the answer will be (5⋅0−0)=0.

  • Test case 3: Given N=2, A=[3,3], C=10.
    It is optimal to apply 3 operations on the array. In each operation, it is optimal to take B=[1,2]. The cost for each operation would be (2−1)2=1. Thus, X=3 and total cost K=1+1+1=3. Thus, (C⋅X−K)=(10⋅3−3)=27.
    After all the operations, the array becomes A=[0,0].

Previous
Next Post »