#include using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; #define rep(i,n) for(ll i=0;i T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); } template T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); } template inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template ostream &operator<<(ostream &os, const vector &a){ if (a.empty()) return os; os << a.front(); for (auto e : a | views::drop(1)){ os << ' ' << e; } return os; } void dump(auto ...vs){ ((cout << vs << ' '), ...) << endl; } void solve() { ll N; cin>>N; ll bx,by; bx=-1; by=-1; vector dp(2,0); rep(i,N){ ll x,y; cin>>x>>y; vector ndp(2,0); chmax(ndp[0],dp[0]+((by==x)*x)); chmax(ndp[0],dp[1]+((bx==x)*x)); chmax(ndp[1],dp[0]+((by==y)*y)); chmax(ndp[1],dp[1]+((bx==y)*y)); if (x==y){ ndp[0]+=x; ndp[1]+=x; } swap(x,bx); swap(y,by); swap(dp,ndp); } cout<<*max_element(all(dp))<sync_with_stdio(0); ll T=1; while (T--){ solve(); } return 0; }