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()))
cnt_A = Counter(A)
# print(cnt_A)

kouho = []

ans = 0

for key in cnt_A:
    if cnt_A[key] >= 2:
        kouho.append(key)
    if cnt_A[key] >= 4:
        ans = max(ans, key ** 2)

if len(kouho) < 2:
    print(ans)
    exit()

kouho.sort()
ans = max(ans, kouho[-1] * kouho[-2])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

acnt = Counter(a)
num = [0,0]
for i in acnt:
    if acnt[i] >= 4:
        num.append(i)
    if acnt[i] >= 2:
        num.append(i)
num.sort()
print(num[-1] * num[-2])

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

dic={}

for i in range(n):
    if a[i] in dic:
        dic[a[i]]+=1
    else:
        dic[a[i]]=1

lst=list(dic.keys())
lst.sort(reverse=True)
# print(lst)
anslst=[]
for i in lst:
    if dic[i]>=2:
        anslst.append(i)
    if dic[i]>=4:
        anslst.append(i)
    if len(anslst)>=2:
        print(anslst[0]*anslst[1])
        break
else:
    print(0)
# print(anslst)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
val = set()
ans = []

for i in a:
    if i in val:
        ans.append(i)
    else:
        val.add(i)
ans.sort()
if len(ans) == 0:
    print(0)
else:
    if ans.count(ans[-1]) >= 4:
        print(ans[-1]**2)
    elif ans.count(ans[-1]) == 3:
        print(ans[-1]*ans[-4])
    elif ans.count(ans[-1]) == 2:
        print(ans[-1]*ans[-3])
    else:
        print(ans[-1]*ans[-2])

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
c = dict(c)
c = sorted(c.items(), key=lambda x:x[0], reverse=True)
h = 0
w = 0
for i in range(len(c)):
  if c[i][1] >= 2 and h == 0:
    h = c[i][0]
    if c[i][1] >= 4:
      w = c[i][0]
  elif c[i][1] >= 2 and w == 0:
    w = c[i][0]
ans = h * w
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
a=list(map(int,input().split()))
dic={}
for i in a:
  if i in dic:
    dic[i]+=1
  else:
    dic[i]=1
a=[0,0]
mx=0
for i in dic:
  if dic[i]>=2:
    a.append(i)
    if dic[i]>3:
      mx=max(i**2,mx)
a=sorted(a)
mx=max(a[-1]*a[-2],mx)
print(mx)

Submission status: Accepted

Solution code:
n = input()
*a, = map(int, input().split())
d = {}
for i in a:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
b = [i for i in d.keys() if d[i] >= 2]
if len(b) == 0:
    print(0)
elif len(b) == 1:
    if d[b[0]] >= 4:
        print(b[0]**2)
    else:
        print(0)
else:
    b.sort()
    if d[b[-1]] >= 4:
        print(b[-1]**2)
    else:
        print(b[-1]*b[-2])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
As=sorted(A,reverse=True)
B=0
C=0
for i in range(len(As)-1):
    tmp=As[i]
    if tmp==As[i+1]:
        if tmp>B:
            B=tmp
            del As[0:i+2]
            break
for j in range(len(As)-1):
    tmp=As[j]
    if tmp==As[j+1]:
        if tmp>C:
            C=tmp
print(B*C)

Submission status: Accepted

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

Submission status: Accepted

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

    A.sort(reverse=True)
    tmp = 0
    bar1 = 0
    bar2 = 0

    for a in A:
        if tmp == 0:
            tmp = a
        elif tmp == a:
            if bar1 > 0:
                bar2 = a
                print(bar1*bar2)
                exit()
            else:
                bar1 = a
                tmp = 0
        elif a < tmp:
            tmp = a

    print(0)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = Counter(list(map(int, input().split())))
c = []
for i, j in a.items():
    if j >= 2:
        for _ in range(j//2):
            c.append(i)
c.sort(reverse = True)
print(c[0]*c[1] if len(c)>=2 else 0)

Submission status: Accepted

Solution code:
from collections import Counter

n=int(input())
a=list(map(int,input().split()))
cnt=Counter(a)
lis = sorted(list(set(a)),reverse=True)
ans1,ans2 = 0,0
for val in lis:
    if ans1==0:
        if cnt[val]>=2:
            cnt[val]-=2
            ans1=val

        if cnt[val]>=2:
            ans2=val
            break
    else:
        if cnt[val]>=2:
            ans2=val
            break
print(ans1*ans2)

Submission status: Accepted

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

dic = {}
cnt = 0

for x in A:
    if x not in dic.keys():
        dic[x] = 1
    else:
        dic[x] += 1

dic = sorted(dic.items(), reverse=True)

ans = 0
for i in range(len(dic)):
    if dic[i][1] >= 4 and cnt == 0:
        ans = dic[i][0]**2
        break
    elif dic[i][1] >= 2:
        if cnt == 1:
          ans = t*dic[i][0]
          break 
        if cnt == 0:
            t = dic[i][0]
            cnt = 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
l = list(map(int, input().split()))
d = Counter(l)
ite = sorted(d.items(), key=lambda x: (-x[0], -x[1]))
first = 0
taken = False
second = 0
for i in range(len(ite)):
    if ite[i][1] >= 4:
        if not taken:
            print(ite[i][0] * ite[i][0])
            exit()
        elif taken:
            second = ite[i][0]
            break
        exit()
    elif ite[i][1] >= 2 and not taken:
        first = ite[i][0]
        taken = True
    elif ite[i][1] >= 2 and taken:
        second = ite[i][0]
        break
print(first * second)

Submission status: Accepted

Solution code:
import collections

n = int(input())
l = list(map(int, input().split()))
l = sorted(l, reverse=True)

i = 1
a = 0
b = 0
while i < n:
    if l[i] == l[i-1]:
        if a == 0:
            a = l[i]
        else:
            b = l[i]
            break
        i += 2
    else:
        i += 1

print(a*b)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

ans = 0 
d = {}
for i in a:
    if i not in d:
        d[i] = 1 
    else:
        d[i] += 1

d = sorted(d.items(), key=lambda x:x[0], reverse=True)
area = 1 
cnt = 0 
allone = True
for i in d:
    if i[1] != 1:
        allone = False
        if i[1] >= 4:
            if cnt == 0:
                area = i[0]*i[0]
                break
        if cnt == 2:
            break
        area *= i[0]
        cnt += 1

if allone:
    print(0)
else:
    print(area)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

ans = 0 
d = {}
for i in a:
    if i not in d:
        d[i] = 1 
    else:
        d[i] += 1

d = sorted(d.items(), key=lambda x:x[0], reverse=True)
area = 1 
cnt = 0 
allone = True
for i in d:
    if i[1] != 1:
        allone = False
        if i[1] >= 4:
            if cnt == 0:
                area = i[0]*i[0]
                break
            else:
                if cnt == 2:
                    break
                area *= i[0]
                cnt += 1
        else:
            if cnt == 2:
                break
            area *= i[0]
            cnt += 1

if allone:
    print(0)
else:
    print(area)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

ans = 0 
d = {}
for i in a:
    if i not in d:
        d[i] = 1 
    else:
        d[i] += 1

d = sorted(d.items(), key=lambda x:x[0], reverse=True)
area = 1 
cnt = 0 
allone = True
for i in d:
    if 1 < i[1] < 4:
        allone = False
        if cnt == 2:
            break
        area *= i[0]
        cnt += 1
    elif i[1] >= 4:
        allone = False
        if cnt == 0:
            area = i[0]*i[0]
            break
        else:
            if cnt == 2:
                break
            area *= i[0]
            cnt += 1

if allone:
    print(0)
else:
    print(area)

Submission status: Accepted

Solution code:
import collections
n = int(input())
l = map(int, input().split())
c = collections.Counter(l)
c = sorted(c.items(), key=lambda c:c[0], reverse=True)
 
ans = 1
cnt = 0
 
for key,val in c:
  if cnt==0 and val>=4:
    ans=key**2
    break
  if val>=2:
    ans*=key
    cnt+=1
  if cnt == 2:
    break
else:
  ans = 0
print(ans)

Submission status: Accepted

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

a.sort()
a.reverse()

ret = []
my_s = set()

for item in a:
    if item in my_s:
        ret.append(item)
        if len(ret) == 2:
            print(ret[0] * ret[1])
            exit()
        my_s.remove(item)
    else:
        my_s.add(item)

print(0)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse = True)
i = 0
state = 0
ans = 1
while i < n-1:
  if a[i] == a[i+1]:
    ans*=a[i]
    state += 1
    if state == 2:
      break
    i += 2
  else:
    i += 1
if state < 2:
  print(0)
else:
  print(ans)

Submission status: Accepted

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

dic = {}
L = set([]) #2本以上
S = set([]) #4本以上
for i in range(N):
  if A[i] not in dic:
    dic[A[i]] = 1
  else:
    dic[A[i]] += 1
    if dic[A[i]] == 2:
      L.add(A[i])
    elif dic[A[i]] == 4:
      S.add(A[i])
L = list(L)
L.sort(reverse = True)
if len(L) >= 2:
  tyo = L[0]*L[1]
else:
  tyo = 0
S = list(S)
S.sort(reverse = True)
if S:
  sei = S[0]**2
else:
  sei = 0
#print(L,S)
print(max(tyo,sei))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = Counter(list(map(int, input().split())))
l = []
for x,y in A.items():
    if y >= 2:
        for _ in range(y//2):
            l.append(x)
l.sort(reverse=True)
print(l[0]*l[1] if len(l)>=2 else 0)

Submission status: Accepted

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

from collections import Counter
c = Counter(As)
ans = []
for a, c in c.most_common():
    if c<2:
        break
    ans.append(a)
    if c>=4:
        ans.append(a)

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    A.sort(reverse=True)
    rect = []
    prev = 0
    for a in A:
        if a == prev:
            rect.append(a)
            prev = 0
            if len(rect) == 2:
                break
        else:
            prev = a

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
ma1 = 0
ma2 = 0
A = list(map(int,input().split()))
A.append(0)
a = sorted(A,reverse = True)

hold = 0
count = 0

for i in a:
  if i == hold:
    count += 1
  else:
    if count > 3 and ma1 == 0:
      ma1 = hold
      ma2 = hold
    elif count > 1:
      if ma1 == 0:
        ma1 = hold
      elif ma2 == 0:
        ma2 = hold
    hold = i
    count = 1
    if ma1 != 0 and ma2 != 0:
      break
    

print(ma1*ma2)

Submission status: Accepted

Solution code:
import collections
s = set()
N = int(input())
count = []*(10**9+1)
ma1 = 0
ma2 = 0
A = list(map(int,input().split()))
a = collections.Counter(A)
b = sorted(a.items(),reverse = True)

for i in b:
  if ma1 == 0 and i[1] > 1:
    ma1 = i[0]
    if i[1] > 3:
      ma2 = i[0]
  elif ma1 != 0 and i[1] > 1:
    ma2 = i[0]
  if ma1 > 0 and ma2 > 0:
    break
  
print(ma1*ma2)

Submission status: Accepted

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

c = sorted(b.items(), reverse = True)
z = [0] * 2
j = 0
ans = 0
for keys,values in c:
    if values < 2:continue
    if values >= 4:
        ans = max(ans, keys ** 2)
    if j < 2:
        z[j] = keys
        j += 1
    ans = max(ans, z[0] * z[1])
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] in d:
        d[a[i]]+=1
    else:
        d[a[i]]=1

can = list(d.items())

edge=[]
for i in range(len(can)):
    if can[i][1]<2:
        continue
    else:
        edge.append(can[i])

if len(edge)==0:
    print(0)
    exit(0)
elif len(edge)==1 and edge[0][1]<4:
    print(0)
    exit(0)
elif len(edge)==1 and edge[0][1]>=4:
    print(edge[0][0]**2)
    exit(0)

edge = sorted(edge,key=lambda x: x[0],reverse=True)

k=len(edge)

s=1
if edge[0][1]>=4:
    print(edge[0][0]**2)
else:
    print(edge[0][0]*edge[1][0])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
dum = [0]*n
a_li = list(map(int,input().split()))
a_counter = Counter(a_li)
a_most = a_counter.most_common()
count = 0
for i,j in a_most:
    if j >= 2:
        dum[count] = i
    count += 1
dum.sort(reverse=1)
if a_counter[dum[0]] >= 4:
    print(dum[0]**2)
else:
    print(dum[0]*dum[1])

Submission status: Accepted

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

Submission status: Accepted

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

c=collections.Counter(a)
d={}
for i in c:
    if c[i]>1:
        d.setdefault(i,c[i])
e=sorted(d,reverse=True)
if len(e)>1:
    if d[e[0]]>3:
        print(e[0]**2)
    else:
        print(e[0]*e[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)
A = sorted(list(set(A)))[::-1]
flag = False
x = 0
for a in A:
	if flag == False:
		if C[a] >= 4:
			print(a ** 2)
			exit()
		elif C[a] >= 2:
			flag = True
			x = a
	elif flag == True and C[a] >= 2:
		print(a * x)
		exit()
else:
	print(0)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

hasOne = defaultdict(bool)
E = []

for a in A:
  if not hasOne[a]:
    hasOne[a] = True
  else:
    E.append(a)
    hasOne[a] = False

E.sort(reverse=True)

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
b = []
g = []
for i in range(1,n):
    if a[i-1] == a[i]:
        b.append(a[i])
if 4 <= n:
    for i in range(3,n):
        if a[i-3] == a[i]:
            g.append(a[i])
c = sorted(list(set(b)),reverse=True)
f = sorted(list(set(g)),reverse=True)
ans = 0
if len(f):
    ans = max(ans,f[0]*f[0])
if 2 <= len(c):
    ans = max(ans,c[0]*c[1])
print(ans)

Submission status: Accepted

Solution code:
import collections

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

rkey=list(set(a))
list.sort(rkey, reverse=True)
rvalue=[c[i] for i in rkey]

for i in range(len(rvalue)):
  if rvalue[i]>=4:
    print(rkey[i]**2)
    exit()
  elif rvalue[i]>=2:
    width=rkey[i]
    rvalue.pop(i)
    rkey.pop(i)
    break
else:
  print("0")
  exit()
for i in range(len(rvalue)):
  if rvalue[i]>=2:
    height=rkey[i]
    break
else:
  print("0")
  exit()
print(width*height)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
l=0
w=0
c=0
for i in range(n-1,0,-1):
    if a[i]==a[i-1] and l==0:
        l=a[i]
        a[i]=0
        a[i-1]=0
    elif a[i]==a[i-1] and w==0:
        w=a[i]
        print(l*w)
        break
if w==0:
    print(0)

Submission status: Accepted

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

x=0
y=0
i=0

while(i<n):
    if x==0 and a[n-1-i]==a[n-2-i]:
        x=a[n-1-i]
        i+=2
        continue
    if x>0 and y==0 and a[n-1-i]==a[n-2-i]:
        y=a[n-1-i]
        break
    i+=1
print(x*y)

Submission status: Accepted

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

a.sort()

side1=0
side2=0
num=0

for num in range(n-1):
    if a[num]==a[num+1]:
        side2=side1
        side1=a[num]
        a[num+1]=0

print(side1*side2)

Submission status: Accepted

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

from collections import Counter
c=Counter(a)

ans_1=0
ans_2=0
for key in c:
    if c[key]>=2 and key>=ans_1:
        ans_1,ans_2=key,ans_1
    elif c[key]>=2 and key>=ans_2:
        ans_2=key
    if c[key]>=4 and key>=ans_2:
        ans_2=key

print(ans_1*ans_2)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit
from collections import defaultdict


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    dic = defaultdict(int)
    max_1 = 0
    max_2 = 0
    for ai in a:
        dic[ai] += 1
        if dic[ai] >= 2 and ai > max_1:
            max_2 = max_1
            max_1 = ai
        if dic[ai] >= 2 and max_1 > ai > max_2:
            max_2 = ai
    if dic[max_1] >= 4:
        print(max_1 ** 2)
    else:
        print(max_1 * max_2)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse = True)
before = 0
ans = 0
for num in a:
    if num == before:
        if ans == 0:
            ans = num
            before = 0
        else:
            print(ans * num)
            exit()
    else:
        before = num
print(0)

Submission status: Accepted

Solution code:
from collections import Counter

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

s = sorted(Counter(a).items(), reverse = True)


count = 0
ans = 0
for i, j in s:
        if count == 2:
            break
        if count == 1 and j >= 2:
            ans *= i
            count += 1
        if count == 0:
            if j >= 4:
                ans = i**2
                count += 2
            elif j >= 2:
                ans = i
                count += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
arr = list(map(int, input().split()))
a_set = sorted(set(arr), reverse=True)
a_dict = defaultdict(int)
for num in arr:
    a_dict[num] += 1
a, b = 0, 0
for num in a_set:
    if a_dict[num] > 3 and a == 0:
        a, b = num, num
        break
    elif a_dict[num] > 1 and a == 0:
        a = num
    elif a_dict[num] > 1:
        b = num
        break

print(a * b)

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
# import math
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]





# @lru_cache(None)


n=l()[0]
A=l()
d={}
for i in range(n):
    if(A[i] not in d):
        d[A[i]]=0
    d[A[i]]+=1
s=list(d.keys())
s.sort(reverse=True)
x=-1
y=-1
for ks in s:
    if(x==-1 and d[ks]>=2):
        x=ks
        if(d[ks]>=4):
            y=ks
            break
    elif(x!=-1 and y==-1 and d[ks]>=2):
        y=ks
        break
y=max(0,y)
print(max(x*y,0))

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
as_ = map(int, input().strip().split())

d = defaultdict(int)

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

rects = sorted([k for k, v in d.items() if v >= 2], reverse=True)
squares = sorted([k for k, v in d.items() if v >= 4], reverse=True)

max_rect = rects[0] * rects[1] if len(rects) >= 2 else 0
max_square = squares[0] ** 2 if len(squares) > 0 else 0

print(max(max_rect, max_square))

Submission status: Accepted

Solution code:
from collections import defaultdict


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

lengthNumberDict = defaultdict(int)
for a in A:
    lengthNumberDict[a] += 1

lengthNumberList = [a for a in list(lengthNumberDict.items()) if a[1] >= 2]
lengthNumberList = sorted(lengthNumberList, key=lambda x: x[0], reverse=True)
if len(lengthNumberList) == 0:
    print(0)
    quit()
if len(lengthNumberList) == 1 and lengthNumberList[0][1] <= 3:
    print(0)
    quit()

if lengthNumberList[0][1] >= 4:
    print(lengthNumberList[0][0] ** 2)
else:
    print(lengthNumberList[0][0] * lengthNumberList[1][0])

Submission status: Accepted

Solution code:
from collections import Counter

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

c = {k: v for k, v in Counter(A).items() if v >= 2}

if len(c) < 2:
    print(0)
    exit()

x = max(c)

if c[x] >= 4:
    print(x * x)
else:
    del c[x]
    y = max(c)
    print(x * y)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
Alist = list(map(int, input().split()))
Ac = Counter(Alist)
length_list = sorted(Ac.keys(), reverse=True)
can_use = []
for l in length_list:
    if Ac[l] >= 4:
        if can_use:
            print(can_use[0]*l)
            exit()
        else:
            print(l**2)
            exit()
    elif Ac[l] >= 2:
        if can_use:
            print(can_use[0]*l)
            exit()
        else:
            can_use.append(l)

print(0)

Submission status: Accepted

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

from collections import Counter

c = Counter(a)

c = [(length,cnt) for length,cnt in sorted(c.items(), key=lambda x:(-x[0], -x[1])) if cnt>=2]
if len(c)<2:
    print(0)
else:
    if c[0][1]>=4:
        print(c[0][0]*c[0][0])
    else:
        print(c[0][0]*c[1][0])

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())

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

edge_dic = defaultdict(int)
for n in range(N):
    edge_dic[al[n]] += 1
dic = sorted(edge_dic.items(), reverse=True)


yoko = 0
tate = 0
for v in dic:
    if yoko == 0:
        if v[1] >= 2:
            yoko = v[0]
            if v[1] >= 4:
                tate = v[0]
                break
    else:
        if v[1] >= 2:
            tate = v[0]
            break
print(yoko * tate)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
C = collections.Counter(A).most_common()
CC = sorted(C,key=lambda x: -x[0])
AA = 0
BB = 0
for cc in CC:
    if AA == 0 and cc[1] >= 4:
        AA = cc[0]
        BB = cc[0]
        break
    if AA == 0 and cc[1] >= 2:
        AA = cc[0]
        continue
    if BB == 0 and cc[1] >= 2:
        BB = cc[0]
        break
print(AA*BB)

Submission status: Accepted

Solution code:

import numpy as np

N = int(input())
As = list(map(int, input().split()))
dic = {}
for a in As:
    if a in dic:
        dic[a] += 1
    else:
        dic[a] = 1

dic = sorted(dic.items(), reverse=True)

first = -1
second = -1

for length, num in dic:
    if num >= 2 and first == -1:
        first = length
        num -= 2
    
    if num >= 2 and second == -1:
        second = length
        break

if first >= 0 and second >= 0:
    print(first * second)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

from collections import Counter

c = Counter(a)

ma1 = 0
ma2 = 0

for k,v in c.items():
    if v < 2:
        continue

    if k > ma1:
        if v >= 4:
            ma2 = k
            ma1 = k
            continue
        else:
            ma2 = ma1
            ma1 = k
    elif k > ma2:
        ma2 = k

print(ma1*ma2)

Submission status: Accepted

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

seta = set(a)

counta = {a : 0 for a in seta}

for i in a:
    if i in seta:
        counta[i] += 1

lines = [i for i in counta.keys() if counta[i] >= 2]

if len(lines) >= 1:
    countsameValue = 0
    a = max(lines)
    secondmax = 0
    for l in lines:
       if(l != a):
           secondmax = max(secondmax, l)

    if (counta[a] > 3):
        print(a**2)
    else:
        print(a * secondmax)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))

C = list(Counter(A).items())
C.sort(reverse=True)

a = 0
b = 0
for c in C:
    if c[1] > 1 and a == 0 and b == 0:
        if c[1] > 3:
            a = b = c[0]
            break
        a = c[0]
    elif c[1] > 1 and a != 0 and b == 0:
        b = c[0]
        break

print(a * b)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = Counter(list(map(int,input().split())))
A = sorted(A.items(),reverse=True)
ans = list()
for key,value in A:
    if value >= 4:
        if len(ans) == 0:
            ans.append(key)
            ans.append(key)
            break
        else:
            ans.append(key)
            break
    elif value >= 2:
        if len(ans) == 0:
            ans.append(key)
        else:
            ans.append(key)
            break
if len(ans) <= 1:
    print(0)
else:
    print(ans[0]*ans[1])

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    from collections import Counter
    c=Counter(l)# ()にカウンターの対象のリストの変数名
    ans=[]
    for key,value in c.items():
        if value>=4:
            ans.append(key)
            ans.append(key)
        elif value>=2:
            ans.append(key)
    ans.sort(reverse=True)
    if ans:
        print(ans[0]*ans[1])
    else:
        print(0)
resolve()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    from collections import Counter
    c=Counter(l)# ()にカウンターの対象のリストの変数名
    cl=list(c.items())
    cl.sort(key=lambda x:x[0],reverse=True)
    ans=[]
    for key,value in cl:
        if value>=4:
            if len(ans)==0:
                ans.append(key)
                ans.append(key)
                break
            elif len(ans)==1:
                ans.append(key)
                break
        elif value>=2:
            ans.append(key)
            if len(ans)==2:
                break
    if ans:
        print(ans[0]*ans[1])
    else:
        print(0)
resolve()

Submission status: Accepted

Solution code:
def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    from collections import Counter
    c=Counter(l)# ()にカウンターの対象のリストの変数名
    cl=list(c.items())
    cl.sort(key=lambda x:x[0],reverse=True)
    ans=[]
    for key,value in cl:
        if value>=4:
            ans.append(key)
            ans.append(key)
            break
        elif value>=2:
            ans.append(key)
            if len(ans)==2:
                break
    if ans:
        print(ans[0]*ans[1])
    else:
        print(0)
resolve()

Submission status: Accepted

Solution code:
from collections import Counter
_, *lst = map(int, open(0).read().split())
c = Counter(lst).items()
stick2 = [i for i, j in c if j > 1]
stick4 = [0] + [i for i, j in c if j > 3]
max4 = max(stick4)
max4 *= max4
if len(stick2) > 1:
  stick2.sort()
  print(max(stick2[-1] * stick2[-2], max4))
else:
  print(max(0, max4))

Submission status: Accepted

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

import collections
C=collections.Counter(A).most_common()
D=[0]+[0]+[a for a,b in C if b>=2]+[a for a,b in C if b>=4]
D.sort()
print(D[-1]*D[-2])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A=Counter(list(map(int,input().split())))
m=[]
for key,value in A.items():
    if value>=2:
        m.append(key)
    if value>=4:
        m.append(key)
print(sorted(m)[-1]*sorted(m)[-2] if len(m)>=2 else 0)

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
a=list(map(int,input().split()))
b=sorted(a,reverse=True)
d=deque(b)
ans=0
tmp=0
flag1=False
flag2=False
while d:
    if d[0]==tmp and flag1==False:
        flag1=True
        d.popleft()
        h1=tmp
        tmp=0
    elif flag1 and d[0]==tmp:
        h2=tmp
        ans=h1*h2
        break
    elif d[0]!=tmp:
        tmp=d.popleft()
print(ans)