#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1000010;
int n, a[N], w[N], stk[N], tt, p[N];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%d%d", &a[i], &w[i]);
for (int i = 1; i <= n; i ++ )
{
while (tt && a[stk[tt]] <= a[i]) tt -- ;
if (tt) p[stk[tt]] = w[i];
stk[ ++ tt] = i;
}
tt = 0;
memset(stk, 0 , sizeof stk);
for (int i = n; i >= 1; i -- )
{
while (tt && a[stk[tt]] <= a[i]) tt -- ;
if (tt) p[stk[tt]] += w[i];
stk[ ++ tt] = i;
}
int res = 0;
for (int i = 1; i <= n; i ++ ) res = max(res, p[i]);
printf("%d\n", res);
return 0;
}