Chief Hopper
Difficulty: Hard

Chief's bot is playing an old DOS based game.  There is a row of buildings of different heights arranged at each index along a number line.  The bot starts at building $0$ and at a height of $0$.  You must determine the minimum energy his bot needs at the start so that he can jump to the top of each building without his energy going below zero.  

Units of height relate directly to units of energy.  The bot's energy level is calculated as follows:  

- If the bot's $botEnergy$ is less than the height of the building, his $newEnergy = botEnergy - (height - botEnergy)$  
- If the bot's $botEnergy$ is greater than the height of the building, his $newEnergy = botEnergy + (botEnergy - height)$  

**Example**  

$arr = [2,3,4,3,2]$   

Starting with $botEnergy = 4$, we get the following table:

<pre>
	botEnergy	height	delta
    4				2		+2
    6				3		+3
    9				4		+5
    14				3		+11
    25				2		+23
    48
</pre>

That allows the bot to complete the course, but may not be the minimum starting value.  The minimum starting $botEnergy$ in this case is $3$.  

**Function Description**  

Complete the *chiefHopper* function in the editor below.  

chiefHopper has the following parameter(s):  

- *int arr[n]:* building heights   

**Returns**  

- *int:* the minimum starting $botEnergy$    



Input Format:
The first line contains an integer $n$, the number of buildings. 

The next line contains $n$ space-separated integers $arr[1]..arr[n]$, the heights of the buildings.  

Output Format:

Sample Input:
    5
    3 4 3 2 4
    

    
**sample Output**  

    4


**Sample Input**  

	3
    4 4 4


Sample Output:
    4
    




Python 3 starter code:
#
# Complete the 'chiefHopper' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#

def chiefHopper(arr):
    # Write your code here




Reference solution (C++):
###C++  
```cpp  
#include <bits/stdc++.h>
using namespace std;

const long long linf = 0x3f3f3f3f3f3f3f3fLL;
const int N = 111111;

int n;
int h[N];

int check(long long x) {
    long long energy = x;
    for(int i = 1; i <= n; i++) {
        energy += energy - h[i];
        if(energy >= linf) return 1;
        if(energy < 0) return 0;
    }
    return 1;
}
int main() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> h[i];
    }

    long long  L = 0, R = linf;

    while(L < R) {
        long long M = (L + R) / 2;
        if(check(M)) {
            R = M;
        } else {
            L = M + 1;
        }
    }

    cout << L << endl;

    return 0;
}
```