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

A.sort()

edge = [0] * 2
for i in range(2):
    while True:
        l = len(A)
        if len(A) < 2:
            print(0)
            exit(0)

        if A[l-2] == A[l-1]:
            edge[i] = A.pop()
            A.pop()
            break

        A.pop()

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

Submission status: Accepted

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

count = Counter(a)
ans = [0]*2
t = 0
for i in sorted(count.items(), reverse=True):
  if i[1] >= 4:
    if t == 0:
      ans[0],ans[1] = i[0],i[0]
      t = 2
    elif t == 1:
      ans[t] = i[0]
      t += 1
  elif i[1] >= 2:
    ans[t] = i[0]
    t += 1
  if t == 2: break
print(ans[0]*ans[1])

Submission status: Accepted

Solution code:
import sys
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
C = sorted(C.items(), reverse=True)
a, b = 0, 0
for k, v in C:
    if a == 0 and v >= 4:
        print(k * k)
        sys.exit()
    if v >= 2:
        if a == 0:
            a = k
        elif b == 0:
            print(a * k)
            sys.exit()
print(0)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())

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

a = Counter(lis)

slis = sorted(set(lis))

t1 = 0
t2 = 0

for k, v in a.items():
    if v >=2 and k > t1:
        t2 = t1
        t1 = k

if a[t1] >= 4:
    print(t1*t1)
    exit()

print(t1*t2)

Submission status: Accepted

Solution code:
n=int(input())
lst=sorted(map(int,input().split()),reverse=True)
memory=-1
ans1=-1
for i in range(n):
    if lst[i]==memory:
        if ans1==-1:
            ans1=memory
            memory=-1
        else:
            ans2=memory
            print(ans1*ans2)
            exit()
    else:
        memory=lst[i]

print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(500000)

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]


def main():
    N = ii()
    A = list(mi())
    dic = {}
    l = []
    for a in A:
        if not a in dic:
            dic[a] = 0
        dic[a] += 1

    for k, v in dic.items():
        if v >= 2:
            l.append(k)
        if v >= 4:
            l.append(k)
        
    l.sort(reverse=True)

    if len(l) >= 2:
        print(l[0]*l[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)

e = []
for k, v in c.items():
    if v > 3:
        e.append(k)
        e.append(k)
    elif v > 1:
        e.append(k)
e.sort(reverse=True)

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

Submission status: Accepted

Solution code:
# 2020/07/04
# AtCoder Beginner Contest 071 - C

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

a.sort()

h_long = 0
h_short = 0
i = n - 1

#for i in range(n-1, 0, -1):
while i > 0:
    if a[i-1] == a[i]:
        if h_long == 0:
            h_long = a[i]
            i = i - 1
        else:
            h_short = a[i]
            break
    i = i - 1

ans = h_short * h_long

# Output
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

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

A_collection = collections.Counter(A)
bars = []
for key in A_collection.keys():
    if A_collection[key] > 1:
        bars.append(key)

ans = 0
bars.sort(reverse = 1)
if len(bars) > 1:
    ans = bars[0] * bars[1]
    if A_collection[bars[0]] > 3:
        ans = bars[0] ** 2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
lst = list(map(int, input().split()))
mclst = Counter(lst).most_common()
sqlst = []
rclst = []
for tpl in mclst:
  if tpl[1] >= 2:
    rclst.append(tpl[0])
  if tpl[1] >= 4:
    sqlst.append(tpl[0])
sqmax, rcmax = 0, 0
if len(sqlst) >= 1:
  sqmax = max(sqlst) ** 2
if len(rclst) >= 2:
  rclst.sort(reverse=True)
  rcmax = rclst[0] * rclst[1]
print(max(sqmax, rcmax))

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)
d = dict(sorted(c.items(), key=lambda x:x[0], reverse=True))

x = 0
y = 0
for k, v in d.items():
   if v >= 4:
      if x == 0:
         x = k
         y = k
         break
      elif y == 0:
         y = k
         break
   if v >= 2:
      if x == 0:
         x = k
      elif y == 0:
         y = k
         break

ans = x * y
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

N = i()
A = l()
A.sort(reverse=True)
list = []
start = 0
while start < N-1: 
    if A[start] == A[start+1]:
        list.append(A[start])
        start += 2
    else:
        if start == N-2:
            break
        start += 1
if len(list) >= 2:
    print(list[0]*list[1])
else:
    print(0)

Submission status: Accepted

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

count = Counter(a)
t = 4
s = []
for i in sorted(count.items(), key=lambda x: x[0], reverse=True):
  if i[1] >= 4 and t == 4:
    print(i[0]**2)
    exit()
  if i[1] >= 2 and t != 0:
    t -= 2
    s.append(i[0])
  elif t == 0: break
    
if len(s) != 0: print(s[0]*s[1])
else: print(0)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split(' ')))
x=0
y=0
a.sort()
a.reverse()
for i in range(len(a)-1):
    if a[i]==a[i+1]:
        x=a[i]
        p=i+1
        break
if x!=0:
    for i in range((p+1),len(a)-1):
        if a[i]==a[i+1]:
            y=a[i]
            break
print(x*y)

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
b=[]
c=[]
d=defaultdict(int)
for i in range(n):
    d[a[i]]+=1

for i,j in d.items():
    if j>=2:
        b.append(i)
    if j>=4:
        c.append(i)

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

Submission status: Accepted

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

i=0
arr=[]
while i<n-1:
    if a[i]==a[i+1]:
        arr.append(a[i])
        i+=2
    else:
        i+=1
    if len(arr)==2:
        print(arr[0]*arr[1])
        exit()
print(0)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
A.append(0)
i=0
ans=1
d=0
while i<N:
  b=A[i]
  c=0
  while i<N and b==A[i]:
    i+=1
    c+=1
  if 2<=c<=3:
    d+=1
    ans*=b
    if d==2:
      break
  elif 4<=c:
    if d==0:
      ans*=b*b
      d=2
      break
    else:
      ans*=b
      d+=1
      break
if d!=2:
  print(0)
else:
  print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
*A, = map(int, input().split())
c = Counter(A)
ac = [(k, v) for k, v in c.items()]
ac.sort()
ac.reverse()

rest = 4
ans = 0
for k, v in ac:
    if v >= 4 and rest == 4:
        ans = k*k
        break
    elif v >= 2:
        rest -= 2
        if ans == 0:
            ans = -k
        else:
            ans *= -k
            break
print(max(ans, 0))

Submission status: Accepted

Solution code:
n = int(input())
A = tuple(map(int, input().split()))
A = sorted(A, reverse=True)

from collections import Counter
CA = Counter(A)

b = 0
for a in A:
    if CA[a] >= 2:
        CA[a] -= 2
        b = a
        break
c = 0
for a in A:
    if CA[a] >= 2:
        c = a
        break

print(b*c)

Submission status: Accepted

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

n, *l = map(int, read().split())

c = Counter(l)
sides = sorted([[x, y] for x, y in c.items() if y >= 2])
ans = 0

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

Submission status: Accepted

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

while True:
    if ans == 0:
        break
    if a[i+j] == a[i+j+1]:
        ch[1] = a[i+j]
        break
    else:
        j += 1
    if i+j == n-1:
        ans = 0

if ans == 0:
    print(0)
else:
    print(ch[0]*ch[1])

Submission status: Accepted

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

c = collections.Counter(a_lst)
cand = [k for k, v in c.items() if v >= 2]
cand = sorted(cand)

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

Submission status: Accepted

Solution code:
# Vicfred
# https://atcoder.jp/contests/abc071/tasks/arc081_a
# greedy, sorting
class Counter(dict):
    def __missing__(self, i):
        return 0

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

a.sort()
a.reverse()

counter = Counter()
for i in a:
    counter[i] += 1

first = -1
second = -1
for item in a:
    if second != -1:
        break;
    if counter[item] >= 2:
        if first == -1:
            first = item
            counter[item] -= 2
        elif second == -1:
            second = item

if first != -1 and second != -1:
    print(first*second)
else:
    print(0)

Submission status: Accepted

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

b= defaultdict(int)
ans = []
ans2 = []
for i in range(n):
    b[a[i]] += 1
    if b[a[i]] == 2:
       ans.append(a[i])
    if b[a[i]] == 4:
        ans2.append(a[i])
ans = sorted(ans,reverse=True)
ans2 = sorted(ans2,reverse=True)
if len(ans) <= 1 and len(ans2) == 0:
    print(0)
    exit()
if len(ans2) == 0:
    print(ans[0]*ans[1])
elif len(ans) <= 1:
    print(ans2[0]**2)
else:
    print(max(ans[0]*ans[1],ans2[0]**2))

Submission status: Accepted

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

while True:
    if ans == 0:
        break
    if a[i+j] == a[i+j+1]:
        ch[1] = a[i+j]
        break
    else:
        j += 1
    if i+j == n-1:
        ans = 0

if ans == 0:
    print(0)
else:
    print(ch[0]*ch[1])

Submission status: Accepted

Solution code:
from collections import *
N = int(input())
C = Counter(map(int,input().split()))
A = [0,0]
B = [0]

for k,v in C.items():
  if 2<=v:
    A+=[k]
  if 4<=v:
    B+=[k]

A.sort()
B.sort()
print(max(A[-1]*A[-2],B[-1]**2))

Submission status: Accepted

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

import collections

c = sorted(collections.Counter(a).items(), reverse=True)

leng_1 = 0
for leng,count in c:
  if count >=4:
    if leng_1 != 0:
      print(leng_1 * leng)
      exit()
    else:
      print(leng**2)
      exit()
  elif count >=2:
    if leng_1 != 0:
      print(leng_1*leng)
      exit()
    else:
      leng_1 = leng
print(0)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
square = []
s1= a[0]
c = 0
result = 0
for i in range(1, n):
    s2 = a[i]
    if s1 == s2:
        c += 1
        if c % 2 == 1:
            square.append(s1)
            if len(square) == 2:
                result = square[0] * square[1]
                break
    else:
        s1 = a[i]
        c = 0
print(result)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(input().split())
A = [int(A[i])for i in range(N)]
A_count = list(Counter(A).most_common())

rectangle = sorted([A_count[i][0] for i in range(len(A_count)) if A_count[i][1] >= 2],reverse=True)
square = sorted([A_count[i][0] for i in range(len(A_count)) if A_count[i][1] >= 4], reverse=True)

S_rec = (rectangle[0] * rectangle[1] if len(rectangle) >= 2 else 0)
S_squ = (square[0] ** 2 if len(square) >= 1 else 0)

print(max(S_rec, S_squ))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
d = Counter(list(map(int, input().split())))
ans = -1

for k in sorted(d.keys())[::-1]:
    if ans == -1 and d[k] >= 4:
        print(k*k)
        exit()
    elif ans == -1 and d[k] >= 2:
        ans = k
    elif ans != -1 and d[k] >= 2:
        print(ans*k)
        exit()

print(0)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
d = Counter(a)
ans = -1

for k in sorted(d.keys())[::-1]:
    if ans == -1 and d[k] >= 4:
        print(k*k)
        exit()
    elif ans == -1 and d[k] >= 2:
        ans = k
    elif ans != -1 and d[k] >= 2:
        print(ans*k)
        exit()

print(0)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
d = Counter(a)
ans = 0
j = False

for k, v in sorted(d.items(), key = lambda x: -x[0]):
    if v >= 4:
        if ans == 0:
            ans = k*k
            j = True
            break
        else:
            ans *= k
            j = True
            break
    elif v >= 2:
        if ans == 0:
            ans = k
        else:
            ans *= k
            j = True
            break
if j == True:
    print(ans)
else:
    print(0)

Submission status: Accepted

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

a = Counter(a)
a = {key: val for key, val in a.items() if val >= 2}
a = dict(sorted(a.items(), reverse=True))

if len(a) == 0:
    print(0)
    exit()

side1 = max(a)
a[side1] -= 2

a = {key: val for key, val in a.items() if val >= 2}
a = dict(sorted(a.items(), reverse=True))

if len(a) == 0:
    print(0)
    exit()

side2 = max(a)

print(side1 * side2)

Submission status: Accepted

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

dic={}
for i in range(N):
    tmp=S[i]
    if tmp in dic:
        dic[tmp]+=1
    else:    
        dic[tmp]=1
kouho1=0
keys = [k for k, v in dic.items() if v >= 4]
keys.sort()
keys.reverse()
if len(keys)>=1:
    kouho1=keys[0]*keys[0]

kouho2=0
keys = [k for k, v in dic.items() if v >= 2]
keys.sort()
keys.reverse()
if len(keys)>=2:
    kouho2=keys[0]*keys[1]

print(max(kouho1,kouho2))

Submission status: Accepted

Solution code:
import collections
import sys
n = int(input())
ls = list(map(int,input().split()))
c =collections.Counter(ls)
d = list(c)
p = []
q = []
mm = 0
mx3 = 0
for i in range(len(d)):
    if c[d[i]] >=4:
        p.append(d[i])
    if c[d[i]] >=2:
        q.append(d[i])
if p == [] and len(q) < 2:
    print(0)
    sys.exit()
if len(q) >= 2:
    mx1 = max(q)
    q.remove(mx1)
    mx2 = max(q)
    mm = mx1 * mx2
if p != []:
    mx3 = max(p)**2
print(max(mm,mx3))

Submission status: Accepted

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

a.sort()
ans = [0,0]
temp_1 = 0
sign = 0

for i in range(1,n+1):
  temp = a[-i]

  if temp == temp_1:
    temp_1 = 0
    ans[sign] = temp
    if sign == 1:
      break
    sign = 1
  else:
    temp_1 = temp

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

Submission status: Accepted

Solution code:
import collections
n=int(input())
c=collections.Counter(list(map(int,input().split())))
p=-1
for x in sorted(c.keys())[::-1]:
    if p==-1 and c[x]>=4:print(x*x);break
    elif p==-1 and c[x]>=2:p=x
    elif p!=-1 and c[x]>=2:print(p*x);break
else:print(0)

Submission status: Accepted

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

count = 0
area = 1

for i in range(len(lista)- 1):
  if(lista[i] == lista[i+ 1]):
    del lista[i]
    area*= lista[i]
    count+= 1
    if(count == 2):
      break
      
if(count == 2):
  print(area)
else:
  print(0)

Submission status: Accepted

Solution code:
import collections

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

CNT_A = collections.Counter(A_s).most_common()
sorted_A = sorted(CNT_A, reverse=True)


verticle = []
ans = 0


for i in range(len(CNT_A)):
    key = sorted_A[i][0]
    value = sorted_A[i][1]
    
    if (value > 1):
        verticle.append(key)
    

    if (len(verticle) == 1 and value > 3):
        ans = key * key
        break

    elif (len(verticle) >= 2):
        ans = verticle[0] * verticle[1]
        break


print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
kouho = set([])
kouho2 = set([])
for i in c.most_common():
	if i[1] >= 2:
		kouho.add(i[0])
	if i[1] >= 4:
		kouho2.add(i[0])
kouholist = list(kouho)
kouholist2 = list(kouho2)
kouholist = kouholist + kouholist2
kouholist = sorted(kouholist)[::-1]
if len(kouholist) >= 2:
	print(kouholist[0]*kouholist[1])
else:
	print(0)

Submission status: Accepted

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

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

b = collections.Counter(a)
l = []

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

l = sorted(l,reverse = True)
if not l:
    print(0)
else:
    if l[0][1] >= 4:
        print(l[0][0]**2)
    else:
        print(l[0][0]*l[1][0])

Submission status: Accepted

Solution code:
from collections import Counter

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

C = Counter(A)
C = [[k, v] for k, v in C.items() if v >= 2]
C.sort(key=lambda x:x[0], reverse=True)

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

print(ans)

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  A = list(map(int, input().split()))
  A = sorted(A, reverse = True)
  flag = True
  num = 0
  ans = 1
  for i in range(N-1):
    if A[i] == A[i+1] and flag:
      ans *= A[i]
      num += 1
      if num == 2:
        break
      flag = False
    elif not flag:
      flag = True
  if num < 2:
    ans = 0
  print(ans)
main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
x=1
l=[]
for i in range(n-1):
    if i!=n-2:
        if a[i]==a[i+1]:
            x+=1
        else:
            if x>=4:
                l.append(a[i])
                l.append(a[i])
                break
            elif x==2 or x==3:
                l.append(a[i])
                x=1
                if len(l)>=2:
                    break
    else:
        if a[i]==a[i+1]:
            x+=1
            if x>=4:
                l.append(a[i])
                l.append(a[i])
                break
            elif x==2 or x==3:
                l.append(a[i])
                x=1
                if len(l)>=2:
                    break
        else:
            if x>=4:
                l.append(a[i])
                l.append(a[i])
                break
            elif x==2 or x==3:
                l.append(a[i])
                x=1
                if len(l)>=2:
                    break
#print(a,x)
if len(l)<2:
    print(0)
else:
    print(l[0]*l[1])

Submission status: Accepted

Solution code:
def find_max_rectangle(pair):
  """
  Args:
    pair (list): reversely sorted 
  Returns:
    area (int)
  """
  if len(pair) >= 2:
    M = pair[0]
    second_max = pair[1]
    return M * second_max
  else:
    return 0



n = int(input())
L = list(map(int, input().split()))
L.sort()
pair = []
prev = None
while L:
  popped = L.pop()
  if prev == popped:
    pair.append(prev)
    prev = None
  else:
    prev = popped

print(find_max_rectangle(pair))

Submission status: Accepted

Solution code:
from collections import Counter

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

# 長さと個数の辞書
length_num = Counter(A)
# 長い順からの長さのリスト
lengths = sorted(list(length_num.keys()), reverse=True)

ans = 0
sides = []

# 長い辺の順にループ
for l in lengths:
  if length_num[l] >= 4:
    ans = max(ans, l * l)
  if length_num[l] >= 2:
    sides.append(l)
  if len(sides) == 2:
    ans = max(ans, sides[0] * sides[1])
    break

print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
import numpy as np

N = INT()
A = LIST()
A = Counter(A)

ans = 1
for k in sorted(A, reverse=True):
	if A[k] >= 4:
		if ans == 1:
			ans = k**2
			break
		else:
			ans *= k
			break
	elif A[k] >= 2:
		if ans == 1:
			ans *= k
		else:
			ans *= k
			break
else:
	ans = 0
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
import numpy as np

N = INT()
A = LIST()
A = Counter(A)
#print(A)

a = []
for x in A:
	if 4 <= A[x]:
		a.extend([x, x])
	elif 2 <= A[x]:
		a.append(x)

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

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

Submission status: Accepted

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

x = 0
y = 0
selection = sorted(list(set(A)))[::-1]
c = collections.Counter(A)
for i in selection:
    if c[i] >= 4:
        if x == 0:
            x = i
        if y == 0:
            y = i
    elif c[i] >= 2:
        if x == 0:
            x = i
        elif y == 0:
            y = i
    if x != 0 and y != 0:
        print(x * y)
        exit()

print(x * y)

Submission status: Accepted

Solution code:
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
n=I()
lis=LI()
c=Counter(lis)
nums=[]
for i in c.keys():
    if c[i]>=2:
        nums.append(i)
nums.sort(reverse=True)
#print(nums)
if len(nums)==0:
    print(0)
elif len(nums)==1:
    if c[nums[0]]>=4:
        print(nums[0]**2)
    else:
        print(0)
else:
    if c[nums[0]]>=4:
        print(nums[0]**2)
    else:
        print(nums[0]*nums[1])

Submission status: Accepted

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

import collections

c = collections.Counter(al)
temp = sorted(list(set(al)),reverse = True)
x = 0
y = 0

for i in temp:
    if c[i]>=4:
        if x ==0:
            x = i
        if y ==0:
            y = i
    elif c[i] >=2:
        if x ==0:
            x =i
        elif y ==0:
            y = i
    if x*y != 0:
        print(x*y)
        exit()    
print(x*y)

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    n = int(input())
    A = list(map(int, input().split()))
    c = Counter(A)
    l1, l2 = 0, 0
    for k, v in c.items():
        if k > l1:
            if v >= 4:
                l1, l2 = k, k
            elif 2 <= v <= 3:
                l1, l2 = k, l1
            else:
                pass
        elif l2 < k <= l1 and v >= 2:
            l2 = k
    print(l1 * l2) 

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in a:
  if i not in dict:
    dict[i] = 1
  else:
    dict[i] += 1
  
dictlis = sorted(dict.items())
count = 0
ans = 1


for i in range(len(dictlis)):
  if dictlis[len(dictlis) -1 -i][1] >= 4 and ans == 1: 
    print((dictlis[len(dictlis) -1 -i][0])**2)
    exit()
  elif  dictlis[len(dictlis) -1 -i][1] >= 2:
    count += 1
    ans = dictlis[len(dictlis) -1 -i][0]*ans
    if count == 2:
      print(ans)
      exit()
      
print(0)

Submission status: Accepted

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

now = A[0]
tateyoko = [0, 0]
count = 0
nowflag = False
for i in range(1, N):
  if (A[i] == now) and (not nowflag):
    tateyoko[count] = now
    count += 1
    nowflag = True
  else:
    nowflag = False
  if count == 2:
    break
  now = A[i]
  
print(tateyoko[0] * tateyoko[1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split(' ')))
    counter = Counter(A)
    dupl_bars = [length for length, cnt in counter.items() if cnt > 1]
    dupl_bars.sort(reverse=True)
    if len(dupl_bars) > 1:
        if counter[dupl_bars[0]] > 3:
            print(dupl_bars[0] ** 2)
        else:
            print(dupl_bars[0] * dupl_bars[1])
    else:
        print(0)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

dic = {}
for i in s:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

dic2 = sorted(dic.items(),reverse=True)#[(4, 1), (3, 1), (2, 2), (1, 2)]

i = 0
h = 0
w = 0
while i < len(dic2):
    if dic2[i][1] >= 4 and h == 0 and w == 0:
        print(dic2[i][0]**2)
        exit()
    if dic2[i][1] >= 2 and h == 0:
        h = dic2[i][0]
    elif dic2[i][1] >= 2 and h != 0:
        print(h*dic2[i][0])
        exit()
    i += 1

print(0)

Submission status: Accepted

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

dic = {}
for i in s:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

dic2 = sorted(dic.items(),reverse=True)#[(4, 1), (3, 1), (2, 2), (1, 2)]

i = 0
tmp = 0
sq = 0
while i < len(dic2):
    if dic2[i][1] >= 4 and sq == 0:
        sq = (dic2[i][0])**2
    if dic2[i][1] >= 2 and tmp == 0:
        tmp = dic2[i][0]
    elif dic2[i][1] >= 2 and tmp != 0:
        print(max(sq,tmp*dic2[i][0]))
        exit()
    i += 1

if sq == 0:
    print(0)
else:
    print(sq)

Submission status: Accepted

Solution code:
import sys
#import numpy as np
import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

#input=sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
a.sort()
res=a.pop()
ans=[]
while a:
    l=a.pop()
    if res==l:
        ans.append(l)
        if a:
            res=a.pop()
        else:
            break
    else:
        res=l
    if len(ans)==2:
        break
if len(ans)<2:
    print(0)
else:
    print(ans[0]*ans[1])

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())), reverse=True) + [0, 0, 0, 0]
l = []
for i in range(len(a) - 1):
  if a[i] == a[i + 1] and (len(l) == 0 or l[-1] != i - 1):
    l.append(i)
    if len(l) >= 2:
      break
print(a[l[0]] * a[l[1]])

Submission status: Accepted

Solution code:
M = int(input())
A = list(map(int,input().split()))
B = {}
C = [0]*2
D = [0]*2

for i in range(len(A)):
	a = str(A[i])
	# 存在したらカウントアップ
	if (a in B): B[ str(A[i]) ] += 1 # カウント
	# 存在しなかったら 1 で初期化
	else: B[ str(A[i]) ] = 1 # カウント
	# {"1":2, "2":3, "3":1}

for i in B:
	# 一個しかなかったら条件を満たさないのでスルー
	# i: string, B[i] = count
	if B[i] < 2: continue

	# 2個以上あれば長方形にスタック
	C.append(int(i))

	# 4個以上あれば正方形にスタック
	if B[i] >= 4: D.append(int(i))

# C を降順でソート
C = sorted(C, reverse=True)

rect = C[0] * C[1]
s = max(D)
square = s * s

print(max(rect,square))