#include using namespace std; typedef long long ll; typedef pair P; #define REP(i,n) for(ll i=0;i> N; vector t(2,0); ll ans=0; for(i=1;i<=N;i++){ ll A,B; cin >> A >> B; if(A%8==0) ans+=B; else{ if(A%4==0) t[1]+=B; else if(A%2==0) t[0]+=B; } } if(t[0]>=t[1]) ans+=t[1]+(t[0]-t[1])/3; else ans+=t[0]+(t[1]-t[0])/2; cout << ans << endl; return 0; }