#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
int32_t main() {
  fast();
  long long n;
  cin >> n;
  vector<long long> v1;
  vector<long long> v2;
  long long a;
  long long k1 = 0, k2 = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a;
    if (a == 1) {
      cin >> a;
      v1.push_back(a);
      k1++;
    } else {
      cin >> a;
      v2.push_back(a);
      k2++;
    }
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  long long dp1[k1 + 1];
  long long dp2[k2 + 1];
  if (k1 == 0) {
    long long ans = 2 * k2;
    dp2[0] = 0;
    dp2[1] = v2[0];
    for (long long i = 2; i < k2 + 1; i++) dp2[i] = dp2[i - 1] + v2[i - 1];
    for (long long i = 0; i < k2; i++) {
      if (dp2[i + 1] <= 2 * (k2 - i - 1)) ans = 2 * (k2 - i - 1);
    }
    cout << ans;
    return 0;
  } else if (k2 == 0) {
    dp1[0] = 0;
    dp1[1] = v1[0];
    for (long long i = 2; i < k1 + 1; i++) dp1[i] = dp1[i - 1] + v1[i - 1];
    long long ans = k1;
    for (long long i = 0; i < k1; i++) {
      if (dp1[i + 1] <= k1 - i - 1) ans = k1 - i - 1;
    }
    cout << ans;
    return 0;
  }
  dp1[0] = 0;
  dp1[1] = v1[0];
  for (long long i = 2; i < k1 + 1; i++) dp1[i] = dp1[i - 1] + v1[i - 1];
  dp2[0] = 0;
  dp2[1] = v2[0];
  for (long long i = 2; i < k2 + 1; i++) dp2[i] = dp2[i - 1] + v2[i - 1];
  long long ans = k1 + 2 * k2;
  for (long long i = 0; i < k1 + 1; i++) {
    for (long long j = 0; j < k2 + 1; j++) {
      if (dp1[i] + dp2[j] <= k1 - i + 2 * (k2 - j) && (i != 0 || j != 0)) {
        ans = min(ans, k1 - i + 2 * (k2 - j));
      }
    }
  }
  cout << ans;
}
