AcWing 1240. 完全二叉树的权值(java)
原题链接
简单
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int N = 100010;
static int n;
static int[] a = new int[N];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
String[] s = br.readLine().split(" ");
for(int i = 1;i <= n;i++) a[i] = Integer.parseInt(s[i - 1]);
long max = Integer.MIN_VALUE;
int depth = 0;
for(int d = 1, i = 1;i <= n;d++, i *= 2) {//d:层数,i:该层的起始节点
long sum = 0;//当前层的权值和
for(int j = i;j < i + (1 << d - 1) && j <= n;j++) {//遍历该层
sum += a[j];
}
if(sum > max) {
max = sum;
depth = d;
}
}
System.out.println(depth);
}
}