#include <bits/stdc++.h> using namespace std; //TEMPLATE {{{ #include <unistd.h> typedef long long lint; typedef unsigned long long ulint; typedef long double ldouble; #define rep(i, n) for(int i = 0, i##s = int(n); i < i##s; i++) #define each(p, c) for(auto &p: c) #define all(c) begin(c), end(c) #define perm(c) sort(all(c)); for(bool c##p = true; c##p; c##p = next_permutation(all(c))) #define uniquenize(v) (v).erase(unique(all(v)), end(v)) #define sz(c) (int)((c).size()) void pexit(const string &s){exit(0*puts(s.c_str()));} void pexit(const char*s){exit(0*puts(s));} template<typename T> void pvec(const vector<T> &v){rep(i, v.size()) cout << v[i] << " \n"[i+1 == v.size()];} template<typename T> inline bool chmin(T &a, const T &b){return a > b ? a = b, true : false;} template<typename T> inline bool chmax(T &a, const T &b){return a < b ? a = b, true : false;} #ifdef HIBIKICHAN #include <dumper.hpp> #define dump(x) dumper::dumper(cerr, __LINE__, (#x), (x), 50, 1, 1) #else #define dump(x) #endif #if defined(_WIN32) || defined(__CYGWIN__) inline int stoi(const string &s){return atoi(s.c_str());} inline long long stoll(const string &s){return atoll(s.c_str());} char __tsbuf[99]; string to_string(long long x){sprintf(__tsbuf, "%lld", x); return string(__tsbuf);} string to_string(double x){sprintf(__tsbuf, "%lf", x); return string(__tsbuf);} #endif //}}} int a[500010], b[500010]; bool isu[500010], isl[500010]; //{{{ struct Starry{ const int N; vector<pair<int, int>> a, b; Starry(int size):N(2<<__lg(size)),a(2*N, {0, 0}), b(2*N, {0, 0}){} void add(int x, int y, int fst, int snd){add(x, y, 0, N, 1, fst, snd);} pair<int, int> add(int x, int y, int l, int r, int k, int fst, int snd){ if(r <= x || y <= l) return {a[k].first + b[k].first, a[k].second + b[k].second}; if(x <= l && r <= y) return {(a[k].first += fst) + b[k].first, (a[k].second += snd) + b[k].second}; b[k] = max(add(x, y, l, (l+r)/2, 2*k, fst, snd), add(x, y, (l+r)/2, r, 2*k+1, fst, snd)); return {a[k].first + b[k].first, a[k].second + b[k].second}; } pair<int, int> query(int x, int y){return query(x, y, 0, N, 1);} pair<int, int> query(int x, int y, int l, int r, int k){ if(r <= x || y <= l) return {-114514, -1}; if(x <= l && r <= y) return {a[k].first + b[k].first, a[k].second + b[k].second}; pair<int, int> tmp = max(query(x, y, l, (l+r)/2, 2*k), query(x, y, (l+r)/2, r, 2*k+1)); return {a[k].first + tmp.first, a[k].second + tmp.second}; } }; //}}} int main(){ int n; scanf("%d", &n); rep(i, n){ scanf("%d", a+i); a[i]--; b[a[i]] = i; } bool iscool = true; rep(i, n-1) iscool &= a[i] < a[i+1]; if(iscool) pexit("Cool Array"); vector<int> upper = {0}; isu[0] = true; for(int i = 1; i < n; i++){ if(a[upper.back()] < a[i]){ upper.push_back(i); isu[i] = true; } } vector<int> lower = {n-1}; for(int i = n-2; i >= 0; i--) if(!isu[i] && a[lower.back()] > a[i]){ lower.push_back(i); isl[i] = true; } reverse(all(lower)); Starry S(sz(upper)); rep(i, sz(upper)) S.add(i, i+1, 0, -upper[i]); vector<int> lx = lower; vector<int> ly; for(const int &x: lx) ly.push_back(a[x]); vector<int> ux = upper; vector<int> uy; for(const int &x: ux) uy.push_back(a[x]); //dump(lx); //dump(ux); pair<int, pair<int, int>> ans = {-114514, {-1, -1}}; int h = 0, w = 0; rep(i, sz(lower)){ const int X = lx[i]; const int Y = ly[i]; while(w <= X){ if(!isu[w]) S.add(int(lower_bound(all(uy), a[w])-begin(uy)), int(lower_bound(all(ux), w) - begin(ux)), 1, 0); w++; } while(h < Y){ if(!isu[b[h]]) S.add(int(lower_bound(all(uy), h)-begin(uy)), int(lower_bound(all(ux), b[h]) - begin(ux)), -1, 0); h++; } auto tmp = S.query(0, sz(upper)); if(tmp.first > ans.first){ ans.first = tmp.first; int ans1 = -tmp.second, ans2 = X; if(ans1 > ans2) swap(ans1, ans2); ans.second = {ans1, ans2}; } else if(tmp.first == ans.first){ int ans1 = -tmp.second, ans2 = X; if(ans1 > ans2) swap(ans1, ans2); ans.second = min(ans.second, make_pair(ans1, ans2)); } } printf("%d %d\n", ans.second.first+1, ans.second.second+1); }