Problem p02920 (python)

Fixed submission:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
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

#A
def A():
    n = I()
    print(n**3)
    return

#B
def B():
    n = I()
    a = LI()
    b = LI()
    c = LI()
    ans = sum(b)
    for i in range(n-1):
        x = a[i]
        y = a[i+1]
        if y == x+1:
            ans += c[x-1]
    print(ans)
    return

#C
def C():
    n = I()
    b = LI()
    b.append(float("inf"))
    a = [min(b[i],b[i-1]) for i in range(n)]
    print(sum(a))
    return

#D
def D():
    n,k = LI()
    a = S()
    ans = 0
    for i in range(n-1):
        if a[i] == a[i+1]:
            ans += 1
    print(min(n-1,ans+2*k))
    return

#E
def E():
    def add(i):
        while i <= n:
            bit[i] += 1
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    n = I()
    p = LI()
    r = [0]*n
    bit = [0]*(n+1)
    for i in range(n)[::-1]:
        r[i] = (n-i-1)-sum(p[i])
        add(p[i])
    print(r)
    l = [0]*n
    bit = [0]*(n+1)
    for i in range(n):
        l[i] = i-sum(p[i])
        add(p[i])
    print(l)

    return

#F
def F():
    n = I()
    s = LI()
    s.sort()
    s = s[::-1]
    m = 1<<n
    f = [-1]*m
    f[0] = 0
    for i in range(n):
        k = 0
        l = 0
        for j in range(1<<i):
            while f[k] != i:
                k += 1
            while f[l] >= i or s[k] <= s[l]:
                l += 1
                if l == m:
                    print("No")
                    return
            f[l] = i+1
            k += 1
        for j in range(m):
            if f[j] >= 0:
                f[j] = i+1
    print("Yes")
    return

#Solve
if __name__ == "__main__":
    F()

Buggy submission (verdict: Runtime Error):
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
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

#A
def A():
    n = I()
    print(n**3)
    return

#B
def B():
    n = I()
    a = LI()
    b = LI()
    c = LI()
    ans = sum(b)
    for i in range(n-1):
        x = a[i]
        y = a[i+1]
        if y == x+1:
            ans += c[x-1]
    print(ans)
    return

#C
def C():
    n = I()
    b = LI()
    b.append(float("inf"))
    a = [min(b[i],b[i-1]) for i in range(n)]
    print(sum(a))
    return

#D
def D():
    n,k = LI()
    a = S()
    if a.count("L") in [0,n]:
        print(n-1)
        return
    if n == 1:
        print(0)
        return
    if n == 2:
        print(1)
        return
    ans = 0
    f = [0]*n
    for i in range(n-1):
        if a[i] == a[i+1]:
            ans += 1
            if a[i] == "L":
                f[i+1] = 1
            else:
                f[i] = 1
    f.append(0)
    f.append(0)
    d = defaultdict(lambda : 0)
    l = 0
    s = a[0]
    for i in range(n):
        if a[i] != s:
            d[(l,i-1)] = 1
            s = a[i]
            l = i
    r = n-1
    l = 0
    m = 0
    for i in range(k):
        if not i%2:
            while a[r] != "R":
                r -= 1
            while a[l] != "R":
                l += 1
        else:
            while a[r] != "L":
                r -= 1
            while a[l] != "L":
                l += 1
        if d[(l,r)]:
            print(n-1)
            return
        else:
            m += 2-f[l-1]-f[r+1]
    r = n-1
    l = 0
    m_ = 0
    for i in range(k):
        if i%2:
            while a[r] != "R":
                r -= 1
            while a[l] != "R":
                l += 1
        else:
            while a[r] != "L":
                r -= 1
            while a[l] != "L":
                l += 1
        if d[(l,r)]:
            print(n-1)
            return
        else:
            m_ += 2-f[l-1]-f[r+1]
    print(ans+max(m,m_))
    return

#E
def E():
    def add(i):
        while i <= n:
            bit[i] += 1
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    n = I()
    p = LI()
    r = [0]*n
    bit = [0]*(n+1)
    for i in range(n)[::-1]:
        r[i] = (n-i-1)-sum(p[i])
        add(p[i])
    print(r)
    l = [0]*n
    bit = [0]*(n+1)
    for i in range(n):
        l[i] = i-sum(p[i])
        add(p[i])
    print(l)

    return

#F
def F():
    def add(i,x):
        while i <= m:
            bit[i] += x
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    n = I()
    s = LI()
    s.sort()
    f = []
    a = s[0]
    k = 1
    for i in s[1:]:
        if a != i:
            f.append(k)
            k = 0
            a = i
        k += 1
    f.append(k)
    f = f[::-1]
    f.insert(0,0)
    m = len(f)
    bit = [0]*(m+1)
    add(1,1)
    if f[1] > 1:
        print("No")
        return
    j = 2
    for i in range(n):
        su_ = sum(j-1)
        su = sum(j)
        aj = su-su_
        while su > 0:
            mi = min(f[j]-aj, su_)
            add(j,mi)
            aj += mi
            su -= mi
            if aj == f[j]:
                su_ = su
                j += 1
            else:
                break
    k = [sum(i+1)-sum(i) for i in range(m-1)]
    if k == f[1:]:
        print("Yes")
    else:
        print("No")
    return

#Solve
if __name__ == "__main__":
    F()