AlgoUniversity
  • Go Back
Discussion
Pyramid Path :

Author

Gigaliths

Difficulty Level : Medium

Submissions : 601

Asked In : Tibra

Marks :10

: 11 | : 1

Your goal is to find the maximum path value of viable paths in a pyramid. A viable path is one were you take either one step to the left or right as you progress down the nodes of the pyramid. You must pass through every level. The total path value is the sum of the values in the nodes traversed by your path. The diagram below represents a simple 3-layer pyramid with 4 possible paths:

In the above example, the correct answer is 7

The pyramid input will be given in the level order traversal flattened into a 1D array.

For the above example, so you will receive an array [1, 3, -1, 3, 1, 5] as input.

Input

A single letter N corresponding to the size of the array followed by the next line containing N space separated integers corresponding to the cost of the $$$i_{th}$$$ node.

$$$1 \le N \le 1000000 $$$

$$$-10^9 \le cost_i \le 10^9 $$$

It is guaranteed that the number of nodes represents a complete pyramid.

example : 1 , 3 , 6 , 10 ...and so on.

Output

A single integer value corresponding to the maximum cost.

Example

Input
6
1 3 -1 3 1 5
Output
7

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.