#include <bits/stdc++.h>   using namespace std;   int n, m, b, mod;   int a[505];   int dp[505][505];   int main() {     scanf( %d%d%d%d , &n, &m, &b, &mod);     for (int i = 1; i <= n; ++i) scanf( %d , a + i);     dp[0][0] = 1;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         for (int k = a[i]; k <= b; ++k) {           dp[j][k] += dp[j - 1][k - a[i]];           if (dp[j][k] >= mod) dp[j][k] -= mod;         }       }     }     int ans = 0;     for (int i = 0; i <= b; ++i) {       ans += dp[m][i];       if (ans >= mod) ans -= mod;     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100050, maxm = 5050;   int n, m, K, p, h[maxn], a[maxn];   long long hh[maxn], ans;   long long tim[maxn];   int cnt[maxm];   bool judg(long long x) {     int i, j;     long long tis = 0;     for (i = 0; i < m; i++) cnt[i] = 0;     for (i = 0; i < n; i++) tis += tim[i] = max(0LL, (hh[i] - x - 1 + p) / p);     if (tis > K * m) return 0;     for (i = 0; i < n; i++) {       int now = 0, h0 = h[i];       for (j = 0; j < tim[i]; j++) {         long long t = h0 + 1LL * (m - now) * a[i] - 1LL * (tim[i] - j - 1) * p -                       x,                   tn;         if (h0 < t)           tn = now + (t - h0 - 1) / a[i] + 1;         else           tn = now;         if (tn >= m) return 0;         h0 = max(0LL, h0 + (tn - now) * a[i] - p);         cnt[now = tn]++;       }     }     for (i = m - 1; i >= 0; i--) {       cnt[i] += cnt[i + 1];       if (cnt[i] > (m - i) * K) return 0;     }     return 1;   }   int main() {     int i, j;     long long l = 0, r = 0, mid;     scanf( %d%d%d%d , &n, &m, &K, &p);     for (i = 0; i < n; i++)       scanf( %d%d , &h[i], &a[i]), r = max(r, hh[i] = h[i] + 1LL * a[i] * m);     while (l < r) {       mid = (l + r) >> 1;       if (judg(mid))         r = mid;       else         l = mid + 1;     }     ans = l;     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 7;   int n, num[100], cnt[N];   long long a[N];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) {       long long temp = a[i];       while (temp && temp % 2 == 0) {         cnt[i]++;         temp /= 2;       }       num[cnt[i]]++;     }     int id = 0;     for (int i = 1; i <= 63; i++)       if (num[i] > num[id]) id = i;     cout << n - num[id] << endl;     for (int i = 1; i <= n; i++)       if (cnt[i] != id) cout << a[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000000 + 2;   const long long MOD = 1e9 + 7;   int N, p[MAXN];   long long s[MAXN], c[MAXN], f[MAXN], Ans;   long long gcd(long long a, long long b) {     if (a < b) swap(a, b);     return b ? gcd(b, a % b) : a;   }   int main() {     cin >> N;     for (int i = 1; i <= N; i++) scanf( %lld , s + i);     for (int i = 2; i <= N; i++) scanf( %d , p + i);     for (int i = N; i; i--) s[p[i]] += s[i];     for (int i = N; i; i--) {       s[i] = s[1] / gcd(s[1], s[i]);       if (s[i] <= N) c[s[i]]++;     }     for (int i = N; i; i--)       for (int j = 2 * i; j <= N; j += i) c[j] = (c[j] + c[i]) % MOD;     f[1] = 1;     for (int i = 1; i <= N; i++)       if (c[i] >= i)         for (int j = 2 * i; j <= N; j += i) f[j] = (f[j] + f[i]) % MOD;     for (int i = 1; i <= N; i++)       if (c[i] >= i) Ans = (Ans + f[i]) % MOD;     cout << Ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       a.push_back(x);     }     sort(a.begin(), a.end());     int l = 0;     int r = n - 1;     for (int i = 0; i < n; i++) {       if (i % 2) {         cout << a[r--] <<    ;       } else {         cout << a[l++] <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cnt[125];   char sa[5005];   int main() {     bool ok, whh;     int lens, lent;     string s, t, my;     cin >> s >> t;     memset(cnt, 0, sizeof cnt);     lens = s.length();     for (int i = 0; i < lens; i++) cnt[s[i]]++;     ok = true;     whh = false;     my = s;     lent = t.length();     if (lens <= lent) {       for (int i = 0; i < lens; i++) {         char c = t[i];         if (whh) {           c =  a ;           while (c <=  z )             if (cnt[c]) {               cnt[c]--;               break;             } else               c++;           my[i] = c;           continue;         }         if (!ok) {           c =  z ;           while (c >=  a )             if (cnt[c]) {               cnt[c]--;               break;             } else               c--;           my[i] = c;           continue;         }         while (c <=  z )           if (cnt[c]) {             cnt[c]--;             break;           } else             c++;         if (c == t[i])           my[i] = c;         else if (c <=  z ) {           my[i] = c;           whh = true;         } else {           ok = false;           c =  z ;           while (c >=  a )             if (cnt[c]) {               cnt[c]--;               break;             } else               c--;           my[i] = c;           continue;         }       }       if (my == t) {         strcpy(sa, my.c_str());         if (!next_permutation(sa, sa + lens))           ok = false;         else {           string tmp(sa);           my = tmp;         }       } else if (my < t) {         strcpy(sa, my.c_str());         while (next_permutation(sa, sa + lens)) {           string tmp(sa);           if (tmp > t) {             my = tmp;             break;           }         }         if (my <= t)           ok = false;         else           ok = true;       }     } else {       for (int i = 0; i < lens; i++) {         char c = t[i];         if (whh) {           c =  a ;           while (c <=  z )             if (cnt[c]) {               cnt[c]--;               break;             } else               c++;           my[i] = c;           continue;         }         if (!ok) {           c =  z ;           while (c >=  a )             if (cnt[c]) {               cnt[c]--;               break;             } else               c--;           my[i] = c;           continue;         }         while (c <=  z )           if (cnt[c]) {             cnt[c]--;             break;           } else             c++;         if (c == t[i])           my[i] = c;         else if (c <=  z ) {           my[i] = c;           whh = true;         } else {           ok = false;           c =  z ;           while (c >=  a )             if (cnt[c]) {               cnt[c]--;               break;             } else               c--;           my[i] = c;           continue;         }       }       if (my == t) {         strcpy(sa, my.c_str());         if (!next_permutation(sa, sa + lens))           ok = false;         else {           string tmp(sa);           my = tmp;         }       } else if (my < t) {         strcpy(sa, my.c_str());         while (next_permutation(sa, sa + lens)) {           string tmp(sa);           if (tmp > t) {             my = tmp;             break;           }         }         if (my <= t)           ok = false;         else           ok = true;       }     }     if (ok)       cout << my << endl;     else       cout <<  -1  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long qpow(long long a, long long n) {     long long res = 1;     while (n) {       if (n % 2) {         res = (res * a) % 100000007;         n--;       } else {         n = n / 2;         a = (a * a) % 100000007;       }     }     return res;   }   void solve() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n, q;     cin >> n >> q;     vector<long long> arr(n + 1);     for (long long i = 1; i <= n; i++) cin >> arr[i];     vector<long long> H(n + 1);     for (long long i = 0; i < q; i++) {       long long l, r;       cin >> l >> r;       H[l]++;       if (r + 1 <= n) H[r + 1]--;     }     for (long long i = 1; i <= n; i++) H[i] += H[i - 1];     sort(H.begin() + 1, H.end());     sort(arr.begin() + 1, arr.end());     long long sum = 0;     for (long long i = 1; i <= n; i++) sum += H[i] * arr[i];     cout << sum <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string A[15];   long long nr[100];   int N, K;   long long mn = 1000000000;   int main() {     scanf( %d %d n , &N, &K);     for (int i = 1; i <= N; i++) cin >> A[i];     vector<int> v;     for (int i = 1; i <= K; i++) v.push_back(i);     do {       for (int i = 1; i <= N; i++) {         nr[i] = 0;         for (int j = 0; j < K; j++) nr[i] = nr[i] * 10 + (A[i][v[j] - 1] -  0 );       }       sort(nr + 1, nr + N + 1);       mn = min(mn, nr[N] - nr[1]);     } while (next_permutation(v.begin(), v.end()));     cout << mn;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, f = 0, s = 0;     char pass[3], x[2];     cin >> pass[0] >> pass[1];     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x[0] >> x[1];       if (x[0] == pass[0] && x[1] == pass[1]) {         cout <<  YES ;         return 0;       }       if (x[1] == pass[0]) {         f = 1;       }       if (x[0] == pass[1]) {         s = 1;       }     }     if (s && f) {       cout <<  YES ;       return 0;     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long a, b, x, y, n;       cin >> a >> b >> x >> y >> n;       long long p = a - x, q = b - y;       long long pp = min(p, n), qq = min(q, n);       long long aa = a - pp, bb = b - qq;       long long tt = a + b - n;       if (p + q <= n)         cout << x * y << endl;       else {         if (aa < bb)           cout << aa * (tt - aa) << endl;         else           cout << bb * (tt - bb) << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110;   int n;   char s[N][30];   char ss[30];   int p;   int len[N];   int in[30];   int nxt[30];   int bf[30][30], ind[30];   void go(int i) {     ss[p++] = char(i +  a );     if (nxt[i] >= 0) go(nxt[i]);   }   int main() {     scanf( %d , &n);     memset(nxt, -1, sizeof(nxt));     for (int i = 0; i < n; ++i) {       scanf( %s , s[i]);       len[i] = strlen(s[i]);     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < len[i]; ++j) {         in[s[i][j] -  a ] = 1;         for (int k = j + 1; k < len[i]; ++k) {           bf[s[i][j] -  a ][s[i][k] -  a ] = 1;         }       }       for (int j = 0; j < len[i] - 1; ++j) {         nxt[s[i][j] -  a ] = s[i][j + 1] -  a ;       }     }     for (int i = 0; i < 26; ++i) {       for (int j = 0; j < 26; ++j) {         if (bf[j][i]) {           ++ind[i];         }       }     }     for (int i = 0; i < 26; ++i) {       if (!ind[i] && in[i]) {         go(i);       }     }     puts(ss);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string st[30] = { zero ,     one ,      two ,        three ,     four ,                     five ,     six ,      seven ,      eight ,     nine ,                     ten ,      eleven ,   twelve ,     thirteen ,  fourteen ,                     fifteen ,  sixteen ,  seventeen ,  eighteen ,  nineteen ,                     twenty ,   thirty ,   forty ,      fifty ,     sixty ,                     seventy ,  eighty ,   ninety };   int main() {     int n;     while (cin >> n) {       if (n <= 20)         cout << st[n];       else if (n % 10 == 0)         cout << st[20 + n / 10 - 2];       else if (n < 30)         cout << st[20] +  -  + st[n % 10];       else if (n < 40)         cout << st[21] +  -  + st[n % 10];       else if (n < 50)         cout << st[22] +  -  + st[n % 10];       else if (n < 60)         cout << st[23] +  -  + st[n % 10];       else         cout << st[20 + n / 10 - 2] +  -  + st[n % 10];       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int dpmax[200005][20];   long long int dpmin[200005][20];   long long int a[200005], b[200005];   int n;   void ST() {     for (int i = 1; i <= n; i++) {       dpmax[i][0] = a[i];       dpmin[i][0] = b[i];     }     for (int j = 1; (1 << j) <= n; j++) {       for (int i = 1; i + (1 << j) - 1 <= n; i++) {         dpmax[i][j] = dpmax[i][j - 1] > dpmax[i + (1 << (j - 1))][j - 1]                           ? dpmax[i][j - 1]                           : dpmax[i + (1 << (j - 1))][j - 1];         dpmin[i][j] = dpmin[i][j - 1] < dpmin[i + (1 << (j - 1))][j - 1]                           ? dpmin[i][j - 1]                           : dpmin[i + (1 << (j - 1))][j - 1];       }     }   }   long long int RMQmax(int l, int r) {     int k = 0;     while ((1 << (k + 1)) <= r - l + 1) k++;     return dpmax[l][k] > dpmax[r - (1 << k) + 1][k] ? dpmax[l][k]                                                     : dpmax[r - (1 << k) + 1][k];   }   long long int RMQmin(int l, int r) {     int k = 0;     while ((1 << (k + 1)) <= r - l + 1) k++;     return dpmin[l][k] < dpmin[r - (1 << k) + 1][k] ? dpmin[l][k]                                                     : dpmin[r - (1 << k) + 1][k];   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) cin >> b[i];     ST();     long long int ans = 0;     for (int i = 1; i <= n; i++) {       int l = i, r = n, mid, flag = 0;       long long int x, y;       long long int last = 0, first = 0;       while (l <= r) {         mid = (l + r) / 2;         x = RMQmax(i, mid);         y = RMQmin(i, mid);         if (x == y) {           flag = 1;           last = mid;         }         if (x > y) r = mid - 1;         if (x < y || x == y) l = mid + 1;       }       if (flag) {         l = i;         r = n;         while (l <= r) {           mid = (l + r) / 2;           x = RMQmax(i, mid);           y = RMQmin(i, mid);           if (x < y) {             l = mid + 1;           } else {             r = mid - 1;             first = mid;           }         }         ans += (last - first + 1);       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   const int maxn = 1e6 + 20;   const int maxm = 1e5 + 10;   const long long int mod = 998244353;   const long long int INF = 1e18 + 100;   const int inf = 0x3f3f3f3f;   const double pi = acos(-1.0);   const double eps = 1e-8;   using namespace std;   int a[50][50];   int num[200], c[200];   int main() {     int _;     for (scanf( %d , &_); _; _--) {       memset(num, 0, sizeof num);       memset(c, 0, sizeof c);       memset(a, 0, sizeof a);       int n, m;       scanf( %d %d , &n, &m);       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++) scanf( %d , &a[i][j]);       if (n == 2 && m == 2) {         if (a[0][0] != a[1][1])           puts( 1 );         else           puts( 0 );       } else {         n--;         m--;         for (int i = 0; i <= n; i++) {           for (int j = 0; j <= m; j++) {             c[i + j]++;             num[i + j] += a[i][j];           }         }         int ans = 0;         if ((n + m) % 2 == 1) {           for (int i = 0; i < (n + m) / 2; i++) {             ans += min(num[i] + num[n + m - i],                        (c[i] + c[n + m - i]) - (num[i] + num[n + m - i]));           }           int i = (n + m) / 2;           ans += min(num[i] + num[n + m - i],                      (c[i] + c[n + m - i]) - (num[i] + num[n + m - i]));         } else {           for (int i = 0; i < (n + m) / 2; i++) {             ans += min(num[i] + num[n + m - i],                        (c[i] + c[n + m - i]) - (num[i] + num[n + m - i]));           }         }         printf( %d n , ans);       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, x, y, t;   int main() {     cin >> t;     while (t--) {       cin >> a >> b >> x >> y;       if (a > b) swap(a, b);       cout << abs(a - b) * x + min(y, x * 2) * a << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 5;   const int MOD = 998244353;   int main() {     int n;     while (cin >> n) {       int p;       long long x = 0, y = 0;       for (int i = 1; i <= n; i++) {         cin >> p;         x += p / 2;         y += p / 2;         if (p % 2) {           if (i % 2)             x++;           else             y++;         }       }       printf( %I64d n , min(x, y));     }   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int i;     char n[100010], m;     stack<char> s;     scanf( %s , n);     for (i = 0; n[i] != 0; i++) {       if (s.empty()) {         s.push(n[i]);       } else {         m = s.top();         if (m == n[i])           s.pop();         else           s.push(n[i]);       }     }     if (s.empty())       printf( Yes n );     else       printf( No n );     return 0;   }


#include <bits/stdc++.h>   int NEXT_FREE_ID = 0;   std::unordered_map<std::string, int> stringId;   namespace persistentid {   int NEXT_FREE_INDEX = 1;   int s[10000000], L[10000000], R[10000000];   int root[100005];   int n = 1 << 30;   int upd(int p, int v, int id, int l = 0, int r = n) {     int ID = NEXT_FREE_INDEX++;     s[ID] = s[id] + v;     if (r - l < 2) return ID;     int mid = (l + r) / 2;     L[ID] = L[id], R[ID] = R[id];     if (p < mid)       L[ID] = upd(p, v, L[ID], l, mid);     else       R[ID] = upd(p, v, R[ID], mid, r);     return ID;   }   int f(int p, int id, int l = 0, int r = n) {     if (r - l < 2) return s[id];     int mid = (l + r) / 2;     if (p < mid)       return f(p, L[id], l, mid);     else       return f(p, R[id], mid, r);   }   }  // namespace persistentid   namespace persistentcount {   int NEXT_FREE_INDEX = 1;   int s[10000000], L[10000000], R[10000000];   int root[100005];   int n = 1 << 30;   int upd(int p, int v, int id, int l = 0, int r = n) {     int ID = NEXT_FREE_INDEX++;     s[ID] = s[id] + v;     if (r - l < 2) return ID;     int mid = (l + r) / 2;     L[ID] = L[id], R[ID] = R[id];     if (p < mid)       L[ID] = upd(p, v, L[ID], l, mid);     else       R[ID] = upd(p, v, R[ID], mid, r);     return ID;   }   int sum(int x, int y, int id, int l = 0, int r = n) {     if (x >= r || l >= y) return 0;     if (x <= l && r <= y) return s[id];     int mid = (l + r) / 2;     return sum(x, y, L[id], l, mid) + sum(x, y, R[id], mid, r);   }   }  // namespace persistentcount   int main() {     std::ios::sync_with_stdio(0);     std::cin.tie(0);     std::cout.tie(0);     int Q;     std::cin >> Q;     for (int q = 1; q <= Q; q++) {       std::string type;       std::cin >> type;       if (type ==  set ) {         std::string a;         std::cin >> a;         int x;         std::cin >> x;         if (stringId.count(a) == 0) {           stringId[a] = NEXT_FREE_ID++;           persistentid::root[q] = persistentid::NEXT_FREE_INDEX;           persistentid::upd(stringId[a], x, persistentid::root[q - 1]);           persistentcount::root[q] = persistentcount::NEXT_FREE_INDEX;           persistentcount::upd(x, 1, persistentcount::root[q - 1]);         } else {           persistentid::root[q] = persistentid::NEXT_FREE_INDEX;           int val = persistentid::f(stringId[a], persistentid::root[q - 1]);           persistentid::upd(stringId[a], -val + x, persistentid::root[q - 1]);           int temp = persistentcount::NEXT_FREE_INDEX;           persistentcount::upd(val, -1, persistentcount::root[q - 1]);           persistentcount::root[q] = persistentcount::NEXT_FREE_INDEX;           persistentcount::upd(x, 1, temp);         }       } else if (type ==  remove ) {         std::string a;         std::cin >> a;         if (stringId.count(a) != 0 &&             persistentid::f(stringId[a], persistentid::root[q - 1]) != 0) {           int val = persistentid::f(stringId[a], persistentid::root[q - 1]);           persistentcount::root[q] = persistentcount::NEXT_FREE_INDEX;           persistentcount::upd(val, -1, persistentcount::root[q - 1]);           persistentid::root[q] = persistentid::NEXT_FREE_INDEX;           persistentid::upd(stringId[a], -val, persistentid::root[q - 1]);         } else {           persistentcount::root[q] = persistentcount::root[q - 1];           persistentid::root[q] = persistentid::root[q - 1];         }       } else if (type ==  query ) {         std::string a;         std::cin >> a;         persistentcount::root[q] = persistentcount::root[q - 1];         persistentid::root[q] = persistentid::root[q - 1];         if (stringId.count(a) != 0 &&             persistentid::f(stringId[a], persistentid::root[q - 1]) != 0) {           std::cout << persistentcount::sum(                            1,                            persistentid::f(stringId[a],                                            persistentid::root[q - 1]),                            persistentcount::root[q - 1])                     <<   n                      << std::flush;         } else {           std::cout <<  -1 n  << std::flush;         }       } else {         int d;         std::cin >> d;         d = q - 1 - d;         persistentcount::root[q] = persistentcount::root[d];         persistentid::root[q] = persistentid::root[d];       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<pair<int, pair<int, int> > > E(m);     {       int cnt = 0;       for (int i = 0; i < m; i++) {         cin >> E[i].first >> E[i].second.first;         E[i].second.second = i;         if (E[i].second.first == 1) cnt++;         E[i].second.first = 1 - E[i].second.first;       }       if (cnt != n - 1) {         cout << -1 << endl;         return 0;       }     }     sort(E.begin(), E.end());     vector<pair<int, int> > res(m);     queue<pair<int, int> > que;     int latest = 1;     for (int i = 0; i < m; i++) {       if (E[i].second.first == 0) {         res[E[i].second.second] = make_pair(latest, latest + 1);         for (int u = 1; u < latest && que.size() < m; u++)           que.push(make_pair(u, latest + 1));         latest++;       } else {         if (que.empty()) {           cout << -1 << endl;           return 0;         } else {           auto v = que.front();           que.pop();           res[E[i].second.second] = v;         }       }     }     for (int i = 0; i < m; i++)       cout << res[i].first <<     << res[i].second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 100;   const int M = 1e9 + 100;   long long a[N];   int main() {     int n, m;     long long x, y;     while (~scanf( %d %d , &n, &m)) {       x = -M;       y = M;       for (int i = 0; i < n; i++) {         scanf( %lld , &a[i]);         x = max(a[i], x);         y = min(a[i], y);       }       if (m == 1)         printf( %lld n , y);       else if (m >= 3)         printf( %lld n , x);       else         printf( %lld n , max(a[0], a[n - 1]));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     int t;     cin >> t;     while (t--) {       int n, m, k;       cin >> n >> m >> k;       vector<int> deg(n);       vector<vector<int>> adj(n);       for (int i = 0, u, v; i < m; ++i) {         cin >> u >> v;         --u, --v;         adj[u].push_back(v);         adj[v].push_back(u);         ++deg[u], ++deg[v];       }       deque<int> q;       for (int i = 0; i < n; ++i) {         sort(adj[i].begin(), adj[i].end());         if (deg[i] == k - 1) q.push_back(i);         if (deg[i] < k - 1) q.push_front(i);       }       vector<int> ans;       vector<bool> mp(n);       while (!q.empty()) {         int u = q.front();         q.pop_front();         if (deg[u] == -1) continue;         if (deg[u] == k - 1) {           vector<int> &c = ans;           for (auto v : adj[u])             if (deg[v] != -1) {               c.push_back(v);               mp[v] = true;             }           c.insert(upper_bound(c.begin(), c.end(), u), u);           mp[u] = true;           bool ok = true;           for (auto &i : c) {             if (adj[i].size() <= 3 * c.size()) {               int cnt = 0;               for (auto j : adj[i]) cnt += mp[j];               if (cnt != k - 1) {                 ok = false;                 break;               }             } else {               for (size_t j = &i - &c[0]; ++j < c.size();) {                 auto it = lower_bound(adj[i].begin(), adj[i].end(), c[j]);                 if (it == adj[i].end() || *it != c[j]) {                   ok = false;                   break;                 }               }             }           }           if (ok) break;           for (auto i : c) mp[i] = false;           c.clear();         }         deg[u] = -1;         for (auto v : adj[u])           if (deg[v] != -1) {             if (deg[v] == k - 1) q.push_front(v);             if (deg[v] == k) q.push_back(v);             --deg[v];           }       }       if (ans.empty()) {         for (int i = 0; i < n; ++i)           if (deg[i] != -1) ans.push_back(i);         if (!ans.empty()) {           cout <<  1   << ans.size() <<   n ;           for (auto &i : ans) cout << i + 1 <<    n [&i == &ans.back()];         } else           cout <<  -1 n ;       } else {         cout <<  2 n ;         for (auto &i : ans) cout << i + 1 <<    n [&i == &ans.back()];       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mas[1001][1001];   char chars[1000][1001];   int n, m;   int st = 0;   vector<int> woi;   int p = 0;   void dfs(int x, int y) {     st++;     mas[x][y] = p;     if (x != 0) {       if (mas[x - 1][y] == 0 && chars[x - 1][y] ==  . ) {         dfs(x - 1, y);       }     }     if (y != 0) {       if (mas[x][y - 1] == 0 && chars[x][y - 1] ==  . ) {         dfs(x, y - 1);       }     }     if (x + 1 != n) {       if (mas[x + 1][y] == 0 && chars[x + 1][y] ==  . ) {         dfs(x + 1, y);       }     }     if (y + 1 != m + 1) {       if (mas[x][y + 1] == 0 && chars[x][y + 1] ==  . ) {         dfs(x, y + 1);       }     }   }   void startdfs(int x, int y) {     st = 0;     p++;     dfs(x, y);     woi.push_back(st);   }   int calculate(int x, int y) {     int ans = 1;     set<int> f;     if (x != 0) {       f.emplace(mas[x - 1][y]);     }     if (y != 0) {       f.emplace(mas[x][y - 1]);     }     if (x != n) {       f.emplace(mas[x + 1][y]);     }     if (y != m + 1) {       f.emplace(mas[x][y + 1]);     }     for (auto w : f) {       ans += woi[w];     }     return ans;   }   int main() {     char ch;     woi.push_back(0);     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m + 1; j++) {         scanf( %c , &ch);         chars[i][j] = ch;       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m + 1; j++) {         if (chars[i][j] ==  .  && mas[i][j] == 0) {           startdfs(i, j);         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m + 1; j++) {         if (chars[i][j] ==  * ) {           int c = calculate(i, j) % 10;           chars[i][j] = c +  0 ;         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 1; j < m + 1; j++) {         printf( %c , chars[i][j]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, A[100000], count0 = 0, max = 0;     scanf( %i , &n);     for (int i = 0; i < n; i++) {       scanf( %i , A + i);       if (A[i] < 0) A[i] = -A[i] - 1;       if (!A[i]) count0++;       if (A[i] > max) max = A[i];     }     if (count0 == n) {       for (int i = 1; i < n; i++) printf( -1  );       return 0 * printf(n & 1 ?  0  :  -1 );     }     for (int i = 0; i < n; i++) {       if (A[i]) {         if (A[i] == max) {           if (n & 1)             printf( %i  , max);           else             printf( %i  , -A[i] - 1);           max = 0;         } else           printf( %i  , -A[i] - 1);       } else         printf( -1  );     }   }


#include <bits/stdc++.h>   const int S = 1 << 21;   char ibuf[S], *iS, *iT, obuf[S], *oS = obuf, *oT = oS + S - 1;   struct Flusher_ {     ~Flusher_() { (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void()); }   } flusher_;   template <class T>   inline void read(T &x) {     x = 0;     register char c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin),                                    (iS == iT ? EOF : *iS++))                                 : *iS++);     register bool f = 0;     while (!isdigit(c))       f ^= c ==  - , c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin),                                       (iS == iT ? EOF : *iS++))                                    : *iS++);     while (isdigit(c))       x = x * 10 + c -  0 ,       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin),                        (iS == iT ? EOF : *iS++))                     : *iS++);     if (f) x = -x;   }   template <class T>   inline void readchar(T &x) {     x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin),                      (iS == iT ? EOF : *iS++))                   : *iS++);     while (!isalpha(x))       x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin),                        (iS == iT ? EOF : *iS++))                     : *iS++);   }   template <class T>   inline void print(T x) {     if (x < 0)       (*oS++ = ( - ),        oS == oT ? (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void())                 : void()),           x = -x;     if (x > 9) print(x / 10);     (*oS++ = (x % 10 +  0 ),      oS == oT ? (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void()) : void());   }   template <class T>   inline void print(T x, char c) {     print(x), (*oS++ = (c),                oS == oT ? (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void())                         : void());   }   const int N = 2e5 + 10, sqn = 1000;   int n, m, k, ans, cnt, a[N], b[N], s[N];   struct sequence {     int siz, ans;     std::vector<int> vec, bln, tag;     std::vector<std::array<short, (1 << 14)>> val;     inline int left(int k) { return k * sqn; }     inline int right(int k) {       return std::min((int)vec.size() - 1, (k + 1) * sqn - 1);     }     void modify(int k, int x) {       ::ans += ans, cnt -= (vec.back() ^ tag.back()) != 0;       for (int i = bln[k] + 1; i <= bln.back(); i++) {         ans -= val[i][tag[i]];         tag[i] ^= x;         ans += val[i][tag[i]];       }       if (bln.back())         for (int i = left(bln[k]); i <= right(bln[k]); i++) val[bln[k]][vec[i]]--;       for (int i = k; i <= right(bln[k]); i++) {         ans -= vec[i] == tag[bln[k]];         vec[i] ^= x;         ans += vec[i] == tag[bln[k]];       }       if (bln.back())         for (int i = left(bln[k]); i <= right(bln[k]); i++) val[bln[k]][vec[i]]++;       ::ans -= ans, cnt += (vec.back() ^ tag.back()) != 0;     }     void init() {       for (int i = 1; i < vec.size(); i++) vec[i] ^= vec[i - 1];       for (int i = 0; i < vec.size(); i++) ans += vec[i] == 0;       for (int i = 0; i < vec.size(); i++) bln.push_back(i / sqn);       tag.resize(bln.back() + 1);       if (bln.back()) {         val.resize(bln.back() + 1);         for (int k = 0; k <= bln.back(); k++) {           for (int i = left(k); i <= right(k); i++) val[k][vec[i]]++;         }       }       ::ans += vec.size() - ans, cnt += (vec.back() ^ tag.back()) != 0;     }   } f[N];   void output() { print(cnt ? -1 : ans,   n ); }   int main() {     read(n), read(k), read(m);     for (int i = 1; i <= n; i++) read(a[i]);     for (int i = 1; i <= n; i++) read(b[i]);     for (int i = 1; i <= n + 1; i++) s[i] = a[i - 1] ^ b[i - 1] ^ a[i] ^ b[i];     for (int i = 1; i <= n + 1; i++) f[(i - 1) % k].vec.push_back(s[i]);     for (int i = 0; i < k; i++) f[i].init();     output();     for (int c, p, x, v, i = 1; i <= m; i++) {       readchar(c), read(p), read(x);       if (c ==  a ) v = a[p] ^ x, a[p] = x;       if (c ==  b ) v = b[p] ^ x, b[p] = x;       f[p % k].modify(p / k, v);       f[(p - 1) % k].modify((p - 1) / k, v);       output();     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   #pragma GCC optimize( unroll-loops )   using namespace std;   const long double eps = 1e-7;   const int inf = 1000000010;   const long long INF = 10000000000000010LL;   const int mod = 1000000007;   const int MAXN = 100010, LOG = 20;   int n, m, k, u, v, x, y, t, a, b, ans;   bool dead[MAXN];   string A;   vector<vector<int>> out;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> A;     n = A.size();     while (1) {       int i = 0, j = n - 1;       vector<int> v1, v2;       while (i < j) {         while (i < j && (A[i] ==  )  || dead[i])) i++;         while (i < j && (A[j] ==  (  || dead[j])) j--;         if (i >= j || A[i] ==  )  || A[j] ==  ( ) break;         v1.push_back(i++);         v2.push_back(j--);       }       if (v1.empty()) break;       reverse(v2.begin(), v2.end());       for (int i : v2) v1.push_back(i);       for (int i : v1) dead[i] = 1;       out.push_back(v1);     }     cout << out.size() <<   n ;     for (auto vec : out) {       cout << vec.size() <<   n ;       for (int i : vec) cout << i + 1 <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T &x) {     register T c = getchar();     x = 0;     int t = 0;     if (c ==  - ) t = 1, c = getchar();     for (; (c < 48 || c > 57); c = getchar())       ;     for (; c > 47 && c < 58; c = getchar()) {       x = (x << 1) + (x << 3) + c - 48;     }     if (t) x = -x;   }   int main() {     int n, Max = 0;     read(n);     string str;     cin >> str;     bool visited[250] = {0};     for (int i = 0; i < n; i++) {       if (str[i] >=  a  && !visited[i]) {         set<char> s;         visited[i] = 1;         s.insert(str[i]);         for (int j = i + 1; j < n; j++) {           if (str[j] <=  Z ) break;           visited[j] = 1;           s.insert(str[j]);         }         if (Max < s.size()) Max = s.size();       }     }     cout << Max;   }


#include <bits/stdc++.h>   const int INF = 1e9;   using namespace std;   int dx[] = {-1, 1, 0, 0};   int dy[] = {0, 0, -1, 1};   bool vis[1005][1005], cvis[50];   vector<pair<int, int> > cc[50];   int m, n, k, a[1005][1005], D[45][1005][1005];   void DFS(int c) {     memset(vis, 0, sizeof(vis));     memset(cvis, 0, sizeof(cvis));     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++) D[c][i][j] = 4000;     queue<pair<int, int> > Q;     for (int i = 0; i < cc[c].size(); i++) {       Q.push(cc[c][i]);       vis[cc[c][i].first][cc[c][i].second] = 1;       D[c][cc[c][i].first][cc[c][i].second] = 0;     }     cvis[c] = 1;     int first, second, xx, yy;     while (!Q.empty()) {       first = Q.front().first;       second = Q.front().second;       Q.pop();       for (int j = 0; j < 4; j++) {         xx = first + dx[j];         yy = second + dy[j];         if (xx < 1 || yy < 1 || xx > m || yy > n) continue;         if (!vis[xx][yy]) {           vis[xx][yy] = 1;           D[c][xx][yy] = D[c][first][second] + 1;           Q.push(pair<int, int>(xx, yy));         }       }       if (!cvis[a[first][second]]) {         cvis[a[first][second]] = 1;         for (int i = 0; i < cc[a[first][second]].size(); i++) {           xx = cc[a[first][second]][i].first;           yy = cc[a[first][second]][i].second;           if (!vis[xx][yy]) {             vis[xx][yy] = 1;             D[c][xx][yy] = D[c][first][second] + 1;             Q.push(pair<int, int>(xx, yy));           }         }       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> m >> n >> k;     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++) {         cin >> a[i][j];         cc[a[i][j]].push_back(pair<int, int>(i, j));       }     for (int i = 1; i <= k; i++) DFS(i);     int ans = INF, q, r1, r2, c1, c2;     cin >> q;     while (q--) {       cin >> r1 >> c1 >> r2 >> c2;       ans = abs(r2 - r1) + abs(c1 - c2);       for (int i = 1; i <= k; i++) {         ans = min(ans, D[i][r1][c1] + D[i][r2][c2] + 1);       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long double EPS = (long double)1e-7;   const long double PI = acos(0) * 2;   bool isZero(const long double& x) { return abs(x) <= EPS; }   int sign(const long double& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); }   long long gcd(long long a, long long b) {     for (; b; a %= b, swap(a, b)) {     }     return abs(a);   }   pair<long long, long long> operator+(const pair<long long, long long>& a,                                        const pair<long long, long long>& b) {     return pair<long long, long long>(a.first + b.first, a.second + b.second);   }   pair<long long, long long> operator-(const pair<long long, long long>& a,                                        const pair<long long, long long>& b) {     return pair<long long, long long>(a.first - b.first, a.second - b.second);   }   pair<long long, long long> operator*(const pair<long long, long long>& a,                                        const long long& b) {     return pair<long long, long long>(a.first * b, a.second * b);   }   long long operator*(const pair<long long, long long>& a,                       const pair<long long, long long>& b) {     return a.first * b.second - b.first * a.second;   }   long long ccw(const pair<long long, long long>& a,                 const pair<long long, long long>& b,                 const pair<long long, long long>& c) {     return a * b + b * c + c * a;   }   void fg(vector<int> G[], int a, int b) {     G[a].emplace_back(b);     G[b].emplace_back(a);   }   void fg(vector<pair<int, int> > G[], int a, int b, int c) {     G[a].emplace_back(b, c);     G[b].emplace_back(a, c);   }   long long A, B, C;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> A >> B >> C;     if (!A && !B) {       puts(C ?  0  :  -1 );       exit(0);     }     if (!A) {       printf( 1 n%.10lf n , double(-C) / B);       exit(0);     }     long long D = B * B - 4 * A * C;     if (D < 0) {       puts( 0 );       exit(0);     }     if (!D) {       printf( 1 n%.10lf n , double(-B) / A / 2);       exit(0);     }     if (A < 0) {       A = -A;       B = -B;       C = -C;     }     printf( 2 n%.10lf n%.10lf n , double(-B - sqrt(D)) / A / 2,            double(-B + sqrt(D)) / A / 2);     return 0;   }


#include <bits/stdc++.h>   const int INT_INF = 0x3f3f3f3f;   const int INT_M_INF = 0x7f7f7f7f;   const long long LL_INF = 0x3f3f3f3f3f3f3f3f;   const long long LL_M_INF = 0x7f7f7f7f7f7f7f7f;   const double pi = acos(-1.0);   using namespace std;   inline void debug(char ch) {     for (int __ii = 0; __ii < 20; ++__ii) putchar(ch);     printf(  n );   }   const double eps = 1e-8;   inline int dcmp(double a, double b) {     if (fabs(a - b) < eps) return 0;     return a < b ? -1 : 1;   }   const long long MOD = 1e9 + 7;   const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};   const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};   const int MAXN = 1000 + 10;   const int MAXT = 10000 + 10;   char s[MAXN], t[MAXN];   int len;   char sta[100];   bool vis[100];   bool solve() {     for (int i = 0; i < len; ++i)       if (s[i] != t[i]) {         if (vis[s[i] -  a ] || vis[t[i] -  a ]) return false;         if (sta[s[i] -  a ] != 0) {           if (sta[s[i] -  a ] != t[i]) return false;         } else if (sta[t[i] -  a ] != 0) {           if (sta[t[i] -  a ] != s[i]) return false;         } else {           sta[s[i] -  a ] = t[i];           sta[t[i] -  a ] = s[i];         }       } else {         if (sta[s[i] -  a ] != 0) return false;         vis[s[i] -  a ] = true;       }     return true;   }   vector<pair<char, char> > vec;   int main() {     while (scanf( %s%s , s, t) == 2) {       memset(sta, 0, sizeof(sta));       memset(vis, false, sizeof vis);       vec.clear();       len = strlen(s);       bool ans = solve();       if (ans) {         for (int i = 0; i < 26; ++i)           if (sta[i] != 0) {             vec.push_back(pair<char, char>( a  + i, sta[i]));             sta[sta[i] -  a ] = 0;           }         printf( %d n , (int)((vec).size()));         for (int i = 0; i < (int)((vec).size()); ++i)           printf( %c %c n , ((vec[i]).first), ((vec[i]).second));       } else         printf( -1 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {1, -1, 0, 0};   int dy[] = {0, 0, -1, 1};   int tt[1016][1016];   int hh[1016][1016];   int n, m;   int ex, ey;   queue<int> q;   queue<int> p;   void bfs() {     while (!p.empty()) {       int x = p.front();       int y = q.front();       p.pop();       q.pop();       for (int i = 0; i < 4; i++) {         int nx = x + dx[i];         int ny = y + dy[i];         if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && tt[nx][ny] == 0 &&             !(nx == ex && ny == ey)) {           p.push(nx);           q.push(ny);           tt[nx][ny] = tt[x][y] + 1;         }       }     }   }   int main() {     scanf( %d %d , &n, &m);     getchar();     int ans = 0;     memset(tt, 0, sizeof(tt));     memset(hh, 0, sizeof(hh));     char c;     int zux, zuy;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %c , &c);         if (c >=  0  && c <=  9 ) {           hh[i][j] = c -  0 ;         }         if (c ==  S ) {           zux = i;           zuy = j;         }         if (c ==  E ) {           tt[i][j] = 1;           ex = i;           ey = j;         }         if (c ==  T ) {           tt[i][j] = -1;         }       }       getchar();     }     p.push(ex);     q.push(ey);     bfs();     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (i == zux && j == zuy) continue;         if (i == ex && j == ey) continue;         if (tt[i][j] <= tt[zux][zuy] && tt[i][j] != 0 && tt[i][j] != -1) {           ans += hh[i][j];         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Node {     map<int, Node *> go;     int l, r;     Node *p, *link;     Node(int _l, int _r, Node *_p) : l(_l), r(_r), p(_p), link(0) {}   };   const int MAXN = 100 * 1000 + 5, S = 1000;   string s[MAXN];   int c[MAXN], rr[MAXN], nn, curSuf;   long long ans = 0;   vector<int> a, sid;   Node *splitEdge(Node *v, int pos) {     Node *res = new Node(v->l, pos, v->p);     v->p->go[a[v->l]] = res;     res->go[a[pos]] = v;     v->p = res;     v->l = pos;     return res;   }   pair<Node *, int> go(Node *v, int l, int len) {     while (len > 0) {       v = v->go[a[l]];       len -= v->r - v->l;       l += v->r - v->l;     }     return make_pair(v, v->r + len);   }   void add(int i, pair<Node *, int> &p) {     Node *w = 0;     while (curSuf <= i) {       Node *v = p.first;       int pos = p.second;       if (pos != v->r) {         if (a[i] == a[pos]) {           p.second++;           return;         }         v = splitEdge(v, pos);         Node *u = new Node(i, nn, v);         v->go[a[i]] = u;         if (w) w->link = v;         w = v;       } else {         if (w) w->link = v;         w = 0;         if (v->go.find(a[i]) != v->go.end()) {           p.first = v->go[a[i]];           p.second = p.first->l + 1;           return;         }         Node *u = new Node(i, nn, v);         v->go[a[i]] = u;       }       int len = 0, l = v->r;       Node *q = v;       while (!q->link && q->p) {         len += q->r - q->l;         l -= q->r - q->l;         q = q->p;       }       if (!q->p) {         if (len) p = go(q, l + 1, len - 1);       } else         p = go(q->link, l, len);       curSuf++;     }   }   Node *buildSufTree() {     nn = a.size();     Node *root = new Node(0, 0, 0);     pair<Node *, int> p = make_pair(root, 0);     curSuf = 0;     for (int i = 0; i < nn; i++) add(i, p);     return root;   }   long long dfs(Node *v, int dep = 0) {     long long res = 0;     if (v->go.empty()) res += c[sid[nn - dep]];     for (map<int, Node *>::iterator it = v->go.begin(); it != v->go.end(); it++)       res += dfs(it->second, dep + it->second->r - it->second->l);     if (v->go.empty()) {       if (a[v->l] < S) ans = max(ans, res * (rr[sid[nn - dep]] - (nn - dep)));     } else       ans = max(ans, res * dep);     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> s[i];     for (int i = 0; i < n; i++) cin >> c[i];     for (int i = 0; i < n; i++) {       for (size_t j = 0; j < s[i].length(); j++) {         a.push_back(s[i][j]);         sid.push_back(i);       }       rr[i] = a.size();       a.push_back(S + i);       sid.push_back(i);     }     dfs(buildSufTree());     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   int getbit(T s, int i) {     return (s >> i) & 1;   }   template <class T>   T onbit(T s, int i) {     return s | (T(1) << i);   }   template <class T>   T offbit(T s, int i) {     return s & (~(T(1) << i));   }   template <class T>   int cntbit(T s) {     return __builtin_popcounll(s);   }   template <class T>   T gcd(T a, T b) {     T r;     while (b != 0) {       r = a % b;       a = b;       b = r;     }     return a;   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   int k, n, m;   int a[200005], b[200005];   vector<pair<int, int> > V[200005];   vector<pair<int, int> > VV;   int M[200005], vt[200005];   vector<pair<long double, int> > T[200005];   vector<pair<long double, int> > TT;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     memset(M, -1, sizeof(M));     memset(vt, -1, sizeof(vt));     cin >> k >> n >> m;     for (int i = (1); i <= (k); ++i) {       cin >> a[i];     }     for (int run = 0; run < (n); ++run) {       int id, u, v;       cin >> id >> u >> v;       b[run + 1] = id;       if (id == 1) {         if (M[u] < v) {           M[u] = v;           vt[u] = run + 1;         }       } else if (id == 2) {         V[u].push_back(make_pair(v, run + 1));       } else {         VV.push_back(make_pair(v, run + 1));       }     }     sort((VV).begin(), (VV).end());     priority_queue<pair<pair<long double, int>, pair<int, int> > > que;     for (int i = (1); i <= (k); ++i) {       if (M[i] > a[i]) {         V[i].push_back(make_pair(M[i] - a[i], vt[i]));       }       sort((V[i]).begin(), (V[i]).end());       long long run = a[i], temp;       for (int j = ((int)(V[i]).size()) - 1; j >= 0; j--) {         temp = run + V[i][j].first;         T[i].push_back(make_pair(log(temp) - log(run), V[i][j].second));         run = temp;       }       if (((int)(V[i]).size())) {         que.push(             make_pair(make_pair(T[i][0].first, T[i][0].second), make_pair(i, 0)));       }     }     long double RR = 0;     while (!que.empty()) {       pair<long double, int> PP = que.top().first;       long double x = PP.first;       int t = PP.second;       pair<int, int> P = que.top().second;       int u = P.first, v = P.second;       que.pop();       if (((int)(T[u]).size()) >= v + 2) {         que.push(make_pair(make_pair(T[u][v + 1].first, T[u][v + 1].second),                            make_pair(u, v + 1)));       }       RR += x;       TT.push_back(make_pair(RR, t));     }     RR = 0;     long double MAX = -1;     int vt = -1;     for (int i = 0; i <= ((int)(VV).size()); i++) {       if (i > m) break;       int u = min(m - i, ((int)(TT).size()));       u--;       long double x;       if (u >= 0)         x = TT[u].first;       else         x = 0;       if (MAX < x + RR) {         MAX = x + RR;         vt = i;       }       if (i < ((int)(VV).size())) RR += log(VV[((int)(VV).size()) - i - 1].first);     }     int u = min(m - vt, ((int)(TT).size()));     cout << u + vt << endl;     vector<pair<int, int> > res;     for (int i = 1; i <= vt; i++)       res.push_back(make_pair(b[VV[((int)(VV).size()) - i].second],                               VV[((int)(VV).size()) - i].second));     for (int i = 0; i < u; i++)       res.push_back(make_pair(b[TT[i].second], TT[i].second));     sort((res).begin(), (res).end());     for (int i = 0; i < (((int)(res).size())); ++i) cout << res[i].second <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     bool flag = true;     for (int i = 0; i < n; i++) {       if (i == 0) {         int l = a[1];         int r = a[2];         if (a[i] >= l + r) flag = false;       } else if (i == n - 1) {         int l = a[n - 3];         int r = a[n - 2];         if (a[i] >= l + r) flag = false;       } else {         int l, r;         if (i + 2 > n - 1)           r = a[n - 1];         else           r = a[i + 2];         if (i - 2 < 0)           l = a[0];         else           l = a[i - 2];         if (a[i] >= l + r) flag = false;       }     }     if (flag == false)       cout <<  NO n ;     else {       cout <<  YES n ;       cout << a[0] <<    ;       int i = 1;       while (i < n) {         cout << a[i] <<    ;         i = i + 2;       }       if (n % 2)         i = n - 1;       else         i = n - 2;       while (i >= 2) {         cout << a[i] <<    ;         i = i - 2;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int c1 = 0, c2 = 0, c3 = 0;     for (int i = 0; i < n; i++) {       int x, y, z;       cin >> x >> y >> z;       c1 += x, c2 += y, c3 += z;     }     if (c1 == 0 && c2 == 0 && c3 == 0)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;      typedef long long ll;      const int Maxn = 1000005;   const int mod = 1000000007;      int fac[Maxn], inv[Maxn];   int n;   int res;      int toPower(int a, int p)   {    int res = 1;    while (p) {     if (p & 1) res = ll(res) * a % mod;     p >>= 1; a = ll(a) * a % mod;    }    return res;   }      int Inv(int x) { return toPower(x, mod - 2); }      int C(int n, int k)   {    if (n < 0 || k < 0 || k > n) return 0;    return ll(fac[n]) * inv[k] % mod * inv[n - k] % mod;   }      int main()   {    fac[0] = inv[0] = 1;    for (int i = 1; i < Maxn; i++) {     fac[i] = ll(i) * fac[i - 1] % mod;     inv[i] = Inv(fac[i]);    }    scanf( %d , &n);    for (int r = 1; r < n; r += 2)     res = (res + ll(C(r + 1, n - r - 1)) * fac[r]) % mod;    res = ll(res) * 2 % mod * n % mod;    printf( %d n , res);       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 3e5 + 5;   vector<long long> f;   vector<long long> s;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long a, b;     cin >> a >> b;     while (a > 0) {       long long rem = a % 10;       a /= 10;       f.push_back(rem);     }     while (b > 0) {       long long rem = b % 10;       b /= 10;       s.push_back(rem);     }     reverse(f.begin(), f.end());     reverse(s.begin(), s.end());     long long dp[10];     memset(dp, 0, sizeof(dp));     for (auto x : f) {       dp[x]++;     }     if (f.size() < s.size()) {       sort(f.begin(), f.end(), greater<long long>());       for (auto x : f) {         cout << x;       }       cout << endl;       return 0;     }     bool done = 0;     for (long long i = 0; i < s.size(); ++i) {       long long x = s[i];       for (long long num = x; num >= 0; --num) {         if (dp[num] > 0) {           if (num == x) {             dp[num]--;             vector<long long> rem;             for (long long j = 0; j < 10; ++j) {               long long tmp = dp[j];               while (tmp--) {                 rem.push_back(j);               }             }             sort(rem.begin(), rem.end());             bool val = 1;             for (long long j = i + 1; j < s.size(); ++j) {               if (rem[j - (i + 1)] > s[j]) val = 0;               if (rem[j - (i + 1)] < s[j]) break;             }             if (!val) {               dp[num]++;               continue;             }             dp[num]++;           }           cout << num;           if (num < x) done = 1;           dp[num]--;           break;         }       }       if (done) break;     }     vector<long long> rem;     for (long long i = 0; i < 10; ++i) {       while (dp[i] > 0) {         rem.push_back(i);         dp[i]--;       }     }     sort(rem.begin(), rem.end(), greater<long long>());     for (auto x : rem) {       cout << x;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   ostream& operator<<(ostream& os, vector<T> V) {     for (auto v : V) os << v <<    ;     return cout <<   ;   }   template <class T>   ostream& operator<<(ostream& os, set<T> S) {     for (auto s : S) os << s <<    ;     return cout <<   ;   }   template <class L, class R>   ostream& operator<<(ostream& os, pair<L, R> P) {     return os << P.first <<     << P.second;   }   vector<long long> vis;   vector<vector<long long>> adj;   int main() {     long long i, j, n, f1, f2, t, k, a, b, x1, x2, m, w, z;     t = 1;     while (t--) {       cin >> n >> z;       vector<pair<long long, long long>> v(n + 1);       vis.assign(n + 1, 0);       adj.assign(n + 1, {});       for (i = 1; i <= n - 1; i++) {         cin >> a >> b;         adj[a].push_back(b);         adj[b].push_back(a);       }       long long s;       queue<long long> q;       vector<long long> used(n + 1);       vector<long long> x(n + 1), y(n + 1);       vector<long long> p(n + 1);       s = z;       q.push(s);       used[s] = 1;       p[s] = -1;       while (!q.empty()) {         long long v = q.front();         q.pop();         for (long long u : adj[v]) {           if (!used[u]) {             used[u] = 1;             q.push(u);             x[u] = x[v] + 1;             p[u] = v;           }         }       }       used.assign(n + 1, 0);       s = 1;       q.push(s);       used[s] = 1;       p[s] = -1;       while (!q.empty()) {         long long v = q.front();         q.pop();         for (long long u : adj[v]) {           if (!used[u]) {             used[u] = 1;             q.push(u);             y[u] = y[v] + 1;             p[u] = v;           }         }       }       long long ans = 0;       for (i = 1; i <= n; i++) {         if (y[i] > x[i]) {           ans = max(ans, 2 * y[i]);         }       }       cout << ans;     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( no-stack-protector )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   #pragma GCC optimize( unroll-loops )   using namespace std;   void seriy() {     cin.tie(0);     cout.tie(0);   }   signed main() {     seriy();     long long n, m;     cin >> n >> m;     long long mn = 2e9 + 8;     for (long long i = 0; i < m; i++) {       long long l, r;       cin >> l >> r;       mn = min(mn, r - l + 1);     }     cout << mn << endl;     vector<long long> ans(n);     for (long long i = 0; i < n; i++) {       ans[i] = i % mn;     }     for (auto i : ans) cout << i <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int a = 0, b = 0;     int n, d, t;     cin >> n >> d;     int sol = 0;     for (int i = 0; i < n; ++i) {       cin >> t;       if (t == 0) {         if (b + t < 0) {           a = 0;           b = d;           sol++;         } else {           a = max(0, a);         }       } else if (a + t > d) {         sol = -1;         break;       }       a = min(d, a + t);       b = min(d, b + t);     }     cout << sol;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t, m, n, a, u, v;   void printAnswer(vector<int> ans) {     sort(ans.begin(), ans.end());     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) {       cout << ans[i] <<    ;     }     cout << endl;   }   int main() {     cin >> t;     for (int j = 0; j < t; j++) {       cin >> n >> m;       vector<vector<int> > G(n + 1);       for (int i = 0; i < m; i++) {         cin >> v >> u;         G[v].push_back(u);         G[u].push_back(v);       }       vector<int> odd;       vector<int> even;       bool seen[n + 1];       for (int i = 0; i <= n; i++) {         seen[i] = false;       }       bool oddLevel = true;       vector<int> currLevel;       currLevel.push_back(1);       seen[1] = true;       while (currLevel.size() > 0) {         if (oddLevel) {           odd.insert(odd.end(), currLevel.begin(), currLevel.end());         } else {           even.insert(even.end(), currLevel.begin(), currLevel.end());         }         vector<int> nextLevel;         for (int i = 0; i < currLevel.size(); i++) {           int n = currLevel[i];           for (int k = 0; k < G[n].size(); k++) {             if (!seen[G[n][k]]) {               seen[G[n][k]] = true;               nextLevel.push_back(G[n][k]);             }           }         }         currLevel = nextLevel;         oddLevel = !oddLevel;       }       if (odd.size() >= even.size()) {         printAnswer(even);       } else {         printAnswer(odd);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int len, a, b, c;   string s;   int main() {     cin >> s;     len = s.length();     for (int i = 0; i < len; i++) {       if (s[i] ==  a )         a++, c++;       else         b++;       a = max(a, b);       b = max(b, c);     }     cout << a;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 11;   const int Inf = 1e9;   vector<int> dx[3] = {       {-1, 0, 0, 1}, {-1, -1, 1, 1}, {-1, 1, 2, 2, 1, -1, -2, -2}};   vector<int> dy[3] = {       {0, -1, 1, 0}, {-1, 1, -1, 1}, {-2, -2, -1, 1, 2, 2, -1, 1}};   int n;   pair<int, int> g[3][3][N][N][N][N];   int a[N][N];   bool ok(int x, int y) { return 0 <= x && x < n && 0 <= y && y < n; }   void build() {     for (int x = 0; x < n; ++x) {       for (int y = 0; y < n; ++y) {         for (int t = 0; t < 3; ++t) {           for (int i = 0; i < dx[t].size(); ++i) {             if (t == 2) {               int cx = x + dx[t][i];               int cy = y + dy[t][i];               if (ok(cx, cy)) {                 g[t][t][x][y][cx][cy] = {1, 0};               }             } else {               for (int k = 1; k <= n; ++k) {                 int cx = x + dx[t][i] * k;                 int cy = y + dy[t][i] * k;                 if (ok(cx, cy)) {                   g[t][t][x][y][cx][cy] = {1, 0};                 }               }             }           }         }       }     }   }   pair<int, int> sm(pair<int, int> a, pair<int, int> b) {     return {a.first + b.first, a.second + b.second};   }   void count(int x, int y, vector<pair<int, int>> &t) {     if (a[x][y] == n * n) {       return;     }     int ix = -1, jx = -1;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         if (a[x][y] + 1 == a[i][j]) {           ix = i;           jx = j;         }       }     }     vector<pair<int, int>> nt(3, {Inf, Inf});     for (int t1 = 0; t1 < 3; ++t1) {       for (int t2 = 0; t2 < 3; ++t2) {         nt[t2] = min(nt[t2], sm(t[t1], g[t1][t2][x][y][ix][jx]));       }     }     t = nt;     count(ix, jx, t);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.setf(ios::fixed);     cout.precision(20);     cin >> n;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         cin >> a[i][j];       }     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         for (int k = 0; k < n; ++k) {           for (int t = 0; t < n; ++t) {             for (int t1 = 0; t1 < 3; ++t1) {               for (int t2 = 0; t2 < 3; ++t2) {                 g[t1][t2][i][j][k][t] = {Inf, Inf};               }             }           }         }       }     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         for (int t1 = 0; t1 < 3; ++t1) {           for (int t2 = 0; t2 < 3; ++t2) {             if (t1 == t2) {               g[t1][t2][i][j][i][j] = {0, 0};             } else {               g[t1][t2][i][j][i][j] = {1, 1};             }           }         }       }     }     build();     for (int t3 = 0; t3 < 3; ++t3) {       for (int kx = 0; kx < n; ++kx) {         for (int ky = 0; ky < n; ++ky) {           for (int t1 = 0; t1 < 3; ++t1) {             for (int ix = 0; ix < n; ++ix) {               for (int iy = 0; iy < n; ++iy) {                 for (int t2 = 0; t2 < 3; ++t2) {                   for (int jx = 0; jx < n; ++jx) {                     for (int jy = 0; jy < n; ++jy) {                       g[t1][t2][ix][iy][jx][jy] =                           min(g[t1][t2][ix][iy][jx][jy],                               sm(g[t1][t3][ix][iy][kx][ky],                                  g[t3][t2][kx][ky][jx][jy]));                     }                   }                 }               }             }           }         }       }     }     int ix = -1, jx = -1;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         if (a[i][j] == 1) {           ix = i;           jx = j;         }       }     }     vector<pair<int, int>> t(3);     count(ix, jx, t);     pair<int, int> ans = {Inf, Inf};     for (int i = 0; i < 3; ++i) {       ans = min(ans, t[i]);     }     cout << ans.first <<     << ans.second <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long int INF = LONG_LONG_MAX;   const long long int NINF = LONG_LONG_MIN;   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long int n, l, k;     cin >> n >> l >> k;     string s;     cin >> s;     sort((s).begin(), (s).end());     vector<string> list(n);     long long int p = 0;     long long int start = 0;     for (long long int i = 0; i < l; i++) {       for (long long int j = start; j < k; j++) {         list[j] += s[p++];         if (j != 0) {           if (list[j].back() != list[j - 1].back()) start = j;         }       }     }     for (long long int i = 0; i < n; i++) {       for (long long int j = (long long int)list[i].size(); j < l; j++) {         list[i] += s[p++];       }     }     for (string& ss : list) cout << ss << endl;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> dan, ans;   vector<pair<long long, long long> > check;   long long calc(long long st) {     multiset<long long> otr;     long long otv = 0;     for (long long i = 0; i < dan.size(); i++) {       st += dan[i];       if (dan[i] < 0) otr.insert(dan[i]);       if (st < 0) {         st -= *otr.begin();         otr.erase(otr.begin());         otv++;       }     }     return otv;   }   int main() {     long long n, m;     cin >> n >> m;     dan.resize(n);     ans.resize(m, -1);     check.resize(m, {0, 0});     for (long long i = 0; i < n; i++) {       cin >> dan[i];     }     for (long long i = 0; i < m; i++) {       cin >> check[i].first;       check[i].second = i + 1;     }     sort(check.begin(), check.end());     reverse(check.begin(), check.end());     long long l = 0;     while (l < m) {       long long l1 = l, r1 = m, mid, mida;       long long now;       if (ans[l1] != -1)         now = ans[l1];       else {         now = calc(check[l1].first);         ans[l1] = now;       }       while (l1 + 1 != r1) {         mid = (l1 + r1) / 2;         if (ans[mid] != -1)           mida = ans[mid];         else {           mida = calc(check[mid].first);           ans[mid] = mida;         }         if (mida > now) {           r1 = mid;         } else {           l1 = mid;         }       }       if (ans[l1] == -1) {         ans[l1] = calc(check[l1].first);       }       l = l1 + 1;     }     long long last = -1;     for (long long i = 0; i < m; i++) {       if (ans[i] != -1)         last = ans[i];       else         ans[i] = last;     }     for (long long i = 0; i < m; i++) {       swap(check[i].first, check[i].second);       check[i].second = ans[i];     }     sort(check.begin(), check.end());     for (long long i = 0; i < m; i++) {       cout << check[i].second <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 500;   int d[maxn * maxn];   map<int, int> id;   int idcnt;   map<pair<int, int>, int> id2;   int id2cnt;   pair<int, int> status[maxn * maxn];   int ans[maxn * maxn];   int first[maxn * maxn], nxt[1750000], des[1750000], tot;   int W[6];   int T, f;   queue<int> Q;   inline int get(int status, int x_) { return status % W[x_ + 1] / W[x_]; }   void dfs(int dep, int rest, int val) {     if (dep == 4) {       int temp = val + rest * W[4];       if (!id.count(temp)) {         id.insert(make_pair(temp, ++idcnt));       }       return;     }     for (int i = 0; i <= rest; i++) {       dfs(dep + 1, rest - i, val + W[dep] * i);     }   }   inline void AE(int id1, int id2) {     tot++;     des[tot] = id2;     nxt[tot] = first[id1];     first[id1] = tot;     d[id2]++;   }   void addEdge(int fi, int se, int idd) {     for (int i = 1; i <= 4; i++) {       for (int j = 1; j <= 4; j++) {         int f = get(fi, i);         int s = get(se, j);         if (f > 0 && s > 0) {           AE(id2[make_pair(se, fi - W[i] + W[(i + j) % 5])], idd);         }       }     }   }   void search() {     for (int i = 1; i <= id2cnt; i++) {       if (d[i] == 0) {         if (status[i].first == 8) {           ans[i] = 1;         } else {           ans[i] = -1;         }         Q.push(i);       }     }     while (!Q.empty()) {       int q = Q.front();       Q.pop();       if (ans[q] == 1) {         for (int t = first[q]; t; t = nxt[t]) {           int v = des[t];           d[v]--;           if (d[v] == 0 && ans[v] == 0) {             ans[v] = -1;             Q.push(v);           }         }       } else {         for (int t = first[q]; t; t = nxt[t]) {           int v = des[t];           d[v]--;           if (ans[v] == 0) {             ans[v] = 1;             Q.push(v);           }         }       }     }   }   void init() {     W[0] = 1;     for (int i = 1; i <= 5; i++) W[i] = W[i - 1] * 10;     dfs(0, 8, 0);     for (pair<int, int> fi : id) {       for (pair<int, int> se : id) {         id2.insert(make_pair(make_pair(fi.first, se.first), ++id2cnt));         status[id2cnt] = make_pair(fi.first, se.first);       }     }     memset(d, 0, sizeof d);     for (pair<pair<int, int>, int> e : id2) {       addEdge(e.first.first, e.first.second, e.second);     }     search();   }   void solve() {     scanf( %d , &f);     int ast = 0, bst = 0;     for (int i = 0; i < 8; i++) {       int t;       scanf( %d , &t);       ast += W[t];     }     for (int i = 0; i < 8; i++) {       int t;       scanf( %d , &t);       bst += W[t];     }     if (f) {       int res = ans[id2[make_pair(bst, ast)]];       if (res == 1) {         printf( Bob n );       } else if (res == -1) {         printf( Alice n );       } else {         printf( Deal n );       }     } else {       int res = ans[id2[make_pair(ast, bst)]];       if (res == 1) {         printf( Alice n );       } else if (res == -1) {         printf( Bob n );       } else {         printf( Deal n );       }     }   }   int main() {     init();     scanf( %d , &T);     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int n, cnt[60], have, r = 1;   int main() {     ios_base::sync_with_stdio(0);     cin.tie();     cout.tie();     cin >> s;     n = s.size();     s =     + s;     for (int i = (1); i <= (n); ++i) {       while (r <= n && have < 26) {         if (s[r] ==  ? )           ++have;         else {           int temp = s[r] -  A ;           if (!cnt[temp]) ++have;           ++cnt[temp];         }         ++r;       }       if (have == 26 && r - i == 26) {         int q[50], top = 0;         for (int j = (0); j <= (25); ++j)           if (!cnt[j]) q[++top] = j;         for (int j = (1); j <= (i - 1); ++j)           if (s[j] ==  ? )             cout <<  A ;           else             cout << s[j];         for (int j = (i); j <= (min(r, n)); ++j)           if (s[j] ==  ? ) {             if (top)               cout << (char)( A  + q[top--]);             else               cout <<  A ;           } else             cout << s[j];         for (int j = (r + 1); j <= (n); ++j)           if (s[j] ==  ? )             cout <<  A ;           else             cout << s[j];         return 0;       }       if (s[i] ==  ? )         --have;       else {         int temp = s[i] -  A ;         --cnt[temp];         if (!cnt[temp]) --have;       }     }     cout << -1;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   const int MAX = 1e5 + 55;   const int inf = 1e9 + 77;   const int MOD = 1e9 + 7;   const double PI = acos(-1.0);   const double eps = 1e-7;   int main() {     int n, k;     scanf( %d , &n);     scanf( %d , &k);     if (n > (long long)k * (long long)(k - 1)) {       printf( NO );       return 0;     }     printf( YES n );     int need = n;     for (int i = 1; i <= k && need; ++i) {       for (int j = i + 1; j <= k && need; ++j) {         printf( %d %d n , i, j);         --need;         if (need) {           printf( %d %d n , j, i);           --need;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long spf[1000005];   vector<long long> primes, prev[1000005], next[1000005];   void sieve() {     for (long long i = 2; i < 1000005; ++i) {       if (spf[i] == 0) {         spf[i] = i;         for (long long j = 2 * i; j <= 1000005; j += i) {           if (spf[j] == 0) spf[j] = i;         }       }     }   }   bool cmp(const pair<long long, long long>& a,            const pair<long long, long long>& b) {     if (a.first == b.first)       return a.second < b.second;     else       return a.first > b.first;   }   string kmp(string s) {     long long n = s.size();     vector<long long> p(n);     long long i = 1, len = 0;     while (i < n) {       if (s[i] == s[len]) {         len++;         p[i] = len;         i++;       } else {         if (len != 0) {           len = p[len - 1];         } else {           p[i] = 0;           i++;         }       }     }     long long ans = p[n - 1];     string t = s.substr(0, ans);     return t;   }   string pal(string t) {     string r = t;     reverse(r.begin(), r.end());     string t1 = kmp(r +  #  + t);     string t2 = kmp(t +  #  + r);     return t1.size() > t2.size() ? t1 : t2;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     srand(static_cast<unsigned>(time(0)));     long long t = 1;     cin >> t;     while (t--) {       string s;       cin >> s;       long long n = s.size();       long long i = 0;       while (i <= n - i - 1 && s[i] == s[n - i - 1]) {         i++;       }       if (i > n - i - 1) {         cout << s <<   n ;         continue;       }       string t1 = s.substr(i, n - 2 * i);       string ans = pal(t1);       string temp = s.substr(0, i);       ans = temp + ans;       reverse(temp.begin(), temp.end());       ans = ans + temp;       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   vector<long long int> graph[100007];   long long int vis[100007];   long long int mxm;   void dfs(long long int u, long long int p, long long int d) {     vis[u] = 1;     mxm = max(mxm, d);     for (long long int v : graph[u]) {       dfs(v, u, d + 1);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int tc = 1;     long long int cs = 0;     while (tc--) {       long long int n;       cin >> n;       mxm = 0;       for (long long int i = 1; i <= n; i++) {         long long int a;         cin >> a;         if (a != -1) graph[a].push_back(i);       }       long long int ans = 0;       for (long long int i = 1; i <= n; i++) {         if (!vis[i]) {           dfs(i, -1, 1);           ans = max(ans, mxm);           mxm = 0;         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 31400;   struct point {     int x, y;   };   int n;   pair<point, point> p[5];   bool used[5];   bool func() {     long long s = 0;     point MIN, MAX;     MIN.x = MIN.y = NMAX;     MAX.x = MAX.y = 0;     for (int i = 0; i < n; i++) {       s += (p[i].second.x - p[i].first.x) * (p[i].second.y - p[i].first.y);       MIN.x = min(MIN.x, p[i].first.x);       MIN.y = min(MIN.y, p[i].first.y);       MAX.x = max(MAX.x, p[i].second.x);       MAX.y = max(MAX.y, p[i].second.y);     }     if ((MAX.x - MIN.x) == (MAX.y - MIN.y) && s == pow(MAX.x - MIN.x, 2)) {       return true;     }     return false;   }   int main() {     cin >> n;     pair<int, int> a, b, MAX, MIN;     for (int i = 0; i < n; i++) {       cin >> p[i].first.x >> p[i].first.y >> p[i].second.x >> p[i].second.y;     }     if (func())       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long int power(unsigned long long int n,                                unsigned long long int m) {     if (n == 0LL)       return 1LL % m;     else if (n & 1LL)       return ((power(n / 2, m) % m) * (power(n / 2, m) % m) * (3LL % m)) % m;     else       return ((power(n / 2, m) % m) * (power(n / 2, m) % m)) % m;   }   void solve() {     unsigned long long int n, m, ans;     cin >> n >> m;     ans = (power(n, m) + m - 1) % m;     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cout.tie(NULL);     cin.tie(NULL);     ;     unsigned long long int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   const long long mod = 1000000007;   int A[N], rmq[N][25], getL[N];   void go(int n) {     for (int i = 0; i < n; i++) rmq[i][0] = i;     for (int i = 1; i < N; i++) getL[i] = log2(i);     for (int j = 1; (1 << j) <= n; j++) {       for (int i = 0; i + (1 << j) - 1 < n; i++) {         if (A[rmq[i][j - 1]] < A[rmq[i + (1 << (j - 1))][j - 1]]) {           rmq[i][j] = rmq[i][j - 1];         } else {           rmq[i][j] = rmq[i + (1 << (j - 1))][j - 1];         }       }     }   }   int getmin(int i, int j) {     int k = getL[j - i + 1];     if (A[rmq[i][k]] < A[rmq[j - (1 << k) + 1][k]]) {       return A[rmq[i][k]];     }     return A[rmq[j - (1 << k) + 1][k]];   }   vector<pair<int, int> > v;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n, d, m;     cin >> d >> n >> m;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       v.push_back(make_pair(x, y));     }     v.push_back(make_pair(d, 0));     sort(v.begin(), v.end());     int st = 0, fuel = n;     for (int i = 0; i < v.size(); i++) A[i] = v[i].second;     go(m + 1);     long long cst = 0;     for (int i = 0; i < v.size(); i++) {       if (v[i].first - st > n) {         cst = -1;         break;       }       fuel -= (v[i].first - st);       int l = i + 1, r = v.size() - 1, res = i;       while (l <= r) {         int mid = (l + r) >> 1;         if (v[mid].first - v[i].first <= fuel) {           l = mid + 1;           res = mid;         } else           r = mid - 1;       }       int nxtmin = i;       l = i + 1, r = v.size() - 1;       while (l <= r) {         int mid = (l + r) >> 1;         if (getmin(i + 1, mid) <= v[i].second) {           r = mid - 1;           nxtmin = mid;         } else           l = mid + 1;       }       st = v[i].first;       if (res >= nxtmin) {         continue;       }       cst += (min(n, v[nxtmin].first - v[i].first) - fuel) * 1LL * v[i].second;       fuel = min(n, v[nxtmin].first - v[i].first);     }     cout << cst;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   struct edge {     int to, next;   } e[N * 2];   int head[N], tot, n;   int sz[N], mxv, mxrt;   void add(int x, int y) {     e[++tot] = (edge){y, head[x]};     head[x] = tot;   }   void dfs(int x, int fa, int n) {     int mx = 0;     sz[x] = 1;     for (int i = head[x]; i; i = e[i].next)       if (e[i].to != fa) {         dfs(e[i].to, x, n);         sz[x] += sz[e[i].to];         mx = max(mx, sz[e[i].to]);       }     mx = max(mx, n - sz[x]);     if (mx < mxv) mxv = mx, mxrt = x;   }   pair<int, int> q[N];   int f[N][N];   int top, be[N], mnv, mnid, dif;   void insert(int x, int y) {     q[++top] = pair<int, int>(x, y);     for (int i = (int)(0); i <= (int)(n); i++) f[top][i] = f[top - 1][i];     for (int i = (int)(y); i <= (int)(n); i++) f[top][i] |= f[top - 1][i - y];   }   int s1, s2, dep[N];   void getans(int x, int fa, int type) {     dep[x] = (type == 1 ? (s1 += 1) : (s2 += dif));     printf( %d %d %d n , x, fa, dep[x] - dep[fa]);     for (int i = head[x]; i; i = e[i].next)       if (e[i].to != fa) getans(e[i].to, x, type);   }   int main() {     scanf( %d , &n);     for (int i = (int)(1); i <= (int)(n - 1); i++) {       int x, y;       scanf( %d%d , &x, &y);       add(x, y);       add(y, x);     }     mxv = 1 << 30;     dfs(1, 0, n);     f[0][0] = 1;     for (int i = head[mxrt]; i; i = e[i].next)       insert(e[i].to, sz[e[i].to] > sz[mxrt] ? n - sz[mxrt] : sz[e[i].to]);     insert(mxrt, 1);     mnv = 1 << 30;     for (int i = (int)(0); i <= (int)(n); i++)       if (abs(n - 2 * i) < mnv && f[top][i]) mnv = n - 2 * i, mnid = i;     dif = mnid;     for (int i = (int)(top); i >= (int)(1); i--) {       if (mnid >= q[i].second && f[i - 1][mnid - q[i].second])         be[i] = 1, mnid -= q[i].second;       else         be[i] = 0;     }     for (int i = (int)(1); i <= (int)(top - 1); i++)       getans(q[i].first, mxrt, be[i]);   }


#include <bits/stdc++.h>   using namespace std;   struct Node {     vector<int> children;     int value, bit;     Node(int b) {       children.assign(2, -1);       value = 0;       bit = b;     }   };   vector<Node> tree;   void add(int id, int amt, int n) {     tree[n].value += amt;     if (tree[n].bit == 0) return;     int next = id / tree[n].bit;     if (tree[n].children[next] == -1) {       tree[n].children[next] = tree.size();       tree.emplace_back(tree[n].bit / 2);     }     add(id & (tree[n].bit - 1), amt, tree[n].children[next]);   }   int query(int id, int l, int n) {     if (tree[n].bit == 0) return 0;     int nextId = id & (tree[n].bit - 1);     int nextL = l & (tree[n].bit - 1);     int child = (id ^ l) / tree[n].bit;     int result = 0;     if (l & tree[n].bit) {       int rChild = tree[n].children[1 - child];       if (rChild != -1) result += tree[rChild].value;     }     if (tree[n].children[child] != -1)       result += query(nextId, nextL, tree[n].children[child]);     return result;   }   int main() {     tree.emplace_back(1 << 30);     int q;     cin >> q;     for (int qa = 0; qa < q; ++qa) {       int type;       cin >> type;       if (type < 3) {         int p;         cin >> p;         add(p, type == 1 ? 1 : -1, 0);       } else {         int p, l;         cin >> p >> l;         cout << query(p, l, 0) << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   double PI = 3.14159;   int dX[] = {1, -1, 0, 0};   int dY[] = {0, 0, 1, -1};   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     double h, m, angle_h = 0, angle_m = 0;     char c;     cin >> h >> c >> m;     h = fmod(h, 12);     angle_h = (h + m / 60) * 5;     angle_h *= 6.0;     angle_m = m * 6;     cout << fixed << setprecision(9) << angle_h <<     << angle_m <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, pos, cnt = 0;     cin >> n >> a >> b;     for (int pos = 1; pos <= n; pos++) {       if (n - pos <= b && pos - 1 >= a) cnt++;     }     cout << cnt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, q;     cin >> n >> q;     while (q--) {       long long a, b;       cin >> a >> b;       if ((a + b) % 2 == 0) {         long long ans = 0;         ans += (a) / 2 * ((n + 1) / 2);         ans += ((a - 1) - (a / 2)) * (n / 2);         if (a % 2) {           ans += (b + 1) / 2;         } else {           ans += b / 2;         }         cout << ans <<   n ;       } else {         long long ans = (n * n + 1) / 2;         ans += (a) / 2 * (n / 2);         ans += ((a - 1) - (a / 2)) * ((n + 1) / 2);         if (a % 2) {           ans += (b) / 2;         } else {           ans += (b + 1) / 2;         }         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k, c, ans = 0;     string s;     cin >> s;     bool sol = 1;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  1  && s[i + 1] ==  4  && s[i + 2] ==  4 ) {         i += 2;         continue;       }       if (s[i] ==  1  || (s[i] ==  4  && s[i - 1] ==  1 ))         continue;       else         sol = 0;     }     if (sol)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 100;   int a[max_n], tmp[max_n];   bool vis[max_n];   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   int main() {     int n;     cin >> n;     for (int i = 0; i < (n); i++) {       cin >> a[i];       a[i]--;       tmp[i] = a[i];     }     sort(tmp, tmp + n);     for (int i = 0; i < (n); i++)       if (i != tmp[i]) {         cout << -1 << endl;         return 0;       }     long long ans = 1;     memset(vis, false, sizeof vis);     for (int i = 0; i < (n); i++)       if (!vis[i]) {         int cnt = 0;         for (int k = i; !vis[k]; k = a[k]) {           vis[k] = true;           cnt++;         }         if (cnt % 2 == 0) cnt /= 2;         ans *= cnt / gcd(ans, cnt);       }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long p;   long long a[1000005];   long long fastpow(long long a, int x) {     if (x < 0) return 1;     if (x == 0) return 1;     if (x == 1) return a;     long long tmp = fastpow(a, x >> 1);     tmp = (tmp * tmp) % 1000000007ll;     if (x & 1) {       tmp = (tmp * a) % 1000000007ll;     }     return tmp;   }   void solve() {     scanf( %d%lld , &n, &p);     for (int i = 0; i < n; i++) {       scanf( %lld , a + i);     }     if (p == 1) {       printf( %d n , n % 2);       return;     }     sort(a, a + n);     long long ans = 0;     long long res = 0;     int last = 0;     for (int i = n - 1; i >= 0; i--) {       if (i != n - 1) {         ans *= fastpow(p, last - a[i]);         ans %= 1000000007ll;       }       if (i != n - 1 && res <= 1e6 + 100) {         for (int j = 0; j < 20 && j < last - a[i]; j++) {           res *= p;           if (res > 1e6 + 100) {             res = 1e18;             break;           }         }       }       if (res == 0) {         ans++;         res++;       } else {         ans += 1000000007ll - 1;         res--;       }       ans %= 1000000007ll;       last = a[i];     }     ans *= fastpow(p, last);     printf( %lld n , ans % 1000000007ll);   }   int main() {     ios_base::sync_with_stdio(false);     int T;     scanf( %d , &T);     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool remin(T &a, const T &b) {     if (a > b) {       a = b;       return true;     }     return false;   }   template <class T>   bool remax(T &a, const T &b) {     if (a < b) {       a = b;       return true;     }     return false;   }   int nxt() {     int first;     scanf( %d , &first);     return first;   }   long long gcd(long long a, long long b) {     a = abs(a);     b = abs(b);     while (b) {       a %= b;       swap(a, b);     }     return a;   }   struct pt {     long double first, second, z;     pt() {}     pt(long double first, long double second, long double z)         : first(first), second(second), z(z) {}     inline pt operator-(const pt &r) const {       return pt(first - r.first, second - r.second, z - r.z);     }     inline pt operator+(const pt &r) const {       return pt(first + r.first, second + r.second, z + r.z);     }     inline pt operator*(const long double &r) const {       return pt(first * r, second * r, z * r);     }     inline long double sqlen() const {       return abs(first * first + second * second + z * z);     }     long double len() const { return sqrtl(sqlen()); }     inline bool operator<(const pt &r) const {       if (first != r.first) return first < r.first;       return second < r.second;     }     inline bool operator==(const pt &r) const {       return first == r.first && second == r.second;     }   };   ostream &operator<<(ostream &os, const pt &p) {     return os <<  (  << p.first <<  ,  << p.second <<  ) ;   }   inline pt cross(const pt &l, const pt &r) {     return pt(l.second * r.z - l.z * r.second, l.z * r.first - l.first * r.z,               l.first * r.second - l.second * r.first);   }   inline long double dot(const pt &l, const pt &r) {     return l.first * r.first + l.second * r.second + l.z * r.z;   }   long long pwmod(long long a, long long n, long long mod) {     long long ret = 1;     while (n) {       if (n & 1) ret = ret * a % mod;       a = a * a % mod;       n >>= 1;     }     return ret;   }   template <typename T>   inline T sqr(T first) {     return first * first;   }   bool remin(long long &first, long long second) {     if (first > second) {       first = second;       return 1;     }     return 0;   }   const int N = 300 * 1000;   vector<int> g[N];   int size[N];   int parent[N];   int answer[N];   int maxSize[N];   vector<int> childs_[N];   void dfs(int v, int p = -1) {     size[v] = 1;     parent[v] = p;     for (int to : g[v]) {       if (to == p) continue;       dfs(to, v);       size[v] += size[to];     }     maxSize[v] = 0;     for (int to : g[v]) {       if (to == p) continue;       remax(maxSize[v], size[to]);     }   }   void dfs2(int v, int p = -1) {     vector<int> &ch = childs_[v];     for (int to : g[v]) {       if (to == p) continue;       dfs2(to, v);       ch.push_back(answer[to]);     }     if (ch.empty()) {       answer[v] = v;       return;     }     for (int &u : ch) {       int s = size[v] / 2;       while (u != v && maxSize[parent[u]] <= s) {         u = parent[u];       }     }     for (int u : ch) {       int s = size[v] / 2;       int top = size[v] - size[u];       if (maxSize[u] <= s && top <= s) {         answer[v] = u;       }     }   }   void solve() {     int n = nxt();     int q = nxt();     for (int i = 1; i < n; ++i) {       int p = nxt() - 1;       g[p].push_back(i);       g[i].push_back(p);     }     dfs(0);     dfs2(0);     for (int i = 0; i < (int)(q); i++) {       int v = nxt() - 1;       cout << answer[v] + 1 <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t = 1;     for (int i = 0; i < (int)(t); i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int mod = 1000000007;   const long long linf = 0x3f3f3f3f3f3f3f3f;   char c[200100];   int qnt[200100][26];   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n, k;       scanf( %d%d , &n, &k);       scanf(  %s , c);       int m = n / k;       int res = 0;       if (k % 2) {         for (int i = 0; i <= k / 2; i++) {           for (int j = 0; j < m; j++) {             int a = i + j * k;             int b = k - 1 - i + j * k;             qnt[i][c[a] -  a ]++;             qnt[i][c[b] -  a ]++;           }         }         for (int i = 0; i < k / 2; i++) {           int maior = 0;           for (int j = 0; j < 26; j++) maior = max(maior, qnt[i][j]);           res += 2 * m - maior;         }         int maior = 0;         for (int j = 0; j < 26; j++) maior = max(maior, qnt[k / 2][j]);         res += (2 * m - maior) / 2;       } else {         for (int i = 0; i < k / 2; i++) {           for (int j = 0; j < m; j++) {             int a = i + j * k;             int b = k - 1 - i + j * k;             qnt[i][c[a] -  a ]++;             qnt[i][c[b] -  a ]++;           }         }         for (int i = 0; i < k / 2; i++) {           int maior = 0;           for (int j = 0; j < 26; j++) maior = max(maior, qnt[i][j]);           res += 2 * m - maior;         }       }       printf( %d n , res);       for (int i = 0; i <= n; i++) {         for (int j = 0; j < 26; j++) qnt[i][j] = 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     bool x = false;     string s;     cin >> s;     string b;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  d  && s[i + 1] ==  o  && s[i + 2] ==  t  && i != 0 &&           i + 2 < s.size() - 1) {         b +=  . ;         s[i + 1] =    ;         s[i + 2] =    ;       } else if (x == false && s[i] ==  a  && s[i + 1] ==  t  && i != 0 &&                  i + 1 < s.size() - 1) {         b +=  @ ;         s[i + 1] =    ;         x = true;       } else if (s[i] !=    ) {         b += s[i];       }     }     cout << b;     return 0;   }


#include <bits/stdc++.h>   long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};   long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};   using namespace std;   class pa3 {    public:     long long x;     long long y, z;     pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {}     bool operator<(const pa3 &p) const {       if (x != p.x) return x < p.x;       if (y != p.y) return y < p.y;       return z < p.z;     }     bool operator>(const pa3 &p) const {       if (x != p.x) return x > p.x;       if (y != p.y) return y > p.y;       return z > p.z;     }     bool operator==(const pa3 &p) const {       return x == p.x && y == p.y && z == p.z;     }     bool operator!=(const pa3 &p) const {       return !(x == p.x && y == p.y && z == p.z);     }   };   class pa4 {    public:     long long x;     long long y, z, w;     pa4(long long x = 0, long long y = 0, long long z = 0, long long w = 0)         : x(x), y(y), z(z), w(w) {}     bool operator<(const pa4 &p) const {       if (x != p.x) return x < p.x;       if (y != p.y) return y < p.y;       if (z != p.z) return z < p.z;       return w < p.w;     }     bool operator>(const pa4 &p) const {       if (x != p.x) return x > p.x;       if (y != p.y) return y > p.y;       if (z != p.z) return z > p.z;       return w > p.w;     }     bool operator==(const pa4 &p) const {       return x == p.x && y == p.y && z == p.z && w == p.w;     }   };   class pa2 {    public:     long long x, y;     pa2(long long x = 0, long long y = 0) : x(x), y(y) {}     pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); }     pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); }     bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; }     bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; }     bool operator==(const pa2 &p) const {       return abs(x - p.x) == 0 && abs(y - p.y) == 0;     }     bool operator!=(const pa2 &p) const {       return !(abs(x - p.x) == 0 && abs(y - p.y) == 0);     }   };   string itos(long long i) {     ostringstream s;     s << i;     return s.str();   }   long long gcd(long long v, long long b) {     if (v > b) return gcd(b, v);     if (v == b) return b;     if (b % v == 0) return v;     return gcd(v, b % v);   }   long long mod;   long long extgcd(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     long long d = extgcd(b, a % b, y, x);     y -= a / b * x;     return d;   }   pair<long long, long long> operator+(const pair<long long, long long> &l,                                        const pair<long long, long long> &r) {     return {l.first + r.first, l.second + r.second};   }   pair<long long, long long> operator-(const pair<long long, long long> &l,                                        const pair<long long, long long> &r) {     return {l.first - r.first, l.second - r.second};   }   long long pr[200010];   long long inv[200010];   long long beki(long long wa, long long rr, long long warukazu) {     if (rr == 0) return 1 % warukazu;     if (rr == 1) return wa % warukazu;     wa %= warukazu;     if (rr % 2 == 1)       return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu;     long long zx = beki(wa, rr / 2, warukazu);     return (zx * zx) % warukazu;   }   long long comb(long long nn, long long rr) {     if (rr < 0 || rr > nn || nn < 0) return 0;     long long r = pr[nn] * inv[rr];     r %= mod;     r *= inv[nn - rr];     r %= mod;     return r;   }   void gya(long long ert) {     pr[0] = 1;     for (long long i = 1; i <= ert; i++) {       pr[i] = (pr[i - 1] * i) % mod;     }     inv[ert] = beki(pr[ert], mod - 2, mod);     for (long long i = ert - 1; i >= 0; i--) {       inv[i] = inv[i + 1] * (i + 1) % mod;     }   }   long long ko[11] = {};   vector<long long> p[11][11], q[11][11];   long long ju[40];   long long solve(pair<string, long long> X) {     string s = X.first;     reverse(s.begin(), s.end());     long long l = X.second;     long long ans = 0;     for (long long i = 1; i <= 10; i++) {       long long e = 0;       for (long long j = 0; j < l; j++) {         e += ju[q[i][l][j]] * (s[j] -  0 ) % mod;         e += ju[p[l][i][j]] * (s[j] -  0 ) % mod;         e %= mod;       }       e *= ko[i];       e %= mod;       ans += e;       ans %= mod;     }     return ans;   }   signed main() {     cin.tie(0);     ios::sync_with_stdio(false);     long long n;     cin >> n;     mod = 998244353;     vector<pair<string, long long>> ve;     ju[0] = 1;     for (long long i = 1; i < 40; i++) {       ju[i] = ju[i - 1] * 10 % mod;     }     for (long long l1 = 1; l1 <= 10; l1++)       for (long long r1 = 1; r1 <= 10; r1++) {         long long f = 0;         for (long long i = 0;; i++) {           if (i >= l1 && i >= r1) break;           if (i < l1) {             p[l1][r1].push_back(f);             f++;           }           if (i < r1) {             q[l1][r1].push_back(f);             f++;           }         }       }     for (long long i = 0; i < n; i++) {       long long y;       cin >> y;       string s = itos(y);       ve.push_back(make_pair(s, s.length()));       ko[s.length()]++;     }     long long ans = 0;     for (auto v : ve) {       long long r = solve(v);       ans += r;       ans %= mod;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 1e3 + 100, M = 1e9 + 7;   long long int n, m, d[mx][mx];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     d[0][0] = 1;     for (int i = 1; i < mx; i++) {       d[i][0] = 1;       for (int j = 1; j < i + 1; j++)         d[i][j] = (d[i - 1][j - 1] + d[i - 1][j]) % M;     }     cout << d[(m * 2) + (n - 1)][(n - 1)] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T1, class T2>   int upmin(T1 &x, T2 v) {     if (x <= v) {       return 0;     }     x = v;     return 1;   }   template <class T1, class T2>   int upmax(T1 &x, T2 v) {     if (x >= v) {       return 0;     }     x = v;     return 1;   }   const int INF = 0x3f3f3f3f;   void init() {     int N;     cin >> N;     map<string, string> mp, rvmp;     for (int i = 0; i < N; ++i) {       string a, b;       cin >> a >> b;       if (not rvmp.count(a)) {         mp[a] = b;         rvmp[b] = a;       } else {         mp[rvmp[a]] = b;         rvmp[b] = rvmp[a];         rvmp.erase(a);       }     }     cout << mp.size() << endl;     for (auto it = mp.begin(); it != mp.end(); ++it) {       cout << it->first <<     << it->second << endl;     }   }   void preprocess() {}   void solve() {}   signed main() {     ios::sync_with_stdio(0);     init();     preprocess();     solve();     return 0;   }


#include <bits/stdc++.h>   int abs(int x) { return x > 0 ? x : -x; }   int max(int x, int y) { return x > y ? x : y; }   int min(int x, int y) { return x < y ? x : y; }   long long read() {     long long ans = 0;     char ch = getchar(), t = ch;     while (ch <  0  || ch >  9 ) {       t = ch;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       ans = ans * 10 + ch -  0 ;       ch = getchar();     }     if (t ==  - ) ans = -ans;     return ans;   }   using namespace std;   int main() {     long long n = read();     while (n--) {       long long l = read(), r = read();       long long bit = 0;       if (l == 0 && r == 0) {         printf( 0 n );         continue;       }       while ((1ll * 1 << bit) <= r) {         if ((l | (1ll * 1 << bit)) > r) {           printf( %I64d n , l);           break;         }         if ((l | (1ll * 1 << bit)) == r) {           printf( %I64d n , l | (1ll * 1 << bit));           break;         }         l = l | (1ll * 1 << bit);         bit++;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, long long> C;   long long B[1100000], pre[1100000], suf[1100000], A[1100000], tot, i, n,       c[1100000], ans;   void add(long long x) {     for (; x <= n; x += (x & (-x))) c[x]++;   }   long long getsum(long long x) {     long long sum = 0;     for (; x >= 1; x -= (x & (-x))) sum += c[x];     return sum;   }   int main() {     scanf( %I64d , &n);     for (i = 1; i <= n; i++) cin >> A[i];     for (i = 1; i <= n; i++) C[A[i]]++, pre[i] = C[A[i]];     C.clear();     for (i = n; i >= 1; i--) C[A[i]]++, suf[i] = C[A[i]];     for (i = n; i >= 1; i--) {       ans += getsum(pre[i] - 1);       add(suf[i]);     }     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   char st[100000];   int n, t, a[26], minn, maxx;   int main() {     scanf( %d%d , &n, &t);     scanf( %s , st);     minn = 25;     maxx = 0;     for (int i = 0; i < n; i++) {       if (st[i] - 97 < minn) minn = st[i] - 97;       if (st[i] - 97 > maxx) maxx = st[i] - 97;       a[st[i] - 97] = 1;     }     if (t > n) {       printf( %s , st);       for (int i = n + 1; i <= t; i++) printf( %c , minn + 97);     } else {       int wei = t - 1;       while (st[wei] - 97 == maxx) {         st[wei] = minn + 97;         wei--;       }       st[wei]++;       while (a[st[wei] - 97] == 0) st[wei]++;       for (int i = 0; i < t; i++) printf( %c , st[i]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 100;   void breakGame(int a, int b) {     int n, j, k, dk;     if (a > b) swap(a, b);     dk = 2 * a;     k = a + 1;     n = b - a;     if (n & 1) k *= 2;     cout << k <<   n ;     if (n % 2 == 0) {       for (j = 0; j <= dk; j += 2) cout << n / 2 + j <<    ;     } else       for (j = 0; j <= dk; j += 2)         cout << n / 2 + j <<     << n / 2 + j + 1 <<    ;     cout <<   n ;   }   int main() {     int t, a, b;     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> t;     while (t--) {       cin >> a >> b;       breakGame(a, b);     }     return 0;   }