#include <bits/stdc++.h>   using namespace std;   const long long int N = 1001;   long long int n, m, k;   vector<pair<long long int, long long int> > adj[N];   vector<long long int> func(long long int x) {     vector<long long int> dis(n + 1, INT_MAX);     if (x == 0) {       return dis;     }     vector<int> vis(n + 1, 0);     set<pair<long long int, long long int> > s;     s.insert(make_pair(0, x));     dis[x] = 0;     while (!s.empty()) {       long long int v = (*s.begin()).second;       s.erase(s.begin());       if (vis[v]) {         continue;       }       vis[v] = 1;       for (auto u : adj[v]) {         if (dis[u.first] > dis[v] + u.second) {           dis[u.first] = dis[v] + u.second;           s.insert(make_pair(dis[u.first], u.first));         }       }     }     return dis;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> m >> k;     vector<pair<pair<long long int, long long int>, long long int> > edges;     for (long long int i = 0; i < m; ++i) {       long long int a, b, w;       cin >> a >> b >> w;       adj[a].push_back(make_pair(b, w));       adj[b].push_back(make_pair(a, w));       edges.push_back(make_pair(make_pair(a, b), w));     }     vector<pair<long long int, long long int> > queries(k);     for (long long int i = 0; i < k; ++i) {       cin >> queries[i].first >> queries[i].second;     }     vector<vector<long long int> > dis;     for (long long int i = 0; i <= n; ++i) {       dis.push_back(func(i));     }     long long int ans = 0;     for (long long int i = 0; i < k; ++i) {       ans += dis[queries[i].first][queries[i].second];     }     for (long long int i = 0; i < m; ++i) {       long long int a = edges[i].first.first;       long long int b = edges[i].first.second;       long long int w = edges[i].second;       long long int cost = 0;       for (long long int j = 0; j < k; ++j) {         long long int x = queries[j].first, y = queries[j].second;         long long int price = dis[x][y];         if (dis[x][a] < dis[x][b] && dis[y][a] > dis[y][b]) {           price = min(price, dis[x][a] + dis[b][y]);         } else if (dis[x][b] < dis[x][a] && dis[y][a] < dis[y][b]) {           price = min(price, dis[x][b] + dis[y][a]);         }         cost += price;       }       ans = min(ans, cost);     }     cout << ans;     return (0);   }


#include <bits/stdc++.h>   using namespace std;   struct node {     long long x;     long long y;     long long id;   } a[200005];   long long n, t;   long long totx, toty;   inline void read(long long &x) {     long long f;     char c;     for (f = 1, c = getchar(); c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = -1;     for (x = 0; c <=  9  && c >=  0 ; c = getchar()) x = x * 10 + (c & 15);     x *= f;   }   inline bool cmp(node p, node q) { return p.y < q.y; }   int main() {     read(t);     while (t--) {       totx = toty = 0;       read(n);       for (int i = 1; i < 2 * n; i++) {         read(a[i].x);         read(a[i].y);         a[i].id = (long long)i;         totx += a[i].x;       }       sort(a + 1, a + 2 * n, cmp);       for (int i = 1; i < 2 * n; i += 2) toty += a[i].x;       puts( YES );       if (toty * 2 >= totx) {         for (int i = 1; i < 2 * n; i += 2) printf( %lld  , a[i].id);         puts(  );       } else {         for (int i = 2; i < 2 * n; i += 2) printf( %lld  , a[i].id);         printf( %lld n , a[2 * n - 1].id);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<vector<int> > G(n + 5, vector<int>(0));     int c[m + 5][2];     memset(c, (0), sizeof(c));     for (int i = 1; i <= m; i++) {       int l, r;       cin >> l >> r;       for (int j = l; j <= r; j++) G[j].push_back(i);     }     int ans[n + 5];     for (int i = 1; i <= n; i++) {       int rs = 0, ls = 0;       int put = 0;       for (int j = 0; j < int(G[i].size()); j++) {         int v = G[i][j];         rs += c[v][0];         ls += c[v][1];         if (rs > ls)           put = 1;         else           put = 0;       }       ans[i] = put;       for (int j = 0; j < int(G[i].size()); j++) {         int v = G[i][j];         c[v][put]++;       }     }     for (int i = 1; i <= n; i++) cout << ans[i];     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, r[20], b[20];   char ch[20];   long long dp[(1 << 16)][260];   long long dfs(int bit, int discount) {     long long& ret = dp[bit][discount];     if (ret != -1) return ret;     if (bit == (1 << n) - 1) {       return ret = 0;     }     ret = 0xffffffffffffff;     int Rcard = 0, Bcard = 0, cnt = 0;     ;     long long Rused = 0, Bused = 0;     for (int i = 0; i < n; i++) {       if (bit & (1 << i)) {         ++cnt;         Rused += r[i];         Bused += b[i];         if (ch[i] ==  R )           ++Rcard;         else           Bcard++;       }     }     int gap = discount - 120;     long long remainR = 0, remainB = 0;     if (gap > 0)       Rused -= gap;     else       Bused += gap;     if (Rused > Bused) {       remainB = Rused - Bused;     } else {       remainR = Bused - Rused;     }     for (int i = 0; i < n; i++) {       if (!(bit & (1 << i))) {         long long maxi = max((long long)(r[i] - Rcard - remainR),                              (long long)(b[i] - Bcard - remainB));         maxi = max(maxi, (long long)0);         long long discountR = min(r[i], Rcard);         long long discountB = min(b[i], Bcard);         long long go = dfs(bit | (1 << i), discount + discountR - discountB);         ret = min(ret, go + maxi);       }     }     return ret;   }   int main() {     memset(dp, -1, sizeof(dp));     scanf( %d , &n);     for (int i = 0; i < n; i++) {       cin >> ch[i];       scanf( %d %d , &r[i], &b[i]);     }     long long ans = dfs(0, 120);     ans += n;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i = 0, j = 0, flag = 0, hash[100002] = {0}, stack[100002] = {0},            top = -1;     string x;     char item;     getline(cin, x);     n = x.length();     for (i = 0; i < n; i++) {       if (x[i] ==  ( ) {         stack[++top] = i;       }       if (x[i] ==  ) ) {         if (top < 0) {           flag = 1;           break;         } else {           item = stack[top];           top--;         }       }       if (x[i] ==  # ) {         hash[j] = i;         j++;         if (top < 0) {           flag = 1;           break;         }       }     }     if (flag == 1 || stack[top] > hash[j - 1]) {       cout <<  -1 ;       return 0;     } else {       for (i = 0; i < j - 1; i++) {         hash[i] = 1;         item = stack[top];         top--;       }     }     if (top < 0)       cout <<  -1 ;     else {       hash[i] = top + 1;       for (i = 0; i < j; i++) {         cout << hash[i] << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long phi(long long x);   const long long mod = 1e9 + 7;   int main() {     long long N, K;     cin >> N >> K;     while (K > 0 && N > 1) {       N = phi(N);       K -= 2;     }     cout << N % mod << endl;   }   long long phi(long long x) {     int R = sqrt(x);     long long ans = 1;     for (int p = 2; p <= R; ++p) {       if (x % p == 0) {         long long curr = 1;         while (x % p == 0) {           curr *= p;           x /= p;         }         ans *= (curr - curr / p);       }     }     if (x > 1) {       ans *= (x - 1);     }     return ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n;       string x;       cin >> n >> x;       string a(n,  0 ), b(n,  0 );       for (long long i = 0; i < n; ++i) {         if (x[i] ==  1 ) {           a[i] =  1 ;           b[i] =  0 ;           for (int j = i + 1; j < n; ++j) {             b[j] = x[j];           }           break;         } else {           a[i] = b[i] =  0  + (x[i] -  0 ) / 2;         }       }       cout << a << endl << b << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }   const int INF = 1 << 30;   const double eps = 1e-8;   const int row = 3000;   const int cow = 220;   const int MAXN = cow * cow;   int n, m;   long long a[50000];   long long sum[50000];   char s[5000];   int main() {     int i, j, t;     long long k;     cin >> k;     scanf( %s , &s);     n = strlen(s);     sum[0] = 0;     for (i = 1; i <= n; i++) sum[i] = sum[i - 1] + s[i - 1] -  0 ;     memset(a, 0, sizeof(a));     for (i = 0; i <= n; i++) {       for (j = i + 1; j <= n; j++) {         a[sum[j] - sum[i]]++;       }     }     long long ans = 0;     if (k == 0) {       ans += (a[0] * a[0]);       for (i = 1; i < 50000; i++) {         ans += (a[0] * a[i] * 2);       }     } else {       for (i = 1; i <= 40000; i++) {         if (k % i == 0 && k / i <= 40000) {           ans += (a[i] * a[k / i]);         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   signed main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, x, y;       cin >> n >> x >> y;       vector<int> a(n);       for (auto &el : a) {         cin >> el;         el--;       }       int need = y - x;       vector<vector<int>> where(n + 1);       for (int i = 0; i < n; i++) {         where[a[i]].push_back(i);       }       int free = 0;       while (where[free].size()) {         free++;       }       set<pair<int, int>, greater<pair<int, int>>> st;       for (int i = 0; i < n + 1; i++) {         st.insert({where[i].size(), i});       }       vector<int> ans(n, free);       while (x--) {         auto [sz, x] = *st.begin();         st.erase(st.begin());         ans[where[x].back()] = x;         where[x].pop_back();         st.insert({sz - 1, x});       }       int d = st.begin()->first;       vector<pair<int, int>> poses;       while (st.size()) {         auto [sz, x] = *st.begin();         st.erase(st.begin());         while (where[x].size()) {           poses.emplace_back(where[x].back(), x);           where[x].pop_back();         }       }       for (int i = 0; i < poses.size() && need; i++) {         int j = (i + d) % poses.size();         if (poses[j].second != poses[i].second) {           need--;           ans[poses[j].first] = poses[i].second;         }       }       if (need) {         cout <<  NO n ;         continue;       }       cout <<  YES n ;       for (auto &el : ans) {         cout << el + 1 <<    ;       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int M = 2e5 + 10;   struct Edge {     int u, v, cap, flow = 0;     Edge(int u, int v, int cap) : u(u), v(v), cap(cap) {}   };   class Dinic {    public:     vector<Edge> edges;     vector<vector<int>> G;     int n, m = 0, s, t;     vector<int> H, ptr;     queue<int> q;     Dinic(int n, int s, int t) : n(n), s(s), t(t) {       G.resize(n + 1);       H = ptr = vector<int>(n + 1);     }     void add(int u, int v, int cap) {       edges.emplace_back(u, v, cap);       edges.emplace_back(v, u, 0);       G[u].push_back(m++);       G[v].push_back(m++);     }     bool bfs() {       while (!q.empty()) {         int u = q.front();         q.pop();         for (int v : G[u]) {           int to = edges[v].v;           int gap = edges[v].cap - edges[v].flow;           if (H[to] != -1 or gap < 1) continue;           H[to] = H[u] + 1, q.push(to);         }       }       return H[t] != -1;     }     int dfs(int u, int X) {       if (!X) return 0;       if (u == t) return X;       for (int &i = ptr[u]; i < (int)(G[u]).size(); ++i) {         int v = G[u][i], to = edges[v].v;         int gap = edges[v].cap - edges[v].flow;         if (H[to] != (H[u] + 1) or gap < 1) continue;         int Y = dfs(to, min(X, gap));         if (!Y) continue;         edges[v].flow += Y;         edges[v ^ 1].flow -= Y;         return Y;       }       return 0;     }     int flow() {       int ans = 0;       while (true) {         fill(H.begin(), H.end(), -1);         H[s] = 0, q.push(s);         if (!bfs()) break;         fill(ptr.begin(), ptr.end(), 0);         while (int X = dfs(s, (int)(1e18))) ans += X;       }       return ans;     }   };   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<pair<int, pair<int, int>>> edges;     vector<int> cand;     for (int i = 0; i < m; ++i) {       int u, v, w;       cin >> u >> v >> w;       edges.push_back({w, {u, v}});       cand.push_back(w);     }     sort((cand).begin(), (cand).end());     cand.erase(unique((cand).begin(), (cand).end()), cand.end());     int lo = 0, hi = (int)(cand).size() - 1, mid, ans = -1;     int s = 0, t = 2 * n + 1;     while (lo <= hi) {       mid = (lo + hi) >> 1;       Dinic dinic(2 * n + 2, s, t);       for (int i = 1; i <= n; ++i) {         dinic.add(s, i, 1);         dinic.add(i + n, t, 1);       }       for (auto [w, p] : edges)         if (w <= cand[mid]) dinic.add(p.first, p.second + n, 1);       int x = dinic.flow();       if (x == n)         hi = mid - 1, ans = cand[mid];       else         lo = mid + 1;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       int arr[n + 1];       for (int i = 1; i <= n; i++) {         cin >> arr[i];       }       int peaks = 0, ind = 1;       for (int i = 2; i < k; i++) {         if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) peaks++;       }       int ans = peaks;       for (int j = 2; j + k - 1 <= n; j++) {         if (arr[j] > arr[j - 1] && arr[j] > arr[j + 1] && ans > 0) ans--;         if (arr[j + k - 2] > arr[j + k - 1] && arr[j + k - 2] > arr[j + k - 3])           ans++;         if (ans > peaks) {           peaks = ans;           ind = j;         }       }       cout << peaks + 1 <<     << ind << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<int> a[n];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int x;         cin >> x;         x--;         a[i].push_back(x);       }     }     int ans = 0;     for (int j = 0; j < m; j++) {       vector<int> cnt(n);       int c = 0;       for (int i = 0; i < n; i++) {         if (a[i][j] % m != j || a[i][j] / m > n)           c++;         else {           if (i >= a[i][j] / m)             cnt[i - a[i][j] / m]++;           else             cnt[i + n - a[i][j] / m]++;         }       }       int mn = 1e9;       for (int i = 0; i < n; i++) {         mn = min(mn, n - cnt[i] + i);       }       ans += mn;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int sol = (int)1e9 + 7;   int op, lim;   int ask(int r, int c) {     op++;     if (op == lim) {       cout <<  !   << sol << endl;       exit(0);     }     cout <<  ?   << r <<     << c << endl;     int x;     cin >> x;     sol = min(sol, x);     return x;   }   int go = 1;   void solve(int r1, int c1, int r2, int c2) {     if (r1 > r2 || c1 > c2) {       return;     }     if (r1 == r2 && c1 == c2) {       ask(r1, c1);       return;     }     int rm = (r1 + r2) / 2;     vector<int> e;     for (int j = max(go, c1); j <= c2; j++) {       e.push_back(ask(rm, j));     }     int mn = e[0];     for (auto &x : e) mn = min(mn, x);     for (int j = c1; j <= c2; j++)       if (e[j - c1] == mn) {         solve(r1, c1, rm - 1, j);         go = max(go, j);         solve(rm + 1, j, r2, c2);         return;       }   }   int main() {     int n, m;     cin >> n >> m;     lim = 4 * (n + m);     solve(1, 1, n, m);     cout <<  !   << sol << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char *argv[]) {     map<int, int> set;     int n, a, b, check1, check2, temp;     cin >> n >> a >> b;     int arr[n];     vector<int> check;     for (int i = int(0); i <= int(n - 1); i++) {       cin >> arr[i];       set[arr[i]] = -1;     }     if (a == b) {       for (int i = int(0); i <= int(n - 1); i++) {         if (set[a - arr[i]] == 0) {           cout <<  NO  << endl;           return 0;         }       }       cout <<  YES  << endl;       for (int i = int(0); i <= int(n - 1); i++) {         cout <<  0  ;       }       return 0;     }     for (int i = int(0); i <= int(n - 1); i++) {       if (set[arr[i]] != -1) continue;       check.clear();       check.push_back(arr[i]);       check1 = a - arr[i];       check2 = b - arr[i];       bool f = false;       while (!f) {         if (set[check1] == -1 && set[check2] == -1) {           check.push_back(check1);           check.push_back(check2);           temp = check1;           check1 = a - check2;           check2 = b - temp;         } else if (set[check1] == -1) {           check.push_back(check1);           f = 1;           for (int i = int(0); i <= int(check.size() - 1); i++) {             set[check.at(i)] = 1;           }         } else if (set[check2] == -1) {           check.push_back(check2);           f = 1;           for (int i = int(0); i <= int(check.size() - 1); i++) {             set[check.at(i)] = 2;           }         } else {           cout <<  NO  << endl;           return 0;         }       }     }     cout <<  YES  << endl;     for (int i = int(0); i <= int(n - 1); i++) {       cout << set[arr[i]] - 1 <<    ;     }     return EXIT_SUCCESS;   }


#include <bits/stdc++.h>   using namespace std;   int N, M;   int _next[10000000];   struct event {     int l, r, x;     event() {}     event(int l, int r, int x) : l(l), r(r), x(x) {}   };   vector<event> e[7005];   inline bool cross(int l1, int r1, int l2, int r2) {     if (l1 > r2) return false;     if (r1 < l2) return false;     return true;   }   int main() {     for (int i = 1; i < (1 << 21) - 1; i++) {       _next[i] = 1;     }     for (int i = 0; i < 21; i++) {       _next[1 << i]++;     }     for (int i = 2; i < (1 << 21) - 1; i++) {       _next[i] += _next[i - 1];     }     cin >> N;     cin >> M;     int t, v;     int n, l, r, x;     for (int i = 0; i < M; i++) {       cin >> t;       if (t == 1) {         cin >> n >> l >> r >> x;         e[n].push_back(event(l, r, x));       } else {         set<int> S;         cin >> n >> v;         l = v - 1, r = v;         for (int i = n; i <= N; i++) {           for (int j = 0; j < e[i].size(); j++) {             if (cross(l + 1, r, e[i][j].l, e[i][j].r)) S.insert(e[i][j].x);           }           l = _next[l], r = _next[r];         }         cout << S.size() << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k;   long long count(long long num) {     long long ans = 0, sum = num, cnt = 0, hest = 0;     while (sum) {       hest = sum % 10;       sum /= 10;       cnt++;     }     cnt--;     long long k = 1;     for (long long i = 1; i <= cnt; i++) {       ans += k;       k *= 10;     }     if (hest > 1) {       ans += k;     } else {       ans += num - k + 1;     }     return ans;   }   double pro(long long l, long long r) {     double ans = 0;     ans = ((double)(count(r) - count(l - 1))) / ((double)(r - l + 1));     return ans;   }   long long l, r;   double p[1005];   double dp[1005][1005];   int main() {     scanf( %lld , &n);     for (long long i = 1; i <= n; i++) {       scanf( %lld%lld , &l, &r);       p[i] = pro(l, r);     }     scanf( %lld , &k);     dp[0][0] = 1;     for (long long j = 1; j <= n; j++) {       dp[0][j] = dp[0][j - 1] * (1.0 - p[j]);     }     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j <= n; j++) {         dp[i][j] = dp[i - 1][j - 1] * p[j] + dp[i][j - 1] * (1 - p[j]);       }     }     double ans = 0;     for (long long i = (long long)(n * k / 100.0 + 0.999); i <= n; i++) {       ans += dp[i][n];     }     printf( %.10lf , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     while (t--) {       int n, m, k;       cin >> n >> m >> k;       vector<long long> a(n + 1), pref_a(n + 1);       for (int i = 1; i <= n; i++) {         cin >> a[i];         pref_a[i] = pref_a[i - 1] + a[i];       }       vector<long long> b(m + 1), pref_b(m + 1);       for (int i = 1; i <= m; i++) {         cin >> b[i];         pref_b[i] = pref_b[i - 1] + b[i];       }       vector<int> divs;       for (int i = 1; i * 1ll * i <= k; i++) {         if (k % i == 0) {           divs.push_back(i);           if (k / i != i) {             divs.push_back(k / i);           }         }       }       long long ans = 0;       for (int x : divs) {         int y = k / x;         int lin = 0, col = 0;         for (int i = x; i <= n; i++) {           if (pref_a[i] - pref_a[i - x] == x) {             lin++;           }         }         for (int i = y; i <= m; i++) {           if (pref_b[i] - pref_b[i - y] == y) {             col++;           }         }         ans += lin * col;       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   clock_t t = clock();   namespace my_std {   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   template <typename T>   inline T rnd(T l, T r) {     return uniform_int_distribution<T>(l, r)(rng);   }   template <typename T>   inline bool chkmax(T &x, T y) {     return x < y ? x = y, 1 : 0;   }   template <typename T>   inline bool chkmin(T &x, T y) {     return x > y ? x = y, 1 : 0;   }   template <typename T>   inline void read(T &t) {     t = 0;     char f = 0, ch = getchar();     double d = 0.1;     while (ch >  9  || ch <  0 ) f |= (ch ==  - ), ch = getchar();     while (ch <=  9  && ch >=  0 ) t = t * 10 + ch - 48, ch = getchar();     if (ch ==  . ) {       ch = getchar();       while (ch <=  9  && ch >=  0 ) t += d * (ch ^ 48), d *= 0.1, ch = getchar();     }     t = (f ? -t : t);   }   template <typename T, typename... Args>   inline void read(T &t, Args &...args) {     read(t);     read(args...);   }   char __sr[1 << 21], __z[20];   int __C = -1, __zz = 0;   inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; }   inline void print(register int x) {     if (__C > 1 << 20) Ot();     if (x < 0) __sr[++__C] =  - , x = -x;     while (__z[++__zz] = x % 10 + 48, x /= 10)       ;     while (__sr[++__C] = __z[__zz], --__zz)       ;     __sr[++__C] =   n ;   }   void file() {}   inline void chktime() {}   long long ksm(long long x, int y) {     long long ret = 1;     for (; y; y >>= 1, x = x * x)       if (y & 1) ret = ret * x;     return ret;   }   }  // namespace my_std   using namespace my_std;   int n, K;   vector<int> bl[30100];   int ok[30100];   struct hh {     int t, nxt;   } edge[30100];   int head[30100], ecnt;   void make_edge(int f, int t) {     edge[++ecnt] = (hh){t, head[f]};     head[f] = ecnt;   }   int vis[30100];   int pth[30100], c;   void dfs(int x) {     for (int &i = head[x], t; (t = i); i = edge[i].nxt)       if (!vis[i]) vis[i] = 1, dfs(edge[i].t), pth[++c] = edge[t].t;   }   int ask(int x) {     printf( ? %d n , x);     fflush(stdout);     char c;     cin >> c;     return c ==  Y ;   }   void add(int id, int flg) {     for (int i = (0); i <= (K - 1); i++) ok[bl[id][i]] &= !(flg & ask(bl[id][i]));   }   void add2(int id, int flg) {     int p = -1;     for (int i = (K - 1); i >= (0); i--)       if (ok[bl[id][i]]) p = i;     if (p == -1) {       for (int i = (0); i <= (K - 1); i++) ask(bl[id][i]);       return;     }     if (p == 0) {       for (int i = (0); i <= (K - 1); i++)         if (ok[bl[id][i]])           ok[bl[id][i]] &= !(flg & ask(bl[id][i]));         else           ask(bl[id][p]);     } else {       ok[bl[id][p]] &= !(flg & ask(bl[id][p]));       for (int i = (1); i <= (p); i++) ask(bl[id][p]);       for (int i = (p + 1); i <= (K - 1); i++)         if (ok[bl[id][i]])           ok[bl[id][i]] &= !(flg & ask(bl[id][i]));         else           ask(bl[id][p]);     }   }   void work(int t) {     int n = ::n / K;     for (int i = (1); i <= (n); i++)       for (int j = (i + 1); j <= (n); j++) make_edge(i, j);     for (int i = (1); i <= (n / 2); i++)       for (int j = (1); j <= (n - 2 * i + 1); j++) make_edge(n - i + 1, i);     dfs(1);     pth[++c] = 1;     reverse(pth + 1, pth + c + 1);     --c;     add(1, !t);     for (int i = (2); i <= (c); i++)       t ? add2(pth[i], pth[i] > pth[i - 1]) : add(pth[i], pth[i] > pth[i - 1]);     for (int i = (1); i <= (ecnt); i++) vis[i] = 0;     c = ecnt = 0;     for (int i = (1); i <= (n); i++) head[i] = 0;   }   int main() {     read(n, K);     for (int i = (1); i <= (n); i++) ok[i] = 1;     for (int i = (1); i <= (n / K); i++)       for (int j = ((i - 1) * K + 1); j <= (i * K); j++) bl[i].push_back(j);     work(0);     for (int i = (1); i <= (n / K); i++) reverse(bl[i].begin(), bl[i].end());     if (K != n) work(1);     int ans = 0;     for (int i = (1); i <= (n); i++) ans += ok[i];     printf( ! %d n , ans);     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       vector<string> G(n);       for (int i = 0; i < n; i++) {         cin >> G[i];       }       vector<vector<pair<int, int>>> arr(27, vector<pair<int, int>>());       bool ret_flag = false;       bool empty_flag = true;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           if (G[i][j] ==  . ) continue;           empty_flag = false;           auto temp = G[i][j] -  a ;           if (arr[temp].size() == 1 &&               !(arr[temp][0].first == i || arr[temp][0].second == j)) {             ret_flag = true;             break;           } else if (arr[temp].size() > 1 &&                      !((arr[temp][0].first == i &&                         arr[temp][arr[temp].size() - 1].first == i) ||                        (arr[temp][0].second == j &&                         arr[temp][arr[temp].size() - 1].second == j))) {             ret_flag = true;             break;           }           arr[temp].push_back(make_pair(i, j));         }         if (ret_flag) break;       }       if (empty_flag) {         cout <<  YES  << endl;         cout << 0 << endl;         continue;       }       if (ret_flag) {         cout <<  NO  << endl;         continue;       }       int max_snake = 0;       for (int i = 25; i >= 0; i--) {         if (arr[i].size() == 0) continue;         max_snake = max(max_snake, i);         if (arr[i][0].first == arr[i][arr[i].size() - 1].first) {           int r = arr[i][0].first;           for (int j = arr[i][0].second; j <= arr[i][arr[i].size() - 1].second;                j++) {             if (G[r][j] -  a  >= i) continue;             ret_flag = true;             break;           }           if (ret_flag) break;         } else if (arr[i][0].second == arr[i][arr[i].size() - 1].second) {           int c = arr[i][0].second;           for (int j = arr[i][0].first; j <= arr[i][arr[i].size() - 1].first;                j++) {             if (G[j][c] >=  a  + i) continue;             ret_flag = true;             break;           }           if (ret_flag) break;         }         if (ret_flag) break;       }       if (ret_flag) {         cout <<  NO  << endl;         continue;       }       cout <<  YES  << endl;       cout << max_snake + 1 << endl;       for (int i = 0; i <= 25; i++) {         int j = i;         if (arr[i].empty()) {           while (i < 26 && arr[i].empty()) i++;         }         if (i == 26) break;         cout << arr[i][0].first + 1 <<     << arr[i][0].second + 1 <<                  << arr[i].back().first + 1 <<     << arr[i].back().second + 1              << endl;         i = j;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string a, b;   int t;   string ssort(string x) {     if (x.size() & 1) return x;     int len = x.size() / 2;     string xx, yy;     xx = ssort(x.substr(0, len));     yy = ssort(x.substr(len, len));     if (xx < yy)       return xx + yy;     else       return yy + xx;   }   signed main() {     cin >> a >> b;     if (a == b) {       printf( YES n );       return 0;     }     if (ssort(a) == ssort(b))       printf( YES n );     else       printf( NO n );   }


#include <bits/stdc++.h>   using namespace std;   int T, n;   char s[50];   int a[50], b[50];   int main() {     scanf( %d , &T);     while (T--) {       int cnt = 0;       scanf( %d , &n);       scanf( %s , s + 1);       for (int i = 1; i <= n; ++i) a[i] = s[i] -  a , b[i] = a[i];       sort(a + 1, a + n + 1);       for (int i = 1; i <= n; ++i)         if (a[i] != b[i]) cnt++;       cout << cnt << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N_MAX = 1e5, V = (1 << 20);   int n, x[N_MAX], t[N_MAX];   struct edge {     int l, v;   };   vector<edge> G[N_MAX];   long long cnt_tree[V + 1], total_tree[V + 1];   void add(int i, long long a, long long tree[]) {     while (i <= V) {       tree[i] += a;       i += (i & -i);     }   }   long long sum(int i, long long tree[]) {     long long s = 0;     while (i) {       s += tree[i];       i -= (i & -i);     }     return s;   }   int ind(long long a, long long tree[]) {     int idx = 0, mask = V;     a--;     while (mask && idx < V) {       int t = idx + mask;       if (a >= tree[t]) {         idx = t;         a -= tree[t];       }       mask >>= 1;     }     return idx + 1;   }   long long T, sol[N_MAX];   void DFS(int v) {     add(t[v], x[v], cnt_tree);     add(t[v], (long long)x[v] * t[v], total_tree);     int i = ind(T, total_tree);     if (i > V)       sol[v] = sum(V, cnt_tree);     else       sol[v] = sum(i - 1, cnt_tree) + (T - sum(i - 1, total_tree)) / i;     long long m1 = -1, m2 = -1;     for (edge e : G[v]) {       if (T <= e.l) continue;       T -= e.l;       DFS(e.v);       T += e.l;       if (m1 < sol[e.v]) {         m2 = m1;         m1 = sol[e.v];       } else if (m2 < sol[e.v])         m2 = sol[e.v];     }     if (v == 0)       sol[v] = max(sol[v], m1);     else       sol[v] = max(sol[v], m2);     add(t[v], -x[v], cnt_tree);     add(t[v], -(long long)x[v] * t[v], total_tree);   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> T;     for (int i = 0; i < n; i++) cin >> x[i];     for (int i = 0; i < n; i++) cin >> t[i];     for (int i = 1; i <= n - 1; i++) {       int p;       edge e;       cin >> p >> e.l;       e.v = i;       e.l *= 2;       G[p - 1].push_back(e);     }     DFS(0);     cout << sol[0] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1100;   const int M = 256;   const int K = 26;   const int INF = 2000000000;   const double eps = 1e-12;   int k, m, n, d[K], a[N];   char s[N];   bool ok;   int F();   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     k = 1;     for (int i = 0; i < 26; i++) d[i] = k, k <<= 1;     for (int i = 0; i < n; i++) {       cin >> s;       m = 0;       for (int j = 0; s[j]; j++) m = m | d[s[j] -  a ];       a[i] = m;     }     sort(a, a + n);     m = 1;     for (int i = 1; i < n; i++)       if (a[i] != a[i - 1]) m++;     cout << m;     return 0;   }   int F() { return 0; }


#include <bits/stdc++.h>   using namespace std;   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   int main() {     long long test, n, a[55], b[55];     scanf( %lld , &test);     while (test--) {       scanf( %lld , &n);       long long mina = 1000000000;       long long minb = 1000000000;       for (long long i = 0; i < n; i++) {         scanf( %lld , &a[i]);         mina = min(a[i], mina);       }       for (long long i = 0; i < n; i++) {         scanf( %lld , &b[i]);         minb = min(b[i], minb);       }       long long ans = 0;       for (long long i = 0; i < n; i++) {         long long p = a[i] - mina;         long long q = b[i] - minb;         ans += min(p, q);         ans += (p - min(p, q)) + (q - min(p, q));       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100001], p[100001];   int main() {     int min = 1000, sum = 0, n, i, j;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d %d , &a[i], &p[i]);       if (p[i] < min) {         min = p[i];       }       sum += a[i] * min;     }     printf( %d n , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5;   const long long MOD = 1e9 + 7;   long long n, m, connected = 0;   long long col[N], vis[N];   vector<pair<long long, long long> > g[N];   long long pow(long long a, long long b, long long m) {     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % m;       b /= 2;       a = (a * a) % m;     }     return ans;   }   void dfs(long long k, long long c) {     if (vis[k]) {       if (col[k] != c) {         cout <<  0 ;         exit(0);       }       return;     }     vis[k] = 1;     col[k] = c;     for (auto &it : g[k]) {       dfs(it.first, c ^ (1 - it.second));     }   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> m;     for (long long i = 1; i <= m; i++) {       long long u, v, t;       cin >> u >> v >> t;       g[u].push_back({v, t});       g[v].push_back({u, t});     }     for (long long i = 1; i <= n; i++) {       if (!vis[i]) {         dfs(i, 0);         connected++;       }     }     long long ans = pow(2LL, connected - 1, MOD);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 510000;   int n;   int sz[MAXN];   int was[MAXN];   int up[MAXN];   vector<int> eds[MAXN];   int ans[MAXN];   void dfs1(int v) {     sz[v] = 1;     was[v] = 1;     for (int u : eds[v]) {       if (!was[u]) {         dfs1(u);         sz[v] += sz[u];       }     }   }   void dfs2(int v) {     was[v] = 1;     up[v] = 0;     if (sz[v] * 2 <= n) up[v] = sz[v];     for (int u : eds[v]) {       if (!was[u]) {         dfs2(u);         up[v] = max(up[v], up[u]);       }     }   }   void dfs3(int v, int now) {     ans[v] = 1;     was[v] = 1;     if ((n - sz[v]) * 2 <= n) now = max(now, n - sz[v]);     if ((n - sz[v] - now) * 2 > n) ans[v] = 0;     int mx1 = 0;     int mx2 = 0;     for (int u : eds[v]) {       if (!was[u]) {         mx2 = max(mx2, up[u]);         if (mx2 > mx1) swap(mx1, mx2);       }     }     for (int u : eds[v]) {       if (!was[u]) {         if ((sz[u] - up[u]) * 2 > n) ans[v] = 0;         int go = now;         if (up[u] == mx1)           go = max(now, mx2);         else           go = max(now, mx1);         dfs3(u, go);       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; ++i) {       int a, b;       scanf( %d%d , &a, &b);       --a;       --b;       eds[a].push_back(b);       eds[b].push_back(a);     }     dfs1(0);     memset(was, 0, sizeof(was));     dfs2(0);     memset(was, 0, sizeof(was));     dfs3(0, 0);     for (int i = 0; i < n; ++i) printf( %d  , ans[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   void print_row(int s, int *l, int size) {     int i = s + 1;     printf( %d , l[s]);     while (i < size) printf(  %d , l[i++]);     i = 0;     while (i < s) printf(  %d , l[i++]);     printf(  n );   }   int main() {     int n, k;     scanf( %d %d , &n, &k);     int l[n];     l[0] = k;     for (int i = 1; i < n; i++) l[i] = 0;     for (int i = 0; i < n; i++) print_row(i, l, n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   int ara[55][55];   void solve() {     int n;     cin >> n;     vector<pair<int, int> > odds;     map<pair<int, int>, int> done;     for (int i = 1; i <= n; i++) {       odds.push_back({i, n / 2 + 1});       done[{i, n / 2 + 1}] = 1;     }     for (int j = 1; j <= n; j++) {       if (!done[{n / 2 + 1, j}]) odds.push_back({n / 2 + 1, j});       done[{n / 2 + 1, j}] = 1;     }     for (int k = 1; k <= n / 2; k++) {       int i = k;       int j = 1;       while (i < n && j < n) {         if (!done[{i, j}]) {           odds.push_back({i, j});           done[{i, j}] = 1;         }         if (!done[{i, n - j + 1}]) {           odds.push_back({i, n - j + 1});           done[{i, n - j + 1}] = 1;         }         if (!done[{n - i + 1, j}]) {           odds.push_back({n - i + 1, j});           done[{n - i + 1, j}] = 1;         }         if (!done[{n - i + 1, n - j + 1}]) {           odds.push_back({n - i + 1, n - j + 1});           done[{n - i + 1, n - j + 1}] = 1;         }         i++;         j++;       }     }     for (int i = 0, k = 1; k <= n * n; k += 2, i++) {       int x = odds[i].first;       int y = odds[i].second;       ara[x][y] = k;     }     for (int i = 1, k = 2; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (!ara[i][j]) {           ara[i][j] = k;           k += 2;         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) cout << ara[i][j] <<    ;       cout <<   n ;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     for (int i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long k, l, ans = 0;     cin >> k >> l;     while (l % k == 0) {       l = l / k;       if (l != 1) ans++;     }     if (l == 1)       cout <<  YES  << endl << ans;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   int n, w, used[300005];   long long ans, a[300005], b[300005];   priority_queue<pair<long long, int>, vector<pair<long long, int> >,                  greater<pair<long long, int> > >       Q1, Q2;   priority_queue<pair<long long, int>, vector<pair<long long, int> >,                  less<pair<long long, int> > >       Used;   int main() {     scanf( %d %d , &n, &w);     for (int i = 1; i <= n; i++) {       scanf( %lld %lld , &a[i], &b[i]);       Q1.push(make_pair(a[i], i));       Q2.push(make_pair(b[i], i));     }     if (w == 1) {       pair<long long, int> x = Q1.top();       ans = x.first;       used[x.second]++;       printf( %lld n , ans);       for (int i = 1; i <= n; i++) printf( %d , used[i]);       return 0;     }     while (w > 0 && !Q1.empty() && !Q2.empty()) {       pair<long long, int> x = Q1.top();       if (used[abs(x.second)] == 2) {         Q1.pop();         continue;       }       pair<long long, int> y = Q2.top();       if (used[y.second]) {         Q2.pop();         continue;       }       if (x.first * 2 <= y.first) {         int now = x.second;         w--;         ans += x.first;         Q1.pop();         if (now > 0) Q1.push(make_pair(b[now] - a[now], -now));         used[abs(now)]++;         Used.push(make_pair(x.first, abs(now)));       } else {         int now = y.second;         w--, w--;         ans += y.first;         Q2.pop();         used[now] = 2;         Used.push(make_pair(b[now] - a[now], now));       }     }     while (w > 0 && !Q1.empty()) {       pair<long long, int> x = Q1.top();       if (used[abs(x.second)] == 2) {         Q1.pop();         continue;       }       int now = x.second;       w--;       ans += x.first;       Q1.pop();       if (now > 0) Q1.push(make_pair(b[now] - a[now], -now));       used[abs(now)]++;       Used.push(make_pair(x.first, abs(now)));     }     while (w > 0 && !Q2.empty()) {       pair<long long, int> y = Q2.top();       if (used[y.second]) {         Q2.pop();         continue;       }       int now = y.second;       w--, w--;       ans += y.first;       Q2.pop();       used[now] = 2;       Used.push(make_pair(b[now] - a[now], now));     }     if (w == -1) {       pair<long long, int> x = Used.top();       ans -= x.first;       used[x.second]--;     }     int f = 1;     while (f) {       f = 0;       while (!Q1.empty()) Q1.pop();       while (!Used.empty()) Used.pop();       for (int i = 1; i <= n; i++) {         if (used[i] == 2)           Used.push(make_pair(b[i], i));         else if (used[i] == 1)           Q1.push(make_pair(b[i] - a[i], i));         else           Q1.push(make_pair(a[i], i));       }       if (Q1.size() < 2 || Used.empty()) break;       pair<long long, int> x = Q1.top();       Q1.pop();       pair<long long, int> y = Q1.top();       Q1.pop();       pair<long long, int> z = Used.top();       Used.pop();       if (x.first + y.first < z.first) {         ans = ans - (z.first - x.first - y.first);         used[x.second]++;         used[y.second]++;         used[z.second] = 0;         f = 1;       }     }     printf( %lld n , ans);     for (int i = 1; i <= n; i++) printf( %d , used[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   const int MOD = 998244353;   struct node {     long long a, b;     node() {}     node(long long a, long long b) {       this->a = a;       this->b = b;     }     node operator+(const node& rhs) const {       return node((a + rhs.a) % MOD, (b + rhs.b) % MOD);     }     node operator*(const node& rhs) const {       return node(a * rhs.a % MOD, b * rhs.b % MOD);     }     bool operator==(const node& rhs) const { return a == rhs.a && b == rhs.b; }   };   node BASE(2333, 233333);   node h[maxn];   node ALL(0, 0);   node s[maxn][15];   vector<pair<int, int> > vec[maxn];   int n, m, k, ans;   void dfs(int u, node hh) {     if (u > k) {       ans += (hh == ALL ? 1 : 0);       return;     }     for (int i = 0; i < u; i++) {       node v = hh + s[u][i];       dfs(u + 1, v);     }     return;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     h[0] = node(1, 1);     for (int i = 1; i <= n; i++) {       h[i] = h[i - 1] * BASE;       ALL = ALL + h[i];     }     for (int i = 1; i <= m; i++) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       vec[u].emplace_back(make_pair(v, w));     }     for (int i = 1; i <= n; i++) {       sort(vec[i].begin(), vec[i].end(), [](pair<int, int> a, pair<int, int> b) {         return a.second < b.second;       });       int lens = vec[i].size();       for (int j = 0; j < lens; j++) s[lens][j] = s[lens][j] + h[vec[i][j].first];     }     dfs(1, node(0, 0));     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m = 1;   vector<vector<int> > edge(105);   int from[105];   int to[105];   bool bfs(int a, int b) {     int use[105] = {0};     queue<int> q;     q.push(a);     use[a] = 1;     while (!q.empty()) {       int cur = q.front();       q.pop();       for (int i = 0; i < edge[cur].size(); i++) {         if (edge[cur][i] == b) return true;         if (use[edge[cur][i]] == 0) {           q.push(edge[cur][i]);           use[edge[cur][i]] = 1;         }       }     }     return false;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       int t, x, y;       cin >> t >> x >> y;       if (t == 1) {         from[m] = x;         to[m] = y;         for (int j = 1; j < m; j++) {           if ((from[j] < x && x < to[j] && y >= to[j]) ||               (from[j] < y && y < to[j]) && x <= from[j]) {             edge[j].push_back(m);             edge[m].push_back(j);           } else if (x <= from[j] && y >= to[j]) {             edge[j].push_back(m);           }         }         m++;       } else {         if (bfs(x, y))           cout <<  YES  << endl;         else           cout <<  NO  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   #pragma GCC optimize( Ofast )   #pragma GCC optimize( O3 )   using namespace std;   const int N = 1e6 + 5;   int n, p = 1, pa[N];   int ch[N][26];   vector<int> cnt[N][2];   vector<pair<int, int> > as;   long long ans;   inline void dfs(int u, int dep) {     for (register int i = 0; i <= 25; ++i) {       int v = ch[u][i];       if (v) {         dfs(v, dep + 1);         for (register int j = 0; j < cnt[v][0].size(); ++j)           cnt[u][0].push_back(cnt[v][0][j]);         for (register int j = 0; j < cnt[v][1].size(); ++j)           cnt[u][1].push_back(cnt[v][1][j]);       }     }     ans += min(cnt[u][0].size(), cnt[u][1].size()) * dep;     for (register int i = min(cnt[u][0].size(), cnt[u][1].size()) - 1; i > -1;          --i) {       as.push_back(           {cnt[u][0][cnt[u][0].size() - 1], cnt[u][1][cnt[u][1].size() - 1]});       cnt[u][0].pop_back();       cnt[u][1].pop_back();     }   }   string a;   int main() {     cin >> n;     for (register int id = 1; id <= n; ++id) {       cin >> a;       int u = 1;       for (register int i = 0; i < a.size(); ++i) {         int c = a[i] -  a ;         if (ch[u][c] == 0) ch[u][c] = ++p;         u = ch[u][c];       }       cnt[u][0].push_back(id);     }     for (register int id = 1; id <= n; ++id) {       cin >> a;       int u = 1;       for (int i = 0; i < a.size(); ++i) {         int c = a[i] -  a ;         if (!ch[u][c]) ch[u][c] = ++p;         u = ch[u][c];       }       cnt[u][1].push_back(id);     }     dfs(1, 0);     printf( %d n , ans);     for (register int i = 0; i <= n - 1; ++i)       printf( %d %d n , as[i].first, as[i].second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   char a[105][3];   int main() {     scanf( %s%d , a[0], &n);     for (int i = 1; i <= n; ++i) {       scanf( %s , a[i]);       if (a[i][0] == a[0][0] && a[i][1] == a[0][1]) {         printf( YES n );         return 0;       }     }     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j)         if ((a[i][1] == a[0][0] && a[j][0] == a[0][1])) {           printf( YES n );           return 0;         }     printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int T, n, a[N];   map<int, int> mp;   int main() {     cin >> T;     while (T--) {       double sum = 0;       mp.clear();       cin >> n;       for (int i = 0; i < n; ++i) {         cin >> a[i];         sum += a[i];       }       double mean = sum / n;       double need = sum - (n - 2) * mean;       if (need != floor(need)) {         cout << 0 << endl;         continue;       }       long long ans = 0;       for (int i = 0; i < n; ++i) {         if (mp.find(need - a[i]) != mp.end()) ans += mp[need - a[i]];         mp[a[i]]++;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isPrime(int num);   int main() {     int n;     cin >> n;     for (int i = 1; i <= 1000; i++) {       if (!(isPrime(n * i + 1))) {         cout << i;         break;       }     }     return 0;   }   bool isPrime(int num) {     if (num <= 1)       return false;     else if (num == 2)       return true;     else if (num % 2 == 0)       return false;     else {       bool prime = true;       int divisor = 3;       double num_d = static_cast<double>(num);       int upperLimit = static_cast<int>(sqrt(num_d) + 1);       while (divisor <= upperLimit) {         if (num % divisor == 0) prime = false;         divisor += 2;       }       return prime;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   const double esp = 1e-7;   const int mod = 1e9 + 7;   vector<int> adj[N];   int leaf = 0;   void dfs(int node = 1, int par = 0) {     int cnt = 0;     for (int child : adj[node]) {       if (child == par) continue;       dfs(child, node);       cnt++;     }     if (!cnt) {       leaf++;     }   }   int main() {     int n, s, u, v;     double ans;     scanf( %d %d , &n, &s);     for (int i = 1; i < n; i++) {       scanf( %d %d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs();     if (adj[1].size() == 1) leaf++;     if (n == 2) {       leaf = 1;       ans = s;     } else {       ans = 2.0 * s / double(leaf);     }     printf( %.09f n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cout << *it <<   =   << a << endl;     err(++it, args...);   }   const long long MOD = 1e9 + 7;   long long modExp(long long b, long long p) {     long long res = 1;     while (p > 0) {       if (p & 1) {         res = (res * b) % MOD;       }       b = (b * b) % MOD;       p >>= 1;     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     long long ans = 1;     for (int i = 1; i <= n; i++) {       ans = ((2 * m - 1) * ans + modExp(m, i - 1)) % MOD;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     for (int k = 1; k <= n / 3; k++) {       if (n % k == 0) {         for (int p = 0; p < k; p++) {           int sum = 0;           for (int v = p; v < n; v += k) {             if (a[v] == 1) sum++;           }           if (sum == n / k) {             cout <<  YES n ;             return 0;           }         }       }     }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   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];   }   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();   }   inline bool read(signed &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(long long &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) {     first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                          (iS == iT ? EOF : *iS++))                       : *iS++);     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 == EOF))       *(++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...);   }   inline bool write(signed 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(long long 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 inf;   struct _init_ {     _init_() { memset(&inf, 0x3f, sizeof(inf)); }   };   _init_ ___INIT__;   long long a[20005], b[20005];   long long f[40005], beg[20005];   vector<long long> poly[40005];   long long t;   const long long p = 998244353, g = 3;   long long rev[1 << 18];   long long fpm(long long a, long long b) {     long long c = 1;     for (; b; b >>= 1, a = a * a % p)       if (b & 1) c = c * a % p;     return c;   }   void NTT(vector<long long> &a, long long n, long long flg) {     for (long long i = 0; i < n; ++i) {       rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (n >> 1));       if (i < rev[i]) swap(a[i], a[rev[i]]);     }     for (long long i = 1; i < n; i <<= 1) {       long long ww = fpm(g, (p - 1) / i / 2 * (flg + p - 1)), w;       for (long long j = 0; j < n; j += i + i) {         w = 1;         for (long long k = 0; k < i; ++k) {           long long u = a[j + k], v = a[j + k + i] * w % p;           a[j + k] = (u + v) % p;           a[j + k + i] = (u - v + p) % p;           w = w * ww % p;         }       }     }     if (flg == -1) {       long long in = fpm(n, p - 2);       for (long long i = 0; i < n; ++i) a[i] = a[i] * in % p;     }   }   void Prod(vector<long long> &a, vector<long long> &b, vector<long long> &c) {     long long first = a.size() + b.size(), n = 1;     while (n < first) n <<= 1;     a.resize(n);     b.resize(n);     c.resize(n);     NTT(a, n, 1);     NTT(b, n, 1);     for (long long i = 0; i < n; ++i) c[i] = a[i] * b[i] % p;     NTT(c, n, -1);     c.resize(first - 1);     a.clear();     b.clear();   }   long long query(long long k) {     long long l = 1;     while (l < t) {       Prod(poly[l], poly[l + 1], poly[++t]);       l += 2;     }     return poly[t][k];   }   signed main() {     long long n, k;     read(n, k);     for (long long i = 1; i <= n; ++i) read(a[i]);     long long q;     long long tp, first, l, r, d;     read(q);     for (; q; --q) {       read(tp, first, l);       if (tp == 1) {         read(d);         for (long long i = 1; i <= n; ++i) {           poly[i].clear();           poly[i].push_back(1);           poly[i].push_back((first - (i == l ? d : a[i]) + 2 * p) % p);         }       } else {         read(r, d);         for (long long i = 1; i <= n; ++i) {           poly[i].clear();           poly[i].push_back(1);           poly[i].push_back((first - a[i] + 2 * p) % p);         }         for (long long i = l; i <= r; ++i)           poly[i][1] = (poly[i][1] + 2 * p - d) % p;       }       t = n;       write(query(k),   n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Popcount(long long int a) {     int k;     for (k = 0; a; a >>= 1)       if (a & 1ll) k++;     return k;   }   int Ffs(long long int a) {     int k;     for (k = 0; a; a >>= 1, k++)       ;     return k;   }   long long int C[50][50];   long long int getAns(long long int n, long long int t) {     if (n < t || t <= 0) return 0ll;     long long int i;     int k = 0, j = Ffs(t) - 1;     for (i = 1ll; (i << 1) < n; (i <<= 1), k++)       ;     long long int res = getAns(n - i, t >> 1);     if (k < j) return res;     return C[k][j] + res;   }   int main() {     C[0][0] = 1ll;     for (int i = 1; i < 50; i++) {       C[i][0] = C[i][i] = 1ll;       for (int j = 1; j < i; j++) C[i][j] = C[i - 1][j - 1] + C[i - 1][j];     }     long long int N, T;     while (cin >> N >> T) {       N++;       if (Popcount(T) != 1) {         puts( 0 );         continue;       }       long long int Ans = 0ll, x, di = 0ll;       for (x = 1ll; x + di <= N; x <<= 1) {         Ans += getAns(x, T);         di += x;       }       Ans += getAns(N - di, T);       if (T == 1ll) Ans--;       cout << Ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct BIT {     vector<int> t;     int n;     void add(int x, int v) {       for (; x < n; x += x & -x) {         t[x] += v;       }     }     BIT(int sz) {       t = vector<int>(sz);       n = sz;     }     void add(int l, int r, int v) {       ++l;       ++r;       add(l, v);       add(r, -v);     }     int query(int x) {       ++x;       int ans = 0;       for (; x; x -= x & -x) {         ans += t[x];       }       return ans;     }   };   const int N = 100100;   vector<BIT> bits;   vector<int> adj[N];   int my_bit[N], my_ind[N], n, q;   int dfs(int u, int p, int bit_ind, int ind) {     my_bit[u] = bit_ind;     my_ind[u] = ind;     int res = ind;     for (auto& v : adj[u]) {       if (v == p) continue;       res = dfs(v, u, bit_ind, ind + 1);     }     return res;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     cin >> n >> q;     int u, v;     for (int i = 1; i <= n - 1; ++i) {       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     bits.emplace_back(1);     int mx = 5;     for (int v : adj[1]) {       int amt = dfs(v, 1, bits.size(), 2);       bits.emplace_back(amt + 5);       mx = max(mx, amt + 5);     }     BIT all(mx);     my_ind[1] = 1;     for (int qq = 0; qq < q; ++qq) {       int type;       cin >> type;       if (type == 0) {         int x, d;         cin >> u >> x >> d;         if (u == 1) {           all.add(1, 2 + d, x);         } else {           int extra = 0;           int lo = my_ind[u] - d;           int hi = my_ind[u] + d + 1;           if (lo < 2) {             extra = 2 - lo;             lo = extra + 1;           }           bits[my_bit[u]].add(lo, hi, x);           if (extra) {             all.add(1, extra + 1, x);           }         }       } else if (type == 1) {         cin >> u;         int res = all.query(my_ind[u]);         if (u != 1) {           assert(my_ind[u] > 1);           res += bits[my_bit[u]].query(my_ind[u]);         }         cout << res <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using lli = long long int;   using pii = pair<int, int>;   using vi = vector<int>;   using vb = vector<bool>;   using vvi = vector<vector<int>>;   using vlli = vector<long long int>;   using vpii = vector<pair<int, int>>;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     lli n, k, a, x[100005], c[100005], money = 0;     multiset<lli> unused;     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> x[i];     cin >> a;     for (int i = 1; i <= n; i++) cin >> c[i];     for (int i = 1; i <= n; i++) {       unused.insert(c[i]);       while (!unused.empty() && k < x[i]) {         auto temp = unused.begin();         k += a;         money += *temp;         unused.erase(temp);       }       if (k < x[i]) return cout << -1, 0;     }     cout << money;   }


#include <bits/stdc++.h>   using namespace std;   #pragma warning(disable : 4996)   const int N = 1e6 + 5, M = 300, MAX = 10, T = 3e4, INF = 1e9;   int d[N], w[T], dist[300][M + 5];   vector<int> h[T], pr;   vector<vector<int>> v;   map<vector<int>, int> mp;   int cnt = 0;   vector<int> path;   void f(int max, int prod, int ct) {     v.push_back(path);     pr.push_back(prod);     mp[path] = cnt++;     if (ct == MAX) return;     for (int i = 2; i <= max; i++) {       if (prod * i <= M) {         path.push_back(i);         f(i, prod * i, ct + 1);         path.pop_back();       }     }   }   vector<int> getV(int a) {     vector<int> g;     while (a > 1) {       int x = 0, y = d[a];       while (d[a] == y) {         a /= d[a];         x++;       }       g.push_back(x + 1);     }     sort(g.rbegin(), g.rend());     return g;   }   int main() {     f(M, 1, 0);     map<vector<int>, int> g;     for (int i = 2; i < N; i++) {       if (!d[i]) {         d[i] = i;         for (long long int j = i * (long long int)i; j < N; j += i) {           if (!d[j]) d[j] = i;         }       }     }     for (int i = 1; i < N; i++) {       vector<int> t = getV(i);       if (!g.count(t)) {         int x = g.size();         g[t] = x;       }     }     for (int i = 0; i < v.size(); i++) {       vector<int> &c = v[i];       for (int j = 0; j < c.size(); j++) {         if (j == 0 || c[j - 1] != c[j]) {           c[j]++;           if (mp.count(c)) {             h[i].push_back(mp[c]);             h[mp[c]].push_back(i);           }           c[j]--;         }       }       if (c.size() < MAX) {         c.push_back(2);         if (mp.count(c)) {           h[i].push_back(mp[c]);           h[mp[c]].push_back(i);         }         c.pop_back();       }     }     for (int i = 0; i < 300; i++) {       for (int j = 0; j <= M; j++) {         dist[i][j] = INF;       }     }     for (auto &p : g) {       fill(w, w + T, -1);       queue<int> q;       w[mp[p.first]] = 0;       q.push(mp[p.first]);       while (!q.empty()) {         int node = q.front();         q.pop();         for (int to : h[node]) {           if (w[to] == -1) {             w[to] = w[node] + 1;             q.push(to);           }         }       }       for (int j = 0; j < v.size(); j++) {         dist[p.second][pr[j]] = min(dist[p.second][pr[j]], w[j]);       }     }     int q;     scanf( %d , &q);     while (q--) {       int a, b;       scanf( %d%d , &a, &b);       a = g[getV(a)];       b = g[getV(b)];       int ans = INF;       for (int i = 0; i <= M; i++) ans = min(ans, dist[a][i] + dist[b][i]);       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 1;   inline int min(const int& x, const int& y) { return (x - y >> 31) ? x : y; }   namespace ST {   int lg[N], t[20][N];   inline void build(int* a, int n) {     copy(a + 1, a + 1 + n, t[0] + 1);     for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;     for (int i = 1; i <= 19; i++)       for (int j = 1; j <= n - (1 << i); j++)         t[i][j] = min(t[i - 1][j], t[i - 1][j + (1 << i - 1)]);   }   inline int ask(int l, int r) {     int p = lg[r - l];     return min(t[p][l], t[p][r - (1 << p)]);   }   }  // namespace ST   namespace SA {   int n, m, sa[N], x[N], y[N], c[N], h[N];   char* st;   inline void input(char* a, int len) {     n = len, m = 128, st = a;     for (int i = 1; i <= n; i++) x[i] = st[i], sa[i] = i;   }   inline void radix_sort() {     for (int i = 1; i <= m; i++) c[i] = 0;     for (int i = 1; i <= n; i++) c[x[i]]++;     for (int i = 1; i <= m; i++) c[i] += c[i - 1];     for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];   }   inline void init() {     for (int k = 0, p = 0; k <= n; k = k << 1 | !k) {       for (int i = n - k + 1; i <= n; i++) y[++p] = i;       for (int i = 1; i <= n; i++)         if (sa[i] > k) y[++p] = sa[i] - k;       radix_sort(), swap(x, y), p = 0;       for (int i = 1; i <= n; i++)         x[sa[i]] = p +=             y[sa[i]] != y[sa[i - 1]] || y[sa[i] + k] != y[sa[i - 1] + k];       if (p >= n) break;       m = p, p = 0;     }     for (int i = 1; i <= n; i++) {       int& p = h[x[i]] = max(0, h[x[i - 1]] - 1);       while (st[i + p] == st[sa[x[i] + 1] + p]) h[x[i]]++;     }     ST::build(h, n);   }   }  // namespace SA   struct tree {     set<int> mp;     long long int val;     inline void init() { mp.clear(), mp.insert(0), mp.insert(N), val = 0; }     inline int pre(int x) { return *--mp.lower_bound(x); }     inline int nxt(int x) { return *mp.upper_bound(x); }     inline void add(int x, int y, int w) {       if (x && y != N) val += ST::ask(x, y) * w;     }     inline void ins(int x) {       int p = pre(x), q = nxt(x);       add(p, q, 1), add(p, x, -1), add(x, q, -1), mp.insert(x);     }   } t[N];   namespace SAM {   int n, tot, last, top;   int fa[N], len[N], ch[N][26], d[N], sta[N], pos[N];   inline void cpy(int x, int y) {     for (int i = 0; i <= 25; i++) ch[x][i] = ch[y][i];   }   inline void ins(int x) {     int p(last), np, q, nq;     len[np = last = ++tot] = len[p] + 1;     while (p && !ch[p][x]) ch[p][x] = np, p = fa[p];     if (!p) return void(fa[np] = 1);     if (len[q = ch[p][x]] == len[p] + 1) return void(fa[np] = q);     cpy(nq = ++tot, q), len[nq] = len[p] + 1, fa[nq] = fa[q], fa[q] = fa[np] = nq;     while (p && ch[p][x] == q) ch[p][x] = nq, p = fa[p];   }   inline void input(char* a, int len) {     n = len, tot = last = pos[0] = 1;     for (int i = 1; i <= n; i++) ins(a[i] -  a ), pos[i] = last;   }   inline void merge(tree& x, tree& y) {     if (x.mp.size() < y.mp.size()) swap(x, y);     for (int v : y.mp)       if (v && v != N) x.ins(v), x.val += n - SA::sa[v] + 1;   }   inline void work() {     long long int ans = 0;     for (int i = 0; i <= tot; i++) d[fa[i]]++, t[i].init();     for (int i = 1; i <= tot; i++)       if (!d[i]) sta[++top] = i;     for (int i = 0; i <= n - 2; i++)       t[pos[i]].ins(SA::x[i + 2]), t[pos[i]].val += n - i - 1;     for (int i = 1; i <= tot; i++) ans += len[i] - len[fa[i]] << (i != pos[n]);     while (int u = sta[top--]) {       ans += t[u].val * (len[u] - len[fa[u]]);       if (fa[u]) merge(t[fa[u]], t[u]);       if (!--d[fa[u]]) sta[++top] = fa[u];     }     cout << ans + t[1].val + 2;   }   };  // namespace SAM   int n;   char a[N];   int main() {     scanf( %s , a + 1), n = strlen(a + 1), SA::input(a, n);     SA::init(), SAM::input(a, n), SAM::work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int p[2 * 1000005];   int n;   int input() {     int i;     scanf( %d , &n);     for (i = 1; i <= n; i++) p[i] = i;     return 0;   }   int solve() {     int i, j, ii;     int t1, t2;     for (i = 2; i <= n; i++) {       t1 = p[i - 1];       for (j = i + i - 1; j < n + i - 1; j += i) {         t2 = p[j];         p[j] = t1;         t1 = t2;       }       p[n + i - 1] = t1;     }     return 0;   }   int output() {     int i;     for (i = n; i < n + n; i++) printf( %d  , p[i]);     printf(  n );     return 0;   }   int main() {     input();     solve();     output();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   int C[N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, r;     cin >> n >> r;     double sum = 0;     for (int i = 0; i < (1 << n); i++) {       cin >> C[i];       sum += C[i];     }     cout << setprecision(15) << fixed << sum / (1 << n) <<   n ;     while (r--) {       int z, g;       cin >> z >> g;       sum += g - C[z];       C[z] = g;       cout << setprecision(15) << fixed << sum / (1 << n) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 7;   long long a[maxn], b[maxn];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i] >> b[i];       a[i] ^= b[i], b[i] ^= b[i - 1];     }     vector<long long> linbs;     for (int i = 1; i <= n; i++) {       long long temp = a[i];       for (auto &e : linbs) temp = min(temp, temp ^ e);       if (temp) {         for (auto &e : linbs) e = min(e, e ^ temp);         linbs.push_back(temp);       }     }     for (auto &e : linbs) b[n] = min(b[n], b[n] ^ e);     if (b[n])       cout << 1 <<  /  << 1 << endl;     else {       int sz = linbs.size();       long long t1 = 1ll << sz, t2 = t1 - 1;       cout << t2 <<  /  << t1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1000000007;   void __print(long long int x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename Th, typename V>   void __print(const pair<Th, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename Th>   void __print(const Th &x) {     long long int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename Th, typename... V>   void _print(Th t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   void binary_rock() {     long long int v, c, n, m;     cin >> v >> c >> n >> m;     if (v + c < n + m) {       cout <<  No  << endl;       return;     }     long long int mini = min(v, c);     if (mini >= m) {       cout <<  Yes  << endl;       return;     }     if (v >= c) {       long long int req = (v - c);       if (n < req) {         cout <<  No  << endl;         return;       }       n -= req;       v -= req;       if (n >= m) {         cout <<  Yes  << endl;         return;       }       m -= n;       v -= n;       c -= n;       n -= n;       if (m > v) {         cout <<  No  << endl;         return;       } else {         cout <<  Yes  << endl;         return;       }       return;     } else {       long long int req = (c - v);       if (n < req) {         cout <<  No  << endl;       } else {         n -= req;         c -= req;         if (n >= m) {           cout <<  Yes  << endl;           return;         }         m -= n;         v -= n;         c -= n;         n -= n;         if (m > v) {           cout <<  No  << endl;           return;         } else {           cout <<  Yes  << endl;           return;         }         cout <<  Yes  << endl;       }       return;     }   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     clock_t t1 = clock();     long long int t = 1;     cin >> t;     while (t--) {       binary_rock();     }     clock_t t2 = clock();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 305, Q = (int)2e6 + 50;   struct Qry {     int x, y, c;     void read() {       cin >> x >> y >> c;       x--, y--;     }   } qry[Q];   int n, m, q;   int mp[N][N];   int f[N * N];   int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};   int ch[Q], off[Q];   int ans[Q];   int gid(int i, int j) { return i * m + j; }   int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }   bool unite(int x, int y) {     x = find(x);     y = find(y);     if (x == y) return false;     f[x] = y;     return true;   }   int build(int val, int ig) {     for (int k = 0; k < n * m; k++) {       f[k] = k;     }     for (int ki = 0; ki < n; ki++) {       for (int kj = 0; kj < m; kj++) {         if (ig != -1 && (!((mp[ki][kj] == val) ^ ig))) continue;         if (ki + 1 < n && mp[ki + 1][kj] == mp[ki][kj])           unite(gid(ki + 1, kj), gid(ki, kj));         if (kj + 1 < m && mp[ki][kj + 1] == mp[ki][kj])           unite(gid(ki, kj + 1), gid(ki, kj));       }     }     int ccnt = 0;     for (int k = 0; k < n * m; k++) ccnt += (f[k] == k);     return ccnt;   }   bool in_bound(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m >> q;     for (int i = 0; i < q; i++) qry[i].read();     int r = 0;     for (int i = 0; i < q; i = r) {       while (r < q && qry[r].c == qry[i].c) r++;       build(qry[i].c, 0);       int cur = 0;       for (int j = i; j < r; j++) {         int x = qry[j].x, y = qry[j].y;         ch[j] = mp[x][y];         if (mp[x][y] != qry[j].c) cur++;         mp[x][y] = qry[j].c;         for (int d = 0; d < 4; d++) {           int nx = x + dx[d], ny = y + dy[d];           if (in_bound(nx, ny) && mp[x][y] == mp[nx][ny]) {             if (unite(gid(x, y), gid(nx, ny))) cur--;           }         }         off[j] += cur;       }       cur = 0;       build(qry[i].c, 1);       for (int j = r - 1; j >= i; j--) {         off[j] += cur;         int x = qry[j].x, y = qry[j].y;         if (mp[x][y] != ch[j]) {           cur++;           mp[x][y] = ch[j];           for (int d = 0; d < 4; d++) {             int nx = x + dx[d], ny = y + dy[d];             if (in_bound(nx, ny) && mp[x][y] == mp[nx][ny]) {               if (unite(gid(x, y), gid(nx, ny))) cur--;             }           }         }       }       int ccnt = build(qry[i].c, -1);       for (int j = i; j < r; j++) {         ans[j] = ccnt - cur + off[j];       }       for (int j = i; j < r; j++) {         int x = qry[j].x, y = qry[j].y;         mp[x][y] = qry[j].c;       }     }     for (int i = 0; i < q; i++) cout << ans[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, x[105], mem[101][201][51][3], ans;   string s;   int solve(int idx, int pos, int rem, int dir) {     int &ret = mem[idx][100 + pos][rem][dir + 1];     if (rem == 0) {       int cur_dir = dir, sum = 0;       for (int i = idx; i < s.size(); i++)         if (x[i])           sum += cur_dir;         else           cur_dir *= -1;       return abs(pos + sum);     }     if (idx == s.size()) return -1e8;     if (ret != -1) return ret;     int next_dir = (x[idx]) ? dir : -dir;     int c1 = solve(idx + 1, pos + x[idx] * dir, rem, next_dir);     int new_dir = (x[idx]) ? -dir : dir;     int new_pos = (x[idx]) ? pos : pos + dir;     int c2 = -1e8;     if (rem >= 2) c2 = solve(idx, pos, rem - 2, dir);     int c3 = -1e8;     if (rem >= 1) c3 = solve(idx + 1, new_pos, rem - 1, new_dir);     ans = max(ans, max(max(c1, c2), c3));     return ret = ans;   }   int main() {     cin >> s >> n;     memset(mem, -1, sizeof mem);     for (int i = 0; i < s.size(); i++) x[i] = (s[i] ==  F );     cout << max(solve(0, 0, n, -1), solve(0, 0, n, 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a >> b;     if (a == b) {       cout << a;     } else {       cout << 1;     }   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, p, x;   long long Pow(long long x, long long y, long long mod) {     if (!y) return 1;     if (y == 1) return x % mod;     long long t = Pow(x, y >> 1, mod), ans = t * t % mod;     if (y & 1) ans = ans * x % mod;     return ans;   }   int main() {     while (scanf( %lld %lld %lld %lld , &a, &b, &p, &x) != EOF) {       long long P = p * (p - 1), ans = 0;       for (long long i = 1; i < p; i++) {         long long r = b * Pow(Pow(a, i, p), p - 2, p) % p, c;         if (i >= r)           c = (i + (i - r) * (p - 1)) % P;         else           c = (i + (i + p - r) * (p - 1)) % P;         if (!c) c = P;         ans += ((x / P) + ((x % P >= c) ? 1 : 0));       }       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int i, j;     if (k == 1 && n == 1) {       cout <<  a ;       return 0;     }     if (k > n || (k == 1)) {       cout << -1;       return 0;     }     i = 1;     while (n - i >= k - 2) {       cout << char( a  + !(i % 2));       ++i;     }     for (i = 2; i < k; i++) cout << char( a  + i);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int a, b, c;       cin >> a >> b >> c;       cout << a + b + c - (2 * (min(min(a, b), min(b, c)))) << endl;     }   }


#include<bits/stdc++.h>   #include<vector>   #include<map>   #include<queue>   #define LL long long   #define INF INT64_MAX   #define MOD 1000000007   #define stree SegTree[root]   #define lson SegTree[root << 1]   #define rson SegTree[root << 1 | 1]   using namespace std;   const int N = 100005;   LL a[N];   int main()   {       int t, n;       cin>>t;       LL Min = INF;       int tt = 0;       while(t--)       {           tt++;           cin>>n;           for(int i = 1;i <= n;i++)           {               cin>>a[i];           }           LL ans = INF, tem = 0, m1 = INT_MAX, m2 = INT_MAX;           for(int i = 1;i <= n;i++)           {               if(i&1)               {                   m1 = min(m1, a[i]);                   ans = min(ans, tem + m1*(n-i/2) + m2*(n-i/2));                   tem += a[i];                   //printf( 1ans = %lld tem = %lld n , ans, tem);               }               else               {                   m2 = min(m2, a[i]);                   ans = min(ans, tem + m2*(n-(i-1)/2) + m1*(n-i/2));                   tem += a[i];                   //printf( 2ans = %lld tem = %lld n , ans, tem);               }           }           printf( %lld n , ans);       }       return 0;   }


#include <bits/stdc++.h>   #include <time.h>   using namespace std;      #define ll long long   #define ld long double   #define precision(x,d) cout<<fixed<<setprecision(d)<<x   #define all(x) x.begin(),x.end()   #define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));   #define MOD 1000000007   #define INF LONG_LONG_MAX   #define NINF LONG_LONG_MIN   #define endl   n    #define yes cout<< YES <<endl   #define no cout<< NO <<endl   #define db4(x,y,z,k) cout<<#x<<  :  <<x<<  t <<#y<<  :  <<y<<  t <<#z<<  :  <<z<<  t <<#k<<  :  <<k<<endl;      int main()   {       IOS;       ll t,n,m,k,u,v,x,y;       t=1;       cin >> t;       while(t--)       {           cin >> n;           vector<ll> a(n);           for(int i=0;i<n;i++)               cin >> a[i];           sort(all(a));           ll ans = 0;           for(int i=1;i<n;i++)           {               m = i;               ans -= m*a[i];               k = (n-i-1);               ans += k*a[i];           }           ans += a[n-1];           cout << ans << endl;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using Pii = pair<int, int>;   using Pll = pair<ll, ll>;   template <typename...>   static inline int getchar_unlocked(void) {     return getchar();   }   template <typename...>   static inline void putchar_unlocked(int c) {     putchar(c);   }   void reader(int& x) {     int k, m = 0;     x = 0;     for (;;) {       (k) = getchar_unlocked();       if (k ==  - ) {         m = 1;         break;       }       if ( 0  <= k && k <=  9 ) {         x = k -  0 ;         break;       }     }     for (;;) {       (k) = getchar_unlocked();       if (k <  0  || k >  9 ) break;       x = x * 10 + k -  0 ;     }     if (m) x = -x;   }   void reader(ll& x) {     int k, m = 0;     x = 0;     for (;;) {       (k) = getchar_unlocked();       if (k ==  - ) {         m = 1;         break;       }       if ( 0  <= k && k <=  9 ) {         x = k -  0 ;         break;       }     }     for (;;) {       (k) = getchar_unlocked();       if (k <  0  || k >  9 ) break;       x = x * 10 + k -  0 ;     }     if (m) x = -x;   }   int reader(char c[]) {     int i, s = 0;     for (;;) {       (i) = getchar_unlocked();       if (i !=     && i !=   n  && i !=   r  && i !=   t  && i != EOF) break;     }     c[s++] = i;     for (;;) {       (i) = getchar_unlocked();       if (i ==     || i ==   n  || i ==   r  || i ==   t  || i == EOF) break;       c[s++] = i;     }     c[s] =   0 ;     return s;   }   int reader(string& c) {     int i;     for (;;) {       (i) = getchar_unlocked();       if (i !=     && i !=   n  && i !=   r  && i !=   t  && i != EOF) break;     }     c.push_back(i);     for (;;) {       (i) = getchar_unlocked();       if (i ==     || i ==   n  || i ==   r  || i ==   t  || i == EOF) break;       c.push_back(i);     };     return ((int)c.size());   }   template <class T, class S>   void reader(T& x, S& y) {     reader(x);     reader(y);   }   template <class T, class S, class U>   void reader(T& x, S& y, U& z) {     reader(x);     reader(y);     reader(z);   }   template <class T, class S, class U, class V>   void reader(T& x, S& y, U& z, V& w) {     reader(x);     reader(y);     reader(z);     reader(w);   }   void writer(int x, char c) {     int s = 0, m = 0;     char f[10];     if (x < 0) m = 1, x = -x;     while (x) f[s++] = x % 10, x /= 10;     if (!s) f[s++] = 0;     if (m) putchar_unlocked( - );     while (s--) putchar_unlocked(f[s] +  0 );     putchar_unlocked(c);   }   void writer(ll x, char c) {     int s = 0, m = 0;     char f[20];     if (x < 0) m = 1, x = -x;     while (x) f[s++] = x % 10, x /= 10;     if (!s) f[s++] = 0;     if (m) putchar_unlocked( - );     while (s--) putchar_unlocked(f[s] +  0 );     putchar_unlocked(c);   }   void writer(const char c[]) {     int i;     for (i = 0; c[i] !=   0 ; i++) putchar_unlocked(c[i]);   }   void writer(const string& x, char c) {     int i;     for (i = 0; x[i] !=   0 ; i++) putchar_unlocked(x[i]);     putchar_unlocked(c);   }   void writer(const char x[], char c) {     int i;     for (i = 0; x[i] !=   0 ; i++) putchar_unlocked(x[i]);     putchar_unlocked(c);   }   template <class T>   void writerLn(T x) {     writer(x,   n );   }   template <class T, class S>   void writerLn(T x, S y) {     writer(x,    );     writer(y,   n );   }   template <class T, class S, class U>   void writerLn(T x, S y, U z) {     writer(x,    );     writer(y,    );     writer(z,   n );   }   template <class T, class S, class U, class V>   void writerLn(T x, S y, U z, V v) {     writer(x,    );     writer(y,    );     writer(z,    );     writer(v,   n );   }   template <class T>   void writerArr(T x[], int n) {     if (!n) {       putchar_unlocked(  n );       return;     }     for (int i = 0; i < (n - 1); i++) writer(x[i],    );     writer(x[n - 1],   n );   }   template <class T>   void writerArr(vector<T>& x) {     writerArr(x.data(), (int)x.size());   }   template <class T>   void chmin(T& a, const T& b) {     if (a > b) a = b;   }   template <class T>   void chmax(T& a, const T& b) {     if (a < b) a = b;   }   template <class T>   T gcd(T a, T b) {     return b ? gcd(b, a % b) : a;   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   ll mod_pow(ll a, ll n, ll mod) {     ll ret = 1;     ll p = a % mod;     while (n) {       if (n & 1) ret = ret * p % mod;       p = p * p % mod;       n >>= 1;     }     return ret;   }   template <class T>   T extgcd(T a, T b, T& x, T& y) {     for (T u = y = 1, v = x = 0; a;) {       T q = b / a;       swap(x -= q * u, u);       swap(y -= q * v, v);       swap(b -= q * a, a);     }     return b;   }   ll mod_inv(ll a, ll m) {     ll x, y;     extgcd<ll>(a, m, x, y);     return (m + x % m) % m;   }   int test(int a, int b, int c) {     set<tuple<int, int, int>> st;     for (int i = 0; i < (a + 1); i++)       if (i != 0 && a % i == 0) {         for (int j = 0; j < (b + 1); j++)           if (j != 0 && b % j == 0) {             for (int k = 0; k < (c + 1); k++)               if (k != 0 && c % k == 0) {                 int cur[] = {i, j, k};                 sort(cur, cur + 3);                 st.emplace(cur[0], cur[1], cur[2]);               }           }       }     int x[] = {0, 0, 0, 0};     for (auto abc : st) {       int a, b, c;       tie(a, b, c) = abc;       if (a == b && b == c)         x[0]++;       else if (a == b && b < c)         x[1]++;       else if (a < b && b == c)         x[2]++;       else if (a < b && b < c)         x[3]++;       else {         cout <<  ? ;       }     }     return ((int)st.size());   }   int cnt[((int)1e5) + 1];   void init() {     for (int i = 1; i <= ((int)1e5); i++) {       for (int j = i; j <= ((int)1e5); j += i) {         cnt[j]++;       }     }   }   int solve(int x, int y, int z) {     int g = cnt[gcd(gcd(x, y), z)];     int d = cnt[gcd(x, y)] - g;     int e = cnt[gcd(x, z)] - g;     int f = cnt[gcd(y, z)] - g;     int a = cnt[x] - d - e - g;     int b = cnt[y] - d - f - g;     int c = cnt[z] - f - e - g;     int h[] = {a, b, c, d, e, f, g};     int oks[3][7] = {         {1, 0, 0, 1, 1, 0, 1},         {0, 1, 0, 1, 0, 1, 1},         {0, 0, 1, 0, 1, 1, 1},     };     bool used[7][7][7] = {};     auto checkUsed = [&](int i, int j, int k) {       if (i > k) swap(i, k);       if (j > k) swap(j, k);       if (i > j) swap(i, j);       if (used[i][j][k]) return false;       used[i][j][k] = true;       return true;     };     int part[5] = {};     int ans = 0;     for (int i = 0; i < (7); i++)       if (oks[0][i]) {         for (int j = 0; j < (7); j++)           if (oks[1][j]) {             for (int k = 0; k < (7); k++)               if (oks[2][k]) {                 if (!checkUsed(i, j, k)) {                   continue;                 }                 if (i != j && j != k && i != k) {                   int cur = h[i] * h[j] * h[k];                   ans += cur;                   part[0] += cur;                 } else if (i == j && j != k) {                   int cur = (h[i] - 1) * h[i] / 2 + h[i];                   cur *= h[k];                   ans += cur;                   part[1] += cur;                 } else if (i != j && j == k) {                   int cur = (h[j] - 1) * h[j] / 2 + h[j];                   cur *= h[i];                   ans += cur;                   part[2] += cur;                 } else if (i == k && j != k) {                   int cur = (h[k] - 1) * h[k] / 2 + h[k];                   cur *= h[j];                   ans += cur;                   part[3] += cur;                 } else {                   int aa = h[i];                   int bb = (h[i]) * (h[i] - 1) * (h[i] - 2);                   int cc = h[i] * h[i] * h[i] - aa - bb;                   int cur = aa + bb / 6 + cc / 3;                   ans += cur;                   part[4] += cur;                 }               }           }       }     return ans;   }   void test() {     for (int i = 1; i <= 70; i++) {       for (int j = 1; j <= 70; j++) {         for (int k = 1; k <= 70; k++) {           int a1 = test(i, j, k);           int a2 = solve(i, j, k);           if (a1 != a2) {             cout <<  ? ;           }         }       }     }     cout <<  ok  << endl;   }   int main() {     init();     int t;     reader(t);     while (t--) {       int x, y, z;       reader(x, y, z);       int ans = solve(x, y, z);       writerLn(ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int c[301];   vector<int> a[301];   vector<int> lt[301];   int id[301];   int dem = 0;   int d[301];   void nhap() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &c[i]);       id[c[i]] = i;     }     for (int i = 1; i <= n; i++) {       string s;       cin >> s;       for (int j = 0; j < s.length(); j++) {         if (s[j] ==  1 ) {           int u = c[i];           int v = c[j + 1];           a[u].push_back(v);           a[v].push_back(u);         }       }     }   }   void bfs(int s) {     queue<int> q;     q.push(s);     dem++;     d[s] = 1;     while (!q.empty()) {       int u = q.front();       q.pop();       lt[dem].push_back(u);       for (vector<int>::iterator i = a[u].begin(); i != a[u].end(); i++) {         if (d[*i] == 0) {           d[*i] = 1;           q.push(*i);         }       }     }   }   void xuly(int k) {     priority_queue<int, vector<int>, greater<int> > q;     vector<int> t;     for (auto i : lt[k]) {       q.push(i);       t.push_back(id[i]);     }     sort(t.begin(), t.end());     for (auto i : t) {       c[i] = q.top();       q.pop();     }   }   int main() {     nhap();     for (int i = 1; i <= n; i++) {       if (d[i] == 0) {         bfs(i);       }     }     for (int i = 1; i <= dem; i++) {       xuly(i);     }     for (int i = 1; i <= n; i++) cout << c[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     if (a == 0) return b;     return gcd(b % a, a);   }   template <typename T>   T pow(T a, T b, long long m) {     T ans = 1;     while (b > 0) {       if (b % 2 == 1) ans = (ans * a) % m;       b /= 2;       a = (a * a) % m;     }     return ans % m;   }   const long long N = 1e6 + 5;   long long n, m;   long long a[N], b[N], x[N], y[N];   map<long long, long long> ans;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> a[i] >> x[i];       ans[a[i]] = x[i];     }     cin >> m;     for (long long i = 1; i <= m; i++) {       cin >> b[i] >> y[i];       ans[b[i]] = max(ans[b[i]], y[i]);     }     long long answer = 0;     for (auto it : ans) answer += it.second;     cout << answer;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2e5 + 888;   long long dp[maxn][12];   void solve() {     long long n;     scanf( %lld , &n);     memset(dp, -1, sizeof(dp));     dp[0][1] = 0;     vector<long long> v[4];     for (long long i = 1; i <= n; i++) {       for (long long j = 0; j < 4; j++) v[j].clear();       long long k;       scanf( %lld , &k);       while (k--) {         long long c, d;         scanf( %lld %lld , &c, &d);         v[c].push_back(d);       }       for (long long j = 1; j <= 3; j++) {         sort(v[j].begin(), v[j].end());         reverse(v[j].begin(), v[j].end());       }       for (long long prec = 0; prec <= 9; prec++) {         if (dp[i - 1][prec] < 0) continue;         long long val = LLONG_MIN / 10;         if (!v[1].empty()) val = max(val, v[1][0]);         if (!v[2].empty()) val = max(val, v[2][0]);         if (!v[3].empty()) val = max(val, v[3][0]);         if (prec == 0) val *= 2;         if (val > 0)           dp[i][(prec + 1) % 10] =               max(dp[i][(prec + 1) % 10], dp[i - 1][prec] + val);         val = LLONG_MIN / 10;         if (!v[1].empty() && !v[2].empty()) {           long long temp = v[1][0] + v[2][0];           long long mx = max(v[1][0], v[2][0]);           if (prec == 0 || prec == 9) temp += mx;           val = max(val, temp);         }         if (v[1].size() >= 2) {           long long temp = v[1][0] + v[1][1];           long long mx = max(v[1][0], v[1][1]);           if (prec == 0 || prec == 9) temp += mx;           val = max(val, temp);         }         if (val > 0)           dp[i][(prec + 2) % 10] =               max(dp[i][(prec + 2) % 10], dp[i - 1][prec] + val);         val = LLONG_MIN / 10;         if (v[1].size() >= 3) {           long long temp = v[1][0] + v[1][1] + v[1][2];           long long mx = max(v[1][0], max(v[1][2], v[1][1]));           if (prec == 0 || prec == 9 || prec == 8) temp += mx;           val = max(val, temp);         }         if (val > 0)           dp[i][(prec + 3) % 10] =               max(dp[i][(prec + 3) % 10], dp[i - 1][prec] + val);         dp[i][prec] = max(dp[i][prec], dp[i - 1][prec]);       }     }     long long ans = 0;     for (long long i = 0; i <= 9; i++) ans = max(ans, dp[n][i]);     cout << ans << endl;   }   signed main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 5;   unsigned long long bt[maxn];   unsigned long long d[maxn];   map<unsigned long long, int> has;   pair<int, int> edg[maxn];   int f[maxn];   int id[maxn];   int lab[maxn];   vector<int> e[maxn];   int vis[maxn];   int getf(int x) {     if (x == f[x])       return x;     else       return f[x] = getf(f[x]);   }   void dfs(int x, int y, int de) {     int i;     lab[x] = de;     vis[x] = 1;     for (i = 0; i < (int)e[x].size(); i++) {       if (e[x][i] == y) continue;       dfs(e[x][i], x, de + 1);     }   }   int main() {     int i, j, n, m;     cin >> n >> m;     int x, y;     bt[0] = 1;     for (int i = 1; i <= n; i++) {       bt[i] = 3 * bt[i - 1];       d[i] = bt[i];     }     for (i = 0; i < m; i++) {       scanf( %d%d , &x, &y);       edg[i] = make_pair(x, y);       d[x] += bt[y], d[y] += bt[x];     }     j = 1;     for (i = 1; i <= n; i++) {       if (!has[d[i]]) has[d[i]] = j++;       id[i] = has[d[i]];       f[i] = i;     }     for (i = 0; i < m; i++) {       x = id[edg[i].first], y = id[edg[i].second];       if (x == y) continue;       for (j = 0; j < (int)e[x].size(); j++) {         if (e[x][j] == y) break;       }       if (j >= 2) {         printf( NO n );         return 0;       }       if (j == (int)e[x].size())         e[x].push_back(y);       else         continue;       for (j = 0; j < (int)e[y].size(); j++) {         if (e[y][j] == x) break;       }       if (j >= 2) {         printf( NO n );         return 0;       }       if (j == (int)e[y].size())         e[y].push_back(x);       else         continue;       if (getf(x) == getf(y)) {         printf( NO n );         return 0;       } else         f[getf(y)] = f[getf(x)];     }     int cnt = 0;     for (i = 1; i <= n; i++) {       if (vis[id[i]] == 0 && (int)e[id[i]].size() < 2) {         vis[id[i]] = 1;         dfs(id[i], -1, 1);       }     }     printf( YES n );     for (i = 1; i <= n; i++) printf(i == n ?  %d n  :  %d  , lab[id[i]]);   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX_N = 2e5 + 5, inf = 1e18, mod = 1000000007;   const double PI = 3.1415926536;   int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   template <typename T>   ostream& operator<<(ostream& out, const vector<T>& mas) {     for (const auto& x : mas) {       out << x <<    ;     }     return out;   }   void ok() { cout <<  YES  <<   n ; }   void no() { cout <<  NO  <<   n ; }   inline long long nxt() {     long long x;     cin >> x;     return x;   }   struct Node {     vector<int> g;     long long sum = 0;     long long sum1 = 0;     void push(int v) { g.push_back(v); }     vector<int>::iterator begin() { return g.begin(); }     vector<int>::iterator end() { return g.end(); }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr), cout.tie(nullptr);     int n = nxt();     vector<Node> g(n);     vector<long long> a(n);     long long total = 0;     for (int i = 0; i < n; i++) {       a[i] = nxt();       total += a[i];     }     for (int i = 0; i < n - 1; i++) {       int v = nxt() - 1, u = nxt() - 1;       g[v].push(u);       g[u].push(v);     }     vector<int> lvl(n);     function<void(int, int)> dfs = [&](int v, int p) {       g[v].sum1 = a[v] * lvl[v];       g[v].sum = a[v];       for (const auto& to : g[v]) {         if (to != p) {           lvl[to] += lvl[v] + 1;           dfs(to, v);           g[v].sum += g[to].sum;           g[v].sum1 += g[to].sum1;         }       }     };     dfs(0, -1);     long long ans = 0;     long long res = g[0].sum1;     function<void(int, int)> calc = [&](int v, int p) {       ans = max(ans, res);       for (const auto& to : g[v]) {         if (to == p) continue;         g[v].sum -= g[to].sum;         res -= g[to].sum;         g[to].sum += g[v].sum;         res += g[v].sum;         calc(to, v);         res -= g[v].sum;         g[to].sum -= g[v].sum;         res += g[to].sum;         g[v].sum += g[to].sum;       }     };     calc(0, -1);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Mod = 1000000000 + 7;   const int MAXK = 1000000;   int power(int x, int k) {     int ret = 1;     while (k) {       if (k & 1) ret = 1LL * ret * x % Mod;       x = 1LL * x * x % Mod;       k >>= 1;     }     return ret;   }   int n, k, ans;   int f[MAXK + 10];   void init() {     cin >> n >> k;     if (n <= k + 10) {       for (int i = 1; i <= n; i++) ans = (power(i, k) + ans) % Mod;       cout << ans;     } else {       for (int i = 1; i <= k + 2; i++) f[i] = (f[i - 1] + power(i, k)) % Mod;       int s = 1, m = 1;       for (int j = 2; j <= k + 2; j++) s = 1LL * s * (n - j) % Mod;       for (int j = 2; j <= k + 2; j++) m = 1LL * m * ((1 - j) + Mod) % Mod;       int l = -(k + 1), r = 1;       for (int i = 1; i <= k + 2; i++) {         ans = (1LL * f[i] * s % Mod * power(m, Mod - 2) % Mod + ans) % Mod;         s = 1LL * s * (n - i) % Mod * power(n - (i + 1), Mod - 2) % Mod;         m = 1LL * m * r % Mod * power(l + Mod, Mod - 2) % Mod;         l++, r++;       }       cout << ans;     }     return;   }   int main() {     init();     return 0;   }