malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   bool a[2000][2000], b[2000][2000], c[2000][2000];   int n, m;   void jian() {     memcpy(c, b, sizeof(b));     for (int i = 1; i < n - 1; i++) {       for (int j = 1; j < m - 1; j++) {         if (!b[i + 1][j] || !b[i][j + 1] || !b[i - 1][j] || !b[i][j - 1])           c[i][j] = 0;       }     }     memcpy(b, c, sizeof(b));   }   void jia() {     memcpy(c, b, sizeof(b));     for (int i = 1; i < n - 1; i++) {       for (int j = 1; j < m - 1; j++) {         if (b[i + 1][j] || b[i][j + 1] || b[i - 1][j] || b[i][j - 1]) {           c[i][j] = 1;         }       }     }     memcpy(b, c, sizeof(b));   }   bool vis[2000][2000];   int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};   int cnt, ans[20000];   pair<int, int> q1[6000000], q2[6000000];   int h1, t1, h2, t2;   bool count(int x, int y) {     h1 = 1;     t1 = 1;     q1[t1++] = make_pair(x, y);     a[x][y] = 0;     int cnt = 0;     while (h1 < t1) {       x = q1[h1].first;       y = q1[h1].second;       h1++;       for (int i = 0; i < 4; i++) {         int tx = x + d[i][0], ty = y + d[i][1];         if (tx > n || tx < 0 || ty > m || ty < 0) continue;         if (a[tx][ty]) {           a[tx][ty] = 0;           cnt++;           q1[t1++] = make_pair(tx, ty);         }       }     }     if (cnt > 5)       return 1;     else       return 0;   }   int bfs(int x, int y) {     h2 = 1;     t2 = 1;     q2[t2++] = make_pair(x, y);     b[x][y] = 0;     int an = 0;     while (h2 < t2) {       x = q2[h2].first;       y = q2[h2].second;       h2++;       for (int i = 0; i < 4; i++) {         int tx = x + d[i][0], ty = y + d[i][1];         if (a[tx][ty]) {           an += count(tx, ty);         }         if (b[tx][ty]) {           b[tx][ty] = 0;           q2[t2++] = make_pair(tx, ty);         }       }     }     return an;   }   inline int read() {     int x = 0;     char c = getchar();     bool flag = 0;     while (c <  0  || c >  9 ) {       if (c ==  - ) flag = 1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + c -  0 ;       c = getchar();     }     return flag ? -x : x;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         int C = read();         a[i][j] = b[i][j] = C;       }     }     n += 2;     m += 2;     for (int i = 1; i <= 4; i++) jian();     for (int i = 1; i <= 8; i++) jia();     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (b[i][j]) a[i][j] = 0;       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (b[i][j]) {           ans[++cnt] = bfs(i, j);         }       }     }     sort(ans + 1, ans + cnt + 1);     cout << cnt << endl;     for (int i = 1; i <= cnt; i++) {       cout << ans[i] <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 10, MN = 2e5 + 10;   const long double eps = 1e-6;   pair<int, int> p[MN], L[MN], R[MN];   int n;   bool check(long long val) {     long long Sq = ceil(sqrt(val) + eps);     while (Sq * Sq > val) --Sq;     long long T = L[n].first - L[n].second;     if (T * T <= val) return true;     for (int i = 0; i < n; ++i) {       int x = p[i].first, l, r;       if (x < 0)         l = x, r = min(x + Sq, (long long)-x);       else         l = max(x - Sq, (long long)-x), r = x;       l = lower_bound(p, p + n, make_pair(l, -INF)) - p;       r = upper_bound(p, p + n, make_pair(r, INF)) - p;       int Mx = max(L[l].first, R[r].first);       int Mn = min(L[l].second, R[r].second);       if (Mn >= INF) return true;       T = Mx - Mn;       T *= T;       T = max(T, 1ll * Mn * Mn + 1ll * x * x);       T = max(T, 1ll * Mx * Mx + 1ll * x * x);       if (T <= val) return true;     }     return false;   }   int main() {     ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 0; i < n; ++i) cin >> p[i].first >> p[i].second;     sort(p, p + n);     L[0] = R[n] = {-INF, INF};     for (int i = 0; i < n; ++i) {       L[i + 1].first = max(L[i].first, p[i].second);       L[i + 1].second = min(L[i].second, p[i].second);     }     for (int i = n - 1; ~i; --i) {       R[i].first = max(R[i + 1].first, p[i].second);       R[i].second = min(R[i + 1].second, p[i].second);     }     long long L = 0, R = 1e18 + 10;     while (L != R) {       long long mid = L + R >> 1;       if (check(mid))         R = mid;       else         L = mid + 1;     }     cout << L <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, pair<int, int>> sm[26];   map<int, vector<pair<int, int>>> smv;   int ca[26];   vector<pair<int, int>> sh;   int main(int argc, char const *argv[]) {     int n, m, x;     cin >> n >> m >> x;     char a[n][m];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> a[i][j];         if (a[i][j] ==  S )           sh.push_back(make_pair(i, j));         else {           smv[a[i][j] -  a ].push_back({i, j});         }       }     }     int c;     cin >> c;     string s;     cin >> s;     for (int i = 0; i < s.size(); i++) {       if (isupper(s[i])) {         ca[s[i] -  A ]++;       } else {         sm[s[i] -  a ].first++;       }     }     int mn = INT_MAX, sol = 0;     for (int i = 0; i < 26; i++) {       if (sm[i].first > 0 && smv[i].size() == 0) {         cout << -1 << endl;         return 0;       }       if (ca[i] > 0 && (smv[i].size() == 0 || !sh.size())) {         cout << -1 << endl;         return 0;       }       if (ca[i] > 0 && smv[i].size() > 0 && sh.size()) {         mn = INT_MAX;         for (int k = 0; k < smv[i].size(); k++) {           for (int j = 0; j < sh.size(); j++) {             int x1 = smv[i][k].first, x2 = sh[j].first, y1 = smv[i][k].second,                 y2 = sh[j].second;             int z = pow(x1 - x2, 2) + pow(y1 - y2, 2);             mn = min(mn, z);           }         }         if (mn > x * x) {           sol += ca[i];         }       }     }     cout << sol << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[20][5000 + 5];   vector<pair<int, int> > e[20][5000 + 5];   int vis[20][5000 + 5];   int n[5000 + 5];   long long sum[20];   vector<pair<int, int> > cycle[5000 * 15 + 5];   int N;   vector<int> bit[(1 << 15) + 5];   pair<int, int> sta[5000 * 15 + 5];   int top;   int dp[(1 << 15) + 5];   void dfs(int i, int j) {     vis[i][j] = 1;     sta[++top] = make_pair(i, j);     if (e[i][j].size()) {       assert(e[i][j].size() == 1);       int x = e[i][j][0].first;       int y = e[i][j][0].second;       if (vis[x][y] == 0)         dfs(x, y);       else if (vis[x][y] == 1) {         int t = top;         vector<pair<int, int> > tmp;         do {           tmp.push_back(sta[t]);         } while (sta[t--] != make_pair(x, y));         int f = 1;         int b[20] = {};         for (auto it : tmp)           if (++b[it.first] > 1) f = 0;         if (f) cycle[++N] = tmp;       }     }     top--;     vis[i][j] = 2;   }   int main() {     int k;     scanf( %d , &k);     long long S = 0;     for (int i = 1; i <= k; i++) {       scanf( %d , &n[i]);       for (int j = 1; j <= n[i]; j++) {         scanf( %d , &a[i][j]);         sum[i] += a[i][j];         S += a[i][j];       }       sort(a[i] + 1, a[i] + 1 + n[i]);     }     if (S % k != 0) {       puts( No );       return 0;     }     S /= k;     for (int i = 1; i <= k; i++) {       long long diff = S - sum[i];       for (int j = 1; j <= n[i]; j++) {         long long f = a[i][j] + diff;         for (int c = 1; c <= k; c++) {           if (c == i && diff != 0) continue;           int id = lower_bound(a[c] + 1, a[c] + n[c] + 1, f) - a[c];           if (a[c][id] == f) {             e[i][j].push_back(make_pair(c, id));           }         }       }     }     for (int i = 1; i <= k; i++)       for (int j = 1; j <= n[i]; j++)         if (vis[i][j] == 0) dfs(i, j);     for (int i = 1; i <= N; i++) {       int b = 0;       for (auto it : cycle[i]) b |= 1 << it.first - 1;       bit[b].push_back(i);     }     memset(dp, -1, sizeof dp);     dp[0] = 0;     for (int s = 0; s <= (1 << k) - 1; s++) {       for (int t = s; t; t = (t - 1) & s)         if (bit[t].size() && dp[s ^ t] != -1) {           dp[s] = t;           break;         }     }     if (dp[(1 << k) - 1] != -1) {       vector<int> ans;       int s = (1 << k) - 1;       while (s) {         ans.push_back(bit[dp[s]][0]);         s = s ^ dp[s];       }       int id1[20] = {}, id2[20] = {};       for (auto id : ans) {         int L = cycle[id].size();         for (int i = 0; i <= L - 1; i++) {           id1[cycle[id][i].first] = cycle[id][i].second;           id2[cycle[id][i].first] = cycle[id][(i + 1) % L].first;         }       }       puts( Yes );       for (int i = 1; i <= k; i++) printf( %d %d n , a[i][id1[i]], id2[i]);     } else       puts( No );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int c[100004], i, n, t, q, w, nr, r, d, s;   char a[100004], b[100004];   int main() {     scanf( %d %d n , &n, &t);     for (i = 1; i <= n; i++) scanf( %c , &a[i]);     scanf(  n );     for (i = 1; i <= n; i++) scanf( %c , &b[i]);     for (i = 1; i <= n; i++) {       if (a[i] == b[i]) c[++nr] = i;     }     if ((n - nr) <= t) {       for (i = 1; i <= n; i++) {         if (a[i] != b[i]) {           if (a[i] !=  a  && b[i] !=  a )             printf( a );           else if (a[i] !=  b  && b[i] !=  b )             printf( b );           else             printf( c );         } else if (r < (n - t)) {           printf( %c , a[i]);           r++;         } else {           if (a[i] !=  a )             printf( a );           else             printf( b );         }       }     } else {       q = n - nr;       if (q > (2 * t))         printf( %d , -1);       else {         s = (n - nr - t) * 2;         if (s > q)           printf( %d , s);         else {           for (i = 1; i <= n; i++) {             if (a[i] != b[i]) {               if (d < s) {                 if (d % 2 == 0)                   printf( %c , a[i]);                 else                   printf( %c , b[i]);                 d++;               } else {                 if (a[i] !=  a  && b[i] !=  a )                   printf( a );                 else if (a[i] !=  b  && b[i] !=  b )                   printf( b );                 else                   printf( c );               }             } else               printf( %c , a[i]);           }         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 205;   const int mod = 1e9 + 7;   long long f[2][N][N];   int n, tot;   struct point {     long long x, y;     point(long long _x = 0, long long _y = 0) {       x = _x;       y = _y;     }     point operator-(const point &rhs) const {       return point(x - rhs.x, y - rhs.y);     }   } p[N], p2[N];   long long cross(point a, point b) { return a.x * b.y - a.y * b.x; }   long long dp(int i, int j, int op, point p[]) {     if (f[op][i][j] != -1) return f[op][i][j];     if (j - i < 2) return 1;     f[op][i][j] = 0;     for (int k = i + 1; k <= j - 1; k++) {       if (cross(p[k] - p[i], p[j] - p[i]) > 0) {         f[op][i][j] += (dp(i, k, op, p) * dp(k, j, op, p)) % mod;         f[op][i][j] %= mod;       }     }     return f[op][i][j];   }   int main() {     memset(f, -1, sizeof(f));     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %lld%lld , &p[i].x, &p[i].y);       p2[n - i - 1].x = p[i].x;       p2[n - i - 1].y = p[i].y;     }     long long ans1, ans2;     ans1 = dp(0, n - 1, 0, p);     ans2 = dp(0, n - 1, 1, p2);     printf( %lld n , max(ans1, ans2));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct T {     T *l, *r;     int v;     int c;     T(int v) : v(v), c(1) { l = r = NULL; }     T(T *a) {       l = a->l;       r = a->r;       v = a->v;       c = a->c;     }     T() {}   };   T pool[(5000000) + 10000];   int stack_size;   T *my_new(int v) { return &(pool[stack_size++] = T(v)); }   T *my_new(T *a) { return &(pool[stack_size++] = T(a)); }   int count(T *c) {     if (!c)       return 0;     else       return c->c;   }   T *update(T *c) {     if (!c) return c;     c->c = count(c->l) + count(c->r) + 1;     return c;   }   T *merge(T *a, T *b, int flag = 1) {     if (!a) return b;     if (!b) return a;     if (rand() % (count(a) + count(b)) < count(a)) {       T *R = flag ? my_new(a) : a;       R->r = merge(a->r, b, flag);       return update(R);     } else {       T *R = flag ? my_new(b) : b;       R->l = merge(a, b->l, flag);       return update(R);     }   }   pair<T *, T *> split(T *c, int k, int flag = 1) {     if (!c) return make_pair(c, c);     T *R = flag ? my_new(c) : c;     if (k <= count(c->l)) {       pair<T *, T *> s = split(c->l, k, flag);       R->l = s.second;       return make_pair(s.first, update(R));     } else {       pair<T *, T *> s = split(c->r, k - count(c->l) - 1, flag);       R->r = s.first;       return make_pair(update(R), s.second);     }   }   T *insert(T *c, int k, int value) {     pair<T *, T *> div = split(c, k, 0);     T *mono_tree = my_new(value);     return merge(merge(div.first, mono_tree, 0), div.second, 0);   }   int tmp[100010];   int tmp2[100010];   int N, M, m;   pair<T *, T *> generate_tree() {     stack_size = 0;     T *root1 = NULL;     T *root2 = NULL;     for (int i = 0; i < N; i++) root1 = insert(root1, i, tmp[i]);     for (int i = 0; i < N; i++) root2 = insert(root2, i, tmp2[i]);     return make_pair(root1, root2);   }   void write(int *arr, T *c) {     if (m >= N) return;     if (!c) return;     write(arr, c->l);     if (m < N) arr[m++] = c->v;     write(arr, c->r);   }   int main() {     scanf( %d%d , &N, &M);     for (int i = 0; i < N; i++) {       scanf( %d , &tmp[i]);     }     for (int i = 0; i < N; i++) {       scanf( %d , &tmp2[i]);     }     T *A, *B;     pair<T *, T *> hoge = generate_tree();     A = hoge.first;     B = hoge.second;     for (int i = 0; i < M; i++) {       int a;       int x, y, k;       scanf( %d , &a);       if (a == 1) {         scanf( %d%d%d , &x, &y, &k);         x--, y--;         pair<T *, T *> sub = split(A, x);         pair<T *, T *> sub2 = split(sub.second, k);         pair<T *, T *> sub3 = split(B, y);         pair<T *, T *> sub4 = split(sub3.second, k);         T *f1 = merge(sub3.first, sub2.first);         T *f2 = merge(f1, sub4.second);         B = f2;         if (stack_size > (5000000)) {           m = 0;           write(tmp, A);           m = 0;           write(tmp2, B);           pair<T *, T *> hoge = generate_tree();           A = hoge.first;           B = hoge.second;         }       } else {         scanf( %d , &x);         x--;         pair<T *, T *> sub = split(B, x);         pair<T *, T *> sub2 = split(sub.second, 1);         printf( %d n , sub2.first->v);       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     int a, b;     cin >> a >> b;     for (int i = 1; i <= a; ++i) cout << i <<    ;     cout << a + b + 1;     for (int i = a + b; i > a; --i) cout <<     << i;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1000LL * 1000LL * 1000LL + 7;   long long n, k, len;   vector<long long> a, b;   vector<vector<long long> > dp;   void init() {     cin >> n >> len >> k;     a.resize(n + 1);     b.resize(n);     for (int i = 1; i <= n; ++i) {       cin >> a[i];       b[i - 1] = a[i];     }     sort(a.begin(), a.end());     dp.resize(k + 1, vector<long long>(n + 1, 0));     for (int i = 1; i <= n; ++i) {       dp[1][i] = 1;     }   }   void solve() {     if (len <= n) {       cout << len;       return;     }     long long sum = 0;     int l = 1, r = 1;     for (int i = 2; i <= k; ++i) {       sum = 0;       l = 1;       r = 1;       while (l <= n) {         r = l;         while (r <= n && a[r] == a[l]) {           sum = (sum + dp[i - 1][r]) % mod;           ++r;         }         for (int j = l; j < r; ++j) {           dp[i][j] = (dp[i][j] + sum) % mod;         }         l = r;       }     }     long long res = 0, pos = min(len / n, k);     for (int i = 1; i <= pos; ++i) {       for (int j = 1; j <= n; ++j) {         res = (res + (((len / n - i + 1) % mod) * dp[i][j]) % mod) % mod;       }     }     if (len % n > 0) {       if (len / n < k) {         pos = len / n + 1;         for (int i = 1; i <= len % n; ++i) {           int d =               std::distance(a.begin(), lower_bound(a.begin(), a.end(), b[i - 1]));           res = (res + dp[pos][d]) % mod;         }       }       pos = min(len / n, k);       for (int i = 1; i <= pos; ++i) {         for (int j = 1; j <= len % n; ++j) {           int d =               std::distance(a.begin(), lower_bound(a.begin(), a.end(), b[j - 1]));           res = (res + dp[i][d]) % mod;         }       }     }     cout << res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     init();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200050;   int n, num[MAXN], pre[MAXN], g[MAXN], ans;   int st[MAXN];   map<int, int> f;   int main() {     int u;     ans = -1;     st[0] = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &num[i]);       if (!f.count(num[i] - 1)) {         g[i] = 1;         pre[i] = -1;       } else {         g[i] = g[pre[i] = f[num[i] - 1]] + 1;       }       if (!f.count(num[i]))         f[num[i]] = i;       else {         u = f[num[i]];         f[num[i]] = g[u] < g[i] ? i : u;       }       if (ans == -1 || g[ans] < g[i]) ans = i;     }     for (int i = ans; i != -1; i = pre[i]) st[++st[0]] = i;     printf( %d n , st[0]);     while (st[0]) printf( %d  , st[st[0]--]);     fclose(stdin);     fclose(stdout);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 400 * 1000 + 10;   vector<int> graph[N];   int n, num[N], par[N], ans[N], c1[N], c2[N];   void dfs(int v) {     num[v]++;     for (int i = 0; i < (int)graph[v].size(); i++) {       int u = graph[v][i];       if (u != par[v]) {         par[u] = v;         dfs(u);         num[v] += num[u];         if (num[u] - c1[u] > n / 2) ans[v] = 1;         c1[v] = max(c1[v], c1[u]);       }     }     if (num[v] <= n / 2) c1[v] = num[v];   }   void dfs2(int v) {     if (n - num[v] - c2[v] > n / 2) ans[v] = 1;     if (n - num[v] <= n / 2) c2[v] = n - num[v];     int mx = c2[v];     for (int i = 0; i < (int)graph[v].size(); i++) {       int u = graph[v][i];       if (u != par[v]) {         c2[u] = max(c2[u], mx);         mx = max(mx, c1[u]);       }     }     mx = 0;     for (int i = (int)graph[v].size() - 1; i >= 0; i--) {       int u = graph[v][i];       if (u != par[v]) {         c2[u] = max(c2[u], mx);         mx = max(mx, c1[u]);         dfs2(u);       }     }   }   int main() {     cin >> n;     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       graph[a].push_back(b);       graph[b].push_back(a);     }     par[0] = -1;     dfs(0);     dfs2(0);     for (int i = 0; i < n; i++) cout << 1 - ans[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 300050;   const long long LINF = 0x3f3f3f3f3f3f3f3f;   map<int, int> odd, even;   int buf[MAXN], pre[MAXN];   int main() {     ios::sync_with_stdio(false);     long long n;     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> buf[i];       pre[i] = pre[i - 1] ^ buf[i];     }     long long res = 0;     even[0]++;     for (int i = 1; i <= n; i++) {       if (i % 2 == 1) {         res += odd[pre[i]];         odd[pre[i]]++;       } else {         res += even[pre[i]];         even[pre[i]]++;       }     }     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   void out(T x) {     cout << x << endl;     exit(0);   }   void relax(int &x, int y) { x = max(x, y); }   const int inf = 1e9;   int n, k;   int dp[20][1 << 18][2][2];   int like[1 << 18];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     for (int i = 0; i <= n + 1; i++) {       for (int j = 0; j < 1 << n; j++) {         for (int a = 0; a < 2; a++) {           for (int b = 0; b < 2; b++) {             dp[i][j][a][b] = -inf;           }         }       }     }     while (k--) {       int i;       cin >> i;       --i;       like[i] = 1;     }     for (int i = 0; i < 1 << n; i += 2) {       for (int a = 0; a < 2; a++) {         for (int b = 0; b < 2; b++) {           if (like[i] + like[i + 1] == a + b) {             relax(dp[1][i][a][b], a || b);           }         }       }     }     for (int d = 2; d <= n; d++) {       for (int i = 0; i < 1 << n; i += (1 << d)) {         int j = i + (1 << (d - 1));         for (int a = 0; a < 2; a++) {           for (int b = 0; b < 2; b++) {             if (dp[d - 1][i][a][b] >= 0) {               for (int x = 0; x < 2; x++) {                 for (int y = 0; y < 2; y++) {                   if (dp[d - 1][j][x][y] >= 0) {                     bool ub = (a || x);                     bool lb = (b || y);                     bool lb2 = (a && x) || lb;                     relax(dp[d][i][ub][lb], ub + lb + lb2 + dp[d - 1][i][a][b] +                                                 dp[d - 1][j][x][y]);                   }                 }               }             }           }         }       }     }     int res = 0;     for (int a = 0; a < 2; a++) {       for (int b = 0; b < 2; b++) {         relax(res, dp[n][0][a][b] + (a || b));       }     }     out(res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long double PI = 3.14159265359;   const long long MOD = (long long)998244353ll;   const long long MAXN = (long long)2e5 + 10;   const long long INF = (long long)2242545357980376863;   const long double EPS = (long double)1e-8;   long long su = 0;   long long a[MAXN], dp1[MAXN], dp2[MAXN], dp3[MAXN];   vector<long long> G[MAXN];   void DFS(long long u, long long p) {     dp1[u] = 0;     dp2[u] = 0;     for (auto adj : G[u]) {       if (adj == p) continue;       DFS(adj, u);       dp1[u] += dp1[adj];       dp2[u] += dp2[adj];     }     dp2[u] += dp1[u];     dp1[u] += a[u];   }   void DFS2(long long u, long long p, long long v) {     dp3[u] = v;     long long tot = 0;     for (auto adj : G[u]) {       if (adj == p) continue;       DFS2(adj, u, (v + dp2[u] - (dp2[adj] + dp1[adj])) + (su - dp1[adj]));     }     return;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) su += a[i];     long long u, v;     for (int i = 1; i < n; i++) {       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     DFS(1, -1);     DFS2(1, -1, 0);     long long ans = 0;     for (int i = 1; i <= n; i++) ans = max(ans, dp2[i] + dp3[i]);     cout << ans;     return 0;   }