結果
| 問題 | 
                            No.1095 Smallest Kadomatsu Subsequence
                             | 
                    
| コンテスト | |
| ユーザー | 
                             soto800
                         | 
                    
| 提出日時 | 2020-06-27 00:29:47 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 144 ms / 2,000 ms | 
| コード長 | 3,220 bytes | 
| コンパイル時間 | 1,616 ms | 
| コンパイル使用メモリ | 175,268 KB | 
| 実行使用メモリ | 11,008 KB | 
| 最終ジャッジ日時 | 2024-07-05 02:44:45 | 
| 合計ジャッジ時間 | 4,357 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 30 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
 
#define lli long long int
#define REP(i,s,n) for(lli i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
 
template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//https://algo-logic.info/segment-tree
/* SegTree<X>(n,fx,ex): モノイド(集合X, 二項演算fx, 単位元ex)についてサイズnで構築
    set(int i, X x), build(): i番目の要素をxにセット。まとめてセグ木を構築する。O(n)
    update(i,x): i 番目の要素を x に更新。O(log(n))
    query(a,b): [a,b) 全てにfxを作用させた値を取得。O(log(n))
   auto maxf = [](lli x1,lli x2) -> lli{return max(x1,x2);};
    SegTree<lli> stmax(n+1,maxf,-INF);
*/
template <typename X>
struct SegTree {
    using FX = function<X(X, X)>; // X•X -> X となる関数の型
    int n;
    FX fx;
    const X ex;
    vector<X> dat;
    SegTree(int n_, FX fx_, X ex_) : n(), fx(fx_), ex(ex_), dat(n_ * 4, ex_) {
        int x = 1;
        while (n_ > x) {
            x *= 2;
        }
        n = x;
    }
 
    void set(int i, X x) { dat[i + n - 1] = x; }
    void build() {
        for (int k = n - 2; k >= 0; k--) dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]);
    }
 
    void update(int i, X x) {
        i += n - 1;
        dat[i] = x;
        while (i > 0) {
            i = (i - 1) / 2;  // parent
            dat[i] = fx(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }
 
    X query(int a, int b) { return query_sub(a, b, 0, 0, n); }
    X query_sub(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) {
            return ex;
        } else if (a <= l && r <= b) {
            return dat[k];
        } else {
            X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return fx(vl, vr);
        }
    }
};
void solve(){
    lli n;
    cin>>n;
    vector<lli> a(n);
    REP(i,0,n)cin>>a[i];
    
    auto maxf = [](lli x1,lli x2) -> lli{return min(x1,x2);};
    SegTree<lli> stmin(n+1,maxf,INF);
    REP(i,0,n){
        stmin.set(i,a[i]);
    }
    stmin.build();
    lli ans = INF;
    REP(i,1,n-1){
        lli lef = stmin.query(0,i);
        lli rig = stmin.query(i+1,n);
        LOG4(i,lef,a[i],rig);
        if(lef< a[i] && a[i] < rig)continue;
        if(lef> a[i] && a[i] > rig)continue;
        chmin(ans,lef+rig+a[i]);
    }
    if(ans==INF)cout<<-1<<endl;
    else cout<<ans<<endl;
}
int main(){
	lli t;
    t=1;
    while(t--)solve();
	return 0;
}
            
            
            
        
            
soto800