import heapq
q = []
n = int(input())
ar = list(map(int, input().split()))
health= 0 
cnt = 0
for v in ar:
    if v >= 0:
        cnt += 1
        health += v
    else:
        if health + v >= 0:
            cnt += 1
            heapq.heappush(q, v)
            health += v
        elif q:
            t = heapq.heappop(q)
            if t < v:
                health -= t
                health += v
                heapq.heappush(q, v) 
            else:
                heapq.heappush(q, t)
print(cnt)

 

后悔药,参照大佬的代码写的。挺灵性的。

注意pop之前要判断优先队列里面是否有元素。

要是pop出来的元素没被替换,要放回优先队列里面去