#include #if __has_include() #endif using namespace std; #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define RDVVL(T,n,m,...) vec>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n,m);lin(__VA_ARGS__) #define VVL(n,m,...) RDVVL(ll,n,m,__VA_ARGS__) #define fo(i,...) for(auto[i,i##stop,i##step]=for_range(0,__VA_ARGS__);ivoid pp(const auto&...a){[[maybe_unused]]const char*c="";((o<(a...);} #define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{ namespace my{ void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};} templateconstexpr auto for_range(T s,T a,T b,T c=1){return array{a-s,b,(1-s*2)*c};} void lin(auto&...a){(cin>>...>>a);} bool amax(auto&a,const auto&b){return astruct pair{ A a;B b; pair()=default; pair(A aa,B bb):a(aa),b(bb){} auto operator<=>(const pair&)const=default; }; templateusing pack_back_t=tuple_element_t>; } namespace my{ templateconcept vectorial=is_base_of_v::value_type>,remove_cvref_t>; templateistream&operator>>(istream&i,vector&v){fe(v,e)i>>e;return i;} templateconstexpr int depth=0; templateconstexpr int depth =depth+1; templatestruct core_t_helper{using type=T;}; templatestruct core_t_helper{using type=typename core_t_helper::type;}; templateusing core_t=core_t_helper::type; templatestruct vec; templatestruct hvec_helper{using type=vec::type>;}; templatestruct hvec_helper<0,T>{using type=T;}; templateusing hvec=hvec_helper::type; templatestruct vec:vector{ static constexpr int D=depth+1; using C=core_t; using vector::vector; vec(const auto&...a)requires(sizeof...(a)>=3){resizes(a...);} void resizes(const auto&...a){if constexpr(sizeof...(a)==D)*this=make(a...,C{});else*this=make(a...);} static auto make(ll n,const auto&...a){ if constexpr(sizeof...(a)==1)return vec(n,array{a...}[0]); else return vec(n,make(a...)); } auto fold(const auto&f)const{ pairr{}; fe(*this,e){ if constexpr(!vectorial){ if(r.b)f(r.a,e); else r={e,1}; }else { } } return r; } auto max()const{return fold([](auto&a,auto b){if(arequires(sizeof...(A)>=2)vec(const A&...a)->vec>>; } namespace my{entry void main(){ LL(N); VVL(N,2,x); vec dp(N+1,2,ll{}); // dp[i][j]:i回まで操作して,末尾がx[i][j]であるときのスコアの最大値 dp[1][0]=dp[1][1]=(x[0][0]==x[0][1])*x[0][1]; fo(i,1,N){ fo(j,2){ fo(k,2){ amax(dp[i+1][k], dp[i][j] +(x[i-1][j]==x[i][1-k])*x[i][1-k] +(x[i][0]==x[i][1])*x[i][1]); } } } pp(dp[N].max()); }}