Problem:
 Score : 600 points    Problem Statement We have one slime.  You can set the health of this slime to any integer value of your choice.  A slime reproduces every second by spawning another slime that has strictly less health. You can freely choose the health of each new slime. The first reproduction of our slime will happen in one second.  Determine if it is possible to set the healths of our first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals a multiset S .  Here S is a multiset containing 2^N (possibly duplicated) integers: S_1,~S_2,~...,~S_{2^N} .      Constraints  All values in input are integers.  1 \leq N \leq 18  1 \leq S_i \leq 10^9         Input Input is given from Standard Input in the following format:  N  S_1  S_2  ...  S_{2^N}       Output If it is possible to set the healths of the first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals S , print Yes ; otherwise, print No .        Sample Input 1 2
4 2 3 1      Sample Output 1 Yes  We will show one way to make the multiset of the healths of the slimes that will exist in 2 seconds equal to S .  First, set the health of the first slime to 4 .  By letting the first slime spawn a slime whose health is 3 , the healths of the slimes that exist in 1 second can be 4,~3 .  Then, by letting the first slime spawn a slime whose health is 2 , and letting the second slime spawn a slime whose health is 1 , the healths of the slimes that exist in 2 seconds can be 4,~3,~2,~1 , which is equal to S as multisets.       Sample Input 2 2
1 2 3 1      Sample Output 2 Yes  S may contain multiple instances of the same integer.       Sample Input 3 1
1 1      Sample Output 3 No       Sample Input 4 5
4 3 5 3 1 2 7 8 7 4 6 3 7 2 3 6 2 7 3 2 6 7 3 4 6 7 3 4 2 5 2 3      Sample Output 4 No

Solution (Python):
from random import random


class TreapNode:
    """Treapのノード定義
    メンバ変数: 値、優先度、親へのポインタ、左右の子へのポインタ"""
    def __init__(self, val):
        self.val = val
        self.priority = random()
        self.parent, self.right, self.left = None, None, None


class Treap:
    """平衡二分探索木: 値の検索、挿入、削除などクエリ処理をO(logN)で実行する"""
    def __init__(self, is_multiset=False):
        self.is_multiset = is_multiset
        self.root = None

    def search(self, val: int) -> bool:
        """値の検索: 集合に値valを持つノードが存在するか"""
        ptr = self.root
        while ptr is not None:
            if val < ptr.val:
                ptr = ptr.left
            elif val > ptr.val:
                ptr = ptr.right
            else:
                return True
        return False

    def insert(self, val: int) -> bool:
        """値の削除: 集合に値valを持つノードを挿入する"""
        if self.root is None:
            self.root = TreapNode(val)
            return True

        
        ptr = self.root 
        while True:
            if val < ptr.val:
                if ptr.left is None:
                    ptr.left = TreapNode(val)
                    ptr.left.parent = ptr
                    ptr = ptr.left
                    break
                ptr = ptr.left
            else:
                if val == ptr.val and not self.is_multiset:
                    return False
                elif ptr.right is None:
                    ptr.right = TreapNode(val)
                    ptr.right.parent = ptr
                    ptr = ptr.right
                    break
                ptr = ptr.right

        
        while (ptr.parent is not None) and (ptr.parent.priority > ptr.priority):
            if ptr.parent.right == ptr:
                self._rotate_left(ptr.parent)
            else:
                self._rotate_right(ptr.parent)
        if ptr.parent is None:
            self.root = ptr 
        return True

    def delete(self, val: int) -> True:
        """値の削除: 集合から値valを持つノードを削除する"""
        if self.root is None:
            return False

        ptr = self.root         
        while True: 
            if ptr is None:
                return False
            elif val < ptr.val:
                ptr = ptr.left
            elif val > ptr.val:
                ptr = ptr.right
            else:
                break

        
        while (ptr.left is not None) or (ptr.right is not None):
            if ptr.left is None:
                self._rotate_left(ptr)
            elif ptr.right is None:
                self._rotate_right(ptr)
            elif ptr.left.priority < ptr.right.priority:
                self._rotate_right(ptr)
            else:
                self._rotate_left(ptr)
            if self.root == ptr:
                self.root = ptr.parent

        
        if ptr.left is None and ptr.right is None:
            if ptr == self.root:
                self.root = None
            elif ptr.parent.left == ptr:
                ptr.parent.left = None
            else:
                ptr.parent.right = None      
        return True

    def _rotate_left(self, ptr):
        """木の左回転を行う"""
        w = ptr.right
        w.parent = ptr.parent
        if w.parent is not None:
            if w.parent.left == ptr:
                w.parent.left = w
            else:
                w.parent.right = w
        ptr.right = w.left
        if ptr.right is not None:
            ptr.right.parent = ptr
        ptr.parent = w
        w.left = ptr
        if ptr == self.root:
            self.root = w
            self.root.parent = None

    def _rotate_right(self, ptr):
        """木の右回転を行う"""
        w = ptr.left
        w.parent = ptr.parent
        if w.parent is not None:
            if w.parent.right == ptr:
                w.parent.right = w
            else:
                w.parent.left = w
        ptr.left = w.right
        if ptr.left is not None:
            ptr.left.parent = ptr
        ptr.parent = w
        w.right = ptr
        if ptr == self.root:
            self.root = w
            self.root.parent = None

    def search_min(self):
        ptr = self.root
        ret = None
        while ptr is not None:
            ret = ptr.val
            ptr = ptr.left
        return ret

    def search_max(self):
        ptr = self.root
        ret = None
        while ptr is not None:
            ret = ptr.val
            ptr = ptr.right
        return ret

    def search_le(self, val: int):
        """val以下の最大の値を探す"""
        ptr = self.root
        ret = None
        while ptr is not None:
            if ptr.val <= val:
                ret = ptr.val
                ptr = ptr.right
            else:
                ptr = ptr.left
        return ret

    def search_lt(self, val: int):
        """valよりも小さいの最大の値を探す"""
        ptr = self.root
        ret = None
        while ptr is not None:
            if ptr.val < val:
                ret = ptr.val
                ptr = ptr.right
            else:
                ptr = ptr.left
        return ret

    def search_ge(self, val: int):
        """val以上の最小の値を探す"""
        ptr = self.root
        ret = None
        while ptr is not None:
            if ptr.val >= val:
                ret = ptr.val
                ptr = ptr.left
            else:
                ptr = ptr.right
        return ret
      
    def search_gt(self, val: int):
        """valよりも大きい最小の値を探す"""
        ptr = self.root
        ret = None
        while ptr is not None:
            if ptr.val > val:
                ret = ptr.val
                ptr = ptr.left
            else:
                ptr = ptr.right
        return ret
      
      
n = int(input())
s = list(map(int, input().split()))
s = sorted(s, reverse=True)

tp = Treap(is_multiset=True)
used = [False] * (1 << n)
tp.insert(s[0])
used[0] = True

for step in range(n):
    cnt = 1 << step
    res = []
    for i in range(1 << n):
        if used[i]:
            continue
        val = tp.search_gt(s[i])
        if val is not None:
            cnt -= 1
            res.append(val)
            res.append(s[i])
            tp.delete(val)
            used[i] = True
        if cnt == 0:
            for val in res:
                tp.insert(val)
            break
    else:
        print("No")
        exit()
print("Yes")

Solution (Python):
N = int(input())
N2 = 2**N
S = list(map(int, input().split()))
S.sort(reverse=True)

slimes = [S[0]]
INF = S[0] + 1
S[0] = INF
num = 1
min_idx = 1
for i in range(N):
  slimes.sort()
  n = num
  idx = min_idx
  is_continuous = True
  while n and idx<=N2-1:
    if S[idx] < slimes[n-1]:
      slimes.append(S[idx])
      S[idx] = INF
      idx += 1
      n -= 1
      min_idx += is_continuous
    else:
      if S[idx] < INF:
        is_continuous = False
      idx += 1
      min_idx += is_continuous
  
  if n:
    print('No')
    quit()
  
  num *= 2

print('Yes')

Solution (Python):
N = int(input())
N2 = 2**N
S = list(map(int, input().split()))
S.sort(reverse=True)

slimes = [S[0]]
S[0] = float('inf')
num = 1
min_idx = 1
for i in range(N):
  slimes.sort()
  n = num
  idx = min_idx
  is_continuous = True
  while n and idx<=N2-1:
    if S[idx] < slimes[n-1]:
      slimes.append(S[idx])
      S[idx] = float('inf')
      idx += 1
      n -= 1
      min_idx += is_continuous
    else:
      if S[idx] < float('inf'):
        is_continuous = False
      idx += 1
      min_idx += is_continuous
  
  if n:
    print('No')
    quit()
  
  num *= 2

print('Yes')

Solution (Python):
N = int(input())
N2 = 2**N
S = list(map(int, input().split()))
S.sort(reverse=True)

slimes = [S[0]]
S[0] = float('inf')
num = 1
for i in range(N):
  slimes.sort()
  n = num
  idx = 0
  while n and idx<=N2-1:
    if S[idx] < slimes[n-1]:
      slimes.append(S[idx])
      S[idx] = float('inf')
      idx += 1
      n -= 1
    else:
      idx += 1
  
  if n:
    print('No')
    quit()
  
  num *= 2

print('Yes')

Solution (Python):
from heapq import heappush, heappop
from copy import deepcopy


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

    left = []
    for e in s:
        heappush(left, -e)
    mx = heappop(left)

    made = []
    heappush(made, mx)

    for _ in range(n):
        made_prev = deepcopy(made)
        tmp = []

        while made_prev:
            made_val = heappop(made_prev)
            while left and left[0] <= made_val:
                pp = heappop(left)
                tmp.append(pp)

            if not left:
                print("No")
                exit()

            left_val = heappop(left)
            heappush(made, left_val)

        for e in tmp:
            heappush(left, e)

    print("Yes")


if __name__ == "__main__":
    main()

Solution (Python):
from heapq import heappush, heappop
from copy import deepcopy

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

left = []
for e in s:
    heappush(left, -e)
mx = heappop(left)

made = []
heappush(made, mx)

for _ in range(n):
    made_nxt = deepcopy(made)
    tmp = []

    while made:
        made_val = heappop(made)
        while left and left[0] <= made_val:
            pp = heappop(left)
            tmp.append(pp)

        if not left:
            print("No")
            exit()

        left_val = heappop(left)
        heappush(made_nxt, left_val)

    made = made_nxt

    for e in tmp:
        heappush(left, e)

print("Yes")

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort(reverse=True)
    visited = [False]*(2**n)
    visited[0] = 0
    now = [a[0]]
    for i in range(n):
        new = []
        now2 = 0
        for j, i in enumerate(a):
            if visited[j]:
                continue
            if now[now2] > i:
                now2 += 1
                new += [i]
                visited[j] = True
                if now2 == len(now):
                    now += new
                    now.sort(reverse=True)
                    break
        else:
            print("No")
            return

    print("Yes")


main()

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



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

solve()

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
a.sort(reverse = True)

class SegTree:
  def segfunc(self, x, y):
    if x[0] >= y[0]: return x
    return y
  def __init__(self, n, ide_ele, init_val):
    
    self.ide_ele = ide_ele
    
    self.num = 2 ** (n - 1).bit_length()
    self.seg = [self.ide_ele] * 2 * self.num
    
    for i in range(n):
      self.seg[i + self.num - 1] = init_val[i]    
    
    for i in range(self.num - 2, -1, -1) :
      self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2]) 
  def update(self, k, x):
    k += self.num - 1
    self.seg[k] = x
    while k + 1:
      k = (k - 1) // 2
      self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2]) 
  def query(self, p, q):
    if q <= p:
      return self.ide_ele
    p += self.num - 1
    q += self.num - 2
    res = self.ide_ele
    while q - p > 1:
      if p & 1 == 0:
        res = self.segfunc(res, self.seg[p])
      if q & 1 == 1:
        res = self.segfunc(res, self.seg[q])
        q -= 1
      p = p // 2
      q = (q - 1) // 2
    if p == q:
      res = self.segfunc(res, self.seg[p])
    else:
      res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
    return res

x = pow(2, N)
table = [0] * pow(2, N)
for i in range(pow(2, N)):
  ng = 0
  ok = x
  while ok - ng > 1:
    m = (ok + ng) // 2
    if a[i] > a[m]: ok = m
    else: ng = m
  table[i] = ok


seg = SegTree(x, (0, N), [(a[i], i) for i in range(x)])
tt = [0]
for i in range(N):
  t = pow(2, i)
  tt.sort(reverse = True)
  ttt = []
  for j in tt:
    
    if table[j] >= x:
      print("No")
      exit(0)
    y, k = seg.query(table[j], x)
    if a[j] <= y or (y == 0):
      print("No")
      exit(0)
    seg.update(k, (0, N))
    ttt.append(k)
  tt += ttt
print("Yes")

Solution (Python):
import math
def cmb(n,r):
  return math.factorial(n)//(math.factorial(r)*math.factorial(n-r))
n=int(input())
a=list(map(int,input().split()))
a.sort()
ls=[0]
cnt=0
for i in range(1,2**n):
  if a[i]>a[i-1]:
    cnt+=1
  ls.append(cnt)
lmt = []
for i in range(n+1):
  lmt.extend([i]*cmb(n,i))
bias = ls[-1]-lmt[-1]
jdg = 0
ulmt = 0
for i in range(n):
  jdg += ls.count(i)
  ulmt += 2**(n-i-1)
  if jdg > ulmt:
    print("No")
    exit()
for x,l in zip(ls,lmt):
  if x-l > bias:
    print("No")
    break
else:
  print("Yes")

Solution (Python):
N = int(input())
M = 2**N
A = list(map(int,input().split()))
A.sort(reverse=True)

ar = [A[0]]
A[0] = -1
for _ in range(N):
    ar.sort(reverse=True)
    br = []
    i = 0
    while i < M and len(br) < len(ar):
        if A[i] >= 0:
            if ar[len(br)] > A[i]:
                br.append(A[i])
                A[i] = -1
        i += 1
    if len(br) < len(ar):
        print('No')
        exit()
    ar += br
print('Yes')

Solution (Python):
from heapq import heapify, heappush, heappop

def solve():
    N = int(input())
    Ss = list(map(int, input().split()))

    Ss.sort(reverse=True)
    pow2s = [2**i for i in range(N)]

    PQs = [[-Ss[0]] for _ in range(N)]
    nums = [0] * N
    tm0 = 0
    for S in Ss[1:]:
        for tm in range(tm0, N):
            if nums[tm] >= pow2s[tm]:
                tm0 = tm
            else:
                if -PQs[tm][0] > S:
                    heappop(PQs[tm])
                    nums[tm] += 1
                    for i in range(tm+1, N):
                        heappush(PQs[i], -S)
                    break
        else:
            return False

    return True

if solve():
    print('Yes')
else:
    print('No')

Solution (Python):
from collections import Counter
from heapq import heapify, heappush, heappop

def solve():
    N = int(input())
    Ss = list(map(int, input().split()))

    cnt = Counter(Ss)

    PQ = [-N]
    for key in sorted(cnt.keys(), reverse=True):
        num = cnt[key]
        if len(PQ) < num:
            return False
        depths = []
        for _ in range(num):
            depths.append(-heappop(PQ))
        for depth in depths:
            for d in range(depth):
                heappush(PQ, -d)

    return True

if solve():
    print('Yes')
else:
    print('No')

Solution (Python):
def main():
    import heapq

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

    timelist=[] 
    importlist=[0]
    before=A[0]
    flag=True

    for a in A[1:]:
        if before!=a: 
            for v in importlist:
                if v<n:
                    heapq.heappush(timelist,v)
            importlist=[]
        before=a
        if len(timelist)!=0:
            v=heapq.heappop(timelist)
            if v<n:
                heapq.heappush(timelist,v+1)
            else:
                flag=False
                break
            importlist.append(v+1)
        else:    
            flag=False
            break
    print("Yes" if flag else "No")
main()

Solution (Python):
import heapq

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

timelist=[] 
importlist=[0]
before=A[0]
flag=True

for a in A[1:]:
    if before!=a: 
        for v in importlist:
            if v<n:
                heapq.heappush(timelist,v)
        importlist=[]
    before=a
    if len(timelist)!=0:
        v=heapq.heappop(timelist)
        if v<n:
            heapq.heappush(timelist,v+1)
        else:
            flag=False
            break
        importlist.append(v+1)
    else:    
        flag=False
        break
print("Yes" if flag else "No")

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

A.sort(reverse=1)
cur = [A[0]]
nxt = cur[:]

A[0] = -1

for _ in range(N):
    j = 0
    for i, a in enumerate(A):
        if a == -1:
            continue
        if a < cur[j]:
            
            nxt.append(a)
            A[i] = -1
            j += 1
            
            if j == len(cur):
                break
    else:
        
        No()
        exit()
    nxt.sort(reverse=1)
    cur = nxt[:]
Yes()

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

A.sort(reverse=1)
cur = [A[0]]

A[0] = -1

for _ in range(N):
    cur.sort(reverse=1)
    nxt = cur[:]
    j = 0
    for i, a in enumerate(A):
        if a == -1:
            continue
        if a < cur[j]:
            
            nxt.append(a)
            A[i] = -1
            j += 1
            
            if j == len(cur):
                break
    else:
        
        No()
        exit()
    cur = nxt[:]
Yes()

Solution (Python):
import sys
input = sys.stdin.readline

import heapq as hp
from collections import Counter


def main():

    N = int(input())
    A = list(map(int, input().split()))
    graph = [[] for _ in range(1<<N)]

    for l in range(N):
        for n in range(1<<l):
            graph[n].append(n+(1<<l))

    C = Counter(A)
    Keys = sorted(C.keys(), reverse=True)
    
    q = [(-len(graph[0]), 0)]
    for key in Keys:
        num = C[key]
        if num > len(q):
            return False
        use = []
        for _ in range(num):
            _, p = hp.heappop(q)
            use.append(p)
        for p in use:
            for np in graph[p]:
                hp.heappush(q, (-len(graph[np]), np))

    return True

if __name__ == "__main__":
    print("Yes" if main() else "No")

Solution (Python):
import sys
input = sys.stdin.readline

import heapq as hp
from collections import Counter


def main():

    N = int(input())
    A = list(map(int, input().split()))
    graph = [[] for _ in range(1<<N)]

    for l in range(N):
        for n in range(1<<l):
            graph[n].append(n+(1<<l))

    C = Counter(A)
    Keys = sorted(C.keys(), reverse=True)
    
    q = [(-len(graph[0]), 0)]
    for key in Keys:
        num = C[key]
        if num > len(q):
            return False
        use = []
        for _ in range(num):
            _, p = hp.heappop(q)
            use.append(p)
        for p in use:
            for np in graph[p]:
                hp.heappush(q, (-len(graph[np]), np))

    return True

if __name__ == "__main__":
    print("Yes" if main() else "No")

Solution (Python):
def main():
    INF = float("inf")

    N, *S = map(int, open(0).read().split())
    M = len(S)

    S.sort(reverse=True)

    Q = [S[0]]
    S[0] = INF
    for _ in range(N):
        Q.sort()
        T = Q[:]

        cur = 1
        while Q:
            p = Q.pop()
            while S[cur] >= p:
                cur += 1
                if cur == M:
                    print("No")
                    exit()
            T.append(S[cur])
            S[cur] = INF

        Q = T

    print("Yes")

main()

Solution (Python):
INF = float("inf")

N, *S = map(int, open(0).read().split())
M = len(S)

S.sort(reverse=True)

Q = [S[0]]
S[0] = INF
for _ in range(N):
    Q.sort()
    T = Q[:]

    cur = 1
    while Q:
        p = Q.pop()
        while S[cur] >= p:
            cur += 1
            if cur == M:
                print("No")
                exit()
        T.append(S[cur])
        S[cur] = INF

    Q = T

print("Yes")

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    s = LI()
    b = list(set([i for i in s]))
    b.sort()
    f = {i:bisect.bisect_left(b,i) for i in s}
    s = [f[i] for i in s]
    m = max(s)
    d = [0]*(m+1)
    for i in s:
        d[i] += 1
    if d[m] > 1:
        print("No")
        return
    ans = defaultdict(lambda : 0)
    ans[m] = 1
    k = 2
    for i in range(n):
        na = defaultdict(lambda : 0)
        su = 0
        for j in range(k):
            l = m-j
            if l < 0:
                break
            na[l] = ans[l]
            p = d[l]-ans[l]
            if p > 0:
                if su < p:
                    na[l] += su
                    su = 0
                else:
                    na[l] = d[l]
                    su -= p
            su += ans[l]
            ans[l] = na[l]
        k <<= 1
    for i in s:
        if d[i] != ans[i]:
            print("No")
            return
    print("Yes")
    return


if __name__ == "__main__":
    solve()

Solution (Python):
import sys

N=int(input())
S=list(map(int,input().split()))
S.sort(reverse=True)
flag=[0]*2**N
flag[0]=1
for _ in range(N):
    h=flag[:]
    child=1
    for parent in range(2**N):
        if flag[parent]==0:
            continue
        while child<2**N:
            if flag[child]==0 and S[parent]>S[child]:
                h[child]=1
                child+=1
                break
            child+=1
    flag=h
for i in flag:
    if i==0:
        print("No")
        break
else:
    print("Yes")

Solution (Python):
from collections import defaultdict
import heapq
N = int(input())
S = list(map(int,input().split()))
d = defaultdict(lambda:0)
for s in S:
    d[s] += 1
S = []
for s in d:
    S.append([s,d[s]])
S.sort(reverse=True)
S = [s[1] for s in S]
c = [-N]
possible = True
for s in S:
    if len(c)<s:
        possible = False
        break
    l = []
    for k in range(s):
        i = -heapq.heappop(c)
        l.append(i)
    for i in l:
        for j in range(i):
            heapq.heappush(c,-j)
if possible:
    print('Yes')
else:
    print('No')

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

"""
[解答]
最初に生成⇒最大のスライム
2回目以降⇒できるだけ大きい値のスライムを先に生成したい
         ⇒生成できるスライムの中で最大のものを常に選択(貪欲)
         ⇒それをn回繰り返す
降順にソートして前から取ると速い(昇順,後ろからでも)
"""
n = I()
s = LI()
s.sort(reverse=True)
use = [s[0]]
rest = s[1:]
for _ in range(n):
    new_use = []
    que = deque(rest)
    for i in use:
        rest_tmp = []
        while que:
            x = que.popleft()
            if i > x:
                new_use.append(x)
                break
            else:
                rest_tmp.append(x)
        que += rest_tmp
    use += new_use
    use.sort(reverse=True)
    rest = list(que)
    rest.sort(reverse=True)
if len(use) == 2**n:
    print('Yes')
else:
    print('No')

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
s = LI()
s.sort(reverse=True)
use = [s[0]]
rest = s[1:]
for _ in range(n):
    new_use = []
    que = deque(rest)
    for i in use:
        rest_tmp = []
        while que:
            x = que.popleft()
            if i > x:
                new_use.append(x)
                break
            else:
                rest_tmp.append(x)
        que += rest_tmp
    use += new_use
    use.sort(reverse=True)
    rest = list(que)
    rest.sort(reverse=True)
if len(use) == 2**n:
    print('Yes')
else:
    print('No')

Solution (Python):
import sys
import heapq

N = int(input())

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

dic = {}
lis = []

rem = 1
pl = 0
loop = N
flag = True

for i in range(2 ** N):

    if S[i] not in dic:
        dic[S[i]] = 1
        lis.append(S[i])

    else:
        dic[S[i]] += 1

lis.sort()
lis.reverse()



q = []

for i in lis:

    if len(q) == 0:
        if dic[i] != 1:
            print ("No")
            sys.exit()
        q = [-N]
        continue

    plq = []
    
    
    for j in range(dic[i]):

        if len(q) == 0:
            print ("No")
            sys.exit()
            
        maxnum = -1 * heapq.heappop(q)
        plq.append(maxnum - 1)

        if maxnum > 1:
            heapq.heappush(q,-1 * (maxnum - 1))

        

    for j in plq:
        if j > 0:
            heapq.heappush(q,-1 * j)

    


print ("Yes")