AlgoUniversity
  • Go Back
Discussion
Maximum Sum Path :

Author

Rishabh Verma

Difficulty Level : Easy

Submissions : 318

Asked In : Sprinklr

Marks :10

: 3 | : 0

You are given an array A of length N consisting of distinct elements.

You need to construct a Binary Search Tree(BST) by inserting each element one by one from array A, starting from the first element till last.

Determine the maximum path sum from root node to leaf node of the BST after each new element is inserted.

Input

First line contains a single integer n (size of array or number of nodes) such that ($$$1 \le n \le 20000$$$).

Second line contains n space separated distinct integers $$$A_i$$$ where ($$$ 1 \le A_i \le 20000$$$).

Output

Print the maximum path sum from root node to leaf node of the BST.

Examples

Input
3
1 2 3
Output
6
Input
6
40 80 20 50 60 70
Output
300

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.