結果
| 問題 | 
                            No.1095 Smallest Kadomatsu Subsequence
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-06-27 20:46:32 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 415 ms / 2,000 ms | 
| コード長 | 938 bytes | 
| コンパイル時間 | 1,677 ms | 
| コンパイル使用メモリ | 174,632 KB | 
| 実行使用メモリ | 13,568 KB | 
| 最終ジャッジ日時 | 2024-07-05 21:18:32 | 
| 合計ジャッジ時間 | 6,464 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 30 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
constexpr char ln = '\n';
const int mx=200010;
const ll mod=1e9+7;
int main(){
  int n;
  cin >> n;
  vector<int> c(n);
  set<int> str,stl;
  rep(i,n){
    cin >> c[i];
    str.insert(c[i]);
  }
  int ans = inf;
  stl.insert(c[0]);
  str.erase(c[0]);
  REP(i,1,n-1){
    str.erase(c[i]);
    auto posl = stl.lower_bound(c[i]);
    auto posr = str.lower_bound(c[i]);
    if(posl!=stl.begin() && posr!=str.begin()){
      ans = min(ans, c[i]+*stl.begin()+*str.begin());
    }
    if(posl!=stl.end() && posr!=str.end()){
      ans = min(ans, c[i]+*posl+*posr);
    }
    stl.insert(c[i]);
  }
  if(ans==inf) ans = -1;
  cout << ans << ln;
  return 0;
}