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 $$i_{th}$$ room contains an integer number $$a_i$$.
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 $$w_1$$, $$x_1$$, $$y_1$$, and $$z_1$$, then the magical equation will be:
$$x_1$$ - $$w_1$$ + $$z_1$$ - $$y_1$$
Help Bob to know what is the maximum answer he can get from the special equation.
Formally, you are required to select 4 indices $$1 \le i_1 < i_2 < i_3 < i_4 \le n$$ to maximize $$-a_{i_1} \times w + a_{i_2} \times x - a_{i_3} \times y + a_{i_4} \times 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
$$4 \le n \le 10^5$$
$$1 \le a_i \le {10^6}$$
$$1 \le w, x, y, z \le {10^9}$$
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,
$$w_1$$ will be 3 * 1 =3 (the initial number multiplying with the number that Bob choose in the cave),
$$x_1$$ will be 4 * 2 =8,
$$y_1$$ will be 1 * 2 =2,
$$z_1$$ will be 5 * 1 =5,
Now compute the equation : $$x_1$$ - $$w_1$$ + $$z_1$$ - $$y_1$$ which will be (8 - 3 ) + (5 - 2) = 8.
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
Login to unlock the editorial
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