#include [[nodiscard]] static inline constexpr uint_fast64_t prepare([[maybe_unused]] const uint_fast32_t N, const std::vector>& operations) noexcept { uint_fast64_t sum = 0; for (const auto& [x, y] : operations) if (x == y) sum += x; return sum; } [[nodiscard]] static inline constexpr uint_fast64_t solve(const uint_fast32_t N, const std::vector>& operations) noexcept { std::array, 2> dp = { std::array{ 0, 0 }, std::array{ 0, 0 } }; for (uint_fast32_t i = 1; i < N; ++i) { const auto& [x, y] = operations[i]; const auto& [prev_x, prev_y] = operations[i - 1]; dp[(i & 1) ^ 1][0] = std::max(dp[i & 1][0] + (x == prev_y ? x : 0), dp[i & 1][1] + (x == prev_x ? x : 0)); dp[(i & 1) ^ 1][1] = std::max(dp[i & 1][0] + (y == prev_y ? y : 0), dp[i & 1][1] + (y == prev_x ? y : 0)); } return std::max(dp[N & 1][0], dp[N & 1][1]); } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); uint_fast32_t N; std::cin >> N; std::vector> operations(N); for (auto& [x, y] : operations) std::cin >> x >> y; std::cout << prepare(N, operations) + solve(N, operations) << '\n'; return 0; }