/******************************************************************************************************
** Copyright (C) 2011.06.12(8:000)-2013.07.01
** Author: famousDT <[email protected]>
** Edit date: 2011-06-12
******************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>//abs,atof(string to float),atoi,atol,atoll
#include <math.h>//atan,acos,asin,atan2(a,b)(a/b atan),ceil,floor,cos,exp(x)(e^x),fabs,log(for E),log10
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <iostream>
#include <string.h>//memcpy(to,from,count
#include <ctype.h>//character process:isalpha,isdigit,islower,tolower,isblank,iscntrl,isprll
#include <algorithm>
using namespace std;
//typedef long long ll;
#define PI acos(-1)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MALLOC(n, type) ((type *)malloc((n) * sizeof(type)))
#define FABS(a) ((a) >= 0 ? (a) : (-(a)))
struct my
{
int a;
int b;
}wo[50005];
int main()
{
int n;
int i;
int d[50005][2];
while (scanf("%d", &n) == 1) {
int t[50005] = {0};
t[0] = 0;
for (i = 0; i < n; ++i)
scanf("%d%d", &wo[i].a, &wo[i].b);
stack<my> s;
s.push(wo[0]);
for (i = 1; i < n; ++i) {
while (!s.empty() && wo[i].a > s.top().a) {
t[i] += s.top().b;
s.pop();
}
s.push(wo[i]);
}
while (!s.empty())
s.pop();
s.push(wo[n - 1]);
for (i = n - 2; i >= 0; --i) {
while (!s.empty() && wo[i].a > s.top().a) {
t[i] += s.top().b;
s.pop();
}
s.push(wo[i]);
}
int max = -1;
for (i = 0; i < n; ++i)
if (max < t[i])
max = t[i];
printf("%d
", max);
}
return 0;
}