CodeEff/ECCO huggingface dataset:
# Original solution
n,m=list(map(int,input().split()))
A=sorted(list(map(int,input().split())))

# M回の握手の幸福度最大化
# X以上になる握手を全て採用したときに、M通りを越える
# Xの最大値を求める

import bisect

ok=-1
ng=(10**10)+2

# x以上の握手をm通り以上作れるか
def isOk(x,m):
  cnt=0
  # 左手をループする
  for i in range(len(A)):
    left=A[i]
    minright=x-left
    # minright以上の数を数える
    cnt+=n-bisect.bisect_left(A,minright)
    if cnt>=m:
      return True
  return cnt>=m

while abs(ok-ng)>1:
  mid=abs(ok+ng)//2
  if isOk(mid,m):
    ok=mid
  else:
    ng=mid

x=ok
# x以上となる幸福度を足し合わせる
# 足し合わせた握手がMを越える場合、xぴったりを足し過ぎている

# 効率化のため累積和を作成
Asum=[0]+A
for i in range(1,len(Asum)):
  Asum[i]=Asum[i-1]+Asum[i]
  
# Aの添字i以降を全て足すときは、
# Asum[-1]-Asum[i]

ans=0
cnt=0
for i in range(len(A)):
  left=A[i]
  minright=x-left
  start=bisect.bisect_left(A,minright)
  cnt+=n-start
  ans+=left*(n-start)+(Asum[-1]-Asum[start])
  
if cnt>m:
  ans-=(cnt-m)*x
  
print(ans)


# Optimized solution
n,m=list(map(int,input().split()))
A=sorted(list(map(int,input().split())))

# M回の握手の幸福度最大化
# X以上になる握手を全て採用したときに、M通りを越える
# Xの最大値を求める

import bisect

ok=-1
ng=(10**10)+1

# x以上の握手をm通り以上作れるか
def isOk(x,m):
  cnt=0
  # 左手をループする
  for i in range(len(A)):
    left=A[i]
    minright=x-left
    # minright以上の数を数える
    cnt+=n-bisect.bisect_left(A,minright)
    if cnt>=m:
      return True
  return cnt>=m

while abs(ok-ng)>1:
  mid=abs(ok+ng)//2
  if isOk(mid,m):
    ok=mid
  else:
    ng=mid

x=ok
# x以上となる幸福度を足し合わせる
# 足し合わせた握手がMを越える場合、xぴったりを足し過ぎている

# 効率化のため累積和を作成
Asum=[0]+A
for i in range(1,len(Asum)):
  Asum[i]=Asum[i-1]+Asum[i]
  
# Aの添字i以降を全て足すときは、
# Asum[-1]-Asum[i]

ans=0
cnt=0
for i in range(len(A)):
  left=A[i]
  minright=x-left
  start=bisect.bisect_left(A,minright)
  cnt+=n-start
  ans+=left*(n-start)+(Asum[-1]-Asum[start])
  
if cnt>m:
  ans-=(cnt-m)*x
  
print(ans)
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

A.sort()

dp = [[0 for _ in range(N)] for _ in range(N)]

for i in range(N):
    for j in range(N):
        dp[i][j] = A[i] + A[j]

flat = []
for p in dp:
    flat.extend(p)
flat.sort()
print((sum(flat[-M:])))

# Optimized solution
n, m = list(map(int, input().split()))
A = list(map(int, input().split()))

A.sort(reverse=True)

def naive_main():
    hands = []
    for i in A:
        for j in A:
            hands.append(i + j)
    hands.sort(reverse=True)
    print((sum(hands[:m])))

naive_main()
# Original solution
#!/usr/bin/env python3
import sys
import heapq

INF = float("inf")


def solve(N: int, M: int, A: "List[int]"):
    A.sort()
    # print(A)
    heap = [(-2*a, i, i) for i, a in enumerate(A)]
    heapq.heapify(heap)

    ans = 0
    while M > 0:
        score, i, j = heapq.heappop(heap)
        # print(-score, (A[i], A[j]))
        if i == j:
            ans += -score
            M -= 1
        elif i != j and M >= 2:
            ans += -2*score
            M -= 2
        else:
            ans -= score
            M -= 1

        if j != 0:
            heapq.heappush(heap, (-(A[i]+A[j-1]), i, j-1))
    print(ans)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, M, A)


if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
from bisect import bisect_left
from itertools import accumulate

INF = float("inf")


def solve(N: int, M: int, A: "List[int]"):
    A.sort()

    # 幸福度X以上を得られる握手をカウントし、M以上となる境界を探す
    # Xが小さい方が握手の個数は多く、Xが大きい方が握手の個数は少ない
    left = 0
    right = 3*(10**5)
    while right - left > 1:
        X = (right + left)//2

        # 握手の個数は、左手を固定した上で、右手をにぶたんする。
        tot = 0
        for left_hand in A:
            tot += N-bisect_left(A, X-left_hand)

        if tot < M:
            right = X
        else:
            left = X

    # print(left, right)
    # 幸福度rightを得られる握手はM個未満
    # 幸福度leftを得られる握手はM個以上

    # 幸福度rightを得られる握手をすべてカウントした上で、
    # 不足する個数だけ、幸福度leftを加える
    happy = 0

    acc = [0]+list(accumulate(A))
    tot = 0
    for left_hand in A:
        i = bisect_left(A, right-left_hand)
        tot += N-i
        happy += (acc[N]-acc[i]) + left_hand*(N-i)

    happy += (M-tot)*left
    print(happy)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, M, A)


if __name__ == '__main__':
    main()
# Original solution
import bisect
import copy
n,m=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()
ng=-1
ok=A[-1]*2+1
B=[0]*n
C=[0]*n
while ok-ng>1:
  mid=(ok+ng)//2
  d=0
  for i in range(n):
    c=n-bisect.bisect_right(A,mid-A[i],lo=0,hi=len(A))
    B[i]=c
    d=d+c
  if d<m:
    ok=mid
    C=copy.deepcopy(B)
  else:
    ng=mid
D=[0]
for i in range(n):
  D.append(D[-1]+A[n-i-1])
ans=0
for i in range(n):
  ans=ans+A[i]*C[i]+D[C[i]]
ans=ans+(m-sum(C))*ok
print(ans)

# Optimized solution
import bisect
import copy
n,m=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()
ng=-1
ok=A[-1]*2+1
B=[0]*n
C=[0]*n
while ok-ng>1:
  mid=(ok+ng)//2
  d=0
  for i in range(n):
    c=n-bisect.bisect_right(A,mid-A[i],lo=0,hi=len(A))
    B[i]=c
    d=d+c
  if d<m:
    ok=mid
    C=copy.deepcopy(B)
  else:
    ng=mid
D=[0]
for i in range(n):
  D.append(D[-1]+A[n-i-1])
ans=0
for i in range(n):
  x=C[i]
  ans=ans+A[i]*x+D[x]
ans=ans+(m-sum(C))*ok
print(ans)
# Original solution
import bisect
import copy
n,m=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()
ng=-1
ok=A[-1]*2+1
B=[0]*n
C=[0]*n
while ok-ng>1:
  mid=(ok+ng)//2
  d=0
  for i in range(n):
    c=n-bisect.bisect_right(A,mid-A[i],lo=0,hi=len(A))
    B[i]=c
    d=d+c
  if d<m:
    ok=mid
    C=copy.deepcopy(B)
  else:
    ng=mid
D=[0]
for i in range(n):
  D.append(D[-1]+A[n-i-1])
ans=0
for i in range(n):
  x=C[i]
  ans=ans+A[i]*x+D[x]
ans=ans+(m-sum(C))*ok
print(ans)

# Optimized solution
import bisect
n,m=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()
ng=-1
ok=A[-1]*2+1
B=[[0]*n for k in range(2)]
ii=0
while ok-ng>1:
  mid=(ok+ng)//2
  d=0
  for i in range(n):
    c=n-bisect.bisect_right(A,mid-A[i],lo=0,hi=len(A))
    B[ii][i]=c
    d=d+c
  if d<m:
    ok=mid
    ii=(ii+1)%2
  else:
    ng=mid
D=[0]
for i in range(n):
  D.append(D[-1]+A[n-i-1])
ans=0
for i in range(n):
  x=B[(ii+1)%2][i]
  ans=ans+A[i]*x+D[x]
ans=ans+(m-sum(B[(ii+1)%2]))*ok
print(ans)
# Original solution
from itertools import product

n, m = list(map(int, input().split()))
a = list(reversed(sorted(list(map(int, input().split())))))
c = len(a)
ans = 0
i, j = 0, 0

li = list(product(a, a))
li.sort(key=lambda x: sum(x), reverse=True)
for i in range(m):
    ans += sum(li[i])
print(ans)


# Optimized solution
from itertools import product

n, m = list(map(int, input().split()))
a = list(reversed(sorted(list(map(int, input().split())))))
c = len(a)
i, j = 0, 0

li = [sum(x) for x in product(a, a)]
li.sort(reverse=True)
print((sum(li[:m])))
# Original solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()
if n==1:
    print((a[0]*2))
    exit()
from bisect import bisect_left,bisect_right
#幸福度がx以上がm以上か？
def f(x):
    global n,a
    ret=0
    for i in range(n):
        ret+=(n-bisect_left(a,x-a[i]))
    return ret
l,r=-1,10**6
while l+1<r:
    k=l+(r-l)//2
    if f(k)>=m:
        l=k
    else:
        r=k
#print(l,r)
#print(f(l),f(r))
if f(r)>=m:
    co,ans=0,0
    for i in range(n):
        co+=(n-bisect_right(a,r-a[i]))
        ans+=(n-bisect_right(a,r-a[i]))*a[i]
    print((2*ans+(m-co)*r))
else:
    co,ans=0,0
    for i in range(n):
        co+=(n-bisect_right(a,l-a[i]))
        ans+=(n-bisect_right(a,l-a[i]))*a[i]
    print((2*ans+(m-co)*l))

# Optimized solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()
if n==1:
    print((a[0]*2))
    exit()
from bisect import bisect_left,bisect_right
#幸福度がx以上がm以上か？
def f(x):
    global n,a
    ret=0
    for i in range(n):
        ret+=(n-bisect_left(a,x-a[i]))
    return ret
#端がやはりコーナーケース(ここまじでバグらせんな)
l,r=-1,10**6
while l+1<r:
    k=l+(r-l)//2
    if f(k)>=m:
        l=k
    else:
        r=k
co,ans=0,0
for i in range(n):
    co+=(n-bisect_right(a,l-a[i]))
    ans+=(n-bisect_right(a,l-a[i]))*a[i]
print((2*ans+(m-co)*l))
# Original solution
# 上からm番目がx
# x以上のヒトがm人以上、x+1以上のヒトがm-1人以下
# x以上のヒトがm人以上となる最大のx
from bisect import bisect_left
from itertools import accumulate

n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
ok = 0
ng = 10 ** 15

B = [0] + A[::-1]
C = list(accumulate(B))

while ng - ok > 1:
    mid = (ok + ng) // 2
    cnt = 0
    for i in range(n):
        gendo = mid - A[i]
        position = bisect_left(A, gendo)
        if position == n:
            continue
        kosuu = n - position
        cnt += kosuu
    if cnt >= m:
        ok = mid
    else:
        ng = mid
ans = 0
cnt = m
for i in range(n):
    gendo = ok - A[i]
    position = bisect_left(A, gendo)
    if position == n:
        continue
    kosuu = n - position
    ans += C[kosuu] + kosuu * A[i]
    cnt -= kosuu
ans += cnt * ok
print(ans)


# Optimized solution
# 上からm番目がx
# x以上のヒトがm人以上、x+1以上のヒトがm-1人以下
# x以上のヒトがm人以上となる最大のx
from bisect import bisect_left
from itertools import accumulate
import sys

input = sys.stdin.buffer.readline


n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
ok = 0
ng = 3 * (10 ** 5)

B = [0] + A[::-1]
C = list(accumulate(B))

while ng - ok > 1:
    mid = (ok + ng) // 2
    cnt = 0
    for i in range(n):
        gendo = mid - A[i]
        position = bisect_left(A, gendo)
        if position == n:
            continue
        kosuu = n - position
        cnt += kosuu
    if cnt >= m:
        ok = mid
    else:
        ng = mid
ans = 0
cnt = m
for i in range(n):
    gendo = ok - A[i]
    position = bisect_left(A, gendo)
    if position == n:
        continue
    kosuu = n - position
    ans += C[kosuu] + kosuu * A[i]
    cnt -= kosuu
ans += cnt * ok
print(ans)
# Original solution
import bisect
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()


def guchoku(n, m, a):
    tmp = []
    for i in range(n):
        for j in range(n):
            tmp.append(a[i]+a[j])
    tmp.sort(reverse=True)
    return sum(tmp[:m])


def C(mid):
    tmp = []
    for i in a:
        pos = bisect.bisect_right(a, mid-i)
        #tmp += n-pos
        tmp.append(n-pos)
    #print(mid, tmp)
    return sum(tmp) >= m, tmp


ub = a[-1]*2
lb = 0
while ub - lb > 1:
    mid = (ub + lb) // 2  # 仮の答え、これを満たす物の個数を数えたい
    c, order = C(mid)
    if c:
        lb = mid
    else:
        ub = mid

ans = 0
cnt = 0
for x in a[::-1]:
    for y in a[::-1]:
        if x + y <= ub:
            break
        ans += x + y
        cnt += 1
# print(order)

ans += (m-cnt)*ub
print(ans)

# Optimized solution
import bisect

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
B = [0] * N
B[-1] = A[-1]
for i in range(N - 2, -1, -1):
    B[i] = B[i+1]+A[i]


def C(mid):
    tmp = 0
    for a in A:
        pos = bisect.bisect_right(A, mid - a)
        tmp += N-pos
    return tmp > M


lb = 0
rb = 10**6
while rb - lb > 1:
    happy = (lb + rb) // 2
    if C(happy):
        lb = happy
    else:
        rb = happy

ans = 0
cnt = 0
for a in A:
    pos = bisect.bisect_right(A, rb - a)
    if pos == N:
        continue
    ans += B[pos]+(N-pos)*a
    cnt += N - pos
ans += (M-cnt)*rb
print(ans)
# Original solution
import bisect

N = int(eval(input()))
a = list(map(int,input().split(' ')))
a.sort(reverse = True)


fathers = [a.pop(0)]
for _ in range(N):
    sons = list()
    for father in fathers:
        for index,value in enumerate(a):
            if father > value:
                sons.append(a.pop(index))
                break

    fathers.extend(sons)

if len(fathers) == 2**N:
    print('Yes')
else:
    print('No')


# Optimized solution
N = int(eval(input()))
A = sorted((int(x) for x in input().split()))
 
parent = [A[-1]]
rest = A[:-1]
bl = True
for _ in range(N):
    next_parent = []
    next_rest = []
    for p in parent[::-1]:
        while rest:
            x = rest.pop()
            if x < p:
                next_parent.append(x)
                break
            else:
                next_rest.append(x)
                continue
    parent += next_parent
    parent.sort()
    rest = rest + next_rest[::-1]
 
bl = len(parent) == (2**N)
answer = 'Yes' if bl else 'No'
print(answer)
# Original solution
N = int(eval(input()))
a = list(map(int,input().split(' ')))
a.sort(reverse = True)
 
 
fathers = [a.pop(0)]
for _ in range(N):
    sons = []
    b = []

    for father in fathers:
        while a:
            x = a.pop(0)
            if father > x:
                sons.append(x)
                break
            else:
                b.append(x)
                continue
                
    a = b + a
    fathers += sons
    fathers.sort(reverse = True)
    


if len(fathers) == 2**N:
    print('Yes')
else:
    print('No')


# Optimized solution

N = int(eval(input()))
a = list(map(int,input().split(' ')))
a.sort()
 
 
fathers = [a.pop()]
for _ in range(N):
    sons = list()
    b = list()

    for father in fathers:
        while a:
            x = a.pop()
            if father > x:
                sons.append(x)
                break
            else:
                b.append(x)
                
    a = a + b[::-1]
    fathers.extend(sons)
    fathers.sort(reverse = True)
    


if len(fathers) == 2**N:
    print('Yes')
else:
    print('No')
# Original solution
from collections import Counter
n = int(eval(input()))
a = [int(item) for item in input().split()]
cnt = Counter(a)

key_num = len(list(cnt.keys()))
rank = [0] * key_num 
keys = list(cnt.keys())
keys.sort(reverse=True)
for i, key in enumerate(keys):
    rank[i] = cnt[key]
cur = [0] * key_num 
cur[0] = 1
for i in range(n):
    tmp = cur[:]
    for j in range(key_num-1):
        par = tmp[j]
        for k in range(j+1, key_num):
            if rank[k] < cur[k] + par and par > 0:
                cur[k] = rank[k]
                par -= rank[k] - cur[k]
            else:
                cur[k] += par
                par = 0
                break
if sum(cur) == 2**n:
    print("Yes")
else:
    print("No")

# Optimized solution
from collections import Counter
n = int(eval(input()))
a = [int(item) for item in input().split()]
cnt = Counter(a)

key_num = len(list(cnt.keys()))
rank = [0] * key_num 
keys = list(cnt.keys())
keys.sort(reverse=True)
for i, key in enumerate(keys):
    rank[i] = cnt[key]
cur = [0] * key_num 
cur[0] = 1
for i in range(n):
    tmp = cur[:]
    free = 0
    for j in range(key_num):
        if rank[j] > cur[j] and free > 0:
            if cur[j] + free > rank[j]:
                free -= rank[j] - cur[j]
                cur[j] = rank[j]
            else:
                cur[j] += free
                free = 0
        free += tmp[j] 
if sum(cur) == 2**n:
    print("Yes")
else:
    print("No")
# Original solution
import sys
import bisect
input = sys.stdin.readline

def main():
    N = int(eval(input()))
    S = [int(s) for s in input().split()]
    S.sort()

    cur = []
    cur.append(S.pop(-1))

    for i in range(N):
        nxt = [j for j in cur]
        bl = len(S)
        for x in cur:
            bl = bisect.bisect_left(S,x,lo=0,hi=bl) - 1
            if bl==-1:
                return("No")
            else:
                nxt.append(S.pop(bl))
        nxt.sort(reverse=True)
        cur = [j for j in nxt]
    return("Yes")

if __name__ == "__main__":
    print((main()))

# Optimized solution
import sys

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])


def main():
    N = I()
    S = sorted(LI(),reverse=True)

    flag = [True]*len(S)

    cur = []
    cur.append(S[0])
    flag[0] = False

    for i in range(N):
        nxt = [x for x in cur]
        j = 0

        for k in range(len(S)):
            if j >= len(cur):
                break
            elif flag[k] and S[k]<cur[j]:
                nxt.append(S[k])
                j+=1
                flag[k] = False
        if len(cur) != j:
            return("No")
        cur = sorted(nxt,reverse=True)
    return("Yes")

if __name__ == "__main__":
    print((main()))
# Original solution
import sys

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])


def main():
    N = I()
    S = sorted(LI(),reverse=True)

    flag = [True]*len(S)

    cur = []
    cur.append(S[0])
    flag[0] = False

    for i in range(N):
        nxt = [x for x in cur]
        j = 0

        for k in range(len(S)):
            if j >= len(cur):
                break
            elif flag[k] and S[k]<cur[j]:
                nxt.append(S[k])
                j+=1
                flag[k] = False
        if len(cur) != j:
            return("No")
        cur = sorted(nxt,reverse=True)
    return("Yes")

if __name__ == "__main__":
    print((main()))

# Optimized solution
import sys

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])


def main():
    N = I()
    S = sorted(LI(),reverse=True)

    flag = [True]*len(S)

    cur = []
    nxt = []
    cur.append(S[0])
    nxt.append(S[0])
    
    flag[0] = False

    for i in range(N):
        j = 0
        for k in range(len(S)):
            if j >= len(cur):
                break
            elif flag[k] and S[k]<cur[j]:
                nxt.append(S[k])
                j+=1
                flag[k] = False
        if len(cur) != j:
            return("No")
        nxt.sort(reverse=True)
        cur = [x for x in nxt]
    return("Yes")

if __name__ == "__main__":
    print((main()))
# Original solution
from collections import Counter
N = int(eval(input()))
S = list(map(int,input().split()))
count = Counter(S)
S = sorted(S)
T = sorted(list(set(S)))
M = len(T)
pre = {T[i]:T[i-1] if i > 0 else 0 for i in range(M)}

l = [S[-1]]
count[S[-1]] -= 1

while len(l) < 2**N:
    for n in l[:]:
        m = pre[n]
        if m == 0:
            print('No')
            exit()
        while count[m] == 0:
            pre[n] = pre[m]
            m = pre[n]
            if m == 0:
                print('No')
                exit()
        l.append(m)
        count[m] -= 1

print('Yes')



# Optimized solution
from collections import Counter
N = int(eval(input()))
S = list(map(int,input().split()))
#N = 18
#S = list(range(1,2**N+1))

def solve():
    global S
    S += [0]
    count = Counter(S)
    S = sorted(S)
    T = sorted(list(set(S)))
    M = len(T)
    pre = {T[i]:T[i-1] if i > 0 else 0 for i in range(M)}

    l = [S[-1]]
    count[S[-1]] -= 1

    while len(l) < 2**N:
        for n in l[:]:
            m = pre[n]
            pl = []
            while count[m] == 0:
                pl.append(m)
                m = pre[m]
            if pl:
                pre[n] = m
                for p in pl:
                    pre[p] = m
            if m == 0:
                print('No')
                exit()
            l.append(m)
            count[m] -= 1

    print('Yes')
    #print(count)

solve()
# Original solution
from sys import exit, stderr, stdin
input = stdin.readline
# setrecursionlimit(10**7)
from collections import deque
import heapq
def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N = int(input())
    S = [int(a) for a in input().split()]
    S = sorted(S,key=lambda x:x)
    # debug(S,'S')
    live = [S[-1]]
    S.pop()
    # iはステップ
    for i in range(N):
        # 各スライムに生んでもらう
        livenext = [l for l in live]
        for l in live:
            borned = False
            for j in range(len(S)-1, -1, -1):
                if l > S[j]:
                    livenext.append(S[j])
                    S.remove(S[j])
                    borned = True
                    break
            if not borned:
                print("No")
                exit()
        live = livenext
    if len(S) == 0:
        print("Yes")
    else:
        print("No")



if __name__ == "__main__":
    main()


# Optimized solution
from sys import exit, stderr, stdin
input = stdin.readline
from collections import defaultdict
from sys import stderr
def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return
def main():
    N = int(input())
    S = [int(a) for a in input().split()]
    S = sorted(S)
    exist = [S.pop()]
    debug(S)
    debug(exist)
    for i in range(N):
        next_exist = []
        next_s = []
        for p in exist[::-1]:
            while S:
                x = S.pop()
                if x < p:
                    next_exist.append(x)
                    break
                else:
                    next_s.append(x)
        exist += next_exist
        exist.sort()
        S += next_s[::-1]

    if len(exist) == 2**N:
        print("Yes")
        exit()
    else:
        print("No")
        exit()

if __name__ == "__main__":
    main()
# Original solution
import time

start = time.time()

n = int(eval(input()))
s = list(map(int, input().split()))

s.sort()
s.reverse()

flag = True
for i in range(n):
    a = s[0:2 ** i]
    b = s[2 ** i:2 ** (i + 1)]
    for j in range(len(a)):
        if b[j] >= a[j]:
            flag = False
            break
if flag:
    print('Yes')
    exit()

s.reverse()

a = []
a.append(s[-1])
s.pop(-1)
for i in range(n):
    for j in range(len(a)):
        for k in range(len(s)):
            if a[j]>s[-1-k]:
                a.append(s[-1-k])
                s.pop(-1-k)
                break
        if time.time() - start > 1.8:
            print('Yes')
            exit()
    if len(a)!=2**(i+1):
        print('No')
        exit()

print('Yes')


# Optimized solution

n = int(eval(input()))
s = list(map(int, input().split()))

s.sort()

a = [s[-1]]
b = s[:-1]

for i in range(n):
    a2 = []
    b2 = []
    for x in a:
        while b:
            y = b.pop()
            if x>y:
                a2.append(y)
                break
            else:
                b2.append(y)
    a = a + a2
    b = b + b2
    a.sort()
    a.reverse()
    b.sort()
    if len(a)!=2**(i+1):
        print('No')
        exit()
print('Yes')