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:
n = int(input())
A = list(map(int,input().split()))
from collections import Counter
A = Counter(A)
B = A.most_common()
B.sort(key = lambda x:x[0], reverse = 1)

fst_len = 0
snd_len = 0

for i,c in B:
  if c >1:
    if fst_len==0:
      fst_len = i
      if c >3:
        snd_len = i
    elif snd_len ==0:
      snd_len = i
    else:
      break


print(fst_len * snd_len)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
A = Counter(A)
A2 = [i for i,j in A.items() if j >= 2]
A2.sort(reverse = True)
A4 = [i for i,j in A.items() if j >= 4]
A4.sort(reverse = True)

H,W = 0,0

for i in range(len(A2)):
  if H == 0:
    H = A2[i]
  else:
    W = A2[i]
    break

for j in range(len(A4)):
  if A4[j] >= H:
    H,W = A4[j],A4[j]
  elif A4[j] >= W:
    W = A4[j]
  break

print(H*W)

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int, input().split()))
count = {}
for a in L:
    if a not in count:
        count[a] = 1
    else:
        count[a] += 1
leng = sorted(set(count), reverse=True)
flag = 0
for i in leng:
    if count[i] >= 4:
        if flag == 0:
            print(i * i)
            break
        else:
            print(l * i)
            break
    elif count[i] >= 2:
        if flag == 0:
            l = i
            flag = 1
        else:
            print(l * i)
            break
else:
    print(0)

Submission status: Accepted

Solution code:
from collections import defaultdict
import numpy as np

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

d = defaultdict(int)

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

values = np.array(list(d.values()))

if np.where(values >= 2)[0].shape[0] < 2:
    print(0)
else:
    over_4 = []
    over_2 = []

    for n, v in d.items():
        if v >= 4:
            over_4.append(n)
        if v >= 2:
            over_2.append(n)

    over_2 = sorted(over_2, reverse=True)

    ans_4 = 0
    if len(over_4) > 0:
        ans_4 = max(over_4)
    ans_2 = over_2[0] * over_2[1]

    print(max(ans_4**2, ans_2))

Submission status: Accepted

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

M = 0
N = 0
for x in c.items():
    if x[1] >= 4 and x[0] > M:
        M = x[0]
        N = x[0]
    elif x[1] >= 2 and x[0] > M:
        N = M
        M = x[0]
    elif x[1] >= 2 and x[0] > N:
        N = x[0]
 
print(M*N)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
import collections
c=collections.Counter(a)
values,counts=zip(*c.most_common())
lv=list(values)
lc=list(counts)
temp=[]
for i in range(0,len(lc)):
  if lc[i]>=4:
    temp.append(lv[i])
    temp.append(lv[i])    
  elif lc[i]>=2:
    temp.append(lv[i])
temp.sort(reverse=True)
if len(temp)>=2:
  ans=temp[0]*temp[1]
print(ans)

Submission status: Accepted

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

d = dict()
for a in A:
    if a in d:
        d[a] += 1
    else:
        d[a] = 1

M = 0
N = 0
for x in d.items():
    if x[1] >= 4 and x[0] > M:
        M = x[0]
        N = x[0]
    elif x[1] >= 2 and x[0] > M:
        N = M
        M = x[0]
    elif x[1] >= 2 and x[0] > N:
        N = x[0]

print(M*N)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
Ac = Counter(list(map(int, input().split())))
sticks = sorted({k:v for k, v in Ac.items() if v >= 2}.items())[::-1]
if len(sticks) == 0:
    print(0)
else:
    if sticks[0][1] >= 4:
        print(sticks[0][0]**2)
    else:
        print(sticks[0][0]*sticks[1][0])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
tate, yoko = 0, 0
i = 0
while i < n:
    if i < n - 3 and a[i] == a[i + 1] == a[i + 2] == a[i + 3]:
        tate = max(tate, a[i])
        yoko = max(yoko, a[i])
        i += 3
    elif i < n - 2 and a[i] == a[i + 1] == a[i + 2]:
        if tate > yoko:
            yoko = max(yoko, a[i])
        else:
            tate = max(tate, a[i])
        i += 2
    elif i < n - 1 and a[i] == a[i + 1]:
        if tate > yoko:
            yoko = max(yoko, a[i])
        else:
            tate = max(tate, a[i])
        i += 1
    i += 1
print(tate * yoko)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
n  = int(input())
#  = map(int, input().split())
l  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
c = collections.Counter(l)

cmc = c.most_common()

if cmc[0][1] == 1 or len(cmc) == 1:
    print(0)
else:
    cmc = sorted(cmc)[::-1]
    cmc = [(k, v) for k, v in cmc if v >= 2]
    print(cmc[0][0] * (cmc[0][0] if cmc[0][1] >= 4 else cmc[1][0]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
N = int(input())
A = list(map(int, input().split()))

T = collections.Counter(A)
hen1 = 0 # 小さい
hen2 = 0 # 大きい
for h, c in sorted(T.items(), key=lambda x:x[0]):
    if c >= 4:
        hen1 = h
        hen2 = h
    elif c >= 2:
        if hen1 >= hen2:
            hen2 = h
        else:
            hen1 = h
print(hen1*hen2)

Submission status: Accepted

Solution code:
import collections
n=int(input())
A=list(map(int,input().split()))
a=collections.Counter(A)
firstnum=0
secondnum=0
firstl=0
secondl=0
values,counts=zip(*a.most_common())
for i,j in a.items():
  if i>=firstl and j>=2:
    secondnum=firstnum
    firstnum=j
    secondl=firstl
    firstl=i
  elif secondl<=i<firstl and j>=2:
    secondnum=j
    secondl=i
if firstnum>=4:
  print(firstl**2)
else:
  print(firstl*secondl)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
bars = list(map(int, input().split()))
bars.sort(reverse=True)
sides = defaultdict(int)
decided = []
ans = 0

for b in bars:
    sides[b] += 1
    if sides[b] == 2:
        decided.append(b)
        sides[b] -= 2
    if len(decided) == 2:
        ans = decided[0] * decided[1]
        break

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
import collections
c = collections.Counter(l)
ans1=[]
ans2=[0]
for x in c.keys():
  if c[x]>1:
    ans1.append(x)
  if c[x]>3:
    ans2.append(x)
ans1.sort()
if len(ans1)<2:
  print(0)
else:
  print(max(ans1[-1]*ans1[-2],max(ans2)**2))

Submission status: Accepted

Solution code:
# C - Make a Rectangle
# https://atcoder.jp/contests/arc081/tasks/arc081_a

from collections import Counter

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

C = sorted(list(Counter(A).items()), key=lambda x: -x[0])

ans = 0
for k, v in C:
  if v >= 4:
    if ans == 0:
      print(k ** 2)
    else:
      print(ans * k)
    exit()
  else:
    if v >= 2:
      if ans == 0:
        ans = k
      else:
        print(ans * k)
        exit()

print(0)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
x={}
edge=[0]*2
for i in a:
  if i in x:
    x[i]+=1
    if x[i]==2:
      edge.append(i)
      x[i]=0
  else:
    x[i]=1
edge.sort(reverse=True)
print(edge[0]*edge[1])

Submission status: Accepted

Solution code:
import sys
import os

def file_input():
    f = open('ARC081/input.txt', 'r')
    sys.stdin = f

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

    n=0
    m=0
    a=dict()

    for i in A:
        if not i in a:
            a[i]=True
        elif not a[i]:
            a[i]=True
        elif n<i or m<i:
            if n<m:
                n=i
            else:
                m=i
            a[i]=False

    if n==0 and m==0:
        print(0)
    else:
        print(str(n*m))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
import collections
n = ii()
A = iil()
d = collections.Counter(A)
fst = 0
sec = 0
for k,v in d.items():
    if v == 1:
        continue
    elif v < 4:
        if k > fst:
            sec = fst
            fst = k
        elif k > sec:
            sec = k
    else:
        if k > fst:
            fst = k
            sec = k
        elif k > sec:
            sec = k
print(fst*sec)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = [int(i) for i in input().split()]
cnt = Counter(a)
cnt = sorted(cnt.items(), key = lambda x: x[0], reverse = True)
#cnt = sorted(cnt ,key = lambda x: x[1], reverse = True)

c = []
for i in range(len(cnt)):
    if cnt[i][1] == 2 or cnt[i][1] == 3:
        c.append(cnt[i][0])
    elif cnt[i][1] >= 4:
        c.append(cnt[i][0])
        c.append(cnt[i][0])

c = sorted(c, reverse = True)

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

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A=list(map(int ,input().split()))
D=Counter(A)
#print(D.items())
L=[]
for num, v in D.items():
    if v>=4:
        L.append((num,4))
    elif v>=2:
        L.append((num, 2))

L=sorted(L, reverse=True)
if len(L)==0:
    print(0)
    exit()
elif len(L)==1 and L[0][1]==2:
    print(0)
    exit()
ans=1
for i,t in enumerate(L):
    num,v=t
    if i==0:
        if v==4:
            ans=num*num
            break
        else:
            ans*=num
    elif i==1:
        ans*=num
        break
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, deque, OrderedDict
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()


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

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


resolve()

Submission status: Accepted

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

a_set = list(set(a_list))
c = Counter(a_list)

if len(a_set) > n-2:
    print(0)
    exit()
else:
    a_list.sort(reverse=True)
    a_set.sort(reverse=True)
    candidate = []
    for i in a_set:
        cnt = c[i]
        
        if 2 <= cnt <= 3:
            candidate.append(i)
        elif cnt >= 4:
            candidate.append(i)
            candidate.append(i)
        if len(candidate) >= 2:
            print(candidate[0] * candidate[1])
            exit()

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)

hen = []

for i,j in zip(c.keys(),c.values()):
    if 2 <= j <= 3:
        hen.append([i,2])
    elif j >= 4:
        hen.append([i,4])


hen.sort(reverse=True)

##print(hen)
ans = 0
finished = False
choosen = False

for i,j in hen:
    if j == 2:
        if choosen:
            ans *= i
            finished = True
            break
        else:
            ans = i
            choosen = True
    else:
        if choosen:
            ans *= i
            finished = True
            break
        else:
            ans = i**2
            finished = True
            break

print(ans if finished else 0)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)

hen = []

for i,j in zip(c.keys(),c.values()):
    if 2 <= j <= 3:
        hen.append([i,2])
    elif j >= 4:
        hen.append([i,4])


hen.sort(reverse=True)

##print(hen)
ans = 0
finished = False
choosen = False

for i,j in hen:
    if choosen:
        ans *= i
        finished = True
        break

    if j == 2:
        ans = i
        choosen = True
    else:
        ans = i**2
        finished = True
        break

print(ans if finished else 0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

a_c = sorted(Counter(a_input).items(),key=lambda x:x[0],reverse=True)

max_len=0
next_len=0
ans=0
for a in a_c:
    if a[1]<2:
        continue
    max_len = max(max_len,a[0])
    if a[1]>=4:
        ans = max(ans,a[0]**2)
    if max_len>a[0]:
        next_len = max(next_len,a[0])
print(max(ans,max_len*next_len))

Submission status: Accepted

Solution code:
from collections import Counter

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

Ac = sorted([i for i in c.keys() if c[i] >= 2], reverse=True)

if len(Ac) >= 2 and c[Ac[0]] >= 4:
    print(max(Ac[0] * Ac[1], Ac[0] * Ac[0]))
elif len(Ac) >= 2:
    print(Ac[0] * Ac[1])
elif len(Ac) == 1 and c[Ac[0]] >= 4:
    print(Ac[0] * Ac[0])
else:
    print(0)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(A)
max_1 = 0
max_2 = 0
for k, v in c.items():
    if v >= 2:
        if k > max_1:
            if v >= 4:
                max_1, max_2 = k, k
            else:
                max_1, max_2 = k, max_1
        elif k > max_2:
            max_2 = k

print(max_1*max_2)

Submission status: Accepted

Solution code:
n = int(input())
A = sorted(list(map(int,input().split())),reverse=True)
from collections import defaultdict
dic = defaultdict(int)
for i in range(len(A)):
    dic[A[i]] += 1
rec = False
ans = 0
nya = []
for k,v in dic.items():
    nya.append([k,v])
nya = sorted(nya,key = lambda x:x[0],reverse = True)
#print(nya)
for k,v in nya:
    if v // 2 >= 1 and not rec:
        if v//4 >= 1:
            ans = k**2
            break
        else:
            po = k
            rec = True
    elif rec:
        if v // 2 >=1:
            ans = po * k
            break
print(ans)

Submission status: Accepted

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

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

for k, v in cnt.items():
    if v>=2:
        l.append(k)

l.sort(reverse=True)

if len(l)==0:
    print(0)
elif len(l)==1:
    if cnt[l[0]]>=4:
        print(l[0]*l[0])
    else:
        print(0)
else:
    if cnt[l[0]]>=4:
        print(l[0]*l[0])
    else:
        print(l[0]*l[1])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())

a = tuple(map(int,input().split()))

c = Counter(a)

temp = sorted(c.items())

h = 0
w = 0

while(len(temp)>0):
  k,v = temp.pop()
  if v>=4:
    print(k**2)
    exit()
  elif v>=2:
    h = k
    break
    
while(len(temp)>0):
  k,v = temp.pop()
  if v>=2:
    w = k
    break
    
print(h*w)

Submission status: Accepted

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

max1 = 0
max2 = 0
dic = {}
for i in range(n):
    if a[i] in dic:
        dic[a[i]] += 1
    else:
        dic[a[i]] = 1
    
for key, value in dic.items():
    if value >= 4:
        max1 = max(max1, key)
        max2 = max(max2, key)
    elif value >= 2:
        if key > max1:
            max2 = max1
            max1 = key
        elif key > max2:
            max2 = key

print(max1*max2)

Submission status: Accepted

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

A.sort(reverse=True)

from collections import defaultdict
d = defaultdict(int)
ans = 0
t = 0
for i in A:
    d[i] +=1
    if d[i] == 4:
        print(i**2)
        exit()
    elif d[i]==2 and ans ==0:
        ans +=2
        t = i  
    elif d[i] == 2:
        ans +=2 
    if ans ==4:
        print(t*i)
        exit()

print(0)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

from collections import Counter
c = Counter(a)

ans = 0

possible_e = []

for i in c.keys():
    if c[i] >= 4:
        ans = max(ans, i ** 2)
    if c[i] >= 2:
        possible_e.append(i)

possible_e.sort()

if len(possible_e) > 1:
    ans = max(ans, possible_e[-1] * possible_e[-2])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(solve())

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

max1, max2 = 0, 0

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

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

Submission status: Accepted

Solution code:
import sys
import collections

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

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc081/tasks/arc081_a

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue May 12 01:19:32 2020

@author: shinba
"""
import collections

n = int(input())

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

data = collections.Counter(A)

I = sorted(data.items(),reverse = True)
#print(I)
s1 = 0
s2 = 1
cnt1 = 0
cnt2 = 0 

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

Submission status: Accepted

Solution code:
#!python3

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

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


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions

import copy

# import heapq
# from collections import deque
# import decimal

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

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


# ===CODE===

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

    c = collections.Counter(a)

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)


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

ans.sort()    

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

Submission status: Accepted

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



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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

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

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

else:
  print(0)

Submission status: Accepted

Solution code:
from collections import Counter


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

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


print(solve())

Submission status: Accepted

Solution code:
from collections import Counter


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

Submission status: Accepted

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

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

Submission status: Accepted

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

li.sort(reverse=True)

check = -1
flag = 0
ans = []

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

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

Submission status: Accepted

Solution code:
n =int(input())

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

a.sort()

c=b=0

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
import os
import math
import bisect
import collections

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

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

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

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


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

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

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

    print(o * t)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

square_max = 0
rectan_a = 0
rectan_b =0

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