#abc271c. C - Manga

C - Manga

Score : 300300 points

问题描述

Takahashi 打算阅读一部共 10910^9 卷的漫画系列 "Snuke-kun"。
最初,Takahashi 已拥有该系列的 NN 本书。其中第 ii 本书是第 aia_i 卷。

开始阅读之前,Takahashi 可以任意次数(包括零次)进行以下操作:

  • 如果他拥有 1 本或更少的书,则不做任何操作;否则,卖出他所拥有的两本书,并用任意一卷的书替换。

然后,Takahashi 按顺序阅读第 1 卷、第 2 卷、第 3 卷、……。但是,当他没有下一本需要阅读的卷时,他会停止阅读该系列(不论他还拥有其他哪些卷的书)。

找出他能够读到的最晚卷数。如果他无法阅读任何卷,则答案为 00

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Takahashi is going to read a manga series "Snuke-kun" in 10910^9 volumes.
Initially, Takahashi has NN books of this series. The ii-th book is Volume aia_i.

Takahashi may repeat the following operation any number of (possibly zero) times only before he begins to read:

  • Do nothing if he has 11 or less books; otherwise, sell two of the books he has and buy one book of any volume instead.

Then, Takahashi reads Volume 11, Volume 22, Volume 33, \ldots, in order. However, when he does not have a book of the next volume to read, he stops reading the series (regardless of the other volumes he has).

Find the latest volume of the series that he can read up to. If he cannot read any, let the answer be 00.

Constraints

  • 1N3×1051 \leq N \leq 3 \times 10^5
  • 1ai1091 \leq a_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

a1a_1 \ldots aNa_N

Output

Print the answer.

Sample Input 1

6
1 2 4 6 7 271

Sample Output 1

4

Before he begins to read the series, he may do the following operation: "sell books of Volumes 77 and 271271 and buy one book of Volume 33 instead." Then, he has one book each of Volumes 11, 22, 33, 44, and 66.
If he then begins to read the series, he reads Volumes 11, 22, 33, and 44, then tries to read Volume 55. However, he does not have one, so he stops reading at this point.
Regardless of the choices in the operation, he cannot read Volume 55, so the answer is 44.

Sample Input 2

10
1 1 1 1 1 1 1 1 1 1

Sample Output 2

5

Takahashi may have multiple books of the same volume.
For this input, if he performs the following 44 operations before he begins to read the series, he can read up to Volume 55, which is the maximum:

  • Sell two books of Volume 11 and buy a book of Volume 22 instead.
  • Sell two books of Volume 11 and buy a book of Volume 33 instead.
  • Sell two books of Volume 11 and buy a book of Volume 44 instead.
  • Sell two books of Volume 11 and buy a book of Volume 55 instead.

Sample Input 3

1
5

Sample Output 3

0

Takahashi cannot read Volume 11.

update @ 2024/3/10 11:25:24