#include <bits/stdc++.h>
using namespace std;
int aaa;
struct st {
  st() {}
  st(int a, int b) {
    ra = a;
    num = b;
  }
  int ra, num;
};
struct tr {
  int s, e, sum, num;
};
tr tree[10010 << 2];
bool operator<(const st &a, const st &b) { return a.ra > b.ra; }
multiset<st> ii;
vector<int> vv[100050];
void build(int k, int s, int e) {
  tree[k].s = s;
  tree[k].e = e;
  tree[k].num = tree[k].sum = 0;
  if (s == e) return;
  int mid = (s + e) >> 1;
  build(k << 1, s, mid);
  build(k << 1 | 1, mid + 1, e);
}
void fins(int k, int pos) {
  if (tree[k].s == pos && tree[k].e == pos) {
    tree[k].num++;
    tree[k].sum += pos;
    return;
  }
  int mid = (tree[k].s + tree[k].e) >> 1;
  if (pos <= mid)
    fins(k << 1, pos);
  else
    fins(k << 1 | 1, pos);
  tree[k].num = tree[k << 1].num + tree[k << 1 | 1].num;
  tree[k].sum = tree[k << 1].sum + tree[k << 1 | 1].sum;
}
void fdel(int k, int pos) {
  if (tree[k].s == pos && tree[k].e == pos) {
    tree[k].num--;
    tree[k].sum -= pos;
    return;
  }
  int mid = (tree[k].s + tree[k].e) >> 1;
  if (pos <= mid)
    fdel(k << 1, pos);
  else
    fdel(k << 1 | 1, pos);
  tree[k].num = tree[k << 1].num + tree[k << 1 | 1].num;
  tree[k].sum = tree[k << 1].sum + tree[k << 1 | 1].sum;
}
void fcha(int k, int num) {
  if (tree[k].num == num) {
    aaa += tree[k].sum;
    return;
  }
  if (tree[k].s == tree[k].e) {
    aaa += tree[k].s * num;
    return;
  }
  if (tree[k << 1].num >= num) {
    fcha(k << 1, num);
  } else {
    fcha(k << 1, tree[k << 1].num);
    fcha(k << 1 | 1, num - tree[k << 1].num);
  }
}
bool cmp(int a, int b) { return a > b; }
int main() {
  int n;
  int tmp_sum = 0;
  build(1, 0, 10000);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    vv[a].push_back(b);
    if (a != 0) fins(1, b);
    tmp_sum += b;
  }
  for (int i = 1; i <= 100000; i++) {
    if (vv[i].size()) {
      sort(vv[i].begin(), vv[i].end(), cmp);
    }
    for (int j = 0; j < vv[i].size(); j++) {
      ii.insert(st(j + 1, vv[i][j]));
    }
  }
  int must = 0;
  int must_num = 0;
  int ans = tmp_sum;
  multiset<st>::iterator it;
  for (int i = n - 1;; i--) {
    int t_num = i + 1;
    if (i == n - 1) {
      it = ii.find(st(i + 1, 0));
      while (it != ii.end()) {
        must_num++;
        must += it->num;
        fdel(1, it->num);
        ii.erase(it);
        it = ii.find(st(i + 1, 0));
      }
    }
    it = ii.find(st(i, 0));
    while (it != ii.end()) {
      must_num++;
      must += it->num;
      fdel(1, it->num);
      ii.erase(it);
      it = ii.find(st(i, 0));
    }
    if (must_num + vv[0].size() <= i &&
        must_num + vv[0].size() + tree[1].num >= i) {
      aaa = 0;
      fcha(1, i - vv[0].size() - must_num);
      aaa += must;
      ans = min(ans, aaa);
    } else
      break;
  }
  printf("%d\n", ans);
}
