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



Sample input:
6
3 1 2 4 2 1


Sample output:
2

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
N = int(input())
arrA = list(map(int,input().split()))
# arrA = np.array(input().split(),dtype=np.int64)

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)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

Solution code:
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]
# print(A)
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)

Submission status: Accepted

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

import collections
c = collections.Counter(a_li)

mc = c.most_common()
# [('a', 4), ('c', 2), ('b', 1)]

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)
#print(all_li)
ans_li = [all_li[0]*all_li[1]]
for s in seihou_li:
    ans_li.append(s**2)
print(max(ans_li))

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)
#print(d)
l=sorted([[i,j] for i,j in d.items() if j>1])
#print(l)
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)

c_list = list(c.items())

#print(c_list)

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import collections
def main():

    n = int(input())
    a = list(map(int,input().split()))
    # a.sort(reverse=True)
    c = collections.Counter(a)
    cl = c.most_common()
    # cl.sort(reverse = True)
    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:
        #     print(lst[0] * lst[1])
        #     return
    if len(lst) >= 2:
        lst.sort(reverse=True)
        ans = max(ans, lst[0] * lst[1])

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
# import bisect
from collections import Counter, deque
# from copy import copy, deepcopy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
# from operator import xor
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

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()

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int, input().split())
from collections import Counter
C=Counter(A)
ca=-1
E=sorted(C.items(), key=lambda x:x[0], reverse=True)
for k,v in E:
    if ca >= 0:
        if v >= 2:
            print(ca*k)
            exit()
    else:
        if v >= 4:
            print(k*k)
            exit()
        elif v >= 2:
            ca = k
print(0)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
A.sort()
A.reverse()
h=int(0)
w=int(0)
i=int(1)
while i<n:
    if A[i]==A[i-1] and h==0:
        h=A[i]
        i+=1
    elif A[i]==A[i-1] and w==0:
        w=A[i]
        i+=1
    i+=1
print(h*w)

Submission status: Accepted

Solution code:
import sys
from heapq import heapify, heappop, heappush

def solve():
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().split()]
    Anum = dict()
    for a in A:
        if a in Anum: Anum[a] += 1
        else: Anum[a] = 1
    q = [0, 0]
    for key in Anum:
        if Anum[key] >= 2: q.append(-key)  
        if Anum[key] >= 4: q.append(-key)
    heapify(q)
    x = heappop(q)
    y = heappop(q)
    print(x * y)


    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

n = INT()
a = LIST()
b = Counter(a)
c = sorted(list(set(a)), reverse=True)
ans = []
for i in c:
    if b[i] >= 2:
        ans.append(i)
    if b[i] >= 4:
        ans.append(i)
    if len(ans) >= 2:
        break
debug(ans)
if len(ans) >= 2:
    print(ans[0] * ans[1])
else:
    print(0)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
from collections import Counter
ctr = Counter(A)
x = None
for k,v in reversed(sorted(ctr.items())):
    if v >= 4:
        if x:
            print(x*k)
        else:
            print(k**2)
        exit()
    if v >= 2:
        if x:
            print(x*k)
            exit()
        x = k
print(0)

Submission status: Accepted

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

S = []
for a, c in A.items():
    S.extend([a] * (c // 2))

S.sort()
if len(S) < 2:
    print(0)
else:
    print(S[-1] * S[-2])

Submission status: Accepted

Solution code:
import collections
n = int(input())
A = collections.Counter([int(x) for x in input().split()])
lst1 = [-1]
lst2 = [-1]
for l, c in A.items():
  if c > 1:
    lst1.append(l)
  if c > 3:
    lst2.append(l*l)
lst1.sort()
if len(lst1)-1>1 or len(lst2)-1>0:
  if len(lst1)-1>1:
    print(max(max(lst2), lst1[-1]*lst1[-2]))
  else:
    print(max(lst2))
else:
  print(0)

Submission status: Accepted

Solution code:
n = input()
l = list(map(int, input().split()))
w, h, temp = 0, 0, 0
cnt = 1
for i in sorted(l, reverse=True):
    if temp == i:
        cnt += 1
        if w == 0:
            w = i
        elif w != i:
            print(w*i)
            exit()
        if cnt == 4:
            print(w*w)
            exit()    
    else:
        cnt = 1
    temp = i
print(0)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
l1 = [0,0]
from collections import Counter
c = Counter(l)
for i in c.items():
  if i[1]>=2 :
    l1.append(i[0])
  if i[1]>=4 :
    l1.append(i[0])
l1.sort()
print(l1[-1]*l1[-2])

Submission status: Accepted

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

c = collections.Counter(A)
cs = sorted(c.items(), key=lambda x:x[0], reverse=True)

area = []
for i in range(len(cs)):
    if(cs[i][1] > 1):
        if(cs[i][1]//2 == 1):
            area.append(cs[i][0])
            if(len(area) == 2):
                print(area[0]*area[1])
                break
        else:
            area.append(cs[i][0])
            if(len(area) > 1):
                print(max(cs[i][0]*cs[i][0], area[0]*area[1]))
                break
            else:
                print(cs[i][0]*cs[i][0])
                break
else:
    print(0)

Submission status: Accepted

Solution code:
# import bisect
from collections import Counter, deque
# from copy import copy, deepcopy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
# from operator import xor
import sys

sys.setrecursionlimit(10 ** 5 + 10)


# input = sys.stdin.readline

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

    AA = Counter(A).most_common()

    ls = []
    for i in range(len(AA)):
        if AA[i][1] >= 2:
            ls.append(AA[i])
        else:
            break

    ls=sorted(ls, key=lambda x: x[0])

    try:
        ans = ls[-1][0] * ls[-2][0]
        for i in reversed(range(len(ls))):
            if ls[i][1] >= 4:
                ans = max(ans, ls[i][0] ** 2)
        print(ans)
    except:
        print(0)


resolve()

Submission status: Accepted

Solution code:
import collections as c

n = int(input())
co = c.Counter(list(map(int, input().split())))
cm = sorted(co.items(), key=lambda x:x[0], reverse=True)
a = 0
b = 0
for v,m in cm:
  if m >= 4:
    if a == 0:
      a = b = v
    else:
      b = v
    break
  elif m >= 2 and a == 0:
    a = v
  elif m >= 2 and a != 0:
    b = v
    break
print(a*b)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
Aset = list(set(A))
Aset.sort(reverse=True)
a, b = 0, 0
L = []
cnt = 1
for i in range(1, N):
    if A[i] == A[i-1]:
        cnt += 1
    else:
        L.append(cnt)
        cnt = 1
if A[-1] == A[-2]:
    L.append(cnt)
idx = 0
for i in range(len(L)):
    if L[i] >= 4:
        a, b = Aset[i], Aset[i]
        break
    elif L[i] >= 2:
        a = Aset[i]
        idx = i + 1
        break
if b <= 0:
    for i in range(idx, len(L)):
        if L[i] >= 2:
            b = Aset[i]
            break
print(a * b)

Submission status: Accepted

Solution code:
# coding: utf-8
N=int(input())
A=list(map(int,input().split()))

A.sort()

L1=0
L2=0
used=[False for i in range(N)]
for i in range(1,N):
    if A[i]==A[i-1] and not used[i-1] and not used[i]:
        if L1<=A[i]:
            used[i-1]=True
            used[i]=True
            L2=L1
            L1=A[i]

print(L1*L2)

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
h = {}
for x in a:
    if x not in h:
        h[x] = 0
    h[x] += 1
b = []
for x in h:
    if h[x]>=2:
        b.append(x)
b.sort()
if len(b)>=1 and h[b[-1]]>=4:
    print(b[-1]*b[-1])
elif len(b)<2:
    print(0)
else:
    print(b[-1]*b[-2])

Submission status: Accepted

Solution code:
import heapq
import collections
N = int(input())
A = list(map(int, input().split()))

table = collections.defaultdict(int)
hq = [0, 0]
for x in A:
    table[x] += 1
    if table[x] == 2:
        heapq.heappush(hq, -x)
        table[x] = 0
print(heapq.heappop(hq) * heapq.heappop(hq))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
dls = []
A.sort()
A.reverse()
now = 0
for i in range(N-1):
  if now >= N-1:
    break
  else:
    if A[now] == A[now+1]:
      dls.append(A[now])
      now += 2
    else:
      now += 1
if len(dls) >= 2:
  print(dls[0]*dls[1])
else:
  print(0)

Submission status: Accepted

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

from collections import Counter
c = Counter(A)

x = 0
y = 0
for k, v in sorted(c.items(), key=lambda x: -x[0]):
    if v >= 4 and x == 0 and y == 0:
        x = y = k
    elif v >= 2:
        if x == 0:
            x = k
        elif y == 0:
            y = k

print(x*y)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
a = list(map(int, input().split()))
d = defaultdict(int)
ans = 0
for i in a:
    d[i] += 1
ans1 = []
for v, c in d.items():
    if c >= 2:
        ans1.append(v)

ans1.sort(reverse=True)
if len(ans1) > 1:
    ans = ans1[0] * ans1[1]

ans2 = []
for v, c in d.items():
    if c >= 4:
        ans2.append(v)

ans2.sort(reverse=True)

if len(ans2) > 0:
    ans = max(ans, ans2[0] * ans2[0])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
cnt = Counter(A)
ans = [0]
l =[]
values, counts = zip(*cnt.most_common())
for v, c in zip(values, counts):
    if c > 3:
        ans.append(v**2)
        l.append(v)
    elif c > 1:
        l.append(v)
l.sort(reverse=True)
if len(l) > 1:
    ans.append(l[0] * l[1])
print(max(ans))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
cnt = Counter(map(int, input().split()))

L = []
for a, c in cnt.items():
    if c >= 2:
        L += [a] * (c // 2)
L.sort()

ans = 0 if len(L) < 2 else L[-1] * L[-2]
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = [int(i) for i in input().split()] 

a.sort(reverse = True)
tmp = []
i = 0
while i < n - 1:
    if a[i] == a[i + 1]:
        tmp.append(a[i])
        i += 2
    else:
        i += 1
if len(tmp) >= 2:
    print(tmp[0] * tmp[1])
else:
    print(0)

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())

A = list(sorted(A, reverse=True))
ans = 1
is_before = False
count = 0
for i in range(N-1):
    if not is_before:
        if A[i] == A[i+1]:
            ans = ans * A[i]
            is_before = True
            count = count + 1
    else:
        is_before = False

    if count>1:
        print ans
        exit()

print 0