Incrementing Subarrays
Practice
4.6 (5 votes)
Dynamic programming
Algorithms
2d dynamic programming
Problem
33% Success 459 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given an array \(A\) of length \(N\). You are also given an integer, \(K\). You can increment all elements of any subarray of \(A\) by \(K\), at most once.

Let us define the score of \(A\) as the sum of \(A[ i ] \% A[ i - 1]\), for all \(i\) in the range \([1, N - 1]\). Find the maximum possible score you can obtain.

Input Format:

  • The first line of input contains a single integer \(T\), which denotes the number of test cases.
  • The first line of each testcase contains two integers, \(N\) and \(K\), denoting the length of the array \(A\), and the increment amount respectively.
  • The second line of each testcase contains \(N\) integers, denoting the array \(A\).

Output Format:

For each test case, print the maximum possible score you can obtain.

Constraints:

\(1 <= T <= 10\)

\(2 <= N <= 10^5\)

\(1 <= A[i], K <= 10^9\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
4 votes
Tags:
Dynamic ProgrammingC++2D dynamic programmingAlgorithms
Points:50
1 votes
Tags:
2D dynamic programmingDynamic ProgrammingAlgorithms
Points:50
4 votes
Tags:
2D dynamic programmingMathamaticsAlgorithmsData StructuresDynamic Programming