#C14357. Maximum Product of Three Numbers

    ID: 43997 Type: Default 1000ms 256MiB

Maximum Product of Three Numbers

Maximum Product of Three Numbers

Given a list of integers, your task is to compute the maximum product attainable by multiplying any three of these numbers. The product is defined as \(a \times b \times c\) for any three chosen integers \(a\), \(b\), and \(c\). There are two candidates to consider:

  • The product of the three largest numbers.
  • The product of the two smallest numbers (which might be negative) and the largest number.

You must output the maximum of these two computed products. If the number of integers provided is less than three, output the error message:

Input list must contain at least three numbers.

inputFormat

The input is provided via standard input (stdin) in the following format:

 n
 a1 a2 a3 ... an

Where n is the number of integers and a1, a2, ..., an are the integer values separated by spaces.

outputFormat

Output the maximum product of any three numbers from the list via standard output (stdout). If the list contains fewer than three integers, output the following error message:

Input list must contain at least three numbers.
## sample
5
1 10 -5 1 -100
5000