Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define sz(s) (int)(s).size()
- #define all(s) s.begin(),s.end()
- void Speed() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- }
- void solve() {
- int n; cin >> n;
- vector<int> a(n), id(n + 1);
- for(int i = 0; i < n; i++){
- cin >> a[i];
- id[a[i]] = i;
- }
- vector<int> pre(n), suf(n);
- for(int i = 0; i < n; i++){
- pre[i] = suf[i] = a[i];
- if(i > 0) pre[i] = max(pre[i], pre[i - 1]);
- }
- for(int i = n - 2; i >= 0; i--){
- suf[i] = max(suf[i], suf[i + 1]);
- }
- int ans = 1e9;
- for(int i = 1; i + 1 < n; i++){
- int cur = 1e9, idx = id[pre[i]];
- if(idx != 0) cur = min(cur, pre[i] + a[0]);
- if(idx != i) cur = min(cur, pre[i] + a[i]);
- ans = min(ans, cur + suf[i + 1]);
- }
- cout << ans << "\n";
- }
- int main() {
- Speed();
- int tc = 1;
- //cin >> tc;
- while (tc--) {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment