Maximise Sum
Practice
4.6 (13 votes)
Dynamic programming
Easy
Two dimensional
Problem
33% Success 2842 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Given an array A of N numbers, and an initially empty array P, you can do operations of 4 types on it:

  1. Take the last number of array A, remove it and add it to P.
  2. Take the last two numbers of array A (if size of array is greater than 1), remove them and add their product to P.
  3. Reverse array A, take the last number of array A, remove it and add it to P.
  4. Reverse array A, take the last two numbers of array A (if size of array is greater than 1), remove them and add their product to P.

Note that after each operation, array A becomes smaller and maybe reversed. You need to continue adding elements to array P until the array A has 0 elements. You need to maximise the sum of all the values in array P.

Input:

First line contains a single integer N. Next line contains N space separated integers denoting the array A.

Output:

Output the maximum sum of all elements of array P that can be obtained by doing the 4 operations as described above on the array A.

Constraints:

\(1 \le N \le 10^3\)

\(1 \le A_i \le 10^6\)

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:30
12 votes
Tags:
CombinatoricsMathematicsDynamic Programming2D dynamic programmingAlgorithms
Points:30
5 votes
Tags:
AlgorithmsDynamic ProgrammingEasyOpenTwo dimensional
Points:30
8 votes
Tags:
AlgorithmsDynamic Programming2D dynamic programming