题目https://www.luogu.com.cn/problem/P8681
代码如下:
cpp
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define endl '\n'
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e5;
int minh = -1;
int n;
int sum[N],a[N];
void dfs(int node,int h){
if(node>n)return;
sum[h]+=a[node];
dfs(2*node,h+1);
dfs(2*node+1,h+1);
if(sum[h]>sum[minh]) minh = h;
}
int main(){
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
dfs(1,1);
cout<<minh<<endl;
return 0;
}
测评结果: