Problem p02920 (python)

Fixed submission:
#!/usr/bin/env python3

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

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

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

h = []
for i in range(2**n):
    heappush(h,-s[i])
x = heappop(h)
use = [-x]
for i in range(n):
    rest = []
    for j in range(2**i):
        x = heappop(h)
        rest.append(-x)
    for i in range(2**i):
        if use[i] > rest[i]:
            continue
        else:
            print('No')
            quit()
    for i in range(2**i):
        use.append(rest[i])
print('Yes')

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

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

if n == 1:
    if s[0] == s[1]:
        print('No')
    else:
        print('Yes')
    quit()
h = []
for i in range(2**n):
    heappush(h,-s[i])
x = heappop(h)
use = [-x]
for i in range(n-1):
    rest = []
    for j in range(2**i):
        x = heappop(h)
        rest.append(-x)
    for i in range(2**i):
        if use[i] > rest[i]:
            continue
        else:
            print('No')
            quit()
    for i in range(2**i):
        use.append(rest[i])
print('Yes')

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

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

if n == 1:
    if s[0] == s[1]:
        print('No')
    else:
        print('Yes')
    quit()
h = []
for i in range(2**n):
    heappush(h,(-s[i],1))
x,y = heappop(h)
y = 0
heappush(h,(x,y))
for i in range(n-1):
    rest = []
    use = []
    rest_cnt = 0
    use_cnt = 0
    tmp = []
    while rest_cnt != i+1 or use_cnt != i+1:
        x,flg = heappop(h)
        if flg and rest_cnt < i+1:
            rest.append(-x)
            rest_cnt += 1
        elif not flg and use_cnt < i+1:
            use.append(-x)
            use_cnt += 1
        else:
            tmp.append((x,flg))
    for j in range(i+1):
        if use[j] > rest[j]:
            heappush(h,(-rest[j],0))
            heappush(h,(-use[j],0))
        else:
            print('No')
            quit()
    for j,f in tmp:
        heappush((j,f))
print('Yes')