func_code_string: #include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const int maxn = 3e5 + 10, lg = 20;   vector<pair<long long, long long> > v[maxn];   long long n, w[maxn], rmq[lg + 2][maxn], dp[maxn], up[maxn], W[maxn],       par[lg + 2][maxn], h[maxn], q;   void dfs_down(int i, int j) {     h[i] = h[j] + 1;     dp[i] = w[i];     for (int y = 0; y < v[i].size(); y++) {       int x = v[i][y].first;       if (x - j) {         W[x] = v[i][y].second;         dfs_down(x, i);         dp[i] += max(0LL, dp[x] - 2 * W[x]);       }     }   }   long long get_dp(int pes) {     long long s = 0;     if (dp[pes] - 2 * W[pes] >= 0 && pes != 1) s = -(dp[pes] - 2 * W[pes]);     return s;   }   void dfs_up(int i, int j) {     if (i != 1) {       up[i] = max(0LL, up[j] + dp[j] + get_dp(i) - 2 * W[i]);     }     for (int y = 0; y < v[i].size(); y++) {       int x = v[i][y].first;       if (x - j) {         dfs_up(x, i);       }     }   }   void get_par(int i, int j) {     par[0][i] = j;     rmq[0][i] = -W[i] + dp[j] + get_dp(i);     for (int y = 1; y <= lg; y++) {       int p = par[y - 1][i];       par[y][i] = par[y - 1][p];       rmq[y][i] = rmq[y - 1][i] + rmq[y - 1][p];     }     for (int y = 0; y < v[i].size(); y++) {       int x = v[i][y].first;       if (x - j) {         get_par(x, i);       }     }   }   long long query(int i, int j) {     if (i == j) return up[i] + dp[i];     if (h[i] > h[j]) swap(i, j);     int o = h[j] - h[i];     int I = i;     int J = j;     long long Ans = 0;     for (int y = 0; y <= lg; y++) {       if (o & (1 << y)) {         Ans += rmq[y][j];         j = par[y][j];       }     }     if (j == i) return Ans + dp[J] + up[i];     Ans += dp[I] + dp[J];     for (int y = lg; y > -1; y--) {       if (par[y][i] != par[y][j])         Ans += rmq[y][j] + rmq[y][i], i = par[y][i], j = par[y][j];     }     int p = par[0][j];     Ans += up[p] + rmq[0][i] + rmq[0][j] + get_dp(i) + get_dp(j) -            (dp[p] + get_dp(i) + get_dp(j));     return Ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> q;     for (int y = 1; y <= n; y++) cin >> w[y];     for (int y = 1; y < n; y++) {       int i, j, p;       cin >> i >> j >> p;       v[i].push_back(pair<long long, long long>(j, p));       v[j].push_back(pair<long long, long long>(i, p));     }     dfs_down(1, 0);     dfs_up(1, 0);     get_par(1, 0);     while (q--) {       int i, j;       cin >> i >> j;       cout << query(i, j) << endl;     }   }   