public void solve() throws IOException {
    int n = nextInt();

    ArrayList<Integer> one = new ArrayList<Integer>();
    ArrayList<Integer> two = new ArrayList<Integer>();

    for (int x = 0; x < n; x++) {
      int thick = nextInt();
      int width = nextInt();
      if (thick == 1) {
        one.add(width);
      } else {
        two.add(width);
      }
    }

    Collections.sort(one);
    Collections.sort(two);

    int minthick = Integer.MAX_VALUE;
    for (int x = one.size() - 1; x >= -1; x--) {
      for (int y = two.size() - 1; y >= -1; y--) {
        int ones = one.size() - 1 - x;
        int twos = two.size() - 1 - y;
        int thickness = ones + 2 * twos;

        int width = 0;
        for (int i = x; i >= 0; i--) {
          width += one.get(i);
        }
        for (int i = y; i >= 0; i--) {
          width += two.get(i);
        }

        if (width <= thickness) {
          minthick = Math.min(thickness, minthick);
        }
      }
    }

    System.out.println(minthick);

  }