func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 1000100;   const int MOD = 1000000007;   vector<long long> al[N], sum[N];   vector<pair<int, int> > adj[N];   pair<int, int> p[N];   int cnt(int u, long long ls) {     return upper_bound(al[u].begin(), al[u].end(), ls) - al[u].begin();   }   long long hp(int u, long long h, long long ls) {     long long c1 = cnt(u, ls);     if (c1 == 0) return 0;     return h * c1 - sum[u][c1 - 1];   }   int n, k, l[N], q, a, h;   void dfs(int u, long long cost) {     al[u].push_back(0);     for (int i = 0; i < adj[u].size(); ++i) {       int v = adj[u][i].first;       dfs(v, cost + adj[u][i].second);       for (int k = 0; k < al[v].size(); ++k) {         al[u].push_back(al[v][k] + adj[u][i].second);       }     }     sort(al[u].begin(), al[u].end());     for (int i = 0; i < al[u].size(); ++i) {       sum[u].push_back((i ? sum[u].back() : 0) + (al[u][i]));     }   }   int main() {     memset(p, -1, sizeof p);     scanf( %d%d , &n, &q);     for (int i = 1; i <= n - 1; ++i) {       scanf( %d , &l[i]);       int u = (i + 1) / 2;       int v = (i + 1);       p[v] = make_pair(u, l[i]);       adj[u].push_back(make_pair(v, l[i]));     }     dfs(1, 0);     while (q--) {       scanf( %d%d , &a, &h);       long long cur = a, prev = -1, rem = h;       long long add = 0;       while (1) {         add += hp(cur, rem, rem);         if (prev != -1) {           long long kk = rem - p[prev].second;           add -= hp(prev, kk, kk);         }         prev = cur;         rem = rem - p[cur].second;         cur = p[cur].first;         if (cur == -1 || rem < 0) break;       }       printf( %lld n , add);     }     return 0;   }   