#line 1 "main.cpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* template start */ #define rep(i, a, b) for (long long i = (a); (i) < (b); (i)++) #define all(i) i.begin(), i.end() #ifdef LOCAL #define debug(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) #endif void debug_out(){std::cerr< void debug_out(Head h,Tail... t){ std::cerr<<" "<0)std::cout<<" :"; debug_out(t...); } template std::ostream& operator<<(std::ostream& os, std::pair pa) { return os << pa.first << " " << pa.second; } template std::ostream& operator<<(std::ostream& os, std::vector vec) { for (std::size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template inline bool chmax(T1& a,T2 b){return a inline bool chmin(T1& a,T2 b){return a>b && (a=b,true);} template constexpr Num mypow(Num a, long long b) { if(b==0)return 1; if (a==0)return 0; Num x = 1; while (b > 0) { if(b & 1)x*=a; a*=a; b >>= 1; } return x; } /* template end */ using ll = long long; #line 2 "/mnt/c/Users/Imperi/Documents/cp-library/lib/SegmentTree/DynamicSegmentTree.hpp" #line 5 "/mnt/c/Users/Imperi/Documents/cp-library/lib/SegmentTree/DynamicSegmentTree.hpp" //Monoid // type value_t // static var id // static (value_t,value_t)->value_t op template class DynamicSegmentTree{ public: using value_t=typename Monoid::value_t; using size_t=std::size_t; private: struct Node{ value_t val; Node *left,*right,*par; Node(Node *par_=nullptr):val(Monoid::id),left(),right(),par(par_){} ~Node(){ if(left)delete left; if(right)delete right; left=nullptr;right=nullptr; par=nullptr; } }; using node_ptr=Node *; size_t n,n0; node_ptr root; value_t fold(size_t a,size_t b,const node_ptr& now,size_t l,size_t r){ if(a<=l&&r<=b)return now->val; if(b<=l||r<=a)return Monoid::id; value_t lval=(now->left)?fold(a,b,now->left,l,l+(r-l)/2):Monoid::id; value_t rval=(now->right)?fold(a,b,now->right,l+(r-l)/2,r):Monoid::id; return Monoid::op(lval,rval); } void copy_dfs(node_ptr& node,const node_ptr& from){ node->val=from->val; if(from->left){ node->left=new Node(node); copy_dfs(node->left,from->left); } if(from->right){ node->right=new Node(node); copy_dfs(node->right,from->right); } } public: DynamicSegmentTree(size_t n_=0):n(n_),root(nullptr){ n0=1; while(n01){ size_t mid=l+(r-l)/2; if(ileft)now->left=new Node(now); now=now->left; r=mid; }else{ if(!now->right)now->right=new Node(now); now=now->right; l=mid; } } if(change)now->val=val; else now->val=Monoid::op(now->val,val); while(now->par){ now=now->par; now->val=Monoid::op((now->left)?now->left->val:Monoid::id, (now->right)?now->right->val:Monoid::id); } } value_t fold(size_t a,size_t b){ assert(0<=a&&a<=b&&b<=n); if(!root)return Monoid::id; return fold(a,b,root,0,n0); } }; #line 85 "main.cpp" constexpr ll INF=1e9; struct Min{ using value_t=ll; static constexpr value_t id=INF; static constexpr value_t op(value_t a,value_t b){ return std::min(a,b); } }; int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); ll n; std::cin>>n; DynamicSegmentTree up(INF),down(INF); ll ans=INF; std::set set; rep(i,0,n){ ll a; std::cin>>a; if(up.fold(a+1,INF)!=INF)chmin(ans,up.fold(a+1,INF)+a); if(down.fold(0,a)!=INF)chmin(ans,down.fold(0,a)+a); if(auto itr=set.upper_bound(a);itr!=set.end())down.update(a,a+*itr,true); if((!set.empty())&& (*set.begin()) < a)up.update(a,*set.begin()+a,true); set.insert(a); } if(ans!=INF)std::cout<