Problem:
Score : 300 points

Problem Statement

We have N sticks with negligible thickness.
The length of the i-th stick is A_i.

Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides.
Find the maximum possible area of the rectangle.

Constraints

4 \leq N \leq 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible area of the rectangle.
If no rectangle can be formed, print 0.

Sample Input 1

6
3 1 2 4 2 1

Sample Output 1

2

1 \times 2 rectangle can be formed.

Sample Input 2

4
1 2 3 4

Sample Output 2

0

No rectangle can be formed.

Sample Input 3

10
3 3 3 3 4 4 4 5 5 5

Sample Output 3

20

Solution in Scala (2.11.7) (judge verdict: Wrong Answer)

import scala.collection.SortedMap
import scala.io.StdIn

object Main extends App {

  val n = StdIn.readLine().toInt
  val a = StdIn.readLine().split(" ")
  val r = a.foldLeft(SortedMap.empty[Int, Int]) { (acc, s) =>
    val key = s.toInt
    if (acc.contains(key)) acc.updated(key, acc(key) + 1)
    else acc + (key -> 1)
  }.filter(p => 2 <= p._2)

  val rev = r.keys.foldLeft(List.empty[(Int, Int)]) { (acc, i) =>
    (i, r.getOrElse(i, 0)) :: acc
  }
  val ans = if (rev.isEmpty) {
    0
  } else {
    //println(rev)

    def loop(h: Int, w: Int, sticks: List[(Int, Int)]): Int = {
      if (sticks.isEmpty) {
        h * w
      } else if (h < 0 || w < 0) {
        if (sticks.head._2 >= 4 && h < 0) {
          loop(sticks.head._1, sticks.head._1, sticks.tail)
        } else if (sticks.head._2 >= 4 && h > 0) {
          loop(h, sticks.head._1, sticks.tail)
        } else if (sticks.head._2 >= 2 && h < 0) {
          loop(sticks.head._1, -1, sticks.tail)
        } else if (sticks.head._2 >= 2 && h > 0) {
          loop(h, sticks.head._1, sticks.tail)
        } else {
          loop(h, w, sticks.tail)
        }
      } else {
        h * w
      }
    }

    loop(-1, -1, rev)
  }

  println(ans)
}