All problems

Problem 973

Maximum Product Difference Between Two Pairs

Easy · 10 pts · Array, Sorting

Problem

Choose four elements at different positions and form two pairs (A, B) and (C, D). The product difference is A × B - C × D. Maximise it. All values are positive, so make the first product as large as possible with the two largest elements and the second as small as possible with the two smallest. Sorting works, or track the two largest and two smallest values in one pass.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the maximum product difference.

Constraints

4 <= N <= 10000; 1 <= A[i] <= 10^4

Example input

5
5 6 2 7 4

Example output

34

Explanation

In 5 6 2 7 4, take (6, 7) and (2, 4): the difference is 42 - 8 = 34, the largest possible.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Number of Moves to Seat Everyone Next: Minimum Sum of Four Digit Number After Splitting Digits