#include <bits/stdc++.h>   using namespace std;   int N, M;   long long K;   int A[505][505];   vector<int> goods, bads;   int remap[505];   double P[505][505];   typedef double matrix[105][105];   void mult(matrix A, matrix B, matrix C) {     matrix temp;     memset(temp, 0, sizeof(temp));     for (int i = 0; i < 105; i++)       for (int j = 0; j < 105; j++)         for (int k = 0; k < 105; k++) temp[i][j] += A[i][k] * B[k][j];     memcpy(C, temp, sizeof(matrix));   }   matrix Q, R;   int V[505];   int main() {     cin >> N >> M >> K;     for (int i = 1; i <= N; i++) {       int x;       cin >> x;       if (x || i == 1)         bads.push_back(i);       else         goods.push_back(i);     }     for (int i = 0; i < bads.size(); i++) remap[bads[i]] = i;     for (int i = 0; i < goods.size(); i++) remap[goods[i]] = bads.size() + i;     for (int i = 0; i < M; i++) {       int u, v;       cin >> u >> v;       u = remap[u], v = remap[v];       A[u][v]++;       A[v][u]++;     }     for (int i = 0; i < N; i++) {       double deg = 0;       for (int j = 0; j < N; j++) deg += A[i][j];       for (int j = 0; j < N; j++) P[i][j] += A[i][j] / deg;     }     while (N > bads.size()) {       N--;       for (int i = 0; i < N; i++) P[N][i] /= (1.0 - P[N][N]);       for (int i = 0; i < N; i++)         for (int j = 0; j < N; j++) P[i][j] += P[i][N] * P[N][j];     }     N--;     for (int i = 1; i <= N; i++) P[0][i] /= (1.0 - P[0][0]);     for (int i = 1; i <= N; i++)       for (int j = 1; j <= N; j++) Q[i][j] = P[i][j] + P[i][0] * P[0][j];     for (int i = 1; i <= N; i++) R[i][i] = 1;     K -= 2;     for (int b = 0; b <= 63; b++) {       if (K & (1LL << b)) mult(Q, R, R);       mult(Q, Q, Q);     }     double total = 0;     for (int i = 1; i <= N; i++) total += P[0][i] * R[i][N];     cout << total <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long arr[n];     int i;     map<long long, int> mp;     for (i = 0; i < n; i++) {       int t;       cin >> t;       mp[t]++;     }     auto it = mp.begin();     int maxe = 0;     while (it != mp.end()) {       if (it->second > maxe) maxe = it->second;       it++;     }     cout << n - maxe << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int count0(0), count5(0);     for (int i = 1; i <= n; i++) {       int a;       cin >> a;       if (a == 0) count0++;       if (a == 5) count5++;     }     if (count0 == 0)       cout << -1;     else if (count5 < 9)       cout << 0;     else {       for (int i = 0; i < floor(count5 / 9); i++) {         cout << 555555555;       }       for (int i = 0; i < count0; i++) cout << 0;     }   }


#include <bits/stdc++.h>   using namespace std;   void OUT_TST(string s, istream_iterator<string> it) { cerr << s; }   template <typename T, typename... Args>   void OUT_TST(string s, istream_iterator<string> it, T a, Args... args) {     cerr << *it <<  =  << a <<    ;     OUT_TST(s, ++it, args...);   }   template <class T>   void rd(T& ret) {     ret = 0;     bool ok = 0, u = 0;     for (;;) {       int c = getchar();       if (c >=  0  && c <=  9 )         ret = (ret << 3) + (ret << 1) + c -  0 , ok = 1;       else if (c ==  - )         u = 1;       else if (ok) {         if (u) ret *= -1;         return;       }     }   }   long long powmod(long long p, long long n, long long mod) {     long long ret = 1;     for (; n; n >>= 1) {       if (n & 1) ret = ret * p % mod;       p = p * p % mod;     }     return ret;   }   template <class T>   bool chmin(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   template <class T>   bool chmax(T& a, const T& b) {     return b > a ? a = b, 1 : 0;   }   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   const int INF = (int)1e8;   void input(vector<vector<int>>& son, vector<int>& an, vector<int>& sz, int n) {     int m;     cin >> m;     son.resize(m);     an.resize(m, -1);     sz.resize(m, 0);     for (int i = 0; i < m - 1; ++i) {       int t;       cin >> t;       --t;       son[t].push_back(i + 1);     }     for (int i = 0; i < n; ++i) {       int t;       cin >> t;       --t;       an[t] = i;     }   }   pair<int, int> dfs(int first, const vector<vector<int>>& son,                      const vector<int>& idx, vector<vector<pair<int, int>>>& cost,                      vector<int>& sz) {     sz[first] = 1;     if (idx[first] != -1) {       cost[idx[first]].emplace_back(idx[first], 1);       return {idx[first], idx[first]};     }     int mi = INF, mx = -INF;     pair<int, int> tmp;     for (int second : son[first]) {       tmp = dfs(second, son, idx, cost, sz);       sz[first] += sz[second];       chmin(mi, tmp.first);       chmax(mx, tmp.second);     }     int c = sz[first];     if (first == 0) --c;     cost[mi].emplace_back(mx, c);     return {mi, mx};   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr);     int n;     cin >> n;     vector<int> an, bn, sza, szb;     vector<vector<int>> sa, sb;     input(sa, an, sza, n);     input(sb, bn, szb, n);     vector<vector<pair<int, int>>> cost(n);     auto tmp = dfs(0, sa, an, cost, sza);     assert(tmp.first == 0 && tmp.second == n - 1);     tmp = dfs(0, sb, bn, cost, szb);     assert(tmp.first == 0 && tmp.second == n - 1);     vector<int> dp(n + 1, 0);     for (auto& i : cost[0]) {       chmax(dp[i.first], i.second);     }     for (int i = 0; i < n - 1; ++i) {       chmax(dp[i + 1], dp[i]);       for (auto& j : cost[i + 1]) {         chmax(dp[j.first], dp[i] + j.second);       }     }     cout << dp[n - 1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<char, long long int>> vp(4000005);   void ans(vector<long long int> dp, vector<long long int> star, long long int x,            vector<pair<char, long long int>> &vp, long long int n,            long long int &flag) {     if (x <= 1) {       return;     }     long long int starpos = upper_bound((dp).begin(), (dp).end(), x) - dp.begin();     x -= dp[starpos - 1];     if (x == 0) {       for (long long int i = star[starpos - 1]; i < n; i++) {         if (vp[i].first ==  * ) {           vp[i].first =  b ;         }       }     } else {       for (long long int i = star[starpos]; i < n; i++) {         while (i < n && vp[i].first ==  * ) {           vp[i].first =  b ;           i++;         }         break;       }       ans(dp, star, x, vp, n, flag);     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int T = 1;     cin >> T;     while (T--) {       long long int n, k, x;       cin >> n >> k >> x;       string s;       cin >> s;       string b =   ;       for (long long int i = 0; i < k; i++) {         b +=  b ;       }       long long int cnt = 0;       for (long long int i = 0; i < n; i++) {         if (s[i] ==  * ) {           long long int t = cnt;           for (long long int j = cnt; j < t + k; j++) {             vp[cnt] = { * , cnt};             cnt++;           }         } else {           vp[cnt] = { a , cnt};           cnt++;         }       }       s.clear();       s =   ;       for (long long int i = 0; i < cnt; i++) {         s += vp[i].first;       }       vector<long long int> dp;       vector<long long int> star;       star.push_back(-1);       dp.push_back(1);       long long int prev = 1;       long long int curr = 1;       long long int flag1 = 1;       for (long long int i = cnt - 1; i >= 0 && flag1; i--) {         long long int flag = 0;         while (i >= 0 && vp[i].first ==  * ) {           if (x < (curr + 1) * 1ll * prev) {             dp.push_back((curr + 1) * 1ll * prev);             star.push_back(i);             flag1 = 0;             break;           }           dp.push_back((curr + 1) * 1ll * prev);           star.push_back(i);           i--;           curr++;           flag = 1;         }         if (flag) {           prev = curr * prev;           curr = 1;         }       }       long long int flag = 1;       ans(dp, star, x, vp, cnt, flag);       for (long long int i = 0; i < cnt; i++) {         if (vp[i].first ==  * ) continue;         cout << vp[i].first;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1 << 29;   const double PI = acos(-1);   const double EPS = 1e-8;   void func(vector<pair<int, int> > &v) {     sort((v).begin(), (v).end());     int start = -1;     int end = -1;     vector<pair<int, int> > res;     for (int i = 0; i < (int)v.size(); ++i) {       if (i == 0) {         start = v[i].first;         end = v[i].second;       } else {         if (v[i].first <= end) {           (end < v[i].second ? (end = v[i].second, 1) : 0);         } else {           res.push_back(pair<int, int>(start, end));           start = v[i].first;           end = v[i].second;         }       }     }     if (start != -1) res.push_back(pair<int, int>(start, end));     v = res;   }   struct P {     int v;     int t, a;     P(int v, int t, int a) : v(v), t(t), a(a) {}   };   int maxbit(int a) {     int r = 0;     while (a) a >>= 1, r++;     return r - 1;   }   const int N = 200000;   bool tflag[N];   bool yflag[N];   int main() {     int w, h, k;     while (cin >> w >> h >> k) {       for (int i = 0; i < (int)N; ++i) tflag[i] = yflag[i] = 0;       map<int, vector<pair<int, int> > > tate, yoko;       for (int i = 0; i < (int)k; ++i) {         int x1, y1, x2, y2;         scanf( %d%d%d%d , &x1, &y1, &x2, &y2);         if (x1 == x2) {           tate[x1].push_back(pair<int, int>(min(y1, y2), max(y1, y2)));         } else {           yoko[y1].push_back(pair<int, int>(min(x1, x2), max(x1, x2)));         }       }       assert(tate.size() <= w - 1 && yoko.size() <= h - 1);       vector<P> nim;       for (__typeof((tate).begin()) it = (tate).begin(); it != (tate).end();            ++it) {         if (it->first < N) tflag[it->first] = 1;         vector<pair<int, int> > &v = it->second;         func(v);         int t = h;         for (__typeof((v).begin()) jt = (v).begin(); jt != (v).end(); ++jt) {           t -= jt->second - jt->first;         }         nim.push_back(P(t, 0, it->first));       }       for (__typeof((yoko).begin()) it = (yoko).begin(); it != (yoko).end();            ++it) {         if (it->first < N) yflag[it->first] = 1;         vector<pair<int, int> > &v = it->second;         func(v);         int t = w;         for (__typeof((v).begin()) jt = (v).begin(); jt != (v).end(); ++jt) {           t -= jt->second - jt->first;         }         nim.push_back(P(t, 1, it->first));       }       if ((w - 1 - tate.size()) % 2 == 1) {         for (int i = 1; i < w; ++i)           if (!tflag[i]) {             nim.push_back(P(h, 0, i));             break;           }       }       if ((h - 1 - yoko.size()) % 2 == 1) {         for (int i = 1; i < h; ++i)           if (!yflag[i]) {             nim.push_back(P(w, 1, i));             break;           }       }       int val = 0;       for (__typeof((nim).begin()) it = (nim).begin(); it != (nim).end(); ++it) {         val ^= it->v;       }       if (val) {         puts( FIRST );         int mb = maxbit(val);         int x1, y1, x2, y2;         for (__typeof((nim).begin()) it = (nim).begin(); it != (nim).end();              ++it) {           if ((it->v) >> mb & 1) {             int num = it->v - (it->v ^ val);             assert(num >= 1);             if (it->t == 0) {               x1 = x2 = it->a;               y1 = 0;               vector<pair<int, int> > &v = tate[it->a];               int low = 0, high = h;               while (low + 1 < high) {                 int mid = (low + high) / 2;                 int sum = mid;                 for (__typeof((v).begin()) it = (v).begin(); it != (v).end();                      ++it) {                   if (it->second <= mid)                     sum -= it->second - it->first;                   else if (it->first <= mid)                     sum -= mid - it->first;                 }                 if (sum >= num)                   high = mid;                 else                   low = mid;               }               y2 = high;             } else {               y1 = y2 = it->a;               x1 = 0;               vector<pair<int, int> > &v = yoko[it->a];               int low = 0, high = w;               while (low + 1 < high) {                 int mid = (low + high) / 2;                 int sum = mid;                 for (__typeof((v).begin()) it = (v).begin(); it != (v).end();                      ++it) {                   if (it->second <= mid)                     sum -= it->second - it->first;                   else if (it->first <= mid)                     sum -= mid - it->first;                 }                 if (sum >= num)                   high = mid;                 else                   low = mid;               }               x2 = high;             }             break;           }         }         printf( %d %d %d %d n , x1, y1, x2, y2);       } else {         puts( SECOND );       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   int main() {     int t;     cin >> t;     for (int tc = 0; tc < t; tc++) {       long long a, b, c, d;       cin >> a >> b >> c >> d;       int temp = 1;       if (a % 2) {         temp *= -1;       }       if (b % 2) {         temp *= -1;       }       if (temp == 1) {         cout <<  Tidak Tidak  ;         if (b > 0 || c > 0) {           cout <<  Ya  ;         } else {           cout <<  Tidak  ;         }         if (a > 0 || d > 0) {           cout <<  Ya  << endl;         } else {           cout <<  Tidak  << endl;         }       } else {         if (a > 0 || d > 0) {           cout <<  Ya  ;         } else {           cout <<  Tidak  ;         }         if (b > 0 || c > 0) {           cout <<  Ya  ;         } else {           cout <<  Tidak  ;         }         cout <<  Tidak Tidak  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 5 * (1e5 + 5);   vector<int> e[MAX];   vector<int> ice[MAX];   int n, m;   int ans[MAX];   bool vis[MAX];   int cnt;   void dfs(int now, int pre) {     int len = ice[now].size();     for (int i = 0; i <= len; i++) vis[i] = 0;     for (int i = 0; i < len; i++)       if (ans[ice[now][i]]) vis[ans[ice[now][i]]] = 1;     int f = 1;     for (int i = 0; i < len; i++)       if (!ans[ice[now][i]]) {         int t = ice[now][i];         while (vis[f]) f++;         ans[t] = f++;       }     for (int i = 0; i < e[now].size(); i++)       if (e[now][i] != pre) dfs(e[now][i], now);   }   int main() {     cin >> n >> m;     int mx = 0;     for (int i = 1; i <= n; i++) {       int u, g;       scanf( %d , &u);       mx = max(mx, u);       for (int j = 1; j <= u; j++) {         scanf( %d , &g);         ice[i].push_back(g);       }     }     for (int i = 1; i <= n - 1; i++) {       int x, y;       scanf( %d%d , &x, &y);       e[x].push_back(y);       e[y].push_back(x);     }     dfs(1, -1);     if (mx)       printf( %d n , mx);     else       printf( 1 n );     if (ans[1])       printf( %d , ans[1]);     else       printf( 1 );     for (int i = 2; i <= m; i++) {       if (ans[i])         printf(  %d , ans[i]);       else         printf(  1 );     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   int n, nxt[100005], fa[100005];   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   int main(void) {     scanf( %d , &n);     if (n % 2) {       puts( -1 );       return 0;     }     for (int i = 0; i <= n; i++) fa[i] = i;     for (int i = 0; i < (n / 2); i++) {       int j = i + (n / 2);       nxt[i] = i * 2, nxt[j] = i * 2 + 1;       fa[find(i)] = find((i * 2) % n);       fa[find(j)] = find((i * 2 + 1) % n);     }     for (int i = 0; i < (n / 2); i++) {       int j = i + (n / 2);       if (find(i) != find(j)) {         nxt[i] ^= nxt[j] ^= nxt[i] ^= nxt[j];         fa[find(i)] = find(j);       }     }     int x = 0;     for (int i = 0; i <= n; i++) printf( %d  , x), x = nxt[x];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a;     cin >> n >> a;     vector<int> m(n);     for (int i = 0; i < n; i++) cin >> m[i];     n--;     a--;     int calc = 0;     if (m[a]) calc++;     for (int j = 1; j <= max(n - a, a); j++) {       if (a - j < 0) {         if (m[a + j]) calc++;         continue;       }       if (a + j > n) {         if (m[a - j]) calc++;         continue;       }       if (m[a + j] && m[a - j]) calc += 2;     }     cout << calc;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int arities[100001];   int legs[100001];   bool markers[100001];   list<int> graph[100001];   void parse() {     int a, b;     cin >> n;     for (int it = 0; it < (100001); it++) {       (arities)[it] = (0);     };     for (int it = 0; it < (100001); it++) {       (markers)[it] = (false);     };     for (int it = 0; it < (100001); it++) {       (legs)[it] = (0);     };     for (int i = 1; i < n; i++) {       cin >> a >> b;       graph[a].push_back(b);       graph[b].push_back(a);       arities[a]++;       arities[b]++;     }   }   void traverse(int i) {     if (markers[i]) return;     if (arities[i] >= 3) {       legs[i]++;       return;     }     markers[i] = true;     for (list<int>::iterator it = graph[i].begin(); it != graph[i].end(); ++it) {       traverse(*it);     }   }   void solve() {     for (int i = 1; i <= n; i++) {       if (arities[i] == 1) traverse(i);     }     for (int i = 1; i <= n; i++) {       if (!markers[i]) {         int r = 0;         for (list<int>::iterator it = graph[i].begin(); it != graph[i].end();              ++it) {           if (!markers[*it] && arities[*it] - min(legs[*it], 2) > 1) r++;         }         if (r > 2) {           cout <<  No ;           return;         }       }     }     cout <<  Yes ;   }   int main() {     ios::sync_with_stdio(false);     std::cout.precision(11);     parse();     solve();     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char stra[110];   char strb[110];   int mark[110];   int g[11000];   int main() {     int b, d;     scanf( %d%d , &b, &d);     scanf( %s , stra);     scanf( %s , strb);     int lena = strlen(stra);     int lenb = strlen(strb);     memset(mark, -1, sizeof(mark));     mark[0] = 0;     int pos = 0;     int key = 0;     int cnt = 0;     int num = 0;     for (int ii = 1; ii <= 1000; ii++) {       int sum = 0;       for (int i = 0; i < lena; i++) {         if (stra[i] == strb[pos]) {           pos++;           if (pos == lenb) {             sum++;             pos = 0;           }         }       }       g[ii] = sum;       if (mark[pos] == -1)         mark[pos] = ii;       else {         key = mark[pos];         cnt = ii - key;         for (int ti = key + 1; ti <= ii; ti++) num += g[ti];         break;       }     }     int ans = 0;     if (b <= key) {       for (int i = 1; i <= b; i++) ans += g[i];     } else {       int tb = (b - key) / cnt;       int tt = (b - key) - tb * cnt;       for (int i = 1; i <= key; i++) ans += g[i];       ans += tb * num;       for (int i = key + 1; i <= key + tt; i++) ans += g[i];     }     printf( %d n , ans / d);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unordered_map<long long, string> Map;   unordered_map<long long, string>::iterator it;   string Get(long long x) {     string ans =   ;     int s[13], cnt = 0;     while (x) {       s[++cnt] = x % 10;       x /= 10;     }     for (int i = cnt; i >= 1; i--) ans += char(s[i] +  0 );     return ans;   }   int main() {     long long n;     scanf( %lld , &n);     long long len = sqrt(n);     for (long long i = 2; i <= len; i++) {       for (long long j = i * i, k = 2; j <= n; j *= i, k++) {         string gg = Get(i) +  ^  + Get(k);         if (gg.size() >= Get(j).size()) continue;         if (Map[j].size() == 0) {           Map[j] = gg;         } else if (Map[j].size() > gg.size())           Map[j] = gg;       }     }     string ans = Get(n);     for (it = Map.begin(); it != Map.end(); it++) {       long long k = (*it).first;       string gg = (*it).second;       if (n / k >= 2) {         gg +=  * ;         gg += (Map.count(n / k)) ? Map[n / k] : Get(n / k);       }       if (n % k) {         gg +=  + ;         gg += (Map.count(n % k)) ? Map[n % k] : Get(n % k);       }       if (ans.size() > gg.size()) ans = gg;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e3 + 13;   const int INF = 1e6 + 7;   int t[MAXN][MAXN];   int tab[MAXN * MAXN];   bool odw[MAXN * MAXN];   int wyn[MAXN * MAXN];   bool prze[MAXN * MAXN];   int xx[4] = {0, 0, -1, 1};   int yy[4] = {-1, 1, 0, 0};   vector<int> v[MAXN * MAXN];   int check(int x) {     int spr = 0;     odw[x] = true;     prze[x] = true;     for (auto it : v[x]) {       if (odw[it] && prze[it])         spr = 1;       else if (!odw[it])         spr = max(spr, check(it));     }     prze[x] = false;     return spr;   }   int DFS(int x, int gl) {     odw[x] = true;     for (auto it : v[x]) {       if (odw[it])         wyn[x] = max(wyn[x], wyn[it]);       else         wyn[x] = max(wyn[x], DFS(it, gl + 1));     }     wyn[x]++;     return wyn[x];   }   int main() {     int n, m;     int mx = 0;     char c;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf(   );       for (int j = 1; j <= m; j++) {         scanf( %c , &c);         if (c ==  I ) t[i][j] = 1;         if (c ==  M ) t[i][j] = 2;         if (c ==  A ) t[i][j] = 3;         tab[(i - 1) * m + j] = t[i][j];       }       t[i][0] = t[i][m + 1] = 5;     }     for (int i = 1; i <= m; i++) t[0][i] = t[n + 1][i] = 5;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         for (int l = 0; l < 4; l++) {           if (t[i + xx[l]][j + yy[l]] == (t[i][j] + 1) % 4)             v[(i - 1) * m + j].push_back((i + xx[l] - 1) * m + j + yy[l]);         }       }     }     for (int i = 1; i <= n * m; i++) {       if (!odw[i] && tab[i] == 0) {         if (check(i) == 1) {           printf( Poor Inna! );           return 0;         }       }     }     for (int i = 1; i <= n * m; i++) odw[i] = false;     for (int i = 1; i <= n * m; i++) {       if (!odw[i] && tab[i] == 0) mx = max(mx, DFS(i, 1) / 4);     }     if (mx == 0)       printf( Poor Dima! );     else       printf( %d , mx);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   pair<int, int> a[100003];   int l = 0;   bool lios(string s) {     if (s.size() < 4) return false;     if (s.substr(s.size() - 4, 4) ==  lios ) return true;     return false;   }   bool liala(string s) {     if (s.size() < 5) return false;     if (s.substr(s.size() - 5, 5) ==  liala ) return true;     return false;   }   bool etr(string s) {     if (s.size() < 3) return false;     if (s.substr(s.size() - 3, 3) ==  etr ) return true;     return false;   }   bool etra(string s) {     if (s.size() < 4) return false;     if (s.substr(s.size() - 4, 4) ==  etra ) return true;     return false;   }   bool initis(string s) {     if (s.size() < 6) return false;     if (s.substr(s.size() - 6, 6) ==  initis ) return true;     return false;   }   bool inites(string s) {     if (s.size() < 6) return false;     if (s.substr(s.size() - 6, 6) ==  inites ) return true;     return false;   }   bool adj, n, v;   int main() {     while (cin >> s) {       a[l].first = -1, a[l].second = -1;       if (lios(s)) {         a[l++] = pair<int, int>{0, 0};         continue;       }       if (liala(s)) {         a[l++] = pair<int, int>{0, 1};         continue;       }       if (etr(s)) {         a[l++] = pair<int, int>{1, 0};         continue;       }       if (etra(s)) {         a[l++] = pair<int, int>{1, 1};         continue;       }       if (initis(s)) {         a[l++] = pair<int, int>{2, 0};         continue;       }       if (inites(s)) {         a[l++] = pair<int, int>{2, 1};         continue;       }       l++;     }     if (l == 1 && a[0].first != -1) {       cout <<  YES ;       return 0;     }     for (int i = 0; i < l; i++) {       if (a[i].first == -1) {         cout <<  NO ;         return 0;       }       if (i == 0)         ;       else {         if (a[i].second != a[i - 1].second) {           cout <<  NO ;           return 0;         }       }       if (a[i].first == 0) {         if (n || v) {           cout <<  NO ;           return 0;         }         adj = 1;         continue;       }       if (a[i].first == 1) {         if (n || v) {           cout <<  NO ;           return 0;         }         n = 1;         continue;       }       if (a[i].first == 2) {         if (!n) {           cout <<  NO ;           return 0;         }         v = 1;         continue;       }     }     if (!n)       cout <<  NO ;     else       cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e15;   long long binpow(long long a, long long b) {     long long res = 1;     while (b > 0) {       if (b & 1) {         res = res * a;       }       a = a * a;       b >>= 1;     }     return res;   }   long long dp[205][205][205];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long T;     T = 1;     while (T--) {       long long n, k;       string s, t;       cin >> n >> k;       cin >> s >> t;       memset(dp, -1, sizeof(dp));       dp[0][0][0] = 0;       long long i, j;       for (i = 0; i < n; i++) {         for (j = 0; j <= k; j++) {           for (long long f0 = 0; f0 <= n; f0++) {             if (dp[i][j][f0] == -1) {               continue;             }             long long e0 = s[i] == t[0];             long long e1 = s[i] == t[1];             long long e01 = t[0] == t[1];             dp[i + 1][j][f0 + e0] =                 max(dp[i + 1][j][f0 + e0], dp[i][j][f0] + (e1 ? f0 : 0));             if (j + 1 <= k)               dp[i + 1][j + 1][f0 + 1] =                   max(dp[i + 1][j + 1][f0 + 1], dp[i][j][f0] + (e01 ? f0 : 0));             if (j + 1 <= k)               dp[i + 1][j + 1][f0 + e01] =                   max(dp[i + 1][j + 1][f0 + e01], dp[i][j][f0] + f0);           }         }       }       long long ans = 0;       for (j = 0; j <= k; j++) {         for (long long f0 = 0; f0 <= n; f0++) {           ans = max(ans, dp[n][j][f0]);         }       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 1011, inf = 1000111222;   int n, a[max_n][max_n], x[max_n];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         scanf( %d , &a[i][j]);       }     }     long long val = 1LL * a[0][1] * a[0][2] / a[1][2];     x[0] = sqrtl(val);     while (1LL * x[0] * x[0] < val) {       ++x[0];     }     printf( %d  , x[0]);     for (int i = 1; i < n; ++i) {       printf( %d  , a[0][i] / x[0]);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int di[] = {-1, 0, 1, 0};   const int dj[] = {0, 1, 0, -1};   const long long MOD = 1e9 + 7;   const long long INF = 1e9;   const double EPS = 1e-11;   int main() {     ios::sync_with_stdio(false), cin.tie(0);     int n, q, c;     cin >> n >> q >> c;     vector<pair<int, int> > pos(n);     vector<int> s(n);     vector<vector<vector<int> > > cnt(         101, vector<vector<int> >(101, vector<int>(c + 1)));     for (int i = 0; i < n; ++i) {       cin >> pos[i].first >> pos[i].second >> s[i];       cnt[pos[i].first][pos[i].second][s[i]]++;     }     for (int i = 0; i < 101; ++i) {       for (int j = 0; j < 101; ++j) {         for (int k = 0; k < c + 1; ++k) {           if (i > 0) cnt[i][j][k] += cnt[i - 1][j][k];           if (j > 0) cnt[i][j][k] += cnt[i][j - 1][k];           if (i > 0 && j > 0) cnt[i][j][k] -= cnt[i - 1][j - 1][k];         }       }     }     while (q--) {       int t, x1, y1, x2, y2;       cin >> t >> x1 >> y1 >> x2 >> y2;       vector<vector<int> > cum(4, vector<int>(11));       cum[0] = cnt[x2][y2];       cum[1] = cnt[x2][y1 - 1];       cum[2] = cnt[x1 - 1][y1 - 1];       cum[3] = cnt[x1 - 1][y2];       vector<int> cur(11), cur2(11);       for (int i = 0; i < c + 1; ++i)         cur[i] = cum[0][i] - cum[1][i] - cum[3][i] + cum[2][i];       for (int i = 0; i < c + 1; ++i) cur2[(i + t) % (c + 1)] = cur[i];       int r = 0;       for (int i = 0; i < c + 1; ++i) r += i * cur2[i];       cout << r << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int t[200005], revt[200005];   int main() {     ios::sync_with_stdio(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> t[i];       revt[t[i]]++;     }     int ans = 1;     for (int i = 0; i <= n; i++) ans += max(0, revt[i] - 1);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   int l[maxn];   int r[maxn];   int mx[maxn];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     long long ans = n;     for (int i = 1; i <= n; i++) {       cin >> l[i] >> r[i];     }     sort(l + 1, l + 1 + n);     sort(r + 1, r + 1 + n);     for (int i = 1; i <= n; i++) ans += max(r[i], l[i]);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   bool vowel(char a) {     a = toupper(a);     if ((a ==  A ) || (a ==  E ) || (a ==  I ) || (a ==  O ) || (a ==  U ))       return 1;     return 0;   }   long long int n, m, q, a, b, c, sum = 0, cnt = 0;   long long int arr[2000005];   map<long long int, bool> mp;   string s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> arr[i];       arr[i]--;     }     cin >> s;     long long int mn = 20000051, mx = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  0 ) {         if (arr[i] != i) {           cout <<  NO  << endl;           return 0;         }       } else if (s[i] ==  1 ) {         mn = 20000051, mx = 0;         int st = i;         mn = min(arr[i], mn);         mx = max(arr[i], mx);         while (i < n && s[i] ==  1 ) {           mn = min(arr[i + 1], mn);           mx = max(arr[i + 1], mx);           i++;         }         if (mn != st || mx != i) {           cout <<  NO  << endl;           return 0;         }         arr[i] = mx;       }     }     cout <<  YES  << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a[6], b[6], c[6];     a[0] =  purple ;     b[0] =  Power ;     a[1] =  green ;     b[1] =  Time ;     a[2] =  blue ;     b[2] =  Space ;     a[3] =  orange ;     b[3] =  Soul ;     a[4] =  red ;     b[4] =  Reality ;     a[5] =  yellow ;     b[5] =  Mind ;     int n, i, j, k = 0, flag = 0;     int arr[6];     cin >> n;     for (i = 0; i < n; i++) cin >> c[i];     for (i = 0; i < n; i++) {       for (j = 0; j < 6; j++) {         if (c[i] == a[j]) {           arr[k] = j;           k++;         }       }     }     cout << 6 - n << endl;     for (i = 0; i < 6; i++) {       flag = 0;       for (k = 0; k < n; k++)         if (i == arr[k]) flag = 1;       if (flag == 0) cout << b[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long int cur = 1;     long long num = 1;     while (cur != n + 1) {       if (cur != 1)         cout << cur * 1ll * cur * 1ll * cur + 2 * cur * cur + 1;       else         cout << 2;       cout << endl;       cur++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, cnt[100005], root, par[100005], br[100005], st[100005], brcnt;   vector<long long> a[100005];   pair<long long, long long> vert[100005];   map<pair<long long, long long>, long long> mm;   set<long long> brcl;   long long tmp = 1;   void dfs(long long v) {     if (a[v].size() > 0)       for (long long i = 0; i <= a[v].size() - 1; ++i) {         if (v == root) {           brcnt++;           st[brcnt] = tmp;         }         if (a[v][i] != par[v]) {           br[a[v][i]] = brcnt;           mm[make_pair(v, a[v][i])] = tmp++;           par[a[v][i]] = v;           dfs(a[v][i]);         }       }   }   inline long long finddist(long long req1, long long req2) {     if (brcl.empty()) return req2 - req1 + 1;     set<long long>::iterator curr = (brcl.lower_bound(req2));     if (*curr == req2) return -1;     if (curr == brcl.begin()) return req2 - req1 + 1;     curr--;     if (*curr >= req1 && *curr <= req2)       return -1;     else       return req2 - req1 + 1;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (long long i = 1; i <= n - 1; ++i) {       long long u, v;       cin >> u >> v;       cnt[u]++;       cnt[v]++;       if (cnt[u] > 2)         root = u;       else if (cnt[v] > 2)         root = v;       a[u].push_back(v);       a[v].push_back(u);       vert[i] = make_pair(u, v);     }     if (root == 0) root = 1;     par[root] = 0;     dfs(root);     for (long long i = 1; i <= n - 1; ++i)       if (vert[i].second == par[vert[i].first])         swap(vert[i].first, vert[i].second);     cin >> m;     for (long long loop = 1; loop <= m; ++loop) {       long long ty, id;       cin >> ty >> id;       if (ty == 1) {         long long edge = mm[vert[id]];         set<long long>::iterator it = brcl.find(edge);         brcl.erase(it);       } else if (ty == 2) {         long long edge = mm[vert[id]];         brcl.insert(edge);       } else {         long long en;         cin >> en;         if (en == id) {           cout << 0 << endl;           continue;         }         if (en == root) swap(en, id);         if (id == root) {           long long req1 = st[br[en]], req2 = mm[make_pair(par[en], en)];           cout << finddist(req1, req2) << endl;         } else if (br[id] == br[en]) {           if (mm[make_pair(par[en], en)] < mm[make_pair(par[id], id)])             swap(id, en);           long long v;           for (long long i = 0; i <= a[id].size() - 1; ++i)             if (a[id][i] != par[id]) v = a[id][i];           long long req1 = mm[make_pair(id, v)],                     req2 = mm[make_pair(par[en], en)];           cout << finddist(req1, req2) << endl;         } else {           long long req1 = st[br[en]], req2 = mm[make_pair(par[en], en)];           long long dist = finddist(req1, req2);           req1 = st[br[id]], req2 = mm[make_pair(par[id], id)];           cout << ((dist == -1 || finddist(req1, req2) == -1)                        ? -1                        : (finddist(req1, req2) + dist))                << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> A(n + 10, 0);     for (int i = 0; i < n; i++) cin >> A[i];     unordered_map<long long, int> fir, sec;     fir[A[0]] = 1;     for (int i = 1; i < n; i++) sec[A[i]]++;     long long S = 0;     for (int i = 0; i < n; i++) S += A[i];     long long sdash = 0;     if (S & 1) {       cout <<  NO  << endl;       return 0;     }     for (int i = 0; i < n; i++) {       sdash += A[i];       if (sdash == S / 2) {         cout <<  YES ;         return 0;       }       if (sdash < S / 2) {         long long x = S / 2 - sdash;         if (sec[x] > 0) {           cout <<  YES ;           return 0;         }       } else {         long long y = sdash - S / 2;         if (fir[y] > 0) {           cout <<  YES ;           return 0;         }       }       fir[A[i + 1]]++;       sec[A[i + 1]]--;     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, a, b, k;   long long q;   long long param = 0;   string ciag;   long long MOD = 1000000009;   long long potega(long long a_1, long long n_1) {     if (n_1 == 0) return 1;     if (n_1 == 1)       return a_1;     else if (n_1 % 2 == 0) {       long long s = potega(a_1, n_1 / 2);       return (s * s) % MOD;     } else {       return (potega(a_1, n_1 - 1) * a_1) % MOD;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> a >> b >> k;     cin >> ciag;     q = (potega(b, k) * potega(a, MOD - 1 - k)) % MOD;     if (q < 0) {       q %= MOD;       q += MOD;       q %= MOD;     }     long long krotnosc = ((n + 1) / k);     if (q != 1) {       for (int i = 0; i < (k); ++i) {         if (ciag[i] ==  - ) {           long long a_1 = (potega(a, n - i) * potega(b, i)) % MOD;           if (a_1 < 0) {             a_1 %= MOD;             a_1 += MOD;             a_1 %= MOD;           }           long long temp_ =               (((a_1 * ((potega(q, krotnosc) - 1 + MOD) % MOD) % MOD)) *                (potega(q - 1, MOD - 2))) %               MOD;           if (temp_ < 0) {             temp_ %= MOD;             temp_ += MOD;             temp_ %= MOD;           }           param = ((param - temp_) % MOD + MOD) % MOD;           if (param < 0) {             param %= MOD;             param += MOD;             param %= MOD;           }         } else {           long long a_1 = (potega(a, n - i) * potega(b, i)) % MOD;           if (a_1 < 0) {             a_1 %= MOD;             a_1 += MOD;             a_1 %= MOD;           }           long long temp_ =               (((a_1 * ((potega(q, krotnosc) - 1 + MOD) % MOD) % MOD)) *                (potega(q - 1, MOD - 2))) %               MOD;           if (temp_ < 0) {             temp_ %= MOD;             temp_ += MOD;             temp_ %= MOD;           }           param = (param + temp_) % MOD;           if (param < 0) {             param %= MOD;             param += MOD;             param %= MOD;           }         }       }     } else {       for (int i = 0; i < (k); ++i) {         if (ciag[i] ==  - ) {           long long a_1 = (potega(a, n - i) * potega(b, i)) % MOD;           if (a_1 < 0) {             a_1 %= MOD;             a_1 += MOD;             a_1 %= MOD;           }           long long temp_ = (a_1 * krotnosc) % MOD;           if (temp_ < 0) {             temp_ %= MOD;             temp_ += MOD;             temp_ %= MOD;           }           param = ((param - temp_) % MOD + MOD) % MOD;           if (param < 0) {             param %= MOD;             param += MOD;             param %= MOD;           }         } else {           long long a_1 = (potega(a, n - i) * potega(b, i)) % MOD;           if (a_1 < 0) {             a_1 %= MOD;             a_1 += MOD;             a_1 %= MOD;           }           long long temp_ = (a_1 * krotnosc) % MOD;           if (temp_ < 0) {             temp_ %= MOD;             temp_ += MOD;             temp_ %= MOD;           }           param = (param + temp_) % MOD;           if (param < 0) {             param %= MOD;             param += MOD;             param %= MOD;           }         }       }     }     cout << (param + MOD) % MOD <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double dist(long double x, long double y, long double a, long double b) {     return sqrt((a - x) * (a - x) + (y - b) * (y - b));   }   long double distMan(long double x, long double y, long double a,                       long double b) {     return abs(x - a) + abs(y - b);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long tt = 1;     while (tt--) {       long double a, b, c;       cin >> a >> b >> c;       long double x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       if (a == 0 || b == 0) {         cout <<     << fixed << setprecision(20) <<                  << abs(x1 - x2) + abs(y1 - y2) <<   n ;       } else {         long double ans = abs(x2 - x1) + abs(y2 - y1);         long double x3, y3, x4, y4;         x3 = x1, x4 = x2;         y3 = (-a * x3 - c) / b, y4 = (-a * x4 - c) / b;         ans = min(ans, distMan(x1, y1, x3, y3) + distMan(x2, y2, x4, y4) +                            dist(x4, y4, x3, y3));         x3 = x1, y4 = y2;         y3 = (-a * x3 - c) / b, x4 = (-b * y4 - c) / a;         ans = min(ans, distMan(x1, y1, x3, y3) + distMan(x2, y2, x4, y4) +                            dist(x4, y4, x3, y3));         y3 = y1, x4 = x2;         x3 = (-b * y3 - c) / a, y4 = (-a * x4 - c) / b;         ans = min(ans, distMan(x1, y1, x3, y3) + distMan(x2, y2, x4, y4) +                            dist(x4, y4, x3, y3));         y3 = y1, y4 = y2;         x3 = (-b * y3 - c) / a, x4 = (-b * y4 - c) / a;         ans = min(ans, distMan(x1, y1, x3, y3) + distMan(x2, y2, x4, y4) +                            dist(x4, y4, x3, y3));         cout <<     << fixed << setprecision(20) <<     << ans <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   long long fp(long long x, long long y) {     long long res = 1;     while (y) res = (y & 1) ? res * x % mod : res, x = x * x % mod, y >>= 1;     return res;   }   int t;   int n, m, rb, cb, rd, cd, lx = 1, ly = 1;   long long p;   void dfs(int x, int y, int dx, int dy, vector<long long>& v) {     int nx = x + dx, ny = y + dy;     if (nx > n || nx < 1) dx = -dx;     if (ny > m || ny < 1) dy = -dy;     if ((x == rb && y == cb && v.size() != 0 && dx == lx && dy == ly)) return;     if (x == rd || y == cd)       v.push_back(p);     else       v.push_back(1);     nx = x + dx, ny = y + dy;     dfs(nx, ny, dx, dy, v);   }   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d%d%d%d%d%d%lld , &n, &m, &rb, &cb, &rd, &cd, &p);       vector<long long> v;       long long fz = 1;       long long ny = fp(100, mod - 2), xx = 1;       p = (100ll - p) * ny % mod;       lx = 1, ly = 1;       int nx = rb + lx, nn = cb + ly;       if (nx > n || nx < 1) lx = -lx;       if (nn > m || nn < 1) ly = -ly;       dfs(rb, cb, lx, ly, v);       for (int i = 0; i < (int)v.size(); i++) {         fz = (fz * v[i]) % mod;       }       long long val = 0;       for (int i = 0; i < (int)v.size(); i++) {         xx = (v[i] * xx) % mod;         val += xx;         val %= mod;       }       fz = (1 - fz + mod) % mod;       val = (val * fp(fz, mod - 2)) % mod;       printf( %lld n , val);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long int n;     cin >> n;     ;     long long int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     long long int dp[n];     memset(dp, 0, sizeof(dp));     long long int ans = 0;     int c1 = 0;     int f = 0;     while (f == 0) {       f = 1;       for (int i = 0; i < n; i++) {         if (dp[i] == 0 && a[i] >= c1) {           dp[i] = 1;           c1++;         }       }       if (c1 > 0) {         ans++;         f = 0;       }       c1 = 0;     }     cout << ans;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, t, a, b, da, db;     cin >> x >> t >> a >> b >> da >> db;     if (x == 0) {       cout <<  YES  << endl;       return 0;     }     for (int i = 0; i < t; i++) {       if (a - da * i == x) {         cout <<  YES  << endl;         return 0;       }       if (b - db * i == x) {         cout <<  YES  << endl;         return 0;       }     }     for (int i = 0; i < t; i++) {       for (int j = 0; j < t; j++) {         if (a - da * i + b - db * j == x) {           cout <<  YES  << endl;           return 0;         }       }     }     cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   void FAST() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int gcd(int x, int y) {     if (x == 0) return y;     return gcd(y % x, x);   }   int powM(int x, int y, int m) {     int ans = 1, r = 1;     x %= m;     while (r > 0 && r <= y) {       if (r & y) {         ans *= x;         ans %= m;       }       r <<= 1;       x *= x;       x %= m;     }     return ans;   }   map<long long, long long> factorize(long long n) {     map<long long, long long> ans;     for (long long i = 2; i * i <= n; i++) {       while (n % i == 0) {         ans[i]++;         n /= i;       }     }     if (n > 1) {       ans[n]++;       n = 1;     }     return ans;   }   const double pi = 3.14159265;   int main() {     FAST();     int n;     cin >> n;     string s;     cin >> s;     int o = 0, c = 0;     for (auto x : s) {       if (x ==  ( )         o++;       else         c++;     }     if (o != c)       cout << -1;     else {       int depth = 0, last = -1, ans = 0;       for (int i = 0; i < n; i++) {         if (s[i] ==  ( ) {           depth++;         } else {           depth--;         }         if (depth == 0 && s[i] ==  ( ) {           ans += (i - last);           last = i;         }         if (depth == 0) last = i;       }       cout << ans;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long int;   struct point {     LL x, y;     point(LL x_ = 0, LL y_ = 0) : x(x_), y(y_) {}     point operator-(const point& b) { return point(x - b.x, y - b.y); }     point operator+(const point& b) { return point(x + b.x, y + b.y); }   };   LL cross_abs(const point& a, const point& b) {     return abs(a.x * b.y - b.x * a.y);   }   bool mult(const point& sp, const point& ep, const point& op) {     return (sp.x - op.x) * (ep.y - op.y) >= (ep.x - op.x) * (sp.y - op.y);   }   bool operator<(const point& l, const point& r) {     return (l.y < r.y) || (l.y == r.y && l.x < r.x);   }   int graham(point pnt[], int n, point res[]) {     int i = 0, len = 0, k = 0, top = 1;     sort(pnt, pnt + n);     if (n == 0) return 0;     res[0] = pnt[0];     if (n == 1) return 1;     res[1] = pnt[1];     if (n == 2) return 2;     res[2] = pnt[2];     for (i = 2; i < n; i++) {       while (top && mult(pnt[i], res[top], res[top - 1])) top--;       res[++top] = pnt[i];     }     len = top;     res[++top] = pnt[n - 2];     for (i = n - 3; i >= 0; i--) {       while (top != len && mult(pnt[i], res[top], res[top - 1])) top--;       res[++top] = pnt[i];     }     return top;   }   int n = 0;   LL ss = 0;   const int maxn = 5e3 + 5;   point p[maxn], cp[maxn];   LL maxs = 0;   int ansa = 0, ansb = 0, ansc = 0;   int main(void) {     scanf( %d %lld , &n, &ss);     for (int i = 0; i < n; ++i) scanf( %lld %lld , &p[i].x, &p[i].y);     int cnt = graham(p, n, cp);     for (int i = 0; i < cnt; ++i) {       for (int j = i + 1, k = (j + 1) % cnt; j < cnt && k != i; ++j) {         while (cross_abs(cp[i] - cp[k], cp[j] - cp[k]) <                cross_abs(cp[i] - cp[(k + 1) % cnt], cp[j] - cp[(k + 1) % cnt]))           k = (k + 1) % cnt;         if (k == i || k == j) continue;         LL s = cross_abs(cp[i] - cp[k], cp[j] - cp[k]);         if (s > maxs) {           maxs = s;           ansa = i, ansb = j, ansc = k;         }       }     }     point a = cp[ansa] - cp[ansb] + cp[ansc];     point b = cp[ansa] + cp[ansb] - cp[ansc];     point c = cp[ansb] + cp[ansc] - cp[ansa];     printf( %lld %lld n , a.x, a.y);     printf( %lld %lld n , b.x, b.y);     printf( %lld %lld n , c.x, c.y);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   void solve() {     int n;     cin >> n;     vector<long long> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     if (n == 1) {       cout <<  1 n ;       return;     }     vector<long long> difs(n - 1);     for (int i = 0; i < n - 1; i++) difs[i] = abs(a[i + 1] - a[i]);     vector<long long> cj(n);     vector<long long> fj(n);     cj[n - 1] = 0;     fj[n - 1] = 1;     long long res = 0;     for (int i = n - 2; i >= 0; i--) {       if (gcd(difs[i], fj[i + 1]) > 1) {         cj[i] = cj[i + 1] + 1;         fj[i] = gcd(difs[i], fj[i + 1]);       } else {         cj[i] = int(difs[i] > 1ll);         fj[i] = difs[i];         for (int k = 1; k < cj[i + 1]; k++) {           if (gcd(fj[i], difs[i + k]) > 1) {             cj[i]++;             fj[i] = gcd(fj[i], difs[i + k]);           } else             break;         }       }       res = max(res, cj[i]);     }     cout << res + 1 <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int tst;     cin >> tst;     while (tst--) solve();     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:247474112 )   #pragma GCC optimize( Ofast )   using namespace std;   const long long maxn = 2e5 + 10;   long long mx[maxn], val[maxn], sum[maxn];   vector<long long> g[maxn];   long long pass[maxn];   bool ok = 0;   long long ans = LLONG_MIN;   void dfs(long long aa = 1, long long aaa = 0) {     vector<long long> v;     sum[aa] = val[aa];     for (long long a : g[aa]) {       if (a == aaa) continue;       dfs(a, aa);       sum[aa] += sum[a];       v.push_back(pass[a]);     }     if (!v.empty()) {       sort(v.begin(), v.end());       pass[aa] = max(v.back(), sum[aa]);       if (v.size() >= 2) {         ans = max(ans, v[v.size() - 1] + v[v.size() - 2]);       }     } else {       pass[aa] = sum[aa];     }   }   int32_t main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long n;     cin >> n;     for (long long i = 1; i <= n; ++i) cin >> val[i];     for (long long i = 1; i <= n; ++i) mx[i] = LLONG_MIN;     for (long long i = 1; i < n; ++i) {       long long u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     for (long long i = 1; i <= n; ++i) pass[i] = LLONG_MIN;     dfs(1);     if (ans == LLONG_MIN)       cout <<  Impossible ;     else       cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long second;   long long a1[1231], a2[2131];   long long b[1231];   long long sum[1231];   int main() {     int n;     cin >> n;     for (int i = 2; i <= n; i++) cin >> a1[i];     for (int i = 1; i <= n - 1; i++) cin >> a2[i];     for (int i = 1; i <= n; i++) {       cin >> b[i];       second += a1[i];       sum[i] = b[i] + second;     }     second = 0;     for (int i = n; i >= 1; i--) {       second += a2[i];       sum[i] += second;     }     sort(sum, sum + n + 1);     cout << sum[1] + sum[2];   }


#include <bits/stdc++.h>   using namespace std;   long long inf;   const double eps = 1e-8;   const double pi = acos(-1.0);   template <class T>   long long chkmin(T &a, T b) {     return a > b ? a = b, 1 : 0;   }   template <class T>   long long chkmax(T &a, T b) {     return a < b ? a = b, 1 : 0;   }   template <class T>   T sqr(T a) {     return a * a;   }   template <class T>   T mmin(T a, T b) {     return a < b ? a : b;   }   template <class T>   T mmax(T a, T b) {     return a > b ? a : b;   }   template <class T>   T aabs(T a) {     return a < 0 ? -a : a;   }   template <class T>   long long dcmp(T a, T b) {     return a > b;   }   template <long long *a>   long long cmp_a(long long first, long long second) {     return a[first] < a[second];   }   struct __INIT__ {     __INIT__() { memset(&inf, 0x3f, sizeof(inf)); }   } __INIT___;   namespace io {   const long long SIZE = (1 << 21) + 1;   char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,                                          qu[55];   long long f, qr;   inline void flush() {     fwrite(obuf, 1, oS - obuf, stdout);     oS = obuf;   }   inline void putc(char first) {     *oS++ = first;     if (oS == oT) flush();   }   template <typename A>   inline bool read(A &first) {     for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                                 (iS == iT ? EOF : *iS++))                               : *iS++);          c <  0  || c >  9 ;          c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                          (iS == iT ? EOF : *iS++))                        : *iS++))       if (c ==  - )         f = -1;       else if (c == EOF)         return 0;     for (first = 0; c <=  9  && c >=  0 ;          c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                          (iS == iT ? EOF : *iS++))                        : *iS++))       first = first * 10 + (c & 15);     first *= f;     return 1;   }   inline bool read(char &first) {     while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                                  (iS == iT ? EOF : *iS++))                               : *iS++)) ==     ||            first ==   n  || first ==   r )       ;     return first != EOF;   }   inline bool read(char *first) {     while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                                   (iS == iT ? EOF : *iS++))                                : *iS++)) ==   n  ||            *first ==     || *first ==   r )       ;     if (*first == EOF) return 0;     while (!(*first ==   n  || *first ==     || *first ==   r ))       *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                                 (iS == iT ? EOF : *iS++))                              : *iS++);     *first = 0;     return 1;   }   template <typename A, typename... B>   inline bool read(A &first, B &...second) {     return read(first) && read(second...);   }   template <typename A>   inline bool write(A first) {     if (!first) putc( 0 );     if (first < 0) putc( - ), first = -first;     while (first) qu[++qr] = first % 10 +  0 , first /= 10;     while (qr) putc(qu[qr--]);     return 0;   }   inline bool write(char first) {     putc(first);     return 0;   }   inline bool write(const char *first) {     while (*first) {       putc(*first);       ++first;     }     return 0;   }   inline bool write(char *first) {     while (*first) {       putc(*first);       ++first;     }     return 0;   }   template <typename A, typename... B>   inline bool write(A first, B... second) {     return write(first) || write(second...);   }   struct Flusher_ {     ~Flusher_() { flush(); }   } io_flusher_;   }  // namespace io   using io ::putc;   using io ::read;   using io ::write;   long long a[100005], b[100005], f[100005], g[100005];   long long s[100005], t;   long long n, k;   struct smt {     long long amn, bmn, atg;     long long ls, rs;     smt *l, *r;     smt(long long la, long long ra) {       amn = inf;       bmn = -inf;       atg = 0;       ls = la;       rs = ra;       if (ls == rs) {         l = r = 0;       } else {         long long mid = (ls + rs) >> 1;         l = new smt(ls, mid);         r = new smt(mid + 1, rs);       }     }     void setg(long long first) {       if (ls == rs) {         amn = bmn = g[first];         return;       }       if (first <= l->rs)         l->setg(first);       else         r->setg(first);       amn = mmin(l->amn, r->amn);       bmn = mmin(l->bmn, r->bmn);     }     void push_down() {       l->atg += atg;       l->amn += atg;       r->atg += atg;       r->amn += atg;       atg = 0;     }     void add(long long la, long long ra, long long w) {       if (la <= ls && rs <= ra) {         atg += w;         amn += w;         return;       }       push_down();       if (la <= l->rs) l->add(la, ra, w);       if (ra >= r->ls) r->add(la, ra, w);       amn = mmin(l->amn, r->amn);     }     long long query(long long first, long long rm) {       if (ls > rm) return 0;       if (bmn - k > first) return 0;       if (ls == rs) return ls;       push_down();       if (mmin(first, l->amn) >= r->bmn - k) {         long long ans = r->query(mmin(first, l->amn), rm);         if (ans) return ans;       }       return l->query(first, rm);     }   };   smt *rt;   signed main() {     read(n, k);     rt = new smt(1, n);     for (long long i = 1; i < n; ++i) read(b[i]);     for (long long i = 1; i <= n; ++i) read(a[i]);     for (long long i = 1; i <= n; ++i) {       f[i] = f[i - 1] + a[i - 1] - b[i - 1];     }     for (long long i = 1; i <= n; ++i) {       g[i] = g[i - 1] + b[i - 1] - a[i];     }     long long ans = 0;     for (long long i = n; i; --i) {       rt->setg(i);       while (t && f[s[t]] >= f[i]) {         if (t > 1) {           rt->add(s[t - 1] - 1, n, f[s[t]] - f[s[t - 1]]);         }         --t;       }       s[++t] = i;       if (t > 1) assert(f[s[t]] >= f[s[t - 1]]);       if (t > 1) {         rt->add(s[t - 1] - 1, n, f[s[t - 1]] - f[s[t]]);       }       s[0] = n + 1;       long long l = 1, r = t, mid;       while (l <= r) {         mid = (l + r) >> 1;         if (f[i] - f[s[mid]] <= k)           r = mid - 1;         else           l = mid + 1;       }       chkmax(ans, rt->query(inf, s[r] - 1) - i + 1);     }     write(ans,   n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   istream &operator>>(istream &in, pair<T1, T2> &a) {     in >> a.first >> a.second;     return in;   }   template <typename T1, typename T2>   ostream &operator<<(ostream &out, pair<T1, T2> a) {     out << a.first <<     << a.second;     return out;   }   template <typename T, typename T1>   T maxs(T &a, T1 b) {     if (b > a) a = b;     return a;   }   template <typename T, typename T1>   T mins(T &a, T1 b) {     if (b < a) a = b;     return a;   }   const long long MOD = 1000000007;   struct mod_int {     long long val;     mod_int(long long v = 0) {       if (v < 0) v = v % MOD + MOD;       if (v >= MOD) v %= MOD;       val = v;     }     static long long mod_inv(long long a, long long m = MOD) {       long long g = m, r = a, first = 0, second = 1;       while (r != 0) {         long long q = g / r;         g %= r;         swap(g, r);         first -= q * second;         swap(first, second);       }       return first < 0 ? first + m : first;     }     explicit operator long long() const { return val; }     mod_int &operator+=(const mod_int &other) {       val += other.val;       if (val >= MOD) val -= MOD;       return *this;     }     mod_int &operator-=(const mod_int &other) {       val -= other.val;       if (val < 0) val += MOD;       return *this;     }     static unsigned fast_mod(uint64_t first, unsigned m = MOD) {       return first % m;       unsigned x_high = first >> 32, x_low = (unsigned)first;       unsigned quot, rem;       asm( divl %4 n  :  =a (quot),  =d (rem) :  d (x_high),  a (x_low),  r (m));       return rem;     }     mod_int &operator*=(const mod_int &other) {       val = fast_mod((uint64_t)val * other.val);       return *this;     }     mod_int &operator/=(const mod_int &other) { return *this *= other.inv(); }     friend mod_int operator+(const mod_int &a, const mod_int &b) {       return mod_int(a) += b;     }     friend mod_int operator-(const mod_int &a, const mod_int &b) {       return mod_int(a) -= b;     }     friend mod_int operator*(const mod_int &a, const mod_int &b) {       return mod_int(a) *= b;     }     friend mod_int operator/(const mod_int &a, const mod_int &b) {       return mod_int(a) /= b;     }     mod_int &operator++() {       val = val == MOD - 1 ? 0 : val + 1;       return *this;     }     mod_int &operator--() {       val = val == 0 ? MOD - 1 : val - 1;       return *this;     }     mod_int operator++(int32_t) {       mod_int before = *this;       ++*this;       return before;     }     mod_int operator--(int32_t) {       mod_int before = *this;       --*this;       return before;     }     mod_int operator-() const { return val == 0 ? 0 : MOD - val; }     bool operator==(const mod_int &other) const { return val == other.val; }     bool operator!=(const mod_int &other) const { return val != other.val; }     mod_int inv() const { return mod_inv(val); }     mod_int pow(long long p) const {       assert(p >= 0);       mod_int a = *this, result = 1;       while (p > 0) {         if (p & 1) result *= a;         a *= a;         p >>= 1;       }       return result;     }     friend ostream &operator<<(ostream &stream, const mod_int &m) {       return stream << m.val;     }     friend istream &operator>>(istream &stream, mod_int &m) {       return stream >> m.val;     }   };   const long long N = 1e5 + 5;   vector<long long> g[N], in[N], out[N];   mod_int p[N];   pair<long long, long long> dp[4 * N][22];   long long te = 0, temp = 0;   long long tin[N], LOG[4 * N], dep[N];   mod_int last = 1;   void dfs(long long u, long long p) {     dep[u] = dep[p] + 1;     te++;     tin[u] = te;     dp[te][0] = {dep[u], u};     for (auto i : g[u]) {       if (i != p) {         dfs(i, u);         te++;         dp[te][0] = {dep[u], u};       }     }   }   long long lca(long long u, long long v) {     long long l = min(tin[u], tin[v]);     long long r = max(tin[u], tin[v]);     long long j = LOG[r - l + 1];     pair<long long, long long> p = min(dp[l][j], dp[r - (1 << j) + 1][j]);     return p.second;   }   long long dis(long long u, long long v) {     return dep[u] + dep[v] - 2 * dep[lca(u, v)];   }   long long ctree[N];   long long sz[N];   long long vis[N];   mod_int dp1[N], dp2[N];   mod_int dp3[N], dp4[N];   void dfs1(long long u, long long par) {     sz[u] = 1;     temp++;     for (auto i : g[u]) {       if (i != par && !vis[i]) {         dfs1(i, u);         sz[u] += sz[i];       }     }   }   long long dfs2(long long u, long long par) {     for (auto i : g[u]) {       if (i != par && sz[i] > temp / 2 && !vis[i]) {         return dfs2(i, u);       }     }     return u;   }   void decompose(long long u, long long par) {     temp = 0;     dfs1(u, u);     long long centroid = dfs2(u, u);     ctree[centroid] = par;     vis[centroid] = 1;     for (auto i : g[centroid]) {       if (vis[i]) continue;       decompose(i, centroid);     }   }   mod_int t = 0;   void insert(long long u) {     t += p[u] * dp1[u];     dp2[u] += p[u];     long long i = u;     while (ctree[i] > 0) {       long long par = ctree[i];       mod_int d = dis(u, par);       t = t + d * p[u] * (dp2[par] - dp4[i]) + p[u] * (dp1[par] - dp3[i]);       dp2[par] += p[u], dp4[i] += p[u];       dp1[par] += p[u] * d, dp3[i] += p[u] * d;       i = ctree[i];     }   }   void remove(long long u) {     long long i = u;     t -= p[u] * dp1[u];     dp2[u] -= p[u];     while (ctree[i] > 0) {       long long par = ctree[i];       mod_int d = dis(u, par);       dp2[par] -= p[u];       dp4[i] -= p[u];       dp1[par] -= p[u] * d;       dp3[i] -= p[u] * d;       t = t - d * p[u] * (dp2[par] - dp4[i]) - p[u] * (dp1[par] - dp3[i]);       i = ctree[i];     }   }   long long solve() {     long long n;     cin >> n;     long long mx = 0;     for (long long i = 1; i < n + 1; i++) {       long long l;       long long r;       cin >> l >> r;       in[l].push_back(i), out[r].push_back(i);       mod_int val = r - l + 1;       p[i] = val.inv();       last *= val;       maxs(mx, r);     }     for (long long i = 0; i < n - 1; i++) {       long long u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1, 1);     mod_int ans = 0;     for (long long j = 1; j <= 20; j++) {       for (long long i = 1; i + (1 << (j - 1)) <= te; i++) {         dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);       }     }     decompose(1, 0);     for (long long i = 1; i <= mx; i++) {       for (auto j : in[i]) {         insert(j);       }       ans += t;       for (auto j : out[i]) {         remove(j);       }     }     cout << ans * last <<   n ;     return 0;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     for (long long i = 2; i < 4 * N; i++) {       LOG[i] = 1 + LOG[i / 2];     }     long long t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s[50];     bool ada[256] = {0};     double dekat[256];     for (int i = 0; i < 256; i++) dekat[i] = 1000000000.0;     int n, m;     double x;     cin >> n >> m >> x;     for (int i = 0; i < n; i++) cin >> s[i];     vector<pair<int, int> > v;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (s[i][j] ==  S ) v.push_back(make_pair(i, j));       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (s[i][j] ==  S ) continue;         ada[s[i][j]] = 1;         for (int k = 0; k < v.size(); k++) {           double temp = sqrt((v[k].first - i) * (v[k].first - i) +                              (v[k].second - j) * (v[k].second - j));           dekat[s[i][j]] = min(dekat[s[i][j]], temp);         }       }     }     int q;     cin >> q;     string qw;     cin >> qw;     int ans = 0;     for (int i = 0; i < qw.length(); i++) {       if (ada[qw[i]] == 1) continue;       if (qw[i] >=  a  && qw[i] <=  z  && v.size() > 0) {         if (ada[qw[i] - 32] == 0) {           ans = -1;           break;         } else {           if (dekat[qw[i] - 32] > x) {             ans++;           }         }       } else if (qw[i] >=  A  && qw[i] <=  Z  && v.size() > 0) {         if (ada[qw[i] + 32] == 0) {           ans = -1;           break;         } else {           if (dekat[qw[i] + 32] > x) {             ans++;           }         }       } else {         ans = -1;         break;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, cnt = 1, point = 0;     vector<int> x;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a >> b;       if (b == 0)         x.push_back(a);       else {         cnt += (b - 1);         point += a;       }     }     sort(x.begin(), x.end());     for (int j = x.size() - 1; j >= 0 && cnt > 0; j--, cnt--) {       point += x[j];     }     cout << point << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     char ch = getchar();     int nega = 1;     while (!isdigit(ch)) {       if (ch ==  - ) nega = -1;       ch = getchar();     }     int ans = 0;     while (isdigit(ch)) {       ans = ans * 10 + ch - 48;       ch = getchar();     }     if (nega == -1) return -ans;     return ans;   }   int fa[200005 * 2], siz[200005 * 2], n;   int find(int u) { return fa[u] == u ? u : fa[u] = find(fa[u]); }   int a[200005], b[200005];   vector<int> v[200005];   void merge(int u, int v) {     u = find(u), v = find(v);     if (u == v) return;     fa[u] = v;     siz[v] += siz[u];   }   bool vis[200005 * 2], ch[200005 * 2];   void work() {     cin >> n;     for (int i = 1; i <= n; i++) v[i].clear();     for (int i = 1; i <= n; i++) a[i] = read(), v[a[i]].push_back(i);     for (int i = 1; i <= n; i++) b[i] = read(), v[b[i]].push_back(i + n);     for (int i = 1; i <= 2 * n; i++) fa[i] = i;     for (int i = 1; i <= n; i++) siz[i] = 1;     for (int i = n + 1; i <= 2 * n; i++) siz[i] = 0;     for (int i = 1; i <= n * 2; i++) vis[i] = ch[i] = 0;     for (int i = 1; i <= n; i++) {       if (v[i].size() != 2) {         cout <<  -1 n ;         return;       }       int a = v[i][0], b = v[i][1];       if (a > b) swap(a, b);       if (a == b - n) continue;       if (a > n) {         a -= n, b -= n;         merge(a, b + n), merge(a + n, b);       } else if (b <= n)         merge(a, b + n), merge(a + n, b);       else {         b -= n;         merge(a, b), merge(a + n, b + n);       }     }     for (int i = 1; i <= n; i++) {       if (find(i) == find(i + n)) {         cout <<  -1 n ;         return;       }     }     int ans = 0;     for (int i = 1; i <= n; i++) {       int u = find(i), v = find(i + n);       if (vis[u] || vis[v]) continue;       ans += min(siz[u], siz[v]);       vis[u] = vis[v] = 1;       if (siz[u] < siz[v])         ch[u] = 1;       else         ch[v] = 1;     }     cout << ans << endl;     for (int i = 1; i <= n; i++) {       int u = find(i);       if (ch[u]) printf( %d  , i);     }     cout <<   n ;   }   signed main() {     int T = read();     while (T--) work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, x[101000], y[101000], fa[101000], sz[101000], tot[101000], u, v;   map<int, vector<int> > xx, yy;   map<int, vector<int> >::iterator it;   long long ans = 1, er[202000];   int GF(int x) { return fa[x] == x ? x : fa[x] = GF(fa[x]); }   void doit(vector<int> &the) {     for (int i = 1; i < (int)the.size(); i++)       if ((u = GF(the[i - 1])) != (v = GF(the[i]))) {         sz[v] += sz[u];         tot[v] += tot[u];         fa[u] = v;       }     tot[GF(the[0])]++;   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> x[i] >> y[i];       xx[x[i]].push_back(i);       yy[y[i]].push_back(i);       fa[i] = i;       sz[i] = 1;     }     for (it = xx.begin(); it != xx.end(); it++) doit(it->second);     for (it = yy.begin(); it != yy.end(); it++) doit(it->second);     er[0] = 1;     for (int i = 1; i < 202000; i++) er[i] = er[i - 1] * 2 % 1000000007;     for (int i = 1; i <= n; i++)       if (fa[i] == i) ans = ans * (er[tot[i]] - (tot[i] > sz[i])) % 1000000007;     cout << (ans + 1000000007) % 1000000007;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1);   int ans, pos[120000], fa[21][120000], tim, dep[120000], K, n, x, y;   vector<int> E[120000];   struct wph {     bool operator()(const int x, const int y) { return pos[x] < pos[y]; }   };   set<int, wph> S;   void prep(int x, int ff) {     fa[0][x] = ff;     for (int i = 1; i <= 20; ++i) fa[i][x] = fa[i - 1][fa[i - 1][x]];   }   void dfs(int x, int ff) {     pos[x] = ++tim;     prep(x, ff);     for (int i = 0; i < E[x].size(); ++i)       if (E[x][i] != ff) {         dep[E[x][i]] = dep[x] + 1;         dfs(E[x][i], x);       }   }   int pathlen(int x, int y) {     int ans = dep[x] + dep[y];     if (dep[x] < dep[y]) swap(x, y);     for (int i = 20; i >= 0; --i)       if ((dep[x] - dep[y]) >= 1 << i) x = fa[i][x];     if (x == y) return ans - 2 * dep[x];     for (int i = 20; i >= 0; --i)       if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];     return ans - 2 * dep[fa[0][x]];   }   int calc(set<int>::iterator it) {     int len = 0;     auto p1 = it, p2 = it;     if (p1 == S.begin()) p1 = S.end();     p1--;     len += pathlen(*p1, *it);     p2++;     if (p2 == S.end()) p2 = S.begin();     len += pathlen(*p2, *it);     len -= pathlen(*p2, *p1);     return len;   }   int main() {     scanf( %d%d , &n, &K);     for (int i = 1; i <= n - 1; ++i) {       scanf( %d%d , &x, &y);       E[x].push_back(y);       E[y].push_back(x);     }     dfs(1, 0);     ans = 1;     S.insert(1);     int j = 1, size = 0;     for (int i = 2; i <= n; ++i) {       auto p = S.insert(i).first;       size += calc(p);       while (size / 2 + 1 > K) {         p = S.find(j);         size -= calc(p);         S.erase(p);         j++;       }       ans = max(ans, i - j + 1);     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   const long long int lim = 1e17;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int T = clock();     long long int n, k;     cin >> n >> k;     map<long long int, long long int> mpp;     vector<long long int> a(n);     for (long long int i = 0; i < n; i++) cin >> a[i];     long long int tot = 0;     mpp[0] = 1;     long long int ans = 0;     for (long long int i = 0; i < n; i++) {       tot += a[i];       long long int r = 1;       if (k == -1) {         ans += mpp[tot - 1];         ans += mpp[tot + 1];       } else if (k == 1)         ans += mpp[tot - 1];       else {         while (r < lim) {           if (mpp.find(tot - r) != mpp.end()) ans += mpp[tot - r];           r *= k;         }       }       mpp[tot]++;     }     cout << ans;     cerr <<   n nTIME:   << (double)(clock() - T) / CLOCKS_PER_SEC <<   sec n ;     T = clock();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char a[103], b[103], c[103];   char aa[103], bb[103], cc[103];   char temp[103];   int temp2;   char ss[602];   char s[602];   int n;   int main() {     scanf( %s%s%s , aa, bb, cc);     scanf( %d , &n);     int i, j, jj;     int coun1 = 0, coun2 = 0, coun3 = 0;     for (i = 0; aa[i]; i++)       if (aa[i] !=  _  && aa[i] !=  ;  && aa[i] !=  - ) a[coun1++] = aa[i];     for (i = 0; bb[i]; i++)       if (bb[i] !=  _  && bb[i] !=  ;  && bb[i] !=  - ) b[coun2++] = bb[i];     for (i = 0; cc[i]; i++)       if (cc[i] !=  _  && cc[i] !=  ;  && cc[i] !=  - ) c[coun3++] = cc[i];     for (i = 0; a[i]; i++)       if (a[i] > 96) a[i] -= 32;     for (i = 0; b[i]; i++)       if (b[i] > 96) b[i] -= 32;     for (i = 0; c[i]; i++)       if (c[i] > 96) c[i] -= 32;     if (coun1 < coun3) {       for (i = 0; i < coun1; i++) temp[i] = a[i];       for (i = 0; i < coun3; i++) a[i] = c[i];       for (i = 0; i < coun1; i++) c[i] = temp[i];       temp2 = coun1;       coun1 = coun3;       coun3 = temp2;     }     if (coun1 < coun2) {       for (i = 0; i < coun1; i++) temp[i] = a[i];       for (i = 0; i < coun2; i++) a[i] = b[i];       for (i = 0; i < coun1; i++) b[i] = temp[i];       temp2 = coun1;       coun1 = coun2;       coun2 = temp2;     }     if (coun2 < coun3) {       for (i = 0; i < coun2; i++) temp[i] = b[i];       for (i = 0; i < coun3; i++) b[i] = c[i];       for (i = 0; i < coun2; i++) c[i] = temp[i];       temp2 = coun2;       coun2 = coun3;       coun3 = temp2;     }     a[coun1] =   0 ;     b[coun2] =   0 ;     c[coun3] =   0 ;     int coun;     bool flag;     bool flag2;     bool flagi[5];     for (i = 0; i < n; i++) {       coun = 0;       scanf( %s , ss);       for (j = 0; j < 5; j++) flagi[j] = false;       for (j = 0; ss[j]; j++)         if (ss[j] !=  ;  && ss[j] !=  _  && ss[j] !=  - ) s[coun++] = ss[j];       for (j = 0; j < coun; j++)         if (s[j] > 96) s[j] -= 32;       for (j = 0; j < coun; j++) {         flag = false;         flag2 = true;         for (jj = 0; a[jj]; jj++)           if (a[jj] != s[j + jj] || j + jj == coun) flag2 = false;         if (flag2 && !flagi[0]) {           flag = true;           flagi[0] = true;           j = j + jj - 1;           continue;         }         flag2 = true;         for (jj = 0; b[jj]; jj++)           if (b[jj] != s[j + jj] || j + jj == coun) flag2 = false;         if (flag2 && !flagi[1]) {           flag = true;           flagi[1] = true;           j = j + jj - 1;           continue;         }         flag2 = true;         for (jj = 0; c[jj]; jj++)           if (c[jj] != s[j + jj] || j + jj == coun) flag2 = false;         if (flag2 && !flagi[2]) {           flag = true;           flagi[2] = true;           j = j + jj - 1;           continue;         }         if (!flag) break;         if (j == coun - 1) break;       }       if (flag) {         for (j = 0; j < 3; j++)           if (!flagi[j]) flag = false;       }       if (flag)         printf( ACC n );       else         printf( WA n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> arr;   int dp[101][101][101][2];   int solve(int index, int even, int odd, int pre) {     if (index == n + 1) return 0;     if (dp[index][even][odd][pre] != -1) return dp[index][even][odd][pre];     if (arr[index] != 0) {       int ans = solve(index + 1, even, odd, arr[index] % 2);       if (pre != -1) {         if ((arr[index] % 2) != pre) ans++;       }       return ans;     }     int ans = INT_MAX;     if (even > 0) {       ans = min(ans, solve(index + 1, even - 1, odd, 0) + (pre == 1));     }     if (odd > 0) {       ans = min(ans, solve(index + 1, even, odd - 1, 1) + (pre == 0));     }     dp[index][even][odd][pre] = ans;     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(NULL);     cin >> n;     for (int i = 0; i < 101; i++) {       for (int j = 0; j < 101; j++) {         for (int k = 0; k < 101; k++) {           dp[i][j][k][0] = dp[i][j][k][1] = -1;         }       }     }     arr.resize(n + 1);     int even = n / 2;     int odd = (n + 1) / 2;     for (int i = 1; i <= n; i++) {       cin >> arr[i];       if (arr[i] % 2)         odd--;       else if (arr[i])         even--;     }     int ans = solve(1, even, odd, -1);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e5 + 5;   int parent[MAX];   int depth[MAX];   int get(int a) {     while (a != parent[a]) a = parent[a];     return a;   }   void join(int a, int b) {     a = get(a);     b = get(b);     if (a == b) return;     if (depth[a] > depth[b]) swap(a, b);     parent[a] = b;     depth[b] += depth[a];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, q, a, b, w;     cin >> n >> q;     for (int i = 1; i <= n + 1; i++) {       parent[i] = i;       depth[i] = 1;     }     vector<pair<int, pair<int, int>>> vp;     vector<long long> sol(MAX);     long long cnt = 0;     for (int i = 0; i < n - 1; i++) {       cin >> a >> b >> w;       vp.push_back({w, {a, b}});     }     sort(vp.begin(), vp.end());     reverse(vp.begin(), vp.end());     for (int j = 1; j < MAX; j++) {       while (!vp.empty() && vp.back().first <= j) {         a = vp.back().second.first;         b = vp.back().second.second;         vp.pop_back();         a = get(a);         b = get(b);         if (a == b) continue;         cnt -= 1ll * depth[a] * (depth[a] - 1) / 2;         cnt -= 1ll * depth[b] * (depth[b] - 1) / 2;         join(a, b);         a = get(a);         cnt += 1ll * depth[a] * (depth[a] - 1) / 2;       }       sol[j] = cnt;     }     while (q--) {       cin >> w;       cout << sol[w] <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     getline(cin, s);     long long n = s.length(), i = n - 1;     while (1) {       if ((s[i] >= 65 && s[i] <= 91) || (s[i] >= 97 && s[i] <= 122)) {         if (s[i] ==  a  || s[i] ==  y  || s[i] ==  e  || s[i] ==  i  ||             s[i] ==  o  || s[i] ==  u ) {           cout <<  YES  << endl;           break;         } else if (s[i] ==  A  || s[i] ==  Y  || s[i] ==  E  || s[i] ==  I  ||                    s[i] ==  O  || s[i] ==  U ) {           cout <<  YES  << endl;           break;         } else {           cout <<  NO  << endl;           break;         }       }       i--;     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1 << 30;   const long long inf = 1LL << 60;   const int N = 200010;   struct Edge {     int to, next;   } e1[N], e2[N];   int list1[N], list2[N], d1, d2;   priority_queue<pair<int, int> > Q;   queue<int> S;   bool vis[N];   vector<int> v[N];   void add(int x, int y, int *list, int &d, Edge *e) {     e[d].to = y, e[d].next = list[x], list[x] = d++;   }   int n, m, k;   int ans[N], Min[N], din[N], dout[N], d[N];   int read() {     int w(0), f(0);     char c = getchar();     while ((c <  0  || c >  9 ) && c !=  - ) c = getchar();     if (c ==  - ) f = 1, c = getchar();     while (c >=  0  && c <=  9 ) w = w * 10 + c -  0 , c = getchar();     return f ? -w : w;   }   void Imps() {     puts( -1 );     exit(0);   }   void Delete(int x) {     int y;     for (int i = list2[x]; i >= 0; i = e2[i].next) {       y = e2[i].to;       d[y]--;       if (!d[y] && !ans[y]) Q.push(make_pair(Min[y], y));     }   }   int main() {     n = read(), m = read(), k = read();     memset(list1, -1, 4 * (n + 1)), memset(list2, -1, 4 * (n + 1));     for (int i = 1; i <= n; ++i)       ans[i] = read(), vis[ans[i]] = 1, v[ans[i]].push_back(i);     int x, y;     pair<int, int> tmp;     for (int i = 1; i <= m; ++i) {       x = read(), y = read();       add(y, x, list1, d1, e1);       add(x, y, list2, d2, e2);       din[x]++, dout[y]++;     }     for (int i = 1; i <= n; ++i) {       d[i] = din[i];       if (!d[i]) S.push(i);     }     while (!S.empty()) {       x = S.front(), S.pop();       if (ans[x]) {         if (ans[x] < Min[x])           Imps();         else           Min[x] = ans[x];       } else if (!Min[x])         Min[x] = 1;       for (int i = list1[x]; i >= 0; i = e1[i].next) {         y = e1[i].to;         Min[y] = max(Min[y], Min[x] + 1);         d[y]--;         if (!d[y]) S.push(y);       }     }     for (int i = 1; i <= n; ++i) {       d[i] = dout[i];       if (!d[i] && !ans[i]) Q.push(make_pair(Min[i], i));     }     for (int i = k; i >= 1; --i) {       for (int j = 0; j < v[i].size(); ++j) {         x = v[i][j];         Delete(x);       }       while (!Q.empty()) {         tmp = Q.top();         if (tmp.first > i)           Imps();         else if (tmp.first == i)           ans[tmp.second] = i, vis[i] = 1, Q.pop(), Delete(tmp.second);         else           break;       }       if (!vis[i]) {         if (!Q.empty()) {           tmp = Q.top();           ans[tmp.second] = i, vis[i] = 1, Q.pop(), Delete(tmp.second);         } else           Imps();       }     }     for (x = 1; x <= n; ++x)       for (int i = list1[x]; i >= 0; i = e1[i].next)         if (ans[x] >= ans[e1[i].to]) Imps();     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       long long move = 0;       for (int i = 1; i <= n / 2; ++i) {         move += i * 1ll * i;       }       cout << move * 8 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, t, a, b, da, db;     while (cin >> x >> t >> a >> b >> da >> db) {       bool f = 0;       for (int i = 0; i <= t; i++) {         for (int j = 0; j <= t; j++) {           int t1, t2;           if (i == t)             t1 = 0;           else             t1 = a - i * da;           if (j == t)             t2 = 0;           else             t2 = b - j * db;           if (t1 + t2 == x) {             f = 1;             break;           }         }       }       if (f)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   void func(void) {     freopen( input.c ,  r , stdin);     freopen( output.c ,  w , stdout);   }   void print(vector<long long> &v) {     cout << v.size() << endl;     for (int i = 0; i < v.size(); i++) {       printf( %lld  , v[i]);     }     printf(  n );   }   void print(vector<pair<long long, long long> > &v) {     cout << v.size() << endl;     for (int i = 0; i < v.size(); i++) {       printf( %lld %lld n , v[i].first, v[i].second);     }   }   void print(double d) { cout << fixed << setprecision(10) << d << endl; }   void print(string s, double d) {     cout << s <<    ;     cout << fixed << setprecision(10) << d << endl;   }   const int N = 1e5 + 100;   struct info {     long long day, cost, place;     bool operator<(info k1) const { return day < k1.day; }   };   vector<info> in;   vector<info> out;   set<pair<long long, long long> > outFlight[N];   long long inFlight[N];   int main() {     long long n, q, i, j = 0, temp, t, k, ans = 0, sum = 0, x, y, z, cnt = 0, m,                        fg = 0, mx = 0, mx1 = 0, mn = 8e18,                        mn1 = 8000000000000000000;     scanf( %lld %lld %lld , &n, &m, &k);     for (int i = 0; i < m; i++) {       scanf( %lld %lld , &x, &y);       scanf( %lld %lld , &z, &t);       info xx;       xx.day = x;       xx.place = y;       if (y == 0) xx.place = z;       xx.cost = t;       if (y)         in.push_back(xx);       else         out.push_back(xx);     }     sort(in.begin(), in.end());     sort(out.begin(), out.end());     for (int i = 0; i < N; i++) inFlight[i] = 1e10;     int ok = 0;     for (int i = 0; i < out.size(); i++) {       outFlight[out[i].place].insert({out[i].cost, i});     }     long long outSum = 0;     long long inSum = 0;     for (int i = 1; i <= n; i++) {       if (outFlight[i].size() == 0) {         printf( -1 n );         return 0;       }       auto it = outFlight[i].begin();       pair<long long, long long> p = *it;       outSum += p.first;     }     cnt = 0;     for (int i = 0; i < in.size(); i++) {       long long now = in[i].day;       long long place = in[i].place;       long long cost = in[i].cost;       if (inFlight[place] == 1e10) {         inFlight[place] = cost;         inSum += cost;         cnt++;       } else {         inSum -= inFlight[place];         inFlight[place] = min(inFlight[place], cost);         inSum += inFlight[place];       }       now += k + 1;       fg = 0;       for (; ok < out.size(); ok++) {         if (out[ok].day >= now) break;         auto it = outFlight[out[ok].place].begin();         pair<long long, long long> p = *it;         outSum -= p.first;         p = {out[ok].cost, ok};         outFlight[out[ok].place].erase(p);         if (outFlight[out[ok].place].size() == 0) {           fg = 1;           break;         }         it = outFlight[out[ok].place].begin();         p = *it;         outSum += p.first;       }       if (fg) break;       if (cnt == n) {         mn = min(mn, inSum + outSum);       }     }     if (mn == 8e18) mn = -1;     cout << mn << endl;   }


#include <bits/stdc++.h>   using namespace std;   int flag = 0, ans, n;   int r[10], d[12], l[12], u[12];   int print(int x1, int y1, int x2, int y2) {     cout <<  ?   << x1 <<     << y1 <<     << x2 <<     << y2 << endl;     int val;     cin >> val;     if (flag) {       if (x1 <= l[0] && r[0] <= x2 && y1 <= d[0] && u[0] <= y2) return val - 1;     }     return val;   }   int findright(int l, int r) {     if (l > r) return 0;     int mid = (l + r) / 2;     int val = print(1, 1, mid, n);     if (val > 0) {       ans = mid;       findright(l, mid - 1);     } else {       findright(mid + 1, r);     }     return 0;   }   int findleft(int l, int r, int r1) {     if (l > r) return 0;     int mid = (l + r) / 2;     int val = print(mid, 1, r1, n);     if (val > 0) {       ans = mid;       findleft(mid + 1, r, r1);     } else {       findleft(l, mid - 1, r1);     }     return 0;   }   int findup(int l, int r, int l1, int r1) {     if (l > r) return 0;     int mid = (l + r) / 2;     int val = print(l1, 1, r1, mid);     if (val > 0) {       ans = mid;       findup(l, mid - 1, l1, r1);     } else {       findup(mid + 1, r, l1, r1);     }     return 0;   }   int finddown(int l, int r, int l1, int r1, int u1) {     if (l > r) return 0;     int mid = (l + r) / 2;     int val = print(l1, mid, r1, u1);     if (val > 0) {       ans = mid;       finddown(mid + 1, r, l1, r1, u1);     } else {       finddown(l, mid - 1, l1, r1, u1);     }     return 0;   }   int main() {     std::ios::sync_with_stdio(false);     int i;     cin >> n;     int r1, l1, u1, d1;     findright(1, n);     r1 = ans;     findleft(1, r1, r1);     l1 = ans;     l[0] = l1;     r[0] = r1;     findup(1, n, l1, r1);     u1 = ans;     finddown(1, u1, l1, r1, u1);     d1 = ans;     u[0] = u1;     d[0] = d1;     flag = 1;     findright(1, n);     r1 = ans;     findleft(1, r1, r1);     l1 = ans;     l[1] = l1;     r[1] = r1;     findup(1, n, l1, r1);     u1 = ans;     finddown(1, u1, l1, r1, u1);     d1 = ans;     u[1] = u1;     d[1] = d1;     cout <<  !  ;     for (i = 0; i < 1; i++) {       cout << l[i] <<     << d[i] <<     << r[i] <<     << u[i] <<    ;     }     i = 1;     cout << l[i] <<     << d[i] <<     << r[i] <<     << u[i];   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)2e5 + 88;   char tu[maxn];   int lu[maxn];   int main() {     int n, a, b, k;     cin >> n >> a >> b >> k;     scanf( %s , tu);     for (int i = 0; i < maxn; i++) {       lu[i] = 0;     }     int ans = 0;     int ant = 0;     int ttt = 0;     for (int i = 0; i < n; i++) {       if (tu[i] ==  0 ) {         ant++;       } else         ant = 0;       if (ant != 0 && ant % b == 0) {         ans++;         if (ans >= a) {           lu[ttt++] = i;         }       }     }     cout << ttt << endl;     for (int i = 0; i < ttt; i++) {       cout << lu[i] + 1 <<    ;     }   }


#include <bits/stdc++.h>   using std::cerr;   using std::cin;   using std::cout;   using std::endl;   using std::list;   using std::map;   using std::max;   using std::min;   using std::pair;   using std::set;   using std::sort;   using std::string;   using std::swap;   using std::vector;   const int MAXN = 100005;   const long long INF = 100000000009;   const long long MOD = (long long)1e9 + 7;   bool used[MAXN];   bool cnt[MAXN][50];   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     int n;     cin >> n;     string sec;     cin >> sec;     for (int i = 0; i < n; ++i) {       if (sec[i] !=  * ) {         used[(int)sec[i]] = true;       }     }     int m;     cin >> m;     vector<string> needed;     for (int i = 0; i < m; ++i) {       string s;       cin >> s;       bool flag = true;       for (int j = 0; j < n; ++j) {         if (sec[j] ==  * ) {           if (used[(int)s[j]]) {             flag = false;             break;           }         } else if (sec[j] != s[j]) {           flag = false;           break;         }       }       if (flag) {         needed.push_back(s);       }     }     for (int j = 0; j < needed.size(); ++j) {       for (int i = 0; i < n; ++i) {         if (sec[i] ==  * ) {           cnt[j][(int)needed[j][i] -  a ] = true;         }       }     }     int ans = 0;     for (int i = 0; i <= (int) z  - (int) a ; ++i) {       bool flag = true;       for (int j = 0; j < needed.size(); ++j) {         if (!cnt[j][i]) {           flag = false;         }       }       if (flag) {         ++ans;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   #pragma clang diagnostic push   #pragma clang diagnostic ignored  -Wunused-const-variable    using namespace std;   template <typename T>   class Modular {    public:     using Type = typename decay<decltype(T::value)>::type;     constexpr Modular() : value() {}     template <typename U>     Modular(const U& x) {       value = normalize(x);     }     static Type inverse(Type a, Type mod) {       Type b = mod, x = 0, y = 1;       while (a != 0) {         Type t = b / a;         b -= a * t;         x -= t * y;         swap(a, b);         swap(x, y);       }       if (x < 0) x += mod;       return x;     }     template <typename U>     static Type normalize(const U& x) {       Type v;       if (-mod() <= x && x < mod())         v = static_cast<Type>(x);       else         v = static_cast<Type>(x % mod());       if (v < 0) v += mod();       return v;     }     const Type& operator()() const { return value; }     template <typename U>     explicit operator U() const {       return static_cast<U>(value);     }     constexpr static Type mod() { return T::value; }     Modular& operator+=(const Modular& other) {       if ((value += other.value) >= mod()) value -= mod();       return *this;     }     Modular& operator-=(const Modular& other) {       if ((value -= other.value) < 0) value += mod();       return *this;     }     template <typename U>     Modular& operator+=(const U& other) {       return *this += Modular(other);     }     template <typename U>     Modular& operator-=(const U& other) {       return *this -= Modular(other);     }     Modular& operator++() { return *this += 1; }     Modular& operator--() { return *this -= 1; }     Modular operator++(int) {       Modular result(*this);       *this += 1;       return result;     }     Modular operator--(int) {       Modular result(*this);       *this -= 1;       return result;     }     Modular operator-() const { return Modular(-value); }     template <typename U = T>     typename enable_if<is_same<typename Modular<U>::Type, int>::value,                        Modular>::type&     operator*=(const Modular& rhs) {       value = normalize(static_cast<int64_t>(value) *                         static_cast<int64_t>(rhs.value));       return *this;     }     template <typename U = T>     typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value,                        Modular>::type&     operator*=(const Modular& rhs) {       int64_t q = static_cast<int64_t>(static_cast<long double>(value) *                                        rhs.value / mod());       value = normalize(value * rhs.value - q * mod());       return *this;     }     template <typename U = T>     typename enable_if<!is_integral<typename Modular<U>::Type>::value,                        Modular>::type&     operator*=(const Modular& rhs) {       value = normalize(value * rhs.value);       return *this;     }     Modular& operator/=(const Modular& other) {       return *this *= Modular(inverse(other.value, mod()));     }     template <typename U>     friend const Modular<U>& abs(const Modular<U>& v) {       return v;     }     template <typename U>     friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);     template <typename U>     friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);     template <typename U>     friend std::istream& operator>>(std::istream& stream, Modular<U>& number);       private:     Type value;   };   template <typename T>   bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) {     return lhs.value == rhs.value;   }   template <typename T, typename U>   bool operator==(const Modular<T>& lhs, U rhs) {     return lhs == Modular<T>(rhs);   }   template <typename T, typename U>   bool operator==(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) == rhs;   }   template <typename T>   bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) {     return !(lhs == rhs);   }   template <typename T, typename U>   bool operator!=(const Modular<T>& lhs, U rhs) {     return !(lhs == rhs);   }   template <typename T, typename U>   bool operator!=(U lhs, const Modular<T>& rhs) {     return !(lhs == rhs);   }   template <typename T>   bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) {     return lhs.value < rhs.value;   }   template <typename T>   Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) += rhs;   }   template <typename T, typename U>   Modular<T> operator+(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) += rhs;   }   template <typename T, typename U>   Modular<T> operator+(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) += rhs;   }   template <typename T>   Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) -= rhs;   }   template <typename T, typename U>   Modular<T> operator-(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) -= rhs;   }   template <typename T, typename U>   Modular<T> operator-(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) -= rhs;   }   template <typename T>   Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) *= rhs;   }   template <typename T, typename U>   Modular<T> operator*(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) *= rhs;   }   template <typename T, typename U>   Modular<T> operator*(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) *= rhs;   }   template <typename T>   Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) /= rhs;   }   template <typename T, typename U>   Modular<T> operator/(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) /= rhs;   }   template <typename T, typename U>   Modular<T> operator/(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) /= rhs;   }   template <typename T, typename U>   Modular<T> power(const Modular<T>& a, const U& b) {     assert(b >= 0);     Modular<T> x = a, res = 1;     U p = b;     while (p > 0) {       if (p & 1) res *= x;       x *= x;       p >>= 1;     }     return res;   }   template <typename T>   bool IsZero(const Modular<T>& number) {     return number() == 0;   }   template <typename T>   string to_string(const Modular<T>& number) {     return to_string(number());   }   template <typename T>   std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {     return stream << number();   }   template <typename T>   std::istream& operator>>(std::istream& stream, Modular<T>& number) {     typename common_type<typename Modular<T>::Type, int64_t>::type x;     stream >> x;     number.value = Modular<T>::normalize(x);     return stream;   }   const int md = 1e9 + 7;   using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;   long long sqr(long long x) { return x * x; }   int mysqrt(long long x) {     int l = 0, r = 1e9 + 1;     while (r - l > 1) {       int m = (l + r) / 2;       if (m * (long long)m <= x)         l = m;       else         r = m;     }     return l;   }   mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());   mt19937_64 rndll(       chrono::high_resolution_clock::now().time_since_epoch().count());   template <typename T>   T gcd(T a, T b) {     return a ? gcd(b % a, a) : b;   }   int gcdex(int a, int b, int& x, int& y) {     if (a == 0) {       x = 0;       y = 1;       return b;     }     int x1, y1;     int ret = gcdex(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return ret;   }   void setmin(int& x, int y) { x = min(x, y); }   void setmax(int& x, int y) { x = max(x, y); }   void setmin(long long& x, long long y) { x = min(x, y); }   void setmax(long long& x, long long y) { x = max(x, y); }   const long long llinf = 4e18 + 100;   const long double eps = 1e-9, PI = atan2(0, -1);   const int maxn = 2e5 + 100, maxw = 1e4 + 100, inf = 1e9 + 100, sq = 450,             LG = 18, mod = 1e9 + 933;   int tp[maxn], h[maxn], n;   vector<int> e[maxn];   long long q[maxn][2];   void dfs(int v, int par) {     int X = 0, Y = 0;     vector<long long> g;     long long cur = 0;     for (int i : e[v])       if (i != par) {         dfs(i, v);         if (h[i] == h[v]) {           g.push_back(q[i][1] - q[i][0]);           cur += q[i][0];           X++;         } else if (h[i] < h[v]) {           X++;           cur += q[i][0];         } else {           Y++;           cur += q[i][1];         }       }     sort(g.begin(), g.end());     q[v][0] = q[v][1] = llinf;     auto upd = [&](int t, int x, int y) {       setmin(q[v][t], max(x, y) * (long long)tp[v] + cur);     };     for (int i = 0; i <= g.size(); i++) {       if (par == -1)         upd(0, X, Y);       else {         upd(0, X, Y + 1);         upd(1, X + 1, Y);       }       if (i < g.size()) {         cur += g[i];         X--;         Y++;       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) cin >> tp[i];     for (int i = 0; i < n; i++) cin >> h[i];     for (int i = 1; i < n; i++) {       int v, u;       cin >> v >> u;       v--;       u--;       e[v].push_back(u);       e[u].push_back(v);     }     dfs(0, -1);     cout << q[0][0] <<   n ;   }