题目:

Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order.

Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0).

Help Ivan, find the required quantity of numbers.

Input

The first line contains integer n (1 ≤ n ≤ 105). The second input line contains nspace-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an.

Output

Print a single integer — the answer to the problem.

Examples

Input

4
0 1 1 1

Output

0

Input

1
3

Output

3

Note

In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7.

In the second sample you need to add numbers 20, 21, 22.

解题报告:首先咱们需要的数学储备知识,(2^a+2^a)=2^(a+1)  ,2^1+2^2……+2^n=2^(n+1)-1;接下来咱们可以开始了,所求的是满足题意的 :将所有的数组元素进行2的幂数操作,然后求和,问再添加几位数可以使和满足2^v-1,因为题目没有要求是否最小添加,所以利用set,将数组元素相同的进行合并操作,然后找出最大的数字,之前的用数补齐,那么所补数的数目就是输出的数值。

ac代码:

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
using namespace std;
typedef long long ll;set<int > ss;
int main()
{int n,a;while(scanf("%d",&n)!=EOF){ss.clear();int maxx=0;for(int i=0;i<n;i++){scanf("%d",&a);while(ss.count(a)){ss.erase(a);a++;}ss.insert(a);maxx=max(maxx,a); }printf("%d\n",maxx-ss.size()+1);}return 0;
}

【Ivan and Powers of Two】【CodeForces - 305C】(思维)(Set 应用)相关推荐

  1. Ivan and Powers of Two CodeForces - 305C(set)

    Ivan has got an array of n non-negative integers a1, a2, -, an. Ivan knows that the array is sorted ...

  2. 【CodeForces - 305C】Ivan and Powers of Two(思维)

    C. Ivan and Powers of Two time limit per test 0.5 seconds memory limit per test 256 megabytes input ...

  3. 【CodeForces - 305C】Ivan and Powers of Two(思维,STL,set,优先队列)

    题干: Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is s ...

  4. CodeForces 305C Ivan and Powers of Two【set】

    这个题啊,纠结了本公举一中午,知道如果出现了重复的,就要往后移,差点就把自己想的恶心的bool 数组付诸实现了,== 既然是思维的题,借助了STL的特性是再正常不过的了,既然想写数组,先应该想想Set ...

  5. Codeforces Round #594 (Div. 2) C. Ivan the Fool and the Probability Theory 思维 + dp

    文章目录 题意: 思路 题意: 思路 一开始找规律,表都打好了,没找出来.. 找规律还是适合让队友来. 先考虑第一行,我们先计算第一行的方案数,设f[i][j]f[i][j]f[i][j]表示到了ii ...

  6. CodeForces 798D 思维,贪心

    CodeForces 798D 题意:长度为 n的两个数组 a[]和 b[],要找出 k ( k<=n/2+1 )个下标,使得在两个数组中这 k个数的和乘上 2 要大于所有数的和. tags: ...

  7. codeforces - 1315C - 思维题

    原题链接:https://codeforces.com/problemset/problem/1315/C 翻译: 这是一个猜谜游戏,你需要猜中一个序列,谜题是一个序列,我们设为b,长度为n.你需要根 ...

  8. Balanced Substring CodeForces - 873B (思维+前缀和)

    Balanced Substring CodeForces - 873B You are given a string s consisting only of characters 0 and 1. ...

  9. Ivan and Powers of Two

    Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorte ...

最新文章

  1. 常用命令linux下vi编辑器命令大全
  2. Buuctf(pwn)[OGeek2019]babyrop
  3. 项目经理怎么运用思维导图
  4. 微服务、容器和Kubernetes的2020你怎么看?
  5. rust房屋建造蓝图_都说蓝图,而不是白图、红图,你知道为什么?
  6. 容器编排技术 -- Kubernetes 在 Namespace 中配置默认的CPU请求与限额
  7. Bzoj 3339: Rmq Problem Bzoj 3585: mex 莫队,树状数组,二分
  8. 机房服务器存放位置要求,服务器机房建设设计要求规范.doc
  9. C语言自己写代码实现的strcmp函数
  10. scala 定义空list_scala-03-list操作
  11. Esxi 5下ROS5.18+Panabit
  12. git clone下载代码,解决中途断开下载的方法
  13. Excel表格如何设置下拉选项并应用到整列
  14. grpc入门到精通_gRPC[go语言]大师教程,构建现代化Api和微服务
  15. Spring5.0源码学习系列之Spring AOP简述(九)
  16. 迅雷5单磁极 v5.9.99.999【单文件-下磁力】解除敏感资源限制
  17. ubuntu20.04如何安装搜狗输入法
  18. Array [] 数组
  19. 解决电脑cpu锁频率0.4GHz问题
  20. ssserver 总是自动退出的原因

热门文章

  1. 二、线性规划中的约束
  2. Read MIPS Run2nd MIPS cache(转)
  3. Dev-C++5.11游戏创作之吃豆人小游戏(转载)
  4. 寒武纪Cambricon MLU270 环境搭建 CNStream EasyDK编译 改变系统路径 CNJPEGENC_FLAG_INVALID_FRAME 错误
  5. python imread的用法_Python imread、newaxis用法详解
  6. BIO聊天室(群聊+私聊)
  7. android时间接收器,android – 在飞机模式关闭/打开后,广播接收器在onReceive()中接收的时间太长...
  8. 【经典算法题】水壶问题
  9. 没有注册类别的一种解决方法
  10. 资产数字化通证NCMT森林币IEO火热进行中