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()))
ans = 0

import collections
c = collections.Counter(A)

two = []
four = []

for i in sorted(set(A)):
    a = c[i]
    
    if a > 1:
        two.append(i)
        
    if a > 3:
        four.append(i)
        
if len(two) > 0:
    two = sorted(two)
    ans = two[-1] * two[-2]
    
if len(four) > 0:
    ans = max(max(four)**2,ans)
    
print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse=True)
B = []
i = 0
while i < N-1:
    if A[i]==A[i+1]:
        B.append(A[i])
        i += 2
    else:
        i += 1
if len(B)<2:
    print(0)
else:
    print(B[0] * B[1])

Submission status: Accepted

Solution code:
from collections import Counter

N=int(input())
A=list(map(int,input().split()))
c=Counter(A)
c=c.most_common()
c.sort(reverse=True,key=lambda x:x[0])
cnt=0
H=0
W=0
for i in range(len(c)):
    if cnt==0 and c[i][1]>=4:
        H,W=c[i][0],c[i][0]
        print(H*W)
        exit()
    elif cnt==0 and c[i][1]>=2:
        H=c[i][0]
        cnt+=1
    elif cnt==1 and c[i][1]>=2:
        W=c[i][0]
        print(H*W)
        exit()
print(H*W)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = Counter(map(int, input().split()))
x = [0, 0]
for i in a.keys():
    if a[i] >= 2:
        x.append(i)
    if a[i] >= 4:
        x.append(i)
x.sort(reverse=True)
print(x[0] * x[1])

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

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

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n = n//a
    count += 1
  if a**count == n:
    return count
  else:
    return count+1

#############
# Main Code #
#############

N = I()
A = IL()
d = DD(int)
for a in A:
  d[a] += 1
  
d = [(k,d[k])for k in d]
d.sort(reverse=True)
ans = []
ans_b = []
for a,b in d:
  if b>=4:
    ans_b.append(a)
  if b>=2:
    ans.append(a)
  if len(ans)==2:
    break
    
if len(ans)==2:
  if ans_b:
    P(max(ans[0]*ans[1],ans_b[0]**2))
  else:
    P(ans[0]*ans[1])
else:
  if ans_b:
    P(ans_b[0]**2)
  else:
    P(0)

Submission status: Accepted

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

d = []
for key,value in Counter(A).items():
    if value >=4 :
        d.append(key)
        d.append(key)
    if value >= 2:
        d.append(key)

if len(d) < 2:
    print(0)
else:
    d = sorted(d,reverse=True)
    x = d[0]
    y = d[1]

    print(x*y)

Submission status: Accepted

Solution code:
N = int(input())

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

a.sort()


x = []
i = len(a)-1
#print(a)

while i > 0:
    if a[i] == a[i-1]:
        x.append(a[i])
        i-=1

    i-=1
    
    if len(x) == 2:
        print(x[0]*x[1])
        exit()



print(0)

Submission status: Accepted

Solution code:
from collections import Counter

n=int(input())
l=[int(i) for i in input().split()]
c=Counter(l).most_common()
s=sorted([key,val] for key,val in c if val>=2)

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a).most_common()
edge = sorted([key, val] for key, val in c if val >= 2)

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[0,0]
from collections import Counter as co
for k,a in co(list(map(int,input().split()))).items():
    if a>=4:l+=[k,k]
    elif a>=2:l.append(k)
    l.sort()
    l=l[-2:]

print(l[0]*l[1])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
s = set()
s.add(0)
s.add(-1)
for i in range(n-1):
  if a[i] == a[i+1]:
    s.add(a[i])
m = max(s)
s.discard(m)
k = max(s)
if a.count(m) > 3:
  print(m**2)
else:
  print(m*k)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    a=list(map(int,readline().split()))

    a.sort()
    ans1,ans2=0,0
    j=-1
    for i in range(n-1,-1,-1):
        if i==n-1:
            m=a[i]
        else:
            if a[i]!=m:
                m=a[i]
            else:
                ans1=m
                j=i
                break

    for i in range(j-1,-1,-1):
        if i==j-1:
            m=a[i]
        else:
            if a[i]!=m:
                m=a[i]
            else:
                ans2=m
                break

    print(ans1*ans2)

if __name__=="__main__":
    main()

Submission status: Accepted

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

useable = []
memo = {}

for a in A:
    try:
        memo[a] += 1
    except:
        memo[a] = 1
    if memo[a] == 2:
        useable.append(a)
        memo[a] = 0

# print(useable)

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

Submission status: Accepted

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

A_count = collections.Counter(A)

keys = sorted(list(A_count.keys()), reverse=True)

hen_list = []
flag = 0
for key in keys:
    if A_count[key] >= 4:
        hen_list.append(key)
        hen_list.append(key)
    elif A_count[key] >= 2:
        hen_list.append(key)
    if len(hen_list) >= 2:
        flag = 1
        break
if flag == 1:
    print(hen_list[0] * hen_list[1])
else:
    print(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:
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
c = Counter(A)
sticks = []
for i in c:
    s = c[i]
    if s >= 4:
        sticks.append(i)
    if s >= 2:
        sticks.append(i)
if len(sticks) < 2:
    print(0)
else:
    sticks.sort(reverse=True)
    print(sticks[0]*sticks[1])

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]
dic = Counter(A)
sticks = []
for i in dic:
    s = dic[i]
    if s>=4:
        sticks.append(i)
    if s>=2:
        sticks.append(i)
if len(sticks)<2:
    print(0)
else:
    sticks.sort(reverse=True)
    print(sticks[0]*sticks[1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

input()
lengths = tuple(map(int, input().split(' ')))
counter = Counter(lengths)

edges = []

for length in sorted(counter.keys(), reverse=True):
    if counter[length] >= 4:
        edges.append(length)
        edges.append(length)
    elif counter[length] >= 2:
        edges.append(length)

    if len(edges) >= 2:
        break

edges.append(0)
edges.append(0)

edges.sort(reverse=True)

print(edges[0] * edges[1])

Submission status: Accepted

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

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:
import collections
N = int(input())
A = list(map(int,input().split()))
A_col = collections.Counter(A)
length = []
for k,v in A_col.items():
    if v >= 4:
        length.append(k)
    if v >= 2:
        length.append(k)
length.sort()
if len(length) <= 1:
    print(0)
else:
    print(length[-1]*length[-2])

Submission status: Accepted

Solution code:
n = int(input())
A = sorted(list(map(int, input().split())), reverse = True)
l = 0
ln = []
for a in A:
    if l == a:
        ln.append(a)
        l = 0
    else:
        l = a
    if len(ln) == 2:
        break
if len(ln) == 2:
    print(ln[0]*ln[1])
else:
    print(0)

Submission status: Accepted

Solution code:
def main():
    from collections import Counter
    n, *a = map(int, open(0).read().split())
    b = Counter(a)
    *c, = map(lambda x: tuple(x), b.items())
    c.sort(key=lambda x: x[0], reverse=True)

    ans = 1
    count = 0
    for (k, v) in c:
        if v >= 4 and ans == 1:
            ans = k ** 2
            count += 2
            break
        if v >= 2:
            ans *= k
            count += 1
        if count == 2:
            break
    if count < 2:
        print(0)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = Counter(A)
rect = []

for i,j in zip(cnt.keys(),cnt.values()):
    if j >= 2:
        rect.append([i,j])

rect.sort(reverse=True)
##print(rect)
ans = 0
flag = False
finished = False
for i,j in rect:
    if flag:
        ans *= i
        finished = True
        break
    elif j < 4:
        ans = i
        flag = True
    else:
        ans = i**2
        finished = True
        break

print(0 if not finished else ans)

Submission status: Accepted

Solution code:
from collections import Counter
input()
angles = sorted(((key, value) for key, value in Counter(map(int, input().split())).items() if value > 1), key=lambda t: t[0])
try:
    square = angles[-1][0] ** 2 if angles[-1][1] > 3 else angles[-1][0] * angles[-2][0]
except:
    square = 0
print(square)

Submission status: Accepted

Solution code:
N = int(input())
List = list(map(int,input().split()))
List_t = []
List_q = []
List.sort()
for x in range(N-1):
  if List[x]==List[x+1]:
    List_t.append(List[x])
for x in range(N-3):
  if List[x]==List[x+1]==List[x+2]==List[x+3]:
    List_q.append(List[x])

if len(List_q) == 0:
  if len(List_t)<=1:
    print(0)
  else:
    M = sorted(set(List_t))[-1]
    m = sorted(set(List_t))[-2]
    print(M*m)
else:
  M_q = sorted(set(List_q))[-1]
  M_t = sorted(set(List_t))[-1]
  m = sorted(set(List_t))[-2]
  print(max([M_q**2,M_t*m]))

Submission status: Accepted

Solution code:
import sys
from collections import Counter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
a=sorted(lmp(),reverse=True)
i=0
l=[]
while i<n-1:
    if a[i]==a[i+1]:
        l.append(a[i])
        i+=1
    i+=1
if len(l)>=2:
    print(l[0]*l[1])
else:
    print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

n=int(input())
a=list(map(int,input().split()))
num=collections.Counter(a)
cnt=num.most_common()
if cnt[0][1]<=1:
    print(0)
    exit()
else:
    if cnt[1][1]<=1:
        print(0)
        exit()
    numlis=[]
    for i in cnt:
        if i[1]>=2:
            numlis.append(i)
        else:
            break
    numlis=[list(a) for a in numlis]
    numlis.sort(reverse=True)
    if numlis[0][1]>=4:
        print(numlis[0][0]**2)
    else:
        print(numlis[0][0]*numlis[1][0])

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
x1=0
x2=0
count=Counter(a)
for i in count:
  if count[i]>=4:
    if x1<=i:
      x2=i
      x1=i
  if count[i]>=2:
    if x1<=i:
      x2=x1
      x1=i
    elif x2 <= i < x1:
      x2=i
print(x1*x2)

Submission status: Accepted

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

N = int(input())
# S = input()
# N, K = map(int, input().split())
A = list(map(int, input().split()))
from collections import Counter

def main():
    cnt = Counter(A).items()
    koho_2 = []
    koho_4 = []
    for k, v in cnt:
        if v >= 4:
            koho_4.append(k)
        if v >= 2:
            koho_2.append(k)
    ans = [0]
    koho_2 = sorted(koho_2)[::-1]
    koho_4 = sorted(koho_4)[::-1]
    if len(koho_2) >= 2:
        ans.append(koho_2[0] * koho_2[1])
    if len(koho_4) >= 1:
        ans.append(koho_4[0] ** 2)
    print(max(ans))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def resolve():
	n = int(input())
	a = list(map(int, input().split()))
	t = dict()
	for i in a:
		if t.get(i):
			t[i] += 1
		else:
			t[i] = 1
	a, b = 0, 0
	for k in t:
		if t[k] >= 4:
				a = max(a, k)
				b = max(b, k)
		elif t[k] >= 2:
			if k > a:
				b = a
				a = k
	print(a*b)
resolve()

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
li2 = [0,0]
li4 = [0]
for key,value in c.items():
    if value>=4:
        li4.append(key)
    elif value>=2:
        li2.append(key)
li2.sort()
li4.sort()
ans1 = li2[-1]*li2[-2]
ans2 = li4[-1]**2
ans3 = li4[-1]*li2[-1]
ans = max(ans1,ans2)
ans = max(ans,ans3)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

adic=defaultdict(int)
for a in alist:
  adic[a]+=1
alist2=list(adic.items())
alist2.sort(reverse=True)
#print(alist2)

x1,x2=0,0
for a2 in alist2:
  if a2[1]>=4:
    x1=max(x1,a2[0])
    x2=max(x2,a2[0])
  elif a2[1]>=2:
    if x1==0:
      x1=a2[0]
    else:
      x2=a2[0]

  if x1*x2>0:
    print(x1*x2)
    break
else:
  print(0)

Submission status: Accepted

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

adic = {}
for i in range(n):
    if a[i] not in adic:
        adic[a[i]] = 0
    adic[a[i]] += 1

alist = sorted(adic.items(),reverse = True)
before = 0
after = 0

for i in range(len(alist)):
    if before == 0:
        if alist[i][1] >= 4:
            before = alist[i][0]
            after = alist[i][0]
            break
        elif alist[i][1] >= 2:
            before = alist[i][0]
    else:
        if alist[i][1] >= 2:
            after = alist[i][0]
            break
print(before * after)

Submission status: Accepted

Solution code:
import collections
n = int(input())
p = list((map(int,input().split())))
x = collections.Counter(p)
y = [i[0] for i in x.items() if i[1]>=2]
z = [i[0] for i in x.items() if i[1]>=4]
y.sort(reverse=True)
y.append(0)
y.append(0)
z.sort(reverse=True)
z.append(0)
ans = y[0]*y[1]
ans1 = z[0]*z[0]
if ans1 > ans:
    ans =ans1
print(ans)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(input())
    a = list(map(int, readline().split()))

    a.sort(reverse=True)

    e1 = 0
    e2 = 0
    prev = 0
    
    for cur in a:
        if prev == cur:
            if e1 == 0:
                e1 = cur
            else:
                e2 = cur
                print(e1*e2)
                sys.exit()
            cur = 0
        prev = cur

    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)
pair = []
# print(c)
for x, cnt in c.items():
    pair += [x] * (cnt//2)

# print(pair)
pair.sort()

ans = 0
if len(pair) >= 2:
    ans = pair[-1] * pair[-2]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
def main():
    N = int(input())
    A = [int(a) for a in input().split()]

    edge = defaultdict(int)

    for i in range(N):
        edge[A[i]] += 1

    edge = [ [k, v] for k, v in edge.items() if v >= 2 ]
    edge.sort(key = lambda x : x[0])

    if len(edge) >= 2 :
        if edge[-1][1] >= 4:
            print(max(edge[-1][0]**2, edge[-1][0]*edge[-2][0]))
        else:
            print(edge[-1][0]*edge[-2][0])
        
    elif len(edge) == 1:
        if edge[-1][1] >= 4:
            print(edge[-1][0]**2)
        else:
            print(0)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n =int(input())
a = list(map(int,input().split()))
a.sort()
x,y,i = 0,0,0
while i<n:
  if a[i-1] == a[i]:
    x,y = y,a[i]
    i += 1
  i += 1
print(x*y)

Submission status: Accepted

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
x,y=0,0
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2
b=groupby(a)
l=len(b)
for i in range(l):
    if x==0:
        if b[i][1]>=4:
            x,y=b[i][0],b[i][0]
        elif b[i][1]>=2:
            x=b[i][0]
    elif y==0:
        if b[i][1]>=2:
            y=b[i][0]
    else:
        break
print(x*y)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = [int(x) for x in input().split()]
pair = []
c = Counter(A)
for x,cnt in c.items():
  pair += [x]*(cnt//2)

pair.sort()

answer = 0
if len(pair)>= 2:
  answer = pair[-1] * pair[-2]

print(answer)

Submission status: Accepted

Solution code:
import collections
def slove(n, a):

    count = collections.Counter()
    a_list = [int(i) for i in a.split()]
    for i in a_list:
        count[i] += 1

    tmp = sorted([item for item in count.items() if item[1] >= 2])

    if len(tmp) == 0:
        return 0
    else:
        x = tmp.pop()
        if x[1] >= 4:
            return x[0] ** 2
        else:
            y = tmp.pop()[0]
            return x[0] * y

if __name__ == "__main__":
    n = int(input())
    a_list = input()
    print(slove(n, a_list))

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ac = Counter(a)
    acl = [i for i in ac if ac[i] >= 2]
    acl.sort(reverse=True)

    if len(acl) == 0:
        print(0)
    elif ac[acl[0]] >= 4:
        print(int(acl[0] * acl[0]))
    elif len(acl) <= 1:
        print(0)
    else:
        print(int(acl[0]*acl[1]))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

c = Counter(a_ls)
edges_ls = [0] * 2
for value, count in c.items():
    if count >= 2:
        edges_ls.append(value)
    if count >= 4:
        edges_ls.append(value)
edges_ls.sort()
ans = edges_ls[-1] * edges_ls[-2]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
i = 0
x = 0
y = 0
while i < n-1:
    if x == 0:
        if a[i] == a[i+1]:
            x = a[i]
            i += 1
    elif y == 0:
        if a[i] == a[i+1]:
            y = a[i]
            i += 1
    i += 1
print(x*y)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
cnt = Counter(A)
side = []
check = 4
A = sorted(set(A), reverse=True)
for i in A:
    if cnt[i] != 1:
        side.append(i)
        check -= cnt[i]
    if check <= 0:
        break
try:
    print(side[0]*side[1])
except IndexError:
    try:
        print(side[0]**2)
    except IndexError:
        print(0)

Submission status: Accepted

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

a.sort(reverse=True)

i=0
icnt=0
ai=0
while icnt<=1 and i<=n-2:
    icnt=1
    while i<=n-2 and a[i]==a[i+1]:
        ai=a[i]
        i+=1
        icnt+=1 
    i+=1

if icnt>=4:
    print(ai*ai)
elif icnt>=2:
    icnt=0
    while icnt<=1 and i<=n-2:
        icnt=1
        while i<=n-2 and a[i]==a[i+1] :
            ai2=a[i]
            i+=1
            icnt+=1
        i+=1
    if icnt>=2:
        print(ai*ai2)
    else:
        print(0)
else:
    print(0)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
#n=6
#a=[3, 1, 2, 4, 2, 1]

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

i=0
icnt=0
ai=0
while icnt<=1 and i<=n-2:
    icnt=1
#    print("3",i,a[i],a[i+1])
    while i<=n-2 and a[i]==a[i+1]:
        ai=a[i]
        i+=1
        icnt+=1 
#    print("1",i,icnt,ai)
    i+=1

#print("4",i,icnt,ai)
if icnt>=4:
    print(ai*ai)
elif icnt>=2:
    icnt=0
#    print("5",i)
    while icnt<=1 and i<=n-2:
        icnt=1
#        print("3",i,a[i],a[i+1])
        while i<=n-2 and a[i]==a[i+1] :
            ai2=a[i]
            i+=1
            icnt+=1
#        print("2",i,icnt,ai2)
        i+=1
    if icnt>=2:
        print(ai*ai2)
    else:
        print(0)
else:
    print(0)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
a.sort(reverse=True)
key = []
p = -1
for i in range(1, n):
  if p == i:
    continue
  if a[i] == a[i-1]:
    key.append(a[i])
    if len(key) == 2:
      break
    p = i+1
if len(key) < 2:
  print(0)
else:
  print(key[0]*key[1])

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    d = {}
    for a in A:
        if not a in d:
            d[a] = 1
        else:
            d[a] += 1
    tup = sorted([(k, v) for k, v in d.items() if v > 1], key=lambda x: x[0])
    #print(tup)
    hd = []
    while len(tup) > 0:
        length, cnt = tup.pop()
        if cnt >= 4:
            hd.append(length)
        if cnt >= 2:
            hd.append(length)
        if len(hd) >= 2:
            print(hd[0]*hd[1])
            return
    print(0)




if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
import collections
import sys
sys.setrecursionlimit(2000000)
# import math
# import itertools
# import statistics
# import numpy as np
n = int(input())
a = list(map(int, input().split()))
a_count = collections.Counter(a)
a_list = list(a_count.items())
a_sorted = sorted(a_list, key=lambda x: x[0], reverse=True)
# print(a_sorted)

ans = []
kouho = []
for i in range(len(a_sorted)):
    if a_sorted[i][1] >= 4:
        b = a_sorted[i][0]
        kouho.append(b)
        ans.append(b*b)
    if a_sorted[i][1] >= 2:
        kouho.append(a_sorted[i][0])


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

Submission status: Accepted

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

def main():
    N = int(readline())
    A = list(map(int, readline().rstrip().split()))
    c = Counter(A)
    A = [a for a in A if c[a] > 1]
    if len(A) < 4:
        print(0)
        exit()
    c = Counter(A)
    keys = c.keys()
    vals = c.values()
    z = zip(keys, vals)
    z = sorted(z, reverse=True)
    keys, vals = zip(*z)
    if vals[0] >= 4:
        a1, a2 = keys[0], keys[0]
    else:
        a1, a2 = keys[0], keys[1]
    print(a1 * a2)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N=int(input())
    A=list(map(int,input().split()))
    d=defaultdict(int)
    t=[0]
    f=[0]
    for x in A:
        d[x]+=1
        if d[x]==2:
            t.append(x)
        elif d[x]==4:
            f.append(x)
    t.sort()
    f.sort()
    if len(t)>=2:
        print(max(f[-1]**2,t[-1]*t[-2]))
    else:
        print(0)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

i = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
li = list(set(li))
li.sort(reverse=True)
ans = 1
x = 0
i = 0

for i in li:
    if c[i] > 3 and x == 0:
        ans = i**2
        x += 2
    elif c[i] > 1:
        ans *= i
        x += 1
    if x == 2:
        break

if x == 2:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
cnt = defaultdict(int)
data = []
for i in range(n):
    cnt[a[i]] += 1

if sum(v >= 2 for v in cnt.values()) < 2:
    print(0)
elif cnt[max(cnt.keys())] >= 4:
    print(max(cnt.keys()) ** 2)

else:
    for k, v in cnt.items():
        if v >= 2:
            data.append(k)
    data.sort(reverse = True)
    print(data[0] * data[1])

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
count = Counter(A)
keys = list(count.keys())
keys.sort(reverse = True)
a = 0
b = 0
for k in keys:
    if a == 0:
        if count[k] >= 4:
            a = b = k
            break
        elif count[k] >= 2:
            a = k
    else:
        if count[k] >= 2:
            b = k
            break
print(a * b)

Submission status: Accepted

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


d = {}


for i in range(N):
    if A[i] in d:
        d[A[i]] += 1
        continue
    d[A[i]] = 1

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

a = []

for key, data in d:
    if data>=4:
        a.append(key)
        a.append(key)
    elif data>=2:
        a.append(key)

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

Submission status: Accepted

Solution code:
# solution
import io
import math

data=int(input())

array=list(map(int, input().split()))
array.sort()
tmp=[]
i=data-1
while i>0:
  if array[i]==array[i-1]:
    tmp.append(array[i])
    i-=2
  else:
    i-=1
if len(tmp)>=2:
  print(tmp[0]*tmp[1])
else:
  print(0)