1240. 完全二叉树的权值
P8681 [蓝桥杯 2019 省 AB] 完全二叉树的权值
AC
java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] res = new int[n+1];
for(int i=1;i<=n;i++) {
res[i] = sc.nextInt();
}
int cs = 1;
long MAX = Long.MIN_VALUE;
int key = 0;
int t = 1;
for(int i=1;i<=n;i*=2,cs++,t*=2) {
int w = t;
long sum = 0;
for(int j=i;j<=n&&t!=0;j++,t--) {
sum+=res[j];
}
if(sum>MAX) {
MAX = sum;
key = cs;
}
t = w;
}
System.out.println(key);
}
}