------
## 最终结论
```python
import sys
import heapq

input = sys.stdin.read
data = input().split()

n = int(data[0])
index = 1

fire_spells = []
lightning_spells = []

max_damage = 0
current_fire_sum = 0
current_lightning_sum = 0

for _ in range(n):
    tp = int(data[index])
    d = int(data[index + 1])
    index += 2
    
    if tp == 0:
        # Fire spell
        if d > 0:
            # Learn new fire spell
            current_fire_sum += d
            heapq.heappush(fire_spells, d)
        else:
            # Forget fire spell
            current_fire_sum += d
            fire_spells.remove(-d)
            heapq.heapify(fire_spells)
    else:
        # Lightning spell
        if d > 0:
            # Learn new lightning spell
            current_lightning_sum += d
            heapq.heappush(lightning_spells, d)
        else:
            # Forget lightning spell
            current_lightning_sum += d
            lightning_spells.remove(-d)
            heapq.heapify(lightning_spells)

    if len(lightning_spells) == 0:
        max_damage = current_fire_sum + current_lightning_sum
    else:
        num_doubles = len(lightning_spells) - 1
        double_damage = sum(lightning_spells) + num_doubles * lightning_spells[0]
        
        max_damage = max(current_fire_sum + double_damage, 
                         current_fire_sum + current_lightning_sum + (len(lightning_spells) * lightning_spells[0]))
    
    print(max_damage)
```