二分、递推
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;const int N = 1e5 + 10;
int h[N] = {0};int n,maxh=0; // 当 E >= maxh 时一定可以满足bool check(int mid){int E = mid;for(int i = 1;i <= n;i++){E = 2 * E - h[i];if(E >= maxh) return true;if(E < 0) return false;}return true;
}int main(){ios::sync_with_stdio(0),cin.tie(0); // 输入数据超过1e5 优化输入流 cin >> n;for(int i = 1;i <= n;i++){cin >> h[i];maxh = max(maxh,h[i]);}int l = 0,r = maxh; // 大于maxh时一定可以通过 此时优化右端点为maxh;while(l < r){int mid = (l + r)/2;if(check(mid)) r = mid;else l = mid + 1;}cout << r << endl;return 0;
}