// Arup Guha
// 3/21/2017
// Solution to 2017 March USACO Silver Problem: Pair Up

import java.util.*;
import java.io.*;

public class pairup {

	public static void main(String[] args) throws Exception {

		BufferedReader stdin = new BufferedReader(new FileReader("pairup.in"));
		int n = Integer.parseInt(stdin.readLine().trim());
		cow[] list = new cow[n];

		// Read cows and sort.
		for (int i=0; i<n; i++) {
			StringTokenizer tok = new StringTokenizer(stdin.readLine());
			int numC = Integer.parseInt(tok.nextToken());
			int milk = Integer.parseInt(tok.nextToken());
			list[i] = new cow(numC, milk);
		}
		Arrays.sort(list);

		// Now create cumulative frequency array of cows.
		int[] cumfreq = new int[n];
		cumfreq[0] = 0;
		for (int i=1; i<n; i++)
			cumfreq[i] = cumfreq[i-1] + list[i-1].numC;

		// Just for readability.
		int totalCows = cumfreq[n-1] + list[n-1].numC;

		// Our initial value will be the first and last...we may have to increase this...
		int res = list[0].amtMilk + list[n-1].amtMilk;

		// Index for matching cow from right end.
		int curI = n-1;

		// Now match up each unique cow with it's "reverse" cow from the other end.
		for (int i=1; i<=curI; i++) {

			// Find the rank of the matching cow.
			int myrank = totalCows - 1 - cumfreq[i];

			// Now find where this cow is in our list.
			while (curI >= 0 && cumfreq[curI] > myrank) curI--;

			// Update our result if necessary.
			res = Math.max(res, list[i].amtMilk + list[curI].amtMilk);
		}

		// Ta da!
		PrintWriter out = new PrintWriter(new FileWriter("pairup.out"));
		out.println(res);
		out.close();
		stdin.close();
	}
}

// Just to store cows in order of milk production.
class cow implements Comparable<cow> {

	public int numC;
	public int amtMilk;

	public cow(int nC, int milk) {
		numC = nC;
		amtMilk = milk;
	}

	public int compareTo(cow other) {
		return this.amtMilk - other.amtMilk;
	}
}