#include<bits/stdc++.h>
using namespace std;

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double
#define ll long long

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)

const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;

int n,a[maxn],b[maxn],p,g[2][maxn];

#define ls p<<1
#define rs p<<1|1
#define mid ((l+r)>>1)

struct segment_tree {
    int mx[maxn<<2],tag[maxn<<2];

    void clear() {
        memset(mx,0,sizeof mx);
        memset(tag,0,sizeof tag);
    }

    void push(int p,int x) {tag[p]+=x,mx[p]+=x;}

    void pushdown(int p) {
        if(tag[p]) push(ls,tag[p]),push(rs,tag[p]),tag[p]=0;
    }

    void update(int p) {mx[p]=max(mx[ls],mx[rs]);}

    void modify(int p,int l,int r,int x,int y,int v) {
        if(y<x) return ;
        if(x<=l&&r<=y) return push(p,v),void();
        pushdown(p);
        if(x<=mid) modify(ls,l,mid,x,y,v);
        if(y>mid) modify(rs,mid+1,r,x,y,v);
        update(p);
    }

    int query(int p,int l,int r,int x,int y) {
        // printf("query :: %d %d %d %d %d\n",p,l,r,x,y);cerr<<"";
        if(y<x) return 0;
        if(x<=l&&r<=y) return mx[p];
        pushdown(p);int ans=0;
        if(x<=mid) ans=max(ans,query(ls,l,mid,x,y));
        if(y>mid) ans=max(ans,query(rs,mid+1,r,x,y));
        return ans;
    }

    int get(int p,int l,int r,int x) {
        // printf("get :: %d %d %d %d\n",l,r,x,mx[p]);
        if(l==r) return mx[p];
        if(x<=mid) return get(ls,l,mid,x);
        if(mx[rs]>mx[ls]) return get(rs,mid+1,r,x);
        return get(ls,l,mid,x);
    }
}T,w;

void solve(int *r,int *f) {
    // for(int i=1;i<=n-1;i++) printf("%d ",r[i]);puts("");
    T.clear();w.clear();
    for(int i=1;i<=n-1;i++) {
        // cerr<<i<<endl;
        int x=w.get(1,1,n,r[i]);
        // printf("??? :: %d %d %d\n",i,r[i],x);
        if(!x) {
            w.modify(1,1,n,r[i],r[i],i);
            f[i]=T.query(1,1,n-1,1,i-1)+1;
            // cerr<<f[i]<<endl;
            T.modify(1,1,n-1,1,i,1);
            continue;
        }
        w.modify(1,1,n,r[i],r[i],i);
        T.modify(1,1,n-1,x+1,i-1,1);
        // printf("get :: %d %d\n",i,T.query(1,1,n-1,x,x)+1);
        T.modify(1,1,n-1,i,i,T.query(1,1,n-1,x,x)+1);
        f[i]=T.query(1,1,n-1,1,i);
        // printf("get :: %d %d\n",i,f[i]);
    }
}

int main() {
    read(n);for(int i=1;i<=n;i++) {read(b[i]);if(b[i]==1) p=i;}
    for(int i=p+1;i<=n;i++) a[i-p]=b[i];
    for(int i=1;i<p;i++) a[n-p+i]=b[i];int mn=1e9,t=0;
    solve(a,g[0]);reverse(a+1,a+n);solve(a,g[1]);reverse(g[1]+1,g[1]+n);
    for(int i=1;i<n-1;i++) {
        if(max(g[0][i],g[1][i+1])<mn) mn=max(g[0][i],g[1][i+1]),t=i;//printf("%d %d\n",p,t);
        // printf("%d %d %d\n",i,g[0][i],g[1][i]);
    }
    // printf("%d %d\n",p,t);
    printf("%d %d\n",mn+1,(p-1+t+1)%n);
    return 0;
}


#pragma optimize("unroll-loops,no-stack-protector")
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define ALL(a) a.begin(),a.end()
#define lowbit(x) ((x)&-(x))
#define dec minus_down
using namespace std;
typedef unsigned long long u64;
typedef unsigned int u32;
typedef pair<int,int>pii;
typedef vector<int>VI;
typedef long long i64;
typedef long long ll;
typedef double ld;
namespace io{
	struct eof{eof(){}};
	const int L=(1<<21)|5;
	char ibuf[L],*iS,*iT,obuf[L],*oS=obuf,*oT=obuf+L-1,c,qu[55];int f,qr;
	#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline void flush(){fwrite(obuf,1,oS-obuf,stdout),oS=obuf;}
	inline void putc(char x){*oS++=x;if(oS==oT)flush();}
	template<class I>inline void gi(I&x){
		for(f=1,c=gc();(c<'0'||c>'9')&&(c!=EOF);c=gc())if(c=='-')f=-1;
 		if(c==EOF)throw eof();
		for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
	}
	template<class I>inline void print(I x){
		if(!x)putc('0');if(x<0)putc('-'),x=-x;
		while(x)qu[++qr]=x%10+'0',x/=10;while(qr)putc(qu[qr--]);
	}
#if __cplusplus < 201103L
	// do nothing
#else
	struct StringReader{
		function<bool(char)>chk;
		StringReader(){chk=[&](char c){return c<='z'&&c>='a';};}
		StringReader(function<bool(char)>func){chk=func;}
		inline char read(){for(c=gc();!chk(c);c=gc());return c;}
		inline void gs(char*s){for(c=gc();!chk(c);c=gc());for(;chk(c);c=gc())*s++=c;*s=0;}
		string readString(){string s;for(c=gc();!chk(c);c=gc());for(;chk(c);c=gc())s+=c;return s;}
	};
#endif
	inline void ps(const char*s){int l=strlen(s),x;for(x=0;x<l;x++)putc(s[x]);}
	struct IOC{~IOC(){flush();}}ioc;
	inline double readld(){
		double x=0,y=1;
		for(c=gc();c<'0'||c>'9';c=gc())if(c=='-')y=-1;assert(c!='.');
		for(;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=y;assert(c!='e');
		if(c=='.')for(c=gc();c<='9'&&c>='0';c=gc())y*=0.1,x+=y*(c&15);
		return x;
	}
	inline void printld(double x,int y){
		if(x<0)putc('-'),x=-x;x+=.5*pow(.1,y);print((u64)x),putc('.');
		for(x-=(u64)x;y;--y)x*=10,putc((int)x+'0'),x-=(int)x;
	}
}using io::gi;using io::print;using io::putc;
template<class T>int ctz(T a){return sizeof(T)>2?__builtin_ctzll(a):__builtin_ctz(a);}
template<class T>inline T gcd(T a,T b){
	int shift=ctz(a|b);
	for(b>>=ctz(b),a>>=ctz(a);a;a-=b,a>>=ctz(a))if(a<b)swap(a,b);
	return b<<shift;
}
template<class T>void exgcd(T a,T b,T&x,T&y){
	if(!b)return (void)(x=1,y=0);
	exgcd(b,a%b,y,x),y-=a/b*x;
}
template<class T>T Inv(T a,T p){T x,y;exgcd(a,p,x,y);return (x%p+p)%p;}
inline int fpow(int a,int t,int p){static int r;for(r=1;t;a=(ll)a*a%p,t>>=1)if(t&1)r=(ll)r*a%p;return r;}
string to_string(string s){return '"'+s+'"';};
string to_string(const char*s){return to_string(string(s));}
string to_string(bool f){return f?"true":"false";}
template<class A,class B>string to_string(pair<A,B>p){return "("+to_string(p.fi)+", "+to_string(p.se)+")";}
template<class T>string to_string(T v){string s="{",e="";for(auto&x:v)s+=e+to_string(x),e=", ";s+="}";return s;}
void debug_out(){cerr<<endl;}template<class A,class...B>void debug_out(A x,B...y){cerr<<" "<<to_string(x),debug_out(y...);}
void scan(){}template<class A,class...B>void scan(A&a,B&...b){gi(a),scan(b...);}
template<class T>void read(T first,T last){for(;first!=last;++first)gi(*first);}
template<class T>void write(T first,T last){for(;first!=last;putc(++first==last?'\n':' '))print(*first);}
template<class T>void show(T x){print(x),putc('\n');}
template<class A,class...B>void show(A a,B...b){print(a),putc(' '),show(b...);}
template<class T>inline bool chkmin(T&a,T b){return b<a?a=b,true:false;}
template<class T>inline bool chkmax(T&a,T b){return a<b?a=b,true:false;}
template<class T>inline T abs(T x){return x<0?-x:x;}
const int P=998244353,mod=P;
inline void inc(int&x,int y){x+=y;x<P?:x-=P;}
inline void inc(int&x,u64 y){x=(x+y)%P;}
inline void dec(int&x,int y){x-=y;x>=0?:x+=P;}
inline void dec(int&x,u64 y){x-=y%P;x>=0?:x+=P;}
inline int add(int x,int y){return (x+=y)<mod?x:x-mod;}
inline int sub(int x,int y){return (x-=y)<0?x+mod:x;}
inline int mul(int x,int y){return i64(x)*y%mod;}
inline void addx(int&x,int y){(x+=y)<mod?:x-=mod;}
inline void subx(int&x,int y){(x-=y)>=0?:x+=mod;}
template<class T>inline int sgn(T x){return (x>0)-(x<0);}
inline int fpow(int a,i64 t){
	static const int fmod=mod-1;
	t%=fmod;if(t<0)t+=fmod;int r;
	for(r=1;t;t>>=1,a=mul(a,a))if(t&1)r=mul(r,a);
	return r;
}
const int N=4e5+5;
namespace lct {
	struct node{
		node *c[2],*fa;
		int sz,sw,val,rval;
		multiset<int> w;
		bool rev;
		node();
		void pushup();
		void pushdown();
	}Tnull,*null=&Tnull,a[N];
	node::node(){
		c[0]=c[1]=fa=null;
		sz=sw=rev=val=rval=0;
		w.insert(0);
	}
	void node::pushup(){
		sz=c[0]->sz+c[1]->sz+1;
		sw=max(c[0]->sw,c[0]->sz+*--w.end());
		if(c[1]!=null) sw=max(sw,c[0]->sz+1+c[1]->sw);
		rval=c[1]==null?val:c[1]->rval;
	}
	void node::pushdown(){
		if (rev) {
			if(c[0]!=null)c[0]->rev^=1;
			if(c[1]!=null)c[1]->rev^=1;
			swap(c[0],c[1]),rev^=1;
		}
	}
	#define isroot(x) ((x)->fa->c[0]!=(x)&&(x)->fa->c[1]!=(x))
	void rotate(node*x){
		node*y=x->fa,*z=y->fa;
		if(z->c[0]==y)z->c[0]=x;
		if(z->c[1]==y)z->c[1]=x;
		bool l=y->c[1]==x,r=l^1;
		x->c[r]->fa=y,y->fa=x,x->fa=z;
		y->c[l]=x->c[r],x->c[r]=y,y->pushup();
	}
	node*st[N];int tp;
	void splay(node*x){
		st[tp=1]=x;
		for(node*y=x;!isroot(y);y=y->fa)st[++tp]=y->fa;
		while(tp)st[tp--]->pushdown();
		while(!isroot(x)){
			node*y=x->fa,*z=y->fa;
			if(!isroot(y))
				if(z->c[0]==y^y->c[0]==x)rotate(x);
				else rotate(y);
			rotate(x);
		}
		x->pushup();
	}
	node *findroot(node*x){
		splay(x);
		for(x->pushdown();x->c[0]!=null;x=x->c[0],x->pushdown());
		return x;
	}
	node *findprev(node*x){
		splay(x);
		for((x=x->c[0])->pushdown();x->c[1]!=null;x=x->c[1],x->pushdown());
		return x;
	}
	void access(node*x){
		node*y=null;
		for(;x!=null;y=x,x=x->fa){
			splay(x);
			if(x->c[1]!=null) x->w.insert(x->c[1]->sw+1);
			if(y!=null) x->w.erase(x->w.find(y->sw+1));
			x->c[1]=y;
			x->pushup();
		}
	}
	void link(node*x,node*y){
		//cerr<<"link "<<x-a<<' '<<y-a<<endl;
		access(x);
		splay(x);
		splay(y);
		y->fa=x;
		x->w.insert(y->sw+1);
		x->pushup();
		//cerr<<"link end"<<endl;
	}
	void cut(node*x,node*y){
		//cerr<<"cut "<<x-a<<' '<<y-a<<endl;
		access(x);
		splay(x);
		splay(y);
		y->fa=null;
		x->w.erase(x->w.find(y->sw+1));
		x->pushup();
	}
};
using lct::node;
using lct::a;
const int M=20;
int n,c[N],L[N],R[N],qu[N],qr,f[M][N],lg[N];
int x,v;
int cmp(int a,int b){ return c[a]<c[b]?a:b; }
int ask(int l,int r){
	if(l>r) r+=n;
	int d=lg[r-l+1];
	int k=cmp(f[d][l],f[d][r-(1<<d)+1]);
	return k<=n?k:k-n;
}
int main(){
	scan(n);
	read(c+1,c+n+1);
	for(int i=1;i<=n;++i){
		while(qr>0&&c[qu[qr]]>c[i]) qu[qr--]=0;
		if(qr) L[i]=qu[qr];
		qu[++qr]=i;
	}
	qr=0;
	for(int i=n;i>=1;--i){
		while(qr>0&&c[qu[qr]]>c[i]) qu[qr--]=0;
		if(qr) R[i]=qu[qr];
		qu[++qr]=i;
	}
	for(int i=1;i<=n;++i) a[i].val=c[i];
	for(int i=1;i<=n;++i) c[i+n]=c[i];
	for(int i=1;i<=n<<1;++i) f[0][i]=i;
	for(int i=2;i<=n<<1;++i) lg[i]=lg[i-1]+!(i&i-1);
	for(int d=1;1<<d<=n<<1;++d) for(int i=1;i+(1<<d)-1<=n<<1;++i) {
		f[d][i]=cmp(f[d-1][i],f[d-1][i+(1<<(d-1))]);
	}
	for(int i=1;i<=n;++i){
		if(L[i]||R[i]){
			if(c[L[i]]>c[R[i]]) lct::link(a+L[i],a+i);
			else lct::link(a+R[i],a+i);
		}else lct::link(a,a+i);
	}
	x=0;
	lct::splay(a);
	v=a->sw;
	//cerr<<v<<' '<<x<<endl;
	for(int l=1;l<=n;++l){
		node *p=a+l;
		lct::access(p);
		lct::splay(p);
		node *u=lct::findprev(p);
		lct::cut(u,p);
		int s=p-a,t=u-a;
		if(s<l) s+=n; if(t<l) t+=n;
		if(s>t) swap(s,t);
		++s,--t;
		if(s<=t){
			node *v=a+ask(s,t);
			lct::cut(p,v);
			lct::link(u,v);
		}
		node *q=a+(l==1?n:l-1);
		lct::access(q);
		lct::splay(q);
		if(q->rval>p->val){
			while(true){
				if(q->c[0]->rval>p->val) q=q->c[0];
				else if(q->val>p->val) break;
				else q=q->c[1];
			}
			lct::access(q);
			lct::splay(q);
			node *k=lct::findprev(q);
			lct::cut(k,q);
			lct::link(k,p);
			lct::link(p,q);
		}else{
			lct::access(q);
			lct::splay(q);
			lct::link(q,p);
		}
		lct::splay(a);
		if(a->sw<v) v=a->sw,x=l;
		//cerr<<v<<' '<<x<<endl;
	}
	print(v),putc(' '),print(x),putc('\n');
}


#pragma optimize("unroll-loops,no-stack-protector,-Ofast")
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define ALL(a) a.begin(),a.end()
#define lowbit(x) ((x)&-(x))
#define dec minus_down
using namespace std;
typedef unsigned long long u64;
typedef unsigned int u32;
typedef pair<int,int>pii;
typedef vector<int>VI;
typedef long long i64;
typedef long long ll;
typedef double ld;
namespace io{
	struct eof{eof(){}};
	const int L=(1<<21)|5;
	char ibuf[L],*iS,*iT,obuf[L],*oS=obuf,*oT=obuf+L-1,c,qu[55];int f,qr;
	#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline void flush(){fwrite(obuf,1,oS-obuf,stdout),oS=obuf;}
	inline void putc(char x){*oS++=x;if(oS==oT)flush();}
	template<class I>inline void gi(I&x){
		for(f=1,c=gc();(c<'0'||c>'9')&&(c!=EOF);c=gc())if(c=='-')f=-1;
 		if(c==EOF)throw eof();
		for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
	}
	template<class I>inline void print(I x){
		if(!x)putc('0');if(x<0)putc('-'),x=-x;
		while(x)qu[++qr]=x%10+'0',x/=10;while(qr)putc(qu[qr--]);
	}
#if __cplusplus < 201103L
	// do nothing
#else
	struct StringReader{
		function<bool(char)>chk;
		StringReader(){chk=[&](char c){return c<='z'&&c>='a';};}
		StringReader(function<bool(char)>func){chk=func;}
		inline char read(){for(c=gc();!chk(c);c=gc());return c;}
		inline void gs(char*s){for(c=gc();!chk(c);c=gc());for(;chk(c);c=gc())*s++=c;*s=0;}
		string readString(){string s;for(c=gc();!chk(c);c=gc());for(;chk(c);c=gc())s+=c;return s;}
	};
#endif
	inline void ps(const char*s){int l=strlen(s),x;for(x=0;x<l;x++)putc(s[x]);}
	struct IOC{~IOC(){flush();}}ioc;
	inline double readld(){
		double x=0,y=1;
		for(c=gc();c<'0'||c>'9';c=gc())if(c=='-')y=-1;assert(c!='.');
		for(;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=y;assert(c!='e');
		if(c=='.')for(c=gc();c<='9'&&c>='0';c=gc())y*=0.1,x+=y*(c&15);
		return x;
	}
	inline void printld(double x,int y){
		if(x<0)putc('-'),x=-x;x+=.5*pow(.1,y);print((u64)x),putc('.');
		for(x-=(u64)x;y;--y)x*=10,putc((int)x+'0'),x-=(int)x;
	}
}using io::gi;using io::print;using io::putc;
template<class T>int ctz(T a){return sizeof(T)>2?__builtin_ctzll(a):__builtin_ctz(a);}
template<class T>inline T gcd(T a,T b){
	int shift=ctz(a|b);
	for(b>>=ctz(b),a>>=ctz(a);a;a-=b,a>>=ctz(a))if(a<b)swap(a,b);
	return b<<shift;
}
template<class T>void exgcd(T a,T b,T&x,T&y){
	if(!b)return (void)(x=1,y=0);
	exgcd(b,a%b,y,x),y-=a/b*x;
}
template<class T>T Inv(T a,T p){T x,y;exgcd(a,p,x,y);return (x%p+p)%p;}
inline int fpow(int a,int t,int p){static int r;for(r=1;t;a=(ll)a*a%p,t>>=1)if(t&1)r=(ll)r*a%p;return r;}
string to_string(string s){return '"'+s+'"';};
string to_string(const char*s){return to_string(string(s));}
string to_string(bool f){return f?"true":"false";}
template<class A,class B>string to_string(pair<A,B>p){return "("+to_string(p.fi)+", "+to_string(p.se)+")";}
template<class T>string to_string(T v){string s="{",e="";for(auto&x:v)s+=e+to_string(x),e=", ";s+="}";return s;}
void debug_out(){cerr<<endl;}template<class A,class...B>void debug_out(A x,B...y){cerr<<" "<<to_string(x),debug_out(y...);}
void scan(){}template<class A,class...B>void scan(A&a,B&...b){gi(a),scan(b...);}
template<class T>void read(T first,T last){for(;first!=last;++first)gi(*first);}
template<class T>void write(T first,T last){for(;first!=last;putc(++first==last?'\n':' '))print(*first);}
template<class T>void show(T x){print(x),putc('\n');}
template<class A,class...B>void show(A a,B...b){print(a),putc(' '),show(b...);}
template<class T>inline bool chkmin(T&a,T b){return b<a?a=b,true:false;}
template<class T>inline bool chkmax(T&a,T b){return a<b?a=b,true:false;}
template<class T>inline T abs(T x){return x<0?-x:x;}
const int P=998244353,mod=P;
inline void inc(int&x,int y){x+=y;x<P?:x-=P;}
inline void inc(int&x,u64 y){x=(x+y)%P;}
inline void dec(int&x,int y){x-=y;x>=0?:x+=P;}
inline void dec(int&x,u64 y){x-=y%P;x>=0?:x+=P;}
inline int add(int x,int y){return (x+=y)<mod?x:x-mod;}
inline int sub(int x,int y){return (x-=y)<0?x+mod:x;}
inline int mul(int x,int y){return i64(x)*y%mod;}
inline void addx(int&x,int y){(x+=y)<mod?:x-=mod;}
inline void subx(int&x,int y){(x-=y)>=0?:x+=mod;}
template<class T>inline int sgn(T x){return (x>0)-(x<0);}
inline int fpow(int a,i64 t){
	static const int fmod=mod-1;
	t%=fmod;if(t<0)t+=fmod;int r;
	for(r=1;t;t>>=1,a=mul(a,a))if(t&1)r=mul(r,a);
	return r;
}
const int N=5e5+5;
namespace lct {
	struct node{
		node *c[2],*fa;
		int sz,sw,val,rval;
		multiset<int> w;
		bool rev;
		node();
		void pushup();
		void pushdown();
	}Tnull,*null=&Tnull,a[N];
	node::node(){
		c[0]=c[1]=fa=null;
		sz=sw=rev=val=rval=0;
		w.insert(0);
	}
	void node::pushup(){
		sz=c[0]->sz+c[1]->sz+1;
		sw=max(c[0]->sw,c[0]->sz+*--w.end());
		if(c[1]!=null) sw=max(sw,c[0]->sz+1+c[1]->sw);
		rval=c[1]==null?val:c[1]->rval;
	}
	void node::pushdown(){
		if (rev) {
			if(c[0]!=null)c[0]->rev^=1;
			if(c[1]!=null)c[1]->rev^=1;
			swap(c[0],c[1]),rev^=1;
		}
	}
	#define isroot(x) ((x)->fa->c[0]!=(x)&&(x)->fa->c[1]!=(x))
	void rotate(node*x){
		node*y=x->fa,*z=y->fa;
		if(z->c[0]==y)z->c[0]=x;
		if(z->c[1]==y)z->c[1]=x;
		bool l=y->c[1]==x,r=l^1;
		x->c[r]->fa=y,y->fa=x,x->fa=z;
		y->c[l]=x->c[r],x->c[r]=y,y->pushup();
	}
	node*st[N];int tp;
	void splay(node*x){
		st[tp=1]=x;
		for(node*y=x;!isroot(y);y=y->fa)st[++tp]=y->fa;
		while(tp)st[tp--]->pushdown();
		while(!isroot(x)){
			node*y=x->fa,*z=y->fa;
			if(!isroot(y))
				if(z->c[0]==y^y->c[0]==x)rotate(x);
				else rotate(y);
			rotate(x);
		}
		x->pushup();
	}
	node *findroot(node*x){
		splay(x);
		for(x->pushdown();x->c[0]!=null;x=x->c[0],x->pushdown());
		return x;
	}
	node *findprev(node*x){
		splay(x);
		for((x=x->c[0])->pushdown();x->c[1]!=null;x=x->c[1],x->pushdown());
		return x;
	}
	void access(node*x){
		node*y=null;
		for(;x!=null;y=x,x=x->fa){
			splay(x);
			if(x->c[1]!=null) x->w.insert(x->c[1]->sw+1);
			if(y!=null) x->w.erase(x->w.find(y->sw+1));
			x->c[1]=y;
			x->pushup();
		}
	}
	void link(node*x,node*y){
	//	cerr<<"link "<<x->val<<' '<<y->val<<endl;
		access(x);
		splay(x);
		splay(y);
		y->fa=x;
		x->w.insert(y->sw+1);
		x->pushup();
		//cerr<<"link end"<<endl;
	}
	void cut(node*x,node*y){
	//	cerr<<"cut "<<x->val<<' '<<y->val<<endl;
		access(x);
		splay(x);
		splay(y);
		y->fa=null;
		x->w.erase(x->w.find(y->sw+1));
		x->pushup();
	}
};
using lct::node;
using lct::a;
const int M=20;
int n,c[N],L[N],R[N],qu[N],qr,f[M][N],lg[N];
int x,v;
int cmp(int a,int b){ return c[a]<c[b]?a:b; }
int ask(int l,int r){
	if(l>r) r+=n;
	int d=lg[r-l+1];
	int k=cmp(f[d][l],f[d][r-(1<<d)+1]);
	return k<=n?k:k-n;
}
int main(){
	scan(n);
	read(c+1,c+n+1);
	for(int i=1;i<=n;++i){
		while(qr>0&&c[qu[qr]]>c[i]) qu[qr--]=0;
		if(qr) L[i]=qu[qr];
		qu[++qr]=i;
	}
	qr=0;
	for(int i=n;i>=1;--i){
		while(qr>0&&c[qu[qr]]>c[i]) qu[qr--]=0;
		if(qr) R[i]=qu[qr];
		qu[++qr]=i;
	}
	for(int i=1;i<=n;++i) a[i].val=c[i];
	for(int i=1;i<=n;++i) c[i+n]=c[i];
	for(int i=1;i<=n<<1;++i) f[0][i]=i;
	for(int i=2;i<=n<<1;++i) lg[i]=lg[i-1]+!(i&i-1);
	for(int d=1;1<<d<=n<<1;++d) for(int i=1;i+(1<<d)-1<=n<<1;++i) {
		f[d][i]=cmp(f[d-1][i],f[d-1][i+(1<<(d-1))]);
	}
	for(int i=1;i<=n;++i){
		if(L[i]||R[i]){
			if(c[L[i]]>c[R[i]]) lct::link(a+L[i],a+i);
			else lct::link(a+R[i],a+i);
		}else lct::link(a,a+i);
	}
	x=0;
	lct::splay(a);
	v=a->sw;
	//cerr<<v<<' '<<x<<endl;
	for(int l=1;l<n;++l){
		node *p=a+l;
		lct::access(p);
		lct::splay(p);
		node *u=lct::findprev(p);
		lct::cut(u,p);
		int s=p-a,t=u-a;
		if(s<l) s+=n; if(t<l) t+=n;
		if(s>t) swap(s,t);
		++s,--t;
		if(s<=t){
			node *v=a+ask(s,t);
			lct::cut(p,v);
			lct::link(u,v);
		}
		node *q=a+(l==1?n:l-1);
		lct::access(q);
		lct::splay(q);
		if(q->rval>p->val){
			while(true){
				if(q->c[0]->rval>p->val) q=q->c[0];
				else if(q->val>p->val) break;
				else q=q->c[1];
			}
			lct::access(q);
			lct::splay(q);
			node *k=lct::findprev(q);
			lct::cut(k,q);
			lct::link(k,p);
			lct::link(p,q);
		}else{
			lct::access(q);
			lct::splay(q);
			lct::link(q,p);
		}
		lct::splay(a);
		if(a->sw<v) v=a->sw,x=l;
	//	cerr<<v<<' '<<x<<endl;
	}
	print(v),putc(' '),print(x),putc('\n');
}


#pragma optimize("unroll-loops,no-stack-protector")
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define ALL(a) a.begin(),a.end()
#define lowbit(x) ((x)&-(x))
#define dec minus_down
using namespace std;
typedef unsigned long long u64;
typedef unsigned int u32;
typedef pair<int,int>pii;
typedef vector<int>VI;
typedef long long i64;
typedef long long ll;
typedef double ld;
namespace io{
	struct eof{eof(){}};
	const int L=(1<<21)|5;
	char ibuf[L],*iS,*iT,obuf[L],*oS=obuf,*oT=obuf+L-1,c,qu[55];int f,qr;
	#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline void flush(){fwrite(obuf,1,oS-obuf,stdout),oS=obuf;}
	inline void putc(char x){*oS++=x;if(oS==oT)flush();}
	template<class I>inline void gi(I&x){
		for(f=1,c=gc();(c<'0'||c>'9')&&(c!=EOF);c=gc())if(c=='-')f=-1;
 		if(c==EOF)throw eof();
		for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
	}
	template<class I>inline void print(I x){
		if(!x)putc('0');if(x<0)putc('-'),x=-x;
		while(x)qu[++qr]=x%10+'0',x/=10;while(qr)putc(qu[qr--]);
	}
#if __cplusplus < 201103L
	// do nothing
#else
	struct StringReader{
		function<bool(char)>chk;
		StringReader(){chk=[&](char c){return c<='z'&&c>='a';};}
		StringReader(function<bool(char)>func){chk=func;}
		inline char read(){for(c=gc();!chk(c);c=gc());return c;}
		inline void gs(char*s){for(c=gc();!chk(c);c=gc());for(;chk(c);c=gc())*s++=c;*s=0;}
		string readString(){string s;for(c=gc();!chk(c);c=gc());for(;chk(c);c=gc())s+=c;return s;}
	};
#endif
	inline void ps(const char*s){int l=strlen(s),x;for(x=0;x<l;x++)putc(s[x]);}
	struct IOC{~IOC(){flush();}}ioc;
	inline double readld(){
		double x=0,y=1;
		for(c=gc();c<'0'||c>'9';c=gc())if(c=='-')y=-1;assert(c!='.');
		for(;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=y;assert(c!='e');
		if(c=='.')for(c=gc();c<='9'&&c>='0';c=gc())y*=0.1,x+=y*(c&15);
		return x;
	}
	inline void printld(double x,int y){
		if(x<0)putc('-'),x=-x;x+=.5*pow(.1,y);print((u64)x),putc('.');
		for(x-=(u64)x;y;--y)x*=10,putc((int)x+'0'),x-=(int)x;
	}
}using io::gi;using io::print;using io::putc;
template<class T>int ctz(T a){return sizeof(T)>2?__builtin_ctzll(a):__builtin_ctz(a);}
template<class T>inline T gcd(T a,T b){
	int shift=ctz(a|b);
	for(b>>=ctz(b),a>>=ctz(a);a;a-=b,a>>=ctz(a))if(a<b)swap(a,b);
	return b<<shift;
}
template<class T>void exgcd(T a,T b,T&x,T&y){
	if(!b)return (void)(x=1,y=0);
	exgcd(b,a%b,y,x),y-=a/b*x;
}
template<class T>T Inv(T a,T p){T x,y;exgcd(a,p,x,y);return (x%p+p)%p;}
inline int fpow(int a,int t,int p){static int r;for(r=1;t;a=(ll)a*a%p,t>>=1)if(t&1)r=(ll)r*a%p;return r;}
string to_string(string s){return '"'+s+'"';};
string to_string(const char*s){return to_string(string(s));}
string to_string(bool f){return f?"true":"false";}
template<class A,class B>string to_string(pair<A,B>p){return "("+to_string(p.fi)+", "+to_string(p.se)+")";}
template<class T>string to_string(T v){string s="{",e="";for(auto&x:v)s+=e+to_string(x),e=", ";s+="}";return s;}
void debug_out(){cerr<<endl;}template<class A,class...B>void debug_out(A x,B...y){cerr<<" "<<to_string(x),debug_out(y...);}
void scan(){}template<class A,class...B>void scan(A&a,B&...b){gi(a),scan(b...);}
template<class T>void read(T first,T last){for(;first!=last;++first)gi(*first);}
template<class T>void write(T first,T last){for(;first!=last;putc(++first==last?'\n':' '))print(*first);}
template<class T>void show(T x){print(x),putc('\n');}
template<class A,class...B>void show(A a,B...b){print(a),putc(' '),show(b...);}
template<class T>inline bool chkmin(T&a,T b){return b<a?a=b,true:false;}
template<class T>inline bool chkmax(T&a,T b){return a<b?a=b,true:false;}
template<class T>inline T abs(T x){return x<0?-x:x;}
const int P=998244353,mod=P;
inline void inc(int&x,int y){x+=y;x<P?:x-=P;}
inline void inc(int&x,u64 y){x=(x+y)%P;}
inline void dec(int&x,int y){x-=y;x>=0?:x+=P;}
inline void dec(int&x,u64 y){x-=y%P;x>=0?:x+=P;}
inline int add(int x,int y){return (x+=y)<mod?x:x-mod;}
inline int sub(int x,int y){return (x-=y)<0?x+mod:x;}
inline int mul(int x,int y){return i64(x)*y%mod;}
inline void addx(int&x,int y){(x+=y)<mod?:x-=mod;}
inline void subx(int&x,int y){(x-=y)>=0?:x+=mod;}
template<class T>inline int sgn(T x){return (x>0)-(x<0);}
inline int fpow(int a,i64 t){
	static const int fmod=mod-1;
	t%=fmod;if(t<0)t+=fmod;int r;
	for(r=1;t;t>>=1,a=mul(a,a))if(t&1)r=mul(r,a);
	return r;
}
const int N=4e5+5;
namespace lct {
	struct node{
		node *c[2],*fa;
		int sz,sw,val,rval;
		multiset<int> w;
		bool rev;
		node();
		void pushup();
		void pushdown();
	}Tnull,*null=&Tnull,a[N];
	node::node(){
		c[0]=c[1]=fa=null;
		sz=sw=rev=val=rval=0;
		w.insert(0);
	}
	void node::pushup(){
		sz=c[0]->sz+c[1]->sz+1;
		sw=max(c[0]->sw,c[0]->sz+*--w.end());
		if(c[1]!=null) sw=max(sw,c[0]->sz+1+c[1]->sw);
		rval=c[1]==null?val:c[1]->rval;
	}
	void node::pushdown(){
		if (rev) {
			if(c[0]!=null)c[0]->rev^=1;
			if(c[1]!=null)c[1]->rev^=1;
			swap(c[0],c[1]),rev^=1;
		}
	}
	#define isroot(x) ((x)->fa->c[0]!=(x)&&(x)->fa->c[1]!=(x))
	void rotate(node*x){
		node*y=x->fa,*z=y->fa;
		if(z->c[0]==y)z->c[0]=x;
		if(z->c[1]==y)z->c[1]=x;
		bool l=y->c[1]==x,r=l^1;
		x->c[r]->fa=y,y->fa=x,x->fa=z;
		y->c[l]=x->c[r],x->c[r]=y,y->pushup();
	}
	node*st[N];int tp;
	void splay(node*x){
		st[tp=1]=x;
		for(node*y=x;!isroot(y);y=y->fa)st[++tp]=y->fa;
		while(tp)st[tp--]->pushdown();
		while(!isroot(x)){
			node*y=x->fa,*z=y->fa;
			if(!isroot(y))
				if(z->c[0]==y^y->c[0]==x)rotate(x);
				else rotate(y);
			rotate(x);
		}
		x->pushup();
	}
	node *findroot(node*x){
		splay(x); x->pushdown();
		for(x->pushdown();x->c[0]!=null;x=x->c[0],x->pushdown());
		return x;
	}
	node *findprev(node*x){
		splay(x); x->pushdown();
		for((x=x->c[0])->pushdown();x->c[1]!=null;x=x->c[1],x->pushdown());
		return x;
	}
	void access(node*x){
		node*y=null;
		for(;x!=null;y=x,x=x->fa){
			splay(x);
			if(x->c[1]!=null) x->w.insert(x->c[1]->sw+1);
			if(y!=null) x->w.erase(x->w.find(y->sw+1));
			x->c[1]=y;
			x->pushup();
		}
	}
	void link(node*x,node*y){
		//cerr<<"link "<<x->val<<' '<<y->val<<endl;
		access(x);
		splay(x);
		splay(y);
		y->fa=x;
		x->w.insert(y->sw+1);
		x->pushup();
	}
	void cut(node*x,node*y){
		//cerr<<"cut "<<x->val<<' '<<y->val<<endl;
		access(x);
		splay(x);
		splay(y);
		y->fa=null;
		x->w.erase(x->w.find(y->sw+1));
		x->pushup();
	}
};
using lct::node;
using lct::a;
const int M=20;
int n,c[N],L[N],R[N],qu[N],qr,f[M][N],lg[N];
int x,v;
int cmp(int a,int b){ return c[a]<c[b]?a:b; }
int ask(int l,int r){
	if(l>r) r+=n;
	int d=lg[r-l+1];
	int k=cmp(f[d][l],f[d][r-(1<<d)+1]);
	return k<=n?k:k-n;
}
int main(){
	scan(n);
	read(c+1,c+n+1);
	for(int i=1;i<=n;++i){
		while(qr>0&&c[qu[qr]]>c[i]) qu[qr--]=0;
		if(qr) L[i]=qu[qr];
		qu[++qr]=i;
	}
	qr=0;
	for(int i=n;i>=1;--i){
		while(qr>0&&c[qu[qr]]>c[i]) qu[qr--]=0;
		if(qr) R[i]=qu[qr];
		qu[++qr]=i;
	}
	for(int i=1;i<=n;++i) a[i].val=c[i];
	for(int i=1;i<=n;++i) c[i+n]=c[i];
	for(int i=1;i<=n<<1;++i) f[0][i]=i;
	for(int i=2;i<=n<<1;++i) lg[i]=lg[i-1]+!(i&i-1);
	for(int d=1;1<<d<=n<<1;++d) for(int i=1;i+(1<<d)-1<=n<<1;++i) {
		f[d][i]=cmp(f[d-1][i],f[d-1][i+(1<<(d-1))]);
	}
	for(int i=1;i<=n;++i){
		if(L[i]||R[i]){
			if(c[L[i]]>c[R[i]]) lct::link(a+L[i],a+i);
			else lct::link(a+R[i],a+i);
		}else lct::link(a,a+i);
	}
	x=0;
	lct::splay(a);
	v=a->sw;
	//cerr<<v<<' '<<x<<endl;
	for(int l=1;l<n;++l){
		node *p=a+l;
		lct::access(p);
		lct::splay(p);
		node *u=lct::findprev(p);
		lct::cut(u,p);
		int s=p-a,t=u-a;
		if(!t) s=l+1,t=l+n-1;
		else{
			if(s<l) s+=n; if(t<l) t+=n;
			if(s>t) swap(s,t);
			++s,--t;
		}
		if(s<=t){
			node *v=a+ask(s,t);
			lct::cut(p,v);
			lct::link(u,v);
		}
		node *q=a+(l==1?n:l-1);
		lct::access(q);
		lct::splay(q);
		if(q->rval>p->val){
			while(true){
				if(q->c[0]->rval>p->val) q=q->c[0];
				else if(q->val>p->val) break;
				else q=q->c[1];
			}
			lct::access(q);
			lct::splay(q);
			node *k=lct::findprev(q);
			lct::cut(k,q);
			lct::link(k,p);
			lct::link(p,q);
		}else{
			lct::access(q);
			lct::splay(q);
			lct::link(q,p);
		}
		lct::splay(a);
		if(a->sw<v) v=a->sw,x=l;
		//cerr<<v<<' '<<x<<endl;
	}
	print(v),putc(' '),print(x),putc('\n');
}


#pragma optimize("unroll-loops,no-stack-protector")
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define ALL(a) a.begin(),a.end()
#define lowbit(x) ((x)&-(x))
#define dec minus_down
using namespace std;
typedef unsigned long long u64;
typedef unsigned int u32;
typedef pair<int,int>pii;
typedef vector<int>VI;
typedef long long i64;
typedef long long ll;
typedef double ld;
namespace io{
	struct eof{eof(){}};
	const int L=(1<<21)|5;
	char ibuf[L],*iS,*iT,obuf[L],*oS=obuf,*oT=obuf+L-1,c,qu[55];int f,qr;
	#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline void flush(){fwrite(obuf,1,oS-obuf,stdout),oS=obuf;}
	inline void putc(char x){*oS++=x;if(oS==oT)flush();}
	template<class I>inline void gi(I&x){
		for(f=1,c=gc();(c<'0'||c>'9')&&(c!=EOF);c=gc())if(c=='-')f=-1;
 		if(c==EOF)throw eof();
		for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
	}
	template<class I>inline void print(I x){
		if(!x)putc('0');if(x<0)putc('-'),x=-x;
		while(x)qu[++qr]=x%10+'0',x/=10;while(qr)putc(qu[qr--]);
	}
#if __cplusplus < 201103L
	// do nothing
#else
	struct StringReader{
		function<bool(char)>chk;
		StringReader(){chk=[&](char c){return c<='z'&&c>='a';};}
		StringReader(function<bool(char)>func){chk=func;}
		inline char read(){for(c=gc();!chk(c);c=gc());return c;}
		inline void gs(char*s){for(c=gc();!chk(c);c=gc());for(;chk(c);c=gc())*s++=c;*s=0;}
		string readString(){string s;for(c=gc();!chk(c);c=gc());for(;chk(c);c=gc())s+=c;return s;}
	};
#endif
	inline void ps(const char*s){int l=strlen(s),x;for(x=0;x<l;x++)putc(s[x]);}
	struct IOC{~IOC(){flush();}}ioc;
	inline double readld(){
		double x=0,y=1;
		for(c=gc();c<'0'||c>'9';c=gc())if(c=='-')y=-1;assert(c!='.');
		for(;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=y;assert(c!='e');
		if(c=='.')for(c=gc();c<='9'&&c>='0';c=gc())y*=0.1,x+=y*(c&15);
		return x;
	}
	inline void printld(double x,int y){
		if(x<0)putc('-'),x=-x;x+=.5*pow(.1,y);print((u64)x),putc('.');
		for(x-=(u64)x;y;--y)x*=10,putc((int)x+'0'),x-=(int)x;
	}
}using io::gi;using io::print;using io::putc;
template<class T>int ctz(T a){return sizeof(T)>2?__builtin_ctzll(a):__builtin_ctz(a);}
template<class T>inline T gcd(T a,T b){
	int shift=ctz(a|b);
	for(b>>=ctz(b),a>>=ctz(a);a;a-=b,a>>=ctz(a))if(a<b)swap(a,b);
	return b<<shift;
}
template<class T>void exgcd(T a,T b,T&x,T&y){
	if(!b)return (void)(x=1,y=0);
	exgcd(b,a%b,y,x),y-=a/b*x;
}
template<class T>T Inv(T a,T p){T x,y;exgcd(a,p,x,y);return (x%p+p)%p;}
inline int fpow(int a,int t,int p){static int r;for(r=1;t;a=(ll)a*a%p,t>>=1)if(t&1)r=(ll)r*a%p;return r;}
string to_string(string s){return '"'+s+'"';};
string to_string(const char*s){return to_string(string(s));}
string to_string(bool f){return f?"true":"false";}
template<class A,class B>string to_string(pair<A,B>p){return "("+to_string(p.fi)+", "+to_string(p.se)+")";}
template<class T>string to_string(T v){string s="{",e="";for(auto&x:v)s+=e+to_string(x),e=", ";s+="}";return s;}
void debug_out(){cerr<<endl;}template<class A,class...B>void debug_out(A x,B...y){cerr<<" "<<to_string(x),debug_out(y...);}
void scan(){}template<class A,class...B>void scan(A&a,B&...b){gi(a),scan(b...);}
template<class T>void read(T first,T last){for(;first!=last;++first)gi(*first);}
template<class T>void write(T first,T last){for(;first!=last;putc(++first==last?'\n':' '))print(*first);}
template<class T>void show(T x){print(x),putc('\n');}
template<class A,class...B>void show(A a,B...b){print(a),putc(' '),show(b...);}
template<class T>inline bool chkmin(T&a,T b){return b<a?a=b,true:false;}
template<class T>inline bool chkmax(T&a,T b){return a<b?a=b,true:false;}
template<class T>inline T abs(T x){return x<0?-x:x;}
const int P=998244353,mod=P;
inline void inc(int&x,int y){x+=y;x<P?:x-=P;}
inline void inc(int&x,u64 y){x=(x+y)%P;}
inline void dec(int&x,int y){x-=y;x>=0?:x+=P;}
inline void dec(int&x,u64 y){x-=y%P;x>=0?:x+=P;}
inline int add(int x,int y){return (x+=y)<mod?x:x-mod;}
inline int sub(int x,int y){return (x-=y)<0?x+mod:x;}
inline int mul(int x,int y){return i64(x)*y%mod;}
inline void addx(int&x,int y){(x+=y)<mod?:x-=mod;}
inline void subx(int&x,int y){(x-=y)>=0?:x+=mod;}
template<class T>inline int sgn(T x){return (x>0)-(x<0);}
inline int fpow(int a,i64 t){
	static const int fmod=mod-1;
	t%=fmod;if(t<0)t+=fmod;int r;
	for(r=1;t;t>>=1,a=mul(a,a))if(t&1)r=mul(r,a);
	return r;
}
const int N=4e5+5;
namespace lct {
	struct node{
		node *c[2],*fa;
		int sz,sw,val,rval;
		multiset<int> w;
		bool rev;
		node();
		void pushup();
		void pushdown();
	}Tnull,*null=&Tnull,a[N];
	node::node(){
		c[0]=c[1]=fa=null;
		sz=sw=rev=val=rval=0;
		w.insert(0);
	}
	void node::pushup(){
		sz=c[0]->sz+c[1]->sz+1;
		sw=max(c[0]->sw,c[0]->sz+*--w.end());
		if(c[1]!=null) sw=max(sw,c[0]->sz+1+c[1]->sw);
		rval=c[1]==null?val:c[1]->rval;
	}
	void node::pushdown(){
		if (rev) {
			if(c[0]!=null)c[0]->rev^=1;
			if(c[1]!=null)c[1]->rev^=1;
			swap(c[0],c[1]),rev^=1;
		}
	}
	#define isroot(x) ((x)->fa->c[0]!=(x)&&(x)->fa->c[1]!=(x))
	void rotate(node*x){
		node*y=x->fa,*z=y->fa;
		if(z->c[0]==y)z->c[0]=x;
		if(z->c[1]==y)z->c[1]=x;
		bool l=y->c[1]==x,r=l^1;
		x->c[r]->fa=y,y->fa=x,x->fa=z;
		y->c[l]=x->c[r],x->c[r]=y,y->pushup();
	}
	node*st[N];int tp;
	void splay(node*x){
		st[tp=1]=x;
		for(node*y=x;!isroot(y);y=y->fa)st[++tp]=y->fa;
		while(tp)st[tp--]->pushdown();
		while(!isroot(x)){
			node*y=x->fa,*z=y->fa;
			if(!isroot(y))
				if(z->c[0]==y^y->c[0]==x)rotate(x);
				else rotate(y);
			rotate(x);
		}
		x->pushup();
	}
	node *findroot(node*x){
		splay(x);
		for(x->pushdown();x->c[0]!=null;x=x->c[0],x->pushdown());
		return x;
	}
	node *findprev(node*x){
		splay(x);
		for((x=x->c[0])->pushdown();x->c[1]!=null;x=x->c[1],x->pushdown());
		return x;
	}
	void access(node*x){
		node*y=null;
		for(;x!=null;y=x,x=x->fa){
			splay(x);
			if(x->c[1]!=null) x->w.insert(x->c[1]->sw+1);
			if(y!=null) x->w.erase(x->w.find(y->sw+1));
			x->c[1]=y;
			x->pushup();
		}
	}
	void link(node*x,node*y){
	//	cerr<<"link "<<x->val<<' '<<y->val<<endl;
		access(x);
		splay(x);
		splay(y);
		y->fa=x;
		x->w.insert(y->sw+1);
		x->pushup();
		//cerr<<"link end"<<endl;
	}
	void cut(node*x,node*y){
	//	cerr<<"cut "<<x->val<<' '<<y->val<<endl;
		access(x);
		splay(x);
		splay(y);
		y->fa=null;
		x->w.erase(x->w.find(y->sw+1));
		x->pushup();
	}
};
using lct::node;
using lct::a;
const int M=20;
int n,c[N],L[N],R[N],qu[N],qr,f[M][N],lg[N];
int x,v;
int cmp(int a,int b){ return c[a]<c[b]?a:b; }
int ask(int l,int r){
	if(l>r) r+=n;
	int d=lg[r-l+1];
	int k=cmp(f[d][l],f[d][r-(1<<d)+1]);
	return k<=n?k:k-n;
}
int main(){
	scan(n);
	read(c+1,c+n+1);
	for(int i=1;i<=n;++i){
		while(qr>0&&c[qu[qr]]>c[i]) qu[qr--]=0;
		if(qr) L[i]=qu[qr];
		qu[++qr]=i;
	}
	qr=0;
	for(int i=n;i>=1;--i){
		while(qr>0&&c[qu[qr]]>c[i]) qu[qr--]=0;
		if(qr) R[i]=qu[qr];
		qu[++qr]=i;
	}
	for(int i=1;i<=n;++i) a[i].val=c[i];
	for(int i=1;i<=n;++i) c[i+n]=c[i];
	for(int i=1;i<=n<<1;++i) f[0][i]=i;
	for(int i=2;i<=n<<1;++i) lg[i]=lg[i-1]+!(i&i-1);
	for(int d=1;1<<d<=n<<1;++d) for(int i=1;i+(1<<d)-1<=n<<1;++i) {
		f[d][i]=cmp(f[d-1][i],f[d-1][i+(1<<(d-1))]);
	}
	for(int i=1;i<=n;++i){
		if(L[i]||R[i]){
			if(c[L[i]]>c[R[i]]) lct::link(a+L[i],a+i);
			else lct::link(a+R[i],a+i);
		}else lct::link(a,a+i);
	}
	x=0;
	lct::splay(a);
	v=a->sw;
	//cerr<<v<<' '<<x<<endl;
	for(int l=1;l<n;++l){
		node *p=a+l;
		lct::access(p);
		lct::splay(p);
		node *u=lct::findprev(p);
		lct::cut(u,p);
		int s=p-a,t=u-a;
		if(s<l) s+=n; if(t<l) t+=n;
		if(s>t) swap(s,t);
		++s,--t;
		if(s<=t){
			node *v=a+ask(s,t);
			lct::cut(p,v);
			lct::link(u,v);
		}
		node *q=a+(l==1?n:l-1);
		lct::access(q);
		lct::splay(q);
		if(q->rval>p->val){
			while(true){
				if(q->c[0]->rval>p->val) q=q->c[0];
				else if(q->val>p->val) break;
				else q=q->c[1];
			}
			lct::access(q);
			lct::splay(q);
			node *k=lct::findprev(q);
			lct::cut(k,q);
			lct::link(k,p);
			lct::link(p,q);
		}else{
			lct::access(q);
			lct::splay(q);
			lct::link(q,p);
		}
		lct::splay(a);
		if(a->sw<v) v=a->sw,x=l;
	//	cerr<<v<<' '<<x<<endl;
	}
	print(v),putc(' '),print(x),putc('\n');
}


#include <bits/stdc++.h>

using namespace std;

const int N = 201110;
int A[N], l[N], r[N];
vector<pair<int,int> > rNodes;
vector <int> depth;
int n, k;

void clear() {
	rNodes.clear();
	depth.clear();
	depth.push_back(0);
	rNodes.push_back({0, 0});
}

void add(int v) {
	int cur = -1;
	while(rNodes.back().first > A[v]) {
		cur = max(cur + 1, rNodes.back().second);
		rNodes.pop_back();
		depth.pop_back();
	}
	rNodes.push_back({A[v], cur + 1});
	depth.push_back(max(depth.back(), cur + (int)rNodes.size()));
}

int main() {
	//freopen("in.in", "r", stdin);
	int k = 0, shifted = 0;
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> A[++k];
		if(A[k] == 1) {
			shifted = k;
			for(int j = n - 1; j >= n - k; --j)
				A[j] = A[j - (n - k)];
			k = 0;
		}
	}

	for(int i = 1; i < n; ++i) cout << A[i] << endl;
	clear();
	l[0] = 0;
	for(int i = 1; i < n; ++i) {
		add(i);
		l[i] = depth.back();
	}
	clear();
	int ans = l[n - 1] + 1, s = 0;
	for(int i = n - 1; i >= 2; --i) {
		add(i);
		r[i] = depth.back();
		if(ans > max(l[i - 1], r[i]) + 1) {
			ans = max(l[i - 1],  r[i]) + 1;
			s = i - 1;
		}
	}
	//for(int i = 1; i < n; ++i) cout << A[i] << " " << l[i] << " " << r[i] << endl;
	shifted = (shifted + s) % n;
	cout << ans << " " << shifted << endl;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 201110;
int A[N], l[N], r[N];
vector<pair<int,int> > rNodes;
vector <int> depth;
int n, k;

void clear() {
	rNodes.clear();
	depth.clear();
	depth.push_back(0);
	rNodes.push_back({0, 0});
}

void add(int v) {
	int cur = -1;
	while(rNodes.back().first > A[v]) {
		cur = max(cur + 1, rNodes.back().second);
		rNodes.pop_back();
		depth.pop_back();
	}
	rNodes.push_back({A[v], cur + 1});
	depth.push_back(max(depth.back(), cur + (int)rNodes.size()));
}

int main() {
	//freopen("in.in", "r", stdin);
	int k = 0, shifted = 0;
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> A[++k];
		if(A[k] == 1) {
			shifted = k;
			for(int j = n - 1; j >= n - k; --j)
				A[j] = A[j - (n - k)];
			k = 0;
		}
	}

	//for(int i = 1; i < n; ++i) cout << A[i] << endl;
	clear();
	l[0] = 0;
	for(int i = 1; i < n; ++i) {
		add(i);
		l[i] = depth.back();
	}
	clear();
	int ans = l[n - 1] + 1, s = 0;
	for(int i = n - 1; i >= 2; --i) {
		add(i);
		r[i] = depth.back();
		if(ans > max(l[i - 1], r[i]) + 1) {
			ans = max(l[i - 1],  r[i]) + 1;
			s = i - 1;
		}
	}
	//for(int i = 1; i < n; ++i) cout << A[i] << " " << l[i] << " " << r[i] << endl;
	shifted = (shifted + s) % n;
	cout << ans << " " << shifted << endl;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 201110;
int A[N], l[N], r[N];
vector<pair<int,int> > rNodes;
vector <int> depth;
int n, k;

void clear() {
	rNodes.clear();
	depth.clear();
	depth.push_back(0);
	rNodes.push_back({0, 0});
}

void add(int v) {
	int cur = -1;
	while(rNodes.back().first > A[v]) {
		cur = max(cur + 1, rNodes.back().second);
		rNodes.pop_back();
		depth.pop_back();
	}
	rNodes.push_back({A[v], cur + 1});
	depth.push_back(max(depth.back(), cur + (int)rNodes.size()));
}

int main() {
	//freopen("in.in", "r", stdin);
	int k = 0, shifted = 0;
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> A[++k];
		if(A[k] == 1) {
			shifted = k;
			for(int j = n - 1; j >= k; --j)
				A[j] = A[j - k];
			k = 0;
		}
	}

	clear();
	l[0] = 0;
	for(int i = 1; i < n; ++i) {
		add(i);
		l[i] = depth.back();
	}
	clear();
	int ans = l[n - 1] + 1, s = 0;
	for(int i = n - 1; i >= 2; --i) {
		add(i);
		r[i] = depth.back();
		if(ans > max(l[i - 1], r[i]) + 1) {
			ans = max(l[i - 1],  r[i]) + 1;
			s = i - 1;
		}
	}
	//for(int i = 1; i < n; ++i) cout << A[i] << " " << l[i] << " " << r[i] << endl;
	shifted = (shifted + s) % n;
	cout << ans << " " << shifted << endl;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e6;
int tree[MAXN], add[MAXN];
int arr[MAXN];

void pull(int v) {
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    tree[v] = max(tree[v1] + add[v1], tree[v2] + add[v2]);
}

void push(int v) {
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    add[v1] += add[v];
    add[v2] += add[v];
    tree[v] += add[v];
    add[v] = 0;
}

int get(int v, int l, int r, int vl, int vr) {
    // cout << v << endl;
    /*if (v == 1) {
        cout << l << " " << r << endl;
    }
    cout << v << " " << vl << " " << vr << endl;*/
    push(v);
    if (l >= vr || r <= vl) {
        return 0;
    }
    if (l <= vl && r >= vr) {
        return tree[v];
    }
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    int vm = (vl + vr) / 2;
    int res1 = get(v1, l, r, vl, vm);
    int res2 = get(v2, l, r, vm, vr);
    // cout << v << " | " << res1 << " | " << res2 << endl;
    return max(res1, res2);
}

void update(int v, int l, int r, int del, int vl, int vr) {
    // cout << v << " " << vl << " " << vr << endl;
    // cout << " " << v << endl;
    push(v);
    if (l >= vr || r <= vl) {
        return;
    }
    if (l <= vl && r >= vr) {
        add[v] += del;
        return;
    }
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    int vm = (vl + vr) / 2;
    update(v1, l, r, del, vl, vm);
    update(v2, l, r, del, vm, vr);
    pull(v);
    // cout << "u " << v << " " << tree[v] << " " << l << " " << r << " " << del << endl;
}

void calc(const vector<int> &arr, vector<int> &ans) {
    int n = arr.size();
    ans.resize(n, 0);
    fill(tree, tree + 4 * n, 0);
    fill(add, add + 4 * n, 0);
    vector<pair<int, int>> st;
    vector<int> lef(n, -1);
    for (int i = 0; i < n; ++i) {
        while (!st.empty() && st.back().first > arr[i]) {
            st.pop_back();
        }
        if (!st.empty()) {
            lef[i] = st.back().second;
        }
        st.push_back({arr[i], i});
    }
    for (int i = 0; i < n; ++i) {
        // cout << arr[i] << " ";
        int x = lef[i];
        // cout << x << " ";
        int k = get(1, x, x + 1, 0, n);
        // cout << k << " ";
        // cout << endl;
        update(1, x + 1, i, 1, 0, n);
        update(1, i, i + 1, k + 1, 0, n);
        // cout << get(1, i, i + 1, 0, n) << " ";
        ans[i] = get(1, 0, i + 1, 0, n);
        // cout << endl;
        /*for (int j = 0; j < 4 * n; ++j) {
            cout << "(" << tree[j] << " " << add[j] << ") ";
        }
        cout << endl;*/
        // cout << k + 1 << " ";
        // cout << ans[i] << ", ";
    }
    // cout << endl;
}

main() {
    int n;
    cin >> n;
    int imin = 0;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < arr[imin]) {
            imin = i;
        }
    }
    vector<int> a;
    for (int i = imin + 1; i != imin; i = (i + 1) % n) {
        a.push_back(arr[i]);
    }
    vector<int> pref, suf;
    calc(a, pref);
    reverse(a.begin(), a.end());
    calc(a, suf);
    /*for (auto elem : pref) {
        cout << elem << " ";
    }
    cout << endl;
    for (auto elem : suf) {
        cout << elem << " ";
    }
    cout << endl;*/
    int res = 1e9;
    int resi = -1;
    for (int i = 0; i < n; ++i) {
        int j1 = i - 1;
        int j2 = n - 1 - i - 1;
        if (j1 == -1) {
            j1 = 0;
        } else {
            j1 = pref[j1];
        }
        if (j2 == -1) {
            j2 = 0;
        } else {
            j2 = suf[j2];
        }
        if (max(j1, j2) < res) {
            res = max(j1, j2);
            resi = i;
        }
    }
    cout << res + 1 << " " << (imin + resi + 1) % n;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e6;
int tree[MAXN], add[MAXN];
int arr[MAXN];

void pull(int v) {
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    tree[v] = max(tree[v1] + add[v1], tree[v2] + add[v2]);
}

void push(int v) {
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    add[v1] += add[v];
    add[v2] += add[v];
    tree[v] += add[v];
    add[v] = 0;
}

int get(int v, int l, int r, int vl, int vr) {
    // cout << v << endl;
    /*if (v == 1) {
        cout << l << " " << r << endl;
    }
    cout << v << " " << vl << " " << vr << endl;*/
    push(v);
    if (l >= vr || r <= vl) {
        return 0;
    }
    if (l <= vl && r >= vr) {
        return tree[v];
    }
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    int vm = (vl + vr) / 2;
    int res1 = get(v1, l, r, vl, vm);
    int res2 = get(v2, l, r, vm, vr);
    // cout << v << " | " << res1 << " | " << res2 << endl;
    return max(res1, res2);
}

void update(int v, int l, int r, int del, int vl, int vr) {
    // cout << v << " " << vl << " " << vr << endl;
    // cout << " " << v << endl;
    push(v);
    if (l >= vr || r <= vl) {
        return;
    }
    if (l <= vl && r >= vr) {
        add[v] += del;
        return;
    }
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    int vm = (vl + vr) / 2;
    update(v1, l, r, del, vl, vm);
    update(v2, l, r, del, vm, vr);
    pull(v);
    // cout << "u " << v << " " << tree[v] << " " << l << " " << r << " " << del << endl;
}

void calc(const vector<int> &arr, vector<int> &ans) {
    int n = arr.size();
    ans.resize(n, 0);
    fill(tree, tree + 4 * n, 0);
    fill(add, add + 4 * n, 0);
    vector<pair<int, int>> st;
    vector<int> lef(n, -1);
    for (int i = 0; i < n; ++i) {
        while (!st.empty() && st.back().first > arr[i]) {
            st.pop_back();
        }
        if (!st.empty()) {
            lef[i] = st.back().second;
        }
        st.push_back({arr[i], i});
    }
    for (int i = 0; i < n; ++i) {
        // cout << arr[i] << " ";
        int x = lef[i];
        // cout << x << " ";
        int k = get(1, x, x + 1, 0, n);
        // cout << k << " ";
        // cout << endl;
        update(1, x + 1, i, 1, 0, n);
        update(1, i, i + 1, k + 1, 0, n);
        // cout << get(1, i, i + 1, 0, n) << " ";
        ans[i] = get(1, 0, i + 1, 0, n);
        // cout << endl;
        /*for (int j = 0; j < 4 * n; ++j) {
            cout << "(" << tree[j] << " " << add[j] << ") ";
        }
        cout << endl;*/
        // cout << k + 1 << " ";
        // cout << ans[i] << ", ";
    }
    // cout << endl;
}

main() {
    int n;
    cin >> n;
    int imin = 0;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < arr[imin]) {
            imin = i;
        }
    }
    vector<int> a;
    for (int i = (imin + 1) % n; i != imin; i = (i + 1) % n) {
        a.push_back(arr[i]);
    }
    vector<int> pref, suf;
    calc(a, pref);
    reverse(a.begin(), a.end());
    calc(a, suf);
    /*for (auto elem : pref) {
        cout << elem << " ";
    }
    cout << endl;
    for (auto elem : suf) {
        cout << elem << " ";
    }
    cout << endl;*/
    int res = 1e9;
    int resi = -1;
    for (int i = 0; i < n; ++i) {
        int j1 = i - 1;
        int j2 = n - 1 - i - 1;
        if (j1 == -1) {
            j1 = 0;
        } else {
            j1 = pref[j1];
        }
        if (j2 == -1) {
            j2 = 0;
        } else {
            j2 = suf[j2];
        }
        if (max(j1, j2) < res) {
            res = max(j1, j2);
            resi = i;
        }
    }
    cout << res + 1 << " " << (imin + resi + 1) % n;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e6;
int tree[MAXN], add[MAXN];
int arr[MAXN];

void pull(int v) {
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    tree[v] = max(tree[v1] + add[v1], tree[v2] + add[v1]);
}

void push(int v) {
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    add[v1] += add[v];
    add[v2] += add[v];
    tree[v] += add[v];
    add[v] = 0;
}

int get(int v, int l, int r, int vl, int vr) {
    // cout << v << endl;
    push(v);
    if (l >= vr || r <= vl) {
        return 0;
    }
    if (l <= vl && r >= vr) {
        return tree[v];
    }
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    int vm = (vl + vr) / 2;
    int res1 = get(v1, l, r, vl, vm);
    int res2 = get(v2, l, r, vm, vr);
    return max(res1, res2);
}

void update(int v, int l, int r, int del, int vl, int vr) {
    // cout << " " << v << endl;
    if (l >= vr || r <= vl) {
        return;
    }
    if (l <= vl && r >= vr) {
        add[v] += del;
        return;
    }
    int v1 = v * 2;
    int v2 = v * 2 + 1;
    int vm = (vl + vr) / 2;
    update(v1, l, r, del, vl, vm);
    update(v2, l, r, del, vm, vr);
    pull(v);
}

void calc(const vector<int> &arr, vector<int> &ans) {
    int n = arr.size();
    ans.resize(n, 0);
    fill(tree, tree + 4 * n, 0);
    fill(add, add + 4 * n, 0);
    vector<pair<int, int>> st;
    vector<int> lef(n, -1);
    for (int i = 0; i < n; ++i) {
        while (!st.empty() && st.back().first > arr[i]) {
            st.pop_back();
        }
        if (!st.empty()) {
            lef[i] = st.back().second;
        }
        st.push_back({arr[i], i});
    }
    for (int i = 0; i < n; ++i) {
        int x = lef[i];
        int k = get(1, x, x + 1, 0, n);
        update(1, x + 1, i, 1, 0, n);
        update(1, i, i + 1, k + 1, 0, n);
        ans[i] = get(1, 0, i + 1, 0, n);
    }
}

main() {
    int n;
    cin >> n;
    int imin = 0;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        if (arr[i] < imin) {
            imin = i;
        }
    }
    vector<int> a;
    for (int i = imin + 1; i != imin; i = (i + 1) % n) {
        a.push_back(arr[i]);
    }
    vector<int> pref, suf;
    calc(a, pref);
    reverse(a.begin(), a.end());
    calc(a, suf);
    /*for (auto elem : pref) {
        cout << elem << " ";
    }
    cout << endl;
    for (auto elem : suf) {
        cout << elem << " ";
    }
    cout << endl;*/
    int res = 1e9;
    int resi = -1;
    for (int i = 0; i < n; ++i) {
        int j1 = i - 1;
        int j2 = n - 1 - i - 1;
        if (j1 == -1) {
            j1 = 0;
        } else {
            j1 = pref[j1];
        }
        if (j2 == -1) {
            j2 = 0;
        } else {
            j2 = suf[j2];
        }
        if (max(j1, j2) < res) {
            res = max(j1, j2);
            resi = i;
        }
    }
    cout << res + 1 << " " << (imin + resi + 1) % n;
    return 0;
}


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl;
#define Get(x,y) scanf("%I64d%I64d",&x,&y);
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define Gett(x,y) scanf("%d%d",&x,&y);
#define f(i,a,b) for(int i=a;i<b;i++)
#define get(x) scanf("%I64d",&x);
#define gett(x) scanf("%d",&x);
#define ops(x) cout<<x<<" !"<<endl;
#define maxm(a,b) a=max(a,b);
#define minm(a,b) a=min(a,b);
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=2e5+9,inf=1e9;

int n,l,r,p1,an,mid,ans1,ans2,A[N],a[N];
pair<int,int> p,tree[N];

int query(int l, int r) {
	pair<int,int> res;r++;
	res.F=inf;
	for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
   	if(l&1){
			if(tree[l]<res)
				res=tree[l];
			l++;
		}
   	if(r&1)
   		if(tree[--r]<res)
   			res=tree[r];
	}
  	return res.S;
}
void make(){
	f(i,n,2*n)
		tree[i]=mp(a[i-n],i-n);
	f_(i,n-1,1){
		if(tree[i*2]<tree[i*2+1]) tree[i]=tree[i*2];
		else tree[i]=tree[i*2+1];
	}
}
int calc(int l,int r){
	if(r<l) return 0;
	an=query(l,r);
	return max(calc(l,an-1),calc(an+1,r))+1;
}
int check(int x){
	p.F=calc(1,x);
	p.S=calc(x+1,n-1);
	return max(p.F,p.S)+1;
}
void solve(){
	l=0,r=n-1;
	while(l+1<r){
		mid=(l+r)/2;
		check(mid);
		if(p.F<p.S) l=mid;
		else r=mid;
	}
}
int main(){
	cin>>n;
	f(i,0,n){
		cin>>A[i];
		if(A[i]==1)
			p1=i;
	}
	for(int i=p1;an<n;i=(i+1)%n)
		a[an++]=A[i];
	make();
	solve();
	cout<<check(l)<<" "<<(p1+(n-l-1))%n<<endl;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mp make_pair
#define F first
#define S second

ll arr[200000];
int t[200000];
int r[200000];
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int count = 0;
    for (int i = 0; i < m; i++)
    {
        int at, ar;
        cin >> at >> ar;
        while (count > 0 && ar >= r[count - 1])
        {
            count--;
        }
        t[count] = at;
        r[count] = ar;
        count++;
    }
    ll b[r[0]];
    for (int i = 0; i < r[0]; i++)
    {
        b[i] = arr[i];
    }
    sort(b, b + r[0]);
    int bl = 0;
    int br = r[0] - 1;
    for(int i = 1; i < count + 1; i++){
        for(int j = r[i - 1]; j > r[i]; j--){
            if(t[i - 1] == 1){
                arr[j - 1] = b[br];
                br--;
            }
            else{
                arr[j - 1] = b[bl];
                bl++;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mp make_pair
#define F first
#define S second

ll arr[200000];
int t[200000];
int r[200000];
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int count = 0;
    for (int i = 0; i < m; i++)
    {
        int at, ar;
        cin >> at >> ar;
        while (count > 0 && ar >= r[count - 1])
        {
            count--;
        }
        t[count] = at;
        r[count] = ar;
        count++;
    }
    ll b[r[0]];
    for (int i = 0; i < r[0]; i++)
    {
        b[i] = arr[i];
    }
    sort(b, b + r[0]);
    int bl = 0;
    int br = r[0];
    for(int i = 1; i < count + 1; i++){
        for(int j = r[i - 1]; j > r[i]; j--){
            if(t[i - 1] == 1){
                arr[j - 1] = b[br];
                br--;
            }
            else{
                arr[j - 1] = b[bl];
                bl++;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main()
{
    int n, m;
    cin >> n >> m;
    ll arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int t[m];
    int r[m];
    int s = 0;
    for (int i = 0; i < m; i++)
    {
        int at, ar;
        cin >> at >> ar;
        while (s > 0 && ar >= r[s - 1])
        {
            s--;
        }
        t[s] = at;
        r[s] = ar;
        s++;
    }
    int bl = 0;
    int br = r[0] - 1;
    r[s] = 0;
    s++;
    ll b[r[0]];
    for (int i = 0; i < r[0]; i++)
    {
        b[i] = arr[i];
    }
    sort(b, b + br + 1);
    for (int i = 1; i < s; i++)
    {
        for (int j = r[i - 1]; j > r[i]; j--)
        {
            if (t[i - 1] == 1)
            {
                arr[j - 1] = b[br];
                br--;
            }
            else
            {
                arr[j - 1] = b[bl];
                bl++;
            }
        }
    }
    for (int i = 0; i < n; i++)
        {
            cout << arr[i] << ' ';
        }
        cout << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main()
{
    int n, m;
    cin >> n >> m;
    ll arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int t[m];
    int r[m];
    int s = 0;
    for (int i = 0; i < m; i++)
    {
        int at, ar;
        cin >> at >> ar;
        while (s > 0 && ar >= r[s - 1])
        {
            s--;
        }
        t[s] = at;
        r[s] = ar;
        s++;
    }
    int bl = 0;
    int br = r[0] - 1;
    //r[s] = 0;
    s++;
    ll b[r[0]];
    for (int i = 0; i < r[0]; i++)
    {
        b[i] = arr[i];
    }
    sort(b, b + br + 1);
    for (int i = 1; i < s; i++)
    {
        for (int j = r[i - 1]; j > r[i]; j--)
        {
            if (t[i - 1] == 1)
            {
                arr[j - 1] = b[br];
                br--;
            }
            else
            {
                arr[j - 1] = b[bl];
                bl++;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mp make_pair
#define F first
#define S second

ll arr[200000];
pair<int, int> mng[200000];
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int count = 0;
    for (int i = 0; i < m; i++)
    {
        int t, r;
        cin >> t >> r;
        while (count > 0 && r >= mng[count - 1].S)
        {
            count--;
        }
        mng[count] = mp(t, r);
        count++;
    }
    ll b[mng[0].S];
    for (int i = 0; i < mng[0].S; i++)
    {
        b[i] = arr[i];
    }
    sort(b, b + mng[0].S);
    int l = 0;
    int r = mng[0].S - 1;
    for(int i = 1; i < count + 1; i++){
        for(int j = mng[i - 1].S; j > mng[i].S; j--){
            if(mng[i - 1].F == 1){
                arr[j - 1] = b[r];
                r--;
            }
            else{
                arr[j - 1] = b[l];
                l++;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main()
{
    int n, m;
    cin >> n >> m;
    ll arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int t[m];
    int r[m];
    int s = 0;
    for (int i = 0; i < m; i++)
    {
        int at, ar;
        cin >> at >> ar;
        while (s > 0 && ar >= r[s - 1])
        {
            s--;
        }
        t[s] = at;
        r[s] = ar;
        s++;
    }
    int bl = 0;
    int br = r[0] - 1;
    r[s] = 0;
    s++;
    ll b[r[0]];
    for (int i = 0; i < r[0]; i++)
    {
        b[i] = arr[i];
    }
    sort(b, b + br + 1);
    for (int i = 1; i < s; i++)
    {
        for (int j = r[i - 1]; j > r[i]; j--)
        {
            if (t[i - 1] == 1)
            {
                arr[j - 1] = b[br];
                br--;
            }
            else
            {
                arr[j - 1] = b[bl];
                bl++;
            }
        }
        for (int i = 0; i < n; i++)
        {
            cout << arr[i] << ' ';
        }
        cout << '\n';
    }
    return 0;
}


//Utkarsh.25dec
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i,n) for(ll i=0;i<n;i++)
#define loop(i,a,b) for(ll i=a;i<=b;i++)
#define vi vector <int>
#define vs vector <string>
#define vc vector <char>
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
using namespace std;
const int N=500023;
bool vis[N];
vector <int> adj[N];
void solve()
{
    ll n,m;
    cin>>n>>m;
    vl v;
    rep(i,n)
    {
        ll c;
        cin>>c;
        v.pb(c);
    }
    set <pair<ll,ll>> s;
    for(int i=1;i<=m;i++)
    {
        ll a,b;
        cin>>a>>b;
        auto it=s.upper_bound(mp(b,10));
        vector <pair<ll,ll>> v;
        for(auto itr=s.begin();itr!=it;itr++)
        {
            v.pb((*itr));
        }
        // cout<<v.size()<<'\n';
        // return;
        for(int i=0;i<v.size();i++)
            s.erase(v[i]);
        s.insert(mp(b,a));
    }
    // cout<<s.size();
    // return;
    vector <pair<ll,ll>> temp;
    for(auto it=s.begin();it!=s.end();it++)
    {
        if(it==s.begin())
            continue;
        auto itr=it;
        itr--;
        if((*it).second==(*itr).second)
            temp.pb((*itr));
    }
    for(auto ele:temp)
        s.erase(ele);
    // cout<<s.size()<<'\n';
    // for(auto it:s)
    //     cout<<(it).first<<' '<<(it).second<<'\n';
    auto it=s.end();
    it--;
    vl ans;
    multiset <ll> store;
    for(int i=n-1;i>=0;i--)
    {
        if(i>=(*it).first)
            ans.pb(v[i]);
        else
            store.insert(v[i]);
    }
    // cout<<store.size()<<'\n';
    for(;;it--)
    {
        if(it==s.begin())
            break;
        auto itr=it;
        itr--;
        ll a=(*it).first;
        ll c=(*itr).first;
        ll terms=a-c;
        if((*it).second==1)
        {
            ll index=0;
            for(;;)
            {
                auto itrr=store.end();
                itrr--;
                if(index>=terms)
                    break;
                index++;
                ans.pb((*itrr));
                store.erase(itrr);
            }
        }
        else
        {
            ll index=0;
            for(;;)
            {
                auto itrr=store.begin();
                if(index>=terms)
                    break;
                index++;
                ans.pb((*itrr));
                store.erase(itrr);
            }
        }
    }
    it=s.begin();
    ll a=(*it).first;
    ll b=(*it).second;
    // cout<<store.size()<<'\n';
    if(store.size()!=0)
    {
        if(b==1)
        {
            auto itrr=store.end();
            itrr--;
            for(;;itrr--)
            {
                ans.pb((*itrr));
                if(itrr==store.begin())
                    break;
            }
        }
        else
        {
            for(auto itrr=store.begin();itrr!=store.end();itrr++)
                ans.pb((*itrr));
        }
    }
    reverse(all(ans));
    for(int i=0;i<ans.size();i++)
        cout<<ans[i]<<' ';
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T=1;
    //cin>>T;
    int t=0;
    while(t++<T)
    {
        //cout<<"Case #"<<t<<":"<<' ';
        solve();
        //cout<<'\n';
    }
}


//Utkarsh.25dec
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i,n) for(ll i=0;i<n;i++)
#define loop(i,a,b) for(ll i=a;i<=b;i++)
#define vi vector <int>
#define vs vector <string>
#define vc vector <char>
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
using namespace std;
const int N=500023;
bool vis[N];
vector <int> adj[N];
void solve()
{
    ll n,m;
    cin>>n>>m;
    vl v;
    rep(i,n)
    {
        ll c;
        cin>>c;
        v.pb(c);
    }
    set <pair<ll,ll>> s;
    for(int i=1;i<=m;i++)
    {
        ll a,b;
        cin>>a>>b;
        auto it=s.upper_bound(mp(b,10));
        vector <pair<ll,ll>> v;
        for(auto itr=s.begin();itr!=it;itr++)
        {
            v.pb((*itr));
        }
        for(int i=0;i<v.size();i++)
            s.erase(v[i]);
        s.insert(mp(b,a));
    }
    vector <pair<ll,ll>> temp;
    for(auto it=s.begin();it!=s.end();it++)
    {
        if(it==s.begin())
            continue;
        auto itr=it;
        itr--;
        if((*it).second==(*itr).second)
            temp.pb((*it));
    }
    for(auto ele:temp)
        s.erase(ele);
    auto it=s.end();
    it--;
    vl ans;
    multiset <ll> store;
    for(int i=n-1;i>=0;i--)
    {
        if(i>=(*it).first)
            ans.pb(v[i]);
        else
            store.insert(v[i]);
    }
    // cout<<store.size()<<'\n';
    for(;;it--)
    {
        if(it==s.begin())
            break;
        auto itr=it;
        itr--;
        ll a=(*it).first;
        ll c=(*itr).first;
        ll terms=a-c;
        if((*it).second==1)
        {
            ll index=0;
            for(;;)
            {
                auto itrr=store.end();
                itrr--;
                if(index>=terms)
                    break;
                index++;
                ans.pb((*itrr));
                store.erase(itrr);
            }
        }
        else
        {
            ll index=0;
            for(;;)
            {
                auto itrr=store.begin();
                if(index>=terms)
                    break;
                index++;
                ans.pb((*itrr));
                store.erase(itrr);
            }
        }
    }
    it=s.begin();
    ll a=(*it).first;
    ll b=(*it).second;
    // cout<<store.size()<<'\n';
    if(store.size()!=0)
    {
        if(b==1)
        {
            auto itrr=store.end();
            itrr--;
            for(;;itrr--)
            {
                ans.pb((*itrr));
                if(itrr==store.begin())
                    break;
            }
        }
        else
        {
            for(auto itrr=store.begin();itrr!=store.end();itrr++)
                ans.pb((*itrr));
        }
    }
    reverse(all(ans));
    for(int i=0;i<ans.size();i++)
        cout<<ans[i]<<' ';
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T=1;
    //cin>>T;
    int t=0;
    while(t++<T)
    {
        //cout<<"Case #"<<t<<":"<<' ';
        solve();
        //cout<<'\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

bool comp(int a, int b) { return a > b; }
const int N = 2e5 + 10;
vector<int> b(N);

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    vector<int> r(m);
    vector<bool> t(m);
    int it = 0;
    for(int i = 0; i < m; i++)
    {
        int tr, type;
        scanf("%d%d", &type, &tr);
        while(it > 0 && r[it - 1] <= tr)
            it--;
        r[it] = tr;
        if (type == 2)
            t[it] = true;
        else
            t[it] = false;
        it++;
    }
    int lf = 0;
    int rg = r[0];
    r[it] = 0;
    ++it;
    for (int i = 0; i < rg; ++i)
        b[i] = a[i];
    sort(b.begin(), b.begin() + rg);
    for (int i = 1; i < it; ++i)
        for (int j = r[i - 1]; j > r[i]; --j)
            if (!t[i - 1])
                a[j - 1] = b[--rg];
            else
                a[j - 1] = b[lf++];

    for (int i = 0; i < n; ++i)
        printf("%d ", a[i]);
    cout << endl;
}


#include <bits/stdc++.h>
using namespace std;

bool comp(int a, int b) { return a > b; }

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    vector<int> r(m);
    vector<bool> t(m);
    int it = 0;
    for(int i = 0; i < m; i++)
    {
        int tr, type;
        scanf("%d%d", &type, &tr);
        while(it > 0 && r[it - 1] <= tr)
            it--;
        r[it] = tr;
        if (type == 2)
            t[it] = true;
        else
            t[it] = false;
        it++;
    }
    for(int i = 0; i < it; i++)
    {
        vector<int> sorted(r[i]);
        for(int j = 0; j < r[i]; j++)
            sorted[j] = a[j];
        sort(sorted.begin(), sorted.end());
        if (!t[i])
            for(int j = 0; j < r[i]; j++)
                a[j] = sorted[j];
        else
            for(int j = 0; j < r[i]; j++)
                a[j] = sorted[sorted.size() - 1 - j];
    }
    for(int i = 0; i < n; i++)
        printf("%d ", a[i]);
    cout << endl;
}


#include <bits/stdc++.h>
using namespace std;

bool comp(int a, int b) { return a > b; }

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    vector<int> r(m);
    vector<bool> t(m);
    int it = 0;
    for(int i = 0; i < m; i++)
    {
        int tr, type;
        scanf("%d%d", &type, &tr);
        while(it > 0 && r[it - 1] <= tr)
            it--;
        r[it] = tr;
        if (type == 2)
            t[it] = true;
        it++;
    }
    vector<int> sorted(r[0]);
    for(int i = 0; i < m; i++)
    {

        for(int j = 0; j < r[i]; j++)
            sorted[j] = a[j];
        sort(sorted.begin(), sorted.end());
        if (!t[i])
            for(int j = 0; j < r[i]; j++)
                a[j] = sorted[j];
        else
            for(int j = 0; j < r[i]; j++)
                a[j] = sorted[sorted.size() - 1 - j];
    }
    for(int i = 0; i < n; i++)
        printf("%d ", a[i]);
}


#include <bits/stdc++.h>
using namespace std;

bool comp(int a, int b) { return a > b; }

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    vector<int> r(m);
    vector<bool> t(m);
    int it = 0;
    for(int i = 0; i < m; i++)
    {
        int tr, type;
        scanf("%d%d", &type, &tr);
        while(it > 0 && r[it - 1] <= tr)
            it--;
        r[it] = tr;
        if (type == 2)
            t[it] = true;
        it++;
    }
    for(int i = 0; i < m; i++)
    {

        vector<int> sorted(r[i]);
        for(int j = 0; j < r[i]; j++)
            sorted[j] = a[j];
        sort(sorted.begin(), sorted.end());
        if (!t[i])
            for(int j = 0; j < r[i]; j++)
                a[j] = sorted[j];
        else
            for(int j = 0; j < r[i]; j++)
                a[j] = sorted[sorted.size() - 1 - j];
    }
    for(int i = 0; i < n; i++)
        printf("%d ", a[i]);
}


#include <bits/stdc++.h>
using namespace std;

bool comp(int a, int b) { return a > b; }

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    vector<int> r(m);
    vector<bool> t(m);
    for(int i = 0; i < m; i++)
    {
        int tr, type;
        scanf("%d%d", &type, &tr);
        int it = i;
        while(it > 0 && r[it - 1] < tr)
            it--;
        r[it] = tr;
        if (type == 2)
            t[it] = true;
    }
    for(int i = 0; i < m; i++)
        if (r[i] > 0)
        {
            if (t[i])
                sort(a.begin(), a.begin() + r[i], comp);
            else
                sort(a.begin(), a.begin() + r[i]);
        }
        else
            break;
    for(int i = 0; i < n; i++)
        printf("%d ", a[i]);
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , r[N] , ans[N] , t[N] ;
multiset <int> st , b;
void update(int prev , int now , int type){
    if(type==1){
        while(now > prev){
            auto it = b.end();
            it--;
            int cur = *it;
            ans[now] = cur;
            b.erase(it);
            now--;
        }
    }
    else {
         while(now > prev){
            auto it = b.begin();
            int cur = *it;
            ans[now] = cur;
            b.erase(it);
            now--;
        }
    }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ; i <= n ; i++) { cin >> a[i]; b.insert(a[i]); }
    for(int i = 0 ; i < m ; i++){
        cin >> t[i] >> r[i];
        st.insert(r[i]);
    }
    auto top = st.end();
    top--;
    int cur = *top;
    for(int i = cur + 1 ; i <= n ; i++) b.erase(a[i]);
    st.insert(0);
    for(int i = 0 ; i < m ; i++){
        if(st.empty()) break;
        auto top = st.end();
        top--;
        int cur = *top;
        if(r[i]==cur){
         auto prev = st.end();
         prev--;
         prev--;
         int s = *prev;
         update(s,cur,t[i]);
         st.erase(top);
        }
        else st.erase(r[i]);
    }
    for(int i = 1 ; i<= n ; i ++){
       if(ans[i]==0) cout << a[i] << ' ';
       else cout << ans[i] << ' ';
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , t[N] , r[N] , f[N] , w[N] , ans[N] , sued[N] , c , L = 1 , R  , bb ;
multiset<int> q;
void update(int prev , int now , int type){
        while(now > prev){
            if(type==2){
            if(L < R){
            ans[now] = a[L];
            sued[now] = 1;
            L++;
            }
            else{
            ans[now] = a[R];
            sued[now] = 1;
            R++;
            }
            }
            else {
            // R = max(R,L);
            if(R > L){
            ans[now] = a[R];
            sued[now] = 1;
            R--;
            }
            else{
            ans[now] = a[L];
            sued[now] = 1;
            L++;
            }
            }
            now--;
        }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ;  i <= n ; i++) cin >> a[i];
    for(int i = 0 ; i < m ; i ++){
        cin >> t[i] >> r[i];
        q.insert(r[i]);
    }
    auto top = q.end();
    top--;
    bb = *top;
    R = bb;
    sort(a+1,a+bb+1);
    for(int i = 0 ; i < m ; i++){
        auto top = q.end();
        top--;
        if(r[i]==*top){
            if(c==0) {
                c++;
                f[c] = r[i];
                w[c] = t[i];
                // continue;
            }
            else if(f[c-1] == r[i]) w[c-1] = t[i];
            else {
                c++;
                f[c] = r[i];
                w[c] = t[i];
            }
        }
        q.erase(q.find(r[i]));
    }
    c++;
    f[c] = 0 , w[c] = 0;
    for(int i = 1 ; i <= c ; i++) {
        // cout << f[i] << " " << w[i] << '\n';
        update(f[i+1],f[i],w[i]);
    }
    for(int i = 1; i <= n ; i ++){
        if(sued[i]==0) cout << a[i] << ' ' ;
        else cout << ans[i] << ' ';
    }
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , t[N] , r[N] , f[N] , w[N] , ans[N] , sued[N] , c , L = 1 , R  , bb ;
multiset<int> q;
void update(int prev , int now , int type){
        while(now > prev){
            if(type==2){
            ans[now] = a[L];
            sued[now] = 1;
            L++;
            }
            else {
            ans[now] = a[R];
            sued[now] = 1;
            R--;
            }
            now--;
        }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ;  i <= n ; i++) cin >> a[i];
    for(int i = 0 ; i < m ; i ++){
        cin >> t[i] >> r[i];
        q.insert(r[i]);
    }
    auto top = q.end();
    top--;
    bb = *top;
    R = bb;
    sort(a+1,a+bb+1);
    for(int i = 0 ; i < m ; i++){
        auto top = q.end();
        top--;
        if(r[i]==*top){
            if(c==0) {
                c++;
                f[c] = r[i];
                w[c] = t[i];
            }
            else if(f[c] == r[i]) w[c] = t[i];
            else {
                c++;
                f[c] = r[i];
                w[c] = t[i];
            }
        }
        q.erase(q.find(r[i]));
    }
    c++;
    f[c] = 0 , w[c] = 0;
    for(int i = 1 ; i <= c ; i++) {
        update(f[i+1],f[i],w[i]);
    }
    for(int i = 1; i <= n ; i ++){
        if(sued[i]==0) cout << a[i] << ' ' ;
        else cout << ans[i] << ' ';
    }
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , t[N] , r[N] , f[N] , w[N] , ans[N] , sued[N] , c , L = 1 , R  , bb ;
multiset<int> q;
void update(int prev , int now , int type){
        while(now > prev){
            if(type==2){
            ans[now] = a[L];
            sued[now] = 1;
            L++;
            }
            else {
            ans[now] = a[R];
            sued[now] = 1;
            R--;
            }
            now--;
        }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ;  i <= n ; i++) cin >> a[i];
    for(int i = 0 ; i < m ; i ++){
        cin >> t[i] >> r[i];
        q.insert(r[i]);
    }
    auto top = q.end();
    top--;
    bb = *top;
    R = bb;
    sort(a+1,a+bb+1);
    for(int i = 0 ; i < m ; i++){
        auto top = q.end();
        top--;
        if(r[i]==*top){
            if(c==0) {
                c++;
                f[c] = r[i];
                w[c] = t[i];
                // continue;
            }
            else if(f[c-1] == r[i]) w[c-1] = t[i];
            else {
                c++;
                f[c] = r[i];
                w[c] = t[i];
            }
        }
        q.erase(q.find(r[i]));
    }
    c++;
    f[c] = 0 , w[c] = 0;
    for(int i = 1 ; i <= c ; i++) {
        // cout << f[i] << " " << w[i] << '\n';
        update(f[i+1],f[i],w[i]);
    }
    for(int i = 1; i <= n ; i ++){
        if(sued[i]==0) cout << a[i] << ' ' ;
        else cout << ans[i] << ' ';
    }
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , t[N] , r[N] , f[N] , w[N] , ans[N] , c , L = 1 , R  , bb ;
multiset<int> q;
void update(int prev , int now , int type){
        while(now > prev){
            if(type==2){
            ans[now] = a[L];
            L++;
            }
            else {
            ans[now] = a[R];
            R--;
            }
            now--;
        }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ;  i <= n ; i++) cin >> a[i];
    for(int i = 0 ; i < m ; i ++){
        cin >> t[i] >> r[i];
        q.insert(r[i]);
    }
    auto top = q.end();
    top--;
    bb = *top;
    R = bb;
    sort(a+1,a+bb+1);
    for(int i = 0 ; i < m ; i++){
        auto top = q.end();
        top--;
        if(r[i]==*top){
            if(f[c-1] == r[i]) w[c-1] = t[i];
            else {
                c++;
                f[c] = r[i];
                w[c] = t[i];
            }
        }
        q.erase(q.find(r[i]));
    }
    c++;
    f[c] = 0 , w[c] = 0;
    for(int i = 1 ; i <= c ; i++) {
        update(f[i+1],f[i],w[i]);
    }
    for(int i = 1; i <= n ; i ++){
        if(ans[i]==0) cout << a[i] << ' ' ;
        else cout << ans[i] << ' ';
    }
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , t[N] , r[N] , f[N] , w[N] , ans[N] , sued[N] , c , L = 1 , R  , bb ;
multiset<int> q;
void update(int prev , int now , int type){
        while(now > prev){
            if(type==2){
            ans[now] = a[L];
            sued[now] = 1;
            L++;
            }
            else {
            ans[now] = a[R];
            sued[now] = 1;
            R--;
            }
            now--;
        }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ;  i <= n ; i++) cin >> a[i];
    for(int i = 0 ; i < m ; i ++){
        cin >> t[i] >> r[i];
        q.insert(r[i]);
    }
    auto top = q.end();
    top--;
    bb = *top;
    R = bb;
    sort(a+1,a+bb+1);
    for(int i = 0 ; i < m ; i++){
        auto top = q.end();
        top--;
        if(r[i]==*top){
            if(f[c-1] == r[i]) w[c-1] = t[i];
            else {
                c++;
                f[c] = r[i];
                w[c] = t[i];
            }
        }
        q.erase(q.find(r[i]));
    }
    c++;
    f[c] = 0 , w[c] = 0;
    for(int i = 1 ; i <= c ; i++) {
        update(f[i+1],f[i],w[i]);
    }
    for(int i = 1; i <= n ; i ++){
        if(sued[i]==0) cout << a[i] << ' ' ;
        else cout << ans[i] << ' ';
    }
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , r[N] , ans[N] , t[N] ;
multiset <int> st , b;
void update(int prev , int now , int type){
    if(type==1){
        while(now > prev){
            auto it = b.end();
            it--;
            int cur = *it;
            ans[now] = cur;
            b.erase(it);
            now--;
        }
    }
    else {
         while(now > prev){
            auto it = b.begin();
            int cur = *it;
            ans[now] = cur;
            b.erase(it);
            now--;
        }
    }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ; i <= n ; i++) { cin >> a[i]; b.insert(a[i]); }
    for(int i = 0 ; i < m ; i++){
        cin >> t[i] >> r[i];
        st.insert(r[i]);
    }
    auto top = st.end();
    top--;
    int cur = *top;
    for(int i = cur + 1 ; i <= n ; i++) b.erase(b.find(a[i]));
    st.insert(0);
    for(int i = 0 ; i < m ; i++){
        if(st.empty()) break;
        auto top = st.end();
        top--;
        int cur = *top;
        if(r[i]==cur){
         auto prev = st.end();
         prev--;
         prev--;
         int s = *prev;
         update(s,cur,t[i]);
         st.erase(top);
        }
        else st.erase(st.find(r[i]));
    }
    for(int i = 1 ; i<= n ; i ++){
       if(ans[i]==0) cout << a[i] << ' ';
       else cout << ans[i] << ' ';
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , r[N] , ans[N] , t[N] ;
multiset <int> st , b;
void update(int prev , int now , int type){
    if(type==1){
        while(now > prev){
            auto it = b.end();
            it--;
            int cur = *it;
            ans[now] = cur;
            b.erase(it);
            now--;
        }
    }
    else {
         while(now > prev){
            auto it = b.begin();
            int cur = *it;
            ans[now] = cur;
            b.erase(it);
            now--;
        }
    }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ; i <= n ; i++) { cin >> a[i]; b.insert(a[i]); }
    for(int i = 0 ; i < m ; i++){
        cin >> t[i] >> r[i];
        st.insert(r[i]);
    }
    auto top = st.end();
    top--;
    int cur = *top;
    for(int i = cur + 1 ; i <= n ; i++) b.erase(a[i]);
    st.insert(0);
    st.insert(0);
    st.insert(0);
    for(int i = 0 ; i < m ; i++){
        auto top = st.end();
        top--;
        int cur = *top;
        if(r[i]==cur){
         auto prev = st.end();
         prev--;
         prev--;
         int s = *prev;
         update(s,cur,t[i]);
         st.erase(top);
        }
        else st.erase(r[i]);
    }
    for(int i = 1 ; i<= n ; i ++){
       if(ans[i]==0) cout << a[i] << ' ';
       else cout << ans[i] << ' ';
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , r[N] , ans[N] , t[N] ;
multiset <int , greater <int> > st;
set <int,greater<int > > b;
set <int> d;
void update(int prev , int now , int type){
    if(type==1){
        while(now > prev){
            auto it = b.begin();
            int cur = *it;
            ans[now] = cur;
            b.erase(it);
            d.erase(cur);
            now--;
        }
    }
    else {
         while(now > prev){
            auto it = d.begin();
            int cur = *it;
            ans[now] = cur;
            b.erase(cur);
            d.erase(it);
            now--;
        }
    }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ; i <= n ; i++) { cin >> a[i]; b.insert(a[i]); d.insert(a[i]); }
    for(int i = 0 ; i < m ; i++){
        cin >> t[i] >> r[i];
        st.insert(r[i]);
    }
    auto top = st.begin();
    int cur = *top;
    for(int i = cur + 1 ; i <= n ; i++) { b.erase(a[i]); d.erase(a[i]); }
    st.insert(0);
    for(int i = 0 ; i < m ; i++){
        auto top = st.begin();
        int cur = *top;
        if(r[i]==cur){
         auto prev = st.begin();
         prev++;
         int s = *prev;
         update(s,cur,t[i]);
         st.erase(top);
        }
        else st.erase(r[i]);
    }
    for(int i = 1 ; i<= n ; i ++){
       if(ans[i]==0) cout << a[i] << ' ';
       else cout << ans[i] << ' ';
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , r[N] , ans[N] , t[N] ;
multiset <int , greater <int> > st , b;
multiset <int> d;
void update(int prev , int now , int type){
    if(type==1){
        while(now > prev){
            auto it = b.begin();
            // cout << *it << " ";
            int cur = *it;
            ans[now] = cur;
            b.erase(it);
            d.erase(cur);
            now--;
        }
    }
    else {
         while(now > prev){
            auto it = d.begin();
            int cur = *it;
            ans[now] = cur;
            b.erase(cur);
            d.erase(it);
            now--;
        }
    }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ; i <= n ; i++) { cin >> a[i]; b.insert(a[i]); d.insert(a[i]); }
    for(int i = 0 ; i < m ; i++){
        cin >> t[i] >> r[i];
        st.insert(r[i]);
    }
    auto top = st.begin();
    int cur = *top;
    for(int i = cur + 1 ; i <= n ; i++) { b.erase(a[i]); d.erase(a[i]); }
    st.insert(0);
    for(int i = 0 ; i < m ; i++){
        auto top = st.begin();
        int cur = *top;
        if(r[i]==cur){
         auto prev = st.begin();
         prev++;
         int s = *prev;
         update(s,cur,t[i]);
         st.erase(top);
        }
        else st.erase(r[i]);
    }
    for(int i = 1 ; i<= n ; i ++){
       if(ans[i]==0) cout << a[i] << ' ';
       else cout << ans[i] << ' ';
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , r[N] , ans[N] , t[N] ;
multiset <int> st , b;
void update(int prev , int now , int type){
    if(type==1){
        while(now > prev){
            auto it = b.end();
            it--;
            ans[now] = *it;
            b.erase(it);
            now--;
        }
    }
    else {
         while(now > prev){
            auto it = b.begin();
            ans[now] = *it;
            b.erase(it);
            now--;
        }
    }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ; i <= n ; i++) { cin >> a[i]; b.insert(a[i]); }
    for(int i = 0 ; i < m ; i++){
        cin >> t[i] >> r[i];
        st.insert(r[i]);
    }
    auto top = st.end();
    top--;
    for(int i = *top + 1 ; i <= n ; i++) b.erase(a[i]);
    st.insert(0);
    st.insert(0);
    st.insert(0);
    for(int i = 0 ; i < m ; i++){
        auto top = st.end();
        top--;
        if(r[i]==*top){
         auto prev = st.end();
         prev--;
         prev--;
         update(*prev,*top,t[i]);
         st.erase(top);
        }
        else st.erase(r[i]);
    }
    for(int i = 1 ; i<= n ; i ++){
       if(ans[i]==0) cout << a[i] << ' ';
       else cout << ans[i] << ' ';
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , r[N] , ans[N] , t[N] ;
multiset <int> st , b;
void update(int prev , int now , int type){
    if(type==1){
        while(now > prev){
            auto it = b.end();
            it--;
            ans[now] = *it;
            b.erase(it);
            now--;
        }
    }
    else {
         while(now > prev){
            auto it = b.begin();
            ans[now] = *it;
            b.erase(it);
            now--;
        }
    }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ; i <= n ; i++) { cin >> a[i]; b.insert(a[i]); }
    for(int i = 0 ; i < m ; i++){
        cin >> t[i] >> r[i];
        st.insert(r[i]);
    }
    auto top = st.end();
    top--;
    for(int i = *top + 1 ; i <= n ; i++) b.erase(a[i]);
    st.insert(0);
    for(int i = 0 ; i < m ; i++){
        auto top = st.end();
        top--;
        if(r[i]==*top){
         auto prev = st.end();
         prev--;
         prev--;
         update(*prev,*top,t[i]);
         st.erase(top);
        }
        else st.erase(r[i]);
    }
    for(int i = 1 ; i<= n ; i ++){
       if(ans[i]==0) cout << a[i] << ' ';
       else cout << ans[i] << ' ';
    }

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , m , a[N] , r[N] , ans[N] , t[N] , sued[N] ;
multiset <int> st , b;
void update(int prev , int now , int type){
    if(type==1){
        while(now > prev){
            auto it = b.end();
            it--;
            int cur = *it;
            ans[now] = cur;
            sued[now] = 1;
            b.erase(it);
            now--;
        }
    }
    else {
         while(now > prev){
            auto it = b.begin();
            int cur = *it;
            ans[now] = cur;
            sued[now] = 1;
            b.erase(it);
            now--;
        }
    }
}
int main(){
fcin;
    cin >> n >> m ;
    for(int i = 1 ; i <= n ; i++) { cin >> a[i]; b.insert(a[i]); }
    for(int i = 0 ; i < m ; i++){
        cin >> t[i] >> r[i];
        st.insert(r[i]);
    }
    auto top = st.end();
    top--;
    int cur = *top;
    for(int i = cur + 1 ; i <= n ; i++) b.erase(b.find(a[i]));
    st.insert(0);
    for(int i = 0 ; i < m ; i++){
        if(st.empty()) break;
        auto top = st.end();
        top--;
        int cur = *top;
        if(r[i]==cur){
         auto prev = st.end();
         prev--;
         prev--;
         int s = *prev;
         update(s,cur,t[i]);
         st.erase(top);
        }
        else st.erase(st.find(r[i]));
    }
    for(int i = 1 ; i<= n ; i ++){
       if(sued[i]==0) cout << a[i] << ' ';
       else cout << ans[i] << ' ';
    }

}