nu = gets.to_i
ar = Array.new(nu).map{Array.new(2, 0)}
ar2 = Array.new(nu).map{Array.new(3, 0)}
ar3 = Array.new(nu).map{Array.new(3, 0)}
for i in 0..nu - 1
  ar[i] = gets.chomp.split(" ").map(&:to_i)
end
for i in 0..nu - 1
  ar2[i][0] = ar[i][0]
  ar2[i][1] = i
  ar3[i][0] = ar[i][1]
  ar3[i][1] = i
end
array1 = ar2.sort
array2 = ar3.sort
count = 0
for i in 1..nu - 1
  if array1[0][0] <= array2[0][0]
    if i == 1
      for j in 0..nu - i
        if array1[0][1] == array2[j][1]
          array2[j][2] = 1
          break
        end
      end
    end
    for j in 1..nu - i + 1
      if array1[0][1] != array2[-j][1] && array2[-j][2] == 0
        count += array1[0][0]
        break
      end
    end
    array1.delete_at(0)
    array2.delete_at(-j)
  else
    if i == 1
      for j in 1..nu - i
        if array2[0][1] == array1[j][1]
          array1[j][2] = 1
          break
        end
      end
    end
    for j in 1..nu - i + 1
      if array2[0][1] != array1[-j][1] && array1[-j][2] == 0
        count += array2[0][0]
        break
      end
    end
    array2.delete_at(0)
    array1.delete_at(-j)
  end
end

if array1[0][0] >= array2[0][0]
  count += array2[0][0]
else
  count += array1[0][0]
end

puts count
