#include<iostream> #include<algorithm> #include<vector> usingnamespacestd; constint maxn = 1000010; typedeflonglong ll; int L[maxn]; vector<ll> dist[maxn], accDist[maxn]; int n, m; voidcalDist(int i){ if (i <= 0 || i > n) return; if (2 * i <= n && 2 * i + 1 <= n) { calDist(2 * i); calDist(2 * i + 1); dist[i].push_back(0); for (int di = 0; di < dist[2 * i].size(); di++) { dist[i].push_back(dist[2 * i][di] + L[2 * i - 1]); } for (int di = 0; di < dist[2 * i + 1].size(); di++) { dist[i].push_back(dist[2 * i + 1][di] + L[2 * i]); } inplace_merge(dist[i].begin() + 1, dist[i].begin() + 1 + dist[2 * i].size(), dist[i].end()); } elseif (2 * i <= n && 2 * i + 1 > n) { calDist(2 * i); dist[i].push_back(0); for (int di = 0; di < dist[2 * i].size(); di++) { dist[i].push_back(dist[2 * i][di] + L[2 * i - 1]); } } else { dist[i].push_back(0); } ll acc_ = 0; for (int di = 0; di < dist[i].size(); di++){ accDist[i].push_back((acc_ += dist[i][di])); } }
ll forSubtree(int v, ll H){ if (H < 0 || v > n) return0; int pos = upper_bound(dist[v].begin(), dist[v].end(), H) - dist[v].begin(); return H * pos - accDist[v][pos - 1]; }
ll forPar(int v, int p, ll H){ // calculate happiness int v_ = (v == 2 * p ? 2 * p + 1 : 2 * p); return forSubtree(v_, H - L[v_ - 1]); }
intmain(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i < n; i++) cin >> L[i]; calDist(1); for (int mi = 0; mi < m; mi++) { int A, H; cin >> A >> H; ll ans = forSubtree(A, H); while (A > 1) { H -= L[A - 1]; if (H <= 0) break; ans += H; ans += forPar(A, A / 2, H); A /= 2; } cout << ans << endl; } }