#include <bits/stdc++.h>
using namespace std;
const long long OO = 1e8;
const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
class UnionFind {
 private:
  vector<int> p, rank;

 public:
  UnionFind(int N) {
    rank.assign(N, 0);
    p.assign(N, 0);
    for (size_t i = 0; i < N; i++) p[i] = i;
  }
  int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
  void unionSet(int i, int j) {
    if (!isSameSet(i, j)) {
      int x = findSet(i), y = findSet(j);
      if (rank[x] > rank[y])
        p[y] = x;
      else {
        p[x] = y;
        if (rank[x] == rank[y]) rank[y]++;
      }
    }
  }
};
int numberOfDigits(int x) { return log10(x) + 1; }
template <class T1, class T2, class Pred = std::less<T2>>
struct sort_pair_second {
  bool operator()(const std::pair<T1, T2>& left,
                  const std::pair<T1, T2>& right) {
    Pred p;
    return p(left.second, right.second);
  }
};
int main() {
  int n;
  cin >> n;
  deque<pair<int, int>> right;
  deque<pair<int, int>> left;
  int sum(0);
  int sumRight(0), sumLeft(0);
  int x, a;
  for (size_t i = 0; i < n; i++) {
    cin >> x >> a;
    if (x == 0)
      sum += a;
    else {
      if (x > 0) {
        right.push_back(make_pair(a, x));
        sumRight += a;
      } else {
        left.push_back(make_pair(a, x));
        sumLeft += a;
      }
    }
  }
  int r(0), l(0);
  r = right.size();
  l = left.size();
  int diff = abs(r - l);
  sort(right.begin(), right.end(), sort_pair_second<int, int>());
  sort(left.begin(), left.end(),
       sort_pair_second<int, int, std::greater<int>>());
  if (diff <= 1 || left.empty() || right.empty()) {
    if (left.empty())
      cout << sum + right.front().first << endl;
    else if (right.empty())
      cout << sum + left.front().first << endl;
    else
      cout << sum + sumLeft + sumRight << endl;
  } else {
    if (right.size() > left.size()) {
      sum += sumLeft;
      for (size_t i = 0; i < left.size() + 1; i++) {
        sum += right.front().first;
        right.pop_front();
      }
    } else {
      sum += sumRight;
      for (size_t i = 0; i < right.size() + 1; i++) {
        sum += left.front().first;
        left.pop_front();
      }
    }
    cout << sum << endl;
  }
  return 0;
}
