treeeee
tree tree tree tree tree treeunknown
c_cpp
a year ago
803 B
2
Indexable
Never
#include<bits/stdc++.h> using namespace std; const int N = 2e4; int a[N], miN[N], maX[N], Msum[N], ans1; int main(){ int n; cin >> n; for(int i = 1; i < 1<<n; i++) cin >> a[i]; for(int i = (1<<n)-1; i >= 1<<(n-1); i--) miN[i] = maX[i] = Msum[i] = a[i]; for(int i = (1<<(n-1))-1; i>0; i--){ Msum[i] = max(Msum[i*2],Msum[i*2+1]) + a[i]; //cout << i << " " << miN[i*2] << " " << maX[i*2] << " " << miN[i*2+1] << " " << maX[i*2+1] << endl; if(ans1 == -1)continue; if(miN[i*2] < miN[i*2+1]){ if(maX[i*2]>=miN[i*2+1]) ans1 =-1; }else if(miN[i*2] > miN[i*2+1]){ if(maX[i*2+1]>=miN[i*2]) ans1 = -1; else ans1++; }else ans1 = -1; maX[i] = max(maX[i*2], maX[i*2+1]); miN[i] = min(miN[i*2], miN[i*2+1]); } cout << ans1 << endl; cout << Msum[1] << endl; }