Special equations

0

0 votes
, Dynamic Programming, Algorithms
Problem

Bob and three of his friends, James, Mark, and John, are standing in a row at the entrance of a cave to explore special equations (they stand in order as mentioned).

The cave contains n roomes and the ith room contains an integer number ai.

John did not see any special equations before. So, Bob will show some special equations to John.

Initially, Bob has four numbers w, x, y, and z. He will take the first number, James will take the second number, Mark will take the third number, and John will take the last number.

They will enter the cave and everyone will take a number from any room. No one can take Bob's number until everyone in front of him takes his number and they cannot go back again.

After they leave the cave, everyone will multiply his initial number with the number he chose in the cave then the special equation will be computed.

Suppose that the final numbers after multiplying will be w1, x1, y1, and z1, then the magical equation will be: 

x1 - w1 + z1 - y1

Help Bob to know what is the maximum answer he can get from the special equation.

Formally, you are required to select 4 indices 1i1<i2<i3<i4n to maximize ai1×w+ai2×xai3×y+ai4×z.

Input format

  • The first line contains one integer n denoting the number of rooms 
  • The second line contains n space-separated integers.
  • The last line contains four integers w, x, y, and z.

Output format

Print the maximum answer that Bob can get from the special equation.

Constraints

4n105

1ai106

1w,x,y,z109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first test case, at first, they will enter the cave as follow: Bob then James then Mark, and John at the end of the row.

Now everyone has a number Bob has 1, James has 2, Mark has 2, and John has 1.

After they enter the cave Bob will take the number 3 which occurs in the last room, then James will take the number 4 which occurs in the fourth room, Mark will take the number 1 which occurs in the third room, and the last one John will take 5 which occurs in the first room.

So after that, they will leave the cave and everyone will multiply his initial number with the number he chose in the cave then,

 w1 will be 3 * 1 =3 (the initial number multiplying with the number that Bob choose in the cave), 

 x1 will be 4 * 2 =8,   

 y1 will be 1 * 2 =2, 

 z1 will be 5 * 1 =5,

Now compute the equation : x1 - w1 + z1 - y1 which will be (8 - 3 ) + (5 - 2) = 8.

Editor Image

?