結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
|
提出日時 | 2020-07-04 21:35:47 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 479 ms / 2,000 ms |
コード長 | 888 bytes |
コンパイル時間 | 1,906 ms |
コンパイル使用メモリ | 175,000 KB |
実行使用メモリ | 14,208 KB |
最終ジャッジ日時 | 2024-09-19 12:17:28 |
合計ジャッジ時間 | 7,831 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h>#define all(x) (x).begin(), (x).end()typedef long long ll;#define MOD 1000000007using namespace std;int main() {int n;cin >> n;vector<ll> a(n);for(int i = 0; i < n; i++) {cin >> a[i];}set<ll> stl, str;for(int i = 1; i < n; i++) {str.insert(a[i]);}ll ans = LLONG_MAX;for(int i = 1; i < n - 1; i++) {stl.insert(a[i - 1]);str.erase(a[i]);auto x = stl.lower_bound(a[i]);auto y = str.lower_bound(a[i]);if(x != stl.end() && y != str.end()) {ans = min(ans, (*x) + (*y) + a[i]);}auto v = *stl.begin();auto w = *str.begin();if(v < a[i] && w < a[i]) {ans = min(ans, v + w + a[i]);}}if(ans < MOD) {cout << ans << endl;} else {cout << -1 << endl;}}