Gym - 101466A—Gaby And Addition

A. Gaby And Addition

Gym - 101466A

Gaby is a little baby who loves playing with numbers. Recently she has learned how to add 2 numbers using the standard addition algorithm which we summarize in 3 steps:

  1. Line up the numbers vertically matching digits places.
  2. Add together the numbers that share the same place value from right to left.
  3. Carry if necessary.

it means when adding two numbers we will get something like this:

Unfortunately as Gaby is too young she doesn't know what the third step means so she just omitted this step using her own standard algorithm (Gaby's addition algorithm). When adding two numbers without carrying when necessary she gets something like the following:

Gaby loves playing with numbers so she wants to practice the algorithm she has just learned (in the way she learned it) with a list of numbers adding every possible pair looking for the pair which generates the largest value and the smallest one.

She needs to check if she is doing it correctly so she asks for your help to find the largest and the smallest value generated from the list of numbers using Gaby's addition algorithm.

Input

The input starts with an integer n (2 ≤ n ≤ 106) indicating the number of integers Gaby will be playing with. The next line contains nnumbers ni (0 ≤ ni ≤ 1018) separated by a single space.

Output

Output the smallest and the largest number you can get from adding two numbers from the list using Gaby's addition algorithm.

Examples

input

Copy

6
17 5 11 0 42 99

output

Copy

0 99

input

Copy

7
506823119072235413 991096248449924896 204242310783332529 778958050378192979 384042493592684633 942496553147499866 410043616343857825

output

Copy

52990443860776502 972190360051424498

Note

In the first sample input this is how you get the minimum and the maximum value

 

字典树,每个节点存放每个位数的值,然后对于每次输人一个值都和所有已经存在的数找最大最小值

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+10;
int tree[maxn*20][10];
int a[50];//存放每一个位数 
int cnt=0;
void insert(ll x,ll rt)//字典树插入 
{
	for(int i=0;i<=18;i++)
	{
		a[i]=x%10;//取出每一位 
		x/=10;
	}
	for(int i=18;i>=0;i--)
	{
		if(tree[rt][a[i]]==0)
		{
			tree[rt][a[i]]=++cnt;
		}
		rt=tree[rt][a[i]];
	}
}
ll findmax(ll x,int rt)//和每一个比较找最大值 
{
	for(int i=0;i<=18;i++)
	{
		a[i]=x%10;
		x/=10;
	}
	int flag;
	ll sum=0;
	for(int i=18;i>=0;i--)//最多19位 
	{
		ll mi=-1;
		for(int j=0;j<=9;j++)//每个数字范围都是0-9 
		{
			if(tree[rt][j]&&(a[i]+j)%10>mi)//如果为真说明有值,且值为j 
			{
				mi=(a[i]+j)%10;
				flag=j;	//存放此次位置,方便下次查找		
			}
		}
		sum=sum*10+mi;
		rt=tree[rt][flag];//记录下次位置 
	}
	return sum;
}
ll findmin(ll x,int rt)//同上,稍微改一下最大最小值就好了 
{
	for(int i=0;i<=18;i++)
	{
		a[i]=x%10;
		x/=10;
	}
	int flag;
	ll sum=0;
	for(int i=18;i>=0;i--)
	{
		ll mi=15;
		for(int j=0;j<=9;j++)
		{
			if(tree[rt][j]&&(a[i]+j)%10<mi)
			{
				mi=(a[i]+j)%10;
				flag=j;			
			}
		}
		sum=sum*10+mi;
		rt=tree[rt][flag];
	}
	return sum;
}
int main()
{
	int n;
	scanf("%d",&n);
	ll minn=0x3f3f3f3f3f3f3f3f;
	ll maxx=0;
	for(int i=1;i<=n;i++)
	{
		ll x;
		scanf("%lld",&x);
		if(i>1)//大于1才找最大最小值 
		{
			maxx=max(maxx,findmax(x,0));
			minn=min(minn,findmin(x,0));
		}
		insert(x,0);//后插入,因为先插入可能会自己+自己最大 
	}
	cout<<minn<<" "<<maxx<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值