import sys, time, random from collections import deque, Counter, defaultdict def debug(*x):print('debug:',*x, file=sys.stderr) input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 61 - 1 mod = 998244353 n = ii() one, two, three = 0, 0, 0 for i in range(n): a, b = mi() if a % 8 == 0: three += b elif a % 4 == 0: two += b elif a % 2 == 0: one += b ans = three cnt = min(one, two) ans += cnt one -= cnt two -= cnt ans += two // 2 + one // 3 print(ans)