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

这题也是被安排的明明白白  组队训练的时候这题不会做

后面说是字典树 学了2个小时字典树还是没写出来

心态蹦了

现学字典树

 #include <bits/stdc++.h>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const LL LLINF = 0x3f3f3f3f3f3f3f3fll;
const int maxn = 1e6 + ;
const int mod = 1e9 + ;
LL t[], a[maxn];
struct trie {
int cnt[maxn * ], tree[maxn * ][], arr[], root, rear;
int newnode() {
cnt[++rear] = ;
mem(tree[rear], );
return rear;
}
void init() {
rear = ;
root = newnode();
}
void add(LL x) {
int now = root, temp;
for (int i = ; i < ; i++) arr[i] = x % , x /= ;
for (int i = ; i >= ; i--) {
temp = arr[i];
if (!tree[now][temp]) tree[now][temp] = newnode();
now = tree[now][temp];
cnt[now]++;
}
}
LL query1(LL x) {
int now = root, maxx, idx;
LL ret = ;
for (int i = ; i < ; i++) arr[i] = x % , x /= ;
for (int i = ; i >= ; i--) {
maxx = -, idx = -;
for (int j = ; j < ; j++)
if (tree[now][j] && (arr[i] + j) % > maxx) maxx = (arr[i] + j) % , idx = j;
ret += t[i] * maxx;
now = tree[now][idx];
}
return ret;
}
LL query2(LL x) {
int now = root, maxx, idx;
LL ret = ;
for (int i = ; i < ; i++) arr[i] = x % , x /= ;
for (int i = ; i >= ; i--) {
maxx = , idx = -;
for (int j = ; j < ; j++)
if (tree[now][j] && (arr[i] + j) % < maxx ) maxx = (arr[i] + j) % , idx = j;
ret += t[i] * maxx;
now = tree[now][idx];
}
return ret;
}
} tr;
int main() {
t[] = ;
for (int i = ; i < ; i++) t[i] = t[i - ] * ;
int n;
sf(n);
LL ans1 = (1LL) << , ans2 = ;
tr.init();
for (int i = ; i < n ; i++) {
scanf("%lld", &a[i]);
if (i) {
ans1 = min(ans1, tr.query2(a[i]));
ans2 = max(ans2, tr.query1(a[i]));
}
tr.add(a[i]);
}
printf("%lld %lld\n", ans1, ans2);
return ;
}

Gaby And Addition Gym - 101466A (初学字典树)的更多相关文章

  1. 字典树变形 A - Gaby And Addition Gym - 101466A

    A - Gaby And Addition Gym - 101466A 这个题目是一个字典树的变形,还是很难想到的. 因为这题目每一位都是独立的,不会进位,这个和01字典树求最大的异或和是不是很像. ...

  2. CodeFoeces GYM 101466A Gaby And Addition (字典树)

    gym 101466A Gaby And Addition 题目分析 题意: 给出n个数,找任意两个数 “相加”,求这个结果的最大值和最小值,注意此处的加法为不进位加法. 思路: 由于给出的数最多有 ...

  3. A .Gaby And Addition (Gym - 101466A + 字典树)

    题目链接:http://codeforces.com/gym/101466/problem/A 题目: 题意: 给你n个数,重定义两个数之间的加法不进位,求这些数中两个数相加的最大值和最小值. 思路: ...

  4. 【贪心】【字典树】Gym - 101466A - Gaby And Addition

    题意:定义一种无进位加法运算,给你n个正整数,问你取出两个数,使得他们加起来和最大/最小是多少. 无进位加法运算,其实是一种位运算,跟最大xor那个套路类似,很容易写出对于每个数字,其对应的最优数字是 ...

  5. ACM: Gym 100935F A Poet Computer - 字典树

    Gym 100935F A Poet Computer Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d &am ...

  6. codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...

  7. stl应用(map)或字典树(有点东西)

    M - Violet Snow Gym - 101350M Every year, an elephant qualifies to the Arab Collegiate Programming C ...

  8. Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点

    题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...

  9. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

随机推荐

  1. 正式放弃Edge,重新拥抱Chrome

    从Edge还叫斯巴达的时候我就开始用了,本来对浏览器的要求也没多高,能够打开多个选项卡,稳定,支持最新的规范就好了. 但是Edge真的是越来越让我失望了,卡死问题越来越多,崩溃越来越频繁,我也快奔溃了 ...

  2. 【MFC】学习与问题整合

    需要源码联系邮件:kangxlchn@163.com 1.新建一个MFC工程(基于对话框) 环境:vs2017 统统NEXT 新建完成后打开MFCPrj.cpp文件 打开类试图 每创建一个MFC项目, ...

  3. 基础数据类型-dict

    字典Dictinary是一种无序可变容器,字典中键与值之间用“:”分隔,而与另一个键值对之间用","分隔,整个字典包含在{}内: dict1 = {key1:value1, key ...

  4. HDU 4169 Wealthy Family(树形DP)

    Problem Description While studying the history of royal families, you want to know how wealthy each ...

  5. NProgress.js加载进度插件的简单实用方法

    NProgress.js 说明: NProgress是基于jquery的,且版本要 >1.8 下载地址: https://github.com/rstacruz/nprogress API: N ...

  6. Python运行的方式

    Python的运行方式多种多样,下面列举几种: 交互式 在命令行中输入python,然后在>>>提示符后面输入Python语句,这里需要注意: 1 语句前面不能有空格,否则会报错 2 ...

  7. C# HttpWebRequest post提交数据,提交对象

    1.客户端方法 //属于客户端 //要向URL Post的方法 public void PostResponse() { HttpWebRequest req = (HttpWebRequest)Ht ...

  8. JS DOM视频相关的知识

    1.实现点击a标签改变图片时,如果a的href属性有一个目标网址,但是点击又必须跳转到另外一张图,往往会最后跳转到目标网址,可以在onclick事件函数中加入ruturn false,阻止跳转到页面. ...

  9. 对alpha发布的总结技术随笔

    对于今天的alpha发布,首先需要自我检讨,因为我们组没有展示作品.主要的原因还是我们投入的时间不足.我们的项目是约跑App,首先选择做安卓平台的东西,我们大家都需要熟悉新的开发软件Android S ...

  10. PAT L1-048 矩阵A乘以B

    https://pintia.cn/problem-sets/994805046380707840/problems/994805082313310208 给定两个矩阵A和B,要求你计算它们的乘积矩阵 ...