注意:
j <= n不能少!!!
代码如下:
#include <iostream>
using namespace std;
const int N = 100010;
typedef long long LL;
int a[N];int main() {int n;LL maxv = -1e18;cin >> n;int depth = 0;for (int i = 1; i <= n; i++)cin >> a[i];for (int i = 1, d = 1; i <= n; i *= 2, d++) {LL s = 0;for (int j = i; j < i + (1 << d - 1) && j <= n ; j++) {s += a[j];}if (s > maxv) {maxv = s;depth = d;}}cout << depth << endl;return 0;
}