Problems > Best Product of Three

Best Product of Three

Difficulty: 1400 3000 ms 128 MB

math sorting

Choose three elements whose product is maximal and print that product. Values may be negative.

Input: the first line has N (N ≥ 3); the second line has N integers.

Output: the maximum product of any three elements.

Constraints:

  • 3 ≤ N ≤ 105
  • −104 ≤ ai ≤ 104

Sample Tests

Input 1
5
-10 -10 1 3 2
Output 1
300
Input 2
3
1 2 3
Output 2
6

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.