#include <bits/stdc++.h>   using namespace std;   stack<int> s;   priority_queue<int> pq;   long long maxm = numeric_limits<long long>::max();   const int maxn = 10005;   struct node {     int l, r;   } q[1001];   bool cmp(node a, node b) { return a.l < b.l; }   int main() {     int n, sum = 0;     scanf( %d , &n);     int a[maxn], l[maxn], r[maxn];     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       sum += a[i];     }     int m, tmp;     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &q[i].l, &q[i].r);     }     sort(q + 1, q + 1 + m, cmp);     if (sum > q[m].r)       printf( -1 n );     else if (sum < q[1].l)       printf( %d n , q[1].l);     else {       for (int i = 1; i <= m; i++) {         if (sum > q[i].r && sum < q[i + 1].l) {           printf( %d n , q[i + 1].l);           break;         } else if (sum >= q[i].l && sum <= q[i].r) {           printf( %d n , sum);           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     bool b[4][n][n];     for (int i = 0; i < 4; i++) {       for (int j = 0; j < n; j++) {         char c[n + 1];         scanf( %s , &c);         for (int k = 0; k < n; k++) b[i][j][k] = c[k] ==  1  ? 1 : 0;       }     }     int s[] = {0, 1, 2, 3}, mi = 1e9, co = 0;     bool o = 0;     do {       for (int k = 0; k < 4; k += 2) {         for (int j = 0; j < n; j++) {           for (int i = 0; i < n; i++) {             if (b[s[k]][j][i] != o) co++;             o ^= 1;           }           for (int i = 0; i < n; i++) {             if (b[s[k + 1]][j][i] != o) co++;             o ^= 1;           }           o ^= 1;         }       }       mi = min(mi, co);       co = 0;     } while (next_permutation(s, s + 4));     cout << mi <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, M;   int A[50050], X[50500];   int L[50050], R[50500];   int XX[1010101];   int ma[50505];   int ret[50505];   signed long long xorxor(signed long long val) {     int i;     signed long long ret = ((val + 1) / 2) % 2;     for (i = 0; i <= 29; i++)       if ((val >> i) % 2) ret |= (1 ^ (val % 2)) << i;     return ret;   }   void solve() {     int i, j, k, l, r, x, y;     string s;     for (i = 1; i <= 1000000; i++) XX[i] = XX[i - 1] ^ i;     cin >> N >> M;     for (i = 0; i < (N); i++) cin >> A[i], X[i] = XX[A[i]];     for (i = 0; i < (M); i++) cin >> L[i] >> R[i], L[i]--, R[i]--;     for (x = 0; x < (N); x++) {       ma[x - 1] = 0;       for (y = x; y < N; y++)         ma[y] = max(ma[y - 1], X[x] ^ X[y] ^ ((A[x] > A[y]) ? A[y] : A[x]));       for (i = 0; i < (M); i++)         if (L[i] <= x) ret[i] = max(ret[i], ma[R[i]]);     }     for (i = 0; i < (M); i++) cout << ret[i] << endl;   }   int main(int argc, char** argv) {     string s;     int i;     if (argc == 1) ios::sync_with_stdio(false);     for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ;     for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   string a;   int f[1005][5];   int main() {     cin >> n >> a;     string t =   , s =   ;     char cur =  0 ;     for (int i = 0; i <= n - 1; i++) {       t += cur;       s +=  1  - cur +  0 ;       cur =  1  - cur +  0 ;     }     int ans = (int)2e9;     int res = 0, res1 = 0;     for (int i = 0; i <= n - 1; i++) {       if (a[i] != s[i]) res++;       if (a[i] != t[i]) res1++;     }     ans = min(res1, res);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, k;   string s;   int MOD = 1e9 + 7;   const int INF = 1e9;   const long long INF64 = 2e18;   const double PI = 3.141592653589793238463;   long long ar[1000004], ar1[1000004], vis[1000004], pre[1000004], dp[3004][3004];   vector<long long> gr[1000004], path;   set<long long> myset;   pair<long long, long long> seg[1000004];   map<long long, long long> mymap;   long long c = 0;   string st1[1000004], st2[1000004];   vector<long long> arr[1000004];   int comp(pair<long long, long long> a, pair<long long, long long> b) {     return a.second < b.second;   }   int gcd(int a, int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     cin >> n;     double w, cargo;     cin >> w;     for (int i = 0; i < int(n); i++) cin >> ar[i];     for (int i = 0; i < int(n); i++) cin >> ar1[i];     if (ar[0] == 1 || ar1[0] == 1) return cout << -1, 0;     cargo = w;     w += w / (ar1[0] - 1);     for (int i = n - 1; i > 0; i--) {       if (ar[i] == 1 || ar1[i] == 1) return cout << -1, 0;       w += w / (ar[i] - 1);       w += w / (ar1[i] - 1);     }     w += w / (ar[0] - 1);     return printf( %10.10f , w - cargo), 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 7005;   const int M = 200000;   struct state {     int l, r, val;     state() {}     state(int l, int r, int val) {       this->l = l;       this->r = r;       this->val = val;     }   };   int n, m, dl[M], dr[M];   vector<state> v[N];   int solve(int d, int x, int y) {     set<int> ans;     for (int i = d; i <= n; i++) {       for (int j = 0; j < v[i].size(); j++) {         state& c = v[i][j];         if (c.l <= y && c.r >= x) ans.insert(c.val);       }       x = (dl[x] == -1 ? dr[x] : dl[x]);       y = dr[y];     }     return ans.size();   }   void init() {     int cnt = 1, p = 3;     memset(dl, -1, sizeof(dl));     memset(dr, -1, sizeof(dr));     dl[1] = 1;     dr[1] = 2;     for (int i = 2; i < M; i++) {       if ((1 << cnt) == i) {         cnt++;         dl[i] = p++;       }       dr[i] = p++;     }   }   int main() {     init();     int order, d, x, y, val;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d , &order);       if (order == 1) {         scanf( %d%d%d%d , &d, &x, &y, &val);         v[d].push_back(state(x, y, val));       } else {         scanf( %d%d , &d, &x);         printf( %d n , solve(d, x, x));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long modpow(long long base, long long exp, long long modulus) {     base %= modulus;     long long result = 1;     while (exp > 0) {       if (exp & 1) result = (result * base) % modulus;       base = (base * base) % modulus;       exp >>= 1;     }     return result;   }   int main() {     long long n, k;     cin >> n >> k;     long long w[100001];     long long q[100001];     memset(q, 0, sizeof(q));     memset(w, 0, sizeof(w));     w[0] = 1;     if (1 >= k)       w[1] = 2;     else       w[1] = 1;     q[0] = 0;     q[1] += q[0] + w[1];     for (long long i = 2; i < 100001; i++) {       if (k <= i) {         w[i] += (w[i - 1] + w[i - k] - 1000000007) % 1000000007;       } else         w[i] = 1;       q[i] += (q[i - 1] + w[i] - 1000000007) % 1000000007;       q[i] %= 1000000007;     }     while (n--) {       long long a, b;       cin >> a >> b;       cout << (q[b] - q[a - 1] + 1000000007) % 1000000007 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   const long long inf = 0x3f3f3f3f;   using namespace std;   const long long maxn = 2e5 + 100;   long long n, k;   long long a[maxn];   int32_t main() {     long long q;     scanf( %lld , &(q));     while (q--) {       scanf( %lld%lld , &(n), &(k));       long long maxx = 0, minn = 0x3f3f3f3f;       for (long long i = (1); i <= (n); i++) {         scanf( %lld , &(a[i]));         if (maxx < a[i]) maxx = a[i];         if (minn > a[i]) minn = a[i];       }       long long x = minn + k;       if (abs(x - maxx) > k)         printf( -1 );       else         printf( %lld , (x));       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     char c = getchar();     int x = 0, f = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = x * 10 + c -  0 ;       c = getchar();     }     return x * f;   }   const int maxn = 1e6 + 10;   const int inf = 2147483647;   int main() {     vector<int> v, prim;     vector<bool> isprim;     int lim = sqrt(1.0 * 1000000000) + 10;     isprim.resize(lim);     isprim[0] = isprim[1] = 1;     for (int i = 2; i <= lim; ++i) {       if (isprim[i] == false) {         for (int j = i + i; j <= lim; j = j + i) isprim[j] = true;       }     }     for (int i = 2; i <= lim; ++i) {       if (isprim[i] == false) prim.push_back(i);     }     int sz = prim.size();     set<int> se;     set<int>::iterator it;     int T;     scanf( %d , &T);     while (T--) {       int n = read();       bool flag = false;       se.clear();       for (int i = 2; i * i <= n; ++i) {         if (n % i == 0 && isprim[i] == false) {           se.insert(i);           n /= i;           flag = true;           break;         }       }       if (!flag) {         puts( NO );         continue;       }       flag = false;       for (int i = sqrt(n); i >= 2; i--) {         if (n % i == 0 && se.count(i) == 0 && se.count(n / i) == 0 &&             i != n / i) {           flag = true;           se.insert(i);           se.insert(n / i);           break;         }       }       if (!flag) {         puts( NO );         continue;       }       puts( YES );       for (it = se.begin(); it != se.end(); ++it) {         printf( %d  , *it);       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   int max(int a, int b) { return a < b ? b : a; }   int min(int a, int b) { return a < b ? a : b; }   void swap(int *a, int *b) {     int t = *a;     *a = *b;     *b = t;   }   int can(int x, int y, int a, int b) {     if ((x <= a && y <= b) || (x <= b && y <= a)) return 1;     return 0;   }   int cal(int x1, int y1, int x2, int y2, int a, int b) {     if (x1 > y1) swap(&x1, &y1);     if (x2 > y2) swap(&x2, &y2);     if (can(x1 + x2, max(y1, y2), a, b)) return x1 * y1 + x2 * y2;     if (can(max(x1, x2), y1 + y2, a, b)) return x1 * y1 + x2 * y2;     if (can(max(x2, y1), x1 + y2, a, b)) return x1 * y1 + x2 * y2;     if (can(max(x1, y2), y1 + x2, a, b)) return x1 * y1 + x2 * y2;     return 0;   }   int main() {     int maxx = 0, n, a, b, i, j, s[200][2];     scanf( %d %d %d , &n, &a, &b);     for (i = 0; i < n; i++) scanf( %d %d , &s[i][0], &s[i][1]);     for (i = 0; i < n; i++) {       for (j = i + 1; j < n; j++) {         int k = cal(s[i][0], s[i][1], s[j][0], s[j][1], a, b);         if (maxx < k) maxx = k;       }     }     printf( %d n , maxx);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300000;   int x[N][2];   int ans[N];   bool hear[N];   vector<pair<int, int> > G[N];   int color[N];   void dfs1(int s) {     color[s] = 1;     for (int i = 0; i < (int)G[s].size(); i++) {       int v = G[s][i].first;       if (color[v] == 0) {         if (G[s][i].second > 0) {           ans[G[s][i].second] = 1;         } else {           ans[-G[s][i].second] = 0;         }         dfs1(v);       }     }     color[s] = 2;   }   pair<int, int> cyc;   void dfs2(int s, int per = -1) {     color[s] = 1;     for (int i = 0; i < (int)G[s].size(); i++) {       int v = G[s][i].first;       if (color[v] == 0) {         dfs2(v, abs(G[s][i].second));       }       if ((color[v] == 1) && (per != abs(G[s][i].second))) {         cyc.first = s;         cyc.second = i;       }     }     color[s] = 2;   }   int color1[N];   void dfs3(int s, int per) {     color1[s] = 1;     for (int i = 0; i < (int)G[s].size(); i++) {       int v = G[s][i].first;       if (abs(G[s][i].second) == per) {         continue;       }       if (color1[v] == 0) {         ans[abs(G[s][i].second)] = (G[s][i].second > 0 ? 1 : 0);         dfs3(v, abs(G[s][i].second));       }     }     color1[s] = 2;   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       int k;       scanf( %d , &k);       for (int j = 0; j < k; j++) {         int r;         scanf( %d , &r);         if (x[abs(r)][0] == 0) {           x[abs(r)][0] = i * (r > 0 ? 1 : -1);         } else {           x[abs(r)][1] = i * (r > 0 ? 1 : -1);         }       }     }     for (int i = 1; i <= m; i++) {       if ((x[i][0] >= 0) && (x[i][1] >= 0)) {         ans[i] = 1;         hear[x[i][0]] = 1;         hear[x[i][1]] = 1;       } else if ((x[i][0] <= 0) && (x[i][1] <= 0)) {         ans[i] = 0;         hear[-x[i][0]] = 1;         hear[-x[i][1]] = 1;       } else {         int v1 = abs(x[i][0]);         int v2 = abs(x[i][1]);         G[v1].push_back(make_pair(v2, i * (x[i][1] > 0 ? 1 : -1)));         G[v2].push_back(make_pair(v1, i * (x[i][0] > 0 ? 1 : -1)));       }     }     for (int i = 1; i <= n; i++) {       if ((hear[i] == 1) && (color[i] == 0)) {         dfs1(i);       }     }     for (int i = 1; i <= n; i++) {       if (color[i] == 0) {         cyc.first = 0;         dfs2(i);         if (cyc.first == 0) {           cout <<  NO  << endl;           return 0;         } else {           int s = cyc.first;           int j = cyc.second;           ans[abs(G[s][j].second)] = (G[s][j].second > 0 ? 1 : 0);           dfs3(G[s][j].first, abs(G[s][j].second));         }       }     }     cout <<  YES  << endl;     for (int i = 1; i <= m; i++) {       cout << ans[i];     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int bits = 10;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<pair<string, int> > commands;     for (int i = 0; i < n; ++i) {       string tp;       int x;       cin >> tp >> x;       commands.emplace_back(tp, x);     }     int ansxor = 0;     int ansand = (1 << bits) - 1;     int ansor = 0;     for (int bit = 0; bit < bits; ++bit) {       vector<int> f = {0, 1};       for (int index = 0; index < 2; ++index) {         for (int i = 0; i < n; ++i) {           string tp;           int x;           tie(tp, x) = commands[i];           x = (x >> bit) & 1;           if (tp ==  & ) f[index] &= x;           if (tp ==  | ) f[index] |= x;           if (tp ==  ^ ) f[index] ^= x;         }       }       if (f == vector<int>{0, 0}) {         ansand ^= 1 << bit;       } else if (f == vector<int>{1, 0}) {         ansxor ^= 1 << bit;       } else if (f == vector<int>{1, 1}) {         ansor ^= 1 << bit;       }     }     cout << 3 << endl;     cout <<  ^   << ansxor << endl;     cout <<  &   << ansand << endl;     cout <<  |   << ansor << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<pair<int, int>> times;     for (int i = 0; i < m; i++) {       int s, e;       cin >> s >> e;       times.push_back(make_pair(s, e));     }     sort(times.begin(), times.end(), [](pair<int, int> a, pair<int, int> b) {       return (a.first == b.first) ? (a.second < b.second) : (a.first < b.first);     });     vector<int> ans(n, 0);     for (auto t : times) {       for (int i = t.first + 1; i <= t.second; i++) {         ans[i - 1] = !ans[i - 2];       }     }     for (auto a : ans) cout << a;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> adj[100001];   long long vis[100001] = {0};   long long n;   long long nodecolor[100001];   long long treecolor[100001];   long long cc[100001];   map<long long, long long> colorcnt;   long long dfs(long long root, long long col) {     long long cnt = 1;     vis[root] = 1;     treecolor[root] = nodecolor[root];     for (auto x : adj[root]) {       if (vis[x] == 0 && nodecolor[x] != col) treecolor[root] = -1;     }     long long flag = 0;     for (auto x : adj[root]) {       if (vis[x] == 0) {         flag = 1;         cnt += dfs(x, nodecolor[x]);         if (treecolor[x] != nodecolor[root]) treecolor[root] = -1;       }     }     if (flag == 0) {       treecolor[root] = nodecolor[root];     }     return cc[root] = cnt;   }   long long fun(long long root) {     vis[root] = 1;     long long flag = 0;     set<long long> st;     long long parentcolor = -1;     for (auto x : adj[root]) {       if (vis[x] == 0) {         if (treecolor[x] == -1) flag = 1;       } else {         parentcolor = nodecolor[x];       }     }     if (flag == 0) {       if (parentcolor == -1) return root;       for (auto x : adj[root]) {         if (vis[x] == 0) {           colorcnt[treecolor[x]] -= cc[x];         }       }       colorcnt[nodecolor[root]]--;       if (colorcnt[parentcolor] == n - cc[root]) {         return root;       }       for (auto x : adj[root]) {         if (vis[x] == 0) {           colorcnt[treecolor[x]] += cc[x];         }       }       colorcnt[nodecolor[root]]++;     }     long long ans = -1;     for (auto x : adj[root]) {       if (vis[x] == 0) {         ans = fun(x);         if (ans != -1) return ans;       }     }     return ans;   }   void solve(long long cno) {     long long m, i, j, k, l;     cin >> n;     long long flag = 0;     for (i = 0; i < n - 1; i++) {       cin >> j >> k;       j--;       k--;       adj[j].push_back(k);       adj[k].push_back(j);     }     for (i = 0; i < n; i++) {       cin >> nodecolor[i];       colorcnt[nodecolor[i]]++;     }     long long temp = dfs(0, nodecolor[0]);     for (i = 0; i < n; i++) vis[i] = 0;     long long ans = -1;     ans = fun(0);     if (ans == -1)       cout <<  NO ;     else {       cout <<  YES  <<   n  << ans + 1;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int TESTS = 1;     long long i = 1;     while (TESTS--) {       solve(i);       i++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   long long dp[N], f[N], ok[N], p[N];   vector<long long> node[N];   long long cur, now;   void dfs(long long u, long long p) {     for (auto j : node[u]) {       if (j == p) continue;       dfs(j, u);       if (f[j]) {         dp[u] += dp[j] + 1;         f[u] = 1;       }     }     if (ok[u]) f[u] = 1;   }   void find_dia(long long u, long long p, long long dis) {     if (dis >= cur && ok[u]) {       if (dis > cur)         now = u;       else         now = min(now, u);       cur = dis;     }     for (auto j : node[u]) {       if (j == p) continue;       find_dia(j, u, dis + 1);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     if (fopen( A.INP ,  r )) {       freopen( A.INP ,  r , stdin);       freopen( A.OUT ,  w , stdout);     }     long long n, m;     cin >> n >> m;     for (int i = 1; i < n; i++) {       long long u, v;       cin >> u >> v;       node[u].push_back(v);       node[v].push_back(u);     }     for (int i = 1; i <= m; i++) cin >> p[i], ok[p[i]] = 1;     dfs(p[1], p[1]);     long long ans = dp[p[1]] * 2;     cur = 0;     now = 1e9;     find_dia(p[1], p[1], 0);     cur = 0;     long long f = now;     now = 1e9;     find_dia(f, f, 0);     cout << min(f, now) <<   n ;     cout << ans - cur;   }


#include <bits/stdc++.h>   using namespace std;   long long Set(long long N, int pos) { return N = N | (1LL << pos); }   long long reset(long long N, int pos) { return N = N & ~(1LL << pos); }   bool check(long long N, int pos) { return (bool)(N & (1LL << pos)); }   struct node {     bool endmark;     node *next[2];     node() {       endmark = 0;       for (int i = 0; i < 2; i++) next[i] = NULL;     }   } * root;   void insert(long long m) {     node *cur = root;     for (int i = 63; i >= 0; i--) {       int id = check(m, i);       if (cur->next[id] == NULL) cur->next[id] = new node();       cur = cur->next[id];     }     cur->endmark = true;   }   long long query(long long m) {     node *cur = root;     long long ret = 0;     for (int i = 63; i >= 0; i--) {       int id = check(m, i);       int a = 1 - id;       if (cur->next[a] != NULL) {         ret = Set(ret, i);         cur = cur->next[a];       } else         cur = cur->next[id];     }     return ret;   }   long long ara[100005], lft[100005], rgt[100005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     root = new node();     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> ara[i];     for (int i = 1; i <= n; i++) lft[i] = lft[i - 1] ^ ara[i];     for (int i = n; i >= 1; i--) rgt[i] = rgt[i + 1] ^ ara[i];     long long ans = 0;     insert(0);     for (int i = 1; i <= n; i++) {       long long temp = query(rgt[i]);       ans = max(ans, temp);       insert(lft[i]);     }     ans = max(ans, query(0));     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string to_string(string c) { return string(c); }   string to_string(char ch) { return string(1, ch); }   template <size_t N>   string to_string(bitset<N> B) {     string second =   ;     for (long long i = 0; i < (long long)N; i++) second += (char) 0  + B[i];     return second;   }   template <typename T, typename U>   string to_string(pair<T, U> P) {     return  {  + to_string(P.first) +  ,  + to_string(P.second) +  } ;   }   template <typename T>   string to_string(T A) {     string second =  [ ;     bool first = 0;     for (auto x : A) {       if (first) second +=    ;       first = 1;       second += to_string(x);     }     return second +  ] ;   }   template <typename T, size_t N>   string to_string(T (&A)[N]) {     string second =  [ ;     bool first = 0;     for (long long i = 0; i < N; i++) {       if (first) second +=    ;       first = 1;       second += to_string(A[i]);     }     return second +  ] ;   }   void _deb() {     cerr <<  ]           <<   n ;   }   template <typename T, typename... U>   void _deb(const T& t, const U&... u) {     cerr << to_string(t);     if (sizeof...(u)) cout <<  ,  ;     _deb(u...);   }   long long ceil(long long a, long long b) {     return a / b + (a % b && (a ^ b) > 0);   }   long long floor(long long a, long long b) {     return a / b - (a % b && (a ^ b) < 0);   }   long long SBITS(long long n) { return __builtin_popcountll(n); }   long long BITS(long long n) { return 63 - __builtin_clzll(n); }   const long long MX = 3e5 + 5;   const long long OO = 1e18;   const long long MOD = 998244353;   long long DX[8] = {1, 0, -1, 0, 1, -1, 1, -1};   long long DY[8] = {0, 1, 0, -1, 1, -1, -1, 1};   long long FAC[MX];   long long FACINV[MX];   long long nCr(long long n, long long r) {     if (n < r) return 0;     return FAC[n] * FACINV[r] % MOD * FACINV[n - r] % MOD;   }   long long fastExp(long long base, long long expo, long long mod) {     if (expo == 0) return 1;     if (expo == 1) return base % mod;     long long t = fastExp(base, expo / 2, mod);     t = (t * t) % mod;     if (expo % 2 == 0)       return t;     else       return ((base % mod) * t) % mod;   }   void calcFact() {     FAC[1] = 1;     for (long long i = 2; i < MX; i++) FAC[i] = i * FAC[i - 1] % MOD;     FACINV[MX - 1] = fastExp(FAC[MX - 1], MOD - 2, MOD);     for (long long i = MX - 1; i > 0; i--) FACINV[i - 1] = i * FACINV[i] % MOD;   }   void solve() {     calcFact();     long long n, ans = 0;     cin >> n;     vector<long long> l(n), r(n);     for (long long i = 0; i < n; i++) cin >> l[i];     for (long long i = 0; i < n; i++) cin >> r[i];     sort(l.begin(), l.end());     sort(r.begin(), r.end());     reverse(r.begin(), r.end());     for (long long i = 0; i < n; i++) ans += abs(l[i] - r[i]);     ans %= MOD;     ans *= nCr(2 * n, n);     ans %= MOD;     cout << ans <<   n ;   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   char s[maxn], t[maxn], st[maxn];   long long cnts[maxn], cntt[maxn];   long long Next[maxn];   int n, m;   long long ans;   long long lent, lens;   long long f, e;   void init() {     ans = 0;     lent = lens = 0;     s[0] = t[0] = 0;     char c[2];     long long x;     for (int i = 1; i <= n; i++) {       scanf( %I64d-%s , &x, c);       if (c[0] == s[lens])         cnts[lens] += x;       else {         cnts[++lens] = x;         s[lens] = c[0];       }     }     for (int i = 1; i <= m; i++) {       scanf( %I64d-%s , &x, c);       if (c[0] == t[lent])         cntt[lent] += x;       else {         cntt[++lent] = x;         t[lent] = c[0];       }     }   }   void getnxt() {     Next[0] = Next[1] = 0;     long long j = 0;     for (long long i = 2; i <= lent - 2; i++) {       while (j && (st[j + 1] != st[i] || cntt[i] != cntt[j + 1])) j = Next[j];       if (st[j + 1] == st[i] && cntt[i] == cntt[j + 1]) j++;       Next[i] = j;     }   }   long long kmp() {     long long sum = 0;     long long j = 0;     for (long long i = 2; i < lens; i++) {       while (j && (st[j + 1] != s[i] || cnts[i] != cntt[j + 1])) j = Next[j];       if (st[j + 1] == s[i] && cnts[i] == cntt[j + 1]) j++;       if (j == lent - 2) {         if (s[i - (lent - 2)] == t[1] && s[i + 1] == t[lent] &&             cnts[i - (lent - 2)] >= f && cnts[i + 1] >= e)           sum++;         j = Next[j];       }     }     return sum;   }   int main() {     while (scanf( %d %d , &n, &m) != EOF) {       init();       if (lent == 1) {         for (int i = 1; i <= lens; i++) {           if (s[i] == t[lent] && cnts[i] >= cntt[lent])             ans += cnts[i] - cntt[lent] + 1;         }       } else if (lent == 2) {         for (int i = 1; i < lens; i++) {           if (s[i] == t[1] && s[i + 1] == t[2] && cnts[i] >= cntt[1] &&               cnts[i + 1] >= cntt[2])             ans++;         }       } else {         strcpy(st + 1, t + 2);         st[lent] = 0;         f = cntt[1], e = cntt[lent];         for (int i = 1; i < lent; i++) cntt[i] = cntt[i + 1];         getnxt();         ans = kmp();       }       printf( %I64d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1010;   int n, a[N], b[N];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n / 2; i++) {       b[i * 2 - 1] = a[i];       b[i * 2] = a[n - i + 1];     }     if (n % 2) b[n] = a[n / 2 + 1];     int flag = 1;     for (int i = 3; i <= n; i += 2) {       if (b[i] > b[i - 1]) {         flag = 0;         break;       }     }     for (int i = 2; i <= n && flag; i += 2) {       if (b[i] < b[i - 1]) {         flag = 0;         break;       }     }     if (!flag) {       puts( Impossible );       return 0;     }     for (int i = 1; i <= n; i++) {       printf( %d , b[i]);       putchar(i == n ?   n  :    );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<long long, long long> q[9];   char s[2];   int main() {     long long n, x0, y0;     scanf( %lld , &n);     scanf( %lld%lld , &x0, &y0);     for (long long m = 0; m < 9; m++) q[m] = make_pair(1E10, INT_MAX);     while (n--) {       long long x, y, i, j, k, g, m, d;       scanf( %s , s);       scanf( %lld%lld , &x, &y);       i = x - x0, j = y - y0;       if (abs(i) != abs(j) && i != 0 && j != 0) continue;       k = max(abs(i), abs(j));       i /= k, j /= k;       i++, j++;       m = 3 * i + j;       if (s[0] ==  Q )         k = 3;       else         k = (s[0] !=  R ) + 1;       d = abs(x - x0) + abs(y - y0);       q[m] = min(q[m], make_pair(d, k));     }     for (long long m = 0; m < 9; m++) {       long long i = m / 3, j = m % 3, check;       i--, j--;       if (i == 0 || j == 0)         check = 0;       else         check = 1;       if (q[m].first != 1E10 && (q[m].second & (1 << check))) {         cout <<  YES n ;         return 0;       }     }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     long long n, k;     cin >> n >> k;     vector<long long> a(n);     for (auto& i : a) cin >> i;     while (a.back() <= k) a.pop_back();     reverse(a.begin(), a.end());     while (a.back() <= k) a.pop_back();     cout << n - a.size() <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 30;   signed main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     string s;     cin >> s;     int n = s.size();     int pref = 0, suf = n - 1;     while (pref < n - 1 && s[pref] != s[pref + 1]) pref++;     while (suf > 0 && s[suf] != s[suf - 1]) suf--;     int ans = min(n, (s[n - 1] != s[0] ? pref + 1 + (n - suf) : 0));     for (int i = 0; i < n; i++) {       int j = i;       while (j < n - 1 && s[j] != s[j + 1]) j++;       ans = max(ans, j - i + 1);       i = j;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1000000007;   const long long INF = 1e18;   int mod = 998244353;   template <typename T1, typename T2>   inline bool chmin(T1& a, T2 b) {     if (a > b) {       a = b;       return 1;     }     return 0;   }   template <typename T1, typename T2>   inline bool chmax(T1& a, T2 b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   template <typename T, typename U>   T pow_(T a, U b) {     return b ? pow_(a * a, b / 2) * (b % 2 ? a : 1) : 1;   }   long long modpow(long long a, long long b, long long _mod) {     return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1;   }   template <class T, class U>   ostream& operator<<(ostream& os, const pair<T, U>& p) {     os << p.first <<     << p.second;     return os;   }   template <class T>   ostream& operator<<(ostream& os, const vector<T>& vec) {     for (int i = 0; i < ((int)vec.size()); ++i) {       if (i) os <<    ;       os << vec[i];     }     return os;   }   template <typename T>   inline istream& operator>>(istream& is, vector<T>& v) {     for (int j = 0; j < ((int)v.size()); ++j) is >> v[j];     return is;   }   template <class T, class T2>   inline void add(T& a, T2 b) {     a += b;     if (a >= mod) a -= mod;   }   void solve();   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cout << fixed << setprecision(10);     int T;     T = 1;     while (T--) solve();   }   void solve() {     int n;     long long k;     cin >> n >> k;     vector<int> a(n);     cin >> a;     long long ma = *max_element(a.begin(), a.end());     int ans = 0;     vector<int> b(n);     auto Get = [&](long long limit) {       long long sum = 0;       for (int i = 0; i < (n); ++i) {         auto binary_search = [&](int ok, int ng) {           auto check = [&](int x) {             if (a[i] - 3LL * x * (x + 1) - 1 >= limit) return 1;             return 0;           };           int d = 1;           while ((ng - ok >= 0 ? ng - ok : -(ng - ok)) > d) {             int mid = (ng + ok) / 2;             (check(mid) ? ok : ng) = mid;           }           return ok;         };         b[i] = binary_search(0, a[i] + 1);         sum += b[i];       }       return sum;     };     auto binary_search = [&](long long ok, long long ng) {       auto check = [&](long long x) {         long long s = Get(x);         if (s >= k) return 1;         return 0;       };       int d = 1;       while ((ng - ok >= 0 ? ng - ok : -(ng - ok)) > d) {         long long mid = (ng + ok) / 2;         (check(mid) ? ok : ng) = mid;       }       return ok;     };     long long L = binary_search(-4e18, 4e18);     long long s = Get(L);     for (int i = 0; i < (s - k); ++i) {       long long mi = INF;       int id = -1;       for (int j = 0; j < (n); ++j)         if (chmin(mi, a[j] - 3LL * b[j] * b[j] + 3 * b[j] - 1)) id = j;       b[id]--;     }     cout << b <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int len = 0, coun = 0;   bool visited[1000006];   vector<int> check;   vector<int> grid[1000006];   void dfs(int v) {     visited[v] = true;     for (int i = 0; i < grid[v].size(); i++) {       if (!visited[grid[v][i]]) {         dfs(grid[v][i]);       }     }     check.push_back(v);   }   void toposort(int n) {     memset(visited, false, sizeof(visited));     coun = 0;     for (int i = 0; i < n; i++) {       if (!visited[i + 1]) dfs(i + 1);     }   }   int main() {     int n, m, x, y;     cin >> n >> m;     map<pair<int, int>, int> ma;     for (int i = 0; i < m; i++) {       cin >> x >> y;       ma[{x, y}] = i;       grid[x].push_back(y);     }     toposort(n);     reverse(check.begin(), check.end());     bool ans = true;     int maxi = 0;     for (int i = 0; i < check.size() - 1; i++) {       if (ma.find(make_pair(check[i], check[i + 1])) == ma.end())         ans = false;       else         maxi = max(maxi, ma[make_pair(check[i], check[i + 1])]);     }     if (ans == false)       cout << -1;     else       cout << maxi + 1;   }


#include <bits/stdc++.h>   using namespace std;   int n, x, y;   vector<vector<int> > adjList(100009, vector<int>());   bool vis[100009];   vector<int> ans;   bool check_valid_edges(vector<vector<int> > &a, int n) {     for (size_t i = 1; i <= n; i++)       if (a[i].size() != 4) return false;     return true;   }   int comp(int node1, int node2) {     int c = 0;     for (int i = 0; i < adjList[node1].size(); i++) {       for (int j = 0; j < adjList[node2].size(); j++) {         if (adjList[node1][i] == adjList[node2][j]) c++;       }     }     return c;   }   void dfs(int node) {     vis[node] = true;     bool f = true;     for (int i = 0; i < adjList[node].size() && f; i++) {       if (comp(node, adjList[node][i]) == 2 && !vis[adjList[node][i]])         ans.push_back(adjList[node][i]), dfs(adjList[node][i]), f = false;     }   }   int main() {     cin >> n;     for (int i = 1; i <= (2 * n); i++) {       cin >> x >> y;       adjList[x].push_back(y);       adjList[y].push_back(x);     }     if (!check_valid_edges(adjList, n)) return cout << -1 << endl, 0;     if (n == 5)       return cout <<  1 2 3 4 5 n  << endl, 0;     else if (n == 6) {       int s_node = 21;       for (int i = 0; i < adjList[1].size(); i++) s_node -= adjList[1][i];       s_node--;       cout <<  1   << adjList[1][0] <<     << adjList[1][1] <<     << s_node            <<     << adjList[1][2] <<     << adjList[1][3] << endl;       ;     } else {       dfs(1);       ans.push_back(1);       if (ans.size() == n)         for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;       else         cout << -1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ii = pair<int, int>;   using ll = long long int;   using vi = vector<int>;   using graph = vector<vi>;   const int INF = 0x3f3f3f3f;   const ll INFL = 0x3f3f3f3f3f3f3f3f;   const int MAXN = 200005;   int fq[4];   int main() {     int n, m, k;     string s;     cin >> n;     cin >> s;     for (auto c : s) {       int x = c -  0 ;       fq[x]++;     }     if (n == 1) {       cout << s <<   n ;     } else if (n == 2) {       if (fq[1] == 2) {         cout << 1 <<   n ;       } else {         cout << s <<   n ;       }     } else {       cout << 1;       while (fq[0]--) {         cout << 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long h[100005], d;   int dp[100005];   int arr[300003];   int tree[(1200004)];   int lazy[(1200004)];   void build_tree(int node, int a, int b) {     if (a > b) return;     if (a == b) {       tree[node] = arr[a];       return;     }     build_tree(node * 2, a, (a + b) / 2);     build_tree(node * 2 + 1, 1 + (a + b) / 2, b);     tree[node] = max(tree[node * 2], tree[node * 2 + 1]);   }   void update_tree(int node, int a, int b, int i, int j, int value) {     if (lazy[node] != 0) {       tree[node] += lazy[node];       if (a != b) {         lazy[node * 2] += lazy[node];         lazy[node * 2 + 1] += lazy[node];       }       lazy[node] = 0;     }     if (a > b || a > j || b < i) return;     if (a >= i && b <= j) {       tree[node] += value;       if (a != b) {         lazy[node * 2] += value;         lazy[node * 2 + 1] += value;       }       return;     }     update_tree(node * 2, a, (a + b) / 2, i, j, value);     update_tree(1 + node * 2, 1 + (a + b) / 2, b, i, j, value);     tree[node] = max(tree[node * 2], tree[node * 2 + 1]);   }   int query_tree(int node, int a, int b, int i, int j) {     if (a > b || a > j || b < i) return -0x7fffffff;     if (lazy[node] != 0) {       tree[node] += lazy[node];       if (a != b) {         lazy[node * 2] += lazy[node];         lazy[node * 2 + 1] += lazy[node];       }       lazy[node] = 0;     }     if (a >= i && b <= j) return tree[node];     int q1 = query_tree(node * 2, a, (a + b) / 2, i, j);     int q2 = query_tree(1 + node * 2, 1 + (a + b) / 2, b, i, j);     int res = max(q1, q2);     return res;   }   void reconstruct(vector<int>& ret, int val) {     long long last = -1e18;     for (int i = 0; i < n; i++) {       if (abs(last - h[i]) >= d && dp[i] == val) {         ret.push_back(i + 1);         last = h[i];         val--;       }     }   }   map<long long, int> htoi;   set<long long> ss;   int main() {     cin >> n >> d;     int counter = 0;     for (int i = 0; i < n; i++) {       scanf( %I64d , &h[i]);       ss.insert(h[i]);     }     set<long long>::iterator iit;     for (iit = ss.begin(); iit != ss.end(); iit++) htoi[*iit] = counter++;     ss.clear();     update_tree(1, 0, 300003 - 1, htoi[h[n - 1]], htoi[h[n - 1]], 1);     ss.insert(h[n - 1]);     dp[n - 1] = 1;     for (int ind = n - 2; ind >= 0; ind--) {       int index = htoi[h[ind]];       long long curmin = *ss.begin();       iit = ss.end();       iit--;       long long curmax = *iit;       int ucand = 0;       int dcand = 0;       if (ss.lower_bound(h[ind] + d) != ss.end()) {         long long curhplud = *ss.lower_bound(h[ind] + d);         ucand = max(ucand,                     query_tree(1, 0, 300003 - 1, htoi[curhplud], htoi[curmax]));       }       iit = ss.lower_bound(h[ind] - d);       if (iit == ss.begin()) {         if (*iit <= h[ind] - d) {           dcand = max(dcand,                       query_tree(1, 0, 300003 - 1, htoi[curmin], htoi[curmin]));         }       } else {         if (*iit > h[ind] - d) iit--;         if (iit == ss.end()) iit--;         long long curhmind = *iit;         dcand = max(dcand,                     query_tree(1, 0, 300003 - 1, htoi[curmin], htoi[curhmind]));       }       int curval = query_tree(1, 0, 300003 - 1, index, index);       int newval = max(max(ucand, dcand) + 1, curval);       update_tree(1, 0, 300003 - 1, index, index, newval - curval);       ss.insert(h[ind]);       dp[ind] = query_tree(1, 0, 300003 - 1, index, index);     }     int len = dp[0];     for (int i = 1; i < n; i++) len = max(len, dp[i]);     vector<int> ret;     reconstruct(ret, len);     cout << ret.size() << endl;     for (int i = 0; i < ret.size(); i++) cout << ret[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   const int inf = 1 << 30;   const long long INF = 1ll << 61;   using namespace std;   const int MOD = 1000000;   int n;   int nnum[500000 + 55];   int vis[1000000 + 55];   int flag[1000000 + 55];   vector<int> G;   void init() {     memset(flag, 0, sizeof(flag));     memset(nnum, 0, sizeof(nnum));     memset(vis, 0, sizeof(vis));     G.clear();   }   bool input() {     while (cin >> n) {       for (int i = 0; i < n; i++) {         scanf( %d , &nnum[i]);         vis[nnum[i]]++;       }       return false;     }     return true;   }   void cal() {     int cnt = 0;     for (int i = 0; i < n; i++) {       if (vis[nnum[i]] && !vis[MOD - nnum[i] + 1]) {         flag[MOD - nnum[i] + 1]++;         cnt++;       }     }     for (int i = 1; i <= MOD && cnt < n; i++) {       if (!vis[i] && !vis[MOD - i + 1]) {         flag[i] = 1;         cnt++;         flag[MOD - i + 1] = 1;         cnt++;       }     }     cout << cnt << endl;     for (int i = 1; i <= MOD; i++)       if (flag[i]) printf( %d  , i);     puts(  );   }   void output() {}   int main() {     while (true) {       init();       if (input()) return 0;       cal();       output();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g[2000];   int vis[2000];   vector<pair<int, int> > cyc;   void dfs(int x, int scc) {     if (vis[x] > 0) {       return;     }     vis[x] = 1;     int j;     for (int i = 0; i < g[x].size(); i++) {       for (j = 0; j < g[g[x][i]].size(); j++) {         if (g[g[x][i]][j] == x) {           g[g[x][i]].erase(g[g[x][i]].begin() + j);         }       }       if (vis[g[x][i]] > 0) {         cyc.push_back(make_pair(x, g[x][i]));       }       dfs(g[x][i], scc);     }     return;   }   int main() {     int n, i, j, x, y, t, u;     cin >> n;     for (i = 0; i < n - 1; i++) {       cin >> x >> y;       x--;       y--;       g[x].push_back(y);       g[y].push_back(x);     }     memset(vis, 0, sizeof(vis));     int scc = 0, days = 0;     cyc.clear();     vector<int> store;     store.clear();     for (i = 0; i < n; i++) {       if (vis[i] == 0) {         scc++;         store.push_back(i);         dfs(i, scc);       }     }     if (scc == 1) {       cout <<  0 n ;       return 0;     }     j = 1;     vector<pair<pair<int, int>, pair<int, int> > > v;     v.clear();     for (i = 0; i < cyc.size(); i++) {       v.push_back(make_pair(make_pair(cyc[i].first + 1, cyc[i].second + 1),                             make_pair(store[j - 1] + 1, store[j] + 1)));       j++;     }     cout << v.size() <<   n ;     for (i = 0; i < v.size(); i++) {       cout << v[i].first.first <<     << v[i].first.second <<                << v[i].second.first <<     << v[i].second.second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gi() {     int x = 0, w = 1;     char ch = getchar();     while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar();     if (ch ==  - ) w = 0, ch = getchar();     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + ch -  0 , ch = getchar();     return w ? x : -x;   }   const int N = 5e5 + 5;   struct node {     int h, nxt;   } a[N];   int n, m, nxt[N], head[N], val[N], ft[N];   char s[N];   int dep[N], sz[N], son[N], vis[N], sum[N], ans[N];   void add(int v, int h, int id) {     a[id] = (node){h, ft[v]};     ft[v] = id;   }   void dfs1(int u, int f) {     dep[u] = dep[f] + 1;     sz[u] = 1;     for (int v = head[u]; v; v = nxt[v]) {       dfs1(v, u), sz[u] += sz[v];       if (sz[v] > sz[son[u]]) son[u] = v;     }   }   void update(int u) {     sum[dep[u]] ^= val[u];     for (int v = head[u]; v; v = nxt[v])       if (!vis[v]) update(v);   }   bool cal(int x) {     int res = 0;     while (x) ++res, x ^= x & -x;     return res <= 1;   }   void dfs(int u, int f, int keep) {     for (int v = head[u]; v; v = nxt[v])       if (v != son[u]) dfs(v, u, 0);     if (son[u]) dfs(son[u], u, 1), vis[son[u]] = 1;     update(u);     for (int i = ft[u]; i; i = a[i].nxt) ans[i] = cal(sum[a[i].h]);     vis[son[u]] = 0;     if (!keep) update(u);   }   int main() {     n = gi();     m = gi();     for (int i = 2, f; i <= n; ++i) f = gi(), nxt[i] = head[f], head[f] = i;     scanf( %s , s + 1);     for (int i = 1; i <= n; ++i) val[i] = 1 << s[i] -  a ;     for (int i = 1, v, h; i <= m; ++i) v = gi(), h = gi(), add(v, h, i);     dfs1(1, 0);     dfs(1, 0, 1);     for (int i = 1; i <= m; ++i) puts(ans[i] ?  Yes  :  No );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool spj(set<int>& s) {     vector<int> v{s.begin(), s.end()};     return (v[2] == 3 * v[0] && v[1] == 2 * v[0]);   }   int main() {     int n;     scanf( %d , &n);     vector<int> result;     for (int i = n, factor = 1; i != 0; i /= 2, factor *= 2) {       if (i == 3) {         {           result.push_back(factor);           result.push_back(factor);           result.push_back(3 * factor);           break;         }       }       int k = (i + 1) / 2;       for (int j = 0; j < k; ++j) {         result.emplace_back(factor);       }     }     for (int x : result) {       printf( %d  , x);     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m, s, f, l, r, t;   int stay() {     int next = (s < f) ? s + 1 : s - 1;     return (s >= l && s <= r) || (next >= l && next <= r);   }   int di[] = {0, 1};   int dj[] = {1, 0};   void write(vector<string> &res, int x, int y, int dir, string s) {     for (int i = 0; i < s.size(); i++) {       res[x][y] = s[i];       x += di[dir];       y += dj[dir];     }   }   void print(vector<string> res) {     for (int i = 0; i < res.size(); i++) {       for (int j = 0; j < res[i].size(); ++j) {         cout << res[i][j];       }       cout << endl;     }   }   bool same(string a, string b, int i, int j) { return a[i] == b[j]; }   int main() {     vector<string> arr(6);     vector<bool> vis(6, 0);     for (int i = 0; i < 6; ++i) {       cin >> arr[i];     }     sort(arr.begin(), arr.end());     vector<string> best, res;     do {       if (!same(arr[0], arr[1], 0, 0) ||           !same(arr[4], arr[5], arr[4].size() - 1, arr[5].size() - 1))         continue;       if (arr[3].size() != arr[0].size() + arr[4].size() - 1 ||           arr[2].size() != arr[1].size() + arr[5].size() - 1)         continue;       if (!same(arr[3], arr[2], arr[0].size() - 1, arr[1].size() - 1) ||           !same(arr[2], arr[0], 0, arr[0].size() - 1) ||           !same(arr[2], arr[4], arr[2].size() - 1, 0))         continue;       if (!same(arr[3], arr[1], 0, arr[1].size() - 1) ||           !same(arr[3], arr[5], arr[3].size() - 1, 0))         continue;       int n = arr[2].size(), m = arr[3].size();       string tmp = string(m,  . );       res = vector<string>(n, tmp);       write(res, 0, 0, 0, arr[0]);       write(res, 0, 0, 1, arr[1]);       write(res, 0, arr[0].size() - 1, 1, arr[2]);       write(res, arr[1].size() - 1, 0, 0, arr[3]);       write(res, n - 1, arr[0].size() - 1, 0, arr[4]);       write(res, arr[1].size() - 1, m - 1, 1, arr[5]);       if (!best.size() || best > res) best = res;     } while (next_permutation(arr.begin(), arr.end()));     if (!best.size())       cout <<  Impossible  << endl;     else       print(best);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int dpl[2010], dpr[2010];   int main() {     cin >> s;     memset(dpr, 0, sizeof(dpr));     memset(dpl, 0, sizeof(dpl));     int i, j, k;     for (i = 0; i < s.size(); i++) {       for (j = i, k = i; j >= 0 && k < s.size() && s[k] == s[j]; j--, k++)         dpl[j]++, dpr[k]++;       for (j = i, k = i + 1; j >= 0 && k < s.size() && s[k] == s[j]; j--, k++)         dpl[j]++, dpr[k]++;     }     for (i = 1; i < s.size(); i++) dpr[i] += dpr[i - 1];     long long ans = 0;     for (i = 1; i < s.size(); i++) ans += dpr[i - 1] * dpl[i];     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> ara;   int main() {     int n, k;     cin >> n >> k;     int t = n / 2;     if (n == 1 && k != 0) {       cout << -1;       return 0;     }     if (n == 1 && k == 0) {       cout << 1;       return 0;     }     if (k < t) {       cout << -1;       return 0;     } else {       int t2 = k - t;       t2++;       cout << t2 <<     << t2 * 2;       cout <<    ;       ara[t2] = 1;       ara[t2 * 2] = 1;       int cnt = 1;       for (int i = 1; i <= n - 2; i++) {         if (ara[cnt] == 1) cnt++;         if (ara[cnt] == 1) cnt++;         cout << cnt <<    ;         cnt++;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const double eps = 1e-6;   const int inf = 1e9;   const long long llf = 1e18;   const int mod = 1e9 + 7;   const int maxn = 1e5 + 10;   char s[110];   int f[310];   int main() {     int tc;     cin >> tc;     while (tc--) {       cin >> s + 1;       int n = strlen(s + 1);       sort(s + 1, s + n + 1);       string s1 =   , s2 =   ;       for (int i = 1; i <= n; i++) {         f[s[i]]++;       }       for (int i =  a ; i <=  z ; i++) {         if (i & 1) {           while (f[i]) {             s1 += i;             f[i]--;           }         } else {           while (f[i]) {             s2 += i;             f[i]--;           }         }       }       string t = s1 + s2;       int flag = 1;       for (int i = 1; i < t.size(); i++) {         if (abs(t[i] - t[i - 1]) == 1) {           flag = 0;           break;         }       }       if (flag) {         cout << t << endl;         continue;       }       t = s2 + s1;       flag = 1;       for (int i = 1; i < t.size(); i++) {         if (abs(t[i] - t[i - 1]) == 1) {           flag = 0;           break;         }       }       if (flag) {         cout << t << endl;       } else {         cout <<  No answer  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   int main() {     std::vector<int> count(1 << 19);     auto modify = [&](char *s, int dt) {       int len = strlen(s);       std::reverse(s, s + len);       int val = 0;       for (int i = 0; i < 18; ++i) {         val = val | (i >= len ? 0 : (s[i] -  0  & 1)) << i;       }       count[val] += dt;     };     auto query = [&](char *s) {       int len = strlen(s);       std::reverse(s, s + len);       int val = 0;       for (int i = 0; s[i]; ++i) {         val |= s[i] -  0  << i;       }       return count[val];     };     int t;     scanf( %d , &t);     for (int _ = 0; _ < t; ++_) {       char op[2];       char str[18 + 1];       scanf( %s%s , op, str);       if (op[0] ==  + ) {         modify(str, 1);       } else if (op[0] ==  - ) {         modify(str, -1);       } else {         printf( %d n , query(str));       }     }   }


#include <bits/stdc++.h>   using namespace std;   namespace Solve {   const int MAXN = 200010;   inline int ScanInt(void) {     int r = 0, c, d;     while (!isdigit(c = getchar()) && c !=  - )       ;     if (c !=  - ) r = c -  0 ;     d = c;     while (isdigit(c = getchar())) r = r * 10 + c -  0 ;     return d ==  -  ? -r : r;   }   int a[MAXN], n, b[MAXN], cnt = 0, sum = 0;   int zero = 0, l1 = 0, l2 = 0;   bool c[MAXN];   inline void solve(void) {     n = ScanInt();     for (int i = 1; i <= n; i++) {       a[i] = ScanInt(), sum += a[i];       if (a[i] == 0) zero++;     }     if (!zero) {       puts( -1 );       return;     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n; i++) {       if (a[i] % 3 == 0) b[++cnt] = a[i], c[i] = true;       if (a[i] % 3 == 1) l1++;       if (a[i] % 3 == 2) l2++;     }     if (sum % 3 == 1) {       if (l1 == 0) {         int t = 2;         for (int i = 1; i <= n; i++)           if (a[i] % 3 == 2 && t) c[i] = true, t--;       } else {         int t = 1;         for (int i = 1; i <= n; i++)           if (a[i] % 3 == 1 && t) c[i] = true, t--;       }     }     if (sum % 3 == 2) {       if (l2 == 0) {         int t = 2;         for (int i = 1; i <= n; i++)           if (a[i] % 3 == 1 && t) c[i] = true, t--;       } else {         int t = 1;         for (int i = 1; i <= n; i++)           if (a[i] % 3 == 2 && t) c[i] = true, t--;       }     }     for (int i = 1; i <= n; i++)       if (!c[i]) b[++cnt] = a[i];     sort(b + 1, b + cnt + 1);     sum = 0;     for (int i = 1; i <= cnt; i++) sum += b[i];     if (sum == 0) {       puts( 0 );       return;     }     assert(sum % 3 == 0);     for (int i = cnt; i >= 1; i--) printf( %d , b[i]);     printf(  n );   }   }  // namespace Solve   int main(void) {     Solve::solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rang(chrono::high_resolution_clock::now().time_since_epoch().count());   const long double PI = 3.141592653589793;   const long long INF = 9223372036854775807ll;   const long long mod = 1e9 + 7;   const int N = 1e5 + 5;   long long tree[4 * N + 10];   long long lazy[4 * N + 10];   long long b[N];   void build(int node, int start, int end) {     if (start == end) {       tree[node] = b[start];       return;     }     int mid = (start + end) / 2;     build(2 * node, start, mid);     build(2 * node + 1, mid + 1, end);     tree[node] = tree[2 * node] + tree[2 * node + 1];   }   long long query(int node, int start, int end, int l, int r) {     if (lazy[node] != -INF / 10000) {       tree[node] = lazy[node] * (end - start + 1);       if (start != end) {         lazy[2 * node] = lazy[node];         lazy[2 * node + 1] = lazy[node];       }       lazy[node] = -INF / 10000;     }     if (l > end || r < start) return 0;     if (l <= start && r >= end) {       return tree[node];     }     int mid = (start + end) / 2;     return query(2 * node, start, mid, l, r) +            query(2 * node + 1, mid + 1, end, l, r);   }   void update(int node, int start, int end, int l, int r, long long val) {     if (lazy[node] != (-INF / 10000)) {       tree[node] = lazy[node] * (end - start + 1ll);       if (start != end) {         lazy[2 * node] = lazy[node];         lazy[2 * node + 1] = lazy[node];       }       lazy[node] = (-INF / 10000);     }     if (l > end || r < start) return;     if (l <= start && r >= end) {       tree[node] = (end - start + 1) * val;       if (start != end) {         lazy[2 * node] = val;         lazy[2 * node + 1] = val;       }       return;     }     int mid = (start + end) / 2;     update(2 * node, start, mid, l, r, val);     update(2 * node + 1, mid + 1, end, l, r, val);     tree[node] = tree[2 * node] + tree[2 * node + 1];   }   void solve() {     fill(tree, tree + 4 * N + 4, 0ll);     fill(lazy, lazy + 4 * N + 4, -INF / 10000);     fill(b, b + N, 0);     long long n;     cin >> n;     long long a[n];     long long t[n + 1];     t[0] = 0;     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     for (int i = 1; i < n; ++i) {       long long k;       cin >> k;       t[i] = t[i - 1] + k;     }     for (int i = 0; i < n; ++i) {       b[i] = a[i] - t[i];     }     for (int i = 1; i < n; ++i) {       t[i] += t[i - 1];     }     b[n] = INF / 10000;     build(1, 0, n);     int q;     cin >> q;     while (q--) {       char now;       cin >> now;       if (now ==  s ) {         int l;         int r;         cin >> l >> r;         l--;         r--;         long long pr = query(1, 0, n, l, r);         if (l - 1 < 0) l++;         cout << (pr + t[r] - t[l - 1]) <<   n ;       } else {         int i;         long long x;         cin >> i >> x;         i--;         long long val = query(1, 0, n, i, i) + x;         int lo = i;         int hi = n;         while (hi - lo > 1) {           int mid = (hi + lo) / 2;           if (query(1, 0, n, mid, mid) <= val) {             lo = mid;           } else {             hi = mid;           }         }         update(1, 0, n, i, lo, val);       }     }   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     srand(chrono::high_resolution_clock::now().time_since_epoch().count());     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int x1 = 0, x2 = 0, y1 = 0, y2 = 0, position = 0;     int length = s.size();     for (int i = 0; i < length; i++) {       if (s[i] ==  R ) {         x1++;         position++;       }       if (s[i] ==  L ) {         x1--;         position--;       }       if (s[i] ==  U ) {         y1++;         position++;       }       if (s[i] ==  D ) {         y1--;         position--;       }     }     if (length % 2 == 0) {       printf( %d , (abs(x1) + abs(y1)) / 2);     } else {       cout << -1;     }   }


#include <bits/stdc++.h>   using namespace std;   const long ka[6] = {0, 0, 0, 0, 2, 4};   int main() {     long long n, m;     while (cin >> n >> m) {       if (n > m) {         int tmp = n;         n = m;         m = tmp;       }       long long ret;       if (n == 1)         ret = m / 6 * 6 + ka[m % 6];       else if (n == 2) {         if (m == 2)           ret = 0;         else if (m == 3)           ret = 4;         else if (m == 7)           ret = 12;         else           ret = n * m;       } else {         if (n % 2 && m % 2)           ret = n * m - 1;         else           ret = n * m;       }       cout << ret << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   inline int read() {     int x = 0;     char c = getchar();     bool f = 0;     while (!isdigit(c)) f |= (c ==  - ), c = getchar();     while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     return f ? -x : x;   }   int n, m, t;   int sq[500005], a[500005], pos[500005], cnt[500005], ans[500005], st[500005],       top;   struct node {     int l, r, id;   } q[500005];   int nowx, nowy;   inline bool operator<(node x, node y) {     return (sq[x.l] ^ sq[y.l]) ? x.l < y.l                                : ((sq[x.l] & 1) ? x.r < y.r : x.r > y.r);   }   inline void Add(int x) {     if (cnt[a[x]] == 1) {       st[pos[a[x]]] = st[top];       pos[st[top]] = pos[a[x]];       pos[a[x]] = st[top--] = 0;     }     cnt[a[x]]++;     if (cnt[a[x]] == 1) st[++top] = a[x], pos[a[x]] = top;   }   inline void Delete(int x) {     if (cnt[a[x]] == 1) {       st[pos[a[x]]] = st[top];       pos[st[top]] = pos[a[x]];       pos[a[x]] = st[top--] = 0;     }     cnt[a[x]]--;     if (cnt[a[x]] == 1) st[++top] = a[x], pos[a[x]] = top;   }   int main() {     n = read(), t = sqrt(n);     for (register int i = 1; i <= n; ++i) a[i] = read(), sq[i] = i / t;     m = read();     for (register int i = 1; i <= m; ++i)       q[i].l = read(), q[i].r = read(), q[i].id = i;     sort(q + 1, q + m + 1);     nowx = 1, nowy = 0;     for (register int i = 1; i <= m; ++i) {       while (nowx < q[i].l) Delete(nowx++);       while (nowx > q[i].l) Add(--nowx);       while (nowy > q[i].r) Delete(nowy--);       while (nowy < q[i].r) Add(++nowy);       ans[q[i].id] = st[top];     }     for (register int i = 1; i <= m; ++i) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x1, x2, x3;     cin >> x1 >> x2 >> x3;     int min, max;     int t = (x1 < x2) ? x1 : x2;     min = (t < x3) ? t : x3;     t = (x1 > x2) ? x1 : x2;     max = (t > x3) ? t : x3;     cout << max - min;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   const int p = 998244353;   int n, k, cnt, a[N], d[N], jc[N], ny[N];   long long s = 1, ans;   inline int read() {     int ans = 0;     char c = getchar();     while (c < 48 || c > 57) c = getchar();     while (c >= 48 && c <= 57)       ans = (ans << 3) + (ans << 1) + (c ^ 48), c = getchar();     return ans;   }   inline long long ksm(long long x, int y) {     long long z = 1;     while (y) {       if (y & 1) z = z * x % p;       x = x * x % p, y >>= 1;     }     return z;   }   inline long long C(int x, int y) {     return 1ll * jc[x] * ny[y] % p * ny[x - y] % p;   }   int main() {     n = read(), k = read() % p;     for (register int i = 1; i <= n; ++i) {       int u = read();       if (u != a[cnt])         a[++cnt] = u;       else         s = s * k % p;     }     if (a[1] == a[cnt]) --cnt, s = s * k % p;     if (cnt <= 1) {       putchar(48);       return 0;     }     d[0] = jc[0] = 1, ans = ksm(k, cnt);     for (register int i = 1; i <= cnt; ++i)       d[i] = 1ll * d[i - 1] * (k - 2) % p, jc[i] = 1ll * jc[i - 1] * i % p;     ny[cnt] = ksm(jc[cnt], p - 2);     for (register int i = cnt - 1; i >= 0; --i)       ny[i] = 1ll * ny[i + 1] * (i + 1) % p;     for (register int i = 0; (i << 1) <= cnt; ++i)       ans = (ans - C(cnt, i) * C(cnt - i, i) % p * d[cnt - (i << 1)] % p + p) % p;     ans = ans * ny[2] % p * s % p;     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   int v[100066];   vector<int> prime;   int mu[100066];   void init() {     int N = 100000;     for (int i = 2; i <= N; i++) {       if (!v[i]) {         mu[i] = 1;         v[i] = i;         prime.push_back(i);       }       for (auto p : prime) {         if (p > v[i] || i > N / p) break;         v[i * p] = p;         if (i % p == 0)           mu[i * p] = 0;         else           mu[i * p] = -mu[i];       }     }   }   long long solve(vector<pair<int, int> > vec) {     long long sum = 0, all = 0;     long long all1 = 0;     for (auto x : vec) {       sum = (sum + 1ll * x.first * x.second) % mod;       all = (all + x.second) % mod;       all1 = (all1 + x.second) % (mod - 1);     }     long long pp = powmod(2, all1 - 2 + mod - 1) * ((all - 1 + mod) % mod) % mod;     long long ret = 0;     long long two0 = powmod(2, all1 - 3 + mod - 1);     long long two1 = two0 * 2 % mod;     long long two2 = two1 * 2 % mod;     long long qq = two1 + (all - 2 + mod) * two0;     qq = qq % mod;     for (auto x : vec) {       ret += pp * x.first % mod * x.first % mod * x.second % mod;       ret %= mod;       long long tmp = (sum + mod - x.first) % mod;       tmp = 1ll * x.second * x.first % mod * tmp % mod * qq % mod;       ret = (ret + tmp) % mod;     }     return ret;   }   int n;   vector<pair<int, int> > vec, V[100066];   int main() {     init();     scanf( %d , &n);     int x, y;     for (int i = 1; i <= n; i++) {       scanf( %d%d , &x, &y);       vec.push_back(make_pair(x, y));       for (int j = 1; 1ll * j * j <= x; j++) {         if (x % j) continue;         V[j].push_back(make_pair(x, y));         if (x / j != j) V[x / j].push_back(make_pair(x, y));       }     }     long long ans = solve(vec);     for (int i = 2; i <= 100000; i++) {       long long tmp = (-solve(V[i]) * mu[i] + mod) % mod;       ans = (ans + tmp) % mod;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (!(ch >=  0  && ch <=  9 )) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + (ch -  0 );       ch = getchar();     }     return x * f;   }   struct edge {     int next, to;   } e[900500];   vector<int> v[900500];   int cnt, head[900500], size[900500], col[900500];   bool used[900500], vis[900500];   inline void ins(int u, int v) {     e[++cnt].to = v;     e[cnt].next = head[u];     head[u] = cnt;   }   inline void insert(int u, int v) {     ins(u, v);     ins(v, u);   }   void dfs(int x) {     int j = 1;     vis[x] = 1;     for (int i = 0; i < size[x]; i++) used[col[v[x][i]]] = 1;     for (int i = 0; i < size[x]; i++)       if (!col[v[x][i]]) {         for (; used[j]; j++)           ;         used[col[v[x][i]] = j] = 1;       }     for (int i = 0; i < size[x]; i++) used[col[v[x][i]]] = 0;     for (int i = head[x]; i; i = e[i].next)       if (!vis[e[i].to]) dfs(e[i].to);   }   int main() {     int n = read(), m = read(), ans = 0;     for (int i = 1; i <= n; i++) {       size[i] = read();       for (int j = 1; j <= size[i]; j++) v[i].push_back(read());       ans = max(ans, size[i]);     }     for (int i = 1; i < n; i++) insert(read(), read());     printf( %d n , ans ? ans : 1);     dfs(1);     for (int i = 1; i <= m; i++) printf( %d  , col[i] ? col[i] : 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Z = (int)3e3 + 228;   const int N = (int)1e6 + 228;   const int INF = (int)1e9 + 228;   const int MOD = (int)1e9 + 7;   int n, d;   long long a[N], delta[N], k;   bool can(long long mid) {     fill(delta + 1, delta + 1 + n, 0LL);     for (int i = 1; i <= n; i++) {       delta[max(1, i - d)] += a[i];       delta[i + d + 1] -= a[i];     }     long long cur = 0, res = 0;     for (int i = 1; i <= n; i++) {       cur += delta[i];       if (cur < mid) {         res += mid - cur;         if (res > k) return false;         delta[i + 2 * d + 1] -= mid - cur;         cur = mid;       }     }     return true;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> d >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     long long l = 0, r = 2e18 + 5;     while (l < r) {       long long mid = (l + r + 1) / 2;       if (can(mid))         l = mid;       else         r = mid - 1;     }     cout << l;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool shown[100001];   vector<pair<int, int> > v;   vector<int> cycle;   vector<vector<int> > ans;   int main() {     int n, tmp;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &tmp);       v.push_back(make_pair(tmp, i));     }     sort(v.begin(), v.end());     for (int i = 0; i < n; i++) {       int j = i;       while (!shown[v[j].second]) {         shown[v[j].second] = true;         cycle.push_back(v[j].second);         j = v[j].second;       }       if (cycle.size() != 0) {         sort(cycle.begin(), cycle.end());         ans.push_back(cycle);         cycle.clear();       }     }     printf( %d n , ans.size());     for (int i = 0; i < ans.size(); i++) {       printf( %d , ans[i].size());       for (int j = 0; j < ans[i].size(); j++) printf(  %d , ans[i][j] + 1);       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, x, r, z, sol;   int main() {     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> x;       if (r > 0) {         x = x - k + r;         sol++;       }       z = x / k;       sol += z;       r = x % k;     }     if (r > 0) {       sol++;     }     cout << sol;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1E6;   int n, q, sz = 1;   long long value[6];   long long dp[MAXN];   inline void upData(int v, long long w) {     for (int i = MAXN - 1; i >= v; --i) dp[i] = max(dp[i], dp[i - v] + w);   }   inline void Divide(int x) {     int v = 3 * sz, all = min(MAXN / v, n);     for (int i = 1; i < all; i <<= 1) upData(v * i, 1ll * value[x] * i), all -= i;     upData(v * all, 1ll * value[x] * all);   }   int main(void) {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n, n = 3 * (n - 1);     for (int i = 0; i < 6; ++i) cin >> value[i];     for (int i = 3; i < MAXN; ++i) {       int num = i, base = 0;       while (num) {         if (((num % 10) % 3) == 0) dp[i] += 1ll * value[base] * ((num % 10) / 3);         num /= 10, ++base;       }     }     for (int i = 0; i < 6; ++i) Divide(i), sz *= 10;     cin >> q;     while (q--) cin >> n, cout << dp[n] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000000 + 7;   map<int, vector<int>> mp;   vector<int> cur(1, -1), goal(1, -1), done(1, -1);   vector<int> ans;   int c;   void funct(int node, int level, int even, int odd) {     int r = level % 2;     done[node] = 1;     if (r == 1 && odd == 1)       cur[node] = 1 - cur[node];     else if (r == 0 && even == 1)       cur[node] = 1 - cur[node];     if (cur[node] != goal[node]) {       c++;       ans.push_back(node);       cur[node] = goal[node];       if (level % 2 == 0)         even = 1 - even;       else         odd = 1 - odd;     }     for (int i = 0; i < mp[node].size(); i++) {       if (done[mp[node][i]] == 0) funct(mp[node][i], level + 1, even, odd);     }   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       mp[a].push_back(b);       mp[b].push_back(a);     }     int num;     for (int i = 1; i <= n; i++) {       scanf( %d , &num);       cur.push_back(num);     }     for (int i = 1; i <= n; i++) {       scanf( %d , &num);       goal.push_back(num);       done.push_back(0);     }     c = 0;     int level = 0, even = 0, odd = 0;     funct(1, level, even, odd);     cout << c << endl;     if (c > 0) {       int i = 0;       while (i < ans.size()) {         printf( %d n , ans[i]);         i++;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a[60], b[60];     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     cin >> m;     for (int i = 0; i < m; i++) cin >> b[i];     int BG = -1;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (b[j] % a[i] == 0) {           if (b[j] / a[i] > BG) {             BG = b[j] / a[i];           }         }       }     }     int ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (b[j] % a[i] == 0 && b[j] / a[i] == BG) {           ans++;         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   using ll = long long;   using ld = long double;   ll mod = 998244353;   ll gcd(ll a, ll b) {     if (a == 0 or b == 0) return a + b;     return gcd(b, a % b);   }   ll sum(ll a, ll b) {     a += b;     a %= mod;     return a;   }   ll sub(ll a, ll b) {     a += mod - b;     a %= mod;     return a;   }   inline ll mul(ll a, ll b) {     if (a * b < mod) return a * b;     return a * b % mod;   }   inline ll pew(ll a, ll b) {     ll res = 1;     while (b) {       if (b & 1) res = mul(res, a);       a = mul(a, a);       b /= 2;     }     return res;   }   int parent[600001];   int rnk[600001];   int suk[600001];   void make_set(int v) {     parent[v] = v;     rnk[v] = 0;     suk[v] = 0;   }   int find_set(int v) {     if (v == parent[v]) return v;     return parent[v] = find_set(parent[v]);   }   void union_sets(int a, int b) {     a = find_set(a);     b = find_set(b);     if (a != b and (suk[a] * suk[b] == 0)) {       if (rnk[a] < rnk[b]) swap(a, b);       parent[b] = a;       if (rnk[a] == rnk[b]) ++rnk[a];       suk[a] += suk[b];     }   }   vector<ll> primes(ll cur) {     vector<ll> res;     if (cur <= 1) return res;     for (ll j = 2; j * j <= cur; j++) {       if (cur % j == 0) {         res.push_back(j);         while (cur % j == 0) {           cur /= j;         }       }     }     if (cur != 1) res.push_back(cur);     return res;   }   int main() {     ios_base::sync_with_stdio(false);     ll n;     cin >> n;     srand(35324);     vector<ll> kek(n);     for (int i = 0; i < n; i++) {       cin >> kek[i];     }     vector<ll> lul(n);     for (int i = 0; i < n; i++) {       lul[i] = kek[i];     }     random_shuffle(lul.begin(), lul.end());     ll bst = n;     for (int q = 0; q < n and q < 23; q++) {       ll cur = lul[q] - 1;       vector<ll> pr = primes(cur);       for (int j = 0; j < pr.size(); j++) {         ll p = pr[j];         ll cnt = 0;         for (int i = 0; i < n; i++) {           ll a = kek[i] % p;           if (kek[i] == a) {             cnt += p - a;           } else {             cnt += min(a, p - a);           }         }         bst = min(bst, cnt);       }       cur = lul[q];       pr = primes(cur);       for (int j = 0; j < pr.size(); j++) {         ll p = pr[j];         ll cnt = 0;         for (int i = 0; i < n; i++) {           ll a = kek[i] % p;           if (kek[i] == a) {             cnt += p - a;           } else {             cnt += min(a, p - a);           }         }         bst = min(bst, cnt);       }       cur = lul[q] + 1;       pr = primes(cur);       for (int j = 0; j < pr.size(); j++) {         ll p = pr[j];         ll cnt = 0;         for (int i = 0; i < n; i++) {           ll a = kek[i] % p;           if (kek[i] == a) {             cnt += p - a;           } else {             cnt += min(a, p - a);           }         }         bst = min(bst, cnt);       }     }     cout << bst << endl;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int flag = 1;     int a, b, c, d;     cin >> a >> b >> c >> d;     if ((a + b) % 2 == 0) {     } else       flag = -1;     if (a + d == 0) {       if (flag == 1) {         cout <<  Tidak Tidak Ya Tidak               <<   n ;         return;       } else {         cout <<  Tidak Ya Tidak Tidak               <<   n ;         return;       }     }     if (b + c == 0) {       if (flag == 1) {         cout <<  Tidak Tidak Tidak Ya               <<   n ;         return;       } else {         cout <<  Ya Tidak Tidak Tidak               <<   n ;         return;       }     }     if (flag == 1) {       cout <<  Tidak Tidak Ya Ya             <<   n ;       return;     } else {       cout <<  Ya Ya Tidak Tidak             <<   n ;       return;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     int _ = 1;     cin >> _;     while (_--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     vector<int> a(n);     for (auto &x : a) cin >> x;     sort(a.begin(), a.end());     set<int> s;     int ans = 0, x;     for (int i = 0; i < n; i++) {       x = a[i];       if (s.find(x) != s.end()) {         while (1) {           if (s.find(x) != s.end())             ans++;           else             break;           x++;         }       }       s.insert(x);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   int main() {     char a[1000];     scanf( %s , a);     if (a[0] > 96) a[0] -= 32;     printf( %s , a);   }


#include <bits/stdc++.h>   using namespace std;   const int dx[] = {-1, 1, 0, 0};   const int dy[] = {0, 0, -1, 1};   const int max_n = 1000;   char a[max_n][max_n];   int res[max_n][max_n], val[max_n * max_n];   vector<pair<int, int> > comp[max_n * max_n];   bool used[10];   bool range(int x, int l, int r) { return ((l <= x) && (x <= r)); }   int main() {     int n, m, r = 0;     scanf( %d%d n , &n, &m);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) scanf( %c , &a[i][j]);       scanf(  n );     }     memset(res, -1, sizeof(res));     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j)         if (a[i][j] ==  . )           if ((j < m - 1) && (a[i][j + 1] ==  . )) {             res[i][j] = res[i][j + 1] = r++, a[i][j] = a[i][j + 1] =  ~ ;           } else if ((i < n - 1) && (a[i + 1][j] ==  . ))             res[i][j] = res[i + 1][j] = r++, a[i][j] = a[i + 1][j] =  ~ ;           else {             for (int d = 0; d < 4; ++d)               if (range(i + dx[d], 0, n - 1) && range(j + dy[d], 0, m - 1) &&                   (a[i + dx[d]][j + dy[d]] !=  # )) {                 res[i][j] = res[i + dx[d]][j + dy[d]];                 break;               }             if (res[i][j] != -1) continue;             printf( -1 n );             return 0;           }     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j)         if (res[i][j] != -1) comp[res[i][j]].push_back(make_pair(i, j));     for (int rc = 0; rc < r; ++rc) {       memset(used, 0, sizeof(used));       for (int k = 0; k < (int)comp[rc].size(); ++k)         for (int d = 0; d < 4; ++d) {           int cur = res[comp[rc][k].first + dx[d]][comp[rc][k].second + dy[d]];           if (cur < rc) used[val[cur]] = true;         }       for (int k = 0; k < 10; ++k)         if (!used[k]) {           val[rc] = k;           break;         }     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j)         printf( %c , res[i][j] == -1 ?  #  :  0  + val[res[i][j]]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   int a[10000];   int main() {     cin >> n >> m >> k;     for (int i = 0; i < n; i++) scanf( %d , a + i);     if (n % 2 == 0)       puts( 0 );     else {       long long r = m / ((n + 1) / 2);       long long hi = (1 << 30);       for (int i = 0; i < n; i += 2) hi = min(hi, (long long)a[i]);       cout << min(hi, r * k) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 1e5 + 5;   int main() {     int n, f[MaxN], cnt[MaxN], mm = 0, ans = 0;     scanf( %d , &n);     memset(f, 0, sizeof(f));     memset(cnt, 0, sizeof(cnt));     for (int i = 1; i <= n; i++) {       int a;       scanf( %d , &a);       cnt[f[a]]--;       f[a]++;       cnt[f[a]]++;       mm = max(mm, f[a]);       bool flag = 0;       if (cnt[1] == i)         flag = 1;       else if (cnt[i] == 1)         flag = 1;       else if (cnt[1] == 1 && cnt[mm] * mm == i - 1)         flag = 1;       else if (cnt[mm - 1] * (mm - 1) == i - mm && cnt[mm] == 1)         flag = 1;       if (flag == 1) ans = i;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll OO = 0x3f3f3f3f3f3f3f3fLL;   int di[8] = {0, 0, 1, -1, -1, 1, -1, 1};   int dj[8] = {1, -1, 0, 0, 1, 1, -1, -1};   string ys =  YES , no =  NO ;   const int N = 1e5 + 5, M = 5 * (1 << 10) + 10;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string s;       cin >> s;       bool ok = 0;       for (int i = 0; i < n; i++) {         int cnta = 0, cntb = 0;         for (int j = i; j < n; j++) {           cnta += (s[j] ==  a );           cntb += (s[j] ==  b );           if (cnta == cntb) {             cout << i + 1 <<     << j + 1 <<   n ;             ok = 1;             goto out;           }         }       }     out:;       if (!ok) cout <<  -1 -1  <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   string a;   void input() {     cin >> n;     cin >> a;   }   void solve() {     int ans = 0;     for (int i = 1; i < n; ++i) {       ans += (a[i] == a[i - 1]);     }     cout << (ans + 1) / 2 <<   n ;   }   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       input();       solve();     }     return 0;   }


#include <bits/stdc++.h>   const long long oo = (long long)1e18 + 1;   using namespace std;   int n, q;   int b[200002];   pair<int, int> a[200002];   int T[200002];   vector<pair<int, int> > query[200002];   int k[200002], p[200002];   int res[200002];   void upd(int x) {     while (x < 200002) {       T[x]++;       x = x + (x & (x ^ (x - 1)));     }   }   int get(int x) {     int res = 0;     while (x > 0) {       res += T[x];       x = x - (x & (x ^ (x - 1)));     }     return res;   }   int find_kth(int cnt) {     int l = 1;     int r = n;     int res = n;     while (l <= r) {       int mid = (l + r) >> 1;       int count_value = get(mid);       if (count_value >= cnt) {         res = mid;         r = mid - 1;       } else         l = mid + 1;     }     return res;   }   bool cmp(pair<int, int> x, pair<int, int> y) {     return x.first > y.first || (x.first == y.first && x.second < y.second);   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       b[i] = x;       a[i] = make_pair(x, i);     }     sort(a + 1, a + n + 1, cmp);     cin >> q;     for (int i = 1; i <= q; i++) {       cin >> k[i] >> p[i];       query[k[i]].push_back(make_pair(p[i], i));     }     for (int i = 1; i <= n; i++) {       upd(a[i].second);       for (int j = 0; j < query[i].size(); j++) {         pair<int, int> cur_query = query[i][j];         int pos = cur_query.first;         int rpos = cur_query.second;         res[rpos] = b[find_kth(pos)];       }     }     for (int i = 1; i <= q; i++) cout << res[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   inline int read() {     char c = getchar();     int tot = 1;     while ((c <  0  || c >  9 ) && c !=  - ) c = getchar();     if (c ==  - ) {       tot = -1;       c = getchar();     }     int sum = 0;     while (c >=  0  && c <=  9 ) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     return sum * tot;   }   inline void wr(int x) {     if (x < 0) {       putchar( - );       wr(-x);       return;     }     if (x >= 10) wr(x / 10);     putchar(x % 10 +  0 );   }   inline void wrn(int x) {     wr(x);     putchar(  n );   }   inline void wri(int x) {     wr(x);     putchar(   );   }   inline void wrn(int x, int y) {     wri(x);     wrn(y);   }   inline void wrn(int a, int b, int c) {     wri(a);     wrn(b, c);   }   const int N = 205;   int n, m, now, cnt, l[N], r[N], d[N];   double ans[N], f[N][N], len, ll, rr, p1, p2, p3;   void calc(int L, int R) {     cnt = 1;     for (int i = 0; i <= n; i++)       for (int j = 0; j <= n; j++) f[i][j] = 0;     f[0][0] = 1;     for (int i = 1; i <= n; i++)       if (i != now) {         if (r[i] <= L) {           cnt++;           continue;         }         if (l[i] >= R) continue;         len = r[i] - l[i];         p1 = (L - l[i]) / len;         p2 = (R - L) / len;         p3 = (r[i] - R) / len;         for (int j = i; j >= 0; j--)           for (int k = i - j; k >= 0; k--) {             f[j + 1][k] += f[j][k] * p1;             f[j][k + 1] += f[j][k] * p2;             f[j][k] *= p3;           }       }     for (int i = 0; i + cnt <= n; i++) {       for (int j = n - i - cnt; j >= 0; j--)         ans[i + cnt] += f[i][j] * (R - L) / (j + 1),             ans[i + j + 1 + cnt] -= f[i][j] * (R - L) / (j + 1);     }   }   signed main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &l[i], &r[i]);       d[++m] = l[i];       d[++m] = r[i];     }     sort(d + 1, d + m + 1);     int k = 1;     for (int i = 2; i <= m; i++)       if (d[i] != d[i - 1]) {         k++;         if (k != i) d[k] = d[i];       }     m = k;     for (int i = 1; i <= n; i++) {       now = i;       for (int j = 1; j <= n; j++) ans[j] = 0;       for (int j = 1; j < m; j++)         if (l[i] <= d[j] && r[i] >= d[j + 1]) calc(d[j], d[j + 1]);       for (int j = 1; j <= n; j++) ans[j] += ans[j - 1];       for (int j = 1; j <= n; j++) printf( %.6lf  , ans[j] / (r[i] - l[i]));       puts(  );     }   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   const int MAXN = 16;   int n;   int mod[MAXN], rem[MAXN];   int egcd(int a, int b, int &x, int &y) {     if (b == 0) {       x = 1, y = 0;       return a;     }     int g = egcd(b, a % b, x, y);     swap(x, y);     y -= x * (a / b);     return g;   }   int mymod(int x, int mod) {     x %= mod;     if (x < 0) x += mod;     return x;   }   bool combine(int &n1, int &a1, int n2, int a2) {     int m1, m2, g = egcd(n1, n2, m1, m2);     if (a1 % g != a2 % g) return false;     int n3 = n1 / g * n2, a3 = mymod(a1 + m1 * (n1 / g) * (a2 - a1), n3);     n1 = n3, a1 = a3;     return true;   }   void run() {     scanf( %d , &n);     for (int i = (0); i < (n); ++i) scanf( %d , &mod[i]);     for (int i = (0); i < (n); ++i) scanf( %d , &rem[i]);     long long den = 20922789888000LL, num = 0;     for (int i = (0); i < (1 << n); ++i) {       int cmod = 1, crem = 0, cnt = 0;       bool ok = true;       for (int j = (0); j < (n); ++j)         if (i & (1 << j)) {           ++cnt;           if (!combine(cmod, crem, mod[j], rem[j])) ok = false;         }       if (!ok) continue;       if (cnt % 2 == 0)         num += den / cmod;       else         num -= den / cmod;     }     printf( %.9lf n , 1.0 * (den - num) / den);   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>   template <typename T>   inline bool chkmin(T &a, const T &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T>   inline bool chkmax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   inline int read() {     static char ch;     bool sgn = false;     while (ch = getchar(), ch <  0  || ch >  9 )       if (ch ==  - ) sgn = true;     int res = ch - 48;     while (ch = getchar(), ch >=  0  && ch <=  9 ) res = res * 10 + ch - 48;     return sgn ? -res : res;   }   const int N = 2e5 + 5, M = 1e6 + 1;   const int mod = 1e9 + 7;   int n, m, a[N], l[N], r[N], p[N];   int f[N], ans[N];   int tot, pri[N], min[M], first[M];   bool isp[M];   inline bool cmp(const int &x, const int &y) { return r[x] < r[y]; }   inline int fpm(long long x, int Exp) {     long long res = 1;     for (; Exp > 0; x = x * x % mod, Exp >>= 1)       if (Exp & 1) res = res * x % mod;     return res;   }   inline void prime_init() {     isp[0] = isp[1] = true;     for (int i = 2; i < M; ++i) {       if (!isp[i]) pri[++tot] = i, min[i] = i;       for (int j = 1; j <= tot; ++j) {         if (i * pri[j] >= M) break;         isp[i * pri[j]] = true;         min[i * pri[j]] = pri[j];         if (i % pri[j] == 0) break;       }     }   }   inline void insert(int x, const int &y) {     for (; x <= n; x += x & -x) f[x] = (long long)f[x] * y % mod;   }   inline int Query(int x) {     int res = 1;     for (; x > 0; x -= x & -x) res = (long long)res * f[x] % mod;     return res;   }   inline void add(const int &x) {     int num = a[x], tmp;     while (num > 1) {       tmp = min[num];       if (first[tmp] > 0) {         insert(first[tmp], fpm(tmp - 1, mod - 2));         insert(first[tmp], tmp);       }       first[tmp] = x;       insert(x, tmp - 1);       num /= tmp;     }   }   int main() {     n = read();     for (int i = (1); i <= (n); ++i) a[i] = read(), f[i] = 1;     m = read();     for (int i = (1); i <= (m); ++i) {       l[i] = read();       r[i] = read();       p[i] = i;     }     std::sort(p + 1, p + m + 1, cmp);     prime_init();     int now = 0;     for (int i = (1); i <= (m); ++i) {       while (now < r[p[i]]) add(++now);       ans[p[i]] =           (long long)Query(r[p[i]]) * fpm(Query(l[p[i]] - 1), mod - 2) % mod;     }     for (int i = (1); i <= (m); ++i) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long st;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, m, k;     cin >> n >> m >> k;     if (k < n) {       cout << (long long)(k + 1) <<     << 1;       return 0;     }     k -= (long long)(n - 1);     long long row = k / (long long)(m - 1), col;     if ((long long)row * (long long)(m - 1) != (long long)k) row++;     if ((long long)(row % (long long)2) == (long long)1) {       st = (long long)(row - (long long)1) * (long long)(m - (long long)1);       st++;       col = k - st + (long long)2;     } else {       st = (long long)row * (long long)(m - 1);       col = (long long)2 + st - k;     }     cout << (long long)(n - row + 1) <<     << col;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, a[100000], b, c = 0, x, y, z, m, i, j, t;     string s;     vector<int> v;     cin >> n;     for (i = 0; i < n; i++) {       cin >> x;       v.push_back(x);     }     sort(v.begin(), v.end());     for (i = 0; i < n / 2; i++) {       c += (v[i] + v[n - i - 1]) * (v[i] + v[n - i - 1]);     }     cout << c << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 3e5 + 2;   const long long N1 = 1e2 + 2;   const long long mod = 1e9 + 7;   const int MASK = 1 << 17 + 1;   const int who = 6 * N * log2(N);   long long power(long long a, long long b, long long c) {     long long ret = 1;     while (b) {       if (b % 2 == 1) {         ret = (ret * a) % c;       }       b /= 2;       a = (a * a) % c;     }     return ret;   }   pair<int, int> a[N + 2];   map<pair<int, int>, long long> ma, ma1;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       int len = s.size();       int plus = 0, minus = 0;       for (int j = 0; j < len; j++) {         if (s[j] ==  ( ) {           plus++;         } else if (s[j] ==  ) ) {           if (plus == 0) {             minus++;           } else {             plus--;           }         }       }       a[i] = make_pair(plus, minus);       if (min(plus, minus) == 0) {         ma[a[i]]++;       }     }     long long tm1 = ma[make_pair(0, 0)];     long long ans = tm1 * tm1;     ma1[make_pair(0, 0)] = 1;     for (int i = 0; i < n; i++) {       int plus = a[i].first, minus = a[i].second;       if (min(plus, minus) > 0 || ma1[a[i]] > 0) {         continue;       }       ma1[a[i]]++;       ma1[make_pair(minus, plus)]++;       ans += ma[a[i]] * ma[make_pair(minus, plus)];     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:36777216 )   using namespace std;   template <typename T1, typename T2>   inline void chkmin(T1 &a, T2 b) {     if (a > b) a = b;   }   template <typename T1, typename T2>   inline void chkmax(T1 &a, T2 b) {     if (a < b) a = b;   }   const string FILENAME =  input ;   const int MAXN = 100228;   int n, m, d;   vector<int> g[MAXN];   bool who[MAXN * 50];   int res = 0;   vector<int> rg[MAXN];   vector<bool> used[102];   vector<int> ord;   int used1[MAXN * 50];   int st[MAXN * 50];   int id[MAXN * 50];   int nxt[MAXN * 50];   int lst[MAXN * 50];   int uks = 0;   int kek[MAXN * 50];   void add(int a, int b) {     if (st[a] == 0) {       uks++;       st[a] = uks;       id[uks] = b;       lst[a] = uks;       nxt[uks] = -1;     } else {       uks++;       nxt[lst[a]] = uks;       nxt[uks] = -1;       lst[a] = uks;       id[uks] = b;     }   }   void dfs1(int u, int t) {     used1[u * d + t] = true;     for (auto h : g[u]) {       if (!used1[h * d + (t + 1) % d]) {         dfs1(h, (t + 1) % d);       }     }     ord.push_back(u * d + t);   }   int used2[MAXN];   void dfs2(int u, int t, int c) {     used1[u * d + t] = c;     if (who[u * d + t]) {       if (used2[u] != c) {         kek[c]++;         used2[u] = c;       }     }     for (auto h : rg[u]) {       if (!used1[h * d + (t - 1 + d) % d]) {         dfs2(h, (t - 1 + d) % d, c);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> d;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       u--, v--;       g[u].push_back(v);       rg[v].push_back(u);     }     for (int i = 0; i < n; i++) {       string s;       cin >> s;       for (int j = 0; j < d; j++) {         if (s[j] ==  1 ) {           who[i * d + j] = true;         }       }     }     for (int i = 0; i < n * d; i++) {       if (!used1[i]) {         dfs1(i / d, i % d);       }     }     memset(used1, 0, sizeof(used1));     int cur = 1;     reverse((ord).begin(), (ord).end());     for (auto x : ord) {       if (!used1[x]) {         dfs2(x / d, x % d, cur);         cur++;       }     }     for (int i = 0; i < n * d; i++) {       for (auto x : g[i / d]) {         int f = (i % d + 1) % d;         if (used1[i] != used1[x * d + f]) {           add(used1[i], used1[x * d + f]);         }       }     }     int ff = used1[0];     for (int x = cur - 1; x >= 1; x--) {       int dp = 0;       int cur = st[x];       if (cur) {         while (cur != -1) {           chkmax(dp, kek[id[cur]]);           cur = nxt[cur];         }       }       kek[x] += dp;     }     cout << kek[ff] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   bool f[100009];   pair<int, int> a[100009];   vector<int> v[100009];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i].first);       a[i].second = i;     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n; i++)       if (!f[i]) {         int x = i;         while (!f[x]) {           f[x] = 1;           v[m].push_back(x);           x = a[x].second;         }         m++;       }     printf( %d n , m);     for (int i = 0; i < m; i++) {       printf( %d  , v[i].size());       for (int j = 0; j < v[i].size(); j++) printf( %d  , v[i][j]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void arr(string &s);   int main() {     int n = 0, k = 0, m = 0;     bool b = 0;     cin >> n >> k;     string s =   ;     cin >> s;     arr(s);     for (int i = 0; i < s.length(); i++) switch (s[i]) {         case  G :           m = i;           i = s.length();           break;       }     for (int i = m; i < s.length(); i += k) switch (s[i]) {         case  T :           b = 1;           i = s.length();           break;         case  # :           i = s.length();           break;       }     switch (b) {       case 1:         cout <<  YES n ;         break;       default:         cout <<  NO n ;     }   }   void arr(string &s) {     int a = 0, b = 0;     string v = s;     for (int i = 0; i < s.length(); i++) {       switch (s[i]) {         case  G :           a = i;           break;         case  T :           b = i;           break;       }     }     if (a > b) {       for (int i = 1; i <= s.length(); i++) v[i - 1] = s[s.length() - i];       s = v;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 7;   int a[N], b[N], na[N], nb[N], t1[N], t2[N], c[N], tp, q[N], mx[N], x, n, m, i,       j;   long long ans;   void init(int* a, int* ans, int n) {     for (memset(ans, 0x3f, (n + 3) << 2), tp = 0, i = 1; i <= n; ++i) {       j = a[i];       while (tp && a[i] < a[q[tp]]) j = max(j, mx[tp--]);       if (tp) ans[i] = min(ans[i], j);       q[++tp] = i;       mx[tp] = j;     }     for (tp = 0, i = n; i; --i) {       j = a[i];       while (tp && a[i] <= a[q[tp]]) j = max(j, mx[tp--]);       if (tp) ans[i] = min(ans[i], j);       q[++tp] = i;       mx[tp] = j;     }   }   void A(int x, int C) {     while (x < N) c[x] += C, x += x & -x;   }   int Q(int x, int C = 0) {     while (x > 0) C += c[x], x -= x & -x;     return C;   }   int main() {     for (scanf( %d%d%d , &n, &m, &x), i = 1; i <= n; ++i)       scanf( %d , a + i), t1[i] = i;     for (i = 1; i <= m; ++i) scanf( %d , b + i), t2[i] = i;     init(a, na, n);     init(b, nb, m);     sort(t1 + 1, t1 + n + 1, [&](int x, int y) { return a[x] < a[y]; });     sort(t2 + 1, t2 + m + 1, [&](int x, int y) { return nb[x] < nb[y]; });     for (i = 1, j = m; i <= n; ++i) {       while (j && a[t1[i]] + nb[t2[j]] > x) A(b[t2[j--]], 1);       ans += Q(x - a[t1[i]]) - Q(x - na[t1[i]]);     }     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, c = getchar(), f = 0;     for (; c >  9  || c <  0 ; f = c ==  - , c = getchar())       ;     for (; c >=  0  && c <=  9 ; x = (x << 1) + (x << 3) + c -  0 , c = getchar())       ;     return f ? -x : x;   }   inline void write(int x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10) write(x / 10);     putchar(x % 10 + 48);   }   const int N = 100 + 10, oo = 1e9;   int n, m, q, a, b, f[N], ans[N], dis[N][N], st[N], en[N], vis[N];   bool must[N][N];   int dfs(int x, int y, int Time) {     if (x == y) return ans[x];     if (vis[x] == Time) return f[x];     vis[x] = Time;     f[x] = 0;     for (int i = (int)1, _y = n; i <= _y; i++)       if (dis[x][i] == 1 && dis[i][y] + 1 == dis[x][y])         f[x] = max(f[x], dfs(i, y, Time));     return f[x] = min(f[x], ans[x]);   }   int main() {     n = read(), m = read(), a = read(), b = read();     for (int i = (int)1, _y = n; i <= _y; i++)       for (int j = (int)i + 1, _y = n; j <= _y; j++) dis[i][j] = dis[j][i] = oo;     for (int i = (int)1, _y = m; i <= _y; i++) {       int u = read(), v = read();       dis[u][v] = 1;     }     for (int k = (int)1, _y = n; k <= _y; k++)       for (int i = (int)1, _y = n; i <= _y; i++)         if (i != k)           for (int j = (int)1, _y = n; j <= _y; j++)             if (i != j && j != k && dis[i][j] > dis[i][k] + dis[k][j])               dis[i][j] = dis[i][k] + dis[k][j];     q = read();     for (int i = (int)1, _y = q; i <= _y; i++) {       st[i] = read(), en[i] = read();       if (dis[st[i]][en[i]] == oo) continue;       memset(vis, 0, sizeof vis);       for (int j = (int)1, _y = n; j <= _y; j++)         if (dis[st[i]][j] + dis[j][en[i]] == dis[st[i]][en[i]])           vis[dis[st[i]][j]]++;       for (int j = (int)1, _y = n; j <= _y; j++)         if (dis[st[i]][j] + dis[j][en[i]] == dis[st[i]][en[i]] &&             vis[dis[st[i]][j]] == 1)           must[i][j] = 1;     }     for (int i = (int)1, _y = n; i <= _y; i++) ans[i] = oo, vis[i] = 0;     ans[b] = 0;     for (int Time = (int)1, _y = n; Time <= _y; Time++) {       memset(vis, 0, sizeof vis);       for (int i = (int)1, _y = q; i <= _y; i++)         if (dis[st[i]][en[i]] != oo)           for (int j = (int)1, _y = n; j <= _y; j++)             if (must[i][j]) ans[j] = min(ans[j], dfs(j, en[i], i) + 1);     }     printf( %d n , ans[a] == oo ? -1 : ans[a]);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int TESTS = 1;     while (TESTS--) {       long long m = 1, ans = 1;       long long n;       cin >> n;       while (ans < n) {         m++;         ans += 2;       }       cout << m <<   n ;       long long x = 1, y = 1;       cout << 1 <<     << 1 <<   n ;       x++;       for (long long i = 1; i < n; i++) {         cout << x <<     << y <<   n ;         if (i == n - 1) break;         i++;         cout << x <<     << y + 1 <<   n ;         x++;         y++;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const long long LLINF = 9223372036854775807LL;   const int ST = 100010;   const int ST1 = 1000010;   const long long MOD = 1000000007;   long long ABS(long long a) {     if (a < 0)       return a * (-1);     else       return a;   }   int mas[30];   bool ans = true;   void bad() {     cout <<  NO ;     exit(0);   }   vector<vector<int> > ppp;   int sz = 0;   void per(vector<int> &t) {     if ((int)((t).size()) == sz) {       ppp.push_back(t);     } else {       t.push_back(0);       per(t);       t.pop_back();       t.push_back(1);       per(t);       t.pop_back();     }   }   void per(vector<int> val, int ed, vector<int> zap) {     if ((int)((val).size()) == 0) {       if (ed == 0) {         cout <<  YES ;         exit(0);       } else         return;     }     vector<vector<int> > pp;     vector<int> em;     sz = (int)((zap).size());     ppp.clear();     per(em);     pp = ppp;     int tek = val[(int)((val).size()) - 1] - 1;     val.pop_back();     int ost = 0;     for (int i = 0; i < (int)((pp).size()); i++) {       vector<int> next;       next.push_back(tek + 1);       ost = 0;       int kk = 0;       for (int j = 0; j < (int)((pp[i]).size()); j++) {         if (pp[i][j] == 1) {           ost += zap[j];           kk++;         } else           next.push_back(zap[j]);       }       if (ost <= tek) {         int s = tek - ost;         if (s > ed) continue;         kk += s;         if (kk == 1) continue;         per(val, ed - s, next);       }     }   }   int main() {     int n;     cin >> n;     if (n == 1) {       cout <<  YES ;       return 0;     }     int kk = 0;     for (int i = 0; i < n; i++) {       cin >> mas[i];       if (mas[i] == 2) bad();       if (mas[i] == 1) kk++;     }     sort(mas, mas + n);     int mn = n / 2;     if (kk < mn) bad();     vector<int> t;     int nol = 0;     for (int i = 0; i < n; i++)       if (mas[i] != 1)         t.push_back(mas[i]);       else         nol++;     vector<int> em;     reverse(t.begin(), t.end());     per(t, nol, em);     cout <<  NO ;     return 0;   }