malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, performance;     long long cost = 0;     long long k;     cin >> n >> k;     int x[n], c[n];     for (int& e : x) cin >> e;     cin >> performance;     for (int& e : c) cin >> e;     priority_queue<int, vector<int>, greater<int>> rem;     int i = 0;     for (int e : x) {       rem.push(c[i++]);       while (k < e) {         if (rem.empty()) {           cout <<  -1 n ;           return 0;         }         k += performance, cost += rem.top(), rem.pop();       }     }     cout << cost <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct dame {     int b, i, r;     dame() {}     dame(int b_, int i_, int r_) : b(b_), i(i_), r(r_) {}   };   bool greater_compare(dame A, dame B) {     return A.b > B.b || A.b == B.b && A.i < B.i ||            A.b == B.b && A.i == B.i && A.r < B.r;   }   bool by_i(dame a, dame b) { return a.i < b.i; }   int size = 2;   vector<int> tree;   void update(int pos, int value) {     pos += size;     if (value <= tree[pos]) return;     tree[pos] = value;     pos /= 2;     while (pos > 0) {       tree[pos] = max(tree[pos * 2], tree[pos * 2 + 1]);       pos /= 2;     }   }   int getmax(int l, int r, int i = 1, int a = 0, int b = size - 1) {     if (l > r) return 0;     if (l == a && r == b) return tree[i];     int center = (a + b) / 2;     if (r <= center) return getmax(l, r, i * 2, a, center);     if (l > center) return getmax(l, r, i * 2 + 1, center + 1, b);     return max(getmax(l, center, i * 2, a, center),                getmax(center + 1, r, i * 2 + 1, center + 1, b));   }   int main() {     int n, answer = 0;     scanf( %d , &n);     while (size < n) size *= 2;     vector<dame> dames(n);     tree = vector<int>(size * 2, 0);     for (int i = 0; i < n; i++) scanf( %d , &dames[i].b);     for (int i = 0; i < n; i++) scanf( %d , &dames[i].i);     for (int i = 0; i < n; i++) scanf( %d , &dames[i].r);     sort(dames.begin(), dames.end(), by_i);     int compress_val = -1, prev = -1;     for (int i = 0; i < n; i++) {       if (dames[i].i != prev) compress_val++;       prev = dames[i].i;       dames[i].i = compress_val;     }     sort(dames.begin(), dames.end(), greater_compare);     for (int i = 0; i < n; i++) {       int max_r = getmax(dames[i].i + 1, n - 1);       if (max_r > dames[i].r) answer++;       update(dames[i].i, dames[i].r);     }     printf( %d n , answer);   }
#include <bits/stdc++.h>   using namespace std;   const long double PI = 3.1415926535897932384626433832795;   template <typename S, typename T>   ostream& operator<<(ostream& out, pair<S, T> const& p) {     out <<  (  << p.first <<  ,   << p.second <<  ) ;     return out;   }   template <typename T>   ostream& operator<<(ostream& out, vector<T> const& v) {     int l = v.size();     for (int i = 0; i < l - 1; i++) out << v[i] <<    ;     if (l > 0) out << v[l - 1];     return out;   }   void tr() { cout << endl; }   template <typename S, typename... Strings>   void tr(S x, const Strings&... rest) {     cout << x <<    ;     tr(rest...);   }   long long fast(long long a, long long b, long long MOD) {     long long ret = 1;     while (b) {       if ((b & 1)) ret = (ret * a) % MOD;       a = (a * a) % MOD;       b >>= 1;     }     return ret;   }   const long long MOD = 1e6 + 3;   int main() {     long long n, k;     cin >> n >> k;     if (n <= 59 and (1ll << n) < k) {       cout << 1 <<     << 1 << endl;       return 0;     }     long long a, b;     b = fast(2, n, MOD);     b = fast(b, k - 1, MOD);     a = 1;     long long x = (k - 1) / MOD;     long long y = (k - 1) % MOD;     long long t = fast(2, n, MOD);     for (int i = 1; i <= MOD; i++) {       a *= (t - i + MOD) % MOD;       a %= MOD;     }     a = fast(a, x, MOD);     for (int i = 1; i <= y; i++) {       a *= (t - i + MOD) % MOD;       a %= MOD;     }     long long tot = 0, tmp = k - 1, pw = 2;     while (pw <= tmp) {       tot += tmp / pw;       pw <<= 1;     }     a = a * fast(fast(2, tot, MOD), MOD - 2, MOD) % MOD;     b = b * fast(fast(2, tot, MOD), MOD - 2, MOD) % MOD;     a = (b - a + MOD) % MOD;     cout << a <<     << b << endl;     return 0;   }
#include <bits/stdc++.h>   long long int trie[10000007][2] = {0};   long long int bin[40] = {0}, x = 0;   long long int a[100005] = {0}, prefix[100005] = {0}, suffix[100005] = {0};   void tobinary(long long int x) {     int cnt = 0;     memset(bin, 0, sizeof(bin));     while (x > 0) {       bin[cnt++] = (x % 2);       x /= 2;     }   }   void update(long long int node, long long int idx) {     if (idx < 0) return;     if (bin[idx] == 0) {       if (trie[node][0] == -1) {         trie[node][0] = ++x;       }       update(trie[node][0], idx - 1);     } else {       if (trie[node][1] == -1) {         trie[node][1] = ++x;       }       update(trie[node][1], idx - 1);     }   }   long long int query(long long int node, long long int idx) {     if (idx < 0) return 0LL;     long long int ret = 0;     if (bin[idx] == 0) {       if (trie[node][1] != -1) {         ret += ((1LL) << idx);         ret += query(trie[node][1], idx - 1);       } else {         ret += query(trie[node][0], idx - 1);       }     } else {       if (trie[node][0] != -1) {         ret += (1LL << idx);         ret += query(trie[node][0], idx - 1);       } else {         ret += query(trie[node][1], idx - 1);       }     }     return ret;   }   int main() {     memset(trie, -1, sizeof(trie));     long long int n, i, ans = 0;     scanf( %I64d , &n);     for (i = 1; i <= n; i++) {       scanf( %I64d , &a[i]);       prefix[i] = a[i] ^ prefix[i - 1];     }     for (i = n; i >= 1; i--) {       suffix[i] = suffix[i + 1] ^ a[i];     }     for (i = 0; i <= n; i++) {       tobinary(prefix[i]);       update(0, 39);       tobinary(suffix[i + 1]);       ans = (ans > query(0, 39) ? ans : query(0, 39));     }     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math )   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native )   static int a[100001], n, m, l, r, x, cnt;   static bool type;   inline static int read() {     register int x = 0;     static char ch = getchar();     while (ch <  0  || ch >  9 ) ch = getchar();     while (ch >=  0  && ch <=  9 )       x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     return x;   }   signed main(void) {     n = read(), m = read();     for (register int i = 1; i <= n; ++i) a[i] = read();     for (; m--;) {       type = (read() == 1);       l = read(), r = read(), x = read();       if (type)         for (register int i = l; i <= r; ++i) a[i] -= (a[i] > x) ? x : 0;       else {         cnt = 0;         for (register int i = l; i <= r; ++i) cnt += (a[i] == x);         printf( %d n , cnt);       }     }   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<pair<int, int> > v;   long long dp[2][5001][51][51];   const long long oo = 1e16;   int main() {     cin >> n;     v.resize(n);     for (auto &x : v) cin >> x.first;     int total = 0;     for (auto &x : v) {       cin >> x.second;       total += x.second;     }     vector<int> bef(n, 1);     sort(v.rbegin(), v.rend());     for (int i = 1; i < n; ++i)       if (v[i - 1].first == v[i].first) bef[i] += bef[i - 1];     bef.push_back(1);     v.push_back(make_pair(-1, -1));     for (int i = n; i >= 0; --i) {       for (int rem = total; rem >= 0; --rem) {         for (int g = 0; g <= i; ++g) {           int me = bef[i];           for (int cg = 0; cg < me; ++cg) {             long long ret = oo;             if (i == n) {               if (rem == 0) ret = 0;             } else {               const bool ch = v[i + 1].first != v[i].first;               if (g > 0) {                 if (ch)                   ret = dp[1 & ~i][rem][g - 1 + cg][0];                 else                   ret = dp[1 & ~i][rem][g - 1][cg];               }               if (rem >= v[i].second) {                 if (ch)                   ret =                       min(ret, v[i].first +                                    dp[1 & ~i][rem - v[i].second][g + cg + 1][0]);                 else                   ret = min(                       ret, v[i].first + dp[1 & ~i][rem - v[i].second][g][cg + 1]);               }             }             dp[i & 1][rem][g][cg] = ret;           }         }       }     }     pair<long long, long long> ans;     long long res = -1;     for (int i = 1; i <= total; ++i) {       long long cur = dp[0][i][0][0];       if (cur == oo) continue;       if (res == -1) {         ans.first = cur;         ans.second = i;         res = 0;       } else {         if (ans.first * i <= cur * ans.second) continue;         ans.first = cur;         ans.second = i;       }     }     res = ans.first * 10000;     res = (res + ans.second - 1) / ans.second;     int add = res % 10 > 0;     res = res / 10 + add;     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int o = 0, t = 0, to = 0;       int n = s.length();       for (int i = 0; i < n; i++) {         if (s.substr(i, 3) ==  one ) {           o++;           i++;         } else if (s.substr(i, 5) ==  twone ) {           to++;           i += 3;         } else if (s.substr(i, 3) ==  two ) {           t++;           i++;         }       }       cout << to + o + t << endl;       for (int i = 0; i < n; i++) {         if (s.substr(i, 3) ==  one ) {           cout << i + 2 <<    ;           i++;         } else if (s.substr(i, 5) ==  twone ) {           cout << i + 3 <<    ;           i += 3;         } else if (s.substr(i, 3) ==  two ) {           cout << i + 2 <<    ;           i++;         }       }       cout << endl;     }   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1024000000,1024000000 )   using namespace std;   const int N = 50 + 10, M = 1e6 + 10, inf = 1e9 + 10;   const long long INF = 5e17 + 10, mod = 1e9 + 7;   int a[N][N], cnt[N][N][N][N];   int s[N][N], sum[N][N][N][N];   int main() {     int n, m, q;     scanf( %d%d%d , &n, &m, &q);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         scanf( %1d , &a[i][j]);         s[i][j] = a[i][j] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];       }     for (int i = n; i >= 1; i--) {       for (int j = m; j >= 1; j--) {         for (int k = i; k <= n; k++) {           for (int l = j; l <= m; l++) {             int x = s[k][l] - s[i - 1][l] - s[k][j - 1] + s[i - 1][j - 1];             cnt[i][j][k][l] = cnt[i][j][k][l - 1] + cnt[i][j][k - 1][l] -                               cnt[i][j][k - 1][l - 1] + !x;           }         }       }     }     for (int i = n; i >= 1; i--) {       for (int j = m; j >= 1; j--) {         for (int k = i; k <= n; k++) {           for (int l = j; l <= m; l++) {             sum[i][j][k][l] = sum[i + 1][j][k][l] + sum[i][j + 1][k][l] -                               sum[i + 1][j + 1][k][l] + cnt[i][j][k][l];           }         }       }     }     while (q--) {       int a, b, c, d;       scanf( %d%d%d%d , &a, &b, &c, &d);       printf( %d n , sum[a][b][c][d]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char t[200002];   struct node {     int nt[5][5];   };   node st[4 * 200000 + 2];   node merge(node a, node b) {     if (a.nt[0][0] == -1) return b;     if (b.nt[0][0] == -1) return a;     node ans;     for (int i = 0; i <= 4; i++)       for (int j = 0; j <= 4; j++) ans.nt[i][j] = -1e9;     for (int i = 0; i <= 4; i++) {       for (int j = i; j <= 4; j++) {         ans.nt[i][j] = max(ans.nt[i][j], a.nt[i][j]);         ans.nt[i][j] = max(ans.nt[i][j], b.nt[i][j]);         for (int k = i; k <= j; k++) {           ans.nt[i][j] = max(ans.nt[i][j], a.nt[i][k] + b.nt[k][j]);         }       }     }     return ans;   }   void build(int n, int s, int e) {     if (s == e) {       for (int i = 0; i <= 4; i++)         for (int j = 0; j <= 4; j++) st[n].nt[i][j] = -1e9;       for (int i = 0; i <= 4; i++) st[n].nt[i][i] = 1;       if (t[s] ==  2 ) {         st[n].nt[0][1] = 1;         st[n].nt[0][0] = -1e9;       } else if (t[s] ==  0 ) {         st[n].nt[1][2] = 1;         st[n].nt[1][1] = -1e9;       } else if (t[s] ==  1 ) {         st[n].nt[2][3] = 1;         st[n].nt[2][2] = -1e9;       } else if (t[s] ==  7 ) {         st[n].nt[3][4] = 1;         st[n].nt[3][3] = -1e9;       } else if (t[s] ==  6 ) {         st[n].nt[3][3] = -1e9;         st[n].nt[4][4] = -1e9;       }     } else {       int mid = (s + e) / 2;       build(n + n, s, mid);       build(n + n + 1, mid + 1, e);       st[n] = merge(st[n + n], st[n + n + 1]);     }   }   node query(int n, int s, int e, int l, int r) {     if (s > r || l > e) {       node ans;       ans.nt[0][0] = -1;       return ans;     }     if (l <= s && e <= r) return st[n];     int mid = (s + e) / 2;     return merge(query(n + n, s, mid, l, r), query(n + n + 1, mid + 1, e, l, r));   }   int main() {     int n, q;     scanf( %d%d , &n, &q);     scanf( %s , t + 1);     build(1, 1, n);     for (int i = 1; i <= q; i++) {       int l, r;       scanf( %d%d , &l, &r);       node ans = query(1, 1, n, l, r);       if (ans.nt[0][4] < 0)         printf( -1 n );       else         printf( %d n , r - l + 1 - ans.nt[0][4]);     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, k, m, t;     cin >> n >> k >> m >> t;     while (t--) {       int x, y;       cin >> x >> y;       if (x == 1) {         if (y <= k) k++;         n++;       } else {         if (y < k)           k -= y, n -= y;         else           n = y;       }       cout << n <<     << k <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int M = 1e9 + 7;   int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};   int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};   int expo(int x, int y) {     int res = 1;     x = x % M;     while (y > 0) {       if (y & 1) res = (1ll * res * x) % M;       y = y >> 1;       x = (1ll * x * x) % M;     }     return res;   }   const int N = 1e3 + 5;   int n, m, a[1000005];   int dp[N][N];   int rec(int s, int mod) {     if (mod == 0) return 1;     if (s >= n) return 0;     if (dp[s][mod] != -1) return dp[s][mod];     dp[s][mod] = 0;     dp[s][mod] |= rec(s + 1, (mod - a[s] + m) % m);     dp[s][mod] |= rec(s + 1, mod);     return dp[s][mod];   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int t = 1;     while (t--) {       cin >> n >> m;       for (int i = 0; i < n; i++) {         cin >> a[i];         a[i] %= m;       }       if (n > m) return cout <<  YES , 0;       memset(dp, -1, sizeof dp);       int chk = rec(0, m);       if (chk)         cout <<  YES ;       else         cout <<  NO ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   int a[1003][1003];   int main() {     int n;     long long int cnt = 0;     cin >> n;     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       a[x][y] = 1;     }     long long int p, q, fl;     for (int i = 0; i <= 1000; i++) {       p = 0;       q = i;       fl = 0;       while (p <= 1000 and q <= 1000) {         if (a[p][q]) fl++;         p++;         q++;       }       cnt += (fl * (fl - 1)) / 2;       if (i) {         p = i;         q = 0;         fl = 0;         while (p <= 1000 and q <= 1000) {           if (a[p][q]) fl++;           p++;           q++;         }         cnt += (fl * (fl - 1)) / 2;       }       p = 0;       q = i;       fl = 0;       while (p <= 1000 and q >= 0) {         if (a[p][q]) fl++;         p++;         q--;       }       cnt += (fl * (fl - 1)) / 2;       if (i) {         p = i;         q = 1000;         fl = 0;         while (p <= 1000 and q >= 0) {           if (a[p][q]) fl++;           p++;           q--;         }         cnt += (fl * (fl - 1)) / 2;       }     }     cout << cnt << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<vector<pair<int, int>>> g(n);     vector<int> goes(m, -1);     vector<int> offset(m);     vector<int> from(m);     vector<int> to(m);     for (int i = 0; i < m; ++i) {       int v, u;       cin >> v >> u;       from[i] = --v;       to[i] = --u;       g[v].emplace_back(u, i);       g[u].emplace_back(v, i);     }     vector<int> pr(n, -1);     vector<int> dfn(n, -1);     vector<int> low(n, -1);     vector<int> val(n, -1);     int tt = 0;     function<void(int)> tarjan = [&](int v) {       dfn[v] = low[v] = tt++;       for (auto e : g[v]) {         int u = e.first;         int w = e.second;         if (u != pr[v]) {           if (dfn[u] == -1) {             pr[u] = v;             val[u] = w;             tarjan(u);             low[v] = min(low[v], low[u]);           } else {             low[v] = min(low[v], dfn[u]);           }         }       }       for (auto e : g[v]) {         int u = e.first;         int w = e.second;         if (pr[u] != v && dfn[v] < dfn[u]) {           vector<int> cycle;           cycle.push_back(w);           for (int t = u; t != v; t = pr[t]) {             cycle.push_back(val[t]);           }           rotate(cycle.begin(), min_element(cycle.begin(), cycle.end()),                  cycle.end());           int p = max_element(cycle.begin(), cycle.end()) - cycle.begin();           bool valid = true;           for (int i = 0; i < p - 1; ++i) {             if (cycle[i] > cycle[i + 1]) {               valid = false;               break;             }           }           for (int i = p; i < (int)cycle.size() - 1; ++i) {             if (cycle[i] < cycle[i + 1]) {               valid = false;               break;             }           }           if (valid) {             goes[cycle[p]] = cycle[0];           }         }       }     };     tarjan(0);     vector<int> dp(n, 1);     for (int i = m - 1; ~i; --i) {       int v = from[i];       int u = to[i];       int w = dp[v] + dp[u] - offset[i];       dp[v] = dp[u] = w;       if (goes[i] != -1) {         offset[goes[i]] = w;       }     }     for (int i = 0; i < n; ++i) {       if (i) {         cout <<    ;       }       cout << dp[i] - 1;     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 3 * 1e18;   long long mi[5], ma[5], low[5], high[5];   long long resx, resy, resz;   long long div2(long long x, long long y) {     if (x > 0 && y > 0 || x < 0 && y < 0) {       long long f = 0;       if (x % 2 && y % 2) f = 1;       if (x > 0 && y > 0)         return x / 2 + y / 2 + f;       else         return x / 2 + y / 2 - f;     } else       return (x + y) / 2;   }   void update(long long s, long long d) {     resy = div2(s, d);     resz = div2(s, -d);   }   bool check(long long x) {     for (int i = 1; i <= 4; i++) {       low[i] = ma[i] - x;       high[i] = mi[i] + x;       if (low[i] > high[i]) return false;     }     long long x1 = div2(low[1], -high[2]);     long long y1 = div2(high[1], -low[2]);     long long x2 = div2(low[3], -high[4]);     long long y2 = div2(high[3], -low[4]);     long long x3 = max(x1, x2);     long long y3 = min(y1, y2);     if (x3 > y3) return false;     for (long long i = x3; i <= y3; i++) {       resx = i;       long long s1 = max(low[1] - i, low[2] + i);       long long s2 = min(high[1] - i, high[2] + i);       long long d1 = max(low[3] - i, low[4] + i);       long long d2 = min(high[3] - i, high[4] + i);       if (s1 > s2 || d1 > d2) continue;       if (s1 < s2) {         if ((s1 & 1) == (d1 & 1))           update(s1, d1);         else           update(s1 + 1, d1);         return true;       } else if (d1 < d2) {         if ((s1 & 1) == (d1 & 1))           update(s1, d1);         else           update(s1, d1 + 1);         return true;       } else {         if ((s1 & 1) == (d1 & 1)) {           update(s1, d1);           return true;         }       }     }     return false;   }   int main() {     int T;     cin >> T;     while (T--) {       int n;       scanf( %d , &n);       for (int i = 1; i <= 4; i++) {         mi[i] = inf;         ma[i] = -inf;       }       for (int i = 0; i < n; i++) {         long long x, y, z;         scanf( %I64d%I64d%I64d , &x, &y, &z);         mi[1] = min(mi[1], x + y + z);         ma[1] = max(ma[1], x + y + z);         mi[2] = min(mi[2], -x + y + z);         ma[2] = max(ma[2], -x + y + z);         mi[3] = min(mi[3], x + y - z);         ma[3] = max(ma[3], x + y - z);         mi[4] = min(mi[4], -x + y - z);         ma[4] = max(ma[4], -x + y - z);       }       long long L = -1, R = inf;       while (R - L > 1) {         long long M = (L + R) >> 1;         if (check(M))           R = M;         else           L = M;       }       check(R);       printf( %I64d %I64d %I64d n , resx, resy, resz);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double E = 1e-10;   const int inf = (int)1e9;   const int maxn = 1010;   int LIM = 60 * 12;   int a[maxn];   int b[maxn], bc = 0;   int t[maxn];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , a + i);     sort(a, a + n);     int sum = 10;     for (int i = 0; i < n; i++) {       if (sum + a[i] > LIM) break;       sum += a[i];       b[bc++] = a[i];     }     t[0] = 10;     for (int i = 0; i < bc; i++) t[i + 1] = t[i] + b[i];     int res = 0;     for (int i = 0; i <= bc; i++)       if (t[i] > LIM / 2) res += t[i] - (LIM / 2);     printf( %d %d n , bc, res);     return 0;   }
#include <bits/stdc++.h>   #include <unordered_map>   #include <unordered_set>   using namespace std;   #define endl   n    #define ll long long   #define sz(s) (int)(s.size())   #define INF 0x3f3f3f3f3f3f3f3fLL   #define all(v) v.begin(),v.end()   #define watch(x) cout<<(#x)<<  =  <<x<<endl   const int dr[] { -1, -1, 0, 1, 1, 1, 0, -1 };   const int dc[] { 0, 1, 1, 1, 0, -1, -1, -1 };   #if __cplusplus >= 201402L   template<typename T>   vector<T> create(size_t n) {    return vector<T>(n);   }   template<typename T, typename ... Args>   auto create(size_t n, Args ... args) {    return vector<decltype(create<T>(args...))>(n, create<T>(args...));   }   #endif   void run() {    ios::sync_with_stdio(false);    cin.tie(NULL);    cout.tie(NULL);   #ifndef ONLINE_JUDGE    freopen( input.in ,  r , stdin);   #else    //freopen( groups.in ,  r , stdin);   #endif   }   const int N = 1e5 + 9;   int l[N], r[N];   ll dp[N][2];   vector<vector<int>> adj;   ll dfs(int node, int par, bool b) {    ll &rt = dp[node][b];    if (~rt)     return rt;    rt = 0;    int myVal = (b ? r[node] : l[node]);    for (int ch : adj[node])     if (ch != par)      rt += max(dfs(ch, node, 0) + abs(l[ch] - myVal),        dfs(ch, node, 1) + abs(r[ch] - myVal));    return rt;   }      int main() {    run();    int T;    cin >> T;    while (T--) {     int n;     cin >> n;     memset(dp, -1, (sizeof dp[0]) * (n + 1));     for (int i = 1; i <= n; i++)      cin >> l[i] >> r[i];     adj = vector<vector<int>>(n + 1);     for (int i = 1; i < n; i++) {      int u, v;      cin >> u >> v;      adj[u].push_back(v);      adj[v].push_back(u);     }     cout << max(dfs(1, -1, 0), dfs(1, -1, 1)) << endl;    }   }