Reach N

5

4 votes
Algorithms, Introduction to Dynamic Programming-2, Dynamic Programming
Problem

You are given an array a consisting of n integers. In one move, you can jump from your current index i to some index j if,

  1. i<j
  2. jn
  3. (ji)=(2K) (where K0) (i.e difference between the current index and next index should be equal to some power of 2)

You will start from index 1 and your task is to reach index n maximizing the sum of the path.

Sum of the path:

Let the indices that you go through while going from index 1 to index n are 1,i2,i3,...,n. So, sum of the path here is equal to a[1]+a[i2]+a[i3]+...+a[n].

Input Format:

  •  The first line contains one integer T  the number of test cases. Then T test cases follow.
  • The first line of each test case contains a single integer n  the number of elements in array a.
  • The second line of each test case contains n integers  the elements of the array a.

Output Format:

For each test case, output in a seperate line:

Maximum sum of path if you start from index 1 and stop at index n.

 Constraints:

  • 1T105
  • 1N106
  • 109ai109 for all i from 1 to n.
  • It is guaranteed that sum of N over all test cases doesn't exceed 106
     
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

To get the maximum path sum, we can go through the following indices in this order: 1245

The path is valid as difference between every consecutive jumps is a perfect power of 2.

Editor Image

?