n = int(input())
x1 = []
x2 = []
S = []
X = []
t = 0
for i in range(n):
    x,s = map(int, input().split())
    if x > 0:
        x1.append(x)
    else:
        x2.append(x)
    S.append(s)
    X.append(x)  
min1 = min(S[X.index(min(x1))], S[X.index(max(x2))])
while min1 != 0:
    t = t + min(x1) + abs(min(x1)- max(x2))
    S[X.index(min(x1))] = S[X.index(min(x1))] - 1
    S[X.index(max(x2))] = S[X.index(max(x2))] - 1
if min1 == S[X.index(min(x1))]:
    x1.pop(x1.index(min1))
    X.pop(x1.index(min1))
else:
    x2.pop(x2.index(min1))
    X.pop(x1.index(min1))
print(t)