malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxN = 1000;   const int mod = 1000 * 1000 + 3;   int toInt(string x) {     stringstream second;     second << x;     int ret;     second >> ret;     return ret;   }   string Ex(int x) {     for (int l = 1, p = 26;; l++, p *= 26)       if (x <= p) {         x--;         string res;         for (int i = 0; i < l; i++) res += string(1,  A  + x % 26), x /= 26;         reverse(res.begin(), res.end());         return res;       } else         x -= p;   }   int rEx(string x) {     int res = 0;     for (int l = 1, p = 26; l < x.size(); l++, p *= 26) res += p;     reverse(x.begin(), x.end());     for (int i = 0, p = 1; i < x.size(); i++, p *= 26) res += p * (x[i] -  A );     return res + 1;   }   int main() {     int t;     cin >> t;     while (t-- > 0) {       string s;       cin >> s;       int valid = 0;       for (int i = 0; i < s.size() - 1; i++)         if ( 0  <= s[i] && s[i] <=  9  && s[i + 1] ==  C ) valid = i + 1;       if (valid) {         int x = toInt(s.substr(1, valid - 1));         int y = toInt(s.substr(valid + 1));         cout << Ex(y) << x << endl;       } else         for (int i = 0; i < s.size(); i++)           if ( 0  <= s[i] && s[i] <=  9 ) {             cout <<  R  << s.substr(i) <<  C  << rEx(s.substr(0, i)) << endl;             break;           }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long tt;     cin >> tt;     while (tt--) {       long long n;       cin >> n;       vector<long long> v(n);       for (auto &i : v) cin >> i;       bool ans = false;       for (size_t i = 0; i < n; i++) {         if (v[i] >= v[i - 1]) {           ans = true;           break;         }       }       cout << ((ans) ?  YES  :  NO ) <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     map<char, vector<int> > m;     for (int j = 0; j < s.size(); j++) {       m[s[j]].push_back(j);     }     for (int i = 0; i < n; i++) {       string p;       cin >> p;       long long f = 0;       for (int j = 0; j < p.size(); j++) {         if (m.find(p[j]) == m.end()) {           f += p.size();           continue;         }         vector<int> v = m[p[j]];         if (v[0] >= j) {           f += v[0] - j;           continue;         }         if (v[v.size() - 1] <= j) {           f += j - v[v.size() - 1];           continue;         }         if (binary_search(v.begin(), v.end(), j)) {           continue;         }         vector<int>::iterator it1 = lower_bound(v.begin(), v.end(), j);         vector<int>::iterator it2 = upper_bound(v.begin(), v.end(), j);         it1--;         if (it2 == v.end()) it2--;         f += min(abs((*it1) - j), abs((*it2) - j));       }       cout << f <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const long long p = 998244353;   long long n, ans, a[1000010], c[1000010], cnt[1000010];   long long ksm(long long x, long long y) {     long long xlh = 1;     while (y) {       if (y & 1) xlh = xlh * x % p;       x = x * x % p;       y /= 2;     }     return xlh;   }   int main() {     long long i, j, x;     scanf( %lld , &n);     for (i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       for (j = 1; j <= a[i]; j++) {         scanf( %lld , &x);         cnt[x]++;         c[x] = (c[x] + ksm(a[i], p - 2) * ksm(n, p - 2) % p) % p;       }     }     for (i = 1; i <= 1000000; i++)       if (cnt[i]) {         ans = (ans + c[i] * cnt[i] % p * ksm(n, p - 2) % p) % p;       }     printf( %lld , ans);   }
#include  bits/stdc++.h    #include <cstring>   using namespace std;   typedef long long ll;   #define FastIO cin.sync_with_stdio(false), cin.tie(nullptr)   #define pb push_back   #define fs first   #define sc second   #define mkp make_pair   #define endl   n       // IO   #define multitest signed __T; cin >> __T; while(__T--)   template<typename T>   void _read(T& t);   template<typename T>   void _read(vector<T>& v);   template<typename T1, typename T2>   void _read(pair<T1, T2>& p);   template<typename T>   void _read(T& t) { cin >> t; }   template<typename T>   void _read(vector<T>& v) { for (unsigned _i = 0; _i < v.size(); _i++)_read(v[_i]); }   template<typename T1, typename T2>   void _read(pair<T1, T2>& p) { _read(p.first); _read(p.second); }   void _masterread() {}   template<typename T, typename... V>   void _masterread(T& t, V& ... v) { _read(t); _masterread(v...); }   #define re(...)_masterread(__VA_ARGS__)      template<typename T>   void _print(T t);   template<typename T>   void _print(vector<T>& v);   template<typename T1, typename T2>   void _print(pair<T1, T2>& p);   template<typename T>   void _print(T t) { cout << t; }   template<typename T>   void _print(vector<T>& v) { for (unsigned _i = 0; _i < v.size(); _i++)_print(v[_i]), cout << (_i == v.size() - 1 ?    :    ); }   template<typename T1, typename T2>   void _print(pair<T1, T2>& p) { _print(p.first); cout <<    ; _print(p.second); }   void _masterprint() { cout << endl; }   template<typename T, typename... V>   void _masterprint(T t, V... v) { _print(t); if (sizeof...(v))cout <<    ; _masterprint(v...); }   #define pr(...)_masterprint(__VA_ARGS__)      // DEBUG   // colored output???   template <typename T> // start forward declaration   void _debug(T t);   template<typename T1, typename T2>   void _debug(pair<T1, T2> p);   template<typename T>   void _debug(vector<T>v);   template <typename T> // end forward declaration   void _debug(T t) { cerr << t; }   template<typename T1, typename T2>   void _debug(pair<T1, T2> p) { cerr <<  { ; _debug(p.first); cerr <<  ,  ; _debug(p.second); cerr <<  } ; }   template<typename T>   void _debug(vector<T>v) { cerr <<  ( ; for (unsigned _i = 0; _i < v.size(); _i++)_debug(v[_i]), cerr << (_i == v.size() - 1 ?    :  ,  ); cerr <<  ) ; }   void _masterdebug() { cerr <<  ] n ; }   template<typename T, typename... V>   void _masterdebug(T t, V... v) { _debug(t); if (sizeof...(v))cerr <<  ,  ; _masterdebug(v...); }   #define debug(...) cerr<< [ <<#__VA_ARGS__<< ] = [ ;_masterdebug(__VA_ARGS__)   #define int long long      vector<int> A, D;   void rec(int l, int r, int d) {    if (l > r) {     return;    }    if (l == r) {     D[l] = d;     return;    }    int mx = l;    for (int i = l; i <= r; i++) {     if (A[i] > A[mx]) {      mx = i;     }    }    D[mx] = d;    rec(l, mx - 1, d + 1);    rec(mx + 1, r, d + 1);   }      void solve() {    int n; re(n);    A = vector<int>(n);  re(A);    D = vector<int>(n);    rec(0, n - 1, 0);    pr(D);      }      signed main() {    FastIO;    int t;    cin >> t;    while(t--)      solve();   }
#include <bits/stdc++.h>   using std::cin;   using std::cout;   using std::endl;   using std::set;   using std::swap;   using std::vector;   void TaskA() {     vector<char> first;     vector<char> second;     char a;     int f, s;     for (int i = 0; i < 4; i++) {       cin >> a;       first.push_back(a);       if (a ==  A ) f = i;     }     swap(first[2], first[3]);     if (f == 2)       f = 3;     else if (f == 3)       f = 2;     for (int i = 0; i < 4; i++) {       cin >> a;       second.push_back(a);       if (a ==  A ) s = i;     }     swap(second[2], second[3]);     if (s == 2)       s = 3;     else if (s == 3)       s = 2;     if (first[(f + 1) % 4] ==  X ) f = (f + 1) % 4;     if (second[(s + 1) % 4] ==  X ) s = (s + 1) % 4;     if (first[(f + 1) % 4] == second[(s + 1) % 4])       cout <<  YES ;     else       cout <<  NO ;   }   int main() {     TaskA();     return 0;   }
#include<bits/stdc++.h>   typedef long long int ll;   #define io ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);   #define fr(i,l,r) for(ll i=l;i<r;i++)   #define lop ll t;cin>>t;while(t--)   #define maxi 1000   #define mod 1000000007   #define f first   #define s second   ll dp[maxi];   using namespace std;         /*      a1 + a2 +... +an = k*m     long n,k;          cin>>n>>k;if(k<n){      k=n/k*k+(n%k!=0)*k;     }     cout<<k/n+(k%n!=0)<<endl;               */         void solve(){       ll n , k ; cin >> n >> k ;       if(k < n) k = k*(n/k) + (n%k != 0)*k ;       cout << k/n + (k%n != 0) << endl ;   }         int main(){       io;       int t ; cin >> t ;       while(t--){           solve() ;       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, sum = 0;     cin >> n >> k;     pair<int, int> p[n];     for (int i = 0; i < n; i++) {       cin >> p[i].first;       p[i].second = i + 1;     }     sort(p, p + n);     for (int i = 0; i < n; i++) {       k -= p[sum].first;       if (k >= 0)         sum++;       else         break;     }     cout << sum << endl;     for (int i = 0; i < sum; i++) {       cout << p[i].second <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 59;   struct State {     char c[MAX_N][MAX_N];   };   int H, W;   vector<pair<int, int>> solve(State S) {     vector<pair<int, int>> D;     for (int t = 1; t <= 100000; t++) {       bool flag = false;       if (H % 2 == 0) {         for (int i = 1; i <= H; i++) {           for (int j = 1; j <= W; j++) {             if (i % 2 == 1 && S.c[i][j] !=  U ) flag = true;             if (i % 2 == 0 && S.c[i][j] !=  D ) flag = true;           }         }       } else {         for (int i = 1; i <= H; i++) {           for (int j = 1; j <= W; j++) {             if (j % 2 == 1 && S.c[i][j] !=  L ) flag = true;             if (j % 2 == 0 && S.c[i][j] !=  R ) flag = true;           }         }       }       if (flag == false) return D;       for (int i = 1; i <= H - 1; i++) {         for (int j = 1; j <= W - 1; j++) {           if (t % 2 == 1) {             if (S.c[i][j] ==  U  && S.c[i][j + 1] ==  U  &&                 S.c[i + 1][j] ==  D  && S.c[i + 1][j + 1] ==  D ) {               S.c[i + 0][j + 0] =  L ;               S.c[i + 0][j + 1] =  R ;               S.c[i + 1][j + 0] =  L ;               S.c[i + 1][j + 1] =  R ;               D.push_back(make_pair(i, j));             }           }           if (t % 2 == 0) {             if (S.c[i][j] ==  L  && S.c[i][j + 1] ==  R  &&                 S.c[i + 1][j] ==  L  && S.c[i + 1][j + 1] ==  R ) {               S.c[i + 0][j + 0] =  U ;               S.c[i + 0][j + 1] =  U ;               S.c[i + 1][j + 0] =  D ;               S.c[i + 1][j + 1] =  D ;               D.push_back(make_pair(i, j));             }           }         }       }     }     return D;   }   int main() {     cin >> H >> W;     State T, U;     for (int i = 1; i <= H; i++) {       for (int j = 1; j <= W; j++) cin >> T.c[i][j];     }     vector<pair<int, int>> D1 = solve(T);     for (int i = 1; i <= H; i++) {       for (int j = 1; j <= W; j++) cin >> U.c[i][j];     }     vector<pair<int, int>> D2 = solve(U);     vector<pair<int, int>> D3 = D1;     for (int i = D2.size() - 1; i >= 0; i--) D3.push_back(D2[i]);     cout << D3.size() << endl;     for (int i = 0; i < D3.size(); i++)       cout << D3[i].first <<     << D3[i].second << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)2 * 1e9;   const long long MOD = (long long)1e9 + 7;   const long long P = 239;   const long long MAX_N = 100000;   const long long ABC = 26;   const long double pi = 3.1415926535897932384626;   const long double eps = 1e-6;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.precision(20);     long long c, h1, h2, w1, w2;     cin >> c >> h1 >> h2 >> w1 >> w2;     if (w2 > w1) {       swap(h1, h2);       swap(w1, w2);     }     if (w1 * w1 >= c) {       long long ans = 0;       for (long long i = 0; i * w1 <= c; i++)         ans = max(ans, i * h1 + (c - i * w1) / w2 * h2);       cout << ans;     } else {       if (h1 * w2 < h2 * w1) {         swap(h1, h2);         swap(w1, w2);       }       long long ans = 0;       for (long long i = 0; i < w1 && i * w2 <= c; i++)         ans = max(ans, i * h2 + (c - i * w2) / w1 * h1);       cout << ans;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1000006, maxm = 1005;   const long long mod = 1000000007;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long powermod(long long a, long long b) {     a = a;     long long ans = 1;     while (b) {       if (b % 2) ans = (ans * a);       b = b / 2;       a = (a * a);     }     return ans;   }   long long a[100005];   long long flag[100005];   int main() {     long long t;     scanf( %lld , &t);     while (t--) {       long long n, m, i, j, k;       scanf( %lld%lld , &n, &k);       long long vis1 = 0, vis2 = 0;       for (i = 1; i <= n; i++) {         scanf( %lld , &a[i]);         if (a[i] == k) vis1 = 1;         if (i >= 2) {           if (a[i] >= k && (a[i - 1] >= k || a[i - 2] >= k)) vis2 = 1;         }       }       if ((vis1 == 1 && vis2 == 1) || (n == 1 && vis1 == 1))         printf( yes n );       else         printf( no n );     }   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > e;   vector<long long> val;   vector<long long> mp, mn;   vector<int> deg;   vector<bool> vis;   pair<long long, long long> dfs(int x) {     vis[x] = true;     long long mx = 0, mi = 0;     for (int to : e[x]) {       if (!vis[to]) {         auto p = dfs(to);         mx = max(mx, p.first);         mi = max(mi, p.second);       }     }     long long d = val[x] + mx - mi;     mp[x] = mx;     mn[x] = mi;     if (d > 0)       mi += d;     else       mx -= d;     return make_pair(mx, mi);   }   int main(int argc, char **argv) {     int n;     scanf( %d , &n);     deg = vector<int>(n);     mp = mn = val = vector<long long>(n);     vis = vector<bool>(n);     e = vector<vector<int> >(n);     for (int i = 0; i < n - 1; i++) {       int a, b;       scanf( %d %d , &a, &b);       e[a - 1].push_back(b - 1);       e[b - 1].push_back(a - 1);       deg[a - 1]++;       deg[b - 1]++;     }     for (int i = 0; i < n; i++) {       int t;       scanf( %d , &t);       val[i] = t;     }     auto p = dfs(0);     cout << p.first + p.second << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long N, x, y, awal, centre, nb, a;   bool visited[100005];   vector<long long> adj[100005];   vector<int> ans;   void dfs(int x) {     int s = adj[x].size();     visited[x] = 1;     bool cek = 1;     for (int i = 0; i < s; i++) {       nb = adj[x][i];       if (!visited[nb]) {         cek = 0;         dfs(nb);       }     }     if (cek) ans.push_back(x);   }   int main() {     cin >> N;     for (int i = 1; i < N; i++) {       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     for (int i = 1; i <= N; i++) {       if (adj[i].size() >= 3) {         awal = i;         centre++;       }     }     if (centre > 1)       cout <<  No n ;     else {       if (centre == 0) {         dfs(1);         cout <<  Yes n  << 1 <<   n ;         for (int i = 1; i <= N; i++) {           if (adj[i].size() == 1) {             cout << i;             a++;             if (a == 1) cout <<    ;           }         }         cout <<   n ;       }       if (centre == 1) {         dfs(awal);         cout <<  Yes n  << ans.size() <<   n ;         for (int i = 0; i < ans.size(); i++) {           cout << awal <<     << ans[i] <<   n ;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100100;   const long long INF = 1000 * 1000 * 1000LL * 1000 * 1000;   vector<pair<int, int> > adj1[N];   vector<pair<long long, pair<int, int> > > e;   set<pair<long long, int> > s;   long long d[N];   int p[N], par[N], sz[N], flag(1);   void Dijkstra() {     while (s.size()) {       auto u = *s.begin();       s.erase(u);       for (auto v : adj1[u.second]) {         long long w = u.first + v.second;         if (w < d[v.first]) {           s.erase({d[v.first], v.first});           d[v.first] = w;           p[v.first] = p[u.second];           s.insert({d[v.first], v.first});         }       }     }     return;   }   int findpar(int v) {     if (v == par[v]) return v;     return (par[v] = findpar(par[v]));   }   bool merge(int u, int v) {     u = findpar(u), v = findpar(v);     if (u == v) return 0;     if (sz[u] < sz[v]) swap(u, v);     sz[u] += sz[v];     par[v] = u;     return 1;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n, m, k, u, v, w;     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> u >> v >> w;       adj1[--u].push_back({--v, w});       adj1[v].push_back({u, w});     }     fill(d, d + n, INF);     cin >> k;     for (int i = 0; i < k; i++) {       cin >> u;       d[--u] = 0;       if (!u) flag = 0;     }     for (int i = 0; i < n; i++) {       p[i] = i;       par[i] = i, sz[i] = 1;       s.insert({d[i], i});     }     Dijkstra();     for (int i = 0; i < n; i++)       for (auto u : adj1[i])         if (p[i] < p[u.first]) {           long long w = d[i] + d[u.first] + u.second;           e.push_back({w, {p[i], p[u.first]}});         }     int cmp = k;     long long ans(0), ans2(0);     sort(e.begin(), e.end());     for (int i = 0; i < e.size() && cmp > 1; i++) {       if (merge(e[i].second.first, e[i].second.second)) {         cmp--;         ans += e[i].first;       }     }     if (flag) ans += d[0];     cout << ((cmp == 1) ? ans : -1) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     map<int, int> mp;     vector<int> l(n), a(n);     for (int i = 0; i < n; i++) cin >> l[i];     vector<int> r(n);     for (int i = 0; i < n; i++) {       cin >> r[i];       a[i] = l[i] + r[i];       mp[a[i]]++;     }     int curr = 1;     map<int, int> mp2;     for (auto it = mp.rbegin(); it != mp.rend(); it++) {       mp2[it->first] = curr++;     }     for (int i = 0; i < n; i++) {       a[i] = mp2[a[i]];     }     for (int i = 0; i < n; i++) {       int cnt1 = 0, cnt2 = 0;       for (int j = 0; j < n; j++) {         if (j == i) continue;         if (a[j] > a[i]) {           if (j > i)             cnt2++;           else             cnt1++;         }       }       if (cnt1 != l[i] || cnt2 != r[i]) {         cout <<  NO n ;         return 0;       }     }     cout <<  YES n ;     for (auto it : a) cout << it <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve();   int main(int argc, char const *argv[]) { solve(); }   void solve() {     map<int, int> arr;     cin >> arr[0] >> arr[1] >> arr[2] >> arr[3];     int total = arr[0] + arr[1] + arr[2] + arr[3];     for (int st = 0; st < 4; st++)       if (arr[st]) {         vector<int> res;         auto t = arr;         t[st]--;         res.push_back(st);         int last = st;         for (int i = 0; i < total - 1; i++) {           if (t[last - 1]) {             t[last - 1]--;             res.push_back(last - 1);             last--;           } else if (t[last + 1]) {             t[last + 1]--;             res.push_back(last + 1);             last++;           } else             break;         }         if ((int)res.size() == total) {           cout <<  YES n ;           for (int i : res) printf( %d  , i);           cout << endl;           return;         }       }     cout <<  NO n ;     return;   }
#include <bits/stdc++.h>   using namespace std;   enum { EMPTY, LINE, INF };   int Type(int a, int b, int c) {     if (a == 0 && b == 0)       if (c == 0)         return INF;       else         return EMPTY;     return LINE;   }   void Exit(int ans) {     printf( %d n , ans);     exit(0);   }   int main() {     int a1, b1, c1;     int a2, b2, c2;     scanf( %d%d%d , &a1, &b1, &c1);     scanf( %d%d%d , &a2, &b2, &c2);     int t1 = Type(a1, b1, c1);     int t2 = Type(a2, b2, c2);     if (t1 == EMPTY || t2 == EMPTY) Exit(0);     if (t1 == INF || t2 == INF) Exit(-1);     if (a1 * b2 - a2 * b1 != 0) Exit(1);     if (a1 * c2 - a2 * c1 == 0 && b1 * c2 - b2 * c1 == 0) Exit(-1);     Exit(0);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     long long i, j, x, n, m, l, r;     cin >> n >> m;     vector<long long> v;     for (i = 0; i < n; i++) {       cin >> x;       v.push_back(x);     }     for (i = 0; i < m; i++) {       cin >> l >> r >> x;       long long c = 0;       if (x < l || x > r) {         cout <<  Yes n ;         continue;       }       for (j = l - 1; j < r; j++) {         if (v[j] < v[x - 1]) {           c++;         }       }       if (c == (x - l)) {         cout <<  Yes n ;       } else {         cout <<  No n ;       }     }   }
#include <bits/stdc++.h>   int main() {     int n, a, b;     while (scanf( %d%d%d , &n, &a, &b) != EOF) {       int c[202] = {0};       int t = 1, i, j, k;       if (b >= 0) {         for (int i = 1; i <= n; i++) c[i] = i;         for (int j = n + 1; j <= 2 * n; j++) c[j] = c[j - n];         printf( %d n , c[a + (b % n)]);       }       if (b < 0) {         for (i = 1; i <= n; i++) c[i] = i;         for (j = n + 1; j <= 2 * n; j++) c[j] = c[j - n];         printf( %d n , c[a + n - (-b) % n]);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<pair<long long, long long>, long long> cost;   map<long long, int> vis;   map<long long, int> freq;   int main() {     int q;     cin >> q;     while (q--) {       int e;       long long v, u;       cin >> e >> v >> u;       long long tv = v;       long long tu = u;       if (e == 1) {         long long w;         cin >> w;         if (u == v) continue;         while (tv > 0) {           freq[tv]++;           tv /= 2;         }         while (tu > 0) {           ++freq[tu];           tu /= 2;         }         tv = v;         tu = u;         while (freq[tv] == 1 && tv > 1) {           cost[make_pair(tv, tv / 2)] += w;           tv /= 2;         }         while (freq[tu] == 1 && tu > 1) {           cost[make_pair(tu, tu / 2)] += w;           tu /= 2;         }       } else {         long long ans = 0;         while (tv > 0) {           freq[tv]++;           tv /= 2;         }         while (tu > 0) {           ++freq[tu];           tu /= 2;         }         tv = v;         tu = u;         while (freq[tv] == 1 && tv > 1) {           ans += cost[make_pair(tv, tv / 2)];           tv /= 2;         }         while (freq[tu] == 1 && tu > 1) {           ans += cost[make_pair(tu, tu / 2)];           tu /= 2;         }         cout << ans << endl;       }       freq.clear();     }   }
#include <iostream>   #include <vector>      using namespace std;      int l[200005], r[200005];   vector<int> e[200005];   long long int dp[2][200005];      void dfs(int u, int fa) {       dp[0][u] = dp[1][u] = 0;       for (auto v : e[u]) {           if (v == fa) continue;           dfs(v, u);           dp[0][u] += max(dp[0][v] + abs(l[u] - l[v]), dp[1][v] + abs(l[u] - r[v]));           dp[1][u] += max(dp[0][v] + abs(r[u] - l[v]), dp[1][v] + abs(r[u] - r[v]));       }   }      int main(void) {       int t;       scanf( %d , &t);       while (t--) {           int n;           scanf( %d , &n);           for (int i = 1;i <= n;i++) scanf( %d %d , &l[i], &r[i]);           for (int i = 1;i <= n;i++) e[i].clear();           for (int i = 0;i < n - 1;i++) {               int u, v;               scanf( %d %d , &u, &v);               e[u].emplace_back(v);               e[v].emplace_back(u);           }           dfs(1, -1);           printf( %lld n , max(dp[0][1], dp[1][1]));       }   }
#include <bits/stdc++.h>   using namespace std;   class Solution {    public:     void solve(std::istream& in, std::ostream& out) {       int w, h;       in >> w >> h;       vector<string> m(h);       for (string& s : m) in >> s;       for (int i = 0; i < 2 * w; ++i) {         for (int j = 0; j < 2 * h; ++j) out << m[j / 2][i / 2];         out <<   n ;       }     }   };   void solve(std::istream& in, std::ostream& out) {     out << std::setprecision(12);     Solution solution;     solution.solve(in, out);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     istream& in = cin;     ostream& out = cout;     solve(in, out);     return 0;   }