#include [[nodiscard]] static inline constexpr std::array prepare([[maybe_unused]] const uint_fast32_t N, const std::vector>& cards) noexcept { std::array count_of = { 0, 0, 0, 0 }; for (const auto& [a, b] : cards) { if ((a & 7) == 0) count_of[3] += b; else if ((a & 3) == 0) count_of[2] += b; else if ((a & 1) == 0) count_of[1] += b; else count_of[0] += b; } return count_of; } [[nodiscard]] static inline constexpr uint_fast64_t solve(const std::array& count_of) noexcept { if (count_of[1] <= count_of[2]) return count_of[1] + count_of[3] + (count_of[2] - count_of[1]) / 2; else return count_of[2] + count_of[3] + (count_of[1] - count_of[2]) / 3; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); uint_fast32_t N; std::cin >> N; std::vector> cards(N); for (auto& [a, b] : cards) std::cin >> a >> b; std::cout << solve(prepare(N, cards)) << '\n'; return 0; }