#include <bits/stdc++.h>
using namespace std;
long long const inf = 1e18;
int const mxn = 2e5 + 10;
long long poww(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
vector<pair<int, int> > vec, vec2;
int n, l, r, mid;
long long sum, ans;
long long pa[1000], pa2[1000];
bool check(int x) {
  int a = vec.size(), b = vec2.size();
  for (int i = 0; i <= min(a, x); i++) {
    if (i + ((x - i) / 2) * 2 >= sum - pa[i] - pa2[(x - i) / 2]) {
      return 1;
    }
  }
  return 0;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    l += a;
    sum += b;
    if (a == 1) {
      vec.push_back({a - b, b});
    } else {
      vec2.push_back({a - b, b});
    }
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < vec.size(); i++) pa[i + 1] = pa[i] + vec[i].second;
  sort(vec2.begin(), vec2.end());
  for (int i = 0; i < vec2.size(); i++) pa2[i + 1] = pa2[i] + vec2[i].second;
  while (l - r > 1) {
    mid = (l + r) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  cout << l;
}
