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 (Python):
from collections import Counter
def solve():
    n = int(input())
    a = list(map(int, input().split()))

    l = Counter(a)
    tw = []
    fo = []
    for i,j in l.items():
        if j >= 2:
            tw.append(i)
        if j >= 4:
            fo.append(i)
    ans = 0
    if len(tw) >= 2:
        tw.sort()
        ans = max(ans, tw[-1]*tw[-2])
    if fo:
        fo.sort()
        ans = max(ans, fo[-1]**2)
    
    return ans
    

print(solve())

Solution (Python):
import collections
import sys
n = int(input())
a = sorted(list(map(int,input().split())))
c = collections.Counter(a)
s = len(set(a))
c = sorted(c.items(), reverse=True)

for i in range(s):
  if c[i][1]>=4:
    print(c[i][0]**2)
    sys.exit()
  elif c[i][1] >=2:
    for j in range(i+1,s):
      if c[j][1]>=2:
        print(c[i][0]*c[j][0])
        sys.exit()
print("0")

Solution (Python):
import sys
from collections import Counter
N = int(input())
a = list(map(int, input().split()))
li = []
C = sorted(Counter(a).items())
for i in range(len(C)):
    if C[i][1] >= 4:
        li.append(C[i][0])
        li.append(C[i][0])
    elif C[i][1] >= 2:
        li.append(C[i][0])
if len(li) <= 1:
    print(0)
    sys.exit()
print(li[-1]*li[-2])

Solution (Python):
import sys
from collections import Counter
N = int(input())
a = list(map(int, input().split()))
li = []
C = sorted(Counter(a).items())
for i in range(len(C)):
    if C[i][1] >= 4:
        li.append(C[i][0])
        li.append(C[i][0])
    elif C[i][1] >= 2:
        li.append(C[i][0])
if len(li) <= 1:
    print(0)
    sys.exit()
print(li[-1]*li[-2])

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

C = sorted(Counter(A).items(), reverse = True)

edge = []
for k,v in C:
    if len(edge) >=2:
        break
    if v >= 2:
        for _ in range(v // 2):
            edge.append(k)
if len(edge) <= 1:
    print(0)
    exit()
print(edge[0] * edge[1])

Solution (Python):
import collections
A=int(input())
l_after=[]
l=list(map(int,input().split()))
l=collections.Counter(l).most_common()
for i in range(len(l)):
   if l[i][1] > 1:
      l_after.append(l[i][0])
   if l[i][1] > 3:
      l_after.append(l[i][0])
l_after.sort(reverse=True)
if len(l_after) < 2:
   print(0)
   exit()
print(l_after[0]*l_after[1])

Solution (Python):
import collections
a = int(input())
ar = list(map(int,input().split(" ")))
br = collections.Counter(ar)
cr = []
for k,v in br.items():
    if v >= 4:
        cr.append(k)
        cr.append(k)
    elif v >= 2:
        cr.append(k)
cr = sorted(cr,reverse=True)
if len(cr) < 2:
    print(0)
else:
    print(cr[0] * cr[1])

Solution (Python):
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
d = defaultdict(int)

max1, max2 = 0, 0

for a in A:
    d[a] += 1
    if d[a] == 2:
        if a > max1:
            max2 = max1
            max1 = a
        elif a > max2:
            max2 = a

if d[max1] >= 4:
    ans = max1 * max1
else:
    ans = max1 * max2
print(ans)

Solution (Python):
import sys
import collections

n = int(input())
a = collections.Counter(list(map(int, sys.stdin.readline().split())))

q = [0, 0]
for v, k in a.items():
    if k >= 4:
        q.append(v)
    if k >= 2:
        q.append(v)
q.sort()
print(q[-1]*q[-2])

Solution (Python):


from collections import Counter
n = int(input())
nums = [int(i) for i in input().split()]
c = Counter(nums)

ans = 0
rec = []
for k, v in c.items():
    if v >= 2:
        rec.append(k)
    if v >= 4:
        ans = max(ans, k ** 2)

if len(rec) >= 2:
    rec.sort(reverse=True)
    ans = max(ans, rec[0] * rec[1])
print(ans)

Solution (Python):

"""
Created on Tue May 12 01:19:32 2020

@author: shinba
"""
import collections

n = int(input())

A = list(map(int,input().split()))

data = collections.Counter(A)

I = sorted(data.items(),reverse = True)

s1 = 0
s2 = 1
cnt1 = 0
cnt2 = 0 

for i,j in I:
    if cnt1 == 0 and j >= 4:
        s1 = i**2
        cnt1 = 1
    if j >= 2:
        s2 *= i
        cnt2 += 1
    if cnt2 == 2:
        print(max(s1,s2))
        break
else:
    if s1 != 0:
        print(s1)
    else:
        print(0)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
A = Counter(A)

p = []
l = []
for k, v in A.items():
    if v >= 4:
        p.append(k)
    if v >= 2:
        l.append(k)

ans = 0
if len(p) >= 1:
    p.sort()
    ans = p[-1]**2
if len(l) >= 2:
    l.sort()
    ans = max(l[-1] * l[-2], ans)
print(ans)

Solution (Python):


def LI():
    return list(map(int, input().split()))


N = int(input())
A = LI()


def main():
    d = {}
    for a in A:
        d[a] = d[a] + 1 if a in d else 1
    
    l = []
    for k, v in d.items():
        if v >= 2:
            l.append(k)
    
    l.sort(reverse=True)
    if len(l) < 2:
        ans = 0
    elif d[l[0]] >= 4:
        ans = l[0] ** 2
    else:
        ans = l[0] * l[1]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions

import copy





sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))




def main():
    n = ni()
    a = na()

    c = collections.Counter(a)

    e = sorted(c.items(), reverse=True)
    ans_e = []
    cnt = 0
    for k, v in e:
        if cnt >= 2:
            break
        if v > 1:
            if v < 4:
                ans_e.append(k)
                cnt += 1
            else:
                ans_e.append(k)
                ans_e.append(k)
                cnt += 2

    if cnt >= 2:
        print(ans_e[0] * ans_e[1])
    else:
        print(0)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))

c = Counter(A)


ans = [0, 0]
for i, j in c.items():
    if j >= 2:
        ans.append(i)
    if j >= 4:
        ans.append(i)

ans.sort()    

print(ans[-1]*ans[-2])

Solution (Python):
from collections import defaultdict

N = int(input())
X = [int(i) for i in input().split()]
D = defaultdict(int)
for x in X:
    D[x] += 1

B = []
for x in D:
    for _ in range(D[x] // 2):
        B.append(x)
B.sort()
ans = B[-1] * B[-2] if len(B) >= 2 else 0
print(ans)

Solution (Python):
from collections import defaultdict

N = int(input())
X = [int(i) for i in input().split()]
D = defaultdict(int)
for x in X:
    D[x] += 1

B = []
for x in D:
    for _ in range(D[x] // 2):
        B.append(x)
B.sort()
ans = B[-1] * B[-2] if len(B) >= 2 else 0
print(ans)

Solution (Python):
from collections import Counter
N=int(input())
A=Counter(list(map(int,input().split())))
x=[0,0]
for a in A:
    if A[a]>1:x.append(a)
    if A[a]>3:x.append(a)
x.sort()
print(x[-1]*x[-2])

Solution (Python):
import sys
from collections import Counter
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7



def main():
    N = int(readline())
    H = list(map(int, readline().split()))
    count = Counter(H)
    sr_count = sorted(count.items(), key=lambda x:x[0],reverse=True)
    ans = 0
    for kye, value in sr_count:
        if value>=4:
            if ans == 0:
                ans = kye**2
                break
            else:
                ans *= kye
                break
        elif value>=2:
            if ans == 0:
                ans += kye
            else:
                ans *= kye
                break
        else:
            continue
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import collections

n = int(input())
p = list(map(int,input().split()))
cnt = collections.Counter(p)

maxs = []
for k, v in cnt.items():
  if v > 1:
    maxs.append(k)
    
if len(maxs) > 1:
  maxs.sort()
  if cnt[maxs[-1]] > 3:
    print(maxs[-1] ** 2)
  else:
    print(maxs[-1] * maxs[-2])

else:
  print(0)

Solution (Python):
from collections import Counter


def solve():
    N = int(input())
    c = Counter(map(int, input().split()))
    usables = sorted([k for k, v in c.items() if v > 1])

    if len(usables) == 0:
        return 0
    else:
        ans = usables[-1]
        if c[ans] > 3:
            return ans ** 2
        elif len(usables) > 1:
            usables.pop()
            return ans * usables[-1]
        else:
            return 0


print(solve())

Solution (Python):
from collections import Counter


N = int(input())
c = Counter(map(int, input().split()))
usables = sorted([(k, v) for k, v in c.items() if v >= 2])
if len(usables) <= 1:
    print(0)
else:
    if usables[-1][1] >= 4:
        print(usables[-1][0] ** 2)
    else:
        print(usables[-1][0] * usables[-2][0])

Solution (Python):
n,*L = map(int,open(0).read().split())
from collections import Counter
L = Counter(L)
li = []
for k,v in L.items():
    li.append([k,v])
li.sort(reverse = True)
LL = []
for i in range(len(li)):
    if li[i][1] >= 2:
        LL.append(li[i])

if len(LL) == 0:
    print(0)
    exit()
if LL[0][1] >= 4:
    print(LL[0][0]**2)
elif len(LL) >=2:
    print(LL[0][0]*LL[1][0])
else:
    print(0)

Solution (Python):
n = int(input())
li = list(map(int, input().split()))

li.sort(reverse=True)

check = -1
flag = 0
ans = []

for i in li:
    if len(ans) == 2:
        break
    
    if flag == 0:
        check = i
        flag = 1
        continue
    
    if check == i:
        ans.append(i)
        flag = 0
    else:
        check = i

if len(ans) < 2:
    print(0)
else:
    print(ans[0] * ans[1])

Solution (Python):
n =int(input())

a=[int(x) for x in input().split()]

a.sort()

c=b=0

for i in range(n-1):
  if(a[-i-1]==a[-i-2]):
    a[-i-2]=0
    if b==0:
      b=a[-i-1]
    else:
      c=a[-i-1]
      break
      
print(b*c)

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))

c = Counter(a)
sorted_count = sorted(c.items(), key=lambda x: x[0], reverse=True)
h = 0
w = 0
for k, v in sorted_count:
    if h != 0 and w != 0:
        break
    if v < 2:
        continue
    if v > 3:
        if h == 0:
            h = k
        if w == 0:
            w = k
        break
    if h == 0:
        h = k
    elif w == 0:
        w = k

if h == 0 or w == 0:
    print(0)
else:
    print(h * w)

Solution (Python):
import sys
import os
import math
import bisect
import collections

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = collections.Counter(il())
    A = sorted(A.items(), key=lambda x: -x[0])
    o, t = 0, 0
    for k, v in A:
        n = v // 2
        if n == 1 and o == 0:
            o = k
        elif n == 1 and t == 0:
            t = k
        elif n > 1:
            o = max(k, o)
            t = max(k, t)

        if t != 0 and o != 0: break

    print(o * t)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
a = list(map(int,(input().split())))
from collections import Counter
c = Counter(a)
L = []
LL = []
for k in c.keys():
    if c[k] >= 2:
        L.append(k)
    if c[k] >= 4:
        LL.append(k)
M = 0 
if LL:
    M = max(LL)**2
if len(L) < 2:
    print(0)
else:
    L.sort(reverse=True)
    print(max(L[0]*L[1],M))

Solution (Python):
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
p = list(c.items())
p.sort(reverse = True)
tate = 0
yoko = 0
for k , v in p:
    if tate == 0 and v >= 4:
        tate = k
        yoko = k
        break
    elif tate == 0 and 2 <= v <= 3:
        tate = k
    elif tate != 0 and v >= 2:
        yoko = k
        break
    
print(tate*yoko)

Solution (Python):
N = int(input())
ls = list(map(int,input().split()))
import collections
c = collections.Counter(ls)
unique_ls = list(reversed(sorted(([i[0] for i in c.items() if i[1] >= 2]))))

if len(unique_ls) >=2:
  if ls.count(unique_ls[0]) >=4:
    print(unique_ls[0]**2)
  else:
    print(unique_ls[0]*unique_ls[1])
elif len(unique_ls) ==1:
  if ls.count(unique_ls[0]) >=4:
    print(unique_ls[0]**2)
  else:
    print(0)
else:
  print(0)

Solution (Python):
N = int(input())
ls = list(map(int,input().split()))
import collections
c = collections.Counter(ls)
unique_ls = list(reversed(sorted(([i[0] for i in c.items() if i[1] >= 2]))))

square_max = 0
rectan_a = 0
rectan_b =0

if len(unique_ls) >=2:
  if ls.count(unique_ls[0]) >=4:
    print(unique_ls[0]**2)
  else:
    print(unique_ls[0]*unique_ls[1])
elif len(unique_ls) ==1:
  if ls.count(unique_ls[0]) >=4:
    print(unique_ls[0]**2)
  else:
    print(0)
else:
  print(0)

Solution (Python):
def myAnswer(N:int,A:list) -> int:
   dic = {}
   for a in A:
      if(a in dic.keys()):
         dic[a]+=1
      else:
         dic[a] = 1
   ans = []
   for key,value in dic.items():
      if(value >= 4):
         ans.append(key)
         ans.append(key)
      elif(value >= 2):
         ans.append(key)
   if(len(ans) <= 1):
      return 0
   ans.sort(reverse = True)
   return ans[0]*ans[1]

def modelAnswer():
   return
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))
if __name__ == '__main__':
   main()

Solution (Python):


from collections import deque,Counter













N = int(input())
arrA = list(map(int,input().split()))


cnt = Counter(arrA)
ans = 0
lis = sorted(list(cnt.items()))
n = len(lis)

shrt = 0
lng = 0
for i in range(n):
    if lis[i][1]>=4:
        shrt = lis[i][0]
        lng = lis[i][0]
        ans = max(ans,lng*shrt)
    elif shrt == 0 and lis[i][1]>=2:
        shrt = lis[i][0]
    elif lis[i][1]>=2:
        lng = lis[i][0]
        ans = max(ans,lng*shrt)
        shrt = lng
    else:
        pass

print(ans)

Solution (Python):
n = int(input())
As = list(map(int, input().split()))

from collections import defaultdict
d = defaultdict(int)

for a in As:
    d[a] += 1

cnt = 0
ans = []
for k in sorted(list(d.keys()),reverse=True):
    if d[k] > 1:
        cnt += 1
        ans.append(k)
    if cnt == 2:break


if len(ans) < 2:
    A = 0
else:
    A = ans[1]*ans[0]

cnt_sqr = 0
ans = []
for k in sorted(list(d.keys()),reverse=True):
    if d[k] > 3:
        cnt_sqr += 1
        ans.append(k)
    if cnt == 1:break

if len(ans) < 1:
    A = max(A,0)
else:
    A = max(A,ans[0]**2)
print(A)

Solution (Python):
n = int(input())
a_li = list(map(int,input().split()))

import collections
c = collections.Counter(a_li)

mc = c.most_common()


if len(mc) == 1 and mc[0][1]<=3:
    print(0)
    exit()
all_li = []
seihou_li = []
for m in mc:
    if m[1]>=2:
        all_li.append(m[0])
    if m[1]>3:
        seihou_li.append(m[0])
if len(all_li) <= 1:
    print(0)
    exit()
all_li.sort(reverse=True)

ans_li = [all_li[0]*all_li[1]]
for s in seihou_li:
    ans_li.append(s**2)
print(max(ans_li))

Solution (Python):
from collections import deque

def main():
    N=int(input())
    A=[int(x) for x in input().split()]
    
    dictionary={}
    for num in A:
        if num in dictionary:
            dictionary[num]+=1
        else:
            dictionary[num]=1
    
    ans=0
    d=deque()
    
    for key,value in sorted(dictionary.items()):
        if value>=2:
            d.append(key)
            if value>=4:
                d.append(key)
    
    d=sorted(d,reverse=True)
    if len(d)==0:
        ans=0
    else:
        ans=d[0]*d[1]
    print(ans)

if __name__=="__main__":
    main()

Solution (Python):
n, *a = map(int, open(0).read().split())

a.sort(reverse=True)

width = 0
length = 0
flg = False
cnt = 0
for i in range(n - 1):
    if flg:
        flg = False
        continue
    
    if a[i] == a[i + 1]:
        if cnt == 0:
            width = a[i]
        elif cnt == 1:
            length = a[i]
            break
        
        flg = True
        cnt += 1

print(width * length)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import collections

n = ni()
a = na()

c = collections.Counter(a)

k = 0
for i,j in sorted(c.items())[::-1]:
    if j < 2:
        continue
    if k > 0:
        print(k*i)
        exit()
    elif j >= 4:
        print(i*i)
        exit()
    else:
        k = i

print(0)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
A.sort(reverse = True)
ans = 1
i = 0
cnt = 0
while i < n-1:
    if A[i] == A[i+1]:
        ans *= A[i]
        i += 2
        cnt += 1
    else:
        i += 1
    if cnt == 2:
        print(ans)
        exit(0)
print(0)

Solution (Python):
n=int(input())
from collections import Counter as co
var=co(list(map(int,input().split()))).items()
f,s=0,0
for k,v in var:
	if v>=4:
		if f<k:
			f,s=k,max(k,f)
		else:
			s=max(s,k)
	if v>=2:
		if f<k:
			f,s=k,f
		else:s=max(s,k)
print(f*s)

Solution (Python):
from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
ans = Counter(a)
b = sorted(ans)
ans1 = []
for i in b[::-1]:
    if ans[i]>=2 and ans[i]<4:
        ans1.append(i)
    elif ans[i]>=4:
        ans1.append(i)
        ans1.append(i)
        
if len(ans1)>=2:
    sqr = ans1[0]*ans1[1]
    print(sqr)
else:
    print(0)

Solution (Python):
from collections import*;a=Counter(list(map(int,open(0).read().split()[1:])));x=[0,0]
for i in a.keys():x+=[i]*((a[i]>1)+(a[i]>3))
x.sort();print(x[-1]*x[-2])

Solution (Python):
def mycounter(d,i):
    if i in d:
        d[i]+=1
    else:
        d[i]=1
    return d

n=int(input())
a=list(map(int,input().split()))
d=dict()
for i in a:
    d=mycounter(d,i)

l=sorted([[i,j] for i,j in d.items() if j>1])

if l==[] or (len(l)==1 and l[-1][1] < 4):
    print(0)
else:
    print(l[-1][0]**2 if l[-1][1]>3 else l[-1][0]*l[-2][0])

Solution (Python):
n = int(input())
a = list(map(int,input().split()))

a.sort(reverse=True)
c = 0
v = []
for i in range(n-1):
    if a[i] != a[i+1]:
        c += 1
        if c >= 2:
            v.append([a[i],c])
        c = 0
    else:
        c += 1
if a.count(a[-1]) >=2 :
    v.append([a[-1],a.count(a[-1])])
if len(v) == 0:
    print(0)
elif v[0][1] >= 4:
    print(v[0][0]**2)
elif len(v) >= 2:
    print(v[0][0]*v[1][0])

Solution (Python):
n = int(input())
a = list(map(int,input().split()))

a.sort(reverse=True)
c = 0
v = []
for i in range(n-1):
    if a[i] != a[i+1]:
        c += 1
        if c >= 2:
            v.append([a[i],c])
        c = 0
    else:
        c += 1
if a.count(a[-1]) >=2 :
    v.append([a[-1],a.count(a[-1])])
if len(v) == 0:
    print(0)
elif v[0][1] >= 4:
    print(v[0][0]**2)
elif len(v) >= 2:
    print(v[0][0]*v[1][0])
else:
    print(0)

Solution (Python):
n=int(input())
a=sorted((list(map(int,input().split()))),reverse=True)
con=1
sta=a[0]
check=0
for i in range(1,n):
  if a[i]==sta:
    con+=1
    if con==2:
      check+=1
      x=sta
      a.remove(sta)
      a.remove(sta)
      break
  else:
    con=1
    sta=a[i]
sta=a[0]
con=1
for i in range(1,n-2):
  if a[i]==sta:
    con+=1
    if con==2:
      check+=1
      y=sta
      break
  else:
    con=1
    sta=a[i]
print(x*y if check==2 else "0")

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int, input().split()))

c = Counter(a)

c_list = list(c.items())



tmp_list = []
for tmp in c_list:
    if tmp[1] >= 2:
        tmp_list.append(tmp)


tmp_list.sort(reverse=True, key=lambda x: x[0])

if len(tmp_list) == 0 or (len(tmp_list) == 1 and tmp_list[0][1] < 4):
    print(0)
else:
    if tmp_list[0][1] < 4:
        print(tmp_list[0][0]*tmp_list[1][0])
    else:
        print(tmp_list[0][0]**2)

Solution (Python):
from collections import Counter

n = int(input())
a = Counter(list(map(int,input().split())))

m1 = 0

key = sorted(a.keys(), reverse = True)

for i in key:

    if a[i]>1 and m1==0:
        m1 = i
        if a[i]>3:
            print(i**2)
            exit()

    elif m1!=0 and a[i]>1:
        print(m1*i)
        exit()

print(0)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort()
A.reverse()
ans_lis = []
count = 1
before = A[0]
for i in range(1,N):
  tmp = A[i]
  if tmp == before:
    count += 1
  else:
    if count == 2 or count == 3:
      ans_lis.append(before)
    elif count >= 4 and len(ans_lis) == 0:
      ans_lis.append(before)
      ans_lis.append(before)
    elif count >= 4 and len(ans_lis) != 0:
      ans_lis.append(before)
    count = 1
  if len(ans_lis) == 2:
    break
  before = tmp
if len(ans_lis) == 0:
  if A[N-1] == A[N-2] and A[N-2] == A[N-3] and A[N-3] == A[N-4]:
    ans_lis.append(A[N-1])
    ans_lis.append(A[N-1])
elif len(ans_lis) == 1:
  if A[N-1] == A[N-2]:
    ans_lis.append(A[N-1])
if len(ans_lis) < 2:
  print(0)
else:
  print(ans_lis[0] * ans_lis[1])

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a=sorted(a,reverse=True)
bef=a[0]
ren=1
c=0 
hen1=0
hen2=0
for i in range(1,n):
    if bef==a[i]:
        ren+=1
    else:
        if 2<=ren:
            if c==0:
                hen1=bef
                if 4<=ren:
                    hen2=bef
                    c=2
                    break
                c=1
            else:
                hen2=bef
                break
        bef=a[i]
        ren=1
if 2<=ren:
    if c==0:
        if ren<=4:
            hen1,hen2=bef,bef
    elif c==1:
        hen2=bef
print(hen1*hen2)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
b=-1
ans=[0,0]
for ai in a:
  if len(ans)==4:
    break
  if b==ai:
    ans.append(ai)
    b=-1
  else:
    b=ai
print(ans[-1]*ans[-2])

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

cnt = {}
for a in A:
    if a not in cnt:
        cnt[a] = 1
    else:
        cnt[a] += 1

A = [[k, v] for k, v in cnt.items()]
A.sort(reverse=True)

h = 0
w = 0
for k, v in A:
    if v >= 4 and h == 0:
        h = k
        w = k
        break
    elif v >= 2 and h > 0:
        w = k
        break
    elif v >= 2 and h == 0:
        h = k

print(h * w)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
cnt = {}
for x in a:
    if x not in cnt:cnt[x] = 1
    else:cnt[x] +=1
res = []
for key in cnt:
    v  = cnt[key]
    if v>=2:
        res.append(key)
    if v >=4:res.append(key)
res.sort(reverse = True)
if len(res) < 2:ans=0
else:
    ans = res[0]*res[1]
print(ans)

Solution (Python):
from collections import Counter
m1=0
N=int(input())
A=Counter(list(map(int,input().split())))
key=sorted(A.keys(),reverse=True)
for i in key:
  if A[i]>1 and m1==0:
    m1=i
    if A[i]>3:
      print(i**2)
      exit()
  elif m1!=0 and A[i]>1:
    print(m1*i)
    exit()
print(0)

Solution (Python):
N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
s = set()
U = []
ans = 0
for a in A:
    if a in s:
        U.append(a)
        if len(U) >= 2:
            ans = U[0]*U[1]
            break
        s.remove(a)
    else:
        s.add(a)
print(ans)

Solution (Python):
import collections
N = int(input())
A = list(map(int, input().split()))
d = [[x, y] for x, y in collections.Counter(A).items() if y > 1]
d.sort(reverse=True)
if len(d) > 0:
    if d[0][1] >= 4:
        print(d[0][0]**2)
    elif len(d) > 1:
        print(d[0][0]*d[1][0])
else:
    print(0)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))

from collections import Counter
F = sorted(Counter(A).items(), key=lambda x: x[0], reverse=True)
edge= []

for k, v in F:
  if v >= 4:
    edge.extend([k, k])
  elif v >= 2:
    edge.append(k)
  if len(edge) >= 2: break

if len(edge) < 2:
  ans = 0
else:
  ans = edge[0] * edge[1]
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
Ac = Counter(list(map(int, input().split())))

ans = []
for k in sorted(Ac.keys())[::-1]:
  if len(ans) == 2:
    break
  if Ac[k] >=4:
    if ans:
      ans.append(k)
    else:
      ans.append(k)
      ans.append(k)
  elif Ac[k] >= 2:
    ans.append(k)
if len(ans) == 2:
  print(ans[0]*ans[1])
else:
  print(0)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
import collections
c=collections.Counter(a)
t=list(c.items())
t.sort(reverse=True)
s=c.most_common()
if s[0][1]<4 and s[1][1]<2:
  print(0)
  exit()
while t[0][1]<2:
  del t[0]
if t[0][1]>=4:
  print(t[0][0]**2)
else:
  while t[1][1]<2:
    del t[1]
  print(t[0][0]*t[1][0])

Solution (Python):
import collections

N = int(input())
A = list(map(int, input().split()))

CA = collections.Counter(A)

max1 = 0
max2 = 0
for item in CA.items():
    if item[1] >= 2 and item[0] > max1:
        if item[1] >= 4:
            max1 = item[0]
            max2 = item[0]
        else:
            max2 = max1
            max1 = item[0]
    elif item[1] >= 2 and item[0] > max2:
        max2 = item[0]
print(max1*max2)

Solution (Python):
import collections
def main():

    n = int(input())
    a = list(map(int,input().split()))
    
    c = collections.Counter(a)
    cl = c.most_common()
    
    cnt = 1
    lst = []
    now = a[0]
    ans = 0
    for i, cc in enumerate(cl):
        if cc[1] >= 4:
            ans = max(ans,cc[0]**2)


        if cc[1] >= 2:
            lst.append(cc[0])
        
        
        
    if len(lst) >= 2:
        lst.sort(reverse=True)
        ans = max(ans, lst[0] * lst[1])

    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
tate = 0
yoko = 0
i = 0
while i < (N - 1):
    if A[i] == A[i + 1]:
        if tate == 0:
            tate = A[i]
        else:
            yoko = A[i]
        if tate > 0 and yoko > 0:
            break
        i += 1
    i += 1
print(tate * yoko)

Solution (Python):
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
c=Counter(a)
l4,l2=[],[]
for k,v in c.items():
    if v>=4:l4.append(k)
    if v>=2:l2.append(k)
l4.sort(reverse=True)
l2.sort(reverse=True)
if len(l4)<=2:l4+=[0,0]
if len(l2)<=2:l2+=[0,0]
print(max(l4[0]*l4[1],l4[0]*l2[0],l2[0]*l2[1]))

Solution (Python):

from collections import Counter, deque







import sys
sys.setrecursionlimit(10 ** 5 + 10)


def resolve():

    N=int(input())
    A=sorted(list(map(int,input().split())),reverse=True)
    temp=-1
    maxans=0
    AA=Counter(A)
    for (i,j) in AA.items():
        if j>=4:
            maxans=max(i**2,maxans)
        if j>=2:
            if temp>=0:
                maxans=max(maxans,temp*i)
            temp=max(i,temp)
    print(maxans)




resolve()

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

from collections import Counter

Acnt = Counter(A)

ans = [-1] * 2
for key,cnt in Acnt.items():
    if cnt >= 4:
        if key > ans[0]:
            ans[0] = key
        if key > ans[1]:
            ans[1] = key
    elif cnt >= 2:
        if key > ans[0]:
            ans[0] = key
    
    ans.sort()

if -1 in ans:
    print(0)
else:
    print(ans[0] * ans[1])

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 0
side = []
a.sort(reverse=True)
last = a[0]
for i in range(1, n):
    if last == a[i]:
        side.append(a[i])
        last = 0
    else:
        last = a[i]
    
    if len(side) == 2:
        ans = side[0] * side[1]
        break

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
a = ""
ans = []
for i in range(N):
  if a == "": a = A[i]
  elif a != A[i]: a = A[i]
  elif A[i] == a:
    ans.append(A[i])
    a = ""
    if len(ans) == 2: break
print(0 if len(ans) != 2 else ans[0] * ans[1])

Solution (Python):
import collections
n = int(input())
a = list(map(int,input().split()))
a = collections.Counter(a)
a = list(a.items())
a.sort(reverse=True)
e1 = 0
e2 = 0
for i in range(len(a)):
    if a[i][1] >= 4 and e1 == 0:
        e1 = e2 = a[i][0]
        break
    elif a[i][1] >= 2:
        if e1 == 0:
            e1 = a[i][0]
        else:
            e2 = a[i][0]
            break
print(e1*e2)

Solution (Python):
from collections import Counter
n = int(input())
l = list(map(int,input().split()))
a = Counter(l)
b = a.most_common()
c = []
for i,j in b:
    if j>=4:
        c.append(i)
        c.append(i)
    elif j>=2:c.append(i)
c.sort()
print(c[-1]*c[-2] if len(c)>=2 else 0)

Solution (Python):
from collections import Counter
def main():
    n = int(input())
    a = list(map(int,input().split()))
    count = Counter(a)
    ans = 0
    o2 = []
    for k, v in count.items():
        if v >= 2:o2.append(k)
        if v >= 4:ans = max(ans,k*k)
    o2.sort()
    if len(o2) >= 2:ans = max(ans,o2[-1] * o2[-2])

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from collections import Counter
    c = Counter(A)
    mc = c.most_common()
    fi = mc[0]
    se = mc[1]
    if mc[0][1] < 2:
        return print(0)
    elif mc[1][1] < 2 and mc[0][1] < 4:
        return print(0)

    
    four = [k for k, v in mc if v >= 4]
    two = [k for k, v in mc if v >= 2]
    ans1, ans2 = 0, 0
    if four:
        ans1 = max(four)**2
    two.sort()
    if len(two) >= 2:
        ans2 = two[-1]*two[-2]
    print(max(ans1, ans2))


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)
tmp = 0
tate = 0
yoko = 0
cnt = 0
for i in A:
    if tmp == i and cnt == 0:
        tate = i
        cnt += 1
        tmp = 0
        continue
    if tmp == i and cnt == 1:
        yoko = i
        cnt += 1
        break
    tmp = i

print(tate*yoko)

Solution (Python):
n = int(input())
A =list(map(int, input().split()))
d = {}
for i in range(n):
    if A[i] not in d:
        d[A[i]] = 1
    else:
        d[A[i]] += 1

s = []
t = []
for k, v in d.items():
    if v >= 4:
        s.append(k)
        t.append(k)
    elif v >= 2:
        t.append(k)

if len(s) == 0 and len(t) < 2:
    print(0)
else:
    if len(s) == 0:
        t.sort(reverse=True)
        print(t[0]*t[1])
    elif len(t) < 2:
        s.sort(reverse=True)
        print(s[0]**2)
    else:
        t.sort(reverse=True)
        s.sort(reverse=True)
        print(max(t[0]*t[1], s[0]**2))