#include <bits/stdc++.h>   using namespace std;   const int N = 2000 + 1, inf = 2e8 + 1;   int a, m, n;   bool d[N];   int p[N];   int dp[N];   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     cin >> a >> n >> m;     for (int i = 0; i < n; ++i) {       int l, r;       cin >> l >> r;       for (int i = l; i < r; ++i) d[i] = true;     }     for (int i = 0; i <= a; ++i) p[i] = inf;     for (int i = 0; i < m; ++i) {       int x, px;       cin >> x >> px;       p[x] = min(p[x], px);     }     for (int i = a - 1; i >= 0; --i) {       dp[i] = inf;       if (!d[i]) dp[i] = dp[i + 1];       if (p[i] != inf)         for (int j = i + 1; j <= a; ++j)           if (dp[j] != inf) dp[i] = min(dp[i], p[i] * (j - i) + dp[j]);     }     cout << (dp[0] == inf ? -1 : dp[0]) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   using pii = pair<int, int>;   using ppi = pair<pii, int>;   char s[200100];   int rk[200100], sa[200100], ht[200100];   int mn[200100][22];   namespace SA {   int head[200100 * 2];   ppi pos[200100 * 2], buf[200100 << 1], tbuf[200100 << 1];   int nxt[200100 * 2];   void bucket_sort(int ok, int n) {     int i, u, j, deg, E, K;     for (i = E = K = 0; i < max(222, n + 10); i++) head[i] = -1;     for (i = 0; i < n; i++) {       if (ok)         u = pos[i].first.first + 1;       else         u = pos[i].first.second + 1;       buf[E] = pos[i];       nxt[E] = head[u];       head[u] = E++;     }     for (int i = 0; i < max(n + 10, 222); i++) {       for (deg = 0, j = head[i]; ~j; j = nxt[j]) {         tbuf[deg++] = buf[j];       }       while (deg) pos[K++] = tbuf[--deg];     }   }   void LCP(int n) {     for (int i = 1; i <= n; i++) mn[i][0] = ht[i];     for (int j = 1; j < 22; j++) {       for (int i = 1; i <= n; i++) {         mn[i][j] = min(mn[i][j - 1], mn[min(n, i + (1 << j - 1))][j - 1]);       }     }   }   void height(int n) {     int h = 0, i;     for (i = 0; i < n; i++) sa[rk[i]] = i;     for (i = 0; i < n; i++) {       if (rk[i] == 1) continue;       h = max(0, h - 1);       for (int j = sa[rk[i] - 1]; s[i + h] == s[j + h]; h++)         ;       ht[rk[i]] = h;     }   }   void buildSA(int n) {     int i, j, cnt, u;     for (i = 0; i < n; i++) rk[i] = s[i];     rk[n] = 0;     for (i = 0; i < 20; i++) {       for (j = 0; j < n; j++)         pos[j] = ppi(pii(rk[j], rk[min(n, j + (1 << i))]), j);       bucket_sort(0, n);       bucket_sort(1, n);       pos[n] = ppi(pii(-1, -1), -1);       for (cnt = j = 0; j < n; j++) {         rk[pos[j].second] = cnt + 1;         cnt += (pos[j].first != pos[j + 1].first);       }       if (cnt == n) break;     }     height(n);     LCP(n);   }   int calc(int u, int v) {     if (u == v) return 100000000;     u = rk[u], v = rk[v];     if (u > v) swap(u, v);     u++;     int l = 31 - __builtin_clz(v - u + 1);     return min(mn[u][l], mn[v - (1 << l) + 1][l]);   }   }  // namespace SA   using namespace SA;   using INT = long long;   char vst[200100];   int cnt[200100];   int main() {     int n;     cin >> n;     cin >> s;     cin >> vst;     reverse(s, s + n);     buildSA(n);     INT ans = 0;     for (int i = 1; i <= n; i++) {       int u = sa[i];       cnt[i] = cnt[i - 1];       u = n - sa[i] - 1;       if (vst[u] ==  0 ) {         ans = max(ans, (INT)n - sa[i]);         cnt[i]++;       }     }     for (int i = 2; i <= n; i++) {       int st = 0, ed = i;       while (st + 1 < ed) {         int md = (st + ed) >> 1;         if (calc(sa[md], sa[i]) >= ht[i])           ed = md;         else           st = md;       }       int l = ed;       st = i, ed = n + 1;       while (st + 1 < ed) {         int md = (st + ed) >> 1;         if (calc(sa[md], sa[i]) >= ht[i])           st = md;         else           ed = md;       }       int r = st;       ans = max(ans, (INT)ht[i] * (cnt[r] - cnt[l - 1]));     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int BSZ1 = 1010;   const int BSZ2 = 200;   int N;   char A[202020];   int B[40404040];   int C[202020];   long long ans;   int R[202020];   void calc(int i, int r, int zero) {     int t = (zero + R[r]) / i;     int u = zero / i;     if (zero % i != 0) u++;     if (t > BSZ2) {       u = max(u, BSZ2 + 1);       ans += (t - u + 1);     }   }   int main() {     scanf( %s , A + 1);     N = strlen(A + 1);     for (int i = N - 1; i >= 1; i--) {       if (A[i + 1] ==  0 )         R[i] = R[i + 1] + 1;       else         R[i] = 0;     }     for (int i = 1; i <= BSZ1; i++) {       int l = 1, r = 1;       int zero = 0, cnt = 0;       while (r <= N && cnt < i) {         if (A[r] ==  1 )           cnt++;         else           zero++;         r++;       }       if (cnt < i) continue;       r--;       while (1) {         while (A[l] ==  0 ) {           calc(i, r, zero);           l++;           zero--;         }         calc(i, r, zero);         l++;         r++;         while (r <= N) {           if (A[r] ==  1 )             break;           else             zero++;           r++;         }         if (r > N) break;       }     }     for (int i = 0; i <= BSZ2; i++) {       B[N]++;       C[0] = N;       int c = N;       for (int j = 1; j <= N; j++) {         if (A[j] ==  1 )           c += i;         else           c--;         ans += B[c];         B[c]++;         C[j] = c;       }       for (int j = 0; j <= N; j++) B[C[j]] = 0;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string num1, num2, num3;     int i, j, k, l, m, n, last, flag = 0;     cin >> num1;     num2 = num1;     num3 = num1;     l = num1.length();     last = num1.at(l - 1) - 48;     for (i = 0; i < l - 1; i++) {       m = num2.at(i) - 48;       if (m % 2 == 0 && m < last) {         swap(num2.at(i), num2.at(l - 1));         flag = 1;         cout << num2 << endl;         return 0;       }     }     if (flag == 0) {       for (i = l - 2; i >= 0; i--) {         m = num3.at(i);         if (m % 2 == 0 && last < m) {           swap(num3.at(i), num3.at(l - 1));           cout << num3 << endl;           return 0;         }       }     }     cout <<  -1  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const double ep = 1e-6;   const double eps = 4e-5;   const double pi = acos(-1);   int tmpx, tmpy;   struct pnt {     double x, y;     pnt(double x = 0, double y = 0) : x(x), y(y) {}     pnt operator+(const pnt &b) const { return pnt(x + b.x, y + b.y); }     pnt operator-(const pnt &b) const { return pnt(x - b.x, y - b.y); }     pnt operator*(const double &b) const { return pnt(x * b, y * b); }     pnt operator/(const double &b) const { return pnt(x / b, y / b); }     double operator&(const pnt &b) const { return x * b.x + y * b.y; }     double operator^(const pnt &b) const { return x * b.y - y * b.x; }     double dist() { return sqrt(x * x + y * y); }     void input() { scanf( %d %d , &tmpx, &tmpy), x = tmpx, y = tmpy; }     void print() { printf( (%.4lf %.4lf) n , x, y); }   };   int n, Q;   pnt p[10101 << 1], q;   double s[10101 << 1], area;   void prep() {     for (int i = 0, j = n - 1; i < j; i++, j--) swap(p[i], p[j]);     for (int i = 0; i <= n; i++) p[i + n] = p[i];     s[0] = 0;     for (int i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + (p[i - 1] ^ p[i]);     area = s[n];   }   double dist(pnt &p, double &a, double &b, double &c) {     return p.x * a + p.y * b + c;   }   pnt intersect(pnt &a, pnt &b, double &A, double &B, double &C) {     double a_ = dist(a, A, B, C);     double b_ = dist(b, A, B, C);     return (a * b_ - b * a_) / (b_ - a_);   }   double calc(double alpha) {     double A = sin(alpha), B = -cos(alpha), C = -A * q.x - B * q.y;     int st, en, u, v, mid;     double st_, en_, u_, v_, mid_;     st = 0, en = n - 1;     st_ = dist(p[st], A, B, C);     en_ = dist(p[en], A, B, C);     while (st < en) {       u = (st + en - 1) >> 1;       v = u + 1;       u_ = dist(p[u], A, B, C);       v_ = dist(p[v], A, B, C);       if ((st_ <= en_ && st_ <= v_) || (u_ <= en_ && u_ <= v_))         en = u;       else         st = v;     }     int mn = st;     st = 0, en = n - 1;     st_ = dist(p[st], A, B, C);     en_ = dist(p[en], A, B, C);     while (st < en) {       u = (st + en - 1) >> 1;       v = u + 1;       u_ = dist(p[u], A, B, C);       v_ = dist(p[v], A, B, C);       if ((st_ >= en_ && st_ >= v_) || (u_ >= en_ && u_ >= v_))         en = u;       else         st = v;     }     int mx = st;     st = mn, en = mx + n;     while (st < en - 1) {       mid = (st + en) >> 1;       mid_ = dist(p[mid], A, B, C);       if (mid_ < 0)         st = mid;       else         en = mid;     }     int ans1 = st % n;     pnt crs1 = intersect(p[ans1], p[ans1 + 1], A, B, C);     st = mx, en = mn + n;     while (st < en - 1) {       mid = (st + en) >> 1;       mid_ = dist(p[mid], A, B, C);       if (mid_ > 0)         st = mid;       else         en = mid;     }     int ans2 = st % n;     pnt crs2 = intersect(p[ans2], p[ans2 + 1], A, B, C);     if (ans1 >= ans2) ans2 += n;     double rlt = (p[ans2] ^ crs2) + (crs2 ^ crs1) + (crs1 ^ p[ans1 + 1]) +                  s[ans2] - s[ans1 + 1];     if (((crs1 - crs2) & pnt(cos(alpha), sin(alpha))) < 0) rlt = area - rlt;     return rlt;   }   int main() {     scanf( %d %d , &n, &Q);     for (int i = 0; i < n; i++) p[i].input();     prep();     while (Q--) {       q.input();       double st = 0, en = pi, mid, tmp;       bool stt = (calc(st) < area / 2), enn = !stt, midd;       for (int step = 1; step <= 42; step++) {         mid = (st + en) / 2;         tmp = calc(mid);         midd = (tmp < area / 2);         if (fabs(tmp * 2 - area) / area < eps) break;         if (midd == stt)           st = mid;         else           en = mid;       }       printf( %.20lf n , (st + en) / 2);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool solve(long long a, long long b) {     if (a > b) {       return solve(b, a);     }     if (a == 0) {       return false;     }     bool next = solve(b % a, a);     if (!next) {       return true;     }     long long c = (b / a);     if ((a % 2 == 1)) {       return (c % 2 == 0);     } else {       return (((b / a) % (a + 1)) % 2 == 0);     }   }   inline void init() {}   int main() {     init();     bool prev = false;     int T;     while (cin >> T) {       if (prev) {         cout << endl;       }       prev = true;       long long a, b;       for (int i = 0; i < T; i++) {         cin >> a >> b;         cout << (solve(a, b) ?  First  :  Second ) << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool visited[2000];   vector<int> G[2000];   vector<int> parent;   int dfs(int u) {     if (visited[u]) {       return 0;     }     visited[u] = true;     int ans = 0;     for (int i = 0; i < G[u].size(); i++) {       ans = max(ans, dfs(G[u][i]));     }     return ans + 1;   }   int ans(int n) {     int a = 0;     for (int i = 0; i < parent.size(); i++) {       a = max(a, dfs(parent[i]));     }     return a;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       visited[i] = false;       int u;       scanf( %d , &u);       if (u != -1) {         u--;         G[u].push_back(i);       } else {         parent.push_back(i);       }     }     printf( %d n , ans(n));   }


#include <bits/stdc++.h>   using namespace std;   long long arr[101], N, T;   void Input() {     cin >> N;     memset(arr, 0, sizeof arr);     for (int i = 0; i < N; i++) {       long long tmp;       cin >> tmp;       arr[tmp]++;     }   }   void Solve() {     int one = 0, two = 0;     for (int i = 0; i <= 100; i++) {       if (arr[i] >= 2 && one == two) {         one++;         two++;       } else if (arr[i] >= 1) {         one++;       } else {         break;       }     }     cout << one + two <<   n ;   }   int main() {     cin >> T;     for (int i = 0; i < T; i++) {       Input();       Solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000005;   int N;   char S[2 * MAXN];   int ord[2 * MAXN];   pair<pair<int, int>, int> E[2 * MAXN];   int step = 1;   int pre[MAXN];   int minp[MAXN];   int mins[MAXN];   char ans[MAXN];   void build() {     for (int i = 0; i < N; i++) ord[i] = (S[i] ==  ) );     for (int jump = 1; jump < N; step++, jump *= 2) {       for (int i = 0; i < N; i++)         E[i] = make_pair(             pair<int, int>(ord[i], (i + jump < N) ? ord[i + jump] : N), i);       sort(E, E + N);       int ind = -1;       pair<int, int> prev(-1, -1);       for (int i = 0; i < N; i++) {         int idx = E[i].second;         pair<int, int> cur = E[i].first;         if (cur != prev) {           ind++;           prev = cur;         }         ord[idx] = ind;       }     }   }   int main() {     scanf( %s , &S);     N = (int)(strlen(S));     memcpy(S + N, S, N);     N += N;     S[N] =  | ;     S[N + 1] = 0;     build();     N /= 2;     for (int i = 1; i <= N; i++)       pre[i] = pre[i - 1] + ((S[i - 1] ==  ( ) ? 1 : -1);     minp[0] = pre[0] + pre[N];     for (int i = 1; i < N; i++) minp[i] = min(minp[i - 1], pre[i] + pre[N]);     mins[N - 1] = pre[N - 1];     for (int i = N - 2; i >= 0; i--) mins[i] = min(mins[i + 1], pre[i]);     int best = -1, x = min(0, pre[N]);     for (int i = 0; i < N; i++)       if (min(minp[i], mins[i]) - pre[i] >= x)         if (best == -1 || ord[i] < ord[best]) best = i;     int ind = 0;     for (int i = 0; i < -pre[N]; i++) ans[ind++] =  ( ;     for (int i = 0; i < N; i++) ans[ind++] = S[best + i];     for (int i = 0; i < pre[N]; i++) ans[ind++] =  ) ;     ans[ind] = 0;     printf( %s n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string findSum(string str1, string str2) {     if (str1.length() > str2.length()) swap(str1, str2);     string str =   ;     int n1 = str1.length(), n2 = str2.length();     reverse(str1.begin(), str1.end());     reverse(str2.begin(), str2.end());     int carry = 0;     for (int i = 0; i < n1; i++) {       int sum = ((str1[i] -  0 ) + (str2[i] -  0 ) + carry);       str.push_back(sum % 10 +  0 );       carry = sum / 10;     }     for (int i = n1; i < n2; i++) {       int sum = ((str2[i] -  0 ) + carry);       str.push_back(sum % 10 +  0 );       carry = sum / 10;     }     if (carry) str.push_back(carry +  0 );     reverse(str.begin(), str.end());     return str;   }   int main() {     long long n;     cin >> n;     string s;     cin >> s;     int idx = (n + 1) / 2;     while (idx < n && s[idx] ==  0 ) idx++;     if (idx == n) {       idx--;       while (s[idx] ==  0 ) idx--;       if (idx == 0) {         cout << s << endl;       } else {         string s1 = s.substr(0, idx);         string s2 = s.substr(idx);         cout << findSum(s1, s2) << endl;       }     } else {       string s1 = findSum(s.substr(0, idx), s.substr(idx));       idx--;       while (s[idx] ==  0 ) idx--;       string s2 = findSum(s.substr(0, idx), s.substr(idx));       if (s1.size() > s2.size()) {         cout << s2 << endl;       } else if (s1.size() < s2.size()) {         cout << s1 << endl;       } else {         cout << min(s1, s2) << endl;       }     }   }


#include <bits/stdc++.h>   using std::cin;   using std::cout;   using std::string;   int main() {     bool newline = true;     bool found;     for (string s; getline(cin, s);) {       found = false;       for (string::size_type index = 0; index < s.size(); ++index)         if (s[index] !=    )           if (s[index] ==  # )             found = true;           else             break;       if (found) {         if (!newline) cout << std::endl;         cout << s << std::endl;         newline = true;       } else {         for (string::size_type index = 0; index < s.size(); ++index)           if (s[index] !=    ) cout << s[index];         newline = false;       }     }     if (!found) cout << std::endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     pair<long long, int> p[400005];     int n, m;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> p[i].first;       p[i].second = 1;     }     cin >> m;     for (int i = 0; i < m; i++) {       cin >> p[i + n].first;       p[i + n].second = 2;     }     int x = n * 3;     int y = m * 3;     sort(p, p + n + m);     int u = x, v = y;     for (int i = 0; i < n + m; i++) {       if (p[i].second == 1)         x--;       else         y--;       if (x - y > u - v) {         u = x;         v = y;       }     }     cout << u <<  :  << v << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 100100;   const int Mod = 1e9 + 7;   long long N, S;   long long p[MaxN];   long long deg[MaxN];   long long pref[MaxN];   long long depth[MaxN];   vector<long long> child[MaxN];   void solve(long long branch) {     for (int j = (2); j < (N + 1); j++) {       p[j] = j - 1;       deg[j - 1] = 1;       child[j - 1].push_back(j);       depth[j] = j;     }     deg[N] = 0;     if (branch == 1) {       cout <<  Yes  << endl;       for (int j = (2); j < (N + 1); j++) {         cout << p[j] <<    ;       }       exit(0);     }     long long lo = 2 * N - 1;     long long hi = N * (N + 1) / 2;     if (S < lo || S > hi) {       cout <<  No  << endl;       exit(0);     }     long long x = N - 2;     long long i = N;     long long z = 0;     vector<long long> anchor;     anchor.push_back(1);     depth[1] = 1;     while (S < hi) {       while (deg[anchor[z]] >= branch) {         for (auto w : child[anchor[z]]) {           anchor.push_back(w);           ;           depth[w] = depth[anchor[z]] + 1;         }         z++;         if (z > (long long)anchor.size()) {           assert(false);           cin.ignore();         }         x = (i - 1) - depth[anchor[z]];       };       ;       if (S + x <= hi) {         deg[p[i]]--;         child[p[i]].pop_back();         p[i] = anchor[z];         deg[anchor[z]]++;         assert(anchor[z] != i);         child[anchor[z]].push_back(i);         S += x;         x--;         i--;       } else {         x = (hi - S);         p[i] = (i - 1) - x;         S += x;       }     }     cout <<  Yes  << endl;     for (int j = (2); j < (N + 1); j++) {       cout << p[j] <<    ;     }   }   long long check() {     for (int i = (1); i < (N + 1); i++) pref[i] = 1;     for (long long i = N; i >= 2; i--) {       pref[p[i]] += pref[i];     }     long long sum = 0;     for (int i = (1); i < (N + 1); i++) {       sum += pref[i];     }     return sum;   }   long long f(long long branch) {     if (branch == 1) {       return (N * (N + 1)) / 2;     }     for (long long i = 1; i <= N; i++) {       deg[i] = 0;     }     long long cur = 1;     for (long long i = 2; i <= N; i++) {       while (deg[cur] >= branch) {         cur++;       }       p[i] = cur;       deg[cur]++;     }     long long ret = check();     return ret;   }   void load() {     cin >> N >> S;     long long lo = 2 * N - 1;     long long hi = N * (N + 1) / 2;     if (S < lo || S > hi) {       cout <<  No  << endl;       exit(0);     }   }   int main() {     ios_base::sync_with_stdio(false);     load();     long long lo = 1, hi = N - 1;     while (lo < hi) {       long long mid = (lo + hi) / 2;       if (f(mid) <= S) {         hi = mid;       } else {         lo = mid + 1;       }     };     solve(lo);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, a, b, sum[200005], sm[200005];   struct node {     long long x, y, z;   } val[200005];   bool comp(node a, node b) { return a.z > b.z; }   int main(void) {     long long ans = 0;     scanf( %lld%lld%lld , &n, &a, &b);     b = min(b, n);     for (int i = 1; i <= n; i++)       scanf( %lld%lld , &val[i].x, &val[i].y), val[i].z = val[i].x - val[i].y;     sort(val + 1, val + n + 1, comp);     for (int i = 1; i <= n; i++) {       sum[i] = sum[i - 1] + max(val[i].x, val[i].y);       sm[i] = sm[i - 1] + val[i].y;     }     ans += sum[b] + sm[n] - sm[b];     if (b == 0) {       printf( %lld n , sm[n]);       return 0;     }     if (a == 0) {       printf( %lld n , ans);       return 0;     }     for (int i = 1; i <= n; i++) {       int num = 0;       long long t = val[i].x;       for (int j = 1; j <= a; j++) t *= 2ll;       if (i <= b - 1)         t = t - max(val[i].x, val[i].y), t += sum[b], t += sm[n] - sm[b];       else         t += sum[b - 1], t += sm[n] - sm[b - 1] - val[i].y;       ans = max(ans, t);     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 205;   const int mod = 10007;   void add(int &x, int y) {     x += y;     if (x >= mod) x -= mod;   }   int mul(int x, int y) { return x * y % mod; }   int pw(int x, int y) {     int ret = 1;     while (y) {       if (y & 1) ret = mul(ret, x);       x = mul(x, x);       y >>= 1;     }     return ret;   }   namespace berlekamp_massey {   vector<int> bm(vector<int> x) {     vector<int> rel, cur;     int lf, ld;     for (int i = 0; i < x.size(); ++i) {       int sum = 0;       for (int j = 0; j < cur.size(); ++j) add(sum, mul(x[i - j - 1], cur[j]));       if (sum == x[i]) continue;       if (!cur.size()) {         cur.resize(i + 1);         lf = i;         ld = sum - x[i];         add(ld, mod);         continue;       }       int coef = mul(sum - x[i] + mod, pw(ld, mod - 2));       vector<int> new_cur(i - lf - 1);       new_cur.push_back(coef);       for (int j = 0; j < rel.size(); ++j) {         new_cur.push_back(mul(mod - rel[j], coef));       }       if (new_cur.size() < cur.size()) new_cur.resize(cur.size());       for (int j = 0; j < cur.size(); ++j) add(new_cur[j], cur[j]);       if (i - lf + rel.size() >= cur.size()) {         rel = cur, lf = i, ld = sum - x[i];         add(ld, mod);       }       cur = new_cur;     }     return cur;   }   int sz;   int a[5005], rel[5005], tmp[5005];   int ff[5005], gg[5005];   void mulmod(int *p, int *q) {     for (int i = 0; i < sz + sz; ++i) tmp[i] = 0;     for (int i = 0; i < sz; ++i)       if (p[i]) {         for (int j = 0; j < sz; ++j) {           add(tmp[i + j], mul(p[i], q[j]));         }       }     for (int i = sz + sz - 1; i >= sz; --i)       if (tmp[i]) {         for (int j = sz - 1; j >= 0; --j) {           add(tmp[i - j - 1], mul(tmp[i], rel[j]));         }       }     for (int i = 0; i < sz; ++i) p[i] = tmp[i];   }   int solve(vector<int> x, int n) {     if (n < x.size()) return x[n];     vector<int> cur = bm(x);     sz = cur.size();     for (int i = 0; i < sz; ++i) rel[i] = cur[i], a[i] = x[i];     for (int i = 0; i < sz; ++i) ff[i] = gg[i] = 0;     ff[0] = 1;     if (sz > 1)       gg[1] = 1;     else       gg[0] = rel[0];     while (n) {       if (n & 1) mulmod(ff, gg);       mulmod(gg, gg);       n >>= 1;     }     int ret = 0;     for (int i = 0; i < sz; ++i) add(ret, mul(ff[i], a[i]));     return ret;   }   }  // namespace berlekamp_massey   using namespace berlekamp_massey;   int n, m;   string s;   int f[805][N][N], ans[1405];   vector<int> g;   int main() {     ios_base::sync_with_stdio(false);     cin >> s >> n;     m = s.size();     s =  ~  + s;     f[0][0][0] = 1;     int len = m + 1400 >> 1;     for (int i = 1; i <= len; ++i) {       for (int j = 0; j <= m; ++j) {         for (int k = 0; k <= m; ++k) {           if (f[i - 1][j][k] == 0) continue;           if (j == m && k == m)             add(f[i][j][k], mul(26, f[i - 1][j][k]));           else if (j == m) {             add(f[i][j][k], mul(25, f[i - 1][j][k]));             add(f[i][j][k + 1], f[i - 1][j][k]);           } else if (k == m) {             add(f[i][j][k], mul(25, f[i - 1][j][k]));             add(f[i][j + 1][k], f[i - 1][j][k]);           } else {             if (s[j + 1] == s[m + 1 - k - 1]) {               add(f[i][j + 1][k + 1], f[i - 1][j][k]);               add(f[i][j][k], mul(25, f[i - 1][j][k]));             } else {               add(f[i][j + 1][k], f[i - 1][j][k]);               add(f[i][j][k + 1], f[i - 1][j][k]);               add(f[i][j][k], mul(24, f[i - 1][j][k]));             }           }         }       }     }     for (int i = 0; i < 1400; ++i) {       int l = m + i >> 1;       for (int j = 0; j <= m; ++j) {         for (int k = 0; k <= m; ++k) {           if (j + k >= m) {             if (m + i & 1)               add(ans[i], mul(26, f[l][j][k]));             else               add(ans[i], f[l][j][k]);           }           if (j + k == m - 1) {             if (m + i & 1) add(ans[i], f[l][j][k]);           }         }       }     }     for (int i = 0; i < 1400; ++i) g.push_back(ans[i]);     cout << solve(g, n) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, A, B;     while (cin >> n) {       vector<int> a, b;       while (n--) {         int p;         cin >> p;         a.push_back(p);       }       int ans = 0;       cin >> A >> B;       sort(a.begin(), a.end());       a.resize(unique(a.begin(), a.end()) - a.begin());       while (A != B) {         int t1 = A - 1;         int len = a.size();         b.clear();         for (int i = 0; i < len; i++) {           int t = A / a[i] * a[i];           if (t >= B) {             t1 = min(t, t1);             b.push_back(a[i]);           }         }         a = b;         A = t1;         ans++;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int N = 5e5 + 2;   const int M = 1e5 + 2;   const int inf = 2e9 + 2;   const long long linf = 4e18 + 2;   long long add(long long a, long long b) {     a += b;     return a >= mod ? a - mod : a;   }   long long sub(long long a, long long b) {     a += mod - b;     return a >= mod ? a - mod : a;   }   long long mul(long long a, long long b) { return (long long)a * b % mod; }   long long a[N], bit[2][N];   long long id[N];   bool cmp(long long c, long long d) { return a[c] < a[d]; }   void upd(int x, int val, int s) {     for (; x < N; x += x & (-x)) bit[s][x] += val;   }   long long get(int x, int s) {     long long ans = 0;     for (; x > 0; x -= x & (-x)) ans += bit[s][x];     return ans;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i], id[i] = i;     sort(id + 1, id + 1 + n, cmp);     long long ans = 0;     for (int j = 1; j <= n; j++) {       int i = id[j];       ans = add(ans, mul(a[i], mul(i, n - i + 1)));       long long l = get(i, 0);       long long r = get(n, 1) - get(i, 1);       ans = add(ans, mul(a[i], mul(l, n - i + 1)));       ans = add(ans, mul(a[i], mul(r, i)));       upd(i, i, 0);       upd(i, n - i + 1, 1);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   int n;   int a[5005], pos[5005];   long long dp[5005], f[5055], inv[5005];   void prework() {     f[0] = 1;     f[1] = 1;     inv[1] = 1;     inv[0] = 1;     for (int i = 2; i <= n; i++) {       f[i] = f[i - 1] * i % mod;       inv[i] = (mod - mod / i) * inv[mod % i] % mod;     }     for (int i = 2; i <= n; i++) {       inv[i] = inv[i] * inv[i - 1] % mod;     }   }   long long A(long long a, long long b) { return f[a] * inv[a - b] % mod; }   int main() {     cin >> n;     prework();     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + n + 1);     int head = 0;     for (int i = 1; i <= n; i++) {       while (a[head + 1] * 2 <= a[i]) head++;       pos[i] = head;     }     dp[0] = 1;     pos[0] = -1;     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= pos[i]; j++) {         dp[i] =             (dp[i] + dp[j] * A(n - 2 - pos[j], pos[i] - pos[j] - 1) % mod) % mod;       }     }     if (pos[n] == n - 1)       cout << dp[n] << endl;     else       cout << 0 << endl;     return 0;   }


#include <bits/stdc++.h>   int a[1010], b[1010];   int n, k, inf = 0x0f0f0f0f;   int test(int cur) {     int i, ans = 0;     if (cur <= 0) return inf;     for (i = 0; i < n; i++) {       if (a[i] != cur) ans++;       cur += k;     }     return ans;   }   int main() {     int i, j, cur;     while (~scanf( %d%d , &n, &k)) {       for (i = 0; i < n; i++) scanf( %d , &a[i]);       for (i = 0; i < n; i++) b[i] = test(a[i] - i * k);       for (i = 1, j = 0; i < n; i++)         if (b[i] < b[j]) j = i;       printf( %d n , b[j]);       cur = a[j] - k * j;       for (i = 0; i < n; i++) {         if (a[i] != cur) {           if (a[i] < cur)             printf( + %d %d n , i + 1, cur - a[i]);           else             printf( - %d %d n , i + 1, a[i] - cur);         }         cur += k;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   struct Event {     int t, L, R, on;     Event(int t = 0, int L = 0, int R = 0, int on = 0)         : t(t), L(L), R(R), on(on) {}     bool operator<(const Event &rh) const { return t < rh.t; }   } E[maxn * 2];   int V[maxn * 2], cnt;   int Pre[maxn * 2], P[maxn * 2];   long long Ans[maxn];   int ID(int v) {     int L = 1, R = cnt;     while (L < R) {       int mid = (L + R) >> 1;       if (V[mid] < v)         L = mid + 1;       else         R = mid;     }     return L;   }   int main() {     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       static int x, y;       scanf( %d%d , &x, &y);       E[2 * i - 1] = Event(x - k + 1, y, y + k - 1, 1);       E[2 * i] = Event(x + 1, y, y + k - 1, -1);       V[++cnt] = y;       V[++cnt] = y + k;     }     sort(V + 1, V + 1 + cnt);     cnt = unique(V + 1, V + 1 + cnt) - V - 1;     sort(E + 1, E + 1 + 2 * n);     for (int i = 1; i <= 2 * n; i++) {       int id = ID(E[i].L);       for (; id < cnt && V[id] <= E[i].R; id++) {         Ans[P[id]] += ((long long)(E[i].t - Pre[id])) * (V[id + 1] - V[id]);         P[id] += E[i].on;         Pre[id] = E[i].t;       }     }     for (int i = 1; i <= n; i++) cout << Ans[i] << (i == n ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[105];   int f[105];   int v[105];   bool cmp(int i, int j) { return a[i] > a[j]; }   int main() {     int n, w;     while (cin >> n >> w) {       int sum = 0;       int sum2 = 0;       for (int i = 0; i < n; i++) {         cin >> a[i];         sum += a[i];         if (a[i] % 2 == 0)           f[i] = a[i] / 2;         else           f[i] = (a[i] + 1) / 2;         sum2 += f[i];       }       for (int i = 0; i < n; i++) v[i] = i;       sort(v, v + n, cmp);       if (sum2 > w || sum < w) {         cout << -1 << endl;         continue;       }       w -= sum2;       for (int i = 0; i < n; i++) {         if (w > a[v[i]] - f[v[i]]) {           w -= (a[v[i]] - f[v[i]]);           f[v[i]] = a[v[i]];         } else if (w <= a[v[i]] - f[v[i]]) {           f[v[i]] += w;           break;         }       }       for (int i = 0; i < n; i++) {         cout << f[i];         if (i != n - 1) cout <<    ;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   class BIT {     std::vector<int> bit;       public:     BIT(int size) : bit(size + 1, 0) {}     void add(int i, int x) {       i++;       while (i < (int)bit.size()) {         bit[i] += x;         i += i & -i;       }     }     int sum(int a) {       a++;       int res = 0;       while (0 < a) {         res += bit[a];         a -= a & -a;       }       return res;     }     int sum(int a, int b) { return sum(b) - sum(a - 1); }   };   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     string s;     cin >> s;     int n;     cin >> n;     BIT xs(s.size()), ys(s.size()), zs(s.size());     for (int i = 0; i < (s.size()); i++) {       if (s[i] ==  x )         xs.add(i, 1);       else if (s[i] ==  y )         ys.add(i, 1);       else         zs.add(i, 1);     }     for (int i = 0; i < (n); i++) {       int l, r;       cin >> l >> r;       l--;       r--;       int x, y, z;       x = xs.sum(l, r);       y = ys.sum(l, r);       z = zs.sum(l, r);       string ans =  NO ;       if (abs(x - y) < 2 && abs(x - z) < 2 && abs(y - z) < 2 || r - l < 2)         ans =  YES ;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   const int N = 1000001;   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, m, k;     string s;     cin >> n >> k >> s;     m = s.size();     int last[200];     memset(last, -1, sizeof last);     for (int i = 0; i < m; i++) {       last[s[i]] = i;     }     pair<int, char> a[k];     for (int i = 0; i < k; i++) {       a[i] = make_pair(last[ a  + i],  a  + i);     }     sort(a, a + k);     int ind = 0;     for (int i = 0; i < n; i++) {       s += a[ind].second;       ind = (ind + 1) % k;     }     long long int dp[200];     memset(dp, 0, sizeof dp);     long long int t = 0;     for (char c : s) {       long long int h = dp[c];       dp[c] = (t + 1 + 1000000007) % 1000000007;       t = (t - h + dp[c] + 1000000007) % 1000000007;     }     cout << (t + 1) % 1000000007;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:640000000 )   using namespace std;   const double EPS = 1e-9;   const int INF = 0x7f7f7f7f;   const double PI = acos(-1.0);   template <class T>   inline T _abs(T n) {     return ((n) < 0 ? -(n) : (n));   }   template <class T>   inline T _max(T a, T b) {     return (!((a) < (b)) ? (a) : (b));   }   template <class T>   inline T _min(T a, T b) {     return (((a) < (b)) ? (a) : (b));   }   template <class T>   inline T _swap(T& a, T& b) {     a = a ^ b;     b = a ^ b;     a = a ^ b;   }   template <class T>   inline T gcd(T a, T b) {     return (b) == 0 ? (a) : gcd((b), ((a) % (b)));   }   template <class T>   inline T lcm(T a, T b) {     return ((a) / gcd((a), (b)) * (b));   }   template <typename T>   string NumberToString(T Number) {     ostringstream second;     second << Number;     return second.str();   }   struct debugger {     template <typename T>     debugger& operator,(const T& v) {       cerr << v <<    ;       return *this;     }   } dbg;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     while (cin >> n) {       priority_queue<pair<int, string> > pq;       for (int i = (int)(1); i <= (int)(n); i++) {         string st;         int no;         cin >> st >> no;         pq.push(make_pair(-no, st));       }       vector<pair<int, string> > v;       int hh = 1;       int flg = 1;       while (!pq.empty()) {         pair<int, string> p = pq.top();         pq.pop();         p.first *= -1;         if (p.first == 0) {           v.push_back(make_pair(hh, p.second));           hh++;           continue;         }         vector<int> tmp;         for (int i = (int)(0); i <= (int)((int)v.size() - 1); i++) {           tmp.push_back(v[i].first);         }         sort(tmp.rbegin(), tmp.rend());         if (tmp.size() < p.first) {           flg = 0;           break;         }         int tmpNo = tmp[p.first - 1];         for (int i = (int)(0); i <= (int)((int)v.size() - 1); i++) {           if (v[i].first >= tmpNo) v[i].first += 1;         }         v.push_back(make_pair(tmpNo, p.second));       }       if (flg) {         for (int i = (int)(0); i <= (int)(n - 1); i++) {           cout << v[i].second <<     << v[i].first <<   n ;         }       } else         printf( -1 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int iinf = 0x3f3f3f3f;   const long long linf = 0x3f3f3f3f3f3f3f3f;   const int N = 1e4, M = 1e5;   int n, m, p, a[N], b[M];   int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }   int mypow(int a, int first = p - 2, int res = 1) {     for (; first; first >>= 1, a = 1ll * a * a % p)       (first & 1) && (res = 1ll * res * a % p);     return res;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     cin >> n >> m >> p;     int phi = p - 1, g = phi;     for (int i(0); i < (n); ++i) cin >> a[i];     for (int i(0); i < (m); ++i) cin >> b[i], g = gcd(g, b[i]);     vector<int> c;     for (int d = 1; d * d <= phi; ++d)       if (phi % d == 0)         c.push_back(d), d *d < phi ? c.push_back(phi / d) : void();     sort(c.begin(), c.end());     vector<int> mb;     for (int i(0); i < (n); ++i) {       int mn_c = -1;       for (int &u : c)         if (mypow(a[i], 1ll * g * u % phi) == 1) {           mb.push_back(phi / u);           break;         }     }     sort(mb.begin(), mb.end());     mb.erase(unique(mb.begin(), mb.end()), mb.end());     vector<int> f(int((mb).size()), 0);     int ns = 0;     for (int i((int((mb).size())) - 1); i >= 0; --i) {       f[i] = phi / mb[i];       for (int j = i + 1; j < int((mb).size()); ++j)         if (mb[j] % mb[i] == 0) f[i] -= f[j];       ns += f[i];     }     cout << ns <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   double p[101], f[101], ans = 0, last = 1;   double Calc(int x) { return 1 - (1 - f[x]) * (1 - p[x]); }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       p[i] = f[i] = x / 100.0;       last *= p[i];     }     ans += last * n;     for (int i = n + 1; i <= 300000; i++) {       int x = 1;       for (int j = 1; j <= n; j++) {         if (Calc(j) / f[j] > Calc(x) / f[x]) x = j;       }       double now = last * Calc(x) / f[x];       f[x] = Calc(x);       ans += (now - last) * i;       last = now;     }     printf( %.8f n , ans);   }


#include <bits/stdc++.h>   using namespace std;   pair<double, double> p[100100];   double dt[100100];   double db[100100];   double da[100100];   int main() {     double ax, ay, bx, by, tx, ty;     int n;     cin >> ax >> ay >> bx >> by >> tx >> ty;     cin >> n;     double ans = 0;     scanf( %lf %lf , &p[0].first, &p[0].second);     ans += sqrt((p[0].first - tx) * (p[0].first - tx) +                 (p[0].second - ty) * (p[0].second - ty));     dt[0] = ans;     for (int i = 1; i < n; i++) {       scanf( %lf %lf , &p[i].first, &p[i].second);       dt[i] = sqrt((p[i].first - tx) * (p[i].first - tx) +                    (p[i].second - ty) * (p[i].second - ty));       ans += dt[i];     }     ans *= 2;     for (int i = 0; i < n; i++) {       da[i] = sqrt((p[i].first - ax) * (p[i].first - ax) +                    (p[i].second - ay) * (p[i].second - ay));       db[i] = sqrt((p[i].first - bx) * (p[i].first - bx) +                    (p[i].second - by) * (p[i].second - by));     }     double p1, p2, p3, p4;     double Da = 1.7976931348623158e+308, Db = 1.7976931348623158e+308;     int indexofa = -1;     int indexofb = -1;     for (int i = 0; i < n; i++) {       if (da[i] - dt[i] < Da) {         Da = da[i] - dt[i];         indexofa = i;       }     }     for (int i = 0; i < n; i++) {       if (db[i] - dt[i] < Db && i != indexofa) {         Db = db[i] - dt[i];         indexofb = i;       }     }     p1 = Da + Db;     Da = 1.7976931348623158e+308, Db = 1.7976931348623158e+308;     indexofa = -1;     indexofb = -1;     for (int i = 0; i < n; i++) {       if (db[i] - dt[i] < Db) {         Db = db[i] - dt[i];         indexofb = i;       }     }     for (int i = 0; i < n; i++) {       if (da[i] - dt[i] < Da && i != indexofb) {         Da = da[i] - dt[i];         indexofa = i;       }     }     p2 = Da + Db;     Db = 1.7976931348623158e+308;     indexofb = -1;     for (int i = 0; i < n; i++) {       if (db[i] - dt[i] < Db) {         Db = db[i] - dt[i];         indexofb = i;       }     }     p3 = Db;     Da = 1.7976931348623158e+308;     indexofa = -1;     for (int i = 0; i < n; i++) {       if (da[i] - dt[i] < Da) {         Da = da[i] - dt[i];         indexofa = i;       }     }     p4 = Da;     printf( %.12lf , min(min(p1, p2), min(p3, p4)) + ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, n;     while (~scanf( %d%d%d%d , &a, &b, &c, &n)) {       if (c > a || c > b || n - (a + b - c) < 1)         printf( -1 n );       else         printf( %d n , n - (a + b - c));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int count = 0, wc = 0;       bool flag = 0;       int m = n;       while (n--) {         string s;         cin >> s;         int l = s.size();         if (l % 2 == 1) flag = 1;         int zeroc = 0, onec = 0;         for (int i = 0; i < l; i++) {           if (s[i] ==  0 ) {             zeroc++;           } else {             onec++;           }         }         if (onec % 2 == 1 && zeroc % 2 == 1) {           wc++;         } else {           count++;         }       }       if (flag == 1)         cout << m << endl;       else {         count = count + 2 * (wc / 2);         cout << count << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n, k;     cin >> n >> k;     long long int cost = 0, ans = 0, a[n];     vector<pair<long long int, long long int>> pr(n);     for (long long int(i) = 0; (i) < (n); (i)++) {       long long int a;       cin >> a;       pr[i] = make_pair(a, i + 1);     }     sort(pr.begin(), pr.end());     vector<long long int> v;     for (long long int(i) = 0; (i) < (n); (i)++) {       if ((cost > k) || (cost + pr[i].first > k))         break;       else {         cost += pr[i].first;         ans++;         v.push_back(pr[i].second);       }     }     cout << ans <<   n ;     sort(v.begin(), v.end());     for (long long int(i) = 0; (i) < (ans); (i)++) {       cout << v[i] <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m, k;     cin >> n >> m >> k;     string ans;     for (int i = 0; i < n; i++) ans +=  U ;     for (int i = 0; i < m; i++) ans +=  L ;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m - 1; j++) {         ans += (i % 2 ?  L  :  R );       }       if (i < n - 1) ans +=  D ;     }     cout << ans.size() <<   n  << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2010, mod = 998244353;   int sm[2][N], n, f[N][N], a[N][N], ans;   void add(int x, int y, int z) {     for (; x <= n; x += x & -x) sm[z][x] += y;   }   int query(int x, int z) {     int ret = 0;     for (; x; x -= x & -x) ret += sm[z][x];     return ret;   }   void Init() {     f[0][0] = 1;     for (int i = 1; i <= n; ++i) {       f[i][0] = (long long)f[i - 1][0] * i % mod;       for (int j = 1; j <= i; ++j)         f[i][j] = (f[i][j - 1] - f[i - 1][j - 1] + mod) % mod;     }   }   int p[N], q[N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     Init();     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) cin >> a[i][j];     for (int i = 1; i <= n; ++i) {       int les = n;       ans = (long long)ans * f[n][n] % mod;       memset(sm, 0, sizeof sm);       memset(p, 0, sizeof p);       memset(q, 0, sizeof q);       for (int j = 1; j <= n; ++j) {         int u = query(a[i][j] - 1, 0);         p[a[i - 1][j]] = 1;         if (q[a[i - 1][j]])           ++les;         else if (a[i - 1][j])           add(a[i - 1][j], 1, 0), add(a[i - 1][j], 1, 1);         int v = a[i][j] - 1 - query(a[i][j] - 1, 1);         les -= 2;         if (i == 1)           ans = (ans + (long long)v * f[n - j][0]) % mod;         else           ans = (ans + (long long)u * f[n - j][les + 1] +                  (long long)v * f[n - j][les]) %                 mod;         q[a[i][j]] = 1;         if (p[a[i][j]])           add(a[i][j], -1, 0), ++les;         else           add(a[i][j], 1, 1);       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string s;       cin >> s;       vector<int> p0;       vector<int> p1;       vector<int> ans(n);       for (int i = 0; i < n; i++) {         int newpos = p0.size() + p1.size();         if (s[i] ==  0 ) {           if (p1.empty()) {             p0.push_back(newpos);           } else {             newpos = p1.back();             p0.push_back(newpos);             p1.pop_back();           }         } else {           if (p0.empty()) {             p1.push_back(newpos);           } else {             newpos = p0.back();             p1.push_back(newpos);             p0.pop_back();           }         }         ans[i] = newpos + 1;       }       cout << p0.size() + p1.size() << endl;       for (int i = 0; i < n; i++) {         cout << ans[i] <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int tt;     cin >> tt;     while (tt--) {       string s;       cin >> s;       reverse(s.begin(), s.end());       if (s.substr(0, 2) ==  op ) {         cout <<  FILIPINO n ;       } else if (s.substr(0, 4) ==  used  || s.substr(0, 4) ==  usam ) {         cout <<  JAPANESE n ;       } else if (s.substr(0, 5) ==  adinm ) {         cout <<  KOREAN n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char field[1000][1000];   int deltas[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};   int n, m;   int si, sj, ti, tj;   bool possible;   void go(int i, int j, char num) {     for (auto d : deltas) {       int ii = i;       int jj = j;       while (true) {         ii += d[0];         jj += d[1];         if (ii < 0 || jj < 0 || ii >= n || jj >= m) break;         if (field[ii][jj] ==  T ) possible = true;         if (field[ii][jj] !=  . ) break;         field[ii][jj] = num;       }     }   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m;     for (int i = 0; i < n; i++) {       string line;       cin >> line;       for (int j = 0; j < m; j++) {         field[i][j] = line[j];         if (line[j] ==  T ) {           ti = i;           tj = j;         }         if (line[j] ==  S ) {           si = i;           sj = j;         }       }     }     go(si, sj,  0 );     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (field[i][j] ==  0 ) {           go(i, j,  1 );         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (field[i][j] ==  1 ) {           go(i, j,  2 );         }       }     }     if (possible)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Df[300005], GG, RR, fw[300005 * 2], U, lt[300005], val[300005], rt[300005],       color[300005], Dep[300005], st[300005], rev[300005], cnt, fa[300005][20],       Fa[300005], cl, dfn[300005], in, col[300005], low[300005],       tot = 1, node[300005], vst[300005 << 1], dep[300005];   vector<int> son[300005], sonn[300005];   struct edge {     int to, nxt;   } e[300005 << 1];   inline void add(int x, int y) {     e[++tot].to = y;     e[tot].nxt = node[x];     node[x] = tot;   }   void dfs(int now) {     dfn[now] = low[now] = ++in;     for (int i = node[now]; i; i = e[i].nxt) {       if (vst[i] | vst[i ^ 1]) continue;       vst[i] = 1;       if (!dfn[e[i].to])         dep[e[i].to] = dep[now] + 1, Fa[e[i].to] = now, dfs(e[i].to),         low[now] = min(low[now], low[e[i].to]);       else         low[now] = min(low[now], dfn[e[i].to]);     }   }   void MEM(int now) {     dfn[now] = 0;     Fa[now] = dep[now] = 0;     for (int i = node[now]; i; i = e[i].nxt) {       if (dfn[e[i].to]) MEM(e[i].to);     }     node[now] = 0;   }   void Dfs(int now) {     col[now] = cl;     for (int T, i = 0; i < son[now].size(); ++i)       if (!col[T = son[now][i]]) col[T] = cl, Dfs(T);   }   void Dfs_(int now) {     color[now] = 0;     for (int T, i = 0; i < sonn[now].size(); ++i)       if (!~color[T = sonn[now][i]]) Dfs_(T);   }   void dfs_v(int now, int fat) {     lt[now] = cnt;     Df[now] = ++in;     vst[now] = 1;     for (int i = 1; i <= 18; ++i) fa[now][i] = fa[fa[now][i - 1]][i - 1];     for (int T, i = 0; i < son[now].size(); ++i)       if ((T = son[now][i]) != fat)         fa[T][0] = now, Dep[T] = Dep[now] + 1, dfs_v(T, now);   }   inline int lca(int x, int y) {     int i = 18;     if (Dep[x] < Dep[y]) swap(x, y);     for (; ~i; --i)       if (Dep[fa[x][i]] >= Dep[y]) x = fa[x][i];     if (x == y) return x;     for (i = 18; ~i; --i)       if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];     return fa[x][0];   }   inline bool cmp(int x, int y) { return Df[x] < Df[y]; }   int n, m, las, fl, lc, hd, q, v, i, X[300005 * 3], V, AL, E, Y[300005 * 3], x,       y, j, Ex[300005], Ey[300005];   int main() {     cin >> n >> m >> q;     for (i = 1; i <= m; ++i) cin >> x >> y, add(y, x), add(X[i] = x, Y[i] = y);     for (i = 1; i <= n; ++i)       if (!dfn[i]) dfs(i);     for (i = 1; i <= m; ++i)       if (dep[X[i]] > dep[Y[i]]) swap(X[i], Y[i]);     for (i = 1; i <= m; ++i) {       if (Fa[Y[i]] == X[i] && low[Y[i]] > dfn[X[i]]) continue;       vst[i] = -1;       son[X[i]].push_back(Y[i]);       son[Y[i]].push_back(X[i]);     }     for (i = 1; i <= n; ++i)       if (!col[i]) ++cl, Dfs(i);     for (i = 1; i <= n; ++i) son[i].clear();     for (i = 1; i <= m; ++i)       if (col[X[i]] != col[Y[i]])         sonn[col[X[i]]].push_back(col[Y[i]]),             sonn[col[Y[i]]].push_back(col[X[i]]);     for (i = 1; i <= cl; ++i)       if (sonn[i].size())         for (sort(sonn[i].begin(), sonn[i].end()), son[i].push_back(sonn[i][0]),              j = 1;              j < sonn[i].size(); ++j)           if (sonn[i][j] != sonn[i][j - 1]) son[i].push_back(sonn[i][j]);     memset(Fa, 0, sizeof(Fa));     memset(vst, 0, sizeof(vst));     memset(dep, 0, sizeof(dep));     memset(node, 0, sizeof(node));     memset(dfn, 0, sizeof(dfn));     in = 0;     for (i = 1; i <= cl; ++i)       if (!vst[i]) ++cnt, Dep[i] = 1, rt[cnt] = i, dfs_v(i, 0);     while (q--) {       cin >> V >> E;       ++GG;       for (i = 1; i <= V; ++i)         cin >> X[i], X[i] = (X[i] + RR - 1) % n + 1, Y[i] = col[X[i]];       for (i = 1; i <= E; ++i)         cin >> X[V + i * 2 - 1] >> X[V + i * 2],             X[V + i * 2 - 1] = (X[V + i * 2 - 1] + RR - 1) % n + 1,                           X[V + i * 2] = (X[V + i * 2] + RR - 1) % n + 1,                           Ex[i] = col[X[V + i * 2 - 1]],                           Ey[i] = col[X[V + i * 2]];       AL = V + E + E + 1;       for (i = 1; i < AL; ++i) X[i] = col[X[i]];       tot = 1;       for (i = V + 1; i < AL; i += 2) add(X[i], X[i + 1]), add(X[i + 1], X[i]);       sort(X + 1, X + AL, cmp);       X[AL] = X[0] = 300005 - 1;       U = 0;       for (las = 1, i = 2; i <= AL; ++i) {         if (lt[X[i]] != lt[X[i - 1]]) {           st[hd = 1] = rt[lt[X[las]]];           fw[++U] = st[1];           for (j = las; j < i; ++j) {             if (X[j] == X[j - 1]) continue;             lc = lca(st[hd], X[j]);             if (st[hd] != lc) {               while (Dep[lc] < Dep[st[hd - 1]]) {                 add(st[hd - 1], st[hd]);                 add(st[hd], st[hd - 1]);                 Ex[++E] = st[hd - 1], Ey[E] = st[hd];                 --hd;               }               add(lc, st[hd]);               add(st[hd], lc);               Ex[++E] = lc, Ey[E] = st[hd];               --hd;               if (lc != st[hd]) st[++hd] = lc, fw[++U] = lc;             }             if (X[j] != st[hd]) st[++hd] = X[j], fw[++U] = X[j];           }           while (hd > 1)             add(st[hd - 1], st[hd]), add(st[hd], st[hd - 1]),                 Ex[++E] = st[hd - 1], Ey[E] = st[hd], --hd;           las = i;         }       }       for (i = 1; i <= U; ++i) dfn[fw[i]] = 0;       for (i = 2; i <= tot; ++i) vst[i] = 0;       dfs(Y[1]);       for (fl = 0, i = 2; i <= V; ++i)         if (!dfn[Y[i]]) fl = 1;       if (fl) {         cout <<  NO n ;         MEM(Y[1]);         for (i = 1; i <= U; ++i) node[fw[i]] = 0;         continue;       }       for (i = 1; i <= U; ++i) sonn[fw[i]].clear();       for (i = 1; i <= E; ++i)         if (dep[Ex[i]] > dep[Ey[i]]) swap(Ex[i], Ey[i]);       for (i = 1; i <= E; ++i) {         if (Fa[Ey[i]] == Ex[i] && low[Ey[i]] > dfn[Ex[i]]) continue;         sonn[Ex[i]].push_back(Ey[i]);         sonn[Ey[i]].push_back(Ex[i]);       }       MEM(Y[1]);       for (i = 1; i <= U; ++i) node[fw[i]] = 0, color[fw[i]] = -1;       Dfs_(Y[1]);       for (fl = 0, i = 2; i <= V; ++i)         if (!~color[Y[i]]) fl = 1;       if (fl)         cout <<  NO n ;       else         cout <<  YES n , (RR += GG) %= n;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int powe(long long int n, long long int m) {     if (m == 0) return 1;     long long int t = powe(n, m / 2);     if (m % 2 == 0) return (t * t);     return (((t * t)) * n);   }   long long int mpowe(long long int n, long long int m) {     if (m == 0) return 1;     long long int t = powe(n, m / 2);     t %= 1000000007;     if (m % 2 == 0) return (t * t) % 1000000007;     return (((t * t) % 1000000007) * n) % 1000000007;   }   long long int logtwo(long long int n) {     if (n == 1) return 0;     return logtwo(n / 2) + 1;   }   long long int A[200000 + 1];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       long long int n, d, i, sum = 0, dt;       cin >> n >> d;       long long int a[n];       for (long long int i = 0; i < n; i++) cin >> a[i];       ;       long long int c;       for (i = 1; i < n; i++) {         c = i;         dt = d / c;         if (a[i] < dt) {           sum += a[i];           d -= (a[i] * c);         } else {           sum += dt;           break;         }       }       sum += a[0];       cout << sum <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-10;   const double PI = 3.1415926535897932;   const int dir[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1};   const int maxn = 2e5 + 5;   const int MOD = 998244353;   int n, m;   long long A[maxn], invA[maxn];   int w[maxn];   bool vis[maxn];   bool del[maxn];   int B[maxn];   int pos[maxn];   int pre[maxn], nex[maxn];   bool judge(int x) {     if (x == 0 || x == n + 1) return 0;     if (vis[w[x]]) return 0;     return 1;   }   int main() {     int T;     scanf( %d , &T);     while (T--) {       scanf( %d%d , &n, &m);       for (int i = 1; i <= n; i++) {         del[i] = vis[i] = false;         pre[i] = i - 1;         nex[i] = i + 1;       }       for (int i = 1; i <= n; i++) scanf( %d , &w[i]), pos[w[i]] = i;       for (int i = 1; i <= m; i++) {         scanf( %d , &B[i]);         vis[B[i]] = true;       }       long long ans = 1;       for (int i = 1; i <= m; i++) {         int x = pos[B[i]];         int cnt = 0;         cnt += judge(pre[x]);         cnt += judge(nex[x]);         ans = ans * cnt % MOD;         vis[w[x]] = false;         nex[pre[x]] = nex[x];         pre[nex[x]] = pre[x];       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t, n, m, i, j, k, sum;     long long int a[100001];     cin >> n >> k;     bool yo = false;     for (i = 1; i <= k; i++) cin >> a[i];     sum = LONG_LONG_MAX;     for (i = 1; i <= k; i++) {       if (n % a[i] < sum) {         sum = n % a[i];         j = i;         m = n / a[i];       }     }     cout << j <<     << m;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int big = 1e7 + 5;   long long int arr[big];   void minDivsieve() {     for (long long int i = 0; i <= big; ++i) arr[i] = i;     for (long long int i = 2; i <= big; ++i) {       if (arr[i] != i) continue;       for (long long int j = i; j <= big; j += i) arr[j] = min(arr[j], i);     }   }   long long int fac[1000001];   long long int power(long long int x, long long int y, long long int p) {     long long int res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long int modInverse(long long int n, int p) { return power(n, p - 2, p); }   long long int nCrModPFermat(unsigned long long n, long long int r,                               long long int p) {     if (r == 0) return 1;     return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %            p;   }   int dp[2][1048577];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int test = 1;     while (test--) {       int n;       cin >> n;       long long int arr[n];       for (int i = 0; i < n; ++i) cin >> arr[i];       long long int res = 0;       long long int currxor = 0;       dp[1][0] = 1;       for (int i = 0; i < n; ++i) {         currxor = currxor ^ arr[i];         res += dp[i % 2][currxor];         dp[i % 2][currxor] += 1;       }       cout << res;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long x, y, val = 0, q[1000010];   char d[1000010];   void build(int now, int x, int y) {     if (x == y) return;     int mid = (x + y) / 2;     build(2 * now, x, mid);     build(2 * now, mid + 1, y);   }   int main() {     build(1, 1, 1000);     cin >> x >> y;     while (1) {       if (x == y && x > 1) {         cout <<  Impossible ;         return 0;       }       if (x == y && x == 1) {         for (int i = 1; i <= val; i++) {           cout << q[i] << d[i];         }         return 0;       }       if (x > y) {         long long k = (x - 1) / y;         x -= k * y;         q[++val] = k;         d[val] =  A ;         continue;       }       if (x < y) {         long long k = (y - 1) / x;         y -= k * x;         q[++val] = k;         d[val] =  B ;         continue;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e7 + 1;   int32_t main() {     long long n;     cin >> n;     if (n % 2)       cout << 1;     else       cout << 2;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   const double EPS = 1e-6;   const int INF = 0x3f3f3f3f;   inline int readint() {     int sum = 0;     char c = getchar();     bool f = 0;     while (c <  0  || c >  9 ) {       if (c ==  - ) f = 1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     if (f) return -sum;     return sum;   }   inline long long readLL() {     long long sum = 0;     char c = getchar();     bool f = 0;     while (c <  0  || c >  9 ) {       if (c ==  - ) f = 1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     if (f) return -sum;     return sum;   }   const int maxn = 250015;   long long inv[maxn], p[maxn];   void CNumberInit(int MAXNUM, long long mod) {     inv[0] = inv[1] = 1;     p[0] = p[1] = 1;     for (int i = 2; i <= MAXNUM; i++) {       inv[i] = (mod - mod / i) * inv[mod % i] % mod;       p[i] = p[i - 1] * i % mod;     }     for (int i = 2; i <= MAXNUM; i++) inv[i] = inv[i - 1] * inv[i] % mod;   }   long long qmod(long long a, long long b, long long c) {     long long ret = 1;     for (; b; b >>= 1) {       if (b & 1) ret = ret * a % c;       a = a * a % c;     }     return ret;   }   vector<int> megumi;   long long mod;   int k;   long long dp[20][250005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long n;     cin >> n >> k >> mod;     CNumberInit(n + 5, mod);     if (n % 2) {       cout << qmod(k, n, mod) << endl;     } else {       for (int i = 2; i <= n; i += 2) {         if ((i & n) == i) megumi.push_back(i);       }       long long ans = 0;       dp[0][n] = 1;       for (int i = 1; i <= min(17, k); i++) {         int tmpans = 0;         for (int j = 0; j < megumi.size(); j++) {           if (!dp[i - 1][megumi[j]]) continue;           int Megumi = megumi[j] & (megumi[j] - 1);           int step = Megumi ^ megumi[j];           for (int k = step; k <= Megumi; k += step) {             if ((k & Megumi) == k) {               dp[i][k] += dp[i - 1][megumi[j]] * p[megumi[j]] % mod * inv[k] %                           mod * inv[megumi[j] - k] % mod;               dp[i][k] %= mod;             }           }           tmpans = (tmpans + dp[i - 1][megumi[j]]) % mod;         }         ans += tmpans * inv[k - i] % mod;       }       ans = ans * p[k] % mod;       ans = qmod(k, n, mod) - ans;       ans = (ans % mod + mod) % mod;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long n, d;     cin >> n >> d;     pair<long long, long long> s[n];     deque<long long> p;     long long ind, cnt = 1;     for (long long i = 0; i < n; i++) {       cin >> s[i].first;       s[i].second = i;     }     for (long long i = 0; i < n; i++) {       long long tmp;       cin >> tmp;       p.push_back(tmp);     }     s[d - 1].first += p[0];     long long num = s[d - 1].first;     p.pop_front();     for (long long i = 0; i < n; i++) {       if (s[i].second != d - 1 && s[i].first + p[p.size() - 1] <= num) {         s[i].first += p[p.size() - 1];         p.pop_back();       } else if (s[i].second != d - 1) {         s[i].first += p[0];         p.pop_front();       }     }     sort(s, s + n);     for (long long i = n - 1; s[i].second != d - 1; i--) {       if (s[i].first != num) {         cnt++;       }     }     cout << cnt;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long n;     cin >> n;     long long p[n + 1];     p[1] = -1;     for (long long i = 2; i <= n; i++) cin >> p[i];     vector<long long> ans;     long long x = n;     while (p[x] != -1) {       ans.push_back(p[x]);       x = p[x];     }     reverse(ans.begin(), ans.end());     for (long long i : ans) cout << i <<    ;     cout << n << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     while (scanf( %d , &n) != EOF) {       if (n & 1) {         int d = (n - 1) / 2;         printf( %d n , d * 2 + d * d - d);         for (int i = 2; i < n; i += 2) {           for (int j = 2; j < i; j += 2) {             printf( 4 %d %d %d %d n , j, i, j + 1, i + 1);             printf( 4 %d %d %d %d n , j, i, j + 1, i + 1);           }           printf( 3 1 %d %d n , i, i + 1);           printf( 3 1 %d %d n , i, i + 1);         }       } else {         int d = (n - 2) / 2;         printf( %d n , 4 + (d - 1) * 3 + d * d - d);         printf( 3 1 2 3 n );         printf( 3 1 2 4 n );         printf( 3 3 4 1 n );         printf( 3 3 4 2 n );         for (int i = 5; i < n; i += 2) {           for (int j = 1; j < i - 2; j += 2) {             printf( 4 %d %d %d %d n , j, i, j + 1, i + 1);             printf( 4 %d %d %d %d n , j, i, j + 1, i + 1);           }           printf( 4 %d %d %d %d n , i - 2, i, i - 1, i + 1);           printf( 3 %d %d %d n , i - 2, i, i + 1);           printf( 3 %d %d %d n , i - 1, i, i + 1);         }       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const long long int maxN = 5005;   long long int n;   vector<long long int> a, coords, keys;   long long int cnt[maxN];   long long int getKey(long long int x) {     if (x == 0) return 0;     vector<long long int> ans;     if (x < 0) ans.push_back(-1);     x = abs(x);     for (long long int i = 2; i * i <= x; i++) {       if (x % i == 0) {         long long int cnt = 0;         while (x % i == 0) {           cnt++;           x /= i;         }         if (cnt % 2 == 1) ans.push_back(i);       }     }     if (x > 1) ans.push_back(x);     long long int res = 1;     for (auto i : ans) res *= i;     return res;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     a.resize(n);     keys.resize(n);     for (long long int i = 0; i < n; i++) cin >> a[i];     for (long long int i = 0; i < n; i++) {       long long int key = getKey(a[i]);       keys[i] = key;       coords.push_back(key);     }     sort(coords.begin(), coords.end());     coords.erase(unique(coords.begin(), coords.end()), coords.end());     for (long long int i = 0; i < n; i++)       keys[i] =           (lower_bound(coords.begin(), coords.end(), keys[i]) - coords.begin());     vector<long long int> ans(n + 1, 0);     for (long long int i = 0; i < n; i++) {       for (long long int j = 0; j < maxN; j++) cnt[j] = 0;       long long int cntZ = 0, cntD = 0;       for (long long int j = i; j < n; j++) {         long long int key = keys[j];         if (coords[key] == 0)           cntZ++;         else {           cnt[key]++;           if (cnt[key] == 1) cntD++;         }         if (cntD > 0)           ans[cntD]++;         else           ans[cntZ > 0]++;       }     }     for (long long int i = 1; i <= n; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int h[5005], pref[5005], dp[5005];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &h[i]);       pref[i] = pref[i - 1] + h[i];       for (int j = i - 1; j >= 0; j--) {         if (pref[i] - pref[j] >= h[j]) {           dp[i] = dp[j] + i - j - 1;           h[i] = pref[i] - pref[j];           break;         }       }     }     printf( %d n , dp[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 12;   const int M = 305;   const int INF = 1e9;   int n;   int a[N][N];   pair<int, int> pos[N * N];   pair<int, int> dist[M][M];   pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) {     return make_pair(a.first + b.first, a.second + b.second);   }   int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};   int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};   bool in(int first, int second) {     return (0 <= first && first < n && 0 <= second && second < n);   }   int get(int first, int second, int p) { return first * n * 3 + second * 3 + p; }   pair<int, int> dp[N * N][3];   int main() {     scanf( %d , &n);     for (int i = 0; i < int(n); i++)       for (int j = 0; j < int(n); j++) {         scanf( %d , &a[i][j]);         --a[i][j];         pos[a[i][j]] = make_pair(i, j);       }     for (int i = 0; i < int(M); i++)       for (int j = 0; j < int(M); j++) dist[i][j] = make_pair(INF, INF);     for (int i = 0; i < int(M); i++) dist[i][i] = make_pair(0, 0);     for (int first = 0; first < int(n); first++)       for (int second = 0; second < int(n); second++) {         for (int i = 0; i < int(8); i++) {           int nx = first + dx[i];           int ny = second + dy[i];           if (in(nx, ny))             dist[get(first, second, 0)][get(nx, ny, 0)] = make_pair(1, 0);         }         for (int i = -n + 1; i <= n - 1; ++i) {           int nx = first + i;           int ny = second + i;           if (in(nx, ny))             dist[get(first, second, 1)][get(nx, ny, 1)] = make_pair(1, 0);           ny = second - i;           if (in(nx, ny))             dist[get(first, second, 1)][get(nx, ny, 1)] = make_pair(1, 0);         }         for (int i = 0; i < int(n); i++) {           int nx = first;           int ny = i;           dist[get(first, second, 2)][get(nx, ny, 2)] = make_pair(1, 0);           nx = i;           ny = second;           dist[get(first, second, 2)][get(nx, ny, 2)] = make_pair(1, 0);         }         for (int i = 0; i < int(3); i++)           for (int j = 0; j < int(3); j++) {             if (i != j) {               dist[get(first, second, i)][get(first, second, j)] =                   make_pair(1, 1);             }           }       }     for (int k = 0; k < int(M); k++)       for (int i = 0; i < int(M); i++)         for (int j = 0; j < int(M); j++)           dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);     for (int i = 0; i < int(N * N); i++)       for (int j = 0; j < int(3); j++) dp[i][j] = make_pair(INF, INF);     dp[0][0] = dp[0][1] = dp[0][2] = make_pair(0, 0);     for (int i = 0; i < int(n * n - 1); i++)       for (int j = 0; j < int(3); j++)         for (int k = 0; k < int(3); k++)           dp[i + 1][k] =               min(dp[i + 1][k],                   dp[i][j] + dist[get(pos[i].first, pos[i].second, j)]                                  [get(pos[i + 1].first, pos[i + 1].second, k)]);     pair<int, int> ans = make_pair(INF, INF);     ans = min(ans, dp[n * n - 1][0]);     ans = min(ans, dp[n * n - 1][1]);     ans = min(ans, dp[n * n - 1][2]);     printf( %d %d n , ans.first, ans.second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   long long a[N], sum[N];   long long int gcd(long long int a, long long int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   int main() {     int n;     scanf( %d , &n);     int i;     long long ans = 0;     for (i = 1; i <= n; i++) {       scanf( %I64d , &a[i]);       ans = ans + a[i];     }     sort(a + 1, a + n + 1);     for (i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];     for (i = 1; i <= n; i++) {       int l = 1, r = n;       int idx = -1;       while (l <= r) {         int mid = (l + r) / 2;         if (a[mid] <= a[i]) {           l = mid + 1;           idx = mid;         } else           r = mid - 1;       }       ans = ans + (idx)*1LL * a[i] - sum[idx] + sum[n] - sum[idx] -             (n - idx) * 1LL * a[i];     }     long long g = gcd(ans, 1LL * n);     ans = ans / g;     n = n / g;     printf( %I64d %d n , ans, n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 3e5 + 5;   long long a[MAXN], kkorz[MAXN], bit[MAXN];   long long n;   long long sum(long long idx) {     long long res = 0;     for (; idx > 0; idx -= (idx & (-idx))) res += bit[idx];     return res;   }   void upd(long long idx, long long x) {     for (; idx <= n; idx += (idx & (-idx))) bit[idx] += x;   }   long long countInv() {     vector<pair<long long, long long>> v;     for (long long i = 0; i < n; i++) v.push_back(make_pair(kkorz[i], i));     sort(v.begin(), v.end());     for (long long i = 0; i < n; i++) kkorz[v[i].second] = i + 1;     long long res = 0;     for (long long i = 1; i <= n; i++) bit[i] = 0;     for (long long i = n - 1; i >= 0; i--) {       res += sum(kkorz[i] - 1);       upd(kkorz[i], 1);     }     return res;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (long long i = 0; i < n; i++) cin >> a[i];     long long maxbit = 32 - __builtin_clz(*max_element(a, a + n));     for (long long i = 0; i < n; i++) kkorz[i] = a[i];     long long ans = countInv(), minx = 0, x = 0;     for (long long b = 0; b <= maxbit; b++) {       long long tmp = x | (1 << b);       for (long long i = 0; i < n; i++) kkorz[i] = a[i] ^ tmp;       long long i1 = countInv();       if (i1 < ans) {         x |= (1 << b);         ans = i1;         minx = x;       }     }     cout << ans <<     << minx <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 7;   const int inf = INT_MAX;   const long long inff = 1e18;   const long long mod = 1e9 + 7;   vector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1};   vector<int> dy = {1, 1, 0, -1, -1, -1, 0, 1};   int can[507][507];   set<pair<int, int> > second[32][9];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     int a[35];     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int j = 250; j <= 250 + a[1] - 1; j++) can[250][j] = 1;     second[1][0].insert(make_pair(250, 250 + a[1] - 1));     for (int i = 1; i < n; i++) {       for (int j = 0; j < 8; j++) {         for (pair<int, int> k : second[i][j]) {           for (int l = 1; l <= a[i + 1]; l++) {             can[k.first + dx[(j + 1) % 8] * l][k.second + dy[(j + 1) % 8] * l] =                 1;             can[k.first + dx[(j + 7) % 8] * l][k.second + dy[(j + 7) % 8] * l] =                 1;           }           second[i + 1][(j + 1) % 8].insert(               make_pair(k.first + dx[(j + 1) % 8] * a[i + 1],                         k.second + dy[(j + 1) % 8] * a[i + 1]));           second[i + 1][(j + 7) % 8].insert(               make_pair(k.first + dx[(j + 7) % 8] * a[i + 1],                         k.second + dy[(j + 7) % 8] * a[i + 1]));         }       }     }     int ans = 0;     for (int i = 0; i <= 500; i++)       for (int j = 0; j <= 500; j++) ans += can[i][j];     cout << ans <<   n ;   }


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


#include <bits/stdc++.h>   using namespace std;   int a[100005], n;   int main() {     while (cin >> n) {       for (int i = 1; i <= n; i++) {         cin >> a[i];         while (a[i] % 2 == 0) a[i] /= 2;         while (a[i] % 3 == 0) a[i] /= 3;       }       int i;       for (i = 2; i <= n; i++)         if (a[i] != a[1]) break;       if (i > n)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t, n, m, i, j, k, sum, cnt, temp, ans;     cin >> t;     while (t--) {       long long n;       cin >> n;       if (n % 3 == 0) {         cout << n / 3 <<     << n / 3 <<   n ;       } else if (n % 3 == 1) {         cout << n / 3 + 1 <<     << n / 3 <<   n ;       } else {         cout << n / 3 <<     << n / 3 + 1 <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[15], b[15];   int cnt[15];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= m; i++) {       int k;       cin >> k;       cnt[k] = 1;     }     vector<int> ans;     for (int i = 1; i <= n; i++) {       if (cnt[a[i]] == 1) {         ans.push_back(a[i]);       }     }     for (auto e : ans) cout << e <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     char ch;     int sig = 1;     for (ch = getchar(); ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) sig = -1;     int x = ch -  0 ;     for (ch = getchar(); ch >=  0  && ch <=  9 ; ch = getchar())       x = x * 10 + ch -  0 ;     return x * sig;   }   const int N = 1e6 + 5;   const long long Mo = 1e9 + 7;   struct P {     long long x, y;     P(long long _x = 0, long long _y = 0) {       x = _x;       y = _y;     }     friend P operator+(P a, P b) { return P(a.x + b.x, a.y + b.y); }     friend P operator-(P a, P b) { return P(a.x - b.x, a.y - b.y); }     friend long long operator^(P a, P b) { return a.x * b.y - a.y * b.x; }   } p[N];   int n;   long long S, sq[N], sx[N], sy[N];   int main() {     n = read();     for (int i = 1; i <= n; i++) p[i].x = read(), p[i].y = read();     for (int i = 1; i <= n; i++) p[i + n] = p[i];     for (int i = 1; i <= n; i++) S += p[i + 1] ^ p[i];     long long ans = 0, sum = 0;     for (int i = 1; i <= n * 2; i++) {       (sum += p[i] ^ (p[i - 1])) %= Mo;       sq[i] = (sq[i - 1] + sum) % Mo;       sx[i] = (sx[i - 1] + p[i].x) % Mo;       sy[i] = (sy[i - 1] + p[i].y) % Mo;     }     long long ns = S % Mo;     int pos = 3;     sum = 0;     for (int i = 2; i <= pos; i++) sum += p[i] ^ p[i - 1];     long long now = 0;     for (int i = 1; i <= n; i++) {       (now += p[i + 1] ^ p[i]) %= Mo;       sum -= p[i + 1] ^ p[i];       if (pos < i + 2) pos++, sum += p[pos] ^ p[pos - 1];       while (pos <= i + n - 2) {         long long ar = sum;         ar -= p[i] ^ p[i + 1];         ar += p[i] ^ p[pos];         if (ar < S - ar)           pos++, sum += p[pos] ^ p[pos - 1];         else           break;       }       int ls = i + 2, rs = pos - 1;       long long dx = (sx[rs] - sx[ls - 1]) % Mo, dy = (sy[rs] - sy[ls - 1]) % Mo;       if (ls <= rs) {         (ans += ns * (rs - ls + 1) % Mo) %= Mo;         (ans -= (sq[rs] - sq[ls - 1]) % Mo * 2 % Mo) %= Mo;         (ans += now * 2 % Mo * (rs - ls + 1) % Mo) %= Mo;         (ans += (p[i] ^ p[i + 1]) % Mo * (rs - ls + 1) % Mo * 2 % Mo) %= Mo;         (ans -= (p[i] ^ P(dx, dy)) % Mo * 2 % Mo) %= Mo;       }       ls = pos, rs = i + n - 2, dx = (sx[rs] - sx[ls - 1]) % Mo,       dy = (sy[rs] - sy[ls - 1]) % Mo;       if (ls <= rs) {         (ans -= ns * (rs - ls + 1) % Mo) %= Mo;         (ans += (sq[rs] - sq[ls - 1]) % Mo * 2 % Mo) %= Mo;         (ans -= now * 2 % Mo * (rs - ls + 1) % Mo) %= Mo;         (ans -= (p[i] ^ p[i + 1]) % Mo * (rs - ls + 1) % Mo * 2 % Mo) %= Mo;         (ans += (p[i] ^ P(dx, dy)) % Mo * 2 % Mo) %= Mo;       }     }     ans = ans * (Mo + 1) / 2 % Mo;     printf( %lld n , (ans + Mo) % Mo);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char str[8][10];     int n, k, i, j, l, mx, mn, mind;     int p[] = {1, 2, 3, 4, 5, 6, 7, 8};     cin >> n >> k;     for (i = 0; i < n; i++) cin >> str[i];     mx = 0;     mn = 1000000000;     mind = mn - mx;     do {       int arr[8] = {0};       mx = 0;       mn = 1000000000;       for (i = 0; i < n; i++) {         for (j = 0; j < k; j++) {           arr[i] *= 10;           arr[i] += (int)(str[i][p[j] - 1] -  0 );         }         if (arr[i] > mx) mx = arr[i];         if (arr[i] < mn) mn = arr[i];       }       mind = min(mind, (mx - mn));     } while (std::next_permutation(p, p + k));     cout << mind << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int a;     cin >> a;     string s;     cin >> s;     map<int, int> Freq;     for (int i = 0; i < s.size(); i++) {       int sum = 0;       bool zero = false;       for (int j = i; j < s.size(); j++) {         sum += (s[j] -  0 );         Freq[sum]++;       }     }     long long ans = 0;     if (a == 0) {       for (int i = 0; i < s.size(); i++) {         int sum = 0;         for (int j = i; j < s.size(); j++) {           sum += s[j] -  0 ;           if (sum == 0)             ans += (s.size() * (s.size() + 1)) / 2;           else             ans += Freq[0];         }       }       cout << ans;       return 0;     }     for (auto &x : Freq) {       if (x.first && a % x.first == 0)         ans += (long long)Freq[a / x.first] * x.second;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   typedef struct {     double x;     double y;     double r;   } circle;   typedef struct {     double x;     double y;   } pt;   int x[3];   int y[3];   int r[3];   pt np(double x, double y) {     pt q;     q.x = x;     q.y = y;     return q;   }   double abs_d(double d1) {     if (d1 > 0) return d1;     return -d1;   }   bool flag2 = true;   pt p;   double dist(pt p1, pt p2) {     return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));   }   void circumcenter(pt p1, pt p2, pt p3) {     double K = abs_d(.5 * (p1.x * p2.y + p2.x * p3.y + p3.x * p1.y - p1.y * p2.x -                            p2.y * p3.x - p3.y * p1.x));     if (K < .01) {       flag2 = false;       return;     }     double a = dist(p1, p2);     double b = dist(p1, p3);     double c = dist(p2, p3);     double R = a * b * c / 4 / K;     double d = sqrt(R * R - (a / 2) * (a / 2));     double nx = (p1.x + p2.x) / 2;     double ny = (p1.y + p2.y) / 2;     double mx = p1.y - p2.y;     double my = p2.x - p1.x;     double len = sqrt(mx * mx + my * my);     mx /= len;     my /= len;     p.x = nx + d * mx;     p.y = ny + d * my;     if (abs_d(dist(p, p3) - R) > .001) {       p.x = nx - d * mx;       p.y = ny - d * my;     }   }   bool flag = true;   pt pos1;   pt pos2;   void intersection(circle c1, circle c2) {     pt p;     p.x = -1e9;     p.y = -1e9;     double d =         sqrt((c1.x - c2.x) * (c1.x - c2.x) + (c1.y - c2.y) * (c1.y - c2.y));     if (d > c1.r + c2.r) flag = false;     if (abs_d(c1.r - c2.r) > d) flag = false;     if (flag == false) return;     double a = d;     double b = c1.r;     double c = c2.r;     double s = (a + b + c) / 2;     double K = sqrt(s * (s - a) * (s - b) * (s - c));     double h = 2 * K / a;     double d_par = sqrt(b * b - h * h);     if (b * b + d * d < c * c) d_par = -d_par;     double nx = c1.x + (c2.x - c1.x) * d_par / d;     double ny = c1.y + (c2.y - c1.y) * d_par / d;     double oppx = c2.y - c1.y;     double oppy = c1.x - c2.x;     double len = sqrt(oppx * oppx + oppy * oppy);     oppx /= len;     oppy /= len;     pos1.x = nx + h * oppx;     pos1.y = ny + h * oppy;     pos2.x = nx - h * oppx;     pos2.y = ny - h * oppy;   }   int main() {     for (int i = 0; i < 3; i++) {       scanf( %d %d %d , &x[i], &y[i], &r[i]);     }     pt p1 = np(x[0], y[0]);     pt p2 = np(x[1], y[1]);     pt p3 = np(x[2], y[2]);     if (r[0] == r[1] && r[1] == r[2]) {       circumcenter(p1, p2, p3);       if (flag2) {         printf( %.5f %.5f , p.x, p.y);       }     } else {       circle c1, c2, c3;       if (r[0] != r[1]) {         double x1 = (r[1] * p1.x + r[0] * p2.x) / (r[1] + r[0]);         double y1 = (r[1] * p1.y + r[0] * p2.y) / (r[1] + r[0]);         double x2 = (r[1] * p1.x - r[0] * p2.x) / (r[1] - r[0]);         double y2 = (r[1] * p1.y - r[0] * p2.y) / (r[1] - r[0]);         c1.x = (x1 + x2) / 2;         c1.y = (y1 + y2) / 2;         c1.r = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2;       }       if (r[0] != r[2]) {         double x1 = (r[2] * p1.x + r[0] * p3.x) / (r[2] + r[0]);         double y1 = (r[2] * p1.y + r[0] * p3.y) / (r[2] + r[0]);         double x2 = (r[2] * p1.x - r[0] * p3.x) / (r[2] - r[0]);         double y2 = (r[2] * p1.y - r[0] * p3.y) / (r[2] - r[0]);         c2.x = (x1 + x2) / 2;         c2.y = (y1 + y2) / 2;         c2.r = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2;       }       if (r[1] != r[2]) {         double x1 = (r[2] * p2.x + r[1] * p3.x) / (r[2] + r[1]);         double y1 = (r[2] * p2.y + r[1] * p3.y) / (r[2] + r[1]);         double x2 = (r[2] * p2.x - r[1] * p3.x) / (r[2] - r[1]);         double y2 = (r[2] * p2.y - r[1] * p3.y) / (r[2] - r[1]);         c3.x = (x1 + x2) / 2;         c3.y = (y1 + y2) / 2;         c3.r = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2;       }       if (r[0] == r[1]) {         intersection(c2, c3);       } else if (r[0] == r[2]) {         intersection(c1, c3);       } else {         intersection(c1, c2);       }       if (flag) {         if (dist(pos1, p1) > dist(pos2, p1)) {           printf( %.5f %.5f , pos2.x, pos2.y);         } else {           printf( %.5f %.5f , pos1.x, pos1.y);         }       }     }     fflush(stdin);     getchar();   }


#include <bits/stdc++.h>   using namespace std;   long long pf(int v) { return (long long)1 * v * v; }   struct sd {     int a, b;     bool operator<(const sd& w) const { return a < w.a; }   } x[100005];   long long maxx(long long a, long long b) { return a > b ? a : b; }   long long minx(long long a, long long b) { return a < b ? a : b; }   int n, fr[2][100005], ba[2][100005];   long long ans = 1e18;   bool jud(long long v) {     int mid = sqrt(v);     for (int i = 1; i <= n; i++) {       if (x[i].a > 0) break;       int l = 1, r = n, R = minx(mid + x[i].a, -x[i].a);       while (l < r) {         int mid = ((l + r) >> 1) + 1;         if (x[mid].a <= R)           l = mid;         else           r = mid - 1;       }       if (i == 1 && l == n) return 1;       int ymin = minx(fr[0][i], ba[0][l]), ymax = maxx(fr[1][i], ba[1][l]);       if (maxx(maxx(pf(ymax), pf(ymin)) + pf(x[i].a), pf(ymax - ymin)) <= v)         return 1;     }     return 0;   }   void work() {     sort(x + 1, x + 1 + n);     fr[0][1] = 1e9;     fr[1][1] = -1e9;     ba[0][n] = 1e9;     ba[1][n] = -1e9;     for (int i = 2; i <= n; i++)       fr[0][i] = minx(fr[0][i - 1], x[i - 1].b),       fr[1][i] = maxx(fr[1][i - 1], x[i - 1].b);     for (int i = n - 1; i >= 1; i--)       ba[0][i] = minx(ba[0][i + 1], x[i + 1].b),       ba[1][i] = maxx(ba[1][i + 1], x[i + 1].b);     long long l = 0, r = 1e18;     while (l < r) {       long long mid = (l + r) / 2;       if (jud(mid))         r = mid;       else         l = mid + 1;     }     ans = minx(ans, l);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d%d , &x[i].a, &x[i].b);     work();     for (int i = 1; i <= n; i++) x[i].a *= -1;     work();     int ymax = -1e9, ymin = 1e9;     for (int i = 1; i <= n; i++)       ymax = maxx(ymax, x[i].b), ymin = minx(ymin, x[i].b);     ans = minx(ans, pf(ymax - ymin));     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int x1, x2, y1, y2;       long long int k;       cin >> x1 >> y1 >> x2 >> y2;       if (x1 == x2 || y1 == y2)         k = abs(x1 - x2) + abs(y1 - y2);       else         k = 2 + abs(x1 - x2) + abs(y1 - y2);       cout << k << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   string S[100001];   int cnt[26];   int from[26];   int find(int ind, char C) {     for (int i = 0; i < S[ind].length(); i++)       if (S[ind][i] == C) return i;     return -1;   }   void merge(int I, int J, char C) {     int indI = find(I, C);     int indJ = find(J, C);     int lenI = S[I].length() - indI;     int lenJ = S[J].length() - indJ;     int L = min(lenI, lenJ);     if (min(indI, indJ) > 0 || S[I].substr(indI, L) != S[J].substr(indJ, L)) {       cout <<  NO  <<   n ;       exit(0);     }     string s;     if (indI > indJ) {       s = S[I];       if (indJ + L < (int)S[J].length())         s += S[J].substr(indJ + L, (int)S[J].length() - (indJ + L));     } else {       s = S[J];       if (indI + L < (int)S[I].length())         s += S[I].substr(indI + L, (int)S[I].length() - (indI + L));     }     S[J] = s;     S[I] =  ! ;   }   int main() {     cin >> N;     for (int i = 0; i < N; i++) {       cin >> S[i];       for (int j = 0; j < 26; j++) cnt[j] = 0;       for (int j = 0; j < S[i].length(); j++) cnt[S[i][j] -  a ]++;       for (int j = 0; j < 26; j++)         if (cnt[j] > 1) {           cout <<  NO  <<   n ;           return 0;         }     }     for (int i = 0; i < 26; i++) from[i] = -1;     for (int i = 0; i < N; i++) {       for (int j = 0; j < S[i].length(); j++) {         if (from[S[i][j] -  a ] == -1)           from[S[i][j] -  a ] = i;         else if (from[S[i][j] -  a ] != i) {           merge(from[S[i][j] -  a ], i, S[i][j]);           int temp = from[S[i][j] -  a ];           for (int k = 0; k < 26; k++)             if (from[k] == temp) from[k] = i;         }       }     }     vector<string> v;     for (int i = 0; i < N; i++)       if (S[i] !=  ! ) v.push_back(S[i]);     sort(v.begin(), v.end());     string ans =   ;     for (auto s : v) ans += s;     for (int i = 0; i < 26; i++) cnt[i] = 0;     for (int i = 0; i < ans.length(); i++) cnt[ans[i] -  a ]++;     for (int i = 0; i < 26; i++)       if (cnt[i] > 1) {         cout <<  NO  <<   n ;         return 0;       }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int vis[400000], cur, path[500000], block[500000], x, y, n, m, q, k, s, t,       seen[400010], ans[400010];   vector<int> g[4000];   map<pair<int, int>, vector<pair<int, int> > > M;   void color(int x) {     if (block[x]) return;     block[x] = 1;     for (int i = 0; i < g[x].size(); i++) color(g[x][i]);   }   void dfs(int x, int dep) {     if (vis[x]) {       int now = dep - 1;       while (path[now] != x && now > 0) color(path[now--]);       color(path[now]);     }     if (seen[x]) return;     path[dep] = x;     vis[x] = seen[x] = 1;     if (M.find(make_pair(cur, x)) != M.end()) {       if (!block[x]) {         vector<pair<int, int> > tmp = M[make_pair(cur, x)];         for (int i = 0; i < tmp.size(); i++)           if (tmp[i].first <= dep) ans[tmp[i].second] = path[tmp[i].first];       }     }     for (int i = 0; i < g[x].size(); i++) dfs(g[x][i], dep + 1);     vis[x] = 0;   }   int main() {     scanf( %d%d%d , &n, &m, &q);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &x, &y);       g[x].push_back(y);     }     for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end());     for (int i = 1; i <= q; i++) {       scanf( %d%d%d , &s, &t, &k);       M[make_pair(s, t)].push_back(make_pair(k, i));     }     memset(ans, 0xff, sizeof(ans));     for (int i = 1; i <= n; i++) {       cur = i;       memset(seen, 0, sizeof(seen));       memset(block, 0, sizeof(block));       dfs(i, 1);     }     for (int i = 1; i <= q; i++) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> in[100005];   pair<int, int> id[100005];   int bit[100005];   int lastNum[100005];   int color[100005];   void solve() {     int n, m;     scanf( %d %d  , &n, &m);     long long sum = 0;     for (int i = 1; i <= n; ++i) {       int num;       scanf( %d  , &num);       sum += num;       while (num--) {         int x;         scanf( %d  , &x);         in[i].insert(x);       }       id[i] = {in[i].size(), i};     }     int x = sum / n;     int lim = (n - sum % n);     int t = 0;     for (int i = 1; i <= n; ++i) {       if (i <= lim) {         lastNum[i] = x;       } else {         lastNum[i] = x + 1;       }       t += lastNum[i];     }     assert(t == sum);     sort(id + 1, id + n + 1);     int l = 1, r = n;     vector<pair<pair<int, int>, int> > out;     bool lin = false, rin = false;     set<int> swapNums;     while (l < r) {       int lid = id[l].second;       int rid = id[r].second;       if (in[lid].size() == lastNum[l]) {         if (lin) {           for (int x : in[lid]) {             assert(color[x] & 1);             color[x] ^= 1;             if (color[x] == 2) {               assert(!swapNums.count(x));               swapNums.insert(x);             }           }         }         ++l;         lin = false;         continue;       }       if (in[rid].size() == lastNum[r]) {         if (rin) {           for (int x : in[rid]) {             assert(color[x] & 2);             if (color[x] == 2) {               assert(swapNums.count(x));               swapNums.erase(x);             }             color[x] ^= 2;           }         }         --r;         rin = false;         continue;       }       if (!lin) {         for (int x : in[lid]) {           color[x] |= 1;           if (color[x] == 3) {             assert(swapNums.count(x));             swapNums.erase(x);           }         }         lin = true;       }       if (!rin) {         for (int x : in[rid]) {           color[x] |= 2;           if (color[x] == 2) {             assert(!swapNums.count(x));             swapNums.insert(x);           }         }         rin = true;       }       assert(in[lid].size() < lastNum[l]);       assert(in[rid].size() > lastNum[r]);       while (in[lid].size() != lastNum[l] && in[rid].size() != lastNum[r]) {         assert(swapNums.size() > 0);         int g = *swapNums.begin();         swapNums.erase(g);         in[lid].insert(g);         in[rid].erase(g);         out.push_back({{rid, lid}, g});         color[g] = 1;       }     }     printf( %d n , out.size());     for (pair<pair<int, int>, int> tmp : out) {       printf( %d %d %d n , tmp.first.first, tmp.first.second, tmp.second);     }   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     queue<int> left;     queue<int> right;     int n;     cin >> n;     char x;     for (int i = 0; i < n; ++i) {       cin >> x;       if (x ==  D )         left.push(i);       else         right.push(i);     }     while (!left.empty() && !right.empty()) {       if (left.front() < right.front())         left.push(left.front() + n);       else         right.push(right.front() + n);       right.pop();       left.pop();     }     if (left.empty())       cout <<  R ;     else       cout <<  D ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int a, b, cnt = 0, sum = 0, i, j, hand, flag = 0, c, n, m, k, t, res,               maxy = 0, miny = 12345678910;     cin >> n;     string str;     getline(cin, str);     int len = str.size();     if (str[len - 1] ==  k ) {       if (n == 5 || n == 6)         printf( 53 n );       else         printf( 52 );     } else if (str[len - 1] ==  h ) {       if (n == 30)         printf( 11 );       else if (n < 30)         printf( 12 );       else         printf( 7 );     }   }


#include <bits/stdc++.h>   const long long INF = LLONG_MAX;   const long long NINF = LLONG_MIN;   const long long MAX = 1e6 + 5;   const long long MOD = 1e9 + 7;   using namespace std;   inline long long gcd(long long a, long long b) {     long long t;     if (a < b) {       t = a;       a = b;       b = t;     }     while (b != 0) {       t = a;       a = b;       b = t % b;     }     return a;   }   inline long long mpow(long long a, long long b) {     long long ans = 1;     while (b != 0) {       if (b & 1) {         ans = ans * a;       }       a = a * a;       b >>= 1;     }     return ans;   }   inline long long mlog2(long long n) {     long long c = 0;     c += 64 - __builtin_clzll(n);     if (__builtin_popcountll(n) == 1) {       c--;     }     return c;   }   void troll() {     long long qwe[100000];     for (long long i = 0; i < 1; i++)       ;   }   long long ass[MAX];   long long ssa[MAX];   int main() {     troll();     string str1, str2;     cin >> str1 >> str2;     long long count = 0;     for (long long i = 0; i < str1.size();) {       long long flag = 1;       if (str1[i] == str2[0]) {         long long j = 1;         for (j = 1; j < str2.size(); j++) {           if (str2[j] != str1[i + j]) {             flag = 0;             break;           }         }         if (!flag) {           flag = 1;           i++;         } else {           i += j;           count++;         }       } else {         i++;       }     }     troll();     cout << count <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   int main() {     long long t, w, b;     cin >> t >> w >> b;     long long cas = t / (w / gcd(w, b)) / b;     long long exa = t - w / gcd(w, b) * b * cas;     long long minimum = min(w, b);     long long res = minimum * cas + min(minimum, exa + 1) - 1;     long long divition = gcd(res, t);     res /= divition;     t /= divition;     cout << res <<  /  << t << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn(5e5 + 5);   const long long inf(1e18);   struct Qry {     int l, r, id;   };   int n, q, dfn[maxn], idx, ed[maxn];   long long mn[maxn << 2], tag[maxn << 2], ans[maxn];   vector<pair<int, int> > edge[maxn];   vector<Qry> qry[maxn];   void Update(int x, int l, int r, int p, long long v) {     if (l == r)       mn[x] = v + tag[x];     else {       int mid = (l + r) >> 1;       p <= mid ? Update(x << 1, l, mid, p, v)                : Update(x << 1 | 1, mid + 1, r, p, v);       mn[x] = min(mn[x << 1], mn[x << 1 | 1]) + tag[x];     }   }   void Modify(int x, int l, int r, int ql, int qr, long long v) {     if (ql <= l && qr >= r)       tag[x] += v, mn[x] += v;     else {       int mid = (l + r) >> 1;       if (ql <= mid) Modify(x << 1, l, mid, ql, qr, v);       if (qr > mid) Modify(x << 1 | 1, mid + 1, r, ql, qr, v);       mn[x] = min(mn[x << 1], mn[x << 1 | 1]) + tag[x];     }   }   long long Query(int x, int l, int r, int ql, int qr) {     if (ql <= l && qr >= r) return mn[x];     int mid = (l + r) >> 1;     long long ret = inf;     if (ql <= mid) ret = Query(x << 1, l, mid, ql, qr);     if (qr > mid) ret = min(ret, Query(x << 1 | 1, mid + 1, r, ql, qr));     return ret + tag[x];   }   void Dfs1(int u, long long d) {     dfn[u] = ++idx;     if (!edge[u].size()) Update(1, 1, n, idx, d);     for (auto v : edge[u]) Dfs1(v.first, d + v.second);     ed[u] = idx;   }   void Dfs2(int u) {     for (auto ques : qry[u]) ans[ques.id] = Query(1, 1, n, ques.l, ques.r);     for (auto v : edge[u]) {       Modify(1, 1, n, 1, n, v.second);       Modify(1, 1, n, dfn[v.first], ed[v.first], -(v.second << 1));       Dfs2(v.first);       Modify(1, 1, n, dfn[v.first], ed[v.first], v.second << 1);       Modify(1, 1, n, 1, n, -v.second);     }   }   int main() {     memset(mn, 63, sizeof(mn));     int p, w, i, l, r;     scanf( %d%d , &n, &q);     for (i = 2; i <= n; ++i) {       scanf( %d%d , &p, &w);       edge[p].push_back(make_pair(i, w));     }     for (i = 1; i <= q; ++i) {       scanf( %d%d%d , &p, &l, &r);       qry[p].push_back((Qry){l, r, i});     }     Dfs1(1, 0), Dfs2(1);     for (i = 1; i <= q; ++i) printf( %lld n , ans[i]);     return 0;   }