結果

問題 No.1095 Smallest Kadomatsu Subsequence
コンテスト
ユーザー chocorusk
提出日時 2020-06-27 12:51:16
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,250 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,089 ms
コンパイル使用メモリ 152,312 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2026-06-11 01:42:24
合計ジャッジ時間 2,742 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int INF=1e9+7;
int solve(int n, vector<int> a){
    int mnl[200020], mnr[200020];
    mnl[0]=INF, mnr[n]=INF;
    for(int i=0; i<n; i++){
        mnl[i+1]=min(mnl[i], a[i]);
    }
    for(int i=n-1; i>=0; i--){
        mnr[i]=min(mnr[i+1], a[i]);
    }
    int ans=INF;
    for(int i=0; i<n; i++){
        if(mnl[i]<a[i] && mnr[i+1]<a[i]) ans=min(ans, a[i]+mnl[i]+mnr[i+1]);
    }
    int t=min_element(a.begin(), a.end())-a.begin();
    ans=min(ans, a[t]+mnl[t]+mnr[t+1]);
    return ans;
}
int main()
{
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0; i<n; i++) cin>>a[i];
    int ans=solve(n, a);
    if(ans>=INF) cout<<-1<<endl;
    else cout<<ans<<endl;
    return 0;
}
0