#include<bits/stdc++.h>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
const int mod=1e9+9;
const int maxn=3e5+5;
int n,m;
int a[maxn],fib[maxn],sum[maxn];
void pre()
{
	fib[1]=fib[2]=1;
	for(int i=3;i<maxn;i++) fib[i]=(fib[i-1]+fib[i-2])%mod;
	for(int i=1;i<maxn;i++) sum[i]=(sum[i-1]+fib[i])%mod;
}
struct point
{
	int s1,s2,sum;
}p[maxn<<2];
int get(int s1,int s2,int len)
{
	if(len==1) return s1;
	return (((long long)s1*sum[len-2]+1)%mod+((long long)s2*sum[len-1])%mod)%mod;
}
int getk(int s1,int s2,int k)
{
	if(k==1) return s1;
	return ((long long)s1*fib[k-2]%mod+(long long)s2*fib[k-1]%mod)%mod;
}
void pushdown(int rt,int l,int r)
{
	if(p[rt].s1==0&&p[rt].s2==0) return ;
	int mid=(l+r)/2;
	p[rt<<1].s1=(p[rt<<1].s1+p[rt].s1)%mod;
	p[rt<<1].s2=(p[rt<<1].s2+p[rt].s2)%mod;
	p[rt<<1].sum=(p[rt<<1].sum+get(p[rt].s1,p[rt].s2,mid-l+1))%mod;
	p[rt<<1|1].s1=(p[rt<<1|1].s1+getk(p[rt].s1,p[rt].s2,mid-l+2))%mod;
	p[rt<<1|1].s2=(p[rt<<1|1].s2+getk(p[rt].s1,p[rt].s2,mid-l+3))%mod;
	p[rt<<1|1].sum=((p[rt<<1|1].sum+get(p[rt].s1,p[rt].s2,r-l+1))%mod+mod-get(p[rt].s1,p[rt].s2,mid-l+1))%mod;
	p[rt].s1=p[rt].s2=0;
}
void pushup(int rt)
{
	p[rt].sum=(p[rt<<1].sum+p[rt<<1|1].sum)%mod;
}
void update(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R)
	{
		p[rt].s1=(p[rt].s1+fib[l-L+1])%mod;
		p[rt].s2=(p[rt].s2+fib[l-L+2])%mod;
		p[rt].sum=(p[rt].sum+(sum[r-L+1]-sum[l-L])%mod)%mod;
		return ;
	}
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	if(L<=mid) update(lson,L,R);
	if(R>=mid+1) update(rson,L,R);
	pushup(rt);
}
int query(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R) return p[rt].sum;
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	int ret=0;
	if(L<=mid) ret=(ret+query(lson,L,R))%mod;
	if(R>=mid+1) ret=(ret+query(rson,L,R))%mod;
	return ret;
}
int main()
{
//	freopen("1.txt","r",stdin);
	pre();
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) a[i]=(a[i-1]+a[i])%mod;
	for(int i=1;i<=m;i++)
	{
		int opt,l,r;
		scanf("%d%d%d",&opt,&l,&r);
		if(opt==1)
		{
			update(1,1,n,l,r);
		}
		if(opt==2)
		{
			printf("%d\n",(query(1,1,n,l,r)+(a[r]-a[l-1])%mod)%mod);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
const int mod=1e9+7;
const int maxn=3e5+5;
int n,m;
int a[maxn],fib[maxn],sum[maxn];
void pre()
{
	fib[1]=fib[2]=1;
	for(int i=3;i<maxn;i++) fib[i]=(fib[i-1]+fib[i-2])%mod;
	for(int i=1;i<maxn;i++) sum[i]=(sum[i-1]+fib[i])%mod;
}
struct point
{
	int left,right,sum;
}p[maxn<<2];
void pushdown(int rt,int l,int r)
{
	if(p[rt].left==0) return ;
	if(l==r) return ;
	int mid=(l+r)/2;
	pushdown(lson);
	pushdown(rson);
	int fibmid=(p[rt].left+p[rt].right)/2;
	p[rt<<1].left=p[rt].left;
	p[rt<<1].right=fibmid;
	p[rt<<1].sum=(p[rt<<1].sum+(sum[p[rt<<1].right]-sum[p[rt<<1].left-1])%mod)%mod;
	p[rt<<1|1].left=fibmid+1;
	p[rt<<1|1].right=p[rt].right;
	p[rt<<1|1].sum=(p[rt<<1|1].sum+(sum[p[rt<<1|1].right]-sum[p[rt<<1|1].left-1])%mod)%mod;
	p[rt].left=p[rt].right=0;
}
void pushup(int rt)
{
	p[rt].sum=(p[rt<<1].sum+p[rt<<1|1].sum)%mod;
}
void build(int rt,int l,int r)
{
	p[rt].left=p[rt].right=0;
	if(l==r) {p[rt].sum=a[l];return ;}
	int mid=(l+r)/2;
	build(lson);
	build(rson);
	pushup(rt);
}
void update(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R)
	{
		pushdown(rt,l,r);
		p[rt].left=l-L+1;
		p[rt].right=r-L+1;
		p[rt].sum=(p[rt].sum+(sum[p[rt].right]-sum[p[rt].left-1])%mod)%mod;
		return ;
	}
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	if(L<=mid) update(lson,L,R);
	if(R>=mid+1) update(rson,L,R);
	pushup(rt);
}
int query(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R) return p[rt].sum;
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	int ret=0;
	if(L<=mid) ret=(ret+query(lson,L,R))%mod;
	if(R>=mid+1) ret=(ret+query(rson,L,R))%mod;
	return (ret+mod)%mod;
}
int main()
{
	pre();
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	build(1,1,n);
	for(int i=1;i<=m;i++)
	{
		int opt,l,r;
		scanf("%d%d%d",&opt,&l,&r);
		if(opt==1)
		{
			update(1,1,n,l,r);
		}
		if(opt==2)
		{
			printf("%d\n",query(1,1,n,l,r));
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
const int mod=1e9+9;
const int maxn=3e5+5;
int n,m;
int a[maxn],fib[maxn],sum[maxn];
void pre()
{
	fib[1]=fib[2]=1;
	for(int i=3;i<maxn;i++) fib[i]=(fib[i-1]+fib[i-2])%mod;
	for(int i=1;i<maxn;i++) sum[i]=(sum[i-1]+fib[i])%mod;
}
struct point
{
	int s1,s2,sum;
}p[maxn<<2];
int get(int s1,int s2,int len)
{
	if(len==1) return s1;
	return (((long long)s1*(sum[len-2]+1))%mod+((long long)s2*sum[len-1])%mod)%mod;
}
int getk(int s1,int s2,int k)
{
	if(k==1) return s1;
	return ((long long)s1*fib[k-2]%mod+(long long)s2*fib[k-1]%mod)%mod;
}
void pushdown(int rt,int l,int r)
{
	if(p[rt].s1==0&&p[rt].s2==0) return ;
	int mid=(l+r)/2;
	p[rt<<1].s1=(p[rt<<1].s1+p[rt].s1)%mod;
	p[rt<<1].s2=(p[rt<<1].s2+p[rt].s2)%mod;
	p[rt<<1].sum=(p[rt<<1].sum+get(p[rt].s1,p[rt].s2,mid-l+1))%mod;
	p[rt<<1|1].s1=(p[rt<<1|1].s1+getk(p[rt].s1,p[rt].s2,mid-l+2))%mod;
	p[rt<<1|1].s2=(p[rt<<1|1].s2+getk(p[rt].s1,p[rt].s2,mid-l+3))%mod;
	p[rt<<1|1].sum=((p[rt<<1|1].sum+get(p[rt].s1,p[rt].s2,r-l+1))%mod+mod-get(p[rt].s1,p[rt].s2,mid-l+1))%mod;
	p[rt].s1=p[rt].s2=0;
}
void pushup(int rt)
{
	p[rt].sum=(p[rt<<1].sum+p[rt<<1|1].sum)%mod;
}
void update(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R)
	{
		p[rt].s1=(p[rt].s1+fib[l-L+1])%mod;
		p[rt].s2=(p[rt].s2+fib[l-L+2])%mod;
		p[rt].sum=(p[rt].sum+(sum[r-L+1]-sum[l-L])%mod)%mod;
		return ;
	}
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	if(L<=mid) update(lson,L,R);
	if(R>=mid+1) update(rson,L,R);
	pushup(rt);
}
int query(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R) return p[rt].sum;
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	int ret=0;
	if(L<=mid) ret=(ret+query(lson,L,R))%mod;
	if(R>=mid+1) ret=(ret+query(rson,L,R))%mod;
	return ret;
}
int main()
{
//	freopen("1.txt","r",stdin);
	pre();
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) a[i]=(a[i-1]+a[i])%mod;
	for(int i=1;i<=m;i++)
	{
		int opt,l,r;
		scanf("%d%d%d",&opt,&l,&r);
		if(opt==1)
		{
			update(1,1,n,l,r);
		}
		if(opt==2)
		{
			printf("%d\n",(query(1,1,n,l,r)+(a[r]-a[l-1])%mod)%mod);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
const int mod=1e9+9;
const int maxn=3e5+5;
int n,m;
int a[maxn],fib[maxn],sum[maxn];
void pre()
{
	fib[1]=fib[2]=1;
	for(int i=3;i<maxn;i++) fib[i]=(fib[i-1]+fib[i-2])%mod;
	for(int i=1;i<maxn;i++) sum[i]=(sum[i-1]+fib[i])%mod;
}
struct point
{
	int s1,s2,sum;
}p[maxn<<2];
int get(int s1,int s2,int len)
{
	if(len==1) return s1;
	return (((long long)s1*(sum[len-2]+1))%mod+((long long)s2*sum[len-1])%mod)%mod;
}
int getk(int s1,int s2,int k)
{
	if(k==1) return s1;
	return ((long long)s1*fib[k-2]%mod+(long long)s2*fib[k-1]%mod)%mod;
}
void pushdown(int rt,int l,int r)
{
	if(p[rt].s1==0&&p[rt].s2==0) return ;
	int mid=(l+r)/2;
	p[rt<<1].s1=(p[rt<<1].s1+p[rt].s1)%mod;
	p[rt<<1].s2=(p[rt<<1].s2+p[rt].s2)%mod;
	p[rt<<1].sum=(p[rt<<1].sum+get(p[rt].s1,p[rt].s2,mid-l+1))%mod;
	p[rt<<1|1].s1=(p[rt<<1|1].s1+getk(p[rt].s1,p[rt].s2,mid-l+2))%mod;
	p[rt<<1|1].s2=(p[rt<<1|1].s2+getk(p[rt].s1,p[rt].s2,mid-l+3))%mod;
	p[rt<<1|1].sum=((p[rt<<1|1].sum+get(p[rt].s1,p[rt].s2,r-l+1))%mod+mod-get(p[rt].s1,p[rt].s2,mid-l+1))%mod;
	p[rt].s1=p[rt].s2=0;
}
void pushup(int rt)
{
	p[rt].sum=(p[rt<<1].sum+p[rt<<1|1].sum)%mod;
}
void update(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R)
	{
		p[rt].s1=(p[rt].s1+fib[l-L+1])%mod;
		p[rt].s2=(p[rt].s2+fib[l-L+2])%mod;
		p[rt].sum=(p[rt].sum+(sum[r-L+1]-sum[l-L]+mod)%mod)%mod;
		return ;
	}
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	if(L<=mid) update(lson,L,R);
	if(R>=mid+1) update(rson,L,R);
	pushup(rt);
}
int query(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R) return p[rt].sum;
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	int ret=0;
	if(L<=mid) ret=(ret+query(lson,L,R))%mod;
	if(R>=mid+1) ret=(ret+query(rson,L,R))%mod;
	return ret;
}
int main()
{
	pre();
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) a[i]=(a[i-1]+a[i])%mod;
	for(int i=1;i<=m;i++)
	{
		int opt,l,r;
		scanf("%d%d%d",&opt,&l,&r);
		if(opt==1)
		{
			update(1,1,n,l,r);
		}
		if(opt==2)
		{
			printf("%d\n",(query(1,1,n,l,r)+(mod+a[r]-a[l-1])%mod)%mod);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
const int mod=1e9+9;
const int maxn=3e5+5;
int n,m;
int a[maxn],fib[maxn],sum[maxn];
void pre()
{
	fib[1]=fib[2]=1;
	for(int i=3;i<maxn;i++) fib[i]=(fib[i-1]+fib[i-2])%mod;
	for(int i=1;i<maxn;i++) sum[i]=(sum[i-1]+fib[i])%mod;
}
struct point
{
	int s1,s2,sum;
}p[maxn<<2];
int get(int s1,int s2,int len)
{
	if(len==1) return s1;
	return (((long long)s1*(sum[len-2]+1))%mod+((long long)s2*sum[len-1])%mod)%mod;
}
int getk(int s1,int s2,int k)
{
	if(k==1) return s1;
	return ((long long)s1*fib[k-2]%mod+(long long)s2*fib[k-1]%mod)%mod;
}
void pushdown(int rt,int l,int r)
{
	if(p[rt].s1==0&&p[rt].s2==0) return ;
	int mid=(l+r)/2;
	p[rt<<1].s1=(p[rt<<1].s1+p[rt].s1)%mod;
	p[rt<<1].s2=(p[rt<<1].s2+p[rt].s2)%mod;
	p[rt<<1].sum=(p[rt<<1].sum+get(p[rt].s1,p[rt].s2,mid-l+1))%mod;
	p[rt<<1|1].s1=(p[rt<<1|1].s1+getk(p[rt].s1,p[rt].s2,mid-l+2))%mod;
	p[rt<<1|1].s2=(p[rt<<1|1].s2+getk(p[rt].s1,p[rt].s2,mid-l+3))%mod;
	p[rt<<1|1].sum=((p[rt<<1|1].sum+get(p[rt].s1,p[rt].s2,r-l+1))%mod+mod-get(p[rt].s1,p[rt].s2,mid-l+1))%mod;
	p[rt].s1=p[rt].s2=0;
}
void pushup(int rt)
{
	p[rt].sum=(p[rt<<1].sum+p[rt<<1|1].sum)%mod;
}
void update(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R)
	{
		p[rt].s1=(p[rt].s1+fib[l-L+1])%mod;
		p[rt].s2=(p[rt].s2+fib[l-L+2])%mod;
		p[rt].sum=(p[rt].sum+(sum[r-L+1]-sum[l-L])%mod)%mod;
		return ;
	}
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	if(L<=mid) update(lson,L,R);
	if(R>=mid+1) update(rson,L,R);
	pushup(rt);
}
int query(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R) return p[rt].sum;
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	int ret=0;
	if(L<=mid) ret=(ret+query(lson,L,R))%mod;
	if(R>=mid+1) ret=(ret+query(rson,L,R))%mod;
	return (ret+mod)%mod;
}
int main()
{
//	freopen("1.txt","r",stdin);
	pre();
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) a[i]=(a[i-1]+a[i])%mod;
	for(int i=1;i<=m;i++)
	{
		int opt,l,r;
		scanf("%d%d%d",&opt,&l,&r);
		if(opt==1)
		{
			update(1,1,n,l,r);
		}
		if(opt==2)
		{
			printf("%d\n",(query(1,1,n,l,r)+((a[r]-a[l-1])%mod+mod)%mod)%mod);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
const int mod=1e9+9;
const int maxn=3e5+5;
int n,m;
int a[maxn],fib[maxn],sum[maxn];
void pre()
{
	fib[1]=fib[2]=1;
	for(int i=3;i<maxn;i++) fib[i]=(fib[i-1]+fib[i-2])%mod;
	for(int i=1;i<maxn;i++) sum[i]=(sum[i-1]+fib[i])%mod;
}
struct point
{
	int left,right,sum;
}p[maxn<<2];
void pushdown(int rt,int l,int r)
{
	if(p[rt].left==0) return ;
	if(l==r) return ;
	int mid=(l+r)/2;
	pushdown(lson);
	pushdown(rson);
	int fibmid=(p[rt].left+p[rt].right)/2;
	p[rt<<1].left=p[rt].left;
	p[rt<<1].right=fibmid;
	p[rt<<1].sum=(p[rt<<1].sum+(sum[p[rt<<1].right]-sum[p[rt<<1].left-1])%mod)%mod;
	p[rt<<1|1].left=fibmid+1;
	p[rt<<1|1].right=p[rt].right;
	p[rt<<1|1].sum=(p[rt<<1|1].sum+(sum[p[rt<<1|1].right]-sum[p[rt<<1|1].left-1])%mod)%mod;
	p[rt].left=p[rt].right=0;
}
void pushup(int rt)
{
	p[rt].sum=(p[rt<<1].sum+p[rt<<1|1].sum)%mod;
}
void build(int rt,int l,int r)
{
	p[rt].left=p[rt].right=0;
	if(l==r) {p[rt].sum=a[l];return ;}
	int mid=(l+r)/2;
	build(lson);
	build(rson);
	pushup(rt);
}
void update(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R)
	{
		pushdown(rt,l,r);
		p[rt].left=l-L+1;
		p[rt].right=r-L+1;
		p[rt].sum=(p[rt].sum+(sum[p[rt].right]-sum[p[rt].left-1])%mod)%mod;
		return ;
	}
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	if(L<=mid) update(lson,L,R);
	if(R>=mid+1) update(rson,L,R);
	pushup(rt);
}
int query(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R) return p[rt].sum;
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	int ret=0;
	if(L<=mid) ret=(ret+query(lson,L,R))%mod;
	if(R>=mid+1) ret=(ret+query(rson,L,R))%mod;
	return (ret+mod)%mod;
}
int main()
{
	pre();
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	build(1,1,n);
	for(int i=1;i<=m;i++)
	{
		int opt,l,r;
		scanf("%d%d%d",&opt,&l,&r);
		if(opt==1)
		{
			update(1,1,n,l,r);
		}
		if(opt==2)
		{
			printf("%d\n",query(1,1,n,l,r));
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
const int mod=1e9+9;
const int maxn=3e5+5;
int n,m;
int a[maxn],fib[maxn],sum[maxn];
void pre()
{
	fib[1]=fib[2]=1;
	for(int i=3;i<maxn;i++) fib[i]=(fib[i-1]+fib[i-2])%mod;
	for(int i=1;i<maxn;i++) sum[i]=(sum[i-1]+fib[i])%mod;
}
struct point
{
	int s1,s2,sum;
}p[maxn<<2];
int get(int s1,int s2,int len)
{
	if(len==1) return s1;
	return (((long long)s1*(sum[len-2]+1))%mod+((long long)s2*sum[len-1])%mod)%mod;
}
int getk(int s1,int s2,int k)
{
	if(k==1) return s1;
	return ((long long)s1*fib[k-2]%mod+(long long)s2*fib[k-1]%mod)%mod;
}
void pushdown(int rt,int l,int r)
{
	if(p[rt].s1==0&&p[rt].s2==0) return ;
	int mid=(l+r)/2;
	p[rt<<1].s1=(p[rt<<1].s1+p[rt].s1)%mod;
	p[rt<<1].s2=(p[rt<<1].s2+p[rt].s2)%mod;
	p[rt<<1].sum=(p[rt<<1].sum+get(p[rt].s1,p[rt].s2,mid-l+1))%mod;
	p[rt<<1|1].s1=(p[rt<<1|1].s1+getk(p[rt].s1,p[rt].s2,mid-l+2))%mod;
	p[rt<<1|1].s2=(p[rt<<1|1].s2+getk(p[rt].s1,p[rt].s2,mid-l+3))%mod;
	p[rt<<1|1].sum=((p[rt<<1|1].sum+get(p[rt].s1,p[rt].s2,r-l+1))%mod+mod-get(p[rt].s1,p[rt].s2,mid-l+1))%mod;
	p[rt].s1=p[rt].s2=0;
}
void pushup(int rt)
{
	p[rt].sum=(p[rt<<1].sum+p[rt<<1|1].sum)%mod;
}
void update(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R)
	{
		p[rt].s1=(p[rt].s1+fib[l-L+1])%mod;
		p[rt].s2=(p[rt].s2+fib[l-L+2])%mod;
		p[rt].sum=(p[rt].sum+(sum[r-L+1]-sum[l-L])%mod)%mod;
		return ;
	}
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	if(L<=mid) update(lson,L,R);
	if(R>=mid+1) update(rson,L,R);
	pushup(rt);
}
int query(int rt,int l,int r,int L,int R)
{
	if(L<=l&&r<=R) return p[rt].sum;
	pushdown(rt,l,r);
	int mid=(l+r)/2;
	int ret=0;
	if(L<=mid) ret=(ret+query(lson,L,R))%mod;
	if(R>=mid+1) ret=(ret+query(rson,L,R))%mod;
	return (ret+mod)%mod;
}
int main()
{
//	freopen("1.txt","r",stdin);
	pre();
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++) a[i]=(a[i-1]+a[i])%mod;
	for(int i=1;i<=m;i++)
	{
		int opt,l,r;
		scanf("%d%d%d",&opt,&l,&r);
		if(opt==1)
		{
			update(1,1,n,l,r);
		}
		if(opt==2)
		{
			printf("%d\n",(query(1,1,n,l,r)+(a[r]-a[l-1]+mod)%mod)%mod);
		}
	}
	return 0;
}


#include <iostream>
using namespace std;
struct mat {
    long long   a, b, c;
    mat () {}
    mat ( long long   a_, long long    b_, long long    c_ ) { a = a_; b = b_; c = c_; }
};
struct v {
    long long    val, a, b;
    v () { a = 0, b = 0, val = 0; }
}it[10000042];
long long   mod = 1000000009;

void modd ( int &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
mat mult ( mat a, mat b ) {
    mat ans;
    ans.a = (1LL*a.a*b.a + 1LL*a.b*b.b)%mod;
    ans.b =  (1LL*a.a*b.b + 1LL*a.b*b.c)%mod;
    ans.c = (1LL*a.b*b.b + 1LL*a.c*b.c)%mod;
    return ans;
}
mat pows[32];
int getFib( int a, int b, int n ) {
    if ( n == -1 ) { return 0; }
    mat crr ( a, b, a+b );
    for ( int i = 0 ; i < 32 ; i ++ ) {
        if ( n&(1<<i) ) {
            crr = mult( crr, pows[i] );
        }
    }
    return crr.a%mod;
}
void prePows ( ) {
    pows[0] = mat ( 0, 1, 1 );
    for ( int i = 1 ; i < 32 ; i ++ ) {
        pows[i] = mult( pows[i-1], pows[i-1] );
    }
}
int n, m, k = 19;
void handle (int ind, int s ) {
    if ( it[ind].a == 0 && it[ind].b == 0 ) { return; }
    if ( ind >= (1<<k) ) {
        it[ind].val += it[ind].a;
        modd( it[ind].val );
        it[ind].a = 0; it[ind].b = 0;
    }

    it[ind].val += getFib( it[ind].a, it[ind].b, s+1 ) - getFib( it[ind].a, it[ind].b, 1 )+mod;
    modd( it[ind].val );

    it [ind*2].a += getFib( it[ind].a, it[ind].b, 0 );
    modd ( it[ind*2].a );
    it [ind*2].b += getFib( it[ind].a, it[ind].b, 1 );
    modd ( it[ind*2].b );

    it[ind*2+1].a += getFib( it[ind].a, it[ind].b, s/2 );
    modd ( it[ind*2+1].a );
    it[ind*2+1].b += getFib( it[ind].a, it[ind].b, s/2+1 );
    modd ( it[ind*2+1].b );

    it[ind].a = 0;
    it[ind].b = 0;
}
int stL;
void add ( int s, int f, int ss, int ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return ; }
    if ( ss <= s && f <= ff ) {
        it[ind].a += getFib( 1, 1, s-stL );
        modd ( it[ind].a );
        it[ind].b += getFib( 1, 1, s-stL+1 );
        modd ( it[ind].b );
        handle( ind, f-s );
        return;
    }
    add( s, (s+f)/2, ss, ff, ind*2 );
    add( (s+f)/2, f, ss, ff, ind*2+1 );
    it[ind].val = it[ind*2].val + it[ind*2+1].val;
    modd ( it[ind].val );
    return ;
}

long long sum ( int s, int f, int ss, int  ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return 0; }
    if ( ss <= s && f <= ff ) { return it[ind].val; }
    return ( sum ( s, (s+f)/2, ss, ff, ind*2 )+sum( (s+f)/2, f, ss, ff, ind*2+1 ) )%mod;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    prePows () ;

    cin >> n >> m;
    for ( int  i = 0 ; i < n ; i ++ ) {
        cin >> it[i+(1<<k)].val;
    }
    for ( int  i = (1<<k)-1 ; i ; i-- ) {
        it[i].val = it[i*2].val + it[i*2+1].val;
        it[i].val %= mod;
    }
    for ( int i = 0 ; i < m ; i ++ ) {
        int t, l ,r;
        cin >> t >> l >> r;
        l --;
        if ( t == 1 ) {
            stL = l;
            add ( 0, (1<<k), l, r, 1 );
        }else {
            cout << sum ( 0, (1<<k), l, r, 1 ) << "\n";
        }
    }
    return 0;
}
/*

a b     a+b   a+2b     2a+3b  3a+5b   5a+8b
a a+b  2a+b   3a+3b    5a+6b  8a+11b 13a+19b

0 1   1     2    3      5     8     13     21     55
0 1   2     4    7     12    20     33     54

3    4     7    11    18     29
3    7     14   25

*/


#include <iostream>
using namespace std;
struct mat {
    int a, b, c;
    mat () {}
    mat ( int a_, int b_, int c_ ) { a = a_; b = b_; c = c_; }
};
struct v {
    int val;
    int a, b;
    v () { a = 0, b = 0, val = 0; }
}it[10000042];
int mod = 1000000009;
void modd ( int &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
mat mult ( mat a, mat b ) {
    mat ans;
    ans.a = (1LL*a.a*b.a + 1LL*a.b*b.b)%mod;
    ans.b =  (1LL*a.a*b.b + 1LL*a.b*b.c)%mod;
    ans.c = (1LL*a.b*b.b + 1LL*a.c*b.c)%mod;
    return ans;
}

mat pows[10000042];
void prePows ( ) {
    pows[0] = mat ( 0, 1, 1 );
    for ( int i = 1 ; i < 10000042 ; i ++ ) {
        pows[i] = mult( pows[i-1], pows[0] );
    }
}

int getFib( int a, int b, int n ) {
    if ( n == -1 ) { return 0; }
    mat crr ( a, b, a+b );
    crr = mult( crr, pows[n] );
    return crr.a;

    for ( int i = 0 ; i < 32 ; i ++ ) {
        if ( n&(1<<i) ) {
            crr = mult( crr, pows[i] );
        }
        if ( (1<<i) >= n ) { return crr.a; }
    }
    return crr.a;
}

int n, m, k = 19;
void handle (int ind, int s ) {
    if ( it[ind].a == 0 && it[ind].b == 0 ) { return; }
    if ( ind >= (1<<k) ) {
        it[ind].val += it[ind].a;
        modd( it[ind].val );
        it[ind].a = 0; it[ind].b = 0;
    }

    it[ind].val += (1LL*getFib( it[ind].a, it[ind].b, s+1 ) - 1LL*it[ind].b );
    modd( it[ind].val );

    it [ind*2].a += it[ind].a;
    modd ( it[ind*2].a );
    it [ind*2].b += it[ind].b;
    modd ( it[ind*2].b );

    it[ind*2+1].a += getFib( it[ind].a, it[ind].b, s/2 );
    modd ( it[ind*2+1].a );
    it[ind*2+1].b += getFib( it[ind].a, it[ind].b, s/2+1 );
    modd ( it[ind*2+1].b );

    it[ind].a = 0;
    it[ind].b = 0;
}
int stL;
void add ( int s, int f, int ss, int ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return ; }
    if ( ss <= s && f <= ff ) {
        it[ind].a += getFib( 1, 1, s-stL );
        it[ind].b += getFib( 1, 1, s-stL+1 );
        handle( ind, f-s );
        return;
    }
    add( s, (s+f)/2, ss, ff, ind*2 );
    add( (s+f)/2, f, ss, ff, ind*2+1 );
    it[ind].val = (it[ind*2].val + it[ind*2+1].val);
    modd ( it[ind].val );
    return ;
}
int dd ( int x ) {
    if ( x >= mod ) { x -= mod; }
    return x;
}
int sum ( int s, int f, int ss, int  ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return 0; }
    if ( ss <= s && f <= ff ) { return it[ind].val; }
    return dd( sum ( s, (s+f)/2, ss, ff, ind*2 )+sum( (s+f)/2, f, ss, ff, ind*2+1 ) );
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    prePows () ;

    cin >> n >> m;
    for ( int  i = 0 ; i < n ; i ++ ) {
        cin >> it[i+(1<<k)].val;
    }
    for ( int  i = (1<<k)-1 ; i ; i-- ) {
        it[i].val = it[i*2].val + it[i*2+1].val;
        modd( it[i].val );
    }
    for ( int i = 0 ; i < m ; i ++ ) {
        int t, l ,r;
        cin >> t >> l >> r;
        l --;
        if ( t == 1 ) {
            stL = l;
            add ( 0, (1<<k), l, r, 1 );
        }else {
            cout << sum ( 0, (1<<k), l, r, 1 ) << "\n";
        }
    }
    return 0;
}


#include <iostream>
using namespace std;
struct mat {
    int a, b, c;
    mat () {}
    mat ( int a_, int b_, int c_ ) { a = a_; b = b_; c = c_; }
};
struct v {
    int val;
    int a, b;
    v () { a = 0, b = 0, val = 0; }
}it[10000042];
long long   mod = 1000000009;

void modd ( long long &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
void modd ( int &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
mat mult ( mat a, mat b ) {
    mat ans;
    ans.a = (1LL*a.a*b.a + 1LL*a.b*b.b)%mod;
    ans.b =  (1LL*a.a*b.b + 1LL*a.b*b.c)%mod;
    ans.c = (1LL*a.b*b.b + 1LL*a.c*b.c)%mod;
    return ans;
}
mat pows[32];
int getFib( int a, int b, int n ) {
    if ( n == -1 ) { return 0; }
    mat crr ( a, b, a+b );
    for ( int i = 0 ; i < 32 ; i ++ ) {
        if ( n&(1<<i) ) {
            crr = mult( crr, pows[i] );
        }
        if ( (1<<i) >= n ) { return crr.a; }
    }
    return crr.a;
}
void prePows ( ) {
    pows[0] = mat ( 0, 1, 1 );
    for ( int i = 1 ; i < 32 ; i ++ ) {
        pows[i] = mult( pows[i-1], pows[i-1] );
    }
}
int n, m, k = 19;
void handle (int ind, int s ) {
    if ( it[ind].a == 0 && it[ind].b == 0 ) { return; }
    if ( ind >= (1<<k) ) {
        it[ind].val += it[ind].a;
        modd( it[ind].val );
        it[ind].a = 0; it[ind].b = 0;
    }

    it[ind].val += (1LL*getFib( it[ind].a, it[ind].b, s+1 ) - 1LL*it[ind].b );
    modd( it[ind].val );

    it [ind*2].a += it[ind].a;
    modd ( it[ind*2].a );
    it [ind*2].b += it[ind].b;
    modd ( it[ind*2].b );

    it[ind*2+1].a += getFib( it[ind].a, it[ind].b, s/2 );
    modd ( it[ind*2+1].a );
    it[ind*2+1].b += getFib( it[ind].a, it[ind].b, s/2+1 );
    modd ( it[ind*2+1].b );

    it[ind].a = 0;
    it[ind].b = 0;
}
int stL;
void add ( int s, int f, int ss, int ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return ; }
    if ( ss <= s && f <= ff ) {
        it[ind].a += getFib( 1, 1, s-stL );
        it[ind].b += getFib( 1, 1, s-stL+1 );
        handle( ind, f-s );
        return;
    }
    add( s, (s+f)/2, ss, ff, ind*2 );
    add( (s+f)/2, f, ss, ff, ind*2+1 );
    it[ind].val = (it[ind*2].val + it[ind*2+1].val);
    modd ( it[ind].val );
    return ;
}

long long sum ( int s, int f, int ss, int  ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return 0; }
    if ( ss <= s && f <= ff ) { return it[ind].val; }
    return ( sum ( s, (s+f)/2, ss, ff, ind*2 )+sum( (s+f)/2, f, ss, ff, ind*2+1 ) )%mod;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    prePows () ;

    cin >> n >> m;
    for ( int  i = 0 ; i < n ; i ++ ) {
        cin >> it[i+(1<<k)].val;
    }
    for ( int  i = (1<<k)-1 ; i ; i-- ) {
        it[i].val = it[i*2].val + it[i*2+1].val;
        it[i].val %= mod;
    }
    for ( int i = 0 ; i < m ; i ++ ) {
        int t, l ,r;
        cin >> t >> l >> r;
        l --;
        if ( t == 1 ) {
            stL = l;
            add ( 0, (1<<k), l, r, 1 );
        }else {
            cout << sum ( 0, (1<<k), l, r, 1 ) << "\n";
        }
    }
    return 0;
}
/*

a b     a+b   a+2b     2a+3b  3a+5b   5a+8b
a a+b  2a+b   3a+3b    5a+6b  8a+11b 13a+19b

0 1   1     2    3      5     8     13     21     55
0 1   2     4    7     12    20     33     54

3    4     7    11    18     29
3    7     14   25

*/


#include <iostream>
using namespace std;
struct mat {
    int   a, b, c;
    mat () {}
    mat ( int   a_, int   b_, int   c_ ) { a = a_; b = b_; c = c_; }
};
struct v {
    int   val, a, b;
    v () { a = 0, b = 0, val = 0; }
}it[10000042];
int  mod = 1000000009;

void modd ( int  &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
mat mult ( mat a, mat b ) {
    mat ans;
    ans.a = (1LL*a.a*b.a + 1LL*a.b*b.b)%mod;
    ans.b =  (1LL*a.a*b.b + 1LL*a.b*b.c)%mod;
    ans.c = (1LL*a.b*b.b + 1LL*a.c*b.c)%mod;
    return ans;
}
mat pows[32];
int   getFib( int   a, int   b, int   n ) {
    if ( n == -1 ) { return 0; }
    mat crr ( a, b, a+b );
    for ( int   i = 0 ; i < 32 ; i ++ ) {
        if ( n&(1<<i) ) {
            crr = mult( crr, pows[i] );
        }
    }
    return crr.a;
}
void prePows ( ) {
    pows[0] = mat ( 0, 1, 1 );
    for ( int   i = 1 ; i < 32 ; i ++ ) {
        pows[i] = mult( pows[i-1], pows[i-1] );
    }
}
int   n, m, k = 19;
void handle (int   ind, int   s ) {
    if ( it[ind].a == 0 && it[ind].b == 0 ) { return; }
    if ( ind >= (1<<k) ) {
        it[ind].val += it[ind].a;
        modd( it[ind].val );
        it[ind].a = 0; it[ind].b = 0;
    }

    it[ind].val += getFib( it[ind].a, it[ind].b, s+1 ) - getFib( it[ind].a, it[ind].b, 1 )+mod;
    modd( it[ind].val );

    it [ind*2].a += getFib( it[ind].a, it[ind].b, 0 );
    modd ( it[ind*2].a );
    it [ind*2].b += getFib( it[ind].a, it[ind].b, 1 );
    modd ( it[ind*2].b );

    it[ind*2+1].a += getFib( it[ind].a, it[ind].b, s/2 );
    modd ( it[ind*2+1].a );
    it[ind*2+1].b += getFib( it[ind].a, it[ind].b, s/2+1 );
    modd ( it[ind*2+1].b );

    it[ind].a = 0;
    it[ind].b = 0;
}
int   stL;
void add ( int   s, int   f, int   ss, int   ff, int   ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return ; }
    if ( ss <= s && f <= ff ) {
        it[ind].a += getFib( 1, 1, s-stL );
        modd ( it[ind].a );
        it[ind].b += getFib( 1, 1, s-stL+1 );
        modd ( it[ind].b );
        handle( ind, f-s );
        return;
    }
    add( s, (s+f)/2, ss, ff, ind*2 );
    add( (s+f)/2, f, ss, ff, ind*2+1 );
    it[ind].val = it[ind*2].val + it[ind*2+1].val;
    modd ( it[ind].val );
    return ;
}
int  dd ( int  x ) {
    if ( x >= mod ) {x -= mod; }
    return x;
}
int   sum ( int   s, int f, int ss, int   ff, int   ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return 0; }
    if ( ss <= s && f <= ff ) { return it[ind].val; }
    return dd( sum ( s, (s+f)/2, ss, ff, ind*2 )+sum( (s+f)/2, f, ss, ff, ind*2+1 ) );
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    prePows () ;

    cin >> n >> m;
    for ( int   i = 0 ; i < n ; i ++ ) {
        cin >> it[i+(1<<k)].val;
    }
    for ( int   i = (1<<k)-1 ; i ; i-- ) {
        it[i].val = it[i*2].val + it[i*2+1].val;
    }
    for ( int   i = 0 ; i < m ; i ++ ) {
        int   t, l ,r;
        cin >> t >> l >> r;
        l --;
        if ( t == 1 ) {
            stL = l;
            add ( 0, (1<<k), l, r, 1 );
        }else {
            cout << sum ( 0, (1<<k), l, r, 1 ) << "\n";
        }
    }
    return 0;
}
/*

a b     a+b   a+2b     2a+3b  3a+5b   5a+8b
a a+b  2a+b   3a+3b    5a+6b  8a+11b 13a+19b

0 1   1     2    3      5     8     13     21     55
0 1   2     4    7     12    20     33     54

3    4     7    11    18     29
3    7     14   25

*/


#include <iostream>
using namespace std;
struct mat {
    int a, b, c;
    mat () {}
    mat ( int a_, int b_, int c_ ) { a = a_; b = b_; c = c_; }
};
struct v {
    int val;
    int a, b;
    v () { a = 0, b = 0, val = 0; }
}it[10000042];
long long   mod = 1000000009;

void modd ( long long &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
void modd ( int &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
mat mult ( mat a, mat b ) {
    mat ans;
    ans.a = (1LL*a.a*b.a + 1LL*a.b*b.b)%mod;
    ans.b =  (1LL*a.a*b.b + 1LL*a.b*b.c)%mod;
    ans.c = (1LL*a.b*b.b + 1LL*a.c*b.c)%mod;
    return ans;
}
mat pows[32];
int getFib( int a, int b, int n ) {
    if ( n == -1 ) { return 0; }
    mat crr ( a, b, a+b );
    for ( int i = 0 ; i < 32 ; i ++ ) {
        if ( n&(1<<i) ) {
            crr = mult( crr, pows[i] );
        }
        if ( (1<<i) >= n ) { return crr.a; }
    }
    return crr.a;
}
void prePows ( ) {
    pows[0] = mat ( 0, 1, 1 );
    for ( int i = 1 ; i < 32 ; i ++ ) {
        pows[i] = mult( pows[i-1], pows[i-1] );
    }
}
int n, m, k = 19;
void handle (int ind, int s ) {
    if ( it[ind].a == 0 && it[ind].b == 0 ) { return; }
    if ( ind >= (1<<k) ) {
        it[ind].val += it[ind].a;
        modd( it[ind].val );
        it[ind].a = 0; it[ind].b = 0;
    }

    it[ind].val += (1LL*getFib( it[ind].a, it[ind].b, s+1 ) - 1LL*it[ind].b );
    modd( it[ind].val );

    it [ind*2].a += it[ind].a;
    modd ( it[ind*2].a );
    it [ind*2].b += it[ind].b;
    modd ( it[ind*2].b );

    it[ind*2+1].a += getFib( it[ind].a, it[ind].b, s/2 );
    modd ( it[ind*2+1].a );
    it[ind*2+1].b += getFib( it[ind].a, it[ind].b, s/2+1 );
    modd ( it[ind*2+1].b );

    it[ind].a = 0;
    it[ind].b = 0;
}
int stL;
void add ( int s, int f, int ss, int ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return ; }
    if ( ss <= s && f <= ff ) {
        it[ind].a += getFib( 1, 1, s-stL );
        it[ind].b += getFib( 1, 1, s-stL+1 );
        handle( ind, f-s );
        return;
    }
    add( s, (s+f)/2, ss, ff, ind*2 );
    add( (s+f)/2, f, ss, ff, ind*2+1 );
    it[ind].val = (it[ind*2].val + it[ind*2+1].val);
    modd ( it[ind].val );
    return ;
}

long long sum ( int s, int f, int ss, int  ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return 0; }
    if ( ss <= s && f <= ff ) { return it[ind].val; }
    return ( sum ( s, (s+f)/2, ss, ff, ind*2 )+sum( (s+f)/2, f, ss, ff, ind*2+1 ) )%mod;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    prePows () ;

    cin >> n >> m;
    for ( int  i = 0 ; i < n ; i ++ ) {
        cin >> it[i+(1<<k)].val;
    }
    for ( int  i = (1<<k)-1 ; i ; i-- ) {
        it[i].val = it[i*2].val + it[i*2+1].val;
        modd( it[i].val );
    }
    for ( int i = 0 ; i < m ; i ++ ) {
        int t, l ,r;
        cin >> t >> l >> r;
        l --;
        if ( t == 1 ) {
            stL = l;
            add ( 0, (1<<k), l, r, 1 );
        }else {
            cout << sum ( 0, (1<<k), l, r, 1 ) << "\n";
        }
    }
    return 0;
}
/*

a b     a+b   a+2b     2a+3b  3a+5b   5a+8b
a a+b  2a+b   3a+3b    5a+6b  8a+11b 13a+19b

0 1   1     2    3      5     8     13     21     55
0 1   2     4    7     12    20     33     54

3    4     7    11    18     29
3    7     14   25

*/


#include <iostream>
using namespace std;
struct mat {
    int   a, b, c;
    mat () {}
    mat ( int   a_, int   b_, int   c_ ) { a = a_; b = b_; c = c_; }
};
struct v {
    int   val, a, b;
    v () { a = 0, b = 0, val = 0; }
}it[10000042];
int  mod = 1000000009;

void modd ( int  &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
}
mat mult ( mat a, mat b ) {
    mat ans;
    ans.a = (1LL*a.a*b.a + 1LL*a.b*b.b)%mod;
    ans.b =  (1LL*a.a*b.b + 1LL*a.b*b.c)%mod;
    ans.c = (1LL*a.b*b.b + 1LL*a.c*b.c)%mod;
    return ans;
}
mat pows[32];
int   getFib( int   a, int   b, int   n ) {
    if ( n == -1 ) { return 0; }
    mat crr ( a, b, a+b );
    for ( int   i = 0 ; i < 32 ; i ++ ) {
        if ( n&(1<<i) ) {
            crr = mult( crr, pows[i] );
        }
    }
    return crr.a;
}
void prePows ( ) {
    pows[0] = mat ( 0, 1, 1 );
    for ( int   i = 1 ; i < 32 ; i ++ ) {
        pows[i] = mult( pows[i-1], pows[i-1] );
    }
}
int   n, m, k = 19;
void handle (int   ind, int   s ) {
    if ( it[ind].a == 0 && it[ind].b == 0 ) { return; }
    if ( ind >= (1<<k) ) {
        it[ind].val += it[ind].a;
        modd( it[ind].val );
        it[ind].a = 0; it[ind].b = 0;
    }

    it[ind].val += getFib( it[ind].a, it[ind].b, s+1 ) - getFib( it[ind].a, it[ind].b, 1 );
    modd( it[ind].val );

    it [ind*2].a += getFib( it[ind].a, it[ind].b, 0 );
    modd ( it[ind*2].a );
    it [ind*2].b += getFib( it[ind].a, it[ind].b, 1 );
    modd ( it[ind*2].b );

    it[ind*2+1].a += getFib( it[ind].a, it[ind].b, s/2 );
    modd ( it[ind*2+1].a );
    it[ind*2+1].b += getFib( it[ind].a, it[ind].b, s/2+1 );
    modd ( it[ind*2+1].b );

    it[ind].a = 0;
    it[ind].b = 0;
}
int   stL;
void add ( int   s, int   f, int   ss, int   ff, int   ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return ; }
    if ( ss <= s && f <= ff ) {
        it[ind].a += getFib( 1, 1, s-stL );
        modd ( it[ind].a );
        it[ind].b += getFib( 1, 1, s-stL+1 );
        modd ( it[ind].b );
        handle( ind, f-s );
        return;
    }
    add( s, (s+f)/2, ss, ff, ind*2 );
    add( (s+f)/2, f, ss, ff, ind*2+1 );
    it[ind].val = it[ind*2].val + it[ind*2+1].val;
    modd ( it[ind].val );
    return ;
}
int  dd ( int  x ) {
    if ( x >= mod ) {x -= mod; }
    return x;
}
int   sum ( int   s, int f, int ss, int   ff, int   ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return 0; }
    if ( ss <= s && f <= ff ) { return it[ind].val; }
    return dd( sum ( s, (s+f)/2, ss, ff, ind*2 )+sum( (s+f)/2, f, ss, ff, ind*2+1 ) );
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    prePows () ;

    cin >> n >> m;
    for ( int   i = 0 ; i < n ; i ++ ) {
        cin >> it[i+(1<<k)].val;
    }
    for ( int   i = (1<<k)-1 ; i ; i-- ) {
        it[i].val = it[i*2].val + it[i*2+1].val;
    }
    for ( int   i = 0 ; i < m ; i ++ ) {
        int   t, l ,r;
        cin >> t >> l >> r;
        l --;
        if ( t == 1 ) {
            stL = l;
            add ( 0, (1<<k), l, r, 1 );
        }else {
            cout << sum ( 0, (1<<k), l, r, 1 ) << "\n";
        }
    }
    return 0;
}
/*

a b     a+b   a+2b     2a+3b  3a+5b   5a+8b
a a+b  2a+b   3a+3b    5a+6b  8a+11b 13a+19b

0 1   1     2    3      5     8     13     21     55
0 1   2     4    7     12    20     33     54

3    4     7    11    18     29
3    7     14   25

*/


#include <iostream>
using namespace std;
struct mat {
    long long a, b, c;
    mat () {}
    mat ( int a_, int b_, int c_ ) { a = a_; b = b_; c = c_; }
};
struct v {
    long long val;
    int a, b;
    v () { a = 0, b = 0, val = 0; }
}it[10000042];
long long   mod = 1000000009;

void modd ( long long &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
void modd ( int &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
mat mult ( mat a, mat b ) {
    mat ans;
    ans.a = (1LL*a.a*b.a + 1LL*a.b*b.b)%mod;
    ans.b =  (1LL*a.a*b.b + 1LL*a.b*b.c)%mod;
    ans.c = (1LL*a.b*b.b + 1LL*a.c*b.c)%mod;
    return ans;
}
mat pows[32];
int getFib( int a, int b, int n ) {
    if ( n == -1 ) { return 0; }
    mat crr ( a, b, a+b );
    for ( int i = 0 ; i < 32 ; i ++ ) {
        if ( n&(1<<i) ) {
            crr = mult( crr, pows[i] );
        }
    }
    return crr.a%mod;
}
void prePows ( ) {
    pows[0] = mat ( 0, 1, 1 );
    for ( int i = 1 ; i < 32 ; i ++ ) {
        pows[i] = mult( pows[i-1], pows[i-1] );
    }
}
int n, m, k = 19;
void handle (int ind, int s ) {
    if ( it[ind].a == 0 && it[ind].b == 0 ) { return; }
    if ( ind >= (1<<k) ) {
        it[ind].val += it[ind].a;
        modd( it[ind].val );
        it[ind].a = 0; it[ind].b = 0;
    }

    it[ind].val += getFib( it[ind].a, it[ind].b, s+1 ) - getFib( it[ind].a, it[ind].b, 1 )+mod;
    modd( it[ind].val );

    it [ind*2].a += getFib( it[ind].a, it[ind].b, 0 );
    modd ( it[ind*2].a );
    it [ind*2].b += getFib( it[ind].a, it[ind].b, 1 );
    modd ( it[ind*2].b );

    it[ind*2+1].a += getFib( it[ind].a, it[ind].b, s/2 );
    modd ( it[ind*2+1].a );
    it[ind*2+1].b += getFib( it[ind].a, it[ind].b, s/2+1 );
    modd ( it[ind*2+1].b );

    it[ind].a = 0;
    it[ind].b = 0;
}
int stL;
void add ( int s, int f, int ss, int ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return ; }
    if ( ss <= s && f <= ff ) {
        it[ind].a += getFib( 1, 1, s-stL );
        modd ( it[ind].a );
        it[ind].b += getFib( 1, 1, s-stL+1 );
        modd ( it[ind].b );
        handle( ind, f-s );
        return;
    }
    add( s, (s+f)/2, ss, ff, ind*2 );
    add( (s+f)/2, f, ss, ff, ind*2+1 );
    it[ind].val = it[ind*2].val + it[ind*2+1].val;
    modd ( it[ind].val );
    return ;
}

long long sum ( int s, int f, int ss, int  ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return 0; }
    if ( ss <= s && f <= ff ) { return it[ind].val; }
    return ( sum ( s, (s+f)/2, ss, ff, ind*2 )+sum( (s+f)/2, f, ss, ff, ind*2+1 ) )%mod;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    prePows () ;

    cin >> n >> m;
    for ( int  i = 0 ; i < n ; i ++ ) {
        cin >> it[i+(1<<k)].val;
    }
    for ( int  i = (1<<k)-1 ; i ; i-- ) {
        it[i].val = it[i*2].val + it[i*2+1].val;
        it[i].val %= mod;
    }
    for ( int i = 0 ; i < m ; i ++ ) {
        int t, l ,r;
        cin >> t >> l >> r;
        l --;
        if ( t == 1 ) {
            stL = l;
            add ( 0, (1<<k), l, r, 1 );
        }else {
            cout << sum ( 0, (1<<k), l, r, 1 ) << "\n";
        }
    }
    return 0;
}
/*

a b     a+b   a+2b     2a+3b  3a+5b   5a+8b
a a+b  2a+b   3a+3b    5a+6b  8a+11b 13a+19b

0 1   1     2    3      5     8     13     21     55
0 1   2     4    7     12    20     33     54

3    4     7    11    18     29
3    7     14   25

*/


#include <iostream>
using namespace std;
struct mat {
    long long   a, b, c;
    mat () {}
    mat ( int  a_, int   b_, int   c_ ) { a = a_; b = b_; c = c_; }
};
struct v {
    int   val, a, b;
    v () { a = 0, b = 0, val = 0; }
}it[10000042];
int  mod = 1000000009;

void modd ( int  &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
mat mult ( mat a, mat b ) {
    mat ans;
    ans.a = (1LL*a.a*b.a + 1LL*a.b*b.b)%mod;
    ans.b =  (1LL*a.a*b.b + 1LL*a.b*b.c)%mod;
    ans.c = (1LL*a.b*b.b + 1LL*a.c*b.c)%mod;
    return ans;
}
mat pows[32];
int   getFib( int   a, int   b, int   n ) {
    if ( n == -1 ) { return 0; }
    mat crr ( a, b, a+b );
    for ( int   i = 0 ; i < 32 ; i ++ ) {
        if ( n&(1<<i) ) {
            crr = mult( crr, pows[i] );
        }
    }
    return crr.a%mod;
}
void prePows ( ) {
    pows[0] = mat ( 0, 1, 1 );
    for ( int   i = 1 ; i < 32 ; i ++ ) {
        pows[i] = mult( pows[i-1], pows[i-1] );
    }
}
int   n, m, k = 19;
void handle (int   ind, int   s ) {
    if ( it[ind].a == 0 && it[ind].b == 0 ) { return; }
    if ( ind >= (1<<k) ) {
        it[ind].val += it[ind].a;
        modd( it[ind].val );
        it[ind].a = 0; it[ind].b = 0;
    }

    it[ind].val += getFib( it[ind].a, it[ind].b, s+1 ) - getFib( it[ind].a, it[ind].b, 1 )+mod;
    modd( it[ind].val );

    it [ind*2].a += getFib( it[ind].a, it[ind].b, 0 );
    modd ( it[ind*2].a );
    it [ind*2].b += getFib( it[ind].a, it[ind].b, 1 );
    modd ( it[ind*2].b );

    it[ind*2+1].a += getFib( it[ind].a, it[ind].b, s/2 );
    modd ( it[ind*2+1].a );
    it[ind*2+1].b += getFib( it[ind].a, it[ind].b, s/2+1 );
    modd ( it[ind*2+1].b );

    it[ind].a = 0;
    it[ind].b = 0;
}
int   stL;
void add ( int   s, int   f, int   ss, int   ff, int   ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return ; }
    if ( ss <= s && f <= ff ) {
        it[ind].a += getFib( 1, 1, s-stL );
        modd ( it[ind].a );
        it[ind].b += getFib( 1, 1, s-stL+1 );
        modd ( it[ind].b );
        handle( ind, f-s );
        return;
    }
    add( s, (s+f)/2, ss, ff, ind*2 );
    add( (s+f)/2, f, ss, ff, ind*2+1 );
    it[ind].val = it[ind*2].val + it[ind*2+1].val;
    modd ( it[ind].val );
    return ;
}

int   sum ( int   s, int f, int ss, int   ff, int   ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return 0; }
    if ( ss <= s && f <= ff ) { return it[ind].val; }
    return ( sum ( s, (s+f)/2, ss, ff, ind*2 )+sum( (s+f)/2, f, ss, ff, ind*2+1 ) )%mod;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    prePows () ;

    cin >> n >> m;
    for ( int   i = 0 ; i < n ; i ++ ) {
        cin >> it[i+(1<<k)].val;
    }
    for ( int   i = (1<<k)-1 ; i ; i-- ) {
        it[i].val = it[i*2].val + it[i*2+1].val;
        it[i].val %= mod;
    }
    for ( int   i = 0 ; i < m ; i ++ ) {
        int   t, l ,r;
        cin >> t >> l >> r;
        l --;
        if ( t == 1 ) {
            stL = l;
            add ( 0, (1<<k), l, r, 1 );
        }else {
            cout << sum ( 0, (1<<k), l, r, 1 ) << "\n";
        }
    }
    return 0;
}
/*

a b     a+b   a+2b     2a+3b  3a+5b   5a+8b
a a+b  2a+b   3a+3b    5a+6b  8a+11b 13a+19b

0 1   1     2    3      5     8     13     21     55
0 1   2     4    7     12    20     33     54

3    4     7    11    18     29
3    7     14   25

*/


#include <iostream>
using namespace std;
struct mat {
    long long   a, b, c;
    mat () {}
    mat ( long long   a_, long long    b_, long long    c_ ) { a = a_; b = b_; c = c_; }
};
struct v {
    long long    val, a, b;
    v () { a = 0, b = 0, val = 0; }
}it[10000042];
long long   mod = 1000000009;

void modd ( long long   &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
mat mult ( mat a, mat b ) {
    mat ans;
    ans.a = (1LL*a.a*b.a + 1LL*a.b*b.b)%mod;
    ans.b =  (1LL*a.a*b.b + 1LL*a.b*b.c)%mod;
    ans.c = (1LL*a.b*b.b + 1LL*a.c*b.c)%mod;
    return ans;
}
mat pows[32];
int getFib( long long a, long long b, long long n ) {
    if ( n == -1 ) { return 0; }
    mat crr ( a, b, a+b );
    for ( int i = 0 ; i < 32 ; i ++ ) {
        if ( n&(1<<i) ) {
            crr = mult( crr, pows[i] );
        }
    }
    return crr.a%mod;
}
void prePows ( ) {
    pows[0] = mat ( 0, 1, 1 );
    for ( int i = 1 ; i < 32 ; i ++ ) {
        pows[i] = mult( pows[i-1], pows[i-1] );
    }
}
int n, m, k = 19;
void handle (int ind, int s ) {
    if ( it[ind].a == 0 && it[ind].b == 0 ) { return; }
    if ( ind >= (1<<k) ) {
        it[ind].val += it[ind].a;
        modd( it[ind].val );
        it[ind].a = 0; it[ind].b = 0;
    }

    it[ind].val += getFib( it[ind].a, it[ind].b, s+1 ) - getFib( it[ind].a, it[ind].b, 1 )+mod;
    modd( it[ind].val );

    it [ind*2].a += getFib( it[ind].a, it[ind].b, 0 );
    modd ( it[ind*2].a );
    it [ind*2].b += getFib( it[ind].a, it[ind].b, 1 );
    modd ( it[ind*2].b );

    it[ind*2+1].a += getFib( it[ind].a, it[ind].b, s/2 );
    modd ( it[ind*2+1].a );
    it[ind*2+1].b += getFib( it[ind].a, it[ind].b, s/2+1 );
    modd ( it[ind*2+1].b );

    it[ind].a = 0;
    it[ind].b = 0;
}
int stL;
void add ( int s, int f, int ss, int ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return ; }
    if ( ss <= s && f <= ff ) {
        it[ind].a += getFib( 1, 1, s-stL );
        modd ( it[ind].a );
        it[ind].b += getFib( 1, 1, s-stL+1 );
        modd ( it[ind].b );
        handle( ind, f-s );
        return;
    }
    add( s, (s+f)/2, ss, ff, ind*2 );
    add( (s+f)/2, f, ss, ff, ind*2+1 );
    it[ind].val = it[ind*2].val + it[ind*2+1].val;
    modd ( it[ind].val );
    return ;
}

long long sum ( int s, int f, int ss, int  ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return 0; }
    if ( ss <= s && f <= ff ) { return it[ind].val; }
    return ( sum ( s, (s+f)/2, ss, ff, ind*2 )+sum( (s+f)/2, f, ss, ff, ind*2+1 ) )%mod;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    prePows () ;

    cin >> n >> m;
    for ( int  i = 0 ; i < n ; i ++ ) {
        cin >> it[i+(1<<k)].val;
    }
    for ( int  i = (1<<k)-1 ; i ; i-- ) {
        it[i].val = it[i*2].val + it[i*2+1].val;
        it[i].val %= mod;
    }
    for ( int i = 0 ; i < m ; i ++ ) {
        int t, l ,r;
        cin >> t >> l >> r;
        l --;
        if ( t == 1 ) {
            stL = l;
            add ( 0, (1<<k), l, r, 1 );
        }else {
            cout << sum ( 0, (1<<k), l, r, 1 ) << "\n";
        }
    }
    return 0;
}
/*

a b     a+b   a+2b     2a+3b  3a+5b   5a+8b
a a+b  2a+b   3a+3b    5a+6b  8a+11b 13a+19b

0 1   1     2    3      5     8     13     21     55
0 1   2     4    7     12    20     33     54

3    4     7    11    18     29
3    7     14   25

*/


#include <iostream>
using namespace std;
struct mat {
    int a, b, c;
    mat () {}
    mat ( int a_, int b_, int c_ ) { a = a_; b = b_; c = c_; }
};
struct v {
    int val;
    int a, b;
    v () { a = 0, b = 0, val = 0; }
}it[10000042];
int mod = 1000000009;

void modd ( int &a ) {
    if ( a < 0 ) { a += mod; }
    if ( a >= mod ) { a -= mod; }
    if ( a >= mod ) { a %= mod; }
}
mat mult ( mat a, mat b ) {
    mat ans;
    ans.a = (1LL*a.a*b.a + 1LL*a.b*b.b)%mod;
    ans.b =  (1LL*a.a*b.b + 1LL*a.b*b.c)%mod;
    ans.c = (1LL*a.b*b.b + 1LL*a.c*b.c)%mod;
    return ans;
}
mat pows[32];
int getFib( int a, int b, int n ) {
    if ( n == -1 ) { return 0; }
    mat crr ( a, b, a+b );
    for ( int i = 0 ; i < 32 ; i ++ ) {
        if ( n&(1<<i) ) {
            crr = mult( crr, pows[i] );
        }
        if ( (1<<i) >= n ) { return crr.a; }
    }
    return crr.a;
}
void prePows ( ) {
    pows[0] = mat ( 0, 1, 1 );
    for ( int i = 1 ; i < 32 ; i ++ ) {
        pows[i] = mult( pows[i-1], pows[i-1] );
    }
}
int n, m, k = 19;
void handle (int ind, int s ) {
    if ( it[ind].a == 0 && it[ind].b == 0 ) { return; }
    if ( ind >= (1<<k) ) {
        it[ind].val += it[ind].a;
        modd( it[ind].val );
        it[ind].a = 0; it[ind].b = 0;
    }

    it[ind].val += (1LL*getFib( it[ind].a, it[ind].b, s+1 ) - 1LL*it[ind].b );
    modd( it[ind].val );

    it [ind*2].a += it[ind].a;
    modd ( it[ind*2].a );
    it [ind*2].b += it[ind].b;
    modd ( it[ind*2].b );

    it[ind*2+1].a += getFib( it[ind].a, it[ind].b, s/2 );
    modd ( it[ind*2+1].a );
    it[ind*2+1].b += getFib( it[ind].a, it[ind].b, s/2+1 );
    modd ( it[ind*2+1].b );

    it[ind].a = 0;
    it[ind].b = 0;
}
int stL;
void add ( int s, int f, int ss, int ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return ; }
    if ( ss <= s && f <= ff ) {
        it[ind].a += getFib( 1, 1, s-stL );
        it[ind].b += getFib( 1, 1, s-stL+1 );
        handle( ind, f-s );
        return;
    }
    add( s, (s+f)/2, ss, ff, ind*2 );
    add( (s+f)/2, f, ss, ff, ind*2+1 );
    it[ind].val = (it[ind*2].val + it[ind*2+1].val);
    modd ( it[ind].val );
    return ;
}
int dd ( int x ) {
    if ( x >= mod ) { x -= mod; }
    return x;
}
int sum ( int s, int f, int ss, int  ff, int ind ) {
    handle( ind, f-s );
    if ( f <= ss || ff <= s ) { return 0; }
    if ( ss <= s && f <= ff ) { return it[ind].val; }
    return dd( sum ( s, (s+f)/2, ss, ff, ind*2 )+sum( (s+f)/2, f, ss, ff, ind*2+1 ) );
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    prePows () ;

    cin >> n >> m;
    for ( int  i = 0 ; i < n ; i ++ ) {
        cin >> it[i+(1<<k)].val;
    }
    for ( int  i = (1<<k)-1 ; i ; i-- ) {
        it[i].val = it[i*2].val + it[i*2+1].val;
        modd( it[i].val );
    }
    for ( int i = 0 ; i < m ; i ++ ) {
        int t, l ,r;
        cin >> t >> l >> r;
        l --;
        if ( t == 1 ) {
            stL = l;
            add ( 0, (1<<k), l, r, 1 );
        }else {
            cout << sum ( 0, (1<<k), l, r, 1 ) << "\n";
        }
    }
    return 0;
}
/*

a b     a+b   a+2b     2a+3b  3a+5b   5a+8b
a a+b  2a+b   3a+3b    5a+6b  8a+11b 13a+19b

0 1   1     2    3      5     8     13     21     55
0 1   2     4    7     12    20     33     54

3    4     7    11    18     29
3    7     14   25

*/


#include <iostream>
#include <vector>
using namespace std;
void scores()
{
    int sereja = 0, dima = 0;
    int n, card;
    int first, last;
    cin >> n ;
    vector<int>cards;
    for(int i = 0; i < n; i++)
    {
        cin >> card;
        cards.push_back(card);
    }
    while(1)
    {
        if(cards.size() == 0)
        {
            break;
        }
        auto it = cards.begin();
        first = *it;
        it = cards.end() - 1;
        last = *it;
        if(last > first)
        {
            sereja = sereja + last;
            cards.pop_back();
        }
        else
        {
            sereja = sereja + first;
            cards.erase(cards.begin());
        }

        it = cards.begin();
        first = *it;
        it = cards.end() - 1;
        last = *it;
        if(last > first)
        {
            dima = dima + last;
            cards.pop_back();

        }
        else
        {
            dima = dima + first;
            cards.erase(cards.begin());

        }
    }
    cout << sereja << " " << dima << cards.empty();
}
int main()
{
    scores();
}


#include <iostream>
#include <vector>
using namespace std;
void scores()
{
    int sereja = 0, dima = 0;
    int n, card;
    int first, last;
    cin >> n ;
    vector<int>cards;
    for(int i = 0; i < n; i++)
    {
        cin >> card;
        cards.push_back(card);
    }
    while(1)
    {
        if(cards.size() == 0)
        {
            break;
        }
        auto it = cards.begin();
        first = *it;
        it = cards.end() - 1;
        last = *it;
        if(last > first)
        {
            sereja = sereja + last;
            cards.pop_back();
        }
        else
        {
            sereja = sereja + first;
            cards.erase(cards.begin());
        }

        it = cards.begin();
        first = *it;
        it = cards.end() - 1;
        last = *it;
        if(last > first)
        {
            dima = dima + last;
            cards.pop_back();

        }
        else
        {
            dima = dima + first;
            cards.erase(cards.begin());

        }
    }
    cout << sereja << " " << dima ;
}
int main()
{
    scores();
}


#include <iostream>
#include <vector>
using namespace std;
void scores()
{
    int sereja = 0, dima = 0;
    int n, card;
    int first, last;
    cin >> n ;
    vector<int>cards;
    for(int i = 0; i < n; i++)
    {
        cin >> card;
        cards.push_back(card);
    }

    while(cards.size() != 0)
    {

        auto it = cards.begin();
        first = *it;
        it = cards.end() - 1;
        last = *it;
        if(last > first && cards.size() != 0)
        {
            sereja = sereja + last;
            cards.pop_back();
        }
        else if(last <= first && cards.size() != 0)
        {
            sereja = sereja + first;

            cards.erase(cards.begin());
        }

        it = cards.begin();
        first = *it;
        it = cards.end() - 1;
        last = *it;
        if(last > first && cards.size() != 0)
        {
            dima = dima + last;
            cards.pop_back();

        }
        else if(last <= first && cards.size() != 0)
        {
            dima = dima + first;
            cards.erase(cards.begin());

        }
    }
    cout << sereja << " " << dima ;
}
int main()
{
    scores();
}


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

int main(){

    int size;
    cin >> size;

    int arr[1000];

    for(int i=0;i<size;i++){
        cin >> arr[i];
    }

    int score1=0,score2=0;
    int start = 0,end = size - 1,i=0;
    while(start > end && i<size){
        if(arr[start] > arr[end]){
            if(i%2==0){
                score1 += arr[start];
            }else{
                score2 += arr[start];
            }

            i++;
            start++;
        }else if(arr[start] < arr[end]){
            if(i%2==0){
                score1 += arr[end];
            }else{
                score2 += arr[end];
            }

            i++;
            end--;
        }
    }

    cout << score1 << " " << score2 << endl;

    return 0;
}


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

int main(){

    int size;
    cin >> size;

    int arr[1000];

    for(int i=0;i<size;i++){
        cin >> arr[i];
    }

    int score1=0,score2=0;
    int start = 0,end = size - 1,i=0;
    while(start <= end && i<size){
        if(arr[start] > arr[end]){
            if(i%2==0){
                score1 += arr[start];
            }else{
                score2 += arr[start];
            }

            i++;
            start++;
        }else if(arr[start] < arr[end]){
            if(i%2==0){
                score1 += arr[end];
            }else{
                score2 += arr[end];
            }

            i++;
            end--;
        }
    }

    cout << score1 << " " << score2 << endl;

    return 0;
}


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

int main(){

    int size;
    cin >> size;

    int arr[1000];

    for(int i=0;i<size;i++){
        cin >> arr[i];
    }

    int score1=0,score2=0;
    int start = 0,end = size - 1,i=0;
    while(start != end && i<size){
        if(arr[start] > arr[end]){
            if(i%2==0){
                score1 += arr[start];
            }else{
                score2 += arr[start];
            }

            i++;
            start++;
        }else if(arr[start] < arr[end]){
            if(i%2==0){
                score1 += arr[end];
            }else{
                score2 += arr[end];
            }

            i++;
            end--;
        }
    }

    cout << score1 << " " << score2 << endl;

    return 0;
}


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

int main(){

    int size;
    cin >> size;

    int arr[1000];

    for(int i=0;i<size;i++){
        cin >> arr[i];
    }

    int score1=0,score2=0;
    int start = 0,end = size - 1,i=0;
    while(start <= end && i<size){
        if(arr[start] >= arr[end]){
            if(i%2==0){
                score1 += arr[start];
            }else{
                score2 += arr[start];
            }

            i++;
            start++;
        }else if(arr[start] <= arr[end]){
            if(i%2==0){
                score1 += arr[end];
            }else{
                score2 += arr[end];
            }

            i++;
            end--;
        }
    }

    cout << score1 << " " << score2 << endl;

    return 0;
}


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

int main(){

    int size;
    cin >> size;

    int arr[1000];

    for(int i=0;i<size;i++){
        cin >> arr[i];
    }

    int score1=0,score2=0;
    int start = 0,end = size - 1,i=0;
    while(start != end && i<size){
        if(arr[start] >= arr[end]){
            if(i%2==0){
                score1 += arr[start];
            }else{
                score2 += arr[start];
            }

            i++;
            start++;
        }else if(arr[start] <= arr[end]){
            if(i%2==0){
                score1 += arr[end];
            }else{
                score2 += arr[end];
            }

            i++;
            end--;
        }
    }

    cout << score1 << " " << score2 << endl;

    return 0;
}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
using namespace std;
int main(){
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int a, swtch = 0, sere = 0, dima =  0, k = 0, l;
        cin >> a;
        l = a-1;
        int nm[a];
        for(int i = 0; i < a ; i++){
                cin >> nm[i];
        }
      for(int i = 0, j = a - 1 ; i<=j; i++, j--)

        {

               if(nm[i]>nm[j]){
                       sere += nm[i];
                       dima += nm[j];
               }
               else{
                       sere+=nm[j];
                       dima += nm[i];
               }
}
if(a%2) cout << sere + nm[(a/2+1)] << endl;
else{
        cout << sere << " " << dima << endl;
}
}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
using namespace std;
int main(){
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int a, swtch = 0, sere = 0, dima =  0, k = 0, l;
        cin >> a;
        l = a-1;
        int nm[a];
        for(int i = 0; i < a ; i++){
                cin >> nm[i];
        }
       while(k<=l)

        {

                if(!(swtch%2)){

              if(nm[k]>nm[l]) {
                      sere += nm[k];
               ++k;
               }
               else{
sere += nm[l], --l;
               }

        }
        else{

               if (nm[k]>nm[l]) {
                       dima += nm[k];
                       ++k;
               }
                       else{
                       dima += nm[l];
                       --l;
                       }

        }
        swtch++;
}
cout << sere << " " << dima << endl;

}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
using namespace std;
int main(){
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int a, swtch = 0, sere = 0, dima =  0, k = 0, l;
        cin >> a;
        l = a-1;
        int nm[a];
        for(int i = 0; i < a ; i++){
                cin >> nm[i];
        }
      for(int i = 0, j = a - 1 ; i<=j; i++, j--)

        {

               if(nm[i]>nm[j]){
                       sere += nm[i];
                       dima += nm[j];
               }
               else{
                       sere+=nm[j];
                       dima += nm[i];
               }
}
if(a%2) cout << sere + nm[(a/2+1)] << " " << dima << endl;
else{
        cout << sere << " " << dima << endl;
}
}


#include<bits/stdc++.h>
#define endl "\n"
#define ll long long
using namespace std;
int main(){
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int a, swtch = 0, sere = 0, dima =  0, k = 0, l;
        cin >> a;
        l = a-1;
        int nm[a];
        for(int i = 0; i < a ; i++){
                cin >> nm[i];
        }
       while(k<=l)

        {

                if(!(swtch%2)){
                        cout << k  << " is k " << l << " is l "  << endl;

              if(nm[k]>nm[l]) {
                      sere += nm[k];
               ++k;
               }
               else{
sere += nm[l], --l;
               }

                 cout << swtch << " " <<  sere <<  endl;
        }
        else{

               cout << k  << " is k " << l << " is l "  << endl;
               if (nm[k]>nm[l]) {
                       dima += nm[k];
                       ++k;
               }
                       else{
                       dima += nm[l];
                       --l;
                       }
                cout << swtch << " " << dima << endl;

        }
        swtch++;
}
cout << sere << " " << dima << endl;

}


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

int main()
{
    vector<int> v;
    int n,sum1=0,sum2=0;
    cin >> n;
    int x,i;
    for(i=0;i<n;i++)
    {
        cin >> x;
        v.push_back(x);
    }
    sort(v.begin(), v.end());
    for(i=0;i<n;i++)
    {
        if(i%2==0)
        {
            sum1 = sum1 + v[i];
        }
        else
        {
            sum2 = sum2 + v[i];
        }
    }
    cout << max(sum1,sum2) << " "<< min(sum1,sum2);

}


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

int main()
{
    vector<int> v;
    vector<int> s;
    vector<int> d;
    int n,i,x,y,z,sum1=0,sum2=0;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> x;
        v.push_back(x);
    }

    for(i=0;i<n;i++)
    {
        if(i%2==0)
        {
            y = max(v.front(),v.back());
            sum1 = sum1 + y;
            if(max(v.front(),v.back()) == v.front())
            {
                vector<int> :: iterator it=v.begin();
                v.erase(it);
            }
            else
            {
                v.pop_back();
            }
        }
        else
        {
            z = max(v.front(),v.back());
            sum2 = sum2 + z;
            if(max(v.front(),v.back()) == v.front())
            {
                vector<int> :: iterator it=v.begin();
                v.erase(it);
            }
            else
            {
                v.pop_back();
            }
        }
    }
    cout << sum1 << " " << sum2;

}


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

int main()
{
    vector<int> v,d,s;
    //vector<int> s;
    //vector<int> d;
    int n,i,x,y,z,sum1=0,sum2=0;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> x;
        v.push_back(x);
    }
    for(i=0;i<v.size();i++)
    {
        if(i%2==0)
        {
            y = max(v.front(),v.back());
            sum1 = sum1 + y;
            if(max(v.front(),v.back()) == v.front())
            {
                vector<int>::iterator it = v.begin();
                v.erase(it);
            }
            else
            {
                v.pop_back();
            }
        }
        else
        {
            z = max(v.front(),v.back());
            sum2 = sum2 + z;
            if(max(v.front(),v.back()) == v.front())
            {
                vector<int>::iterator it = v.begin();
                v.erase(it);

            }
            else
            {
                v.pop_back();
            }
        }
    }
    cout << sum1 << " " << sum2;

}


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

int main()
{
    int n,i,sum1=0,sum2=0;
    cin >> n;
    int a[n];
    for(i=0;i<n;i++)
    {
        cin >> a[i];
    }
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        if(i%2==0)
        {
            sum1 = sum1 + a[i];
        }
        else
        {
            sum2 = sum2 + a[i];
        }
    }
    if(sum1>sum2)
    {
        cout << sum1 << " "<< sum2;
    }
    else
    {
        cout << sum2 << " " << sum1;
    }

    return 0;
}


#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<sstream>

using namespace std;

int main()
{
   int  n,s=0,d=0,z=0,x=0;
    cin>>n;
    int  arr[n];
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
    }
    x=n-1;
   for(int i=0;i<n;i++)
   {
       if(i%2==0)
       {
        d+=max(arr[z],arr[x]);
        if(max(arr[z],arr[x])==arr[x])
            x--;
        else
            z--;
       }
       else
       {
        s+=max(arr[z],arr[x]);
        if(max(arr[z],arr[x])==arr[x])
            x--;
        else
            z--;
       }
   }
    cout<<d<<" "<<s;
        return 0;
    }


#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<sstream>

using namespace std;

int main()
{
    int  n,s=0,d=0;
    cin>>n;
    int  arr[n];
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
    }
   sort(arr,arr+n);
   for(int i=0;i<n;i++)
   {
       if(i%2!=0)
        d+=arr[i];
       else
        s+=arr[i];
   }
  if(d>s)
    cout<<d<<" "<<s;
  else
    cout<<s<<" "<<d;
        return 0;
    }


#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<sstream>

using namespace std;

int main()
{
    int  n,s=0,d=0;
    cin>>n;
    int  arr[n];
    for(int i=1; i<=n; i++)
    {
        cin>>arr[i];

    }
   sort(arr,arr+n);
   for(int i=1;i<=n;i++)
   {
       if(i%2!=0)
        d+=arr[i];
       else
        s+=arr[i];
   }
  if(d>s)
    cout<<d<<" "<<s;
  else
    cout<<s<<" "<<d;
        return 0;
    }


#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<sstream>

using namespace std;

int main()
{
   long long n,s=0,d=0,z=0,x=0;
    cin>>n;
    long long arr[10001];
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
    }
    x=n-1;
   for(int i=0;i<n;i++)
   {
       if(i%2==0)
       {
        d+=(max(arr[z],arr[x]));
        if(max(arr[z],arr[x])==arr[z])
            z++;
        else
            x--;
       }
       else
       {
        s+=(max(arr[z],arr[x]));
        if(max(arr[z],arr[x])==arr[z])
            z++;
        else
            x--;
       }
   }
    cout<<d<<" "<<s;
        return 0;
    }


#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<sstream>

using namespace std;

int main()
{
    long long  n,s=0,d=0;
    cin>>n;
    long long arr[n];
    for(int i=1; i<=n; i++)
    {
        cin>>arr[i];

    }
   sort(arr,arr+n);
   for(int i=1;i<=n;i++)
   {
       if(i%2!=0)
        d+=arr[i];
       else
        s+=arr[i];
   }
  if(d>s)
    cout<<d<<" "<<s;
  else
    cout<<s<<" "<<d;
        return 0;
    }


#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<sstream>
#include<deque>
using namespace std;

int main()
{
    deque<int> d;
    long long n,s=0,dd=0,x;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x;
        d.push_back(x);
    }
int i=0;
    while(!d.empty())
    {
        if(i%2==0)
        {
            dd+=(max(d.front(),d.back()));
            if(max(d.front(),d.back())== d.front())
                d.pop_front();
                else
                    d.pop_back();
                }
        else
        {
            s+=(max(d.front(),d.back()));
            if(max(d.front(),d.back())==d.front())
                d.pop_front();
                else
                    d.pop_back();

                }
        i++;
    }

    cout<<dd<<" "<<s;
    return 0;
}


#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string>
#include<sstream>

using namespace std;

int main()
{
   long long n,s=0,d=0,z=0,x=0;
    cin>>n;
    long long arr[n];
    for(int i=0; i<n; i++)
    {
        cin>>arr[i];
    }
    x=n-1;
   for(int i=0;i<n;i++)
   {
       if(i%2==0)
       {
        d+=max(arr[z],arr[x]);
        if(max(arr[z],arr[x])==arr[x])
            x--;
        else
            z--;
       }
       else
       {
        s+=max(arr[z],arr[x]);
        if(max(arr[z],arr[x])==arr[x])
            x--;
        else
            z--;
       }
   }
    cout<<d<<" "<<s;
        return 0;
    }


#include <iostream>

using namespace std;

int main()
{
int n , S=0 , D=0 ;
cin >> n ;
int nums [n] ;
for ( int i =0 ; i<n ; i++ )
    cin >> nums[i] ;
int j=0 , k = n-1 ;
for ( int i =0 ; i<n ; i++ )
{
  if( j%2 ==0)
  {
    if (nums[i]>nums[k])
     S+=nums[i] ;
    else{
     S+=nums[k];
     i--;
     k--;}
  }
  else
  {
    if (nums[i]>nums[k])
     D+=nums[i] ;
    else{
     D+=nums[k];
     i--;
     k--;
  }
}
if (k==i)
break;
j++ ;
}
cout<< S <<" "<< D << endl;
    return 0;
}


#include <iostream>
using namespace std;
int main(){
int n,S=0,D=0;
cin>>n;
int a[n];
for(int i=0;i<n;i++)cin>>a[i];
bool turn=true;
for(int i=0,j=n-1;i<=j;)
{
if(turn){
if(a[i]>=a[j]){S+=a[i];i++;}
else {S+=a[j];j--;}
turn=false;}
else
{
if(a[i]>=a[j]){D+=a[i];i++;}
else {D+=a[j];j--;}
turn=true;}
}
cout<<S<<" "<<D<<endl;
return 0;
}


#include <iostream>
#include <string>

using namespace std;
int main()
{
//	cout<<1%2<<" "<<2<<endl;
	int n;cin>>n;
	int sereja=0,dima=0,fidx=0,bidx=n-1,arr[n];
	for(int i=0;i<n;i++)cin>>arr[i];
	for(int i=0;i<n;i++)
	{if(i%2==0)
	 {if(arr[fidx]>=arr[bidx]){sereja+=arr[fidx];fidx++;}
	 else{sereja+=arr[bidx];bidx--;}
	 }
	 else
     {if(arr[fidx]>=arr[bidx]){dima+=arr[fidx];fidx++;}
	 else{dima+=arr[bidx];bidx--;}
     }
	}
	cout<<sereja<<" "<<dima<<endl;
	return 0;
}


#include <iostream>

using namespace std;

int main()
{
    //first sol
    /*
int n , S=0 , D=0 ;
cin >> n ;
int nums [n] ;
for ( int i =0 ; i<n ; i++ )
    cin >> nums[i] ;
int j=0 , k = n-1 ;
for ( int i =0 ; i<n ; i++ )
{
  if( j%2 ==0)
  {
    if (nums[i]>nums[k])
     S+=nums[i] ;
    else{
     S+=nums[k];
     i--;
     k--;}
  }
  else
  {
    if (nums[i]>nums[k])
     D+=nums[i] ;
    else{
     D+=nums[k];
     i--;
     k--;
  }
}
if (k==i)
break;
j++ ;
}
cout<< S <<" "<< D << endl;
    */

//Second Sol:

int n , S=0 , D=0 ;
cin >> n ;
int j = 0 , k = n-1 ;
int nums [n] ;
for ( int i =0 ; i<n ; i++ )
    cin >> nums[i] ;
bool Sereja = true ;
while(j<=k)
{
    if (Sereja)
    {
       if ( nums[j]>nums[k] )
           {S+=nums[j];
           j++;}
       else
           {S+=nums[k];
           k--;}
    }
    else
    {
           if ( nums[j]>nums[k] )
           {D+=nums[j];
           j++;}
       else
           {D+=nums[k];
            k--;}
    }

    Sereja = !Sereja ;
}

cout<< S <<" "<< D << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){

int res;
int n;
int sereja=0;
int dima=0;
cin>>n;
int num[n];
for(int i=0;i<n;i++){
    int t;
    cin>>t;
    num[i]=t;
}
sort(num,num+n-1);
for(int p=n-1;p>=0;p--){
sereja+=num[p];
if(p>0){
dima+=num[p-1];
}
p--;
}
cout<<sereja<<" "<<dima<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){

int res;
int n;
int turns;
int sereja=0;
int dima=0;
cin>>n;
int num[n];
for(int i=0;i<n;i++){
    int t;
    cin>>t;
    num[i]=t;
}
if(n%2==0){
    turns=n/2;
}else{
    turns=(n+1)/2;
}
int f=0;
int p=n-1;
for(int i=0;i<=turns;i++){

if(p==0){
    if(n%2==0){
        dima+=num[f];
    }
    else{
sereja+=num[f];
    }
break;
    }
    if(p>f){
    if(num[f]>num[p]){
    sereja+=num[f];
    f++;
        cout<<"F:"<<f<<endl;

    }
    else{
        if(p>0){

        sereja+=num[p];
        p--;
        cout<<"P:"<<p<<endl;
        }
    }
    if(num[f]>num[p]){
        dima+=num[f];
        f++;
            cout<<"F:"<<f<<endl;

    }
    else{
  if(p>0){
        dima+=num[p];
        p--;
        cout<<"P:"<<p<<endl;
        }

    }
    }

}

cout<<sereja<<" "<<dima<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){

int res;
int n;
int turns;
int sereja=0;
int dima=0;
cin>>n;
int num[n];
for(int i=0;i<n;i++){
    int t;
    cin>>t;
    num[i]=t;
}
if(n%2==0){
    turns=n/2;
}else{
    turns=(n+1)/2;
}
int f=0;
int p=n-1;
for(int i=1;i<=turns;i++){
if(p==f){
sereja+=num[p];
break;
    }
    if(p>f){
    if(num[f]>num[p]){
    sereja+=num[f];
    f++;
    }
    else{
        if(p>0){

        sereja+=num[p];
        p--;
        }
    }
    if(num[f]>num[p]){
        dima+=num[f];
        f++;
    }
    else{
  if(p>0){
        dima+=num[p];
        p--;
        }

    }
    }
}

cout<<sereja<<" "<<dima<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){

int res;
int n;
int turns;
int sereja=0;
int dima=0;
cin>>n;
int num[n];
for(int i=0;i<n;i++){
    int t;
    cin>>t;
    num[i]=t;
}
if(n%2==0){
    turns=n/2;
}else{
    turns=(n+1)/2;
}
int f=0;
int p=n-1;
for(int i=0;i<=turns;i++){

if(p==0){
    if(n%2==0){
        dima+=num[f];
    }
    else{
sereja+=num[f];
    }
break;
    }
    if(p>f){
    if(num[f]>num[p]){
    sereja+=num[f];
    f++;

    }
    else{
        if(p>0){

        sereja+=num[p];
        p--;
        }
    }
    if(num[f]>num[p]){
        dima+=num[f];
        f++;

    }
    else{
  if(p>0){
        dima+=num[p];
        p--;
        }

    }
    }

}

cout<<sereja<<" "<<dima<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){

int res;
int n;
int turns;
int sereja=0;
int dima=0;
cin>>n;
int num[n];
for(int i=0;i<n;i++){
    int t;
    cin>>t;
    num[i]=t;
}
if(n%2==0){
    turns=n/2;
}else{
    turns=(n+1)/2;
}
int f=0;
int p=n-1;
for(int i=1;i<=turns;i++){

    if(num[f]>num[p]){

    sereja+=num[f];
    f++;
    }
    else{
        if(p>0){
        sereja+=num[p];
        p--;
        }
    }
    if(num[f]>num[p]){
        dima+=num[f];
        f++;
    }
    else{
  if(p>0){
        dima+=num[p];
        p--;
        }

    }
if(p==0){
sereja+=num[0];
break;
    }

}

cout<<sereja<<" "<<dima<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){

int res;
int n;
int turns;
int sereja=0;
int dima=0;
cin>>n;
int num[n];
for(int i=0;i<n;i++){
    int t;
    cin>>t;
    num[i]=t;
}
if(n%2==0){
    turns=n/2;
}else{
    turns=(n+1)/2;
}
int f=0;
int p=n-1;
for(int i=1;i<=turns;i++){
if(p>0){

    if(num[f]>num[p]){
    sereja+=num[f];
    f++;
    }
    else{
        sereja+=num[p];
        p--;
    }
    if(num[f]>num[p]){
        dima+=num[f];
        f++;
    }
    else{
        dima+=num[p];
        p--;
    }
}
    else if(p==0){
sereja+=num[0];
    }

}

cout<<sereja<<" "<<dima<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){

int res;
int n;
int turns;
int sereja=0;
int dima=0;
cin>>n;
int num[n];
for(int i=0;i<n;i++){
    int t;
    cin>>t;
    num[i]=t;
}
if(n%2==0){
    turns=n/2;
}else{
    turns=(n+1)/2;
}
int f=0;
int p=n-1;
for(int i=0;i<=turns;i++){

if(p==f){
    if(n%2==0){
        dima+=num[f];
    }
    else{
sereja+=num[f];
    }
break;
    }
    if(p>f){
    if(num[f]>num[p]){
    sereja+=num[f];
    f++;

    }
    else{
        if(p>0){

        sereja+=num[p];
        p--;
        }
    }
    if(num[f]>num[p]){
        dima+=num[f];
        f++;

    }
    else{
  if(p>0){
        dima+=num[p];
        p--;
        }

    }
    }

}

cout<<sereja<<" "<<dima<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){

int res;
int n;
int sereja=0;
int dima=0;
cin>>n;
int num[n];
for(int i=0;i<n;i++){
    int t;
    cin>>t;
    num[i]=t;
}
sort(num,num+n-1);
for(int p=n-1;p>=0;p--){
sereja+=num[p];
if(p>1){
dima+=num[p-1];
}
p--;
}
cout<<sereja<<" "<<dima<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){

int res;
int n;
int turns;
int sereja=0;
int dima=0;
cin>>n;
int num[n];
for(int i=0;i<n;i++){
    int t;
    cin>>t;
    num[i]=t;
}
if(n%2==0){
    turns=n/2;
}else{
    turns=(n+1)/2;
}
int f=0;
int p=n-1;
for(int i=1;i<=turns;i++){
if(p==0){
sereja+=num[0];
break;
    }
    if(num[f]>num[p]){

    sereja+=num[f];
    f++;
    }
    else{
        if(p>0){
        sereja+=num[p];
        p--;
        }
    }
    if(num[f]>num[p]){
        dima+=num[f];
        f++;
    }
    else{
  if(p>0){
        dima+=num[p];
        p--;
        }

    }

}

cout<<sereja<<" "<<dima<<endl;
return 0;
}


#include <cmath>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include<set>
#include<cstdlib>
#include<stack>
#include<string>
#include <iostream>
#include <algorithm>
#include<map>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    int a[n];
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    int m= sizeof(a)/sizeof(a[0]);
    sort(a, a+m);
    int p=0, q=0;
    for(int i=0; i<n; i++)
    {
        if(i%2 == 0)
            p += a[i];
        else
            q += a[i];
    }
    cout<<q<<" "<<p;

}


#include <cmath>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include<set>
#include<cstdlib>
#include<stack>
#include<string>
#include <iostream>
#include <algorithm>
#include<map>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    int a[n];
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    int m= sizeof(a)/sizeof(a[0]);
    sort(a, a+m);
    int p=0, q=0;
    for(int i=0; i<n; i++)
    {
        if(i%2 == 0)
            p += a[i];
        else
            q += a[i];
    }

    cout<<min(p,q)<<" "<<max(p,q);

}


#include <cmath>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include<set>
#include<cstdlib>
#include<stack>
#include<string>
#include <iostream>
#include <algorithm>
#include<map>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    int a[n];
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    int m= sizeof(a)/sizeof(a[0]);
    sort(a, a+m);
    int p=0, q=0;
    for(int i=0; i<n; i++)
    {
        if(i%2 == 0)
            p += a[i];
        else
            q += a[i];
    }
    cout<<p<<" "<<q;

}


#include <cmath>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include<set>
#include<cstdlib>
#include<stack>
#include<string>
#include <iostream>
#include <algorithm>
#include<map>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    int a[n];
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    int m= sizeof(a)/sizeof(a[0]);
    sort(a, a+m);
    int p=0, q=0;
    for(int i=0; i<n; i++)
    {
        if(i%2 == 0)
            p += a[i];
        else
            q += a[i];
    }
    cout<<q<<" "<<p;
}


#include <cmath>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include<set>
#include<cstdlib>
#include<stack>
#include<string>
#include <iostream>
#include <algorithm>
#include<map>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    int a[n];
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    int m= sizeof(a)/sizeof(a[0]);
    sort(a, a+m);
    int p=0, q=0;
    for(int i=0; i<n; i++)
    {
        if(i%2 == 0)
            p += a[i];
        else
            q += a[i];
    }
    cout<<q;

}


#include <cmath>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include<set>
#include<cstdlib>
#include<stack>
#include<string>
#include <iostream>
#include <algorithm>
#include<map>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    int a[n];
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    int m= sizeof(a)/sizeof(a[0]);
    sort(a, a+m, greater<int>());
    int p=0, q=0;
    for(int i=0; i<n; i++)
    {
        if(i%2 == 0)
            p += a[i];
        else
            q += a[i];
    }
    cout<<p<<" "<<q;

}


#include <cmath>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include<set>
#include<cstdlib>
#include<stack>
#include<string>
#include <iostream>
#include <algorithm>
#include<map>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    int a[n];
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    int m= sizeof(a)/sizeof(a[0]);
    sort(a, a+m);
    int p=0, q=0;
    for(int i=0; i<n; i++)
    {
        if(i%2 == 0)
            p += a[i];
        else
            q += a[i];
    }
    cout<<q;
    cout<<" ";
    cout<<p;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define ii pair<int,int>
#define lp(x,y) for(ll i=x;i<y;i++)
#define pb push_back
const int N = 200 * 1000 + 11;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll t;
    cin>>t;
    int arr[t],s=0,d=0;
    lp(0,t) cin>>arr[i];
    int a=0,b=t-1;
    lp(0,t)
    {
        if(i%2==0)
        {
            if(arr[a]>arr[b]) {s=s+arr[a]; a++;}
            else {s=s+arr[b]; b--;}
        }
        else
        {
            if(arr[a]>arr[b]) {d=d+arr[a]; a++;}
            else {d=d+arr[b]; b--;}
        }
    }
    cout<<s<<' '<<d;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(0);
ll t,s,d;
cin >> t;
vector<int> c;
int r;
while(t--){
    cin >> r;
    c.push_back(r);
}
if(c.size()%2!=0)
s+=c.back();
while(c.size()!=0){
    if(c.back()>c.front()){
        s+=c.back();
        c.pop_back();
    }
    else{
        s+=c.front();
        c.erase(c.begin());
    }
    if(c.back()>c.front()){
        d+=c.back();
        c.pop_back();
    }
    else{
        d+=c.front();
        c.erase(c.begin());
    }
}
cout <<s<<" "<<d;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(0);
ll t,s,d;
cin >> t;
vector<int> c;
int r;
while(t--){
    cin >> r;
    c.push_back(r);
}
if(c.size()%2!=0)
s+=c.back();
while(c.size()!=0){
    if(c.back()>c.front()){
        s+=c.back();
        c.pop_back();
    }
    else{
        s+=c.front();
        c.erase(c.begin());
    }
    if(c.back()>c.front()){
        d+=c.back();
        c.pop_back();
    }
    else{
        d+=c.front();
        c.erase(c.begin());
    }
}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(0);
ll t,s=0,d=0;
cin >> t;
vector<int> c;
int r;
while(t--){
    cin >> r;
    c.push_back(r);
}

while(c.size()!=0){
    if(c.back()>c.front()){
        s+=c.back();
        c.pop_back();
    }
    else{
        s+=c.front();
        c.erase(c.begin());
    }
    if(c.size()==0)
      break;
    if(c.back()>c.front()){
        d+=c.back();
        c.pop_back();
    }
    else{
        d+=c.front();
        c.erase(c.begin());
    }
}
cout << s << " " << d;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(0);
ll t,s,d;
cin >> t;
vector<int> c;
int r;
while(t--){
    cin >> r;
    c.push_back(r);
}
if(c.size()%2!=0)
s+=c.back();
while(c.size()!=0){
    if(c.back()>c.front()){
        s+=c.back();
        c.pop_back();
    }
    else{
        s+=c.front();
        c.erase(c.begin());
    }
    if(c.back()>c.front()){
        d+=c.back();
        c.pop_back();
    }
    else{
        d+=c.front();
        c.erase(c.begin());
    }
}


#include<bits/stdc++.h>
using namespace std;
int n,a[1010],p[2],m;
int main(){
	scanf("%d",&n);
	int l=1,r=n;
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i]);
	}
	int t=0;
	while(l<=r){
		if(a[l]>a[r]){
			p[t%2]+=a[l];
			l++;
		}else{
			p[t%2]+=a[r];
			r--;
		}
		t++;
	}
	printf("%d %d",p[0],p[1]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[100001],m,cnt1,cnt2;
bool cmp(int a,int b){
    return a < b;
}
int main(){
	cin>>m;
	for(int i=1;i<=m;i++)cin>>a[i];
	sort(a+1,a+m+1,cmp);
	for(int i=1;i<=m;i++){
		if(i%2==1)cnt1+=a[i];
		else cnt2+=a[i];
	}
	cout<<cnt1<<' '<<cnt2;
}


#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#define ll long long
#define itn int
using namespace std;

ll read(){
	ll n=0,s=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')s=-1;c=getchar();}
	while(c>='0'&&c<='9')n=n*10+c-48,c=getchar();
	return n*s;
}
int a[1005];
int main(){
	int n=read(),ans1=0,ans2=0;
	for(itn i=0;i<n;i++)a[i]=read();
	sort(a,a+n);
	for(itn i=n-1,j=0;i>=0;i--,j++){
		if(j%2==0)ans1+=a[i];
		else ans2+=a[i];
	}
	cout<<ans1<<' '<<ans2;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long mn(long long a,long long b){return a<b?a:b;}
long long mx(long long a,long long b){return a>b?a:b;}
int n,a[1010],ans[3],l,r,fl;
int main(){
    cin>>n;
    for(register int i=1;i<=n;i++)
        cin>>a[i];
    l=1,r=n;
    while(n--){
        if(fl==0){
            if(a[l]<a[r]){
                ans[1]=ans[1]+a[r];
                --r;
            }
            else{
                ans[1]=ans[1]+a[l];
                ++l;
            }
            fl=1;
        }
        else{
            if(a[l]<a[r]){
                ans[2]=ans[2]+a[r];
                --r;
            }
            else{
                ans[2]=ans[2]+a[l];
                ++l;
            }
            fl=0;
        }
    }
    printf("%d %d",ans[1],ans[2]);
    return 0;
}


#include<iostream>
using namespace std;
int n,a[1010],l,r,ans[2],m=0;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	l=1,r=n;
	while(l<=r){
		if(a[l]>=a[r])ans[m]+=a[l++];
		else  ans[m]+=a[r--];
		m=(m+1)%2;
	}
	cout<<ans[0]<<" "<<ans[1]<<endl;
}


#include <bits/stdc++.h>
using namespace std;
long long n,a[1001],x,y,l,r;
int main()
{
	cin >> n;
	l = 1,r = n;
	for (int i = 1; i <= n; i++) {cin >> a[i];}
	for (int i = 1; i <= n; i++)
		{
			if (i % 2 == 0)
				{
					if (a[l] > a[r])
						{
							y += a[l];
							l++;
						}
						else
							{
								y += a[r];
							    r--;
							}
				}
				else
					{
						if (a[l] > a[r])
						{
							x += a[l];
							l++;
						}
						else
							{
								x += a[r];
							    r--;
							}
					}
		}
	cout << x << " " << y;
	return 0;
 }


#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;

inline char gc()
{
	static const int L = 1000;
	static char sxd[L], *sss = sxd, *ttt = sxd;
	if(sss == ttt)
	{
		ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
		if(sss == ttt)
			return EOF;
	}
	return *sss++;
}

#define dd c = getchar()
inline int read(int& x)
{
	x = 0;
	char dd;
	bool f = false;
	for(; !isdigit(c); dd)
	{
		if(c == EOF)
			return -1;
		if(c == '-')
			f = true;
	}
	for(; isdigit(c); dd)
		x = (x << 1) + (x << 3) + (c ^ 48);
	if(f)
		x = -x;
	return 1;
}
#undef dd

int n,a[2000],i,l,r,turn,sum[2];
int main()
{
    read(n);
    for(i=1;i<=n;i++) read(a[i]);
    l=1;
	r=n;
    while(l<=r)
	{
        if(a[l]<a[r])
		{
            sum[turn]+=a[r];
            r--;
        }
        else
		{
            sum[turn]+=a[l];
            l++;
        }
        turn=!turn;
    }
    cout<<sum[0]<<' '<<sum[1];
    return 0;
}


#include<iostream>
using namespace std;
int n,a[1001],ans[2],m=0;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	int l=1,r=n;
	while(l<=r)
	{
		if(a[l]>=a[r]) ans[m]+=a[l++];
		else ans[m]+=a[r--];
		m=(m+1)%2;
	}
	cout<<ans[0]<<" "<<ans[1]<<"\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[1001],i,l,r,turn;
int sum[2];//sum[0]表示sereja,sum[1]表示dima
int main(){
	cin>>n;
	for(i=1;i<=n;i++) cin>>a[i];
	l=1,r=n;//初始化边界
	turn=0;//0表示sereja取牌,1表示dima
	while(l<=r){//注意此处必须有=
		if(a[l]<a[r]){
			sum[turn]+=a[r];
			r--;
		}//取最右端
		else{
			sum[turn]+=a[l];
			l++;
		}//取最左端
		turn=!turn;//轮流
	}
	cout<<sum[0]<<' '<<sum[1];
	return 0;
}


#include<iostream>
using namespace std;
int n,a[1010],l,r,ans[2],m=0;
int main()
{
    cin>>n;
    for(int i=1; i<=n; i++)
	cin>>a[i];
    l=1,r=n;
    while(l<=r)
    {
        if(a[l]>=a[r])ans[m]+=a[l++];
        else ans[m]+=a[r--];
        m=(m+1)%2;
    }
    cout<<ans[0]<<" "<<ans[1]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int card[1001]={};
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>card[i];
	}
	sort(card+1,card+n+1,greater<int>());
	int sereja=0,dima=0;
	for(int i=1;i<=n;i++)
	{
		if(i%2)sereja+=card[i];
		else dima+=card[i];
	}
	cout<<sereja<<" "<<dima<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[100001],m,cnt1,cnt2;
int main(){
	cin>>m;
	for(int i=1;i<=m;i++)cin>>a[i];
	sort(a+1,a+m+1);
	for(int i=1;i<=m;i++){
		if(i%2==1)cnt2+=a[i];
		else cnt1+=a[i];
	}
	cout<<cnt1<<' '<<cnt2;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cstdlib>
using namespace std;
int n,a[1001],ans;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=n;i>=1;i-=2) ans+=a[i];
    cout<<ans;
    ans=0;
    for(int i=n-1;i>=1;i-=2) ans+=a[i];
    cout<<" "<<ans;
    return 0;
}


/**/
#include<bits/stdc++.h>
using namespace std;
int n,a[100001],ansa,ansb;
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	sort(a,a+n);
	if(n%2==0){
		for(int i=n-1;i>=0;i--){
			if(i%2==1){
				ansa+=a[i];
			}else{
				ansb+=a[i];
			}
		}
	}else{
		for(int i=n-1;i>=0;i--){
			if(i%2==0){
				ansa+=a[i];
			}else{
				ansb+=a[i];
			}
		}
	}
	cout<<ansa<<" "<<ansb<<endl;
	return 0;
}


///*BISMILLAHIR_RAHMANIR_RAHIM*///

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,i,c=0,j,temp;
    cin>>n;
    int a[n+2];
    for(i=0; i<n; i++)
    {
        cin>>a[i];

    }

     for(j=1; j<=n; j++)
    {
        for(i=0; i<n-j; i++)
        {

            if(a[i]<a[i+1])
            {
                temp=a[i];
                a[i]=a[i+1];
                a[i+1]=temp;
            }

        }
    }

    i= 0,j=n-1;
    int s=0,d=0,mx;

    while(i<=j){

       if(a[i]>=a[j]){
            mx = a[i];
            i++;
       }
       else {
            mx = a[j];
            j++;
       }

            if(c%2==0){
                        s+=mx;
            }
            else d+=mx;

            c++;

    }
    cout<<s<<" "<<d<<endl;
}


///*BISMILLAHIR_RAHMANIR_RAHIM*///

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,i,c=0,j,temp;
    cin>>n;
    int a[n+2];
    for(i=0; i<n; i++)
    {
        cin>>a[i];

    }

    i= 0,j=n-1;
    int s=0,d=0,mx;

    while(i<=j)
    {

        if(a[i]>=a[j])
        {
            mx = a[i];
            i++;
        }
        else
        {
            mx = a[j];
            j--;
        }

        if(c%2==0)
        {
            s+=mx;
        }
        else
            d+=mx;

        c++;

    }
    cout<<s<<" "<<d<<endl;
}


///*BISMILLAHIR_RAHMANIR_RAHIM*///

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,i;
    cin>>n;
    int a[n+2];
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);

    int s=0,d=0;
    for(i=0; i<n; i+=2)
    {
        d+=a[i];
        s+=a[i+1];

    }
    cout<<s<<" "<<d<<endl;
}


///*BISMILLAHIR_RAHMANIR_RAHIM*///

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,i,c=0;
    cin>>n;
    int a[n+2];
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        c++;
    }
    sort(a,a+n);

    int s=0,d=0;
    if(c%2==0){
    for(i=0; i<n; i+=2)
    {
        d+=a[i];
        s+=a[i+1];

    }
    cout<<s<<" "<<d<<endl;
    }
    else if(c%2!=0){
    for(i=1; i<n; i+=2)
    {
        d+=a[i];
        s+=a[i+1];

    }
    cout<<s+a[0]<<" "<<d<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[n+1];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }a[n]=0;
    sort(a,a+n,greater<int>());
    int j=0,k=1;
    int m1=0,m2=0;
    while(j<=n&&k<=n)
    {
        m1+=a[j];
        m2+=a[k];
        j+=2;
        k+=2;
    }cout<<m1<<" "<<m2<<endl;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
   int n,s1=0,s2=0;
   cin>>n;
   int a[n];
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
   }sort(a,a+n,greater<int>());
   int j=0,k=1;
   while(j<n&&k<n)
   {
       s1+=a[j];
       s2+=a[k];
       j+=2;
       k+=2;
   }cout<<s1<<" "<<s2<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,s1=0,s2=0;
    cin>>n;
    int a[n+1];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }a[n]=0;
    sort(a,a+n,greater<int>());
    int k=0,j=1;
   // if(n&1)n++;
    a[n]=0;
    while(j<=n&&k<=n)
    {
        s1+=a[k];
        s2+=a[j];
        k+=2;
        j+=2;
    }cout<<s1<<" "<<s2<<endl;
}


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

const int MAXN = 1001;
int n, num[MAXN], f = 0, l, t;
long long s, d;

int main(){
	cin >> n;
	l = n - 1;
	for(int i = 0 ; i < n ; i++) cin >> num[i];
	for(int i = 0 ; i < n ; i++){
		if(num[f] >= num[l]){
			if(t == 0){
				s += num[f];
				t = 1;
			}
			else{
				d += num[f];
				t = 0;
			}
			f++;
		}
		else{
			if(t == 0){
				s += num[l];
				t = 1;
			}
			else{
				d += num[l];
				t = 0;
			}
			l--;
		}
	}
	if(s > d)
		cout << s << " " << d;
	else
		cout << d << " " << s;
}


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

const int MAXN = 1001;
int n, num[MAXN], f = 0, l, s, d, t;

int main(){
	cin >> n;
	l = n - 1;
	for(int i = 0 ; i < n ; i++) cin >> num[i];
	for(int i = 0 ; i < n ; i++){
		if(num[f] >= num[l]){
			if(t == 0){
				s += num[f];
				t = 1;
			}
			else{
				d += num[f];
				t = 0;
			}
			f++;
		}
		else{
			if(t == 0){
				s += num[l];
				t = 1;
			}
			else{
				d += num[l];
				t = 0;
			}
			l--;
		}
	}
	if(s > d)
		cout << s << " " << d;
	else
		cout << d << " " << s;
}


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

int n, s, tt, t;

int main(){
	cin >> n;
	int num[n];
	for(int i = 0 ; i < n ; i ++) cin >> num[i];
	sort(num, num + n);
	while(n != 0){
		//cout << num[n - 1] << " ";
		if(t == 0){
			s += num[n - 1];
			t = 1;
		}
		else{
			tt += num[n - 1];
			t = 0;
		}
		n--;
	}
	//cout << endl;
	cout << tt << " " << s;
}


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

int n, s, tt, t;

int main(){
	cin >> n;
	int num[n];
	for(int i = 0 ; i < n ; i ++) cin >> num[i];
	sort(num, num + n);
	while(n != 0){
		cout << num[n - 1] << " ";
		if(t == 0){
			s += num[n - 1];
			t = 1;
		}
		else{
			tt += num[n - 1];
			t = 0;
		}
		n--;
	}
	cout << endl;
	cout << tt << " " << s;
}


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

int n, s, tt, t;

int main(){
	cin >> n;
	int num[n];
	for(int i = 0 ; i < n ; i ++) cin >> num[i];
	sort(num, num + n);
	while(n != 0){
		//cout << num[n - 1] << " ";
		if(t == 0){
			s += num[n - 1];
			t = 1;
		}
		else{
			tt += num[n - 1];
			t = 0;
		}
		n--;
	}
	//cout << endl;
	cout << s << " " << tt;
}


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

const int MAXN = 1001;
int n, num[MAXN], f = 0, l, t;
long long s, d;

int main(){
	cin >> n;
	l = n - 1;
	for(int i = 0 ; i < n ; i++) cin >> num[i];
	for(int i = 0 ; i < n ; i++){
		if(num[f] >= num[l]){
			if(t == 0){
				s += num[f];
				t = 1;
			}
			else{
				d += num[f];
				t = 0;
			}
			f++;
		}
		else{
			if(t == 0){
				s += num[l];
				t = 1;
			}
			else{
				d += num[l];
				t = 0;
			}
			l--;
		}
	}
	cout << s << " " << d;
}


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

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,ser=0,dim=0;
	cin>>n;
	int arr[n];
	for(int i=0;i<n;i++){
		cin>>arr[i];
	}
	sort(arr,arr+n);
	for(int i=n-1;i>-1;i-=2){
		cout<<i<<" "<<i-1<<endl;
		cout<<arr[i]<<" "<<arr[i-1]<<endl;
		dim+=arr[i];
		ser+=arr[i-1];
	}
	cout<<dim<<" "<<ser;

	return 0;
}


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

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,ser=0,dim=0;
	cin>>n;
	int arr[n];
	for(int i=0;i<n;i++){
		cin>>arr[i];
	}
	sort(arr,arr+n);
	for(int i=n-1;i>-1;i-=2){

		dim+=arr[i];
		ser+=arr[i-1];
	}
	cout<<dim<<" "<<ser;

	return 0;
}


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

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,ser=0,dim=0,count=0;
	cin>>n;
	int a=0,b=n-1;
	int arr[n];
	for(int i=0;i<n;i++){
		cin>>arr[i];
	}
	while(count<n){
		if(arr[a]>arr[b]){
			cout<<a<<" 1 "<<b<<endl;
			ser+=arr[a];
			a++;
		}else{
			cout<<a<<" 2 "<<b<<endl;
			ser+=arr[b];
			b--;
		}
		if(arr[a]>arr[b] && count!=n-1){
			cout<<a<<" 3 "<<b<<endl;
			dim+=arr[a];
			a++;
		}else if(count!=n-1){
			cout<<a<<" 4 "<<b<<endl;
			dim+=arr[b];
			b--;
		}

		count+=2;
}
	cout<<ser<<" "<<dim;

	return 0;
}


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

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,ser=0,dim=0;
	cin>>n;
	int arr[n];
	for(int i=0;i<n;i++){
		cin>>arr[i];
	}
	sort(arr,arr+n);
	arr[-1]=0;
	for(int i=n-1;i>-1;i-=2){

		dim+=arr[i];
		ser+=arr[i-1];
	}
	cout<<dim<<" "<<ser;

	return 0;
}


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

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,ser=0,dim=0,count=0;
	cin>>n;
	int a=0,b=n-1;
	int arr[n];
	for(int i=0;i<n;i++){
		cin>>arr[i];
	}
	while(count<n){
		if(arr[a]>arr[b]){
			ser+=arr[a];
			a++;
		}else{
			ser+=arr[b];
			b--;
		}
		if(arr[a]>arr[b] && count!=n-1){
			dim+=arr[a];
			a++;
		}else if(count!=n-1){
			dim+=arr[b];
			b--;
		}

		count+=2;
}
	cout<<ser<<" "<<dim;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,a[1005];
int main()
{
	cin >>n;
	for (int i=1;i<=n;i++) cin >>a[i];
	sort (a+1,a+1+n);
	int s1=0,s2=0;
	for (int i=n;i>=1;i--)
	{
		if (i%2!=0) s1+=a[i];
		else s2+=a[i];
	}
	cout <<max(s1,s2)<<' '<<min(s1,s2)<<endl;
	return 0;
}


#include <queue>
#include <cstdio>
using namespace std;
int n, ans[2];
deque<int> q;
int main() {
	scanf("%d", &n);
	for(int i = 1, a; i <= n; ++i) scanf("%d", &a), q.push_back(a);
	for(int i = 1; i <= n; ++i)
		if(q.front() > q.back()) ans[i&1] += q.front(), q.pop_front();
		else ans[i&1] += q.back(), q.pop_back();
	printf("%d %d\n", ans[1], ans[0]);
}


#include <bits/stdc++.h>

using namespace std;

inline int read()
{
	int f=1,x=0;
	char c=getchar();

	while(c<'0' || c>'9')
	{
		if(c=='-')f=-1;
		c=getchar();
	}

	while(c>='0' && c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}

	return f*x;
}

int n,a[1010],ans[3],l,r,fl;

int main()
{
	n=read();

	for(register int i=1; i<=n; i++)
	{
		a[i]=read();
	}

	l=1,r=n;

	while(n--)
	{
		if(fl==0)
		{
			if(a[l]<a[r])
			{
				ans[1]=ans[1]+a[r];

				--r;
			}
			else
			{
				ans[1]=ans[1]+a[l];

				++l;
			}

			fl=1;
		}
		else
		{
			if(a[l]<a[r])
			{
				ans[2]=ans[2]+a[r];

				--r;
			}
			else
			{
				ans[2]=ans[2]+a[l];

				++l;
			}

			fl=0;
		}
	}

	printf("%d %d",ans[1],ans[2]);

	return 0;
}


#include<iostream>

using namespace std;

int n,a[1010],l,r,ans[2],m=0;

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)  cin>>a[i];

	l=1,r=n;
	while(l<=r)
	{
		if(a[l]>=a[r])  ans[m]+=a[l++];
		else  ans[m]+=a[r--];
		m=(m+1)%2;
	}

	cout<<ans[0]<<" "<<ans[1]<<endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cstdlib>
using namespace std;
int n,a[1001],l=1,r,x,y;
int main(){
    cin>>n;
    r=n;
    for(int i=1;i<=n;i++) cin>>a[i];
    while(l<=r){
    	if(a[l]>a[r]){
    		x+=a[l];
    		l++;
		}
		else{
    		x+=a[r];
    		r--;
		}
		if(l<=r)
			if(a[l]>a[r]){
    			y+=a[l];
    			l++;
			}
			else{
    			y+=a[r];
    			r--;
			}
	}
	cout<<x<<" "<<y;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[1001],i,l,r,turn;
int sum[2];//sum[0]表示sereja,sum[1]表示dima
int main()
{
    cin>>n;
    for(i=1;i<=n;i++) cin>>a[i];
    l=1,r=n;//初始化边界
    turn=0;//0表示sereja取牌,1表示dima
    while(l<=r){//注意此处必须有=
        if(a[l]<a[r]){
            sum[turn]+=a[r];
            r--;
        }//取最右端
        else{
            sum[turn]+=a[l];
            l++;
        }//取最左端
        turn=!turn;//轮流
    }
    cout<<sum[0]<<' '<<sum[1];
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[100001],m,cnt1,cnt2;
bool cmp(int a,int b){
    return a < b;
}
int main(){
	cin>>m;
	for(int i=1;i<=m;i++)cin>>a[i];
	sort(a+1,a+m+1,cmp);
	for(int i=1;i<=m;i++){
		if(i%2==1)cnt1+=a[i];
		else cnt2+=a[i];
	}
	cout<<cnt1<<' '<<cnt2;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,a[1005];
	cin >> n;
	int sum1=0,sum2=0;
	for(int i=0;i<n;i++){
		cin >> a[i];
	}
	for(int i=0,j=n-1; ; ){
		if(a[i]>a[j]){
			sum1 += a[i];
			i++;
		}
		else{
			sum1 += a[j];
			j--;
		}
		if(a[i]>a[j]){
			sum2 += a[i];
			i++;
		}
		else{
			sum2 += a[j];
			j--;
		}
		if(i>j) break;
		if(i==j){
			sum1 += a[j];
			break;
		}
	}
	cout << sum1 <<" "<< sum2;
}