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 = sorted(list(map(int, input().split())))
a.reverse()
ans = []
c = 1
for i in range(1, n):
    if len(ans) >= 2:
        break
    if a[i] == a[i - 1]:
        c += 1
        m = a[i]
    else:
        if c >= 2:
            ans.append([a[i - 1], c])
        c = 1
if len(ans) < 2 and c >= 2:
    ans.append([a[n - 1], c])

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
b = []
i = 0
while i+1<N:
    if len(b)==2:
        break
    if a[i] == a[i+1]:
        b += [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())
stics = [int(i) for i in input().split()]

c = Counter(stics)

cnt = [k for k, v in c.items() if v >= 2]

if len(cnt) < 2:
    print(0)
else :
    cnt = sorted(cnt, reverse=True)
    if c[cnt[0]] >= 4:
        print(cnt[0] * cnt[0])
    else :
        print(cnt[0] * cnt[1])

Submission status: Accepted

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

exist_dict = {}
first_max = 0
second_max = 0

for i in l:
    if i in exist_dict:
        if exist_dict[i]==1 and i > first_max:
            second_max = first_max
            first_max = i
        elif exist_dict[i]==3 and i==first_max:
            second_max = i
        elif exist_dict[i]==1 and i > second_max:
            second_max = i
    
        exist_dict[i] = exist_dict[i] + 1
    else:
        exist_dict[i] = 1
    # print("first_max"+str(first_max))
    # print("second_max"+str(second_max))
    # print("exist_dict"+str(exist_dict))    
print(first_max*second_max)

Submission status: Accepted

Solution code:
from collections import Counter

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

l = Counter(lines)

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

if len(l.keys()) <= 1:
  print(0)
else:
  anss = list(l.keys())
  ma  = max(anss)
  ma2 = sorted(anss)[-2]
  print(ma * ma2 if l[ma] < 4 else ma ** 2)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]

from collections import Counter
Aco = Counter(A)

ing = []
ing2 = []
for a in Aco.keys() :
    if Aco[a] >= 2 :
        ing.append(a)
    if Aco[a] >= 4 :
        ing2.append(a)

ing.sort(reverse = True)
ing2.sort(reverse = True)
if ing2 :
    if ing2[0] == ing[0] :
        print(ing2[0]**2)
        exit()

if len(ing) <= 1 :
    print(0)
else :
    print(ing[0] * ing[1])

Submission status: Accepted

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

n = int(input())
a = list(map(int, input().split()))
cnt = 0
check = 0
ans = 1

a.sort(reverse=True)
for i in range(n - 1):
    if a[cnt] == a[cnt + 1]:
        check += 1
        ans *= a[cnt]
        cnt += 2
        if check == 2:
            print(ans)
            exit()
    else:
        cnt += 1

    if cnt == n - 1:
        break
print(0)

Submission status: Accepted

Solution code:
N = input()
a = input().split()
import numpy as np 
a = np.array(a).astype(int)
a.sort()
pai = -1
v = []
i = 0
for ai in a[::-1]:
    if pai == ai:
      v.append(ai)
      pai = -1
      i = i + 1
      if i >= 2:
          break
      continue
    pai = ai
if len(v) >= 2:
    print(v[0]*v[1])
else:
    print(0)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
from collections import Counter
d=Counter(a)
f=True
tt=0
ans=0
for i ,j in sorted(d.items(),reverse=True):
    if j==1:
        continue
    elif j>=4 and f:
        print(i*i)
        exit()
    else:
        f=False
        ans=tt*i
        tt=i
        if ans>0:
            break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int,input().split())),reverse = True)
use = []
flag = -1
for i in range(n):
  if flag == a[i]:
    use.append(a[i])
    flag = -1
  else:
    flag = a[i]
if len(use)<2:
  print(0)
else:
  use.sort(reverse =True)
  print(use[0]*use[1])

Submission status: Accepted

Solution code:
n = int(input())
d = {}
for ai in input().split():
  ai = int(ai)
  if ai not in d:
    d[ai] = 0
  d[ai] += 1

n2 = [k for k in d if d[k] > 1]
n4 = [k for k in d if d[k] > 3]

n2.sort(reverse = True)
n4.sort(reverse = True)

ans = 0

if n4:
  ans = n4[0] ** 2

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

print(ans)

Submission status: Accepted

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

sq = False
top, sec = 0,0
for k,v in c.items():
    if v < 2:
        continue
    if k > top:
        sq = v >= 4
        sec = top
        top = k
    elif k > sec:
        sec = k

if sq:
    print(top * top)
else:
    print(top * sec)

Submission status: Accepted

Solution code:
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    N = int(input())
    A = list(map(int, input().split()))
    a = sorted(A, reverse=1)
    res1 = 0
    res2 = []
    for i in range(N-3):
        if a[i]==a[i+1]==a[i+2]==a[i+3]:
            res1 = a[i]
            break
    for i in range(N-1):
        if a[i]==a[i+1] and not a[i] in res2:
            res2.append(a[i])
        if len(res2)==2:
            break
    if len(res2)<2:
        print(res1**2)
    else:
        print(max(res1**2, res2[0]*res2[1]))



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    N = int(input())
    A = list(map(int, input().split()))
    a = sorted(A, reverse=1)
    res1 = 0
    res2 = []
    for i in range(N-3):
        if a[i]==a[i+1]==a[i+2]==a[i+3]:
            res1 = a[i]
            break
    for i in range(N-1):
        if a[i]==a[i+1] and not a[i] in res2:
            res2.append(a[i])
        if len(res2)==2:
            break
    if len(res2)==0:
        print(0)
    elif len(res2)==1:
        print(res1**2)
    else:
        print(max(res1**2, res2[0]*res2[1]))



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
n = int(input())
A = list(map(int, input().split()))
B = collections.Counter(A)
s = 0; t = 0
B = sorted(B.items(), key=lambda x:x[0], reverse=True)
se = 0
for i in B:
    if i[1] > 3 and se == 0:
        se = i[0]**2
    if i[1] > 1 and s == 0:
        s = i[0]
    elif i[1] > 1 and t == 0:
        t = i[0]
print(max(s*t, se))

Submission status: Accepted

Solution code:
import collections

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

c=collections.Counter(a)

MAX1=0
MAX2=0

for i,m in c.items():
    if m>=2 and m<4:
        if i>MAX1 and i>MAX2:
            if MAX1>MAX2:
                MAX2=i
            else:
                MAX1=i
        elif MAX1>i and i>MAX2:
            MAX2=i
        elif MAX2>i and i>MAX1:
            MAX1=i
    elif m>=4:
        if i>MAX1 and i>MAX2:
            MAX1=i
            MAX2=i
        elif MAX1>i and i>MAX2:
            MAX2=i
        elif MAX2>i and i>MAX1:
            MAX1=i

print(MAX1*MAX2)

Submission status: Accepted

Solution code:
import collections

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

c=collections.Counter(a)

MAX1=0
MAX2=0

for i,m in c.items():
    if m>=2 and m<4:
        if i>MAX1 and i>MAX2:
            if MAX1>MAX2:
                MAX2=i
            else:
                MAX1=i
        elif MAX1>i and i>MAX2:
            MAX2=i
    elif m>=4:
        if i>MAX1 and i>MAX2:
            MAX1=i
            MAX2=i
        elif MAX1>i and i>MAX2:
            MAX2=i

print(MAX1*MAX2)

Submission status: Accepted

Solution code:
from collections import Counter

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

A_=Counter(A)
A_=A_.most_common()
A_.sort(reverse=True)


ans=[]
cnt=0
for a in A_:
    if a[1]==2 or a[1]==3:
        ans.append(a[0])
        cnt+=1
    elif a[1]>=4:
        ans.append(a[0])
        cnt+=1
        if cnt==1:
            ans.append(a[0])
            cnt+=1
    if cnt>=2:
        break
if len(ans)<2:
    print(0)
    exit()
print(ans[0]*ans[1])

Submission status: Accepted

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

from collections import Counter

A.sort(reverse=True)
d = Counter(A)
d = d.most_common()
d.sort(reverse=True)
x = []

for i in range(len(d)):
    if d[i][1] >= 4:
        x.append(d[i][0])
        x.append(d[i][0])
    elif d[i][1] >= 2:
        x.append(d[i][0])
        
if len(x) < 2:
    print(0)
else:   
    print(x[0]*x[1])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = []
a.sort(reverse=True)
key = a[0]
i = 1
while True:
    if i > n-1:
        break
    if len(ans) == 2:
        break
    if a[i] == key:
        i += 1
        ans.append(key)
        if i > n-1:
            break
        key = a[i]
    else:
        key = a[i]
    i += 1


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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = {}
for x in a:
    d[x] = d.get(x, 0) + 1
l = []
ans = 0
for num, cnt in sorted(d.items(), key=lambda x: x[0], reverse=True):
    if cnt > 3:
        if len(l) == 0:
            ans = num ** 2
            print(ans)
            exit()
        else:
            l.append(num)
            ans = l[0] * l[1]
            print(ans)
            exit()
    elif cnt > 1:
        l.append(num)
        if len(l) == 2:
            ans = l[0] * l[1]
            print(ans)
            exit()
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = {}
for x in a:
    d[x] = d.get(x, 0) + 1
d = sorted(d.items(), key=lambda x: x[0], reverse=True)

l = []
ans = 0
for num, cnt in d:
    if cnt > 1:
        l.append(num)
        if len(l) == 2:
            ans = l[0] * l[1]
            print(ans)
            exit()
    if cnt > 3:
        ans = num ** 2
        print(ans)
        exit()

print(ans)

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    N=I()
    A=LI()
    from collections import defaultdict
    dd = defaultdict(int)
    
    for i in range(N):
        dd[A[i]]+=1
        
    L=[0,0]
    for k,v in dd.items():
        if v>=2:
            L.append(k)
        if v>=4:
            L.append(k)
            
        
    L.sort()
    print(L[-1]*L[-2])

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n = II()
    a = LI()
    d = defaultdict(int)
    for ai in a:
        d[ai] += 1
    lis = list(d.items())
    ans = []
    for i,j in lis:
        for _ in range(j // 2):
            ans.append(i)
    ans.sort(reverse=True)
    if len(ans) < 2:
        print(0)
    else:
        print(ans[0]*ans[1])

    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
b=[]
for i in range(n-1):
    if a[i]==a[i+1]:
        b.append(a[i])
b=sorted(b,reverse=True)
if len(b)<=1:
    print(0)
elif len(b)==2 and b[0]==b[1]:
    print(0)
elif b.count(b[0])>=3:
    print(b[0]*b[0])
else:
    b=list(set(b))
    b=sorted(b,reverse=True)
    print(b[0]*b[1])

Submission status: Accepted

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
h,w,i=0,0,0
while i<n-1:
    if a[i]==a[i+1]:
        if h==0:
            h=a[i]
            i+=1 #
        else:
            w=a[i]
            break
    i+=1
print(h*w)

Submission status: Accepted

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

A.sort(reverse=True)
edges = 2
cnt = 1
ans = 1
for i in range(N-1):
    if A[i] == A[i+1]:
        cnt += 1
        continue
    if edges == 2 and cnt >= 4:
        edges -= 2
        ans *= A[i] * A[i]
    elif cnt >= 2:
        edges -= 1
        ans *= A[i]
    if edges == 0:
        break
    cnt = 1
if edges == 2 and cnt >= 4:
    edges -= 2
    ans *= A[i] * A[i]
elif edges == 1 and cnt >= 2:
    edges -= 1
    ans *= A[i]
if edges == 0:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
n = int(input())
a = list(map(int, input().split()))
count_a = list(Counter(a).items())
count_a = sorted(count_a, reverse=True)
result = 1
c = 0
for x, y in count_a:
    if y>=2:
        result *= x
        c += 1
        if c == 1 and y>=4:
            result *= x
            c += 1
        if c == 2:
            print(result)
            sys.exit()
print(0)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
n = int(input())
a = list(map(int, input().split()))
count_a = list(Counter(a).items())
count_a = sorted(count_a, reverse=True)
result = 1
c = 0
for x, y in count_a:
    if y>=2:
        result *= x
        c += 1
        if y>=4 and c == 1:
            result *= x
            c += 1
        if c == 2:
            print(result)
            sys.exit()
print(0)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
n = int(input())
a = list(map(int, input().split()))
count_a = list(Counter(a).items())
count_a = sorted(count_a, reverse=True)
result = 1
c = 0
for x, y in count_a:
    if y>=2:
        result *= x
        c += 1
        if y>=4 and c == 1:
            result *= x
            c += 1
    if c == 2:
        print(result)
        sys.exit()
print(0)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = [int(_) for _ in input().split()]
aa = collections.Counter(a).most_common()
aa = sorted(aa, reverse=True)
length = []
cnt = 0
for i in aa:
    if i[1] >= 2:
        length.append(i)
        cnt += 1
        if cnt == 2:
            break
    else:
        pass

if cnt == 2 and length[0][1] >= 4:
    print(length[0][0] * length[0][0])
elif cnt == 2:
    print(length[0][0] * length[1][0])
elif cnt == 1 and length[0][1] >= 4:
    print(length[0][0] * length[0][0])
else:
    print(0)

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    A = [int(i) for i in input().split()]
    from collections import Counter
    c = Counter(A)
    li = [(k, v) for k, v in c.items() if v >= 2]
    li.sort(reverse=True)
    if li and li[0][1] >= 4:
        print(li[0][0] * li[0][0])
    elif len(li) <= 1:
        print(0)
    else:
        print(li[0][0] * li[1][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)
m=c.most_common()
t=sorted(m,key=lambda m:m[0],reverse=True)
w=0
b=0
for i in range(len(t)):
    if(t[i][1]>=4):
        if(w==b==0):
            w=t[i][0]
            b=t[i][0]
        elif(w==0):
            w=t[i][0]
        elif(b==0):
            b=t[i][0]
    elif(t[i][1]>=2):
        if(w==0):
            w=t[i][0]
        elif(b==0):
            b=t[i][0]
print(w*b)

Submission status: Accepted

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

rec1,rec2,i=0,0,0
while i<n-1:
    if a[i]==a[i+1]:
        if rec1==0:
            rec1=a[i]
            i+=1
        else:
            rec2=a[i]
            break
    i+=1
print(rec1*rec2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(str,input().split()))
c = collections.Counter(a)
set_a = set(a)
lists = []
for i in set_a:
    if int(c[i]) >= 2:
       lists.append(int(i))
    if int(c[i]) >= 4:
        lists.append(int(i))
lists.sort()
lists.reverse()
if len(lists) > 1:
    ans = int(lists[0]) * int(lists[1])
else:
    ans = 0
print(ans)

Submission status: Accepted

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

lis = sorted(a.items(), key = lambda z: z[0], reverse = True)
ans = []
for i in lis:
    if len(ans) == 0 and i[1] >= 4:
        print(i[0]**2)
        sys.exit()
    elif i[1] >= 2 and len(ans) <= 1:
        ans.append(i[0])
    else:
        pass
if len(ans) == 2:
    print(ans[0] * ans[1])
else:
    print(0)

Submission status: Accepted

Solution code:
def main():
    sticks = int(input())
    stick_length = list(map(int, input().split()))
    length_count = {}
    for length in stick_length:
        if length in length_count:
            length_count[length] += 1
        else:
            length_count[length] = 1
    length_count = sorted(length_count.items(), key=lambda x: x[0], reverse=True)
    answer = 0
    for stick_info in length_count:
        if stick_info[1] < 2:
            continue
        elif stick_info[1] >= 4:
            if answer == 0:
                answer = stick_info[0] ** 2
                break
            else:
                answer *= stick_info[0]
                break
        else:
            if answer > 0:
                answer *= stick_info[0]
                break
            else:
                answer = stick_info[0]
    print(answer)


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)
ans = []
for i, v in cnt.items():
    if 2 <= v < 4:
        ans.append(i)
    elif 4 <= v:
        ans.append(i)
        ans.append(i)

if not ans:
    print(0)
else:
    ans.sort()
    x = ans.pop()
    y = ans.pop()
    print(x * y)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]

l = []
c = Counter(A)
for k in c.keys():
    if c[k] >= 2:
        l.append(k)
    if c[k] >= 4:
        l.append(k)
l.sort(reverse=True)
if len(l) > 1:
    print(l[0]*l[1])
else:
    print(0)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(s)

c = sorted(c.items())

max1 = 0
max2 = 0
max3 = 0

for i in range(len(c)):
    if c[i][1] >= 2:
        if c[i][0] >= max2:
            if c[i][0] >= max1:
                max2 = max1
                max1 = c[i][0]
            else:
                max2 = c[i][0]

    if c[i][1] >= 4:
        max3 = c[i][0]

if max2 == 0:
    print("0")
else:
    print(max(max1*max2, max3**2))

Submission status: Accepted

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

A = sorted(A, reverse=True)

counter = 1
edge1 = 0
flag = 0

for i in range(len(A)-1):
    if edge1 == 0:
        edge1 = A[i]
    if counter == 4:
        print(edge1*edge1)
        break
    elif A[i] == A[i+1] and counter < 4:
        counter = counter + 1
    elif A[i] != A[i+1] and counter >= 2:
        flag = 1
        start = i
        break
    else:
        edge1 = 0
else:
    print(0)
if flag == 1:
    for i in range(start+1, len(A)-1):
        if A[i] == A[i+1]:
            print(edge1*A[i])
            break
    else:
        print(0)

Submission status: Accepted

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

tmp1=a[0]
for i in range(n-1):
    l,s= 0,0
    if tmp1 == a[i+1]:
        idx = i+1
        l = tmp1 #lを確定させる
        #break
        tmp2=a[idx+1]
        for j in range(idx+1,n-1):
            if tmp2==a[j+1]:
                s = tmp2
                print(l*s)
                exit()
            else:
                tmp2 = a[j+1]
    else:
        idx=i
        tmp1=a[i+1]
        
print(0)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
d = {}
for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1

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

Submission status: Accepted

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

dic = {}
for i in range(n):
    if a[i] in dic:
        dic[a[i]] += 1
    else:
        dic[a[i]] = 1
#print(dic)
dic = sorted(dic.items(), key=lambda x:x[0])
dic.reverse()
#print(dic)
ans = 0
cnt = 0
for i in range(len(dic)):
    if dic[i][1] >= 4 and cnt == 0:
        ans = dic[i][0] ** 2
        break
    elif dic[i][1] >= 2:
        if cnt == 0:
            ans = dic[i][0]
            cnt += 1
        else:
            ans = ans * dic[i][0]
            break
print(ans)

Submission status: Accepted

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

for k,i in ac.items():
"""
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
count=0
ans4=0
ans2=[0]*2
for i in range(n-1):
    #print(a[i],i)
    if a[i]==a[i+1]:
        count+=1
    if i==(n-2) or a[i]!=a[i+1]:
        if 1<=count:
            if 3<=count:
                ans4=max(ans4,a[i])
                ans2.append(a[i])
            else:
                ans2.append(a[i])
        count=0
ans2.sort(reverse=True)
#print(ans2)  
print(max(ans4**2,ans4*ans2[0],ans2[0]*ans2[1]))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
v = sorted([(key, value) for key, value in c.items() if value != 1],
           key=lambda x: x[0],
           reverse=True)
# print(v)
if len(v) <= 0:
    print(0)
    exit()
if len(v) == 1:
    if v[0][1] >= 4:
        print(v[0][0] * v[0][0])
    else:
        print(0)
    exit()
if v[0][1] >= 4:
    print(v[0][0] * v[0][0])
else:
    print(v[0][0] * v[1][0])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
dic = {}
for a in A:
  if a in dic:
    dic[a] += 1
  else:
    dic[a] = 1
l = []
for k, v in dic.items():
  if v >= 2:
    l.append(k)
l.sort()
if len(l) == 0:
  print(0)
elif len(l) == 1:
  if dic[l[-1]] >= 4:
    print(l[-1] ** 2)
  else:
    print(0)
else:
  if dic[l[-1]] >= 4:
    print(l[-1] ** 2)
  else:
    print(l[-1] * l[-2])

Submission status: Accepted

Solution code:
def makerectangle(n , a):

    listmax = []
    a.sort(reverse=True)
    for i in range(n):
        if len(a) > i + 1 and a[i] == a[i + 1] and listmax.count(a[i]) == 0:
            if len(a) > i + 3 and a[i] == a[i + 3] and len(listmax) == 0:
                return a[i] * a[i]
            else:
                listmax.append(a[i])
        if len(listmax) > 1:
            return listmax[0] * listmax[1]
    return 0


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
l.sort(reverse=True)
a = 0
b = 0
check = -1
for i in range(n-1):
  if l[i]==l[i+1]:
    if check != i:
      if a == 0:
        a = l[i]
        check = i+1
      elif b == 0:
        b = l[i]
print(a*b)

Submission status: Accepted

Solution code:
import collections
N=int(input())
A=list(map(int,input().split()))
S = collections.Counter(A)
T=[]
sq=-1;r1=-1;r2=-1
T=S.most_common()
T.sort(reverse=True,key=lambda x: x[0])
#print(T)
for i in range(len(T)):
  if T[i][1]>3:
    if sq==-1:
      sq=i
      if r1==-1:
        r1=i
      else:
        if r2==-1:
          r2=i
  elif T[i][1]>1:
    if r1==-1:
      r1=i
    else:
      if r2==-1:
        r2=i
#print(sq,r1,r2)
if r1!=-1 and r2!=-1:
  if sq!=-1:
    print(max(T[sq][0]**2,T[r1][0]*T[r2][0]))
  else:
    print(T[r1][0]*T[r2][0])
elif sq!=-1:
  print(T[sq][0]**2)
else:
  print(0)

Submission status: Accepted

Solution code:
from collections import defaultdict


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

A.sort()

dict = defaultdict(int)

count = 0
select = []
for _ in range(n):
    length = A.pop()

    if dict[length] > 0:
        dict[length] -= 1
        select.append(length)
        count += 1
    else:
        dict[length] += 1

    if count == 2:
        break

if len(select) < 2:
    print(0)
else:
    a = select.pop()
    b = select.pop()
    print(a*b)

Submission status: Accepted

Solution code:
from collections import Counter

input()
a = sorted(filter(lambda x: x[1] > 1, Counter(map(int, input().split())).items()), reverse=True)
if a and a[0][1] >= 4:
    print(a[0][0]**2)
else:
    if len(a) < 2:
        print(0)
    else:
        print(a[0][0] * a[1][0])

Submission status: Accepted

Solution code:
from collections import Counter
from operator import itemgetter

N = int(input())
A = Counter(map(int,input().split()))
A1 = list(A.items())
A1 = sorted(A1,key=itemgetter(1),reverse = True)
A1 = sorted(A1,key=itemgetter(0),reverse = True)
#print(A1)

#正方形の場合
B =[]
for i in range(len(A1)):
    if A1[i][1] > 3:
        B.append(A1[i][0])
        if len(B) == 1:
            break
C =[]
for i in range(len(A1)):
    if A1[i][1] > 1:
        C.append(A1[i][0])
        if len(C) == 2:
            break

if len(C) == 0:
    print(0)
elif len(B) !=0:
    print(max(B[0]*B[0],C[0]*C[1]))
else:
    print(C[0]*C[1])

Submission status: Accepted

Solution code:
N = int(input())
lst = [int(x) for x in input().split()]
dct = {}
for x in lst:
    n = dct.get(x, 0) + 1
    dct[x] = n

lst = []
for k, v in sorted(dct.items(), reverse=True):
    if v <= 1:
        continue
    lst.append(k)
    if len(lst) == 1 and v // 2 >= 2:
        lst.append(k)
    if len(lst) == 2:
        print(lst[0] * lst[1])
        exit()
print(0)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n = int(readline())
lst1 = list(map(int,readline().split()))
ans = []
lst1.sort()
i = 0
while True:
    if i >= n-1:
        break
    if lst1[i] == lst1[i+1]:
        ans.append(lst1[i])
        i += 1
    i += 1
if len(ans) < 2:
    print(0)
else:
    print(ans[-1]*ans[-2])

Submission status: Accepted

Solution code:
from collections import *
n=int(input())
c=Counter(map(int,input().split()))
x=y=0
for k,v in c.items():
    if v>=2 and k>y:
        if k>=x:
            if v>=4:
                x=y=k
            else:
                x,y=k,x
        else:
            y=k
print(x*y)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = Counter(map(int, input().split()))
c = []
for k,v in a.items():
    if v >= 2:
        c.append(k)
    if v >= 4:
        c.append(k)

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

Submission status: Accepted

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

x = dict()
for i in range(N):
    if A[i] in x:
        x[A[i]] += 1
    else:
        x[A[i]] = 1

m=0
m2 = 0
flag = False
tmp=0
for idx in x:
    tmp=m
    if x[idx]>=2:
        m=max(m,idx)
        if m!=tmp:
            if x[idx]>=4:
                flag=True
            else:
                flag=False


for idx in x:
    if x[idx]>=2:
        if idx!=m:
            m2 = max(m2,idx)

if flag:
    ans = m**2
else:
    ans = m*m2
print(ans)

Submission status: Accepted

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

rec_hen = 0
#a_set = sorted(set(a), reverse=True)
acc = collections.Counter(a)
ssa = sorted(acc.items(), key=lambda x: x, reverse=True)
for i, c in ssa:
  if c >= 4:
    if rec_hen != 0:
      print(rec_hen * i)
      exit(0)
    print(i * i)
    exit(0)
  if c >= 2:
    if rec_hen != 0:
      print(rec_hen * i)
      exit(0)
    rec_hen = i

print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

def solve(n, A):
    c = Counter(A)
    cands = sorted(filter(lambda kv: kv[1] >= 2, c.items()),  key=lambda kv: -kv[0])
    ret = 0
    if len(cands) >= 1 and cands[0][1] >= 4:
        ret = cands[0][0] * cands[0][0]
    elif len(cands) >= 2:
        ret = cands[0][0] * cands[1][0]
    return ret

_n = int(input())
_A = map(int, input().split())
print(solve(_n, _A))