// #include // Temp fix for gcc13 global pragma // #pragma GCC target("avx2,bmi2,popcnt,lzcnt") // #pragma GCC optimize("O3,unroll-loops") #include // #include using namespace std; using namespace numbers; #ifdef LOCAL #include "Debug.h" #else #define debug_endl() 42 #define debug(...) 42 #define debug2(...) 42 #define debug_bin(...) 42 #endif // Returns the largest integer k with x >= k * y template U floor_div(T x, U y){ assert(y > 0); return x / y - (x % y < 0); } // Returns the smallest integer k with x <= k * y template U ceil_div(T x, U y){ assert(y > 0); return x / y + (x % y > 0); } template T &ctmin(T &x){ return x; } template T &ctmin(T &x, const Head &h, const Tail &... t){ return ctmin(x = min(x, h), t...); } template T &ctmax(T &x){ return x; } template T &ctmax(T &x, const Head &h, const Tail &... t){ return ctmax(x = max(x, h), t...); } // std::chunk_by cuz AtCoder is stuck on 3 years old gcc vector> chunk_by(auto data, auto eq){ vector> chunks; for(auto l = data.begin(); l != data.end(); ){ auto r = next(l); vector chunk{*l}; while(r != data.end() && eq(*prev(r), *r)){ chunk.push_back(*r); r = next(r); } chunks.push_back(chunk); l = r; } return chunks; } int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); int n; cin >> n; vector> a(n); for(auto &[x, y]: a){ cin >> x >> y; } array dp{}; for(auto i = 1; i < n; ++ i){ int extra = a[i][0] == a[i][1] ? a[i][0] : 0; dp = { max(dp[0] + a[i][1] * (a[i - 1][0] == a[i][1]), dp[1] + a[i][1] * (a[i - 1][1] == a[i][1])) + extra, max(dp[0] + a[i][0] * (a[i - 1][0] == a[i][0]), dp[1] + a[i][0] * (a[i - 1][1] == a[i][0])) + extra }; } cout << ranges::max(dp) << "\n"; return 0; } /* */