Potions (Easy Version) (Codeforces 1526C1)
Tags: Greedy, data structures

This is the easy version of the problem. The only difference is that in this version $$$n \leq 2000$$$. You can make hacks only if both versions of the problem are solved.There are $$$n$$$ potions in a line, with potion $$$1$$$ on the far left and potion $$$n$$$ on the far right. Each potion will increase your health by $$$a_i$$$ when drunk. $$$a_i$$$ can be negative, meaning that potion will decrease will health.You start with $$$0$$$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.What is the largest number of potions you can drink?

Input: The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$) — the number of potions. The next line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ... ,$$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) which represent the change in health after drinking that potion.

Output: Output a single integer, the maximum number of potions you can drink without your health becoming negative.

Note: For the sample, you can drink $$$5$$$ potions by taking potions $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$ and $$$6$$$. It is not possible to drink all $$$6$$$ potions because your health will go negative at some point

Sample Inputs:
6
4 -4 1 -3 1 -3

Sample Outputs:
5

Solution (Python):
import heapq
n = int(int(input()))
health_changes = list(map(int, input().split()))
total_health = 0
min_heap = []
for health_change in health_changes:
    heapq.heappush(min_heap, health_change)
    total_health += health_change
    if total_health < 0:
        total_health -= heapq.heappop(min_heap)
potions_drank = len(min_heap)
print(potions_drank)

Solution (Python):
import heapq
n = int(input())
transactions = list(map(int, input().split()))

def max_trans(n, transactions):
    max_potions = 0
    curr_health = 0
    min_heap = []
    for i in range(n):
        heapq.heappush(min_heap, transactions[i])
        curr_health += transactions[i]
        if curr_health < 0:
            most_positive_effect = heapq.heappop(min_heap)
            curr_health -= most_positive_effect
        else:
            max_potions += 1
    return max_potions
print(max_trans(n, transactions))

Solution (Python):
import heapq
n = int(input())
potions = list(map(int, input().split()))
max_potions = 0
current_health = 0
heap = []
for i in range(n):
    heapq.heappush(heap, potions[i])
    current_health += potions[i]
    max_potions += 1
    while current_health < 0 and heap:
        most_effective = heapq.heappop(heap)
        current_health -= most_effective
        max_potions -= 1
print(max_potions)