#abc368c. C - Triple Attack

C - Triple Attack

Score : 300300 points

问题陈述

你正在玩一个游戏。

NN 个敌人排成一行,第 ii 个敌人从前面开始的生命值为 HiH_i

你将重复以下动作,直到所有敌人的生命值都变成 00 或更少,使用一个变量 TT,初始值为 00

  • TT 增加 11。然后,攻击最前面的敌人,如果其生命值大于或等于 11。如果 TT33 的倍数,敌人的生命值减少 33;否则,减少 11

找出当所有敌人的生命值都变成 00 或更少时,TT 的值。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are playing a game.

There are NN enemies lined up in a row, and the ii-th enemy from the front has a health of HiH_i.

You will repeat the following action until the healths of all enemies become 00 or less, using a variable TT initialized to 00.

  • Increase TT by 11. Then, attack the frontmost enemy with health 11 or more. If TT is a multiple of 33, the enemy's health decreases by 33; otherwise, it decreases by 11.

Find the value of TT when the healths of all enemies become 00 or less.

Constraints

  • 1N2×1051 \leq N \leq 2\times 10^5
  • 1Hi1091 \leq H_i \leq 10^9
  • All input values are integers.

Input

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

NN

H1H_1 H2H_2 \ldots HNH_N

Output

Print the answer.

Sample Input 1

3
6 2 2

Sample Output 1

8

The actions are performed as follows:

  • TT becomes 11. Attack the 1st enemy, and its health becomes 61=56-1=5.
  • TT becomes 22. Attack the 1st enemy, and its health becomes 51=45-1=4.
  • TT becomes 33. Attack the 1st enemy, and its health becomes 43=14-3=1.
  • TT becomes 44. Attack the 1st enemy, and its health becomes 11=01-1=0.
  • TT becomes 55. Attack the 2nd enemy, and its health becomes 21=12-1=1.
  • TT becomes 66. Attack the 2nd enemy, and its health becomes 13=21-3=-2.
  • TT becomes 77. Attack the 3rd enemy, and its health becomes 21=12-1=1.
  • TT becomes 88. Attack the 3rd enemy, and its health becomes 11=01-1=0.

Sample Input 2

9
1 12 123 1234 12345 123456 1234567 12345678 123456789

Sample Output 2

82304529

Sample Input 3

5
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

3000000000

Beware of integer overflow.