N = int(input()) ans = 0 count = [0,0,0,0] for _ in range(N): a,b = list(map(int,input().split())) if(a%8 == 0): count[0] += b elif(a%4 == 0): count[1] += b elif(a%2 == 0): count[2] += b # 8 v = count[0] ans += v count[0] -= v # 42 v = min(count[1],count[2]) ans += v count[1] -= v count[2] -= v # 44 v = count[1]//2 ans += v count[1] -= v*2 # 222 v =count[2]//3 ans += v count[2] -= v*3 print(ans)