// Arup Guha
// Solution to 2013 November Silver Problem: Pogo Cow
// 7/2/2015

import java.util.*;
import java.io.*;

public class pogocow {

	public static int n;
	public static cow[] cows;

	public static void main(String[] args) throws Exception {

		// Read and sort cows.
		Scanner stdin = new Scanner(new File("pogocow.in"));
		n = stdin.nextInt();
		cows = new cow[n];
		for (int i=0; i<n; i++) {
			int x = stdin.nextInt();
			int p = stdin.nextInt();
			cows[i] = new cow(x, p);
		}
		Arrays.sort(cows);

		// Get forward answer.
		int res = getBest();

		// Pretty hacky, but it lets me reuse my old code without changing it.
		int sub = cows[n-1].x;
		for (int i=0; i<n; i++)
			cows[i].x = sub - cows[i].x;
		Arrays.sort(cows);

		// Run other way and take best.
		res = Math.max(res, getBest());

		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("pogocow.out")));
		out.println(res);
		out.close();
	}

	public static int getBest() {

		//dp[i][j] stores max cost starting with i followed by j.
		int[][] dp = new int[n][n];

		// max[i][j] stores max(dp[i][j], dp[i][j+1], ...d[i][n-1])
		int[][] max = new int[n][n];

		// Initial
		for (int i=0; i<n-1; i++) {
			dp[i][n-1] = cows[i].value + cows[n-1].value;
			max[i][n-1] = dp[i][n-1];
		}

		int res = 0;

		// Not sure if this is needed.
		dp[n-1][n-1] = cows[n-1].value;
		max[n-1][n-1] = dp[n-1][n-1];

		// Run through indices in desired order so all lookups are valid.
		for (int i=n-2; i>=0; i--) {
			for (int j=n-2; j>=i; j--) {

				// Find earliest spot we can jump to.
				int k = binsearch(i, j);

				// We're at the end of the row, so just take this value.
				if (k == n) dp[i][j] = j > i ? cows[i].value + cows[j].value : cows[i].value;

				// Or build off best spot that starts at j...
				else 		dp[i][j] = cows[i].value + max[j][k];

				// Update max.
				max[i][j] = Math.max(max[i][j+1], dp[i][j]);
				res = Math.max(res, max[i][j]);
			}
		}

		return res;
	}

	public static int binsearch(int i, int j) {

		// Find smallest index in cows with an x value of min or greater.
		int dist = cows[j].x - cows[i].x;
		int min = cows[j].x + dist;

		int low = j, high = n;

		// Typical binary search.
		while (low < high-1) {

			int mid = (low+high)/2;
			if (mid == n) return n;

			// This guess is too small.
			if (cows[mid].x < min) 	low = mid+1;

			// Or it's good enough.
			else					high = mid;
		}

		// Be careful to avoid array out of bounds.
		if (low == n) return n;
		return cows[low].x < min ? low+1 : low;
	}
}

class cow implements Comparable<cow> {

	public int x;
	public int value;

	public cow(int myx, int pts) {
		x = myx;
		value = pts;
	}

	public int compareTo(cow other) {
		return this.x - other.x;
	}
}