#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int n;
struct node {
  int t, w;
  node() {}
  node(int t, int w) : t(t), w(w) {}
} p[maxn];
bool cmp(const node &a, const node &b) {
  if (a.t == b.t) return a.w > b.w;
  return a.t < b.t;
}
int main() {
  while (~scanf("%d", &n)) {
    int sum = 0;
    memset(p, 0, sizeof(p));
    ;
    for (int i = 1; i < n + 1; ++i) {
      scanf("%d%d", &p[i].t, &p[i].w);
      sum += p[i].w;
    }
    sort(p + 1, p + 1 + n, cmp);
    int ans = 0, tmp = 0;
    int k = 0;
    for (int i = 1; i < n + 1; ++i) {
      if (p[i].t == 2) break;
      k = i;
    }
    int i = 1, j = k + 1;
    while (j <= n && (ans < sum - tmp)) {
      ans += p[j].t;
      tmp += p[j].w;
      j++;
    }
    while (i <= k) {
      if (j - 1 <= k) {
        if (ans >= sum - tmp) break;
        ans++;
        tmp += p[i].w;
        i++;
        continue;
      }
      if (ans - 2 >= sum - (tmp - p[j - 1].w)) {
        ans -= 2;
        tmp -= p[j - 1].w;
        j--;
      } else if (ans - 1 >= sum - (tmp - p[j - 1].w + p[i].w)) {
        ans--;
        tmp = tmp - p[j - 1].w + p[i].w;
        i++;
        j--;
      } else if (i < k &&
                 (ans >= sum - (tmp - p[j - 1].w + p[i].w + p[i + 1].w))) {
        tmp = tmp - p[j - 1].w + p[i].w + p[i + 1].w;
        i += 2;
        j--;
      } else if (ans < sum - tmp) {
        ans++;
        tmp += p[i].w;
        i++;
      } else
        break;
    }
    printf("%d\n", ans);
  }
  return 0;
}
