#include <bits/stdc++.h>   using namespace std;   void boost() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   const int N = 2e5 + 5;   int arr[N], ans[N];   vector<int> adj[N];   vector<int> factor[2][N];   bool avail[N];   void dfs(int u, int p, int d) {     if (u != 1) {       for (auto x : factor[0][p])         if (arr[u] % x == 0)           factor[0][u].push_back(x);         else           factor[1][u].push_back(x);       for (auto x : factor[1][p])         if (arr[u] % x == 0) factor[1][u].push_back(x);     }     if (d == 2) {       for (int i = 1; i * i <= arr[u]; i++) {         if (arr[u] % i == 0) {           if (!avail[i]) factor[1][u].push_back(i);         }         if (i * i != arr[u] && (arr[u] % (arr[u] / i)) == 0) {           if (!avail[arr[u] / i]) {             factor[1][u].push_back(arr[u] / i);           }         }       }     }     for (auto x : factor[0][u]) ans[u] = max(ans[u], x);     for (auto x : factor[1][u]) ans[u] = max(ans[u], x);     for (auto v : adj[u]) {       if (v != p) dfs(v, u, d + 1);     }   }   void solve() {     int n;     cin >> n;     for (int i = 1; i < n + 1; ++i) {       cin >> arr[i];     }     int u, v;     for (int i = 0; i < n - 1; ++i) {       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     for (int i = 1; i * i <= arr[1]; i++) {       if (arr[1] % i == 0) {         factor[0][1].push_back(i);         avail[i] = true;       }       if (i * i != arr[1] && (arr[1] % (arr[1] / i)) == 0) {         factor[0][1].push_back(arr[1] / i);         avail[arr[1] / i] = true;       }     }     dfs(1, 0, 1);     for (int i = 1; i < n + 1; ++i) {       cout << ans[i] <<    ;     }     return;   }   int main() {     boost();     int tc = 1;     while (tc--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, g, t;     cin >> n >> k;     char a[n];     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       if (a[i] ==  G ) g = i;       if (a[i] ==  T ) t = i;     }     for (int i = 0; i < n; i++) {       if (g < t) {         while (g < n && a[g] !=  #  && a[g] !=  T ) g = g + k;         if (a[g] ==  T ) {           cout <<  YES  << endl;           return 0;         } else if (a[g] ==  #  || g >= n) {           cout <<  NO  << endl;           return 0;         }       } else if (g > t) {         while (g >= 0 && a[g] !=  #  && a[g] !=  T ) g = g - k;         if (a[g] ==  T ) {           cout <<  YES  << endl;           return 0;         } else if (a[g] ==  #  || g < 0) {           cout <<  NO  << endl;           return 0;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     string s;     cin >> s;     int n = s.size();     bool dp[n + 1][8];     memset(dp, false, sizeof(dp));     string ans[8];     for (int i = 1; i < n + 1; i++) {       string temp_ans[8];       dp[i][(s[i - 1] -  0 ) % 8] = true;       temp_ans[(s[i - 1] -  0 ) % 8] = s[i - 1];       for (int j = 0; j < 8; j++) {         if (dp[i - 1][j]) {           int temp = (10 * j + (s[i - 1] -  0 )) % 8;           dp[i][temp] = true;           temp_ans[temp] = ans[j] + s[i - 1];           dp[i][j] = true;           temp_ans[j] = ans[j];         }       }       for (int j = 0; j < 8; j++) ans[j] = temp_ans[j];     }     if (dp[n][0]) {       cout <<  YES n ;       cout << ans[0] << endl;     } else       cout <<  NO n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int b, l, d;     cin >> b >> l >> d;     long long int m = b;     if (l > m) {       m = l;     }     if (d > m) {       m = d;     }     long long int miss = 0;     if (m - 1 - b > 0) {       miss += m - 1 - b;     }     if (m - 1 - l > 0) {       miss += m - 1 - l;     }     if (m - 1 - d > 0) {       miss += m - 1 - d;     }     cout << miss;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > a[100500];   int d[100500];   int main() {     int n, m, cap;     scanf( %d%d%d , &n, &m, &cap);     cap--;     for (int i = 0; i < (m); i++) {       int x, y, w;       scanf( %d%d%d , &x, &y, &w);       x--, y--;       a[x].push_back(pair<int, int>(y, w));       a[y].push_back(pair<int, int>(x, w));     }     int l;     scanf( %d , &l);     const int INF = 1100000000;     for (int i = 0; i < (n); i++) d[i] = INF;     d[cap] = 0;     set<pair<int, int> > s;     s.insert(pair<int, int>(d[cap], cap));     while (!s.empty()) {       int v = s.begin()->second;       s.erase(s.begin());       for (typeof((a[v]).begin()) it = (a[v]).begin(); it != (a[v]).end(); ++it) {         int u = it->first;         int w = it->second;         if (d[u] >= INF || d[u] >= d[v] + w) {           if (d[u] < INF) s.erase(s.find(pair<int, int>(d[u], u)));           d[u] = d[v] + w;           s.insert(pair<int, int>(d[u], u));         }       }     }     int ans = 0;     for (int i = 0; i < (n); i++)       for (typeof((a[i]).begin()) it = (a[i]).begin(); it != (a[i]).end(); ++it) {         int u = i;         int v = it->first;         int w = it->second;         int x = d[v] - d[u] + w;         assert(0 <= x && x <= w + w);         if (0 < x && x < w + w) {           if (d[u] < l && l + l <= 2 * d[u] + x) ans++;           if (d[v] < l && l + l <= 2 * d[v] + w + w - x) ans++;           if (l + l == 2 * d[u] + x && l + l == 2 * d[v] + w + w - x) ans--;         } else {           if (min(d[u], d[v]) < l && l < max(d[u], d[v])) ans++;         }       }     ans >>= 1;     for (int i = 0; i < (n); i++) ans += d[i] == l;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using D = double;   using uint = unsigned int;   template <typename T>   using pair2 = pair<T, T>;   const int maxn = 300005;   bool used[maxn];   int n, k;   vector<int> all;   int main() {     cin >> n >> k;     if (k == 6) {       if (n < 6)         printf( No n );       else {         printf( Yes n );         printf( 5 n );         printf( 1 2 4 5 6 n );       }       return 0;     }     int cur = 0;     int mx = 0;     for (int i = 1; i <= n && cur < k; i++) {       mx = i;       used[i] = true;       for (int j = 1; j < i && j * j <= i; j++)         if (i % j == 0) {           cur++;           if (i / j > j && i / j < i) cur++;         }     }     if (cur < k) {       printf( No n );       return 0;     }     for (int IT = 0; IT < 3; IT++) {       for (int i = mx; i >= 1 && cur > k; i--)         if (used[i]) {           int cnt = 0;           for (int j = 1; j < i && j * j <= i; j++)             if (i % j == 0) {               if (used[j]) cnt++;               if (i / j > j && i / j < i && used[i / j]) cnt++;             }           for (int j = 2 * i; j <= mx; j++)             if (used[j]) cnt++;           if (cnt <= cur - k) {             used[i] = false;             cur -= cnt;           }         }     }     assert(cur == k);     for (int i = 0; i <= mx; i++)       if (used[i]) all.push_back(i);     printf( Yes n );     printf( %d n , (int)all.size());     for (auto t : all) printf( %d  , t);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sz = 55;   int n, r1, c1, r2, c2, ans = 1000000000;   string s[sz];   bool visited[sz][sz];   vector<pair<int, int> > v;   void dfs(int x, int y, bool flag) {     if (x < 1 || x > n || y < 1 || y > n || s[x][y] ==  1  ||         visited[x][y] == true) {       return;     }     visited[x][y] = true;     if (flag == true) {       v.push_back(make_pair(x, y));     } else {       for (int i = 0; i < v.size(); i++) {         int cost = (v[i].first - x) * (v[i].first - x) +                    (v[i].second - y) * (v[i].second - y);         ans = min(ans, cost);       }     }     dfs(x - 1, y, flag);     dfs(x + 1, y, flag);     dfs(x, y - 1, flag);     dfs(x, y + 1, flag);   }   int main() {     cin >> n;     cin >> r1 >> c1 >> r2 >> c2;     for (int i = 1; i <= n; i++) {       cin >> s[i];       s[i] =     + s[i];     }     memset(visited, false, sizeof(visited));     dfs(r2, c2, true);     memset(visited, false, sizeof(visited));     dfs(r1, c1, false);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s1, s2, t =   ;     cin >> s1 >> s2;     int n = s1.size(), c = 0, c1 = 0, c2 = 0;     for (int i = 0; i < n; i++) {       if (s1[i] != s2[i]) c++;     }     if (c % 2 != 0)       cout <<  impossible  << endl;     else {       c1 = c2 = c / 2;       for (int i = 0; i < n; i++) {         if (s1[i] == s2[i])           t += s1[i];         else if (s1[i] != s2[i]) {           if (c1 > 0) {             c1--;             t += s1[i];           } else if (c2 > 0) {             c2--;             t += s2[i];           }         }       }       cout << t << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void checkMin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   inline void checkMax(T &a, T b) {     if (a < b) a = b;   }   template <class T>   inline T Min(T a, T b) {     return a < b ? a : b;   }   template <class T>   inline T Max(T a, T b) {     return a > b ? a : b;   }   template <class T>   inline T Myabs(T a) {     return a > 0 ? a : -a;   }   const int dx[] = {-1, 0, 1, 0};   const int dy[] = {0, 1, 0, -1};   const int inf = 0x3f3f3f3f;   const long long linf = 1LL << 60;   const double pi = acos(-1.0);   const double eps = 1e-9;   const int mod = 100000007;   const int maxn = 10010;   int n, r, c;   char ch[maxn], s[maxn];   void print(int v) {     if (v) {       print((v - 1) / 26);       printf( %c ,  A  + (v - 1) % 26);     }   }   int main() {     scanf( %d , &n);     for (int cas = 1; cas <= n; cas++) {       scanf( %s , ch);       if (sscanf(ch,  R%dC%d , &r, &c) == 2) {         print(c);         printf( %d n , r);       } else {         sscanf(ch,  %[^0-9]%d , s, &r);         int ans = 0;         for (int i = 0; s[i]; i++) ans *= 26, ans += s[i] -  A  + 1;         printf( R%dC%d n , r, ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[2][4500005], xl[3005], ans, gd[3005];   void merge(int l, int r) {     if (l == r) return;     int mid = (l + r) >> 1;     merge(l, mid), merge(mid + 1, r);     int i = l, j = mid + 1, dq = l;     while (i <= mid && j <= r) {       if (xl[i] <= xl[j])         gd[dq++] = xl[i++];       else         gd[dq++] = xl[j++], ans += mid - i + 1;     }     for (; i <= mid; ++i) gd[dq++] = xl[i];     for (; j <= r; ++j) gd[dq++] = xl[j];     for (int i = l; i <= r; ++i) xl[i] = gd[i];     return;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &xl[i]);     merge(1, n);     for (int i = 1; i <= ans; ++i) {       dp[1][i] = dp[0][i - 1] + 1;       dp[0][i] = dp[1][i - 1] + 3;     }     printf( %.6lf , (double)dp[1][ans]);   }


#include <bits/stdc++.h>   int main() {     int n, i;     while (scanf( %d , &n) != EOF) {       printf( %d , n);       for (i = 1; i < n; i++) printf(  %d , i);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 203600;   pair<pair<int, int>, int> p[N];   int n;   vector<int> ans;   int main() {     cin >> n;     for (int i = (0); i < (int)(n); i++) {       cin >> p[i].first.first >> p[i].first.second;       p[i].second = i + 1;     }     sort(p, p + n);     for (int i = (0); i < (int)(n); i++) {       int cur = -1e9;       bool ok = 1;       for (int j = (0); j < (int)(n); j++)         if (j != i) {           if (p[j].first.first < cur)             ok = 0;           else             cur = p[j].first.second;         }       if (ok) ans.push_back(p[i].second);     }     sort((ans).begin(), (ans).end());     printf( %d n , (int)ans.size());     for (int i = (0); i < (int)(ans.size()); i++)       printf( %d%s , ans[i], i + 1 == ans.size() ?   n  :    );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, r1 = -1, p1 = -1, p2 = INT_MAX, x, a, b;     cin >> n;     while (n--) cin >> x, r1 = max(r1, x);     cin >> n;     while (n--) cin >> x, p1 = max(p1, x);     cin >> n;     while (n--) cin >> x, p2 = min(p2, x);     cin >> a >> b;     long double q = ((long long)(b * p1) / (long double)(a * p2 + b * p1));     q = sqrt(q);     q *= r1;     printf( %.12Lf n , q);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100005], f[100005], st[100005];   int main() {     int n, top = 0;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , a + i);     for (int i = n - 1; i >= 0; i--) {       int t = 0;       while (top && a[st[top - 1]] < a[i]) f[i] = t = max(t + 1, f[st[--top]]);       st[top++] = i;     }     printf( %d n , *max_element(f, f + n));   }


#include <bits/stdc++.h>   using namespace std;   map<long long, int> m;   map<long long, int>::iterator it;   int main() {     int n, k, q, a[150001];     cin >> n >> k >> q;     for (int i = 1; i <= n; i++) cin >> a[i];     while (q--) {       int x, y;       cin >> x >> y;       if (x == 1)         m.insert(make_pair(-a[y], y));       else {         int count = 0, p = 1;         if (m.find(-a[y]) == m.end())           p = 0;         else {           for (it = m.begin(); it != m.end(); it++) {             if (it->first == -a[y])               break;             else               count++;             if (count == k) {               p = 0;               break;             }           }         }         if (p)           cout <<  YES n ;         else           cout <<  NO n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double PI = 3.141592653589793;   vector<complex<double> > duzywek;   vector<complex<double> > duzyret[50][2];   void szykuj() {     for (int j = 0; j < 22; j++) {       for (int i = 1; i <= (1 << (21 - j)); i++) {         duzyret[j][0].push_back(0);         if (j) duzyret[j][1].push_back(0);       }     }   }   int potenga(int v) {     for (int i = 1; 1; i <<= 1) {       if (i >= v) {         return i;       }     }   }   void dft(int nn, int kt, int coile, complex<double> omega, int gdzie, int poz) {     if (nn == 1) {       duzyret[poz][gdzie][0] = duzywek[kt];       return;     }     dft(nn >> 1, kt, coile << 1, omega * omega, 0, poz + 1);     dft(nn >> 1, kt + coile, coile << 1, omega * omega, 1, poz + 1);     complex<double> jeden(1.0, 0.0);     int w = 0;     int an = (nn >> 1) - 1;     for (int i = 0; i < nn; i++) {       duzyret[poz][gdzie][i] =           duzyret[poz + 1][0][i & an] + jeden * duzyret[poz + 1][1][i & an];       jeden *= omega;     }   }   vector<int> fft(vector<int> jed, vector<int> dwa) {     int n1 = potenga(jed.size() + dwa.size());     while (jed.size() < n1) {       jed.push_back(0);     }     while (dwa.size() < n1) {       dwa.push_back(0);     }     complex<double> omega(cos(2.0 * PI / n1), sin(2.0 * PI / n1));     vector<complex<double> > pam;     duzywek.clear();     for (int i = 0; i < n1; i++) {       duzywek.push_back(complex<double>(jed[i], 0.0));     }     dft(n1, 0, 1, omega, 0, 0);     pam = duzyret[0][0];     for (int i = 0; i < n1; i++) {       duzywek[i] = complex<double>(dwa[i], 0.0);     }     dft(n1, 0, 1, omega, 0, 0);     for (int i = 0; i < n1; i++) {       duzywek[i] = pam[i] * duzyret[0][0][i];     }     omega = (complex<double>(1.0, 0.0)) / omega;     dft(n1, 0, 1, omega, 0, 0);     vector<int> retret;     int dowrzucenia;     for (int i = 0; i < n1; i++) {       duzyret[0][0][i] /= n1;       dowrzucenia = round(duzyret[0][0][i].real());       retret.push_back(dowrzucenia);     }     return retret;   }   int n, m;   vector<int> dopo;   vector<int> otrz;   vector<int> wyn;   int x;   int main() {     szykuj();     scanf( %d%d , &n, &m);     while (dopo.size() <= m) {       dopo.push_back(0);     }     dopo[0] = 1;     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       dopo[x] = 1;     }     otrz = fft(dopo, dopo);     while (otrz.size() > m + 1) otrz.pop_back();     for (int i = 1; i < otrz.size(); i++) {       if (otrz[i] && !dopo[i]) {         printf( NO );         return 0;       }       if (otrz[i] == 2) {         wyn.push_back(i);       }     }     printf( YES n );     printf( %d n , (int)wyn.size());     for (int i = 0; i < wyn.size(); i++) {       printf( %d  , wyn[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, int> last;   int b[1000000];   long long a[100000];   long long d[100][2];   pair<long long, long long> solve(long long x, long long y) {     long long xx, yy;     pair<long long, long long> ans;     if (x == 1) {       return make_pair(1, 0);     }     xx = x;     yy = y % x;     if (yy == 1) {       ans = make_pair(1, xx - 1);     } else {       ans = solve(xx % yy, yy);       ans.second += (xx / yy) * ans.first;       ans.first %= yy;       ans.second %= xx;     }     ans.first += (y / x) * ans.second;     ans.first %= y;     ans.second %= x;     return ans;   }   long long inv(long long x, long long y) {     x %= y;     pair<long long, long long> p = solve(x, y);     return p.first;   }   long long gcd(long long x, long long y) {     if (y == 0)       return x;     else       return gcd(y, x % y);   }   int main() {     vector<long long> primes;     long long i, j, k, l;     long long m, n, now, tmp, tmp1, cnt, b1, nn, ans, all;     memset(b, 0, sizeof(b));     primes.clear();     for (i = 2; i < 1000000; i++) {       if (b[i] == 0) {         primes.push_back(i);         for (j = i + i; j < 1000000; j += i) b[j] = 1;       }     }     scanf( %I64d%I64d%I64d , &n, &m, &k);     for (i = 0; i < k; i++) scanf( %I64d , &a[i]);     last.clear();     now = 1;     for (i = 0; i < k; i++) {       tmp = gcd(now, a[i]);       now /= tmp;       if ((1LL << 60) / now < a[i]) {         printf( NO n );         return 0;       }       now *= a[i];       if (now > n) {         printf( NO n );         return 0;       }       tmp = a[i];       for (j = 0; j < primes.size() && primes[j] * primes[j] <= tmp; j++) {         cnt = 0;         while (tmp % primes[j] == 0) {           tmp /= primes[j];           cnt++;         }         tmp1 = 1;         for (l = 0; l < cnt; l++) {           tmp1 *= primes[j];           if (i - tmp1 >= 0) {             if ((last.find(tmp1) == last.end()) || (last[tmp1] != i - tmp1)) {               printf( NO n );               return 0;             }             last[tmp1] = i;           } else {             if (last.find(tmp1) != last.end()) {               printf( NO n );               return 0;             }             last[tmp1] = i;           }         }       }       if (tmp > 1) {         tmp1 = tmp;         if (i - tmp1 >= 0) {           if ((last.find(tmp1) == last.end()) || (last[tmp1] != i - tmp1)) {             printf( NO n );             return 0;           }           last[tmp1] = i;         } else {           if (last.find(tmp1) != last.end()) {             printf( NO n );             return 0;           }           last[tmp1] = i;         }       }     }     for (map<long long, int>::iterator it = last.begin(); it != last.end();          it++) {       if (it->second + it->first < k) {         printf( NO n );         return 0;       }     }     nn = 0;     for (map<long long, int>::reverse_iterator it = last.rbegin();          it != last.rend(); it++) {       b1 = 1;       for (i = 0; i < nn; i++)         if (d[i][0] % it->first == 0) {           b1 = 0;           break;         }       if (b1 == 0) continue;       d[nn][0] = it->first;       d[nn][1] = k - 1 - it->second;       nn++;     }     ans = 0;     all = 1;     for (i = 0; i < nn; i++) all *= d[i][0];     for (i = 0; i < nn; i++) {       tmp = 1;       for (j = 0; j < nn; j++)         if (j != i) tmp *= d[j][0];       tmp *= inv(tmp, d[i][0]);       tmp *= d[i][1];       tmp %= all;       ans += tmp;     }     ans %= all;     if (ans - (k - 1) <= 0) ans += all;     if (ans <= m) {       printf( YES n );     } else       printf( NO n );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       bitset<32> b1(n);       bitset<32> b2(m + 1);       vector<int> a(32), b(32);       for (int i = 0; i < 32; i++) a[i] = b1[31 - i];       for (int i = 0; i < 32; i++) b[i] = b2[31 - i];       long long ans = 0;       for (int i = 0; i < 32; i++) {         if (a[i] == 1 && b[i] == 0)           break;         else           ans += (a[i] ^ b[i]) * pow(2, 31 - i);       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   const int INFi = 2e9;   const int maxN = 1e5 + 5;   const int md = 998244353;   const int md2 = 1e9 + 7;   double getTime() { return clock() / (double)CLOCKS_PER_SEC; };   void solve() {     int m, n;     cin >> m >> n;     vector<pair<int, int>> a(n);     for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;     sort((a).begin(), (a).end());     vector<long long> suf(n + 1);     suf[n] = 0;     suf[n - 1] = a[n - 1].first;     for (int i = n - 2; i >= 0; --i) suf[i] = suf[i + 1] + a[i].first;     long long ans = 0;     for (int i = 0; i < n; ++i) {       long long res = 0;       pair<int, int> kek = {a[i].second, -1};       int l = lower_bound((a).begin(), (a).end(), kek) - a.begin();       l = max(l, n - m);       int cnt = n - l;       res += suf[l];       if (i < l && cnt < m) {         cnt++;         res += a[i].first;       }       res += 1ll * a[i].second * (m - cnt);       ans = max(ans, res);     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int tests = 1;     cin >> tests;     for (int _ = 0; _ < tests; ++_) {       solve();     }     return 0;   }


#include <bits/stdc++.h>      const int N = 5010;   const int M = 1 << 20;   const int K = 30;      char s[N];   int vis[K];   bool dp[M];   int cnt[K], l[K], r[K];      int main(){       int n, a, b;       scanf( %d%d%d , &n, &a, &b);       scanf( %s , s);       memset(vis, -1, sizeof(vis));       int id = 0;       for (int i = 0; i < n; ++ i){           if (vis[s[i] -  a ] != -1){               continue;           }           vis[s[i] -  a ] = id;           l[id] = i;           for (int j = i; j < n; ++ j){               if (s[j] == s[i]){                   r[id] = j;                   ++ cnt[id];               }           }           if (1ll * a * (r[id] - l[id] + 1) <= 1ll * cnt[id] * b){               dp[1 << id] = true;           }           ++ id;       }       for (int i = 3; i < (1 << id) - 1; ++ i){           if (__builtin_popcount(i) <= 1){               continue;           }           for (int j = 1; j < id; ++ j){               int x = i & ((1 << j) - 1);               int y = i ^ x;               if (x < i && y < i){                   if (dp[x] && dp[y]){                       dp[i] = true;                       break;                   }               }           }           if (dp[i]){               continue;           }           int left = INT_MAX, right = INT_MIN;           int ccnt = 0;           for (int j = 0; j < id; ++ j){               if (i >> j & 1){                   left = std::min(left, l[j]);                   right = std::max(right, r[j]);                   ccnt += cnt[j];               }           }           if (1ll * a * (right - left + 1) <= 1ll * ccnt * b){               for (int j = 0; j < id; ++ j){                   if (i >> j & 1){                       if (dp[i ^ 1 << j]){                           dp[i] = true;                           break;                       }                   }               }           }       }       dp[0] = true;       std::vector <char> ans;       for (int i = 0; i < 26; ++ i){           if (vis[i] == -1){               continue;           }           if (dp[((1 << id) - 1) ^ (1 << vis[i])]){               ans.emplace_back( a  + i);           }       }       printf( %d , int(ans.size()));       for (auto u : ans){           printf(  %c , u);       }       puts(  );       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     char a[55][55];     memset(a, 0, sizeof(a));     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> a[i][j];       }     }     cout << a[1] << endl;     int r[55], c[55], rn = 0, cn = 0;     bool rr[55];     memset(rr, 0, sizeof(rr));     for (int i = 1; i <= n; i++) {       rr[i] = 1;       memset(r, 0, sizeof(r));       memset(c, 0, sizeof(c));       rn = cn = 0;       for (int j = 1; j <= m; j++) {         if (a[i][j] ==  # ) {           c[++cn] = j;           for (int k = i + 1; k <= n; k++) {             if (a[k][j] ==  #  && !rr[k]) {               rr[k] = 1;               r[++rn] = k;             }           }         }       }       for (int j = 1; j <= rn; j++) {         for (int k = 1; k <= cn; k++) {           if (a[r[j]][c[k]] !=  # ) {             cout <<  No n ;             return 0;           }         }         int num = 0;         for (int k = 1; k <= m; k++) {           if (a[r[j]][k] ==  # ) {             num++;           }         }         if (num != cn) {           cout <<  No n ;           return 0;         }       }     }     cout <<  Yes n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   int h;   int a[N];   int sum[N];   int main() {     scanf( %d , &h);     for (int i = 0; i <= h; i++) {       scanf( %d , &a[i]);       sum[i] += sum[i - 1] + a[i];     }     bool check = false;     int pre = 1;     for (int i = 0; i <= h; i++) {       if (a[i] == 1) {         pre = 1;         continue;       } else {         if (pre == 1) {           pre = a[i];           continue;         } else {           check = true;         }       }     }     if (check == false) {       cout <<  perfect ;       return 0;     }     cout <<  ambiguous  << endl;     cout <<  0  ;     for (int i = 1; i <= h; i++) {       for (int j = 1; j <= a[i]; j++) {         cout << sum[i - 2] + 1 <<    ;       }     }     cout << endl;     cout <<  0  ;     for (int i = 1; i <= h; i++) {       for (int j = 1; j <= a[i]; j++) {         if (sum[i - 2] + j <= sum[i - 1])           cout << sum[i - 2] + j <<    ;         else {           cout << sum[i - 2] + 1 <<    ;         }       }     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110;   int T, mod = 1e9 + 7, n, k, deep[N], timer, sz;   long long ans, fact[N], mem[N][N], vis[N][N];   vector<int> adj[N], v;   map<int, int> mp[N];   void _clear() {     ans = 0;     for (int i = 1; i <= n; i++) adj[i].clear();   }   long long po(long long x, long long os) {     if (os == 0) return 1;     long long z = po(x, os / 2);     if (os & 1) return z * z % mod * x % mod;     return z * z % mod;   }   long long modInv(long long x) { return po(x, mod - 2); }   long long c(long long n, long long r) {     return fact[n] * modInv(fact[r] * fact[n - r] % mod) % mod;   }   void dfs(int u, int v) {     deep[u] = deep[v] + 1;     mp[u][deep[u]]++;     for (auto &x : adj[u])       if (x != v) {         dfs(x, u);         for (auto &p : mp[x]) mp[u][p.first] += p.second;       }   }   long long dp(int i, int k) {     if (k == 0) return 1;     if (i == sz) return 0;     long long &ret = mem[i][k];     if (vis[i][k] == timer) return ret;     vis[i][k] = timer;     return ret = (dp(i + 1, k) + dp(i + 1, k - 1) * v[i] % mod) % mod;   }   long long calc(int p) {     deep[p] = 0;     for (int i = 1; i <= n; i++) mp[i].clear();     dfs(p, p);     long long ret = 0;     for (int i = 2; i <= n; i++) {       int num = 0;       v.clear();       for (auto &x : adj[p])         if (mp[x].find(i) != mp[x].end()) {           num++;           v.push_back(mp[x][i]);         }       if (num < k) continue;       timer++;       sz = (int)(v.size());       ret = (ret + dp(0, k)) % mod;     }     return ret;   }   int main() {     fact[0] = 1;     for (int i = 1; i < N; i++) fact[i] = fact[i - 1] * i % mod;     scanf( %d , &T);     while (T--) {       scanf( %d%d , &n, &k);       _clear();       for (int i = 1; i <= n - 1; i++) {         int a, b;         scanf( %d%d , &a, &b);         adj[a].push_back(b);         adj[b].push_back(a);       }       if (k == 2) {         printf( %lld n , c(n, 2));         continue;       }       for (int i = 1; i <= n; i++) ans = (ans + calc(i)) % mod;       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   int read() {     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     int x = 0;     while (c >=  0  && c <=  9 ) {       x = x * 10 + c -  0 ;       c = getchar();     }     return x;   }   const int maxn = (1 << 21) + 3;   const int INF = 0x3f3f3f3f;   struct segtree {     int n;     int tag[maxn], mmin[maxn];     int ul, ur, uv;     void update(int o, int l, int r) {       if (ul <= l && ur >= r) {         tag[o] = max(tag[o], uv);         mmin[o] = max(mmin[o], uv);         return;       }       int mid = (l + r) / 2;       if (ul <= mid) update(o * 2, l, mid);       if (ur > mid) update(o * 2 + 1, mid + 1, r);       mmin[o] = max(min(mmin[o * 2], mmin[o * 2 + 1]), tag[o]);     }     void modify(int l, int r, int v) {       if (l > r) return;       ul = l, ur = r, uv = v;       update(1, 1, n);     }     int p, x;     int query2(int o, int l, int r) {       if (max(tag[o], mmin[o]) >= x) return n + 1;       if (l == r) return l;       int mid = (l + r) / 2;       if (mmin[o * 2] < x)         return query2(o * 2, l, mid);       else         return query2(o * 2 + 1, mid + 1, r);     }     int query(int o, int l, int r) {       if (max(tag[o], mmin[o]) >= x) return n + 1;       if (l == r) return l;       int mid = (l + r) / 2;       if (p > mid)         return query(o * 2 + 1, mid + 1, r);       else {         int ans = query(o * 2, l, mid);         if (ans != n + 1)           return ans;         else           return query2(o * 2 + 1, mid + 1, r);       }     }     int query(int p, int x) {       if (p > n) return n + 1;       this->p = p, this->x = x;       return query(1, 1, n);     }   } t;   struct edge {     int l, r;     bool operator<(const edge& rhs) const { return r < rhs.r; }   } e[maxn];   struct query {     int id, x, y;     bool operator<(const query& rhs) const { return y < rhs.y; }   } q[maxn];   int ans[maxn];   void write(int x) {     if (x >= 10) write(x / 10);     putchar(x % 10 +  0 );   }   void writeln(int x) {     write(x);     putchar(  n );   }   int main() {     int n = t.n = read(), k = read();     for (int i = 1; i <= k; i++) e[i].l = read(), e[i].r = read();     sort(e + 1, e + k + 1);     int m = read();     for (int i = 1; i <= m; i++) q[i].id = i, q[i].x = read(), q[i].y = read();     sort(q + 1, q + m + 1);     int now = 1;     for (int i = 1; i <= m; i++) {       while (now <= k && e[now].r <= q[i].y) {         t.modify(e[now].l + 1, e[now].r, e[now].l);         now++;       }       ans[q[i].id] = t.query(q[i].x + 1, q[i].x) - 1;     }     for (int i = 1; i <= m; i++) writeln(ans[i]);   }


#include <bits/stdc++.h>   const int MOD = 1000000007;   const int SIZE_N = 2000 + 10;   int n;   int ary[SIZE_N];   int vis[SIZE_N];   int fac[SIZE_N];   int madd(long long a, long long b) { return (a + b) % MOD; }   int mmul(long long a, long long b) { return (a * b) % MOD; }   int dp[SIZE_N][SIZE_N], v[SIZE_N][SIZE_N];   int f(int a, int b) {     if (v[a][b] == 1) return dp[a][b];     v[a][b] = 1;     if (a == 0) return dp[a][b] = fac[b];     if (a == 1) return dp[a][b] = mmul(b, fac[b]);     return dp[a][b] = madd(mmul(b, f(a - 1, b)), mmul(a - 1, f(a - 2, b + 1)));   }   int main(void) {     fac[0] = 1;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , ary + i);       vis[ary[i]] = 1;       fac[i] = mmul(fac[i - 1], i);     }     int a = 0, b = 0;     for (int i = 1; i <= n; ++i) {       if (vis[i] == 1) continue;       if (ary[i] == -1)         ++a;       else         ++b;     }     int ans = 0;     ans = f(a, b);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   const int M = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int s, x1, x2, t1, t2, p, d;     cin >> s >> x1 >> x2;     cin >> t1 >> t2;     cin >> p >> d;     int ans = t2 * abs(x1 - x2);     if (x1 < x2) {       if (d == -1)         ans = min(ans, (p + x2) * t1);       else if (p <= x1)         ans = min(ans, (x2 - p) * t1);       else         ans = min(ans, (s + s - p + x2) * t1);     } else {       if (d == 1)         ans = min(ans, (s + s - p - x2) * t1);       else if (p >= x1)         ans = min(ans, (p - x2) * t1);       else         ans = min(ans, (s + p + s - x2) * t1);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while ((t--)) {       int n;       cin >> n;       int arr[n];       int diff[n - 1];       for (int i = 0; i < n; i++) {         cin >> arr[i];       }       int ctr = 0;       int min = arr[n - 1];       for (int i = n - 2; i >= 0; i--) {         if (min >= arr[i]) {           min = arr[i];         } else {           ctr++;         }       }       cout << ctr <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 1e9;   vector<long long> lucky_nums;   void generate_lucky(long long n) {     if (n > MAX) return;     long long n1 = 10 * n + 4;     lucky_nums.push_back(n1);     generate_lucky(n1);     long long n2 = 10 * n + 7;     lucky_nums.push_back(n2);     generate_lucky(n2);   }   void solve() {     generate_lucky(0);     sort(lucky_nums.begin(), lucky_nums.end());     long long l, r;     cin >> l >> r;     int i =         lower_bound(lucky_nums.begin(), lucky_nums.end(), l) - lucky_nums.begin();     long long prev = l, ans = 0;     while (lucky_nums[i] < r) {       ans += (lucky_nums[i] - prev + 1) * lucky_nums[i];       prev = lucky_nums[i] + 1;       i++;     }     ans += (r - prev + 1) * lucky_nums[i];     cout << ans << endl;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500005;   int n, k;   char a[N], b[N];   int main() {     scanf( %d%d%s%s , &n, &k, a, b);     long long cur = 1, ans = 0;     if (k == 1) {       cout << n << endl;       return 0;     }     for (int i = 0; i < n; i++) {       cur *= 2;       if (a[i] !=  a ) --cur;       if (b[i] !=  b ) --cur;       cur = min(cur, k * 1ll);       ans += cur;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (1 << 30) - 1;   const long long oo = (1ll << 62) - 1;   const long double PI = 3.1415926535898;   const int N = 1000000 + 5;   int n, m;   vector<int> a[N], e[N];   int num[N], low[N], vt[N], dd[N];   int Time = 0, tpltm = 0;   stack<int> s;   vector<int> res1, res2;   void minimize(int &a, int b) {     if (a > b) a = b;   }   void dfs(int u) {     num[u] = low[u] = ++Time;     s.push(u);     for (int v : a[u]) {       if (num[v] == -2) continue;       if (num[v] > 0) {         minimize(low[u], num[v]);       } else {         dfs(v);         minimize(low[u], low[v]);       }     }     if (low[u] >= num[u]) {       tpltm += 1;       int v;       do {         v = s.top();         s.pop();         vt[v] = tpltm;         num[v] = -2;       } while (v != u);     }   }   void solve() {     cin >> n >> m;     for (int i = 1; i <= n; i++) a[i].clear();     for (int i = 1; i <= m; i++) {       int u, v;       cin >> u >> v;       if (u == v) continue;       a[u].push_back(v);     }     Time = 0;     tpltm = 0;     while (s.size()) s.pop();     for (int i = 1; i <= n; i++) num[i] = 0;     for (int i = 1; i <= n; i++) {       if (num[i]) continue;       dfs(i);     }     if (tpltm <= 1) {       cout <<  No  <<   n ;       return;     }     for (int i = 1; i <= tpltm; i++) e[i].clear();     for (int u = 1; u <= n; u++) {       for (int v : a[u]) {         if (vt[u] == vt[v]) continue;         e[vt[u]].push_back(vt[v]);       }     }     int gt = INF, dinh = 0;     for (int i = 1; i <= tpltm; i++) {       if (e[i].size() < gt) {         gt = e[i].size();         dinh = i;       }     }     res1.clear();     res2.clear();     for (int i = 1; i <= n; i++) {       if (vt[i] == dinh) {         res1.push_back(i);       } else         res2.push_back(i);     }     if (res1.size() == 0 || res2.size() == 0) {       cout <<  No  <<   n ;       return;     }     cout <<  Yes  <<   n ;     cout << res1.size() <<     << res2.size() <<   n ;     for (int i = 0; i < res1.size(); i++) cout << res1[i] <<    ;     cout <<   n ;     for (int i = 0; i < res2.size(); i++) cout << res2[i] <<    ;     cout <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     for (int i = 1; i <= t; i++) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 505;   long long l1[MAX], l2[MAX], r1[MAX], r2[MAX], a1[MAX], a2[MAX];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int q;     cin >> q;     for (int i = 0; i < q; ++i) {       cin >> l1[i] >> r1[i] >> l2[i] >> r2[i];     }     for (int i = 0; i < q; ++i) {       a1[i] = l1[i], a2[i] = l2[i];       while (a1[i] == a2[i] && a2[i] <= r2[i]) {         a2[i]++;       }       cout << a1[i] <<     << a2[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   const int M = 1000000000;   const int INF = 1e18;   int a[N], b[N], s1[N], s2[N];   int n, m, k, d, top1, top2, ansr, ansl, sum[N], pos[N], t[N], mn[N];   map<int, int> mp;   void reset(int x) {     mn[x] = INF;     for (int i = (x - 1) * m + 1; i <= min(n, x * m); i++)       mn[x] = min(mn[x], t[i] + sum[x]);   }   inline void add(int l, int r, int c) {     for (int i = l; i <= min(r, pos[l] * m); i++) t[i] += c;     reset(pos[l]);     if (pos[l] != pos[r]) {       for (int i = (pos[r] - 1) * m + 1; i <= min(n, r); i++) t[i] += c;       reset(pos[r]);     }     for (int i = pos[l] + 1; i <= pos[r] - 1; i++) sum[i] += c, mn[i] += c;   }   inline int query(int l, int r, int c) {     for (int i = r; i >= max((pos[r] - 1) * m + 1, l); i--)       if (t[i] + sum[pos[i]] <= c) {         return i;       }     for (int i = pos[r] - 1; i >= pos[l] + 1; i--)       if (mn[i] <= c)         for (int j = i * m; j >= (i - 1) * m + 1; j--)           if (t[j] + sum[pos[j]] <= c) {             return j;           }     if (pos[l] != pos[r]) {       for (int i = pos[l] * m; i >= l; i--) {         if (t[i] + sum[pos[i]] <= c) return i;       }     }   }   inline void sol(int L, int R) {     if (L == R) return;     mp.clear();     int r = R + 1;     top1 = top2 = 0;     s1[top1] = s2[top2] = R + 1;     for (int i = 0; i <= pos[n]; i++) sum[i] = 0;     for (int i = R; i >= L; i--) {       if (mp.count(a[i])) r = min(r, mp[a[i]]);       while (top1 && a[i] > a[s1[top1]]) {         add(s1[top1], s1[top1 - 1] - 1, -a[s1[top1]]);         top1--;       }       s1[++top1] = i;       add(s1[top1], s1[top1 - 1] - 1, a[s1[top1]]);       while (top2 && a[i] < a[s2[top2]]) {         add(s2[top2], s2[top2 - 1] - 1, a[s2[top2]]);         top2--;       }       s2[++top2] = i;       add(s2[top2], s2[top2 - 1] - 1, -a[s2[top2]]);       int rr = query(i, r - 1, k - i);       if (rr - i > ansr - ansl || (rr - i == ansr - ansl && i < ansl))         ansl = i, ansr = rr;       mp[a[i]] = i;     }     return;   }   int main() {     scanf( %d%d%d , &n, &k, &d);     if (d == 0) {       for (int i = 1; i <= n; i++) scanf( %d , &a[i]);       int ansl = 1;       int ansr = 1;       int lst = 1;       for (int i = 2; i <= n; i++) {         if (a[i] != a[lst]) {           if (i - 1 - lst > ansr - ansl) {             ansl = lst;             ansr = i - 1;           }           lst = i;         }       }       printf( %d %d n , ansl, ansr);       return 0;     }     ansl = n + 1;     ansr = 0;     m = sqrt(n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       b[i] = (a[i] += M) % d;       a[i] = (a[i]) / d + 1;       pos[i] = (i - 1) / m + 1;       t[i] = -i;     }     for (int i = 1, j; i <= n; i = j) {       for (j = i + 1; j <= n; j++) {         if (b[i] != b[j]) break;       }       sol(i, j - 1);     }     printf( %d %d n , ansl, ansr);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, p[200005];   int pn;   int main() {     scanf( %I64d , &n);     n *= 2;     for (int k = 0; k < 60; ++k) {       long long a = (1LL << (k + 1)) - 3;       long long l = 0, r = 2000000000;       if (a > 1) r = min(r, n / a + 1);       while (r - l > 1) {         long long x = (r + l) / 2;         if (x * x + a * x <= n)           l = x;         else           r = x;       }       if (l % 2 == 1 && l * l + a * l == n) p[pn++] = (l << k);     }     if (pn == 0) {       puts( -1 );       exit(0);     }     sort(p, p + pn);     for (int i = 0; i < pn; ++i) printf( %I64d n , p[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[200010], t[200010];   string ans;   void solve() {     int n = strlen(s), x = 0;     for (int i = 0; i < n; i++) {       if (i) ans +=  + ;       if (i < n - 1 && rand() % 2 == 0) {         ans += (s[i]), ans += s[++i];         x += 10 * (s[i - 1] -  0 ) + s[i] -  0 ;       } else {         ans += (s[i]);         x += s[i] -  0 ;       }     }     ans +=   n ;     sprintf(s,  %d , x);   }   int main() {     srand(time(0));     scanf( %s%s , s, t);     while (1) {       strcpy(s, t);       ans =   ;       solve();       solve();       solve();       if (strlen(s) == 1) return cout << ans, 0;     }   }


#include <bits/stdc++.h>   namespace fastIO {   static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;   char buf[IN_BUF], *ps = buf, *pt = buf;   inline char my_getchar() {     return ps == pt && (pt = (ps = buf) + fread(buf, 1, IN_BUF, stdin), ps == pt)                ? EOF                : *ps++;   }   template <typename T>   inline bool read(T &x) {     char op = 0, ch = my_getchar();     for (x = 0; !isdigit(ch) && ch != EOF; ch = my_getchar())       if (ch ==  - ) op ^= 1;     if (ch == EOF) return false;     for (; isdigit(ch); ch = my_getchar()) x = x * 10 + (ch ^  0 );     if (ch !=  . )       return op ? x = -x : 0, true;     else       ch = my_getchar();     T t = 1;     for (; isdigit(ch); ch = my_getchar()) t /= 10, x += (ch ^  0 ) * t;     return op ? x = -x : 0, true;   }   inline int reads(char *s) {     int n = 0;     char ch = my_getchar();     for (; isspace(ch) && ch != EOF; ch = my_getchar())       ;     for (; !isspace(ch) && ch != EOF; ch = my_getchar()) s[n++] = ch;     return s[n] =   0 , n;   }   char pbuf[OUT_BUF], *pp = pbuf;   struct _flusher {     ~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); }   } OutputFlusher;   inline void my_putchar(char x) {     pp == pbuf + OUT_BUF ? fwrite(pbuf, 1, OUT_BUF, stdout), pp = pbuf : 0,                                                              *pp++ = x;   }   template <typename T>   inline void print_(T x) {     if (x == 0) return my_putchar( 0 ), void(0);     std::vector<int> num;     for (x < 0 ? my_putchar( - ), x = -x : 0; x; x /= 10) num.push_back(x % 10);     while (!num.empty()) my_putchar(num.back() ^  0 ), num.pop_back();   }   template <typename T>   inline void print(T x, char ch =   n ) {     print_(x), my_putchar(ch);   }   inline void prints_(const char *s, int n = -1) {     if (n == -1) n = strlen(s);     for (register int i = 0; i < n; ++i) my_putchar(s[i]);   }   inline void prints(const char *s, int n = -1, char ch =   n ) {     prints_(s, n), my_putchar(ch);   }   template <typename T>   inline void printd_(T x, int n) {     if (x < 0) x = -x, my_putchar( - );     long long a, b;     for (register int i = 0; i < n; ++i) x *= 10;     x = (long long)(x + 0.5);     for (register int i = 0; i < n; ++i) x /= 10;     a = x, x -= a;     for (register int i = 0; i < n; ++i) x *= 10;     b = x;     print_(a);     if (n)       my_putchar( . );     else       return;     std::vector<int> num(n);     for (register int i = 0; i < n; ++i) num[i] = b % 10, b /= 10;     while (!num.empty()) my_putchar(num.back() ^  0 ), num.pop_back();   }   template <typename T>   inline void printd(T x, int n, char ch =   n ) {     printd_(x, n), my_putchar(ch);   }   }  // namespace fastIO   using namespace fastIO;   const int N = 200005;   int n, p[N], c[N], vis[N];   int solve(std::vector<int> &a) {     std::vector<int> d;     int l = a.size();     for (register int i = 1; 1ll * i * i <= l; ++i)       if (l % i == 0) {         d.push_back(i);         if (i * i < l) d.push_back(l / i);       }     std::sort(d.begin(), d.end());     for (int k : d) {       for (register int i = 0; i < k; ++i) {         bool flag = 1;         for (register int j = i; j < l; j += k) flag &= a[j] == a[i];         if (flag) return k;       }     }     return l;   }   void solve() {     read(n);     for (register int i = 1; i <= n; ++i) read(p[i]), vis[i] = 0;     for (register int i = 1; i <= n; ++i) read(c[i]);     int ans = n;     for (register int i = 1; i <= n; ++i)       if (!vis[i]) {         std::vector<int> tmp;         tmp.push_back(c[i]), vis[i] = 1;         for (register int j = p[i]; j != i; j = p[j])           tmp.push_back(c[j]), vis[j] = 1;         ans = std::min(ans, solve(tmp));       }     print(ans);   }   int main() {     int T = 1;     read(T);     while (T--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int p[1001][1001], row[1001], col[1001];   int main() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int r = 1; r <= n; ++r) row[r] = r;     for (int c = 1; c <= m; ++c) col[c] = c;     for (int r = 1; r <= n; ++r)       for (int c = 1; c <= m; ++c) scanf( %d , &p[r][c]);     while (k--) {       char s[2];       int x, y;       scanf( %s%d%d , s, &x, &y);       switch (s[0]) {         case  r :           swap(row[x], row[y]);           break;         case  c :           swap(col[x], col[y]);           break;         case  g :           printf( %d n , p[row[x]][col[y]]);           break;         default:           break;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool ok = false, a[5][5];   int u[5];   string p, maxs;   int pol(char s) {     if (s ==  A ) return 1;     if (s ==  B ) return 2;     if (s ==  C ) return 3;   }   string pol2(int s) {     if (s == 1) return  A ;     if (s == 2) return  B ;     if (s == 3) return  C ;   }   void dfs(int v) {     if (ok) return;     u[v] = 1;     for (int i = 1; i <= 3; i++)       if (a[v][i] && u[i] == 0)         dfs(i);       else if (a[v][i] && u[i] == 1)         ok = true;     u[v] = 2;     p = p + pol2(v);   }   int main() {     string s;     for (int i = 1; i <= 3; i++) {       cin >> s;       if (s[1] ==  > )         a[pol(s[0])][pol(s[2])] = 1;       else         a[pol(s[2])][pol(s[0])] = 1;     }     for (int i = 1; i <= 3; i++) {       for (int j = 1; j <= 3; j++) u[j] = 0;       p =   ;       dfs(i);       if (p.size() > maxs.size()) maxs = p;     }     if (ok)       cout <<  Impossible ;     else       cout << maxs;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned int seed;   unsigned int getrand() {     seed ^= seed << 13;     seed ^= seed >> 17;     seed ^= seed << 5;     return seed;   }   int get() {     char ch;     while (ch = getchar(), (ch <  0  || ch >  9 ) && ch !=  - )       ;     if (ch ==  - ) {       int s = 0;       while (ch = getchar(), ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 ;       return -s;     }     int s = ch -  0 ;     while (ch = getchar(), ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 ;     return s;   }   const int N = 2e5 + 5;   int n;   int fa[N * 2];   struct node {     int l, r, tot;     long long sum, sumv;     unsigned int key;     int val;   } tree[N];   int lef[N], rig[N];   int rt[N * 2];   long long ans;   int tot;   int getfather(int x) { return fa[x] == x ? x : fa[x] = getfather(fa[x]); }   void update_ans(int x, long long v) {     ans = ans + v * (tree[rt[x]].sum + tree[rt[x]].sumv * (lef[x] - 1));   }   void update(int now) {     int l = tree[now].l, r = tree[now].r;     tree[now].tot = tree[l].tot + tree[r].tot + 1;     tree[now].sumv = tree[l].sumv + tree[r].sumv + tree[now].val;     tree[now].sum = tree[l].sum + 1ll * tree[now].val * (tree[l].tot + 1) +                     tree[r].sum + tree[r].sumv * (tree[l].tot + 1);   }   pair<int, int> split(int now, int v) {     if (!now) return make_pair(0, 0);     if (tree[now].val > v) {       pair<int, int> u = split(tree[now].r, v);       tree[now].r = u.first;       update(now);       return make_pair(now, u.second);     }     pair<int, int> u = split(tree[now].l, v);     tree[now].l = u.second;     update(now);     return make_pair(u.first, now);   }   int merge(int x, int y) {     if (!x || !y) return x | y;     if (tree[x].key < tree[y].key) {       tree[x].r = merge(tree[x].r, y);       update(x);       return x;     }     tree[y].l = merge(x, tree[y].l);     update(y);     return y;   }   void add(int x, int v, int id = 0) {     if (!id) update_ans(x, -1);     pair<int, int> u = split(rt[x], v);     int t = id;     if (!id) t = ++tot;     tree[t].l = tree[t].r = 0;     tree[t].sum = tree[t].val = tree[t].sumv = v;     tree[t].tot = 1;     tree[t].key = getrand();     rt[x] = merge(u.first, merge(t, u.second));     if (!id) update_ans(x, 1);   }   int que[N];   int id[N];   int k;   void getque(int now) {     if (!now) return;     getque(tree[now].l);     que[++k] = tree[now].val;     id[k] = now;     getque(tree[now].r);   }   void Merge(int x, int y) {     update_ans(x, -1);     update_ans(y, -1);     if (tree[rt[x]].tot < tree[rt[y]].tot) {       k = 0;       getque(rt[x]);       for (int i = 1; i <= k; i++) add(y, que[i], id[i]);       lef[y] = lef[x];       fa[x] = y;       update_ans(y, 1);     } else {       k = 0;       getque(rt[y]);       for (int i = 1; i <= k; i++) add(x, que[i], id[i]);       rig[x] = rig[y];       fa[y] = x;       update_ans(x, 1);     }   }   int main() {     seed = 29382343;     n = get();     ans = 0;     for (int i = 1; i <= n; i++) {       int x = get(), v = get();       ans = ans - 1ll * x * v;       if (!fa[x]) {         fa[x] = x;         lef[x] = rig[x] = x;         add(x, v);       } else {         int y = getfather(x);         fa[++rig[y]] = y;         add(y, v);       }       int y = getfather(x);       if (fa[rig[y] + 1]) Merge(y, getfather(rig[y] + 1));       y = getfather(y);       if (fa[lef[y] - 1]) Merge(getfather(lef[y] - 1), y);       printf( %I64d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 69;   const int MAXP = 2e6 + 69;   int n, q;   int val[MAXN];   vector<int> a[MAXN];   int check[MAXP];   vector<int> p;   void build() {     for (long long i = 2; i < MAXP; i++) {       if (check[i] == 0) {         p.push_back(i);         check[i] = i;         for (long long j = i * i; j < MAXP; j += i) {           if (check[j] == 0) check[j] = i;         }       }     }   }   int ans[MAXN];   int h[MAXN];   vector<int> prim[MAXP];   void ptich(int i) {     int x = val[i];     while (check[x] != 0) {       int t = check[x];       if (!prim[t].empty()) {         int u = prim[t].back();         if (h[u] > h[ans[i]]) {           ans[i] = u;         }       }       prim[t].push_back(i);       while (x % t == 0) x /= t;     }   }   void xoa(int i) {     int x = val[i];     while (check[x] != 0) {       int t = check[x];       prim[t].pop_back();       while (x % t == 0) x /= t;     }   }   void dfs(int x, int p) {     h[x] = h[p] + 1;     ptich(x);     for (int u : a[x]) {       if (u == p) continue;       dfs(u, x);     }     xoa(x);   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     build();     cin >> n >> q;     for (int i = 1; i <= n; i++) cin >> val[i];     for (int i = 1; i < n; i++) {       int x, y;       cin >> x >> y;       a[x].push_back(y);       a[y].push_back(x);     }     dfs(1, 1);     while (q--) {       int type;       cin >> type;       if (type == 2) {         for (int i = 1; i <= n; i++) ans[i] = 0, h[i] = 0;         for (int i = 1; i < MAXP; i++) prim[i].clear();         int x, y;         cin >> x >> y;         val[x] = y;         dfs(1, 1);       } else {         int x;         cin >> x;         if (ans[x] != 0)           cout << ans[x] <<   n ;         else           cout << -1 <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int MN = 1000010;   int exp(int x, int n) {     int ret = 1;     while (n) {       if (n & 1) ret = 1LL * ret * x % mod;       x = 1LL * x * x % mod;       n >>= 1;     }     return ret;   }   int inv(int x) { return exp(x, mod - 2); }   int fact[MN], invf[MN];   int K, W, ans;   int main() {     fact[0] = 1;     for (int i = 1; i < MN; i++) {       fact[i] = 1LL * fact[i - 1] * i % mod;     }     for (int i = 0; i < MN; i++) {       invf[i] = inv(fact[i]);     }     cin >> K >> W;     for (int i = 1; i <= min(K, W - 1); i++) {       ans += 1LL * i * fact[K] % mod * fact[K] % mod * exp(K, W - 1 - i) % mod *              invf[K - i] % mod * invf[K - i] % mod;       ans %= mod;     }     for (int i = W; i <= K; i++) {       ans += 1LL * W * fact[K] % mod * fact[K - i + W - 1] % mod * invf[K - i] %              mod * invf[K - i] % mod;       ans %= mod;     }     for (int i = 1; i <= min(K - 1, W - 2); i++) {       ans += 1LL * i * fact[K] % mod * fact[K] % mod * exp(K, W - 2 - i) % mod *              invf[K - i] % mod * invf[K - i - 1] % mod;       ans %= mod;     }     for (int i = W - 1; i <= K - 1; i++) {       ans += 1LL * (W - 1) * fact[K] % mod * fact[K - i + W - 2] % mod *              invf[K - i] % mod * invf[K - i - 1] % mod;       ans %= mod;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const long long int inf = 9e18;   const long double pi = 2 * acos(0.0);   long long int arr[100005];   void solve() {     long long int n, k;     cin >> n >> k;     ;     for (int i = 0; i < n; i++) cin >> arr[i];     ;     long long int mn = inf, pos = 0;     for (long long int i = 0; i < n; ++i) {       if (arr[i] < mn) {         mn = arr[i];         pos = i;       }     }     long long int ans = 0;     for (long long int i = 0; i < n; ++i) {       if (i == pos) continue;       long long int t = abs(k - arr[i]);       ans += (t / mn);     }     cout << ans <<   n ;     return;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int tc = 1;     cin >> tc;     while (tc--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   string s;   vector<vector<int> > a[28];   vector<int> t, v1, v2;   int ans = 1e9;   void gen(int ind) {     if (ind == 3) {       a[t[0] + t[1] + t[2]].push_back(t);       return;     }     for (int i = 0; i <= 9; i++) {       t.push_back(i);       gen(ind + 1);       t.pop_back();     }   }   int dif(vector<int> aa, vector<int> bb) {     int ret = 0;     for (int i = 0; i < aa.size(); i++) ret += (aa[i] != bb[i]);     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     cin >> s;     v1.push_back(s[0] -  0 );     v1.push_back(s[1] -  0 );     v1.push_back(s[2] -  0 );     v2.push_back(s[3] -  0 );     v2.push_back(s[4] -  0 );     v2.push_back(s[5] -  0 );     gen(0);     for (int i = 0; i <= 27; i++)       for (int j = 0; j < a[i].size(); j++)         for (int k = 0; k < a[i].size(); k++)           ans = min(ans, dif(v1, a[i][j]) + dif(v2, a[i][k]));     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct trie {     trie* child[2];     int cnt;     trie() {       child[0] = child[1] = NULL;       cnt = 0;     }   } * root;   int n, k, sum;   long long res;   void add(int val) {     trie* node = root;     for (int i = 29; i >= 0; i--) {       int j = (val >> i) & 1;       if (!node->child[j]) node->child[j] = new trie;       node = node->child[j];       node->cnt++;     }   }   void get(trie* node, int i, int val, int remain) {     if (i == -1) return;     if (remain > (1 << (i + 1)) - 1) return;     int j = (val >> i) & 1;     if (node->child[j ^ 1]) {       if ((1 << i) >= remain)         res += node->child[j ^ 1]->cnt;       else         get(node->child[j ^ 1], i - 1, val, remain - (1 << i));     }     if (node->child[j]) get(node->child[j], i - 1, val, remain);   }   int main(int argc, char const* argv[]) {     ios ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     root = new trie;     add(0);     for (int i = 1; i <= n; i++) {       int a;       cin >> a;       sum ^= a;       get(root, 29, sum, k);       add(sum);     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, A[1000100], A2[1000010], N;   int main() {     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> A[i];     }     N = n;     long long cumsum = 0;     for (int i = 2, rank = 2; i <= n; i++) {       long long cumsum2 = cumsum + (rank - 2) * A2[rank - 1];       long long d = cumsum2 - (rank - 1) * (N - rank) * A[i];       if (d < k) {         cout << i << endl;         N--;       } else {         A2[rank] = A[i];         rank++;         cumsum = cumsum2;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> vis;   string s[1000009];   int n, m, i;   bool dfs(int x, int y) {     if (x == n - 1 && y == m - 1) return true;     if (x < 0 || y < 0 || x >= n || y >= m || vis[y + x * m] == 1 ||         s[x][y] ==  # )       return false;     if (x || y) {       vis[y + x * m] = 1;     }     return dfs(x + 1, y) || dfs(x, y + 1);   }   int main() {     cin >> n >> m;     for (i = 0; i < n; i++) cin >> s[i];     if (dfs(0, 0) == false) {       cout <<  0 ;       return 0;     }     if (dfs(0, 0) == false) {       cout <<  1 ;       return 0;     }     cout <<  2 ;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int init[110];   string lst[110];   vector<int> fin;   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = (int)0; i < (int)n; ++i) cin >> lst[i];     for (int i = (int)0; i < (int)n; ++i) cin >> init[i];     bool flag = 1;     while (flag) {       flag = 0;       for (int i = (int)0; i < (int)n; ++i)         if (init[i] == 0) {           flag = 1;           fin.push_back(i + 1);           for (int j = (int)0; j < (int)n; ++j) init[j] -= lst[i][j] -  0 ;         }     }     cout << fin.size() << endl;     for (int ele : fin) cout << ele <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mm[110][110];   int tmp[110][110];   void draw(int a, int b, int v) {     for (int lx = 0; lx < a; lx++)       for (int ly = 0; ly < b; ly++) tmp[lx][ly] += v;     return;   }   void prt(int a, int b) {     for (int lx = 0; lx < a; lx++) {       for (int ly = 0; ly < b; ly++) printf( %c , tmp[lx][ly] ?  W  :  B );       puts(  );     }     return;   }   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int lx = 0; lx < n; lx++) {       char buf[200];       scanf( %s , buf);       for (int ly = 0; ly < m; ly++) mm[lx][ly] = (buf[ly] ==  W );     }     int cnt = 1;     for (int lx = 0; lx < n; lx++)       for (int ly = 0; ly < m; ly++) tmp[lx][ly] = mm[n - 1][m - 1];     for (int cc = n + m - 3; cc >= 0; cc--) {       for (int x = 0; x <= cc; x++) {         int y = cc - x;         if (x >= n or y >= m) continue;         if (tmp[x][y] != mm[x][y]) {           draw(x + 1, y + 1, mm[x][y] - tmp[x][y]), cnt++;         }       }     }     printf( %d n , cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     return (!b) ? a : gcd(b, a % b);   }   int n, k;   void solve() {     cin >> n >> k;     string s;     cin >> s;     vector<vector<int>> mvs;     int mxs = 0;     int cnt = 0;     while (cnt <= n) {       vector<int> cr;       for (int i = 0; i <= (int)s.size() - 2; i++) {         if (s[i] ==  R  && s[i + 1] ==  L ) {           cr.push_back(i);           s[i] =  L ;           s[i + 1] =  R ;           i += 1;         }       }       if ((int)cr.size() == 0) break;       mvs.push_back(cr);       mxs += (int)cr.size();       cnt += 1;     }     if (k >= (int)mvs.size() && k <= mxs) {       int rem = (int)mvs.size();       for (auto fv : mvs) {         vector<int> cv = fv;         int i = 0;         while (i < (int)cv.size()) {           int ch = 1;           if (rem == k) ch = (int)cv.size() - i;           cout << ch <<    ;           for (int j = 0; j < ch; j++) {             cout << cv[i] + 1 <<    ;             i += 1;           }           cout <<   n ;           k -= 1;         }         rem -= 1;       }     } else {       cout << -1;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   vector<string> results(100001);   vector<pair<int, string> > wishes;   vector<pair<int, string> > S_M, M_L, L_XL, XL_XXL, XXL_XXXL;   bool compare(pair<int, string> a, pair<int, string> b) {     if (a.second ==  S,M ) return false;     if (a.second ==  M,L ) return b.second ==  S,M ;     if (a.second ==  L,XL ) return b.second ==  S,M  || b.second ==  M,L ;     if (a.second ==  XL,XXL ) return b.second !=  XXL,XXXL ;     return true;   }   int main() {     int S = 0, M = 0, L = 0, XL = 0, XXL = 0, XXXL = 0;     cin >> S >> M >> L >> XL >> XXL >> XXXL;     map<string, int> sizes;     sizes[ S ] = S;     sizes[ M ] = M;     sizes[ L ] = L;     sizes[ XL ] = XL;     sizes[ XXL ] = XXL;     sizes[ XXXL ] = XXXL;     int n;     string s;     cin >> n;     unsigned long long xx = 0;     for (int i = 0; i < n; i++) {       cin >> s;       int pos = s.find( , );       if (pos == string::npos) {         results[i] = s;         if (sizes[s] > 0)           sizes[s]--;         else {           cout <<  NO ;           return 0;         }       } else {         if (s ==  S,M ) S_M.push_back(make_pair(i, s));         if (s ==  M,L ) M_L.push_back(make_pair(i, s));         if (s ==  L,XL ) L_XL.push_back(make_pair(i, s));         if (s ==  XL,XXL ) XL_XXL.push_back(make_pair(i, s));         if (s ==  XXL,XXXL ) {           XXL_XXXL.push_back(make_pair(i, s));           xx++;         }       }     }     wishes.insert(wishes.end(), S_M.begin(), S_M.end());     wishes.insert(wishes.end(), M_L.begin(), M_L.end());     wishes.insert(wishes.end(), L_XL.begin(), L_XL.end());     wishes.insert(wishes.end(), XL_XXL.begin(), XL_XXL.end());     wishes.insert(wishes.end(), XXL_XXXL.begin(), XXL_XXXL.end());     for (int i = 0; i < wishes.size(); i++) {       pair<int, string> wish = wishes[i];       string sz = wish.second;       int ind = wish.first;       int pos = sz.find( , );       string first = sz.substr(0, pos);       string second = sz.substr(pos + 1);       if (sizes[first] == 0 && sizes[second] == 0) {         cout <<  NO ;         return 0;       }       if (sizes[first] != 0) {         results[ind] = first;         sizes[first]--;       } else {         results[ind] = second;         sizes[second]--;       }     }     cout <<  YES  << endl;     for (int i = 0; i < n; i++) cout << results[i] << endl;     return 0;   }


#include <bits/stdc++.h>   struct TSubStr {     TSubStr(const std::string& text, size_t begin, size_t end)         : Text(text), Begin(begin), End(end) {}     const std::string& Text;     size_t Begin;     size_t End;     static inline size_t getId(const std::string& text, size_t begin,                                size_t end) {       size_t id = 0;       size_t power = 1;       for (size_t i = begin; i < end; ++i) {         id += (text[i] -  a  + 1) * power;         power *= 27;       }       return id;     }     static inline size_t getId(const std::string& str) {       size_t id = 0;       size_t power = 1;       for (size_t i = 0; i < str.size(); ++i) {         id += (str[i] -  a  + 1) * power;         power *= 27;       }       return id;     }     inline size_t getId() const {       size_t id = 0;       size_t power = 1;       for (size_t i = Begin; i < End; ++i) {         id += (Text[i] -  a  + 1) * power;         power *= 27;       }       return id;     }   };   void FillIndex(const std::string& text,                  std::vector<std::vector<size_t> >& index) {     index.clear();     index.resize(27 * 27 * 27 * 27);     for (size_t i = 0; i < text.size(); ++i) {       index[TSubStr::getId(text, i, i + 1)].push_back(i);       if (i + 1 < text.size()) index[TSubStr::getId(text, i, i + 2)].push_back(i);       if (i + 2 < text.size()) index[TSubStr::getId(text, i, i + 3)].push_back(i);       if (i + 3 < text.size()) index[TSubStr::getId(text, i, i + 4)].push_back(i);     }   }   std::string ReverseStr(const std::string& str) {     std::string res(str);     std::reverse(res.begin(), res.end());     return res;   }   struct TProblemQuery {     TProblemQuery(const std::string& firstStr, const std::string& secondStr) {       if (firstStr < secondStr) {         FirstStr = firstStr;         SecondStr = secondStr;       } else {         FirstStr = secondStr;         SecondStr = firstStr;       }     }     TProblemQuery(const TProblemQuery& rhs)         : FirstStr(rhs.FirstStr), SecondStr(rhs.SecondStr) {}     TProblemQuery() {}     bool operator==(const TProblemQuery& rhs) const {       return FirstStr == rhs.FirstStr && SecondStr == rhs.SecondStr;     }     std::string FirstStr;     std::string SecondStr;   };   struct TProblemQueryHash {     size_t operator()(const TProblemQuery& query) const {       size_t x = HashFn(query.FirstStr);       size_t y = HashFn(query.SecondStr);       return (x * 1000000009) ^ y;     }     std::hash<std::string> HashFn;   };   inline void TryImprove(       const std::string& freqStr, const std::string& text,       const std::vector<size_t>& freqStrOccurencies,       std::unordered_map<TProblemQuery, size_t, TProblemQueryHash>& results,       int freqStrI, int textI, int len) {     if (textI + len > text.size()) {       return;     }     TProblemQuery query(freqStr, text.substr(textI, len));     if (results.count(query)) {       size_t& res = results[query];       size_t begI = std::min<size_t>(textI, freqStrOccurencies[freqStrI]);       size_t endI = std::max<size_t>(           textI + len, freqStrOccurencies[freqStrI] + freqStr.size());       size_t potentialRes = endI - begI;       if (freqStrI > 0) {         begI = std::min<size_t>(textI, freqStrOccurencies[freqStrI - 1]);         endI = std::max<size_t>(             textI + len, freqStrOccurencies[freqStrI - 1] + freqStr.size());         potentialRes = std::min<size_t>(potentialRes, endI - begI);       }       res = std::min<size_t>(res, potentialRes);     }   }   void ProcessFreq(       const std::string& freqStr, const std::string& text,       const std::vector<std::vector<size_t> >& index,       std::unordered_map<TProblemQuery, size_t, TProblemQueryHash>& results,       std::vector<bool>& processedAsFreq) {     const std::vector<size_t>& freqStrOccurencies =         index[TSubStr::getId(freqStr)];     int freqStrI = 0;     int textI = 0;     while (textI < text.size()) {       if (freqStrI < freqStrOccurencies.size() - 1 &&           textI > freqStrOccurencies[freqStrI]) {         ++freqStrI;         continue;       }       TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 1);       TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 2);       TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 3);       TryImprove(freqStr, text, freqStrOccurencies, results, freqStrI, textI, 4);       ++textI;     }     processedAsFreq[TSubStr::getId(freqStr)] = true;   }   void ProcessQuery(       const TProblemQuery& query, const std::string& text,       const std::vector<std::vector<size_t> >& index,       std::unordered_map<TProblemQuery, size_t, TProblemQueryHash>& results,       std::vector<bool>& processedAsFreq) {     if (index[TSubStr::getId(query.FirstStr)].empty() ||         index[TSubStr::getId(query.SecondStr)].empty()) {       return;     }     if (processedAsFreq[TSubStr::getId(query.FirstStr)] ||         processedAsFreq[TSubStr::getId(query.SecondStr)]) {       return;     }     if (index[TSubStr::getId(query.FirstStr)].size() > 800) {       ProcessFreq(query.FirstStr, text, index, results, processedAsFreq);       return;     }     if (index[TSubStr::getId(query.SecondStr)].size() > 800) {       ProcessFreq(query.SecondStr, text, index, results, processedAsFreq);       return;     }     const std::vector<size_t>& firstStrOccurencies =         index[TSubStr::getId(query.FirstStr)];     const std::vector<size_t>& secondStrOccurencies =         index[TSubStr::getId(query.SecondStr)];     size_t& res = results[query];     int firstI = 0;     int secondI = 0;     int potentialSubstrBegin =         std::min(firstStrOccurencies[firstI], secondStrOccurencies[secondI]);     int potentialSubstrEnd =         std::max(firstStrOccurencies[firstI] + query.FirstStr.size(),                  secondStrOccurencies[secondI] + query.SecondStr.size());     size_t potentialRes = potentialSubstrEnd - potentialSubstrBegin;     res = std::min(res, potentialRes);     while (secondI < secondStrOccurencies.size() &&            secondStrOccurencies[secondI] < firstStrOccurencies[firstI]) {       potentialSubstrBegin =           std::min(firstStrOccurencies[firstI], secondStrOccurencies[secondI]);       potentialSubstrEnd =           std::max(firstStrOccurencies[firstI] + query.FirstStr.size(),                    secondStrOccurencies[secondI] + query.SecondStr.size());       potentialRes = potentialSubstrEnd - potentialSubstrBegin;       res = std::min(res, potentialRes);       ++secondI;     }     while (secondI < secondStrOccurencies.size()) {       if (firstI < firstStrOccurencies.size() - 1 &&           firstStrOccurencies[firstI + 1] < secondStrOccurencies[secondI]) {         ++firstI;         continue;       }       potentialSubstrBegin =           std::min(firstStrOccurencies[firstI], secondStrOccurencies[secondI]);       potentialSubstrEnd =           std::max(firstStrOccurencies[firstI] + query.FirstStr.size(),                    secondStrOccurencies[secondI] + query.SecondStr.size());       potentialRes = potentialSubstrEnd - potentialSubstrBegin;       res = std::min(res, potentialRes);       if (firstI < firstStrOccurencies.size() - 1) {         potentialSubstrBegin = std::min(firstStrOccurencies[firstI + 1],                                         secondStrOccurencies[secondI]);         potentialSubstrEnd =             std::max(firstStrOccurencies[firstI + 1] + query.FirstStr.size(),                      secondStrOccurencies[secondI] + query.SecondStr.size());         potentialRes = potentialSubstrEnd - potentialSubstrBegin;         res = std::min(res, potentialRes);       }       ++secondI;     }   }   int main() {     std::string text;     std::cin >> text;     std::vector<std::vector<size_t> > index;     FillIndex(text, index);     std::vector<bool> processedAsFreq(27 * 27 * 27 * 27, false);     std::unordered_map<TProblemQuery, size_t, TProblemQueryHash> results;     results.max_load_factor(3.0);     results.reserve(300000);     std::vector<TProblemQuery> queries;     size_t m;     std::cin >> m;     size_t NO_ANSWER = 1000000009;     for (size_t i = 0; i < m; ++i) {       std::string strFirst;       std::string strSecond;       std::cin >> strFirst;       std::cin >> strSecond;       TProblemQuery query(strFirst, strSecond);       queries.push_back(query);       results[query] = NO_ANSWER;     }     for (size_t i = 0; i < m; ++i) {       ProcessQuery(queries[i], text, index, results, processedAsFreq);       size_t res = results[queries[i]];       std::cout << (res == NO_ANSWER ? -1 : (int)res) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int mex = 1e9;     for (int i = 0; i < m; i++) {       int l, r;       cin >> l >> r;       mex = min(mex, r - l + 1);     }     cout << mex <<   n ;     for (int i = 0; i < n; i++) {       cout << i % mex <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int maxn = 1e4 + 5;   class Matrix {    public:     long long a[3][3];     int n;     void Init(int key) {       memset(a, 0, sizeof a);       if (key)         for (int i = 0; i < n; i++) a[i][i] = 1;     }     Matrix operator*(const Matrix& b) {       Matrix c;       c.n = n;       c.Init(0);       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++)           for (int k = 0; k < n; k++)             c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j] % mod) % mod;       return c;     }     Matrix Power(long long t) {       Matrix ans, p = *this;       ans.n = p.n;       ans.Init(1);       while (t) {         if (t & 1) ans = ans * p;         p = p * p;         t >>= 1;       }       return ans;     }     void Print() {       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           if (j == 0)             printf( %lld , a[i][j]);           else             printf(  %lld , a[i][j]);         }         puts(  );       }     }   };   struct node {     int x, tag;     long long y;     friend bool operator<(const node& a, const node& b) { return a.y < b.y; }   } a[maxn * 2];   long long sum[3], b[3];   void calc(long long n) {     Matrix f, A;     f.Init(0);     A.Init(0);     f.n = A.n = 3;     for (int i = 0; i < 3; i++) {       for (int j = 0; j < 3; j++) {         if (sum[j] == 0)           f.a[i][j] = 1;         else           f.a[i][j] = 0;       }     }     f.a[0][2] = f.a[2][0] = 0;     A.a[0][0] = b[0], A.a[0][1] = b[1], A.a[0][2] = b[2];     A = A * f.Power(n);     b[0] = A.a[0][0], b[1] = A.a[0][1], b[2] = A.a[0][2];   }   int main() {     int n;     long long m;     scanf( %d%lld , &n, &m);     int cnt = 0;     for (int i = 1; i <= n; i++) {       int x;       long long l, r;       scanf( %d%lld%lld , &x, &l, &r);       a[++cnt].x = x - 1, a[cnt].y = l - 1, a[cnt].tag = 1;       a[++cnt].x = x - 1, a[cnt].y = r, a[cnt].tag = -1;     }     sort(a + 1, a + cnt + 1);     b[0] = b[2] = 0, b[1] = 1;     long long cur = 1;     for (int i = 1; i <= cnt; i++) {       calc(a[i].y - cur);       cur = a[i].y;       sum[a[i].x] += a[i].tag;     }     calc(m - cur);     printf( %lld n , b[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool smin(T &a, const T &b) {     return b < a ? a = b, 1 : 0;   }   template <typename T>   inline bool smax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   const long long N = 1e5 + 10, mod = (long long)1e9 + 7;   long long n, k, ps[N], f[N], inv[N], pw10[N];   string s;   long long pw(long long x, long long y) {     x %= mod;     long long ret = 1;     for (; y; y >>= 1, (x *= x) %= mod)       if (y & 1) (ret *= x) %= mod;     return ret;   }   long long C(long long n, long long r) {     if (r > n || r < 0 || n < 0) return 0;     return ((f[n] * inv[r] % mod) * inv[n - r] % mod) % mod;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k >> s;     ps[0] = s[0] -  0 ;     for (long long i = 1; i < n; ++i) (ps[i] = ps[i - 1] + (s[i] -  0 )) %= mod;     f[0] = inv[0] = 1;     for (long long i = 1; i < N; ++i) {       (f[i] = f[i - 1] * i) %= mod;       inv[i] = pw(f[i], mod - 2);     }     pw10[0] = 1;     for (long long i = 1; i < N; ++i) (pw10[i] = pw10[i - 1] * 10) %= mod;     long long res = 0;     for (long long sz = 0; sz <= n - 2; ++sz) {       long long add =           pw10[sz] * C(n - sz - 2, k - 1) % mod * ps[n - sz - 2] % mod;       (res += add) %= mod;     }     for (long long i = 0; i < n; ++i) {       long long add = (s[i] -  0 ) * pw10[n - i - 1] % mod * C(i, k);       (res += add) %= mod;     }     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   const int inf = 0x3f3f3f3f;   const double eps = 1e-8;   map<int, int> ma;   int n, x, num;   int getId(int x) {     if (ma.count(x) == 0) {       ma[x] = num++;     }     return ma[x];   }   int cnt[maxn];   vector<int> v;   int main() {     while (~scanf( %d , &n)) {       memset(cnt, 0, sizeof(cnt));       num = 1;       ma.clear();       v.clear();       for (int i = 1; i <= n; i++) {         scanf( %d , &x);         cnt[getId(x)]++;         x = getId(x);       }       for (int i = 0; i <= n + 1; i++) {         if (cnt[i] != 0) v.push_back(cnt[i]);       }       sort(v.begin(), v.end());       int now = 0, ans = 0;       int len = v.size();       for (int i = 1; i <= v[len - 1]; i++) {         int sum = 0;         now = 0;         for (int j = i;;) {           int pos = lower_bound(v.begin() + now, v.end(), j) - v.begin();           if (pos == len) break;           now = pos + 1;           sum += j;           j = j * 2;         }         ans = max(ans, sum);       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native )   #pragma GCC optimize( unroll-loops )   #pragma GCC optimize( fast-math )   using namespace std;   mt19937 rnd(time(0) + 228 +  k  +  e  +  k  +  e  +  r  +  o  +  f  +  e  +                y );   vector<int> genvec(int sz, int minelem, int maxelem) {     ++maxelem;     vector<int> ans(sz);     for (int i = 0; i < sz; ++i) {       ans[i] = minelem + rnd() % (maxelem - minelem);     }     return ans;   }   const long double EPS = 1e-14;   bool checkprime(int x) {     for (int i = 2; i * i <= x; ++i)       if (x % i == 0) return 0;     return 1;   }   const long double PI = acos(-1);   const int MOD7 = 1000000007;   const int MOD9 = 1000000009;   const long long INF = 2e18;   int mod = MOD7;   const int inf = 1e9;   signed main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     srand(time(NULL));     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> p(2 * n);       for (long long i = 0; i < p.size(); ++i) cin >> p[i];       ;       int mx = 0;       vector<int> luls;       int cnt = 0;       for (int i : p) {         if (i > mx) {           mx = i;           if (cnt) luls.push_back(cnt);           cnt = 1;         } else {           ++cnt;         }       }       luls.push_back(cnt);       vector<bool> dp(n + 1);       dp[0] = 1;       int ps = 0;       for (int i : luls) {         ps += i;         for (int sum = n; sum >= 0; --sum) {           if (!dp[sum]) continue;           if (ps - sum - i > n) continue;           if (sum + i <= n) {             dp[sum + i] = 1;           }         }       }       cout << (dp[n] ?  YES  :  NO ) << endl;     }   }


#include <bits/stdc++.h>   const int N = 1e5 + 10;   const int LOGN = 20;   long long ans;   int n;   int pre[N << 1], to[N << 1], head[N], wcnt;   long long dis[N];   int val[N], fa[N];   int dep[N];   int table[LOGN][N << 1];   int LOG2[N << 1];   int idcnt;   int st[N];   inline int minp(int x, int y) { return (dep[x] < dep[y]) ? x : y; }   inline void swap(int &x, int &y) {     int t = x;     x = y;     y = t;   }   template <class T>   inline T max(const T &x, const T &y) {     return (x < y) ? y : x;   }   inline void addedge(int u, int v) {     wcnt++;     pre[wcnt] = head[u];     head[u] = wcnt;     to[wcnt] = v;   }   inline void add2edge(int u, int v) {     addedge(u, v);     addedge(v, u);   }   void init() {     int i, j, k;     for (i = int(2); i <= int(idcnt); i++) LOG2[i] = LOG2[i >> 1] + 1;     for (i = k = 1; k <= idcnt; i++, k <<= 1)       for (j = 1; j <= int(idcnt - k); j++)         table[i][j] = minp(table[i - 1][j], table[i - 1][j + k]);   }   inline long long getdis(int x, int y) {     int l, r, k, z;     l = st[x];     r = st[y];     if (l > r) swap(l, r);     k = LOG2[r - l + 1];     z = minp(table[k][l], table[k][r - (1 << k) + 1]);     return dis[x] + dis[y] - 2 * dis[z] + val[z];   }   struct Node {     int x, y;     long long d;     Node() {       x = y = 0;       d = -1;     }     Node(int x, int y) : x(x), y(y) { d = getdis(x, y); }     bool operator<(const Node &b) const { return d < b.d; }     Node operator+(const Node &b) const {       if (d == -1)         return b;       else if (b.d == -1)         return *this;       Node res = max(*this, b);       res = max(res, Node(x, b.x));       res = max(res, Node(x, b.y));       res = max(res, Node(y, b.x));       res = max(res, Node(y, b.y));       return res;     }   };   Node sum[N], g[N];   void predfs(int u) {     int i;     table[0][st[u] = ++idcnt] = u;     dis[u] = dis[fa[u]] + val[u];     for (i = head[u]; i; i = pre[i])       if (fa[u] ^ to[i]) {         fa[to[i]] = u;         dep[to[i]] = dep[u] + 1;         predfs(to[i]);         table[0][++idcnt] = u;       }   }   void dfs1(int u) {     int i;     sum[u] = Node(u, u);     for (i = head[u]; i; i = pre[i])       if (fa[u] ^ to[i]) {         dfs1(to[i]);         sum[u] = sum[u] + sum[to[i]];       }   }   void dfs2(int u) {     static Node pref[N], suff[N];     static int son[N];     int i, sn = 0;     for (i = head[u]; i; i = pre[i])       if (fa[u] ^ to[i]) son[++sn] = to[i];     pref[0] = suff[sn + 1] = g[u] + Node(u, u);     for (i = 1; i <= int(sn); i++) pref[i] = pref[i - 1] + sum[son[i]];     for (i = int(sn); i; i--) suff[i] = suff[i + 1] + sum[son[i]];     for (i = 1; i <= int(sn); i++) g[son[i]] = pref[i - 1] + suff[i + 1];     for (i = int(0); i <= int(sn + 1); i++) pref[i] = suff[i] = Node();     if (u > 1) ans = max(ans, sum[u].d + g[u].d);     for (i = head[u]; i; i = pre[i])       if (fa[u] ^ to[i]) dfs2(to[i]);   }   int main() {     int i;     int u, v;     scanf( %d , &n);     for (i = 1; i <= int(n); i++) scanf( %d , &val[i]);     for (i = 1; i <= int(n - 1); i++) {       scanf( %d %d , &u, &v);       add2edge(u, v);     }     predfs(1);     init();     dfs1(1);     ans = sum[1].d;     dfs2(1);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   ifstream fin( input.txt );   ofstream fout( output.txt );   bool isPrime(int x) {     if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;     for (int i = 5; i * i <= x; i += 6)       if (x % i == 0 || x % (i + 2) == 0) return 0;     return 1;   }   inline void boostIO() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int main() {     boostIO();     int n, s;     cin >> n >> s;     int prev = 0;     int h = 0;     int m = 0;     for (int i = 0; i < (n); ++i) {       cin >> h >> m;       m += h * 60;       if (i == 0 && m - prev >= s + 1) {         cout << 0 <<     << 0 << endl;         return 0;       }       if (m - prev >= s * 2 + 2) {         cout << (prev + s + 1) / 60 <<     << (prev + s + 1) % 60 << endl;         return 0;       }       prev = m;     }     cout << (prev + s + 1) / 60 <<     << (prev + s + 1) % 60;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5006;   const int mod = 1000000009;   double x[maxn], y[maxn], z[maxn];   double d[maxn][maxn];   int n;   double dis(double x1, double y1, double z1, double x2, double y2, double z2) {     double dx = x1 - x2;     double dy = y1 - y2;     double dz = z1 - z2;     return sqrt(dx * dx + dy * dy + dz * dz);   }   double getans() {     double ret = 100000000000.0;     for (int i = 1; i < n; ++i)       for (int j = i + 1; j < n; ++j) {         double dd = d[i][j] + d[0][i] + d[0][j];         ret = min(ret, dd);       }     return ret;   }   void init() {     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j)         d[i][j] = dis(x[i], y[i], z[i], x[j], y[j], z[j]);   }   int main() {     while (~scanf( %d , &n)) {       for (int i = 0; i < n; ++i) {         scanf( %lf%lf%lf , x + i, y + i, z + i);       }       init();       double ans = getans();       ans /= 2;       printf( %.9lf n , ans);     }     return 0;   }


#include <bits/stdc++.h>   template <typename Y>   inline bool updmin(Y &a, Y b) {     if (a > b) {       a = b;       return 1;     }     return 0;   }   template <typename Y>   inline bool updmax(Y &a, Y b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   template <typename Y>   inline Y abs(Y a) {     if (a < 0) a = -a;     return a;   }   template <typename Y>   inline Y sqr(Y a) {     return a * a;   }   int read() {     int w = 1, q = 0, ch =    ;     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) w = -1;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) q = q * 10 + ch - 48;     return q * w;   }   inline void FileIO() {     freopen(                     .in ,          r , stdin);     freopen(                     .out ,          w , stdout);   }   const int maxn = 800010;   std::vector<std::pair<int, long long> > G[maxn];   int n, m, fa[maxn];   long long a[maxn], d[maxn], dk[maxn], df[maxn];   int dep[maxn], dfn[maxn], st[maxn][21], dfc = 0, lg[maxn];   int lca(int a, int b) {     a = dfn[a];     b = dfn[b];     if (a > b) {       std::swap(a, b);     }     int L = lg[b - a], u = st[a][L], v = st[b - (1 << L) + 1][L];     return dep[u] < dep[v] ? u : v;   }   void up(int p) {     d[p] = a[p];     st[++dfc][0] = p;     dfn[p] = dfc;     dep[p] = dep[fa[p]] + 1;     for (auto t : G[p]) {       if (t.first == fa[p]) continue;       fa[t.first] = p;       up(t.first);       dk[t.first] = std::max(0LL, d[t.first] - 2 * t.second);       d[p] += dk[t.first];       st[++dfc][0] = p;     }   }   void down(int p) {     for (auto t : G[p]) {       if (t.first == fa[p]) continue;       df[t.first] = std::max(0LL, d[p] - dk[t.first] - 2 * t.second);       d[t.first] += df[t.first];       down(t.first);     }   }   long long ans[maxn];   std::vector<std::pair<int, int> > Q[maxn];   long long tr[maxn];   inline void add(int p, long long v) {     if (!p) return;     for (; p <= n; p += p & -p) {       tr[p] += v;     }   }   inline long long ask(int p) {     long long r = 0;     for (; p; p -= p & -p) {       r += tr[p];     }     return r;   }   inline long long ask(int l, int r) { return ask(r) - ask(l - 1); }   void dfs(int p, long long ev) {     add(dep[p] - 1, -ev - dk[p]);     add(dep[p], d[p] - df[p]);     for (auto q : Q[p]) {       ans[q.second] += ask(dep[q.first], dep[p]);     }     for (auto t : G[p]) {       if (t.first == fa[p]) continue;       dfs(t.first, t.second);     }     add(dep[p] - 1, ev + dk[p]);     add(dep[p], df[p] - d[p]);   }   int main() {     n = read();     m = read();     for (int i = 1; i <= n; i++) {       a[i] = read();     }     for (int i = 1; i < n; i++) {       int x = read(), y = read();       long long z = read();       G[x].push_back(std::make_pair(y, z));       G[y].push_back(std::make_pair(x, z));     }     up(1);     down(1);     lg[1] = 0;     for (int i = 2; i <= dfc; i++) {       lg[i] = lg[i >> 1] + 1;     }     for (int k = 1; k <= 20; k++) {       for (int i = 1; i + (1 << k) - 1 <= dfc; i++) {         int u = st[i][k - 1], v = st[i + (1 << (k - 1))][k - 1];         st[i][k] = dep[u] < dep[v] ? u : v;       }     }     for (int i = 1; i <= m; i++) {       int u = read(), v = read();       if (u == v) {         ans[i] = d[u];         continue;       }       int l = lca(u, v);       if (l != u && l != v) {         ans[i] += 2 * df[l] - d[l];       } else {         ans[i] += df[l];       }       if (l != u) {         Q[u].push_back(std::make_pair(l, i));       }       if (l != v) {         Q[v].push_back(std::make_pair(l, i));       }     }     dfs(1, 0);     for (int i = 1; i <= m; i++) {       printf( %lld n , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int main() {     long long k, d, t;     scanf( %lld %lld %lld , &k, &d, &t);     if (k % d == 0) {       printf( %.10lf n , (double)t);       return 0;     }     t *= 2;     long long period = (k / d + 1) * d;     long long periodPoints = 2 * k + period - k;     long long nPeriods = t / periodPoints;     long long timeSpent = nPeriods * periodPoints;     long long rest = t - timeSpent;     if (rest == 0) {       printf( %.10lf n , (double)nPeriods * period);       return 0;     }     if (2 * k >= rest) {       printf( %.10lf n , (double)nPeriods * period + rest / 2.0);       return 0;     }     printf( %.10lf n , (double)nPeriods * period + rest - k);   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void gn(T &first) {     char c, sg = 0;     while (c = getchar(), (c >  9  || c <  0 ) && c !=  - )       ;     for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ;          c = getchar())       first = (first << 1) + (first << 3) + c -  0 ;     if (sg) first = -first;   }   template <class T1, class T2>   inline void gn(T1 &x1, T2 &x2) {     gn(x1), gn(x2);   }   char S1[1000100], S2[1000100];   char B[1000100 * 3];   int fail[1000100 * 3];   void build_fail_function(char B[], int len) {     int j = fail[0] = -1;     for (int i = 1; i < len; i++) {       while (j != -1 and B[j + 1] != B[i]) j = fail[j];       if (B[j + 1] == B[i]) j++;       fail[i] = j;     }   }   int main() {     int n;     gn(n);     n--;     scanf( %s %s , S1, S2);     for (int i = 0; i < n; i++) {       if (S1[i] ==  N )         S1[i] =  S ;       else if (S1[i] ==  S )         S1[i] =  N ;       else if (S1[i] ==  W )         S1[i] =  E ;       else         S1[i] =  W ;     }     int nn = 0;     for (int i = 0; i < n; i++) B[nn++] = S1[n - i - 1];     B[nn++] =  * ;     for (int i = 0; i < n; i++) B[nn++] = S2[i];     build_fail_function(B, nn);     if (fail[nn - 1] == -1) return puts( YES );     puts( NO );   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2500 + 5;   int cnt = 0;   int rec[MAXN], mp[MAXN][MAXN], dp[MAXN][MAXN];   bool prime[MAXN];   void init(int n) {     memset(prime, true, sizeof(prime));     prime[0] = prime[1] = false;     for (int i = 2; i <= n; i++) {       if (prime[i]) rec[cnt++] = i;       for (int j = 0; j < cnt && rec[j] <= n / i; j++) {         prime[i * rec[j]] = false;         if (i % rec[j] == 0) break;       }     }   }   int main() {     init(600);     int n, m;     scanf( %d%d , &n, &m);     memset(mp, 0, sizeof(mp));     memset(dp, 0, sizeof(dp));     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         scanf( %1d , &mp[i][j]);         dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1] + mp[i][j];       }     int ans = dp[n][m], up = max(n, m);     for (int i = 0; rec[i] < up && i < cnt; i++) {       int tmp = 0;       for (int j = 0; j < n + rec[i]; j += rec[i])         for (int k = 0; k < m + rec[i]; k += rec[i]) {           int sum = dp[min(n, j + rec[i])][min(m, k + rec[i])] -                     dp[min(n, j)][min(m, k + rec[i])] -                     dp[min(n, j + rec[i])][min(m, k)] + dp[min(n, j)][min(m, k)];           tmp += min(rec[i] * rec[i] - sum, sum);         }       ans = min(ans, tmp);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> prefix_function(string t, string s, int k) {     int m = (int)s.size();     s = t +  ?  + s;     int n = (int)s.length();     vector<int> pi(n);     for (int i = 1; i < n; ++i) {       int j = pi[i - 1];       while (j > 0 && s[i] != s[j]) j = pi[j - 1];       if (s[i] == s[j]) ++j;       pi[i] = j;     }     if (k >= (int)t.size()) {       for (int i = t.size() + 1, ii = 1; i < n; i++, ii++) {         if (pi[i] == t.size()) {           if (m - ii >= k) {             cout <<  Yes  << endl;             int x = ii - k + 1, y = ii + 1;             if (x <= 0) y += 1 - x, x += 1 - x;             cout << x <<     << y << endl;             exit(0);           }           int jj = ii - (int)t.size();           if (jj >= k) {             cout <<  Yes  << endl;             int x = jj - k + 1, y = jj + 1;             if (y > m - k + 1) x += m - k + 1 - y, y += m - k + 1 - y;             cout << x <<     << y << endl;             exit(0);           }         }       }     }     vector<int> occ(t.size() + 1, -1);     for (int i = t.size() + k; i < n; i++) {       int j = pi[i];       int idx = i - t.size() - 1;       while (occ[j] == -1) {         occ[j] = idx;         if (j == 0) break;         j = pi[j - 1];       }     }     return occ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m, k;     cin >> n >> m >> k;     string s, t;     cin >> s >> t;     vector<int> L = prefix_function(t, s, k);     reverse(t.begin(), t.end());     reverse(s.begin(), s.end());     vector<int> R = prefix_function(t, s, k);     for (int i = 0; i < R.size(); i++) {       if (R[i] == -1) continue;       R[i] = n - 1 - R[i];     }     for (int l = 0; l <= m; l++) {       int r = m - l;       if (L[l] == -1 or R[r] == -1 or l > k or r > k) continue;       if (L[l] < R[r]) {         cout <<  Yes  << endl;         cout << L[l] - k + 1 + 1 <<     << R[r] + 1 << endl;         exit(0);       }     }     cout <<  No  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1010;   int n, m;   int fw[N][N], fb[N][N];   int dx[8] = {-2, -2, 2, 2, 1, 1, -1, -1};   int dy[8] = {1, -1, 1, -1, 2, -2, 2, -2};   queue<pair<int, int> > q;   void bfs(int x, int y, int f[][N]) {     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) f[i][j] = -1;     f[x][y] = 0;     q.push(pair<int, int>(x, y));     while (!q.empty()) {       pair<int, int> h = q.front();       q.pop();       int x = h.first, y = h.second;       for (int i = 0; i < 8; ++i) {         int xx = x + dx[i], yy = y + dy[i];         if (xx <= 0 || xx > n || yy <= 0 || yy > m) continue;         if (f[xx][yy] == -1) {           f[xx][yy] = f[x][y] + 1;           q.push(pair<int, int>(xx, yy));         }       }     }   }   void go(int x, int y, int f[][N]) {     while (f[x][y]) {       for (int i = 0; i < 8; ++i) {         int xx = x + dx[i], yy = y + dy[i];         if (xx <= 0 || xx > n || yy <= 0 || yy > m) continue;         if (f[xx][yy] != -1 && f[xx][yy] < f[x][y]) {           x = xx, y = yy;           printf( %d %d n , x, y);           fflush(stdout);           break;         }       }     }   }   int main() {     int xw, yw, xb, yb, hn, hm;     scanf( %d%d , &n, &m);     scanf( %d%d%d%d , &xw, &yw, &xb, &yb);     hn = n >> 1, hm = m >> 1;     bfs(hn, hm, fw);     bfs(hn + 1, hm, fb);     double tw = fw[xw][yw];     double tb = fb[xb][yb] + 0.5;     double twb = fb[xw][yw];     double tbb = fw[xb][yb] + 0.5;     if (((xw + yw) & 1) ^ ((xb + yb) & 1)) {       if (tw < tb) {         puts( WHITE );         go(xw, yw, fw);       } else if (twb < tb + 1) {         puts( WHITE );         if (fb[xw][yw]) {           go(xw, yw, fb);         } else {           printf( %d %d n , xw - 2, yw - 1);           printf( %d %d n , xw, yw);         }         go(hn + 1, hm, fw);       } else {         puts( BLACK );         go(xb, yb, fb);       }     } else {       if (tb < tw) {         puts( BLACK );         go(xb, yb, fb);       } else if (tbb < tw + 1) {         puts( BLACK );         if (fw[xb][yb]) {           go(xb, yb, fw);         } else {           printf( %d %d n , xb - 2, yb - 1);           printf( %d %d n , xb, yb);         }         go(hn, hm, fb);       } else {         puts( WHITE );         go(xw, yw, fw);       }     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int N;     std::cin >> N;     std::set<std::pair<int, int>> doubled;     std::set<std::pair<int, int>> pend;     int to_double = 0;     int f_doubled = 0;     int64_t total_damage = 0;     int t;     int d;     for (int i = 0; i < N; i++) {       std::cin >> t;       std::cin >> d;       total_damage += d;       to_double += t * (d > 0 ? 1 : -1);       if (d > 0) {         pend.insert({d, t});       } else {         auto search = doubled.find({-d, t});         if (search != doubled.end()) {           doubled.erase(search);           if (t == 0) f_doubled--;           total_damage += d;         } else {           pend.erase(pend.find({-d, t}));         }         if (t == 1) {           if (doubled.size() > 0) {             auto it_doubled = doubled.begin();             std::pair<int, int> max = *it_doubled;             doubled.erase(it_doubled);             pend.insert(max);             if (max.second == 0) f_doubled--;             total_damage -= max.first;           }         }       }       if (to_double > doubled.size()) {         if (pend.size() > 0) {           auto it_pend = --pend.end();           std::pair<int, int> max = *it_pend;           doubled.insert(max);           pend.erase(it_pend);           if (max.second == 0) f_doubled++;           total_damage += max.first;         }       } else if (doubled.size() == to_double) {         if (to_double > 0 && pend.size() > 0) {           auto it_doubled = doubled.begin();           auto it_pend = --pend.end();           std::pair<int, int> min = *it_doubled;           std::pair<int, int> max = *it_pend;           if (max.first > min.first) {             if (max.second == 0) f_doubled++;             if (min.second == 0) f_doubled--;             doubled.erase(it_doubled);             pend.erase(it_pend);             doubled.insert(max);             pend.insert(min);             total_damage += max.first - min.first;           }         }       }       if (doubled.size() > 0 && f_doubled == 0 && doubled.size() == to_double) {         auto it_doubled = doubled.begin();         doubled.erase(it_doubled);         std::pair<int, int> min = *it_doubled;         total_damage -= min.first;         if (pend.size() > 0) {           auto it_pend = --pend.end();           std::pair<int, int> max = *it_pend;           pend.erase(it_pend);           doubled.insert(max);           f_doubled++;           total_damage += max.first;         }         pend.insert(min);       }       printf( %lld n , total_damage);     }     return 0;   }