Problem p02920 (python)

Fixed submission:
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()

Buggy submission (verdict: Runtime Error):
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 = live.copy()
        for l in live:
            borned = False
            for j in range(len(S)-1, -1, -1):
                if l > S[j]:
                    # debug((l,S[j]),'parent, child')
                    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()

Buggy submission (verdict: Runtime Error):
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 = live.copy()
        for l in live:
            borned = False
            for j in range(len(S)-1, -1, -1):
                if l > S[j]:
                    # debug((l,S[j]),'parent, child')
                    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()

Buggy submission (verdict: Runtime Error):
from sys import exit, stderr, stdin
input = stdin.readline
# setrecursionlimit(10**7)
def gau(n):
    return (n-1)*(n-2)//2

def main():
    N = int(input())
    A = list(map(int,input().split()))
    from collections import defaultdict
    dic = defaultdict(int)
    for i in range(len(A)):
        dic[A[i]]+=1
    klis = sorted(dic.keys(),key=lambda x: -x)
    klis
    for i in len(klis):
        if i == 0 and dic[i] != 1:
            print("No")
            exit()
        elif dic[i] > N-(i-1):
            print("No")
            exit()
    print("Yes")
if __name__ == "__main__":
    main()