n = gets.to_i

modifiers = {}
(0..n-1).each do |i|
    l, j = gets.split.map(&:to_i)
    next if j < 0
    (modifiers[j] ||= []) << l 
end

costs = modifiers.values.map do |a|
    a.sort!
    a.reverse!
    a.map.with_index {|x, i| x * i}.inject(:+)
end
p costs.inject(:+)