A - Gaby And Addition Gym - 101466A

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 n numbers 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

6
17 5 11 0 42 99

Output

0 99

Input

7
506823119072235413 991096248449924896 204242310783332529 778958050378192979 384042493592684633 942496553147499866 410043616343857825

Output

52990443860776502 972190360051424498

Note

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

 

 

字典树的题目

把每个数都化成一位一位的保存在字典树中,个位在最下面。

然后输入一个数就找一次最大最小值

 

#include<bits/stdc++.h>
using namespace std;
long long a[1000005],tree[13000005][10],cnt,flag,aa[55];
void insert(long long x,long long t)//字典树的插入操作 
{
	for(long long i=0; i<=18; i++)//分解  
	{
		aa[i]=x%10;
		x/=10;
	}
	for(long long i=18; i>=0; i--)
	{
		if(tree[t][aa[i]]==0)
		{
			tree[t][aa[i]]=++cnt;
		}
		t=tree[t][aa[i]];
	}
}
long long find(long long x,long long t)//查找最大值的操作 
{
	long long sum=0;
	for(long long i=0; i<=18; i++,x/=10)//分解 
		aa[i]=x%10;
	for(long long i=18; i>=0; i--)
	{
		long long mx=-1;//給一个最小值 
		for(long long j=0; j<=9; j++)
		{
			if(tree[t][j]&&mx<(aa[i]+j)%10)//这个数存在 并且加起来最大 
			{
				mx=(aa[i]+j)%10;
				flag=j;
			}
		}
		sum=sum*10+mx;
		t=tree[t][flag];
	}
	return sum;
}
long long find2(long long x,long long t)//查找最小值的操作  同上 
{
	long long sum=0;
	for(long long i=0; i<=18; i++,x/=10)
		aa[i]=x%10;
	for(long long i=18; i>=0; i--)
	{
		long long mx=15;//赋一个最大值 
		for(long long j=0; j<=9; j++)
		{
			if(tree[t][j]&&mx>(aa[i]+j)%10)
			{
				mx=(aa[i]+j)%10;
				flag=j;
			}
		}
		sum=sum*10+mx;
		t=tree[t][flag];
	}
	return sum;
}
int main()
{
	long long n,maxn=0,minn=0x3f3f3f3f3f3f3f3f;
	scanf("%lld",&n);
	for(long long i=1; i<=n; i++)
	{
		scanf("%lld",&a[i]);
		if(i>1)//只有大于2个数才能相加 
		{
			maxn=max(maxn,find(a[i],0));
			minn=min(minn,find2(a[i],0));//求出最大最小值 
		}
		insert(a[i],0);//先求再插入 ,因为不能和自己相加 
	}
	printf("%lld %lld\n", minn, maxn);
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值