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:
from collections import Counter

N = int(input())
A=list(map(int,input().split()))
c=dict(Counter(A))
#print(c)
e=[]
f=[]

for i in c:
  if c[i]>1:
    e.append(i)
  if c[i]>=4:
    f.append(i)

e.sort(reverse=True)
f.sort(reverse=True)
if len(f)>=1:
  if f[0]>=e[0]:
    print(f[0]**2)
  else:
    if len(e)>=2:
      print(e[0]*e[1])
    else:
      print(0)
else:
  if len(e)>=2:
    print(e[0]*e[1])
  else:
    print(0)

Submission status: Accepted

Solution code:
import collections
 
n = int(input())
lens = list(map(int,input().split()))
lens = collections.Counter(lens)
lens = sorted(lens.items(), key=lambda x:x[0], reverse = True)
cnt = 0
for j in lens:
  if j[1] >= 4 and cnt == 0:
    print(j[0]**2)
    break
  elif j[1] >= 2 and cnt == 0:
    cnt = j[0]
  elif j[1] >= 2 and cnt != 0:
    print(cnt * j[0])
    break
else:
    print(0)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
a = collections.Counter(A)
ans = [0, 0]
for i in a.keys():
    if a[i] >= 2:
        ans.append(i)
    if a[i] >= 4:
        ans.append(i)
ans.sort()
print(ans[-1]*ans[-2])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
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])

Submission status: Accepted

Solution code:
N = int(input())
import collections
A = list(map(int,input().split()))
C = collections.Counter(A)
use = []
for i,val in C.items():
  if val >= 4:
    use.append(i)
  if val >= 2:
    use.append(i)

use = sorted(use,reverse= True)
if len(use)<2:
  print(0)
else:
  print(use[0]*use[1])

Submission status: Accepted

Solution code:
N = int(input())
import collections
A = list(map(int,input().split()))
C = collections.Counter(A)
use = [0,0]
for i,val in C.items():
  if val >= 4:
    use.append(i)
  if val >= 2:
    use.append(i)

use.sort(reverse=True)
 
print(use[0] * use[1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
def main():

    n = int(input())
    a = list(map(int,input().split()))
    cnt = collections.Counter(a)
    set_a = set(a)
    a = []
    a = list(set_a)
    a.sort(reverse=True)
    select = 0
    ans = 1
    tmp = 0
    flg = 0
    for i, aa in enumerate(a):
        if cnt[aa] >= 4 and flg == 0:
            tmp = aa * aa
            flg = 1
        if cnt[aa] >= 2:
            select += 1
            ans *= aa
        if select == 2:
            ans = max(tmp,ans)
            print(ans)
            return
    print(0)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

f,t=[],[]
for k in a:
    if a[k]>=4:
        f.append(k)
    if a[k]>=2:
        t.append(k)
# print('f:',f)
# print('t:',t)
ans1=0
for ff in f:
    ans1=max(ans1,ff**2)
ans2=0
t.sort(reverse=True)
if len(t)>=2:
    ans2=t[0]*t[1]
print(max(ans1,ans2))

Submission status: Accepted

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

ans = []
s = A[0]
n = 1
for i in range(1, N):
  if len(ans) == 2: break
  if A[i] == s:
    n += 1
    if n == 2 or n == 4:
      ans.append(s)
  else:
    s = A[i]
    n = 1
print(ans[0] * ans[1] if len(ans) != 0 else 0)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
ac = sorted([(i, v) for i, v in Counter(A).items() if v >= 2], reverse=True)
s = []
for k, v in ac:
  for _ in range(v//2):
    s.append(k)
  if len(s) >= 2:
    break
if len(s) >= 2:
  print(s[0]*s[1])
else:
  print(0)

Submission status: Accepted

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

edge1 = 0
edge2 = 0
start = 0

prev = A[start]
for i in range(start + 1, N):
    if A[i] == prev:
        edge1 = prev
        start = i + 1
        break
    prev = A[i]

prev = A[start]
for i in range(start + 1, N):
    if A[i] == prev:
        edge2 = prev
        break
    prev = A[i]

print(str(edge1 * edge2))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
scount=1
flg=0
ans=1
for i in range(1,N):
    if A[i]==A[i-1]:
        scount+=1
        if i==N-1 and scount>=2 and flg==1:
            flg=2
            ans*=A[i-1]
        if i==N-1 and scount>=4 and flg==0:
            flg=2
            ans=A[i-1]**2
    else:
        if scount>=4 and flg==0:
            flg=2
            ans=A[i-1]**2
            break
        elif scount>=2:
            if flg==0:
                flg=1
                ans*=A[i-1]
            elif flg==1:
                flg=2
                ans*=A[i-1]
                break
        scount=1

print(ans if flg==2 else 0)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
A = sorted(A, reverse=True)
d = defaultdict(int)
for i in range(N):
  d[A[i]] += 1
d = sorted(d.items(), key=lambda x:x[1], reverse=True)
d = sorted(d, key=lambda x:x[0], reverse=True)
a = []
for key, val in d:
  if 2 <= val <= 3:
    a.append(key)
  elif val >= 4:
    a.append(key)
    a.append(key)
  if len(a) >= 2:
    print(a[0]*a[1])
    exit()
if len(a) < 2:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

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

A = sorted(A)[::-1]
a = []
ng = 9999999
for i in range(N-1):
    if i != ng:
        if A[i] == A[i+1]:
            a.append(A[i])
            ng = i+1
            if len(a) == 2:
                break

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

print(ans)

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
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 input(): return sys.stdin.readline().strip()
def INT(): return int(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()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

c = Counter(A)
c = sorted(c.items(), key=lambda x:x[0], reverse=True)
d = []
for a in c:
    # print(a)
    if a[1] >= 2:
        d.append(a)
# print(d)
d = sorted(d, key=lambda x:x[0], reverse=True)
squ = 0
for y in d:
    # print(y)
    if y[1] >= 4:
        squ = (y[0] ** 2)
    
    break
ans = squ

if len(d) >= 2:
    
    
# if len(d) >= 2:
    ans = (max(d[0][0] * d[1][0], ans))


print(ans)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = [int(a) for a in input().split()]
    A.sort()

    Adict = dict()
    for i, a in enumerate(A):
        if a in Adict: Adict[a] += 1
        else: Adict[a] = 1

    rect = 0
    ri = -1
    for i in reversed(range(N)):
        if Adict[A[i]] >= 4:
            rect = A[i] ** 2
            break
        if Adict[A[i]] >= 2:
            rect = A[i]
            ri = i
            break
    if ri > -1:
        for i in reversed(range(ri)):
            if Adict[A[i]] >= 2 and A[i] != rect:
                rect *= A[i]
                break
    print(rect)

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
bar = dict()
for i in range(n):
    if a[i] not in bar:
        bar[a[i]] = 0
    bar[a[i]] += 1
l_bar = list(bar.items())
l_bar.sort(reverse=True)
cnt = 0
hen1 = 0
hen2 = 0
for i in range(len(l_bar)):
    if l_bar[i][1] >= 4 and cnt == 0:
        print(l_bar[i][0]**2)
        exit()
    if l_bar[i][1] >= 2:
        if cnt == 0:
            cnt += 1
            hen1 = l_bar[i][0]
        elif cnt == 1:
            cnt += 1
            hen2 = l_bar[i][0]
    if cnt == 2:
        print(hen1*hen2)
        exit()
print(0)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
C = Counter(A)
T = []
F = []
for e in C:
    if C[e] >= 4:
        F.append(e)
    if C[e] >= 2:
        T.append(e)
ans = 0
for e in F:
    ans = max(ans,e**2)
T = sorted(T)
if len(T) >= 2:
    ans = max(ans,T[-1]*T[-2])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
from collections import Counter
C = Counter(A)
e = []
for k,v in C.items():
    if v >= 2: e.append(k)
    if v >= 4: e.append(k)
e.sort(reverse=True)
if len(e) >= 2:
    print(e[0]*e[1])
else:
    print(0)

Submission status: Accepted

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

a = sorted(a, reverse = True)
for i in range(n - 1):
    if a[i] == a[i + 1]:
        ans1 = a[i]
        tmp1 = i + 1
        break
    if i == n - 2:
        ans1 = 0
if ans1 == 0:
    print(0)
    exit()
for i in range(tmp1+1, n-1):
    if a[i] == a[i + 1]:
        ans2 = a[i]
        break
    if i == n - 2:
        ans2 = 0
print(ans1 * ans2)

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)
    most = c.most_common()
    if most[0][1] >= 4:
        ans = []
        for k, v in most:
            if v >= 4:
                ans.append(k)
                ans.append(k)
            elif v >= 2:
                ans.append(k)
        ans.sort()
        print(ans[-1]*ans[-2])
    elif most[0][1] >= 2 and most[1][1] >= 2:
        ans = []
        for k, v in most:
            if v >= 2:
                ans.append(k)
        ans.sort()
        print(ans[-1]*ans[-2])
    else:
        print(0)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

cntA = Counter(A)
B = []
for a, c in cntA.items():
    if c // 2 == 0:
        continue
    for _ in range(c // 2):
        B.append(a)

B.sort(reverse=True)
ans = B[0] * B[1] if len(B) >= 2 else 0

print(ans)

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
ans = 0
l = []
for k, v in d.items():
    if v >= 4:
        ans = max(ans, k**2)
        l.append(k)
    elif v >= 2:
        l.append(k)
    else:
        continue
if len(l) <= 1:
    print(ans)
else:
    l.sort(reverse=True)
    ans = max(ans, l[0]*l[1])
    print(ans)

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
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 input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(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()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n = INT()
a = LIST()
a.sort(reverse=True)
b = list(set(a))
b.sort(reverse=True)
c = [1 for _ in range(len(b))]
j = 0
for i in range(n - 1):
    if a[i] == a[i + 1]:
        c[j] += 1
    else:
        j += 1
ans = []
for i in range(len(b)):
    if c[i] >= 2:
        ans.append(b[i])
    if c[i] >= 4:
        ans.append(b[i])
if len(ans) >= 2:
    print(ans[0] * ans[1])
else:
    print(0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

N = int(input().strip())
A_list = list(map(int, input().rstrip().split()))
#-----

count_A_dic={}
for size in A_list:
    count_A_dic.setdefault(size,0)
    count_A_dic[size] += 1

length = sorted(set(A_list),reverse=True)

side=[]
for l in length:
    if count_A_dic[l] >= 4 and len(side) == 0:
        side = [l,l]
        break

    elif count_A_dic[l] >= 2 and len(side) < 2:
        side.append(l)
    
    if len(side) == 2:
        break

print( side[0] * side[1] if len(side)==2 else 0 )

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
C = Counter(A)
ans = sorted([(k,v) for k,v in C.items() if v>=2], key = lambda x:-x[0])[:2]

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
a_count = sorted(Counter(a).items(),reverse=True)
f = False

for k,v in a_count:
    if (4 <= v) and (not f):
        print (k**2)
        exit ()
    elif 2 <= v:
        if f:
            print (x*k)
            exit ()
        else:
            x = k
            f = True
    else:
        pass

print (0)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
a.sort()
a.reverse()
tate = 0
yoko = 0
now = 0
#print(a)
for i in a:
    if tate == 0:
        if now == i:
            tate = now
            now = 0
            continue
    else:
        if now == i:
            yoko = now
            break
    now = i

print(tate * yoko)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
import collections
c =collections.Counter(a)
ln = [i[0] for i in c.items() if i[1] >= 2]
ln.sort(reverse = True)
if len(ln) < 1:
    print(0)
elif c[ln[0]] >= 4:
    print(ln[0] ** 2)
elif len(ln) <= 1:
    print(0)
else:
    print(ln[0] * ln[1])

Submission status: Accepted

Solution code:
import collections
N=int(input())
A=list(map(int,input().split()))
l=[]
c=collections.Counter(A)
a=[]
values=list(c.values())
key=list(c.keys())
b=[]
for i in range(len(key)):
    l.append([key[i],values[i]])
l.sort(key=lambda x:x[1],reverse=True)
for i in range(len(l)):
    if l[i][1]>=2:
        num=l[i][0]
        b=[num]*((l[i][1])//2)
        a.extend(b)
if len(a)<2:
    print("0")
else:
    a.sort(reverse=True)
    print(a[0]*a[1])

Submission status: Accepted

Solution code:
from collections import Counter

n=int(input())
a=Counter(list(map(int,input().split())))
x=[0,0]
for i in a.keys():
    if a[i]>1:
        x.append(i)
    if a[i]>3:
        x.append(i)
x.sort()
print(x[-1]*x[-2])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
bars = [0,0]
for i in range(n-1):
  if a[i + 1] == a[i]:
    bars.pop(0)
    bars.append(a[i])
    a[i+1] = 0
print(bars[0]*bars[1])

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
pre=-1
cnt=0
ans=0
for i in range(n):
    a=A[i]
    if a==pre:
        cnt+=1
        pre=-1
        if ans ==0:
            ans+=a
        else:
            ans*=a
    else:
        pre=a
    if cnt==2:
        break
print(ans)

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  a = list(input_list())
  ac = collections.Counter(sorted(a))
  aa = []
  hw = []
  for v, count in ac.items():
    if count >= 2:
      aa.append(v)
    if count >= 4:
      hw.append(v)
      
  sa = sorted(aa, reverse=True)
  shw = sorted(hw, reverse=True)
  if len(shw) > 0:
    if len(sa) < 2:
      print(shw[0]*shw[0])
    else:
      print(max(shw[0]*shw[0], sa[0]*sa[1]))
    exit()
  
  if len(sa) < 2:
    print(0)
    exit()
  
  print(sa[0]*sa[1])
    
    
  
def input_list():
  return map(int, input().split())

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c 

import math
import fractions
import collections
from functools import reduce
main()

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]

from collections import Counter

d=Counter(a)
d=d.most_common()
ans = [i for i,j in d if j>=2]
ans2 = [i for i,j in d if j>=4]

if len(ans) <2:
    a1=0
else:
    ans.sort()
    a1 = ans[-1]*ans[-2]
    
if len(ans2)<1:
    a2=0
else:
    ans2.sort()
    a2=ans2[-1]**2
print(max(a1,a2))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
a.append(0)
cnt=1
l1,l2=0,0
for i in range(n):
    if a[i]==a[i+1]:
        cnt+=1
    else:
        if cnt>=4:
            if l1==0:
                l1=a[i]
                l2=a[i]
                break
            else:
                l2=a[i]
                break
        elif cnt>=2:
            if l1==0:
                l1=a[i]
                cnt=1
            else:
                l2=a[i]
                break
print(l1*l2)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
a=[]
tmp=l.pop(-1)
while len(a)<2 and len(l)>0:
  if tmp==l[-1]:
    a.append(tmp)
    l.pop(-1)
    if len(l)==0:
      break
    tmp=l.pop(-1)
  else:
    tmp=l.pop(-1)
if len(a)<2:
  print(0)
else:
  print(a[-1]*a[-2])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = {}
for a in A:
    if a not in B:
        B[a] = 0
    B[a] += 1

# print(B)
C = [(b, B[b]) for b in B]
C.sort(reverse=True, key=lambda x: x[0])
# print(C)

ans = []
for c in C:
    if len(ans) == 0 and c[1] >= 4:
        print(c[0]**2)
        exit()
    if c[1] >= 2:
        ans.append(c)
    if len(ans) >= 2:
        print(ans[0][0] * ans[1][0])
        exit()
print(0)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
c = Counter(A)
a, b = 0, 0
for key in sorted(c.keys(), reverse=True):
  if c[key] >= 4:
    a = max(a, key)
    b = max(b, key)
  elif c[key] >= 2:
    if a == 0:
      a = key
    elif a != 0 and b == 0:
      b = key
print(a*b)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A = sorted(A,reverse=True)
a, b, i = 0, 0, 0
while i < N-1:
  if A[i] == A[i+1]:
    if a == 0 and b == 0:
      a = max(a, A[i])
      i += 2
    elif a != 0 and b == 0:
      b = max(b, A[i])
      i += 2
    else:
      i += 1
  else:
    i += 1
print(a*b)

Submission status: Accepted

Solution code:
n = int(input())
dat = list(map(int, input().split()))
import collections
c = collections.Counter(dat)
data = []
for k in c:
    cnt = c[k]
    if cnt > 1:
        data.append((k, cnt//2))
data.sort(reverse=True)
#print(data)
if len(data) == 0:
    print(0)
else:
    a = data[0]
    x =a[0]
    if a[1] > 1:
        y = a[0]
        print(x*y)
    elif len(data) > 1:
        y = data[1][0]
        print(x*y)
    else:
        print(0)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines

# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np


def main():
	n=int(input())
	A=list(map(int,input().split()))
	A.sort(reverse=True)
	ans=0
	cnt=0
	li=[]
	for i in range(n-1):
		if A[i+1]==A[i]:
			cnt+=1
			if cnt==3:
				ans=A[i]**2
				break
			if cnt==1:
				li.append(A[i])
				if len(li)==2:
					ans=li[0]*li[1]
					break
		else:
			cnt=0
	print(ans)
	
	

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines

# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np

from collections import defaultdict

def main():
	d=defaultdict(int)
	
	n=int(input())
	A=list(map(int,input().split()))
	for x in A:
		d[x]+=1
	
	li=[]
	li2=[]
	for k,v in d.items():
		if v>=2:
			li.append(k)
		if v>=4:
			li2.append(k)

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

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
b = list(set(a))
b.sort(reverse=True)
l = len(b)
c = [0 for _ in range(l)]
prei = b[0]
j = 0
for i in a:
    if i != prei:
        j += 1
    c[j] += 1
    prei = i
x = []
for i in range(l):
    if c[i] >= 2:
        x.append(b[i])
    if c[i] >= 4:
        x.append(b[i])
if len(x) >= 2:
    ans = x[0] * x[1]
    print(ans)
else:
    print(0)

Submission status: Accepted

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

a.sort()

tmp_val = 0
pair_val = 0
for i in range(n-1, -1, -1):
  if tmp_val == a[i]:
    if pair_val != 0:
      print(pair_val * a[i])
      exit()
    else:
      pair_val = a[i]
      tmp_val = 0
  else:
    tmp_val = a[i]

print(0)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
ctr = Counter(list(map(int, input().split())))
side = []
for k in sorted(ctr.keys(), reverse=True):
    if ctr[k] >= 4:
        side.append(k)
        side.append(k)
    elif ctr[k] >= 2:
        side.append(k)
print(side[0] * side[1] if len(side) >= 2 else 0)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
A.sort()
cnt={}
K=list(set(A))
K.sort()
for k in K:
  cnt[k]=0
for a in A:
  cnt[a]+=1
E=[]
for k in K:
  if cnt[k]>=2:
    if cnt[k]>4:
      E.append(k)
    E.append(k)
E.sort()
# print(E)
if len(E)>=2:
  print(E[-1]*E[-2])
else:
  print(0)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
A.sort()
cnt={}
K=list(set(A))
K.sort()
for k in K:
  cnt[k]=0
for a in A:
  cnt[a]+=1
E=[]
for k in K:
  if cnt[k]>=2:
    if cnt[k]>=4:
      E.append(k)
    E.append(k)
E.sort()
# print(E)
if len(E)>=2:
  print(E[-1]*E[-2])
else:
  print(0)

Submission status: Accepted

Solution code:
from collections import Counter

N=int(input())
L=list(map(int,input().split()))
Z=[]
M=Counter(L)
for i,j in M.items():
	if j>=2:
		Z.append(i)
	if j>=4:
		Z.append(i)
V=sorted(Z)
if len(V)<2:
	print(0)
else:
	print(V[-1]*V[-2])

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    sidelist = dict()

    for aa in a:
        if aa not in sidelist:
            sidelist[aa] = 1
        else:
            sidelist[aa] += 1

    side_a = 0
    side_b = 0

    for s in sidelist:
        if sidelist[s] >= 4 and s > side_a:
            side_a = s
            side_b = s
        elif sidelist[s] >= 2:
            if s > side_a:
                side_b = side_a
                side_a = s
            elif s > side_b:
                side_b = s

    print(side_a*side_b)

Submission status: Accepted

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

list.sort(a, reverse = True)
#print(a)

i = 0
tmp = []
while True:
    if a[i] == a[i + 1]:
        tmp.append(a[i])
        if i != n - 1:
            i += 2
        if i == n - 1:
            break
    else:
        i += 1
    if i == n - 2:
        break
if a[n-2] == a[n-1]:
    tmp.append(a[n-2])

list.sort(tmp, reverse = True)
#print(tmp2)
#print(tmp)
if len(tmp) >= 2:
    print(tmp[0] * tmp[1])
else:
    print(0)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
b = list(set(a))
b.sort(reverse=True)
l = len(b)
c = [0 for _ in range(l)]
prei = b[0]
j = 0
for i in a:
    if i != prei:
        j += 1
    c[j] += 1
    prei = i
x = []
for i in range(l):
    if c[i] >= 2:
        x.append(b[i])
if len(x) >= 2:
    ans = x[0] * x[1]
    for i in range(len(x)):
        if c[i] >= 4:
            ans  = max(ans, x[i] ** 2)
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement, groupby
from math import floor, ceil, sqrt, factorial, log
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from heapq import heappop, heappush, heappushpop
from itertools import product
import sys
stdin = sys.stdin
mod = 10**9 + 7


def ns(): return stdin.readline().rstrip()


def ni(): return int(ns())


def na(): return list(map(int, stdin.readline().split()))


n = ni()
a = na()

a.sort(reverse=True)
l = list()
for key, gr in groupby(a):
    if len(l) == 2:
        break
    num = len(list(gr))
    if num >= 2:
        l.append(key)
    if num >= 4:
        l.append(key)
print(l[0] * l[1] if len(l) >= 2 else 0)

Submission status: Accepted

Solution code:
import sys

def solve():
    N = int(input())
    A = [int(a) for a in input().split()]
    lengthD = dict()
    for a in A:
        if a in lengthD: lengthD[a] += 1
        else: lengthD[a] = 1
    moreThanTwo = []
    count = 0
    for key in lengthD:
        if lengthD[key] > 1: 
            moreThanTwo.append(key)
            count += 1
    
    if count == 0: print(0)
    else:
        ans = 0
        moreThanTwo.sort()
        if lengthD[moreThanTwo[-1]] > 3: ans = moreThanTwo[-1] ** 2
        if count < 2: print(ans)
        else:
            ans = max(ans, moreThanTwo[-1] * moreThanTwo[-2])
            print(ans)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
ans = []
for k in sorted(cnt.keys(), reverse=True):
    if len(ans) == 2:
        break
    if cnt[k] >= 4:
        ans.append(k)
        ans.append(k)
    elif cnt[k] >= 2:
        ans.append(k)
if len(ans) < 2:
    print(0)
else:
    print(ans[0] * ans[1])

Submission status: Accepted

Solution code:
N, = map(int, input().split())
X = map(int, input().split())
from collections import defaultdict
d = defaultdict(int)
ccs = []
ccs2 = []
for x in X:
    d[x] += 1
    if d[x] == 2:
        ccs.append(x)
ccs.sort()
if len(ccs) > 1:
    r =ccs[-1]*ccs[-2]
    if d[ccs[-1]] >= 4:
        print(ccs[-1]**2)
    else:
        print(r)
else:
    print(0)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
num=a[0]
fin=0
res=[]
i=1
while i<n:
    #print(a[i-1],a[i])
    if a[i-1]==a[i]:
        res.append(a[i])
        i+=2
    else:
        i+=1
#print(res)
if len(res)<2:
    print(0)
else:
    print(res[-1]*res[-2])

Submission status: Accepted

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

cnt=Counter(a)
#print(cnt)
cand=[]
for k,v in cnt.items():
    if 2<=v<4:
        cand.append([k,2])
    elif 4<=v:
        cand.append([k,2])
        cand.append([k,2])
cand=sorted(cand,reverse=True)
print(cand[0][0]*cand[1][0] if len(cand)>=2 else 0)

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
#print(d)

L = []
for k, v in d.items():
    if v >= 2:
        L.append([k, v])
L.sort(reverse=True)
if len(L) == 0:
    print(0)
elif len(L) == 1:
    if L[0][1] < 4:
        print(0)
    else:
        print((L[0][0])**2)
else:
    if L[0][1] >= 4:
        print((L[0][0])**2)
    else:
        print(L[0][0]*L[1][0])

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)
    if (c.most_common()[0][1] < 2 or c.most_common()[1][1] < 2) \
            and c.most_common()[0][1] < 4:
        return print(0)
    d = [i[0] for i in c.items() if i[1] >= 2]
    if d:
        d.sort()
    e = [i[0] for i in c.items() if i[1] >= 4]
    if e:
        e.sort()
    if d and e:
        print(max(d[-1] * d[-2], e[-1]*e[-1]))
    elif d:
        print(d[-1] * d[-2])
    else:
        print(e[-1]*e[-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

b = []
prev = cnt = 0
for i in range(n):
        if prev==a[i]: cnt+=1
        else:
                if cnt>1: b.append([prev, cnt])
                cnt = 1
        prev = a[i]
if cnt>1: b.append([prev, cnt])

h, w = 0, 0
for i, cnt in b:
        if cnt>3: h, w = i, i
        else: h, w = max(h, w), i
print(h*w)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
a = list(map(int,input().split()))
c = Counter(a)
ans = 0
s = []
for e in c:
    if c[e] >= 2:
        s.append(e)
    if c[e] >= 4:
        ans = max(ans,e*e)
s = sorted(s)
if len(s) < 2:
    print(0)
else:
    print(max(ans,s[-1]*s[-2]))

Submission status: Accepted

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

e = -1
for k,v in sorted(ctr.items(), reverse=True):
    if v < 2: continue
    if e > 0:
        print(e*k)
        exit()
    elif v >= 4:
        print(k*k)
        exit()
    else:
        e = k
print(0)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
cnt = Counter(A)
l = []
ans = 0

for k in cnt.keys():
    if cnt[k]>=4:
        ans = max(ans, k*k)
    
    if cnt[k]>=2:
        l.append(k)

l.sort(reverse=True)

if len(l)>=2:
    ans = max(ans, l[0]*l[1])

print(ans)