#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   const int mo = 1000000007;   const int inf = 0x3f3f3f3f;   const int INF = 2000000000;   int n, m, phi;   int lt[100008], size[100008], f[100008], vis[100008], a[100008], b[100008], sum,       tot, root, pre1[100008], pre2[100008];   long long ans, Alltmp;   map<int, int> mp;   struct edge {     int u, v, w, nt;   } eg[100008 * 2];   void add(int u, int v, int w) {     eg[++sum] = (edge){u, v, w, lt[u]};     lt[u] = sum;   }   long long quick(long long x, long long y) {     long long res = 1;     while (y) {       if (y & 1) res = res * x % m;       x = x * x % m;       y >>= 1;     }     return res;   }   void init() {     int mm = m;     phi = m;     for (int i = 2; i * i <= m; i++) {       if (mm % i == 0) {         while (mm % i == 0) mm /= i;         phi = phi / i * (i - 1);       }     }     if (mm > 1) phi = phi / mm * (mm - 1);     memset(lt, 0, sizeof(lt));     ;     sum = 1;     memset(f, 0, sizeof(f));     ;     f[0] = INF;     memset(vis, 0, sizeof(vis));     ;     ans = 0;     pre1[0] = 1;     pre2[0] = 1;     for (int i = 1; i <= 100000; i++) {       pre1[i] = 1ll * pre1[i - 1] * 10 % m;       pre2[i] = quick(pre1[i], phi - 1);     }   }   void getRoot(int u, int fa) {     size[u] = 1;     f[u] = 0;     for (int i = lt[u]; i; i = eg[i].nt) {       int v = eg[i].v;       if (vis[v] || v == fa) continue;       getRoot(v, u);       size[u] += size[v];       f[u] = max(f[u], size[v]);     }     f[u] = max(f[u], tot - size[u]);     if (f[u] < f[root]) root = u;   }   void getA(int u, int fa, int len) {     for (int i = lt[u]; i; i = eg[i].nt) {       int v = eg[i].v;       if (vis[v] || v == fa) continue;       a[v] = (1ll * eg[i].w * pre1[len] + a[u]) % m;       getA(v, u, len + 1);     }     Alltmp += mp[a[u]];     int tp = (m - b[u]) % m;     tp = 1ll * tp * pre2[len] % m;     if (a[u] == tp) Alltmp--;   }   void getB(int u, int fa, int len) {     int tp = (m - b[u]) % m;     tp = 1ll * tp * pre2[len] % m;     mp[tp]++;     for (int i = lt[u]; i; i = eg[i].nt) {       int v = eg[i].v;       if (vis[v] || v == fa) continue;       b[v] = (1ll * b[u] * 10 + eg[i].w) % m;       getB(v, u, len + 1);     }   }   long long calc(int u, int key) {     key %= m;     Alltmp = 0;     mp.clear();     a[u] = b[u] = key;     getB(u, 0, key == 0 ? 0 : 1);     getA(u, 0, key == 0 ? 0 : 1);     return Alltmp;   }   void solve(int u) {     ans += calc(u, 0);     vis[u] = 1;     for (int i = lt[u]; i; i = eg[i].nt) {       int v = eg[i].v;       if (vis[v]) continue;       ans -= calc(v, eg[i].w);       root = 0;       tot = size[v];       getRoot(v, u);       solve(root);     }   }   int main() {     scanf( %d%d , &n, &m);     init();     for (int i = 1; i <= n - 1; i++) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       add(u + 1, v + 1, w);       add(v + 1, u + 1, w);     }     root = 0;     tot = n;     getRoot(1, 0);     solve(root);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, x1, y1, x2, y2;     scanf( %d%d%d%d%d%d , &n, &m, &x1, &y1, &x2, &y2);     int x, y;     x = abs(x1 - x2);     y = abs(y1 - y2);     if (x > y) swap(x, y);     if (y < 4) {       cout <<  First  << endl;       return 0;     }     if (x < 3 && y < 5) {       cout <<  First  << endl;       return 0;     }     cout <<  Second  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 55;   int n, m;   int a[MAXN];   double dp[MAXN][MAXN][MAXN];   double C[MAXN][MAXN];   void init() {     C[0][0] = 1.0;     for (int i = 1; i <= 50; i++) {       C[i][0] = 1.0;       for (int j = 1; j <= i; j++) C[i][j] = C[i - 1][j - 1] + C[i - 1][j];     }   }   int main() {     scanf( %d%d , &n, &m);     init();     for (int i = 1; i <= m; i++) scanf( %d , &a[i]);     for (int i = 0; i <= n; i++) dp[0][0][i] = i;     for (int i = 1; i <= m; i++) {       for (int j = 0; j <= n; j++) {         for (int k = 0; k <= n; k++) {           for (int c = 0; c <= j; c++) {             int maxq = max(k, (c + a[i] - 1) / a[i]);             dp[i][j][k] +=                 dp[i - 1][j - c][maxq] * pow(i - 1, j - c) / pow(i, j) * C[j][c];           }         }       }     }     printf( %.12f n , dp[m][n][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double f(double m, vector<int> &v) {     int n = v.size();     vector<long double> v2(n);     for (int i = 0; i < (n); i++) v2[i] = v[i] - m;     long double mx_otr = 0;     long double mn_otr = 0;     long double cur = 0;     for (int i = 0; i < (n); i++) {       cur += v2[i];       if (cur < 0) cur = 0;       mx_otr = max(mx_otr, cur);     }     cur = 0;     for (int i = 0; i < (n); i++) {       cur += v2[i];       if (cur >= 0) cur = 0;       mn_otr = min(mn_otr, cur);     }     return max(mx_otr, abs(mn_otr));   }   inline void solve() {     int n;     cin >> n;     vector<int> v(n);     for (int i = 0; i < (n); i++) cin >> v[i];     long double l = -10000, r = 10000;     while (abs(r - l) > 1e-12) {       long double m1 = l + (r - l) / 3;       long double m2 = r - (r - l) / 3;       if (f(m1, v) <= f(m2, v))         r = m2;       else         l = m1;     }     cout << fixed << setprecision(10) << f(l + (r - l) / 2.0, v);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int q = 1;     while (q--) {       solve();       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300005;   int n, tt, rt, ans, ls[N], rs[N], val[N], tg[N], rd[N];   inline int nw(int v) {     val[++tt] = v;     rd[tt] = rand();     return tt;   }   inline void add(int x, int v) {     val[x] += v;     tg[x] += v;   }   inline void dn(int x) {     add(ls[x], tg[x]);     add(rs[x], tg[x]);     tg[x] = 0;   }   void split(int x, int k, int& a, int& b) {     if (!x) {       a = b = 0;       return;     }     dn(x);     val[x] < k ? (a = x, split(rs[x], k, rs[a], b))                : (b = x, split(ls[x], k, a, ls[b]));   }   int merge(int x, int y) {     if (!x || !y)       return x | y;     else if (rd[x] < rd[y]) {       dn(x);       rs[x] = merge(rs[x], y);       return x;     } else {       dn(y);       ls[y] = merge(x, ls[y]);       return y;     }   }   int main() {     scanf( %d , &n);     while (n--) {       int l, r, x, y, z;       scanf( %d%d , &l, &r);       split(rt, r, x, z);       split(x, l, x, y);       add(y, 1);       if (z) {         int w = z;         while (ls[w]) dn(w), w = ls[w];         split(z, val[w] + 1, w, z);       } else         ans++;       rt = merge(merge(x, nw(l)), merge(y, z));     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6;   char buff[MAXN + 5];   string input, subs;   int freq[10];   int counter(int num) {     int ret = 0;     while (num) {       ret++;       num /= 10;     }     return ret;   }   string res;   bool flag;   void solve(bool taken) {     for (int i = 0; i <= 10; i++) {       if (!taken && (subs[0] -  0 ) < i) {         res += subs;         solve(1);         return;       } else if (i <= 9 && freq[i]) {         char c = (i +  0 );         res += c;         freq[i]--;         solve(taken);         freq[i]++;         return;       }     }     return;   }   void solve2(bool taken) {     for (int i = 0; i <= 10; i++) {       if (!taken && (subs[0] -  0 ) <= i) {         res += subs;         solve2(1);         return;       } else if (i <= 9 && freq[i]) {         char c = (i +  0 );         res += c;         freq[i]--;         solve2(taken);         freq[i]++;         return;       }     }     return;   }   int main() {     scanf( %s , buff);     input = buff;     scanf( %s , buff);     subs = buff;     if (input.size() == 2) {       if (input[0] ==  1 ) swap(input[0], input[1]);       printf( %c n , input[0]);       return 0;     }     int k;     string sk;     for (int i = 1; i <= MAXN; i++) {       if (i == input.size() - counter(i)) {         k = i;         break;       }     }     int temp = k;     while (temp) {       char c = ((temp % 10) +  0 );       sk = c + sk;       temp /= 10;     }     for (int i = 0; i < input.size(); i++) freq[input[i] -  0 ]++;     for (int i = 0; i < subs.size(); i++) freq[subs[i] -  0 ]--;     for (int i = 0; i < sk.size(); i++) freq[sk[i] -  0 ]--;     flag = 0;     char d = sk[0];     for (int i = 1; i < sk.size(); i++) {       if (sk[i] < d) {         flag = 1;         break;       } else if (sk[i] == d)         ;       else         break;     }     solve(1);     string res1 = subs + res;     res.clear();     for (int i = 1; i <= 9; i++) {       if (freq[i]) {         char c = (i +  0 );         res += c;         freq[i]--;         solve(0);         freq[i]++;         break;       }     }     string res2 = res;     res.clear();     for (int i = 1; i <= 9; i++) {       if (freq[i]) {         char c = (i +  0 );         res += c;         freq[i]--;         solve2(0);         freq[i]++;         break;       }     }     string res3 = res;     char c =  9  + 1;     if (res1[0] ==  0  || res1 ==   ) res1 =   , res1 += c;     if (res2[0] ==  0  || res2 ==   ) res2 =   , res2 += c;     if (res3[0] ==  0  || res3 ==   ) res3 =   , res3 += c;     if (res1 < res2 && res1 < res3)       printf( %s n , res1.c_str());     else if (res2 < res1 && res2 < res3)       printf( %s n , res2.c_str());     else       printf( %s n , res3.c_str());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void B();   int main() {     ios::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     B();   }   class segmen {    public:     long long x1, y1, x2, y2;     segmen(long long xx1, long long yy1, long long xx2, long long yy2)         : x1{xx1}, x2{xx2}, y1{yy1}, y2{yy2} {}     segmen() {}   };   vector<segmen> hor, ver;   const long long N = 10010;   const long long shift = 5001;   bool compar(segmen A, segmen B) { return A.y1 <= B.y1; }   long long T[N];   void upd(long long x, long long val) {     for (; x < N; x += (x & (-x))) T[x] += val;   }   long long F(long long x) {     long long ret = 0;     for (; x; x -= (x & (-x))) ret += T[x];     return ret;   }   long long gett(long long l, long long r) { return F(r) - F(l - 1); }   vector<pair<long long, long long> > H[N];   vector<pair<long long, long long> > V[N];   void B() {     long long n;     cin >> n;     for (long long i = 0; i < n; i++) {       long long x1, x2, y1, y2;       cin >> x1 >> y1 >> x2 >> y2;       x1 += shift;       x2 += shift;       y1 += shift;       y2 += shift;       if (y1 == y2) {         H[y1].push_back({min(x1, x2), max(x1, x2)});       }       if (x1 == x2) {         V[x1].push_back({min(y1, y2), max(y1, y2)});       }     }     long long ans = 0;     vector<long long> endy[N];     for (long long i = 1; i < N; i++) {       for (auto A : H[i]) {         long long xl = A.first;         long long xr = A.second;         long long maxn = 0;         for (long long j = xl; j <= xr; j++) {           for (auto B : V[j]) {             if (B.first <= i && B.second > i) {               upd(j, 1);               endy[B.second].push_back(j);               maxn = max(maxn, B.second);             }           }         }         for (long long j = i + 1; j <= maxn; j++) {           for (auto B : H[j]) {             long long ret = gett(B.first, B.second);             ans += (ret * (ret - 1)) >> 1;           }           for (auto B : endy[j]) {             upd(B, -1);           }           endy[j].clear();         }       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 50;   long long fac[N + 1];   void init() {     fac[0] = 1;     for (long long i = 1; i <= N; i++) {       fac[i] = (i * fac[i - 1]) % 1000000007LL;     }   }   long long pw(long long a, long long e) {     if (e == 0) {       return 1;     }     long long ret = pw(a, e / 2);     ret = (ret * ret) % 1000000007LL;     if (e % 2 == 1) {       ret = (a * ret) % 1000000007LL;     }     return ret;   }   long long choose(long long x, long long y, long long x0, long long y0) {     return (((((fac[x] * pw(fac[x0], 1000000007LL - 2)) % 1000000007LL) *               pw(fac[x - x0], 1000000007LL - 2)) %              1000000007LL) *             ((((fac[y] * pw(fac[y0], 1000000007LL - 2)) % 1000000007LL) *               pw(fac[y - y0], 1000000007LL - 2)) %              1000000007LL)) %            1000000007LL;   }   long long X, Y;   vector<pair<long long, long long> > adj[(N + 1) * (N + 1) * 2];   long long getVertex(long long x, long long y, long long sh) {     return sh * (X + 1) * (Y + 1) + x * (Y + 1) + y;   }   queue<long long> q;   long long dist[(N + 1) * (N + 1) * 2], ways[(N + 1) * (N + 1) * 2];   int main() {     init();     long long n, k;     cin >> n >> k;     while (n--) {       long long w;       cin >> w;       if (w == 50) {         X++;       } else {         Y++;       }     }     for (long long x = 0; x <= X; x++) {       for (long long y = 0; y <= Y; y++) {         for (long long x0 = 0; x0 <= x; x0++) {           for (long long y0 = 0; y0 <= y; y0++) {             if (x0 + y0 >= 1 && (50 * x0 + 100 * y0 <= k)) {               long long w = choose(x, y, x0, y0);               adj[getVertex(x, y, 0)].push_back(pair<long long, long long>(                   getVertex(X - x + x0, Y - y + y0, 1), w));               adj[getVertex(x, y, 1)].push_back(pair<long long, long long>(                   getVertex(X - x + x0, Y - y + y0, 0), w));             }           }         }       }     }     long long start = getVertex(X, Y, 0), end = getVertex(X, Y, 1);     for (long long u = 0; u <= (X + 1) * (Y + 1) * 2 - 1; u++) {       dist[u] = 1000000000000000000LL;     }     q.push(start);     dist[start] = 0;     ways[start] = 1;     while (!q.empty()) {       long long u = q.front();       q.pop();       for (pair<long long, long long> vw : adj[u]) {         long long v = vw.first, w = vw.second;         if (dist[u] + 1 < dist[v]) {           q.push(v);           dist[v] = dist[u] + 1;           ways[v] = (ways[u] * w) % 1000000007LL;         } else if (dist[u] + 1 == dist[v]) {           ways[v] = (ways[v] + ways[u] * w) % 1000000007LL;         }       }     }     cout << (dist[end] == 1000000000000000000LL ? -1 : dist[end]) <<   n           << ways[end] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     char a, b;     a = s[0];     b = s[s.length() - 1];     int j = s.length() - 2;     int i = 0;     int maxcount = 0;     while (j - i >= 0) {       int k1 = j - i;       int k = s[k1];       int q1 = s.length() - 1;       int q = s[q1];       int count = 0;       while (k1 > 0 && q == k) {         count++;         q1--;         q = s[q1];         k1--;         k = s[k1];       }       if (q == k) {         count++;       } else {         k1 = -1;       }       if (maxcount < count && k1 == 0) {         maxcount = count;       }       i++;     }     string x = s.substr(0, s.length() - maxcount);     string y = s.substr(s.length() - maxcount, s.length());     for (int i = 0; i < k; i++) {       cout << x;     }     cout << y;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, d, t, result, stored = 0;     cin >> n >> d;     for (int i = 0; i < n; i++) {       cin >> t;       stored += t;     }     d -= (n - 1) * 10;     if (d - stored >= 0) {       cout << (n - 1) * 2 + (d - stored) / 5;     } else       cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solution() {     long long n;     long long k;     cin >> n >> k;     vector<long long> v(n);     for (long long i = 0; i < n; i++) cin >> v[i];     long long sum = 0;     sort(v.begin(), v.end());     for (long long i = 0; i < (k); i++) {       sum += (v[n - k - 1 - i] / v[n - 1 - i]);     }     for (long long i = 0; i < n - 2 * k; i++) sum += v[i];     cout << sum << endl;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) solution();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Get() {     char c;     while (c = getchar(), (c <  0  || c >  9 ) && c !=  - )       ;     bool Flag = (c ==  - );     if (Flag) c = getchar();     int X = 0;     while (c >=  0  && c <=  9 ) {       X = X * 10 + c - 48;       c = getchar();     }     return Flag ? -X : X;   }   int main() {     int N = Get(), Sum = 0;     for (int i = 0; i < N; i++) Sum += Get();     printf( %d n , (Sum % N) ? (N - 1) : N);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 10;   namespace StrAlgo {   vector<int> Kmp(string s) {     int n = (int(s.size()));     vector<int> ans(n + 1);     for (int i = 2; i <= n; i++) {       ans[i] = ans[i - 1];       while (ans[i] > 0 && s[ans[i]] != s[i - 1]) ans[i] = ans[ans[i]];       ans[i] += s[ans[i]] == s[i - 1];     }     return ans;   }   vector<int> Z(string s) {     int n = (int(s.size()));     vector<int> ans(n + 1);     int L = -1, R = -1;     for (int i = 2; i <= n; i++) {       if (i < R)         ans[i] = min(ans[i - L], R - i);       else         L = i, R = i;       while (i + ans[i] <= n && s[i + ans[i] - 1] == s[ans[i] - 1]) ans[i]++;       if (i + ans[i] > R) L = i, R = i + ans[i];     }     return ans;   }   };  // namespace StrAlgo   void print(string s, int len) {     if (len == 0)       cout <<  Just a legend n ;     else       cout << s.substr((int(s.size())) - len) <<   n ;     exit(0);   }   int cnt[maxn];   int main() {     ios_base::sync_with_stdio(0), cin.tie(), cout.tie();     string s;     cin >> s;     vector<int> kmp = StrAlgo::Kmp(s);     for (int x : kmp) cnt[x]++;     if (cnt[kmp.back()] > 1) print(s, kmp.back());     print(s, kmp[kmp.back()]);   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const double EPS = 1e-9;   const long long MOD = 100000000;   const int N = 300009;   const int N2 = 300009;   void fast() { std::ios_base::sync_with_stdio(0); }   int main() {     long long n, m;     long long x = 0;     scanf( %lld%lld , &n, &m);     vector<long long> b(n), g(m);     for (long long &i : b) {       scanf( %lld , &i);       x += i * m;     }     for (long long &i : g) scanf( %lld , &i);     sort(b.rbegin(), b.rend());     sort(g.rbegin(), g.rend());     long long i = 0, cnt = 0;     long long ans = 0;     if (g[m - 1] < b[0] || b[0] > g[m - 1]) {       puts( -1 );       return 0;     }     for (long long j = 0; j < m; j++) {       if (cnt == m - 1) i++, cnt = 0;       if (g[j] > b[0]) {         ans += g[j] - b[i];         cnt++;       }     }     printf( %lld , ans + x);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 21;   int n;   long long s, a[N], down = 1, ans;   void exgcd(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1, y = 0;       return;     }     exgcd(b, a % b, y, x);     y -= a / b * x;   }   long long inv(long long x) {     long long res, y;     exgcd(x, 1000000007, res, y);     return res;   }   long long C(long long u, long long d) {     if (d < u) return 0;     long long res = 1;     for (long long i = d; i > d - u; i--) (res *= i % 1000000007) %= 1000000007;     return res * down % 1000000007;   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> s;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 1; i < n; i++) (down *= i) % 1000000007;     down = inv(down);     for (int i = 0; i < (1 << n); i++) {       long long f = 1, d = s + n - 1, u = n - 1;       for (int j = 0; j < n; j++)         if (i & (1 << j)) {           d -= a[j] + 1;           f *= -1;         }       (ans += f * C(u, d)) %= 1000000007;     }     cout << (ans + 1000000007) % 1000000007;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500001;   const int oo = 1e9;   const int LOGN = 20;   const int MOD = 1000000007;   const double PI = acos(-1);   int n, a[N];   int main() {     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     int mn = oo, idx = -1;     for (int t = 1; t <= 101; ++t) {       int cur = 0;       for (int j = 0; j < n; ++j) {         int cst = max(0, abs(a[j] - t) - 1);         cur += cst;       }       if (cur < mn) {         mn = cur;         idx = t;       }     }     cout << idx <<     << mn << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double EPS = 1e-6;   const int MOD = 1e9 + 7;   const int MAXN = 1100001;   const double PI = acos(-1.0);   long long ipt[MAXN];   long long lsum[MAXN], rsum[MAXN];   long long sum[MAXN];   int main() {     long long n, len;     while (cin >> n >> len) {       memset(sum, 0, sizeof(sum));       memset(lsum, 0, sizeof(lsum));       memset(rsum, 0, sizeof(rsum));       for (int i = 0; i < (n); ++i) cin >> ipt[i];       for (int i = (1); i < (n); ++i) {         sum[i] += sum[i - 1] + ipt[i] - ipt[i - 1];       }       long long group = 0;       for (long long i = 0; i + len < n; i += len) {         long long l = len * group, r = l + len;         if (group) lsum[group] = lsum[group - 1];         lsum[group] += (sum[r] - sum[l]) * (group + 1);         group++;       }       group = 0;       for (long long i = n - 1; i - len >= 0; i -= len) {         long long r = len * group, l = r + len;         l = n - 1 - l;         r = n - 1 - r;         if (group) rsum[group] = rsum[group - 1];         rsum[group] += (sum[r] - sum[l]) * (group + 1);         group++;       }       long long ans = 1e18;       for (long long i = 0; i < n; i++) {         long long t = 0;         if (i / len > 0) t += lsum[i / len - 1] * 2;         if ((n - 1 - i) / len > 0) t += rsum[(n - 1 - i) / len - 1] * 2;         t += (sum[i] - sum[i / len * len]) * 2 * (i / len + 1);         t += (sum[n - 1 - (n - 1 - i) / len * len] - sum[i]) * 2 *              ((n - 1 - i) / len + 1);         ans = min(ans, t);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using cat = long long;   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     cout << fixed << setprecision(10);     int N, MX = 500000;     cin >> N;     vector<cat> A(N);     for (int i = 0; i < N; i++) cin >> A[i];     sort(begin(A), end(A));     vector<int> cnt(MX + 1, 0), cnt_ps(MX + 1, 0);     for (int i = 0; i < N; i++) cnt[A[i]]++;     for (int i = 0; i <= MX; i++) cnt_ps[i] = (i ? cnt_ps[i - 1] : 0) + cnt[i];     cat ans = 0;     for (int y = 2; y <= MX; y++) {       cat cnt_y = 0;       for (int i = 1; i <= MX / y; i++)         cnt_y += 1LL * (cnt_ps[min(MX, y * i + y - 1)] - cnt_ps[y * i - 1]) * i;       if (cnt_y < 2) continue;       int min_x = min(1LL * y, cnt_y - 2), max_x = min(MX + 1LL, cnt_y + 1);       while (max_x - min_x > 1 && 1LL * (max_x - 1) * y > ans) {         int x = (min_x + max_x) / 2;         if (2 * (x / y) + x > cnt_y) {           max_x = x;           continue;         }         if (2 * x <= MX) {           int cnt2_r = 0;           for (int i = (2 * x) / y; i <= MX / y; i++)             cnt2_r += cnt_ps[min(MX, y * i + y - 1)] -                       cnt_ps[min(MX, max(0, y * i + (2 * x) % y - 1))];           if (cnt2_r >= 1 && x + (2 * x) / y <= cnt_y) {             min_x = x;             continue;           }           if (cnt_ps[MX] > cnt_ps[2 * x - 1] &&               x + (2 * x + y - 1) / y <= cnt_y) {             min_x = x;             continue;           }         }         int cnt_r = 0;         for (int i = x / y; i <= MX / y; i++)           cnt_r += cnt_ps[min(MX, y * i + y - 1)] -                    cnt_ps[min(MX, max(0, y * i + x % y - 1))];         if (cnt_r >= 2) {           min_x = x;           continue;         }         if (cnt_ps[MX] >= 2 + cnt_ps[x - 1] &&             x + 2 * ((x + y - 1) / y) - cnt_r <= cnt_y) {           min_x = x;           continue;         }         max_x = x;       }       if (min_x >= 2) ans = max(ans, 1LL * min_x * y);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e4;   int main() {     ios::sync_with_stdio(false);     int n;     while (cin >> n) {       int a[12];       for (int i = 0; i < n; ++i) cin >> a[i];       sort(a, a + n);       long long p = clock();       while (clock() - p < 1400)         ;       cout << a[0];       for (int i = 1; i < n; ++i) cout <<     << a[i];       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class DTrie {     int N = 30;     struct node {       node* left = nullptr;       node* right = nullptr;       int cnt = 0;     };     void _add(node* cur, int a, int p) {       cur->cnt++;       if (p == -1) return;       if (a & (1 << p)) {         if (cur->right == nullptr) cur->right = new node();         _add(cur->right, a, p - 1);       } else {         if (cur->left == nullptr) cur->left = new node();         _add(cur->left, a, p - 1);       }     }     int _get(node* cur, int a, int p) {       cur->cnt--;       if (p == -1) return 0;       int result = 0;       if (a & (1 << p)) {         if (cur->right == nullptr || cur->right->cnt == 0)           result = _get(cur->left, a, p - 1);         else           result = (1 << p) + _get(cur->right, a, p - 1);       } else {         if (cur->left == nullptr || cur->left->cnt == 0)           result = (1 << p) + _get(cur->right, a, p - 1);         else           result = _get(cur->left, a, p - 1);       }       return result;     }     node* root = nullptr;       public:     DTrie() { root = new node(); }     void add(int a) { _add(root, a, N); }     int get(int a) { return _get(root, a, N); }   };   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     vector<int> a(n), p(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 0; i < n; i++) {       cin >> p[i];     }     DTrie dt;     for (int i = 0; i < n; i++) {       dt.add(p[i]);     }     for (int i = 0; i < n; i++) {       cout << (a[i] ^ dt.get(a[i])) <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int s, tens, sum = 0;       cin >> s;       for (; s > 0;) {         tens = s / 10;         s -= tens * 10;         s += tens;         sum += tens * 10;         if (s >= 10 && s <= 18) {           s -= 10;           s++;           sum += 10;           if (s) {             sum += s;             s = 0;           }         } else if (s <= 9) {           sum += s;           s = 0;         }       }       cout << sum << endl;     }     return 0;   }


#include <bits/stdc++.h>   using std::map;   using std::max;   using std::min;   using std::set;   using std::sort;   using std::swap;   using std::vector;   inline int readInt() {     int ans = 0;     char c;     do c = getchar();     while (!isdigit(c));     while (isdigit(c)) {       ans = ans * 10 + c -  0 ;       c = getchar();     }     return ans;   }   const int N = 55;   int A[N][N];   int main() {     int n = readInt();     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) A[i][j] = readInt();     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j)         if (A[i][j] != 1) {           bool ok = false;           for (int k = 0; k < n; ++k)             if (i != k) {               for (int l = 0; l < n; ++l)                 if (j != l && A[i][j] == A[k][j] + A[i][l]) {                   ok = true;                   break;                 }               if (ok) break;             }           if (!ok) return puts( NO ), 0;         }     return puts( YES ), 0;   }


#include <bits/stdc++.h>   const int N = 4e6 + 10;   int idx[N], rank[N];   struct Point {     int x, y;   } A[N], Line[N];   inline double slope(Point a) {     return double(A[a.x].y - A[a.y].y) / double(A[a.x].x - A[a.y].x);   }   inline long long area(Point A, Point B, Point C) {     return fabs(1LL * (A.x - C.x) * (B.y - C.y) -                 1LL * (B.x - C.x) * (A.y - C.y));   }   inline int find1(int l, int r, int u, int v, long long s) {     int res = -1;     while (l <= r) {       int mid = (l + r) >> 1;       if (area(A[idx[mid]], A[u], A[v]) >= s)         l = mid + 1, res = mid;       else         r = mid - 1;     }     return res != -1 && area(A[idx[res]], A[u], A[v]) == s ? res : -1;   }   inline int find2(int l, int r, int u, int v, long long s) {     int res = -1;     while (l <= r) {       int mid = (l + r) >> 1;       if (area(A[idx[mid]], A[u], A[v]) <= s)         l = mid + 1, res = mid;       else         r = mid - 1;     }     return res != -1 && area(A[idx[res]], A[u], A[v]) == s ? res : -1;   }   inline int solve(int u, int v, int n, long long s) {     if (rank[u] > rank[v]) std::swap(u, v);     int res1 = find1(1, rank[u] - 1, u, v, s);     if (res1 != -1) return res1;     int res2 = find2(rank[v] + 1, n, u, v, s);     if (res2 != -1) return res2;     std::swap(rank[u], rank[v]), std::swap(idx[rank[u]], idx[rank[v]]);     return 0;   }   int main() {     int n, tot = 0;     long long s;     std::cin >> n >> s;     s <<= 1;     for (int i = 1; i <= n; ++i)       std::cin >> A[i].x >> A[i].y, idx[i] = i, rank[i] = i;     std::sort(A + 1, A + n + 1, [&](const Point &a, const Point &b) {       return a.x < b.x || a.x == b.x && a.y < b.y;     });     for (int i = 1; i <= n; ++i)       for (int j = i + 1; j <= n; ++j) Line[++tot] = Point{i, j};     std::sort(Line + 1, Line + tot + 1, [&](const Point &a, const Point &b) {       int x1 = A[a.y].x - A[a.x].x, y1 = A[a.y].y - A[a.x].y;       int x2 = A[b.y].x - A[b.x].x, y2 = A[b.y].y - A[b.x].y;       return 1LL * x1 * y2 - 1LL * x2 * y1 > 0;     });     for (int i = 1; i <= tot; ++i) {       int res = solve(Line[i].x, Line[i].y, n, s);       if (res)         return printf( Yes n%d %d n%d %d n%d %d n , (A[Line[i].x]).x,                       (A[Line[i].x]).y, (A[Line[i].y]).x, (A[Line[i].y]).y,                       (A[idx[res]]).x, (A[idx[res]]).y),                0;     }     return puts( No ), 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   pair<int, int> Result[100005];   bool Use[100005];   int counter, Array[100005];   void buildArray(int prime) {     Array[0] = 0;     for (int k = prime; k <= N; k += prime) {       if (Use[k] == 0) {         Array[++Array[0]] = k;         Use[k] = 1;       }     }   }   void makeArrayValid() {     if (Array[0] % 2 == 0) return;     Use[Array[2]] = 0;     swap(Array[Array[0]], Array[2]);     Array[0]--;   }   void addToResult() {     int i;     for (i = 1; i <= Array[0]; i += 2) {       Result[++counter].first = Array[i];       Result[counter].second = Array[i + 1];     }   }   void Eratostenes() {     int i, j;     int last = 3;     for (i = 3; i <= N / 2; i += 2) {       if (Use[i] == 1) continue;       buildArray(i);       makeArrayValid();       addToResult();     }     buildArray(2);     makeArrayValid();     addToResult();   }   int main() {     cin >> N;     Eratostenes();     cout << counter <<   n ;     for (int i = 1; i <= counter; i++)       cout << Result[i].first <<     << Result[i].second <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<long long> a(n);     long long i, j;     for (i = 0; i < n; i++) cin >> a[i];     sort(a.begin(), a.end());     for (i = 0; i < n; i++) {       for (j = i + 1; j < n; j++) {         if (a[i] == 0) continue;         if (a[j] % a[i] == 0) {           a[j] = 0;         }       }     }     long long cnt = 0;     for (i = 0; i < n; i++)       if (a[i] > 0) cnt++;     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct point {     double x, y;     point(double x = 0, double y = 0) : x(x), y(y) {}     point operator-(const point &b) const { return point(x - b.x, y - b.y); }     double operator*(const point &b) const { return x * b.y - y * b.x; }   };   point p[200050];   int a[200050], q[200050];   double sum[200050], t[200050], E[200050], dp[55][200050];   bool check(int i, int j, double v) {     return p[j].y - p[i].y <= (p[i].x - p[j].x) * v;   }   int main() {     int n, k;     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) {       sum[i] = sum[i - 1] + a[i];       t[i] = t[i - 1] + 1.0 / a[i];       E[i] = E[i - 1] + sum[i] / a[i];     }     for (int i = 1; i <= n; ++i) dp[1][i] = E[i];     for (int x = 2; x <= k; ++x) {       int head = 0, tail = 0;       q[tail++] = 0;       for (int i = 1; i <= n; ++i) {         if (i < x) {           p[i] = point(-sum[i], sum[i] * t[i] - E[i] + dp[x - 1][i]);           while (head + 1 < tail &&                  (p[q[tail - 1]] - p[q[tail - 2]]) * (p[i] - p[q[tail - 2]]) >= 0)             tail--;           q[tail++] = i;         }         while (head + 1 < tail && check(q[head], q[head + 1], t[i])) head++;         int j = q[head];         dp[x][i] = p[j].x * t[i] + p[j].y + E[i];         p[i] = point(-sum[i], sum[i] * t[i] - E[i] + dp[x - 1][i]);         while (head + 1 < tail &&                (p[q[tail - 1]] - p[q[tail - 2]]) * (p[i] - p[q[tail - 2]]) >= 0)           tail--;         q[tail++] = i;       }     }     printf( %.9f n , dp[k][n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e9;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int a[n + 5];     long long ans = 0;     for (int i = 1; i <= n; i++) {       cin >> a[i];       ans += a[i] * a[i];     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n / 2; i++) {       ans += 2 * a[i] * a[n - i + 1];     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int maxn = (1 << 22) + 5;   bool vis[maxn << 2][2];   int n, m;   int a[maxn];   bool p[maxn];   void dfs(int id, int s) {     if (vis[id][s]) return;     vis[id][s] = 1;     for (int i = 0; i < n; i++) {       if (id & (1 << i)) {         dfs(id ^ (1 << i), s);       }     }     if (p[id]) {       if (vis[id][0]) return;       vis[id][0] = 1;       dfs((1 << n) - 1 - id, 1);     }   }   int main() {     scanf( %d%d , &n, &m);     memset(vis, 0, sizeof(vis));     memset(p, 0, sizeof(p));     for (int i = 1; i <= m; i++) {       int x;       scanf( %d , a + i);       p[a[i]] = 1;     }     int ans = 0;     for (int i = 1; i <= m; i++) {       if (!vis[a[i]][0]) {         vis[a[i]][0] = 1;         ans++;         dfs((1 << n) - 1 - a[i], 1);       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   int main(int argc, char* argv[]) {     int t, cups = 0, medals = 0, n, shelives_needed = 0;     for (int i = 0; i < 3; i++) {       scanf( %d , &t);       cups += t;     }     for (int i = 0; i < 3; i++) {       scanf( %d , &t);       medals += t;     }     scanf( %d , &n);     if (cups % 5 != 0) {       shelives_needed += cups / 5 + 1;     } else {       shelives_needed += cups / 5;     }     if (medals % 10 != 0) {       shelives_needed += medals / 10 + 1;     } else {       shelives_needed += medals / 10;     }     if (shelives_needed > n) {       printf( NO n );     } else {       printf( YES n );     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx )   using namespace std;   int a, b, c, d, n, m, k;   long long pc[1000001];   long long dp[7][333335];   int f[6];   const int N = 333333;   const long long infll = (long long)1000000000 * 1000000000;   inline void upd(long long& a, long long b) {     if (a < b) a = b;   }   long long solve(int n) {     long long ans = pc[n];     for (int _n((n)-1), i(0); i <= _n; i++) {       int rem = n - i;       if (rem % 3) continue;       ans = max(ans, pc[i] + dp[5][rem / 3]);     }     return ans;   }   int main() {     scanf( %d , &k);     --k;     for (int _n((6) - 1), i(0); i <= _n; i++) scanf( %d , &f[i]);     pc[0] = 0;     for (int _n((999999)), i(1); i <= _n; i++) {       int t = i;       long long a = 0;       for (int _n((6) - 1), j(0); j <= _n; j++) {         int dig = t % 10;         if (dig == 3 || dig == 6 || dig == 9) {           a += (long long)f[j] * (dig / 3);         }         t /= 10;       }       pc[i] = a;     }     for (int _n((7) - 1), i(0); i <= _n; i++)       for (int _n((N + 2) - 1), j(0); j <= _n; j++) dp[i][j] = -8 * infll;     for (int _n((N + 1) - 1), i(0); i <= _n; i++) {       if (i > 3 * k) break;       dp[0][i] = (long long)f[0] * i;     }     auto ev1 = [&](int i, int s) {       return (long long)i * (f[1] - 10LL * f[0]) + (long long)s * f[0];     };     for (int _n((N + 1) - 1), i(0); i <= _n; i++) {       if (i > 33LL * k) break;       int l = 0;       if (i > 3 * k) {         int rem = i - 3 * k;         l = rem / 10;         if (rem % 10) ++l;       }       int r = min(3 * k, i / 10);       if (l > r) continue;       dp[1][i] = max(ev1(l, i), ev1(r, i));     }     for (int _n(5), d(2); d <= _n; d++) {       long long o = 0;       for (int _n((d + 1) - 1), z(0); z <= _n; z++) o = o * 10 + 3;       o *= k;       o = min<long long>(o, N);       int t = 1;       for (int _n((d)-1), j(0); j <= _n; j++) t *= 10;       for (int _n((o + 1) - 1), i(0); i <= _n; i++) {         int r = min(3 * k, i / t);         long long cd = 0;         for (int _n((r + 1) - 1), j(0); j <= _n; j++) {           upd(cd, dp[d - 1][i - j * t] + (long long)f[d] * j);         }         dp[d][i] = cd;       }     }     scanf( %d , &m);     for (int _n((m)), hod(1); hod <= _n; hod++) {       scanf( %d , &n);       long long ans = solve(n);       printf( %I64d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   bool vis[10000001];   int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }   int lcm(int a, int b) { return ((a * b) / gcd(a, b)); }   int main() {     int n;     scanf( %d , &n);     int sum = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         int a;         scanf( %d , &a);         if (i == j) sum += a;       }     }     sum %= 2;     int q;     cin >> q;     for (int i = 0; i < q; i++) {       int t;       scanf( %d , &t);       if (t == 3)         cout << sum;       else {         int y;         scanf( %d , &y);         sum = 1 - sum;       }     }   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > re;   vector<pair<int, int> > e, o;   int n, c[404][404], sink, Find, pre[404], flow;   bool ch[100009], visit[404];   void init() {     for (int i = 2; i * i <= 100000; i++) {       if (!ch[i]) {         for (int j = i * i; j <= 100000; j += i) ch[j] = 1;       }     }   }   void dfs(int cur) {     visit[cur] = 1;     if (cur == sink) Find = 1;     if (Find) return;     for (int i = 0; i <= sink && !Find; i++) {       if (c[cur][i] > 0 && !visit[i]) {         dfs(i);         pre[i] = cur;       }     }   }   void solve() {     init();     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int a;       scanf( %d , &a);       if (a % 2)         o.push_back(pair<int, int>(a, i + 1));       else         e.push_back(pair<int, int>(a, i + 1));     }     for (int i = 0; i < o.size(); i++) {       c[0][i + 1] = 2;     }     sink = e.size() + o.size() + 1;     for (int i = 0; i < e.size(); i++) {       c[o.size() + i + 1][sink] = 2;     }     for (int i = 0; i < o.size(); i++) {       for (int j = 0; j < e.size(); j++) {         int t = o[i].first + e[j].first;         if (!ch[t]) c[i + 1][o.size() + j + 1] = 1;       }     }     for (;;) {       memset(visit, 0, sizeof(visit));       Find = 0;       dfs(0);       if (!Find) break;       flow++;       int st, en;       en = sink;       while (en) {         st = pre[en];         c[st][en]--;         c[en][st]++;         en = st;       }     }     if (flow != 2 * e.size()) {       puts( Impossible );       return;     }     memset(visit, 0, sizeof(visit));     for (int i = 1; i < sink; i++) {       if (visit[i]) continue;       vector<int> tmp;       int cur = i;       while (!visit[cur]) {         visit[cur] = 1;         if (cur <= o.size())           tmp.push_back(o[cur - 1].second);         else           tmp.push_back(e[cur - o.size() - 1].second);         for (int i = 1; i < sink; i++) {           if (!visit[i] && ((cur <= o.size() && c[i][cur] > 0) ||                             cur > o.size() && c[cur][i] > 0)) {             cur = i;             break;           }         }       }       re.push_back(tmp);     }     printf( %d n , re.size());     for (int i = 0; i < re.size(); i++) {       printf( %d  , re[i].size());       for (int j = 0; j < re[i].size(); j++) printf( %d  , re[i][j]);       puts(  );     }   }   int main(void) {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N;     cin >> N;     int max = INT_MIN, min = INT_MAX;     for (int i = 0; i < N; ++i) {       int x;       cin >> x;       if (x > max) max = x;       if (x < min) min = x;     }     cout << max - min + 1 - N << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b[1001], c, cnt1, cnt2, d, n, m;   string s;   int main() {     cin >> a;     if (a == 1) {       cout << 1;       return 0;     }     for (int i = 1; i <= a; i++) {       cin >> b[i];       c += b[i];     }     sort(b + 1, b + a + 1);     reverse(b + 1, b + a + 1);     for (int i = 1; i <= a; i++) {       d = (c - b[i]);       n += c - d;       cnt1++;       if (n > d) {         cout << cnt1;         return 0;       }       c = d;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 20;   const int MAXX = 65536;   int n, m;   long long year;   vector<int> graph[MAXN], rev_graph[MAXN];   vector<int> sol;   long long dp[MAXX];   long long countPermutations(int mask, int pos) {     dp[0] = 1;     for (int i = 1; i < 1 << (n - pos); ++i) {       dp[i] = 0;     }     for (int i = 0; i < 1 << (n - pos); ++i) {       int value = n - 1;       while (value >= 0 && (mask & (1 << value))) --value;       for (int j = 0; j < n - pos; ++j)         if (i & (1 << j)) {           do {             --value;           } while (value >= 0 && (mask & (1 << value)));         }       for (int j = 0; j < n - pos; ++j)         if ((i & (1 << j)) == 0) {           int ok = 1;           for (size_t k = 0; k < graph[j + pos].size() && ok; ++k)             if ((graph[j + pos][k] >= pos &&                  (i & (1 << (graph[j + pos][k] - pos))) == 0) ||                 (graph[j + pos][k] < pos && sol[graph[j + pos][k]] < value)) {               ok = 0;             }           for (size_t k = 0; k < rev_graph[j + pos].size() && ok; ++k)             if (rev_graph[j + pos][k] < pos && sol[rev_graph[j + pos][k]] > value)               ok = 0;           if (ok) dp[i ^ (1 << j)] += dp[i];         }     }     return dp[(1 << (n - pos)) - 1];   }   void findPermutation(int mask, int pos) {     if (pos < n) {       for (int i = 0; i < n; ++i)         if ((mask & (1 << i)) == 0) {           int ok = 1;           for (size_t j = 0; j < graph[pos].size() && ok; ++j)             if (graph[pos][j] < pos && sol[graph[pos][j]] < i) ok = 0;           for (size_t j = 0; j < rev_graph[pos].size() && ok; ++j)             if (rev_graph[pos][j] < pos && sol[rev_graph[pos][j]] > i) ok = 0;           if (!ok) {             continue;           }           mask ^= 1 << i;           sol.push_back(i);           long long cnt = countPermutations(mask, pos + 1);           if (cnt >= year) {             findPermutation(mask, pos + 1);             break;           }           year -= cnt;           mask ^= 1 << i;           sol.pop_back();         }     }   }   int main() {     cin >> n >> year >> m;     year -= 2000;     for (int i = 1; i <= m; ++i) {       int x, y;       cin >> x >> y;       --x, --y;       graph[x].push_back(y);       rev_graph[y].push_back(x);     }     findPermutation(0, 0);     if ((int)sol.size() != n) {       cout <<  The times have changed n ;       return 0;     }     for (int i = 0; i < n - 1; ++i) cout << sol[i] + 1 <<    ;     cout << sol[n - 1] + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     long long m = 0, a = 1, b = 1, ans = 0;     while (m <= n) {       m = a + b;       a = b;       b = m;       ans++;     }     cout << ans - 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 1e5;   const int Inf = 1 << 30;   double a[105];   int main() {     int n, cnt;     double pos, pp, ans, sum;     while (~scanf( %d , &n)) {       sum = 0;       pos = 4.5;       pos *= n;       for (int i = 1; i <= n; i++) {         scanf( %lf , &a[i]);         sum += a[i];       }       cnt = 0;       sort(a + 1, a + 1 + n);       while (sum < pos) {         cnt++;         sum += 5 - a[cnt];       }       printf( %d n , cnt);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> tree[300003];   int colors[300003];   int blueC[300003];   int redC[300003];   int ans = 0;   pair<int, int> dfs(int nd, int pr) {     int blue = (colors[nd] == 2);     int red = (colors[nd] == 1);     for (int i = 0; i < (int)tree[nd].size(); i++) {       if (tree[nd][i] == pr) continue;       pair<int, int> sub = dfs(tree[nd][i], nd);       if ((bool)sub.first ^ (bool)sub.second) {         if (sub.first)           ans += (blueC[0] == sub.first);         else           ans += (redC[0] == sub.second);       }       blue += sub.first;       red += sub.second;     }     return make_pair(blue, red);   }   void solve() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &colors[i]);     int u, v;     for (int i = 1; i < n; i++) {       scanf( %d %d , &u, &v);       tree[u].push_back(v);       tree[v].push_back(u);     }     memset(blueC, 0, sizeof(blueC));     memset(redC, 0, sizeof(redC));     for (int i = 1; i <= n; i++) {       if (colors[i] == 1)         redC[0]++;       else if (colors[i] == 2)         blueC[0]++;     }     dfs(1, 0);     printf( %d , ans);   }   int main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   int n, p[50], sz[50], v[50], id[50], ct, lg[(1 << 24)], bc[(1 << 24)];   unsigned int f[(1 << 24)], av[50];   char s[50][50];   vector<int> vc[50];   int fd(int x) { return p[x] == x ? x : p[x] = fd(p[x]); }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) p[i] = i;     for (int i = 1; i <= n; i++) {       scanf( %s , s[i] + 1);       for (int j = 1; j <= n; j++)         if (s[i][j] ==  A ) {           int xx = fd(i), yy = fd(j);           p[xx] = yy;         }     }     for (int i = 1; i <= n; i++) {       int xx = fd(i);       sz[xx]++;       vc[xx].push_back(i);     }     for (int i = 1; i <= n; i++)       if (sz[i] > 1 && p[i] == i) {         id[i] = ct++;         for (auto x : vc[i])           for (auto y : vc[i])             if (s[x][y] ==  X ) {               printf( -1 n );               return 0;             }       }     for (int i = 0; i < ct; i++) bc[1 << i] = i;     if (!ct) {       printf( %d , n - 1);       return 0;     }     for (int i = 1; i <= n; i++) {       if (p[i] == i && sz[i] == 1) continue;       for (int j = 1; j <= n; j++) {         if (p[j] == j && sz[j] == 1) continue;         if (s[i][j] ==  X ) {           int xx = fd(i), yy = fd(j);           v[id[xx]] |= (1 << id[yy]);           v[id[yy]] |= (1 << id[xx]);         }       }     }     f[0] = 1;     for (int i = 1; i < (1 << ct); i++) {       int lb = i & -i;       lg[i] = lg[i - lb] + 1;       f[i] = f[i - lb] + f[(i - lb) ^ (i & v[bc[lb]])];     }     for (int i = 0; i < (1 << ct); i++) f[i]--;     for (int i = 0; i < (1 << ct); i++) {       unsigned int r = 1;       for (int j = 1; j <= ct; j++) {         r *= f[i];         if ((ct - lg[i]) & 1)           av[j] -= r;         else           av[j] += r;       }     }     for (int j = 1; j <= ct; j++)       if (av[j]) {         printf( %d , j + n - 1);         return 0;       }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int MAX = 1123;   int ans[MAX];   int main() {     int n;     cin >> n;     memset(ans, INF, sizeof(ans));     for (int i = 0; i < 10; i++) {       vector<int> data1, data2;       for (int j = 1; j <= n; j++) {         if (j & (1 << i))           data1.push_back(j);         else           data2.push_back(j);       }       if (data1.size()) {         cout << data1.size() << endl;         for (int j = 0; j < data1.size(); j++) {           cout << data1[j] <<    ;         }         for (int j = 1; j <= n; j++) {           int t;           cin >> t;           if ((j & (1 << i)) == 0) {             ans[j] = min(ans[j], t);           }         }         cout << endl;         fflush(stdout);       }       if (data2.size()) {         cout << data2.size() << endl;         for (int j = 0; j < data2.size(); j++) {           cout << data2[j] <<    ;         }         for (int j = 1; j <= n; j++) {           int t;           cin >> t;           if (j & (1 << i)) ans[j] = min(ans[j], t);         }         cout << endl;         fflush(stdout);       }     }     fflush(stdout);     cout << -1 << endl;     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   vector<int> adj[100005];   int din[100005];   queue<int> que;   int Vec[100005], V_size;   int a, b, c, d, cur;   bool succ;   int main() {     int Test;     scanf( %d , &Test);     while (Test--) {       scanf( %d%d%d , &n, &m, &k);       for (int i = 1; i <= n; i += 1) {         adj[i].clear();         din[i] = 0;       }       while (!que.empty()) que.pop();       for (int i = 1; i <= m; i += 1) {         scanf( %d%d , &a, &b);         adj[a].push_back(b), adj[b].push_back(a);         din[a]++, din[b]++;       }       if (k == 1) {         puts( 2 );         puts( 1 );         continue;       }       if (k > ((int)sqrt((double)2 * m) + 2)) {         puts( -1 );         continue;       }       for (int i = 1; i <= n; i += 1) sort(adj[i].begin(), adj[i].end());       for (int i = 1; i <= n; i += 1)         if (din[i] < (k - 1)) que.push(i);       while (!que.empty()) {         cur = que.front();         que.pop();         for (auto& i : adj[cur]) {           if (din[i] <= 0) continue;           din[cur]--, din[i]--;           if (din[i] == (k - 2)) que.push(i);         }         if (din[cur] != 0) {           cout <<  fds ;           return 0;         }       }       for (int i = 1; i <= n; i += 1)         if (din[i] == (k - 1)) que.push(i);       bool nex_loop = false;       while (!que.empty()) {         cur = que.front();         que.pop();         if (din[cur] == (k - 1)) {           V_size = 0;           for (auto& i : adj[cur]) {             if (din[i] <= 0) continue;             Vec[V_size++] = i;           }           succ = true;           for (int i = 0; i <= V_size - 1; i += 1) {             for (int j = i + 1; j <= V_size - 1; j += 1) {               a = Vec[i], b = Vec[j];               auto it = lower_bound(adj[a].begin(), adj[a].end(), b);               if ((it == adj[a].end()) || (*it != b)) {                 succ = false;                 break;               }             }             if (!succ) break;           }           if (succ) {             puts( 2 );             printf( %d  , cur);             for (int i = 0; i <= V_size - 1; i += 1) printf( %d  , Vec[i]);             printf(  n );             nex_loop = true;             break;           }         }         for (auto& i : adj[cur]) {           if (din[i] <= 0) continue;           din[cur]--, din[i]--;           if (din[i] == (k - 1)) que.push(i);         }       }       if (nex_loop) continue;       V_size = 0;       for (int i = 1; i <= n; i += 1) {         if (din[i] <= 0) continue;         Vec[V_size++] = i;       }       if (V_size == 0)         puts( -1 );       else {         printf( 1 %d n , V_size);         for (int i = 0; i <= V_size - 1; i += 1) {           printf( %d  , Vec[i]);         }         printf(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     int n, num, ctr;     string s;     while (t--) {       cin >> n >> s;       num = 0;       ctr = 0;       for (char ch : s) {         if (ch ==  ) ) {           num--;           if (num < 0) ctr++, num++;         } else           num++;       }       cout << ctr <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007LL;   const long long MXSZ = 1000000LL;   long long fpow(long long b, long long e) {     long long ret = 1;     while (e > 0) {       if (e & 1) (ret *= b) %= mod;       (b *= b) %= mod;       e >>= 1;     }     return ret;   }   long long fac[MXSZ + 5];   void prec() {     fac[0] = 1;     for (long long i = 1; i <= MXSZ; i++) {       fac[i] = fac[i - 1] * i;       fac[i] %= mod;     }   }   long long inv(long long u) { return fpow(fac[u], mod - 2); }   inline long long comb(long long n, long long k) {     long long ret = fac[n];     (ret *= inv(k)) %= mod;     (ret *= inv(n - k)) %= mod;     return ret;   }   inline long long perm(long long n, long long k) {     long long ret = fac[n];     (ret *= inv(n - k)) %= mod;     return ret;   }   long long n, m, a, b;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     prec();     cin >> n >> m >> a >> b;     long long tot = 0LL;     for (int btwn = 0; btwn < n - 2; btwn++) {       if (m < btwn + 1) break;       long long vleft = n - btwn - 2;       long long k = btwn + 2;       long long curr = comb(m - 1, btwn) * perm(n - 2, btwn);       curr %= mod;       curr *= k;       curr %= mod;       curr *= fpow(n, n - k - 1);       curr %= mod;       curr *= fpow(m, vleft);       curr %= mod;       tot += curr;       tot %= mod;     }     if (m - 1 >= n - 2) {       long long curr = comb(m - 1, n - 2) * perm(n - 2, n - 2);       curr %= mod;       tot += curr;       tot %= mod;     }     cout << tot <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long dist[200001] = {0};   int n, ans[200001], Val[200001], lg, tree[200001][20];   vector<vector<pair<int, long long> > > G(200001);   void Build(int v, int par = 0) {     tree[v][0] = par;     for (int i = 1; i <= lg; i++) tree[v][i] = tree[tree[v][i - 1]][i - 1];     for (int i = 0; i < G[v].size(); i++) {       dist[G[v][i].first] = dist[v] + G[v][i].second;       Build(G[v][i].first, v);     }   }   int FinalDfs(int v) {     for (int i = 0; i < G[v].size(); i++) ans[v] += FinalDfs(G[v][i].first) + 1;     return ans[v];   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &Val[i]);     while ((1 << lg) <= n) lg++;     lg--;     for (int i = 2; i <= n; i++) {       long long dis;       int par;       scanf( %d%I64d , &par, &dis);       G[par].push_back(make_pair(i, dis));     }     Build(1);     for (int i = 1; i <= n; i++) {       if (dist[i] <= Val[i]) continue;       int Pos = i;       int Value = Val[i];       for (int j = lg; j >= 0; j--)         if (dist[Pos] - dist[tree[Pos][j]] <= Value && tree[Pos][j] != 0) {           Value -= dist[Pos] - dist[tree[Pos][j]];           Pos = tree[Pos][j];         }       ans[tree[Pos][0]]--;     }     FinalDfs(1);     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string A, B;   int used[10][2];   int memo[10][2];   int m;   int dp(int pos, int mayor) {     if (pos == m) return mayor;     if (used[pos][mayor]) return memo[pos][mayor];     used[pos][mayor] = 1;     int &dev = memo[pos][mayor] = 0;     if (B[pos] ==  ? ) {       if (mayor) {         if (pos == 0) {           dev = dp(pos + 1, 1);         } else {           dev = dp(pos + 1, 1);         }       } else {         for (int c = A[pos]; c <=  9 ; ++c) {           if (pos == 0 && c ==  0 ) continue;           dev |= dp(pos + 1, c > A[pos]);         }       }     } else {       if (!mayor && B[pos] < A[pos])         return 0;       else         dev = dp(pos + 1, mayor ? 1 : (B[pos] > A[pos]));     }     return dev;   }   string rec(int pos, int mayor) {     if (pos == m) return   ;     if (B[pos] ==  ? ) {       if (mayor) {         if (pos == 0) {           return string(1,  1 ) + rec(pos + 1, 1);         } else {           return string(1,  0 ) + rec(pos + 1, 1);         }       } else {         for (int c = A[pos]; c <=  9 ; ++c) {           if (pos == 0 && c ==  0 ) continue;           if (dp(pos + 1, c > A[pos]))             return string(1, c) + rec(pos + 1, c > A[pos]);         }       }     } else {       return string(1, B[pos]) + rec(pos + 1, mayor ? 1 : (B[pos] > A[pos]));     }   }   int toi(string &s) {     istringstream in(s);     int x;     in >> x;     return x;   }   string tos(int x) {     ostringstream out;     out << x;     return out.str();   }   string S[100000 + 5];   void fix(string &s) {     for (int i = 0; i < ((int)(s).size()); ++i) {       if (s[i] ==  ? ) {         if (i == 0)           s[i] =  1 ;         else           s[i] =  0 ;       }     }   }   bool f(string &s, string &t) {     if (((int)(s).size()) > ((int)(t).size())) return 0;     for (int i = 0; i < ((int)(s).size()); ++i)       if (s[i] ==  ? ) return 0;     if (((int)(s).size()) < ((int)(t).size())) {       fix(t);       return 1;     }     memset(used, 0, sizeof(used));     m = ((int)(s).size());     A = s, B = t;     if (!dp(0, 0)) return 0;     string C = rec(0, 0);     t = C;     return 1;   }   int main() {     ios_base ::sync_with_stdio(0);     int n;     while (cin >> n) {       S[0] =   ;       bool possible = 1;       for (int i = 0; i < n; ++i) {         string s;         cin >> s;         bool ok = f(S[i], s);         if (!ok) possible = 0;         S[i + 1] = s;       }       if (!possible)         cout <<  NO  <<   n ;       else {         cout <<  YES  <<   n ;         for (int i = 0; i < n; ++i) cout << S[i + 1] <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   struct ufds {     int n;     vector<int> p, s;     ufds(int n = 0) : n(n), p(n), s(n, 1) {       for (int i = 0; i < n; i++) p[i] = i;     }     int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); }     bool inSameSet(int i, int j) { return findSet(i) == findSet(j); }     int getSizeOfSet(int i) { return s[findSet(i)]; }     void unionSet(int i, int j) {       i = findSet(i);       j = findSet(j);       if (i != j) {         if (s[i] < s[j]) swap(i, j);         s[i] += s[j];         p[j] = i;       }     }   };   struct maximum_matching {     int n, mm = 0, t = 0;     vector<int> m, l, p, o, v;     vector<vector<int>> &e;     queue<int> q;     maximum_matching(vector<vector<int>> &e)         : n(e.size()), m(n, -1), l(n), p(n), o(n), v(n), e(e) {       for (int i = 0; i < n; i++)         if (m[i] == -1) mm += bfs(i);     }     int lca(int i, int j) {       for (t++;; swap(i, j))         if (i != -1) {           if (v[i] == t) return i;           v[i] = t;           i = m[i] == -1 ? -1 : o[p[m[i]]];         }     }     void blossom(int i, int j, int a) {       while (o[i] != a) {         p[i] = j;         j = m[i];         if (l[j] == 1) l[j] = 0, q.push(j);         o[i] = o[j] = a;         i = p[j];       }     }     bool bfs(int s) {       for (int i = 0; i < n; i++) l[i] = -1, o[i] = i;       while (!q.empty()) q.pop();       l[s] = 0;       q.push(s);       while (!q.empty()) {         int i = q.front();         q.pop();         for (int j : e[i])           if (l[j] == -1) {             l[j] = 1;             p[j] = i;             if (m[j] == -1) {               while (j != -1) m[j] = p[j], swap(j, m[m[j]]);               return true;             }             l[m[j]] = 0;             q.push(m[j]);           } else if (l[j] == 0 && o[i] != o[j]) {             int a = lca(o[i], o[j]);             blossom(i, j, a);             blossom(j, i, a);           }       }       return false;     }   };   int n, a[300100], r[300100], value, m, rng[300100][3], c = 1, comp[700];   bool u[300100], rngu[300100];   vector<int> er[2], te[700];   void fill() {     int nv = 1;     for (int i = 0; i < n - 1; i++)       if (r[i] == 0 && r[i + 1] == 0) {         while (u[nv]) nv++;         r[i] = r[i + 1] = nv, value++, u[nv] = true;       }     for (int i = 0; i < n; i++)       if (r[i] == 0) r[i] = 1;   }   void fillOE(int k, int av) {     int i = rng[k][0], j = rng[k][1];     if (av == a[i - 1])       r[i] = av;     else       r[j] = av;     value++, u[av] = true;   }   void fillE(int k) {     int i = rng[k][0], j = rng[k][1];     r[i] = r[i - 1], r[j] = r[j + 1];     u[a[i - 1]] = u[a[j + 1]] = true;     value++;   }   void dfsTree(int i, int p = -1) {     for (int k : te[i]) {       int j = i ^ rng[k][2];       if (j != p) {         fillOE(k, j);         dfsTree(j, i);       }     }   }   void dfsComp(int i) {     comp[i] = c;     for (int k : te[i]) {       int j = i ^ rng[k][2];       if (comp[j] == 0) dfsComp(j);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout << fixed << setprecision(12);     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i], r[i] = a[i];     int s = 0;     while (s < n && a[s] == 0) s++;     if (s < n && s % 2 == 1) a[s - 1] = r[s - 1] = a[s], s--;     int e = n - 1;     while (e >= 0 && a[e] == 0) e--;     if (e >= 0 && (n - e - 1) % 2 == 1) a[e + 1] = r[e + 1] = a[e], e++;     if (s < e) {       for (int i = s + 1; i < e; i++)         if (a[i] == 0) {           int j = i;           while (a[j + 1] == 0) j++;           rng[m][0] = i, rng[m][1] = j;           rng[m][2] = a[i - 1] ^ a[j + 1];           er[(j - i + 1) & 1].push_back(m);           m++;           i = j;         }       ufds uf(700);       for (int k : er[1]) {         int i = rng[k][0], j = rng[k][1];         if (!uf.inSameSet(a[i - 1], a[j + 1])) {           uf.unionSet(a[i - 1], a[j + 1]);           te[a[i - 1]].push_back(k);           te[a[j + 1]].push_back(k);           rngu[k] = true;         }       }       for (int k : er[1]) {         int i = rng[k][0], j = rng[k][1];         if (!rngu[k] && uf.inSameSet(a[i - 1], a[j + 1]) && !u[a[i - 1]]) {           fillOE(k, a[i - 1]);           dfsTree(a[i - 1]);         }       }       for (int i = s; i < e; i++)         if (a[i] > 0 && a[i] == a[i + 1] && !u[a[i]])           value++, u[a[i]] = true, dfsTree(a[i]);       for (int i = 1; i <= min(n, 600); i++)         if (!u[i] && comp[i] == 0) dfsComp(i), c++;       vector<vector<int>> e(c);       for (int k : er[0]) {         int i = rng[k][0], j = rng[k][1];         if (!u[a[i - 1]] && !u[a[j + 1]] && comp[a[i - 1]] != comp[a[j + 1]]) {           e[comp[a[i - 1]]].push_back(comp[a[j + 1]]),               e[comp[a[j + 1]]].push_back(comp[a[i - 1]]);         }       }       maximum_matching mm(e);       for (int k : er[0]) {         int i = rng[k][0], j = rng[k][1];         if (mm.m[comp[a[i - 1]]] != -1 &&             mm.m[comp[a[i - 1]]] == comp[a[j + 1]]) {           fillE(k);           dfsTree(a[i - 1]);           dfsTree(a[j + 1]);           mm.m[comp[a[i - 1]]] = -1, mm.m[comp[a[j + 1]]] = -1;         }       }       for (int i = 1; i <= min(n, 600); i++)         if (!u[i]) dfsTree(i);     }     fill();     for (int i = 0; i < n; i++) cout << r[i] << (i < n - 1 ?     :   n );   }


#include <bits/stdc++.h>   using namespace std;   template <typename tp>   inline void read(tp& x) {     x = 0;     char tmp;     bool key = 0;     for (tmp = getchar(); !isdigit(tmp); tmp = getchar()) key = (tmp ==  - );     for (; isdigit(tmp); tmp = getchar()) x = (x << 3) + (x << 1) + (tmp ^  0 );     if (key) x = -x;   }   template <typename tp>   inline void ckmn(tp& x, tp y) {     x = x < y ? x : y;   }   template <typename tp>   inline void ckmx(tp& x, tp y) {     x = x < y ? y : x;   }   const long long LLINF = (long long)1e18;   const int N = 100010, MP = 20;   int n, q, sta[N];   long long rec[MP], val[N];   struct node {     long long mn;     long long rv;     long long tag;     int rec;   } t[N << 2];   void puttag(int x, long long v) {     t[x].tag += v;     t[x].mn -= v;     t[x].rv += v;   }   void putrec(int x) {     t[x].rec = 1;     t[x].rv = t[x].mn = LLINF;     t[x].tag = 0;   }   void push_down(int x) {     if (t[x].tag) {       puttag(x << 1, t[x].tag);       puttag(x << 1 | 1, t[x].tag);       t[x].tag = 0;     }     if (t[x].rec) {       putrec(x << 1);       putrec(x << 1 | 1);       t[x].rec = 0;     }   }   void push_up(int x) {     t[x].mn = min(t[x << 1].mn, t[x << 1 | 1].mn);     t[x].rv = t[x << 1 | 1].rv;     if (t[x].rv >= LLINF) t[x].rv = t[x << 1].rv;   }   long long query(int p, int x = 1, int lp = 1, int rp = n) {     if (lp == rp) return t[x].rv;     push_down(x);     int mid = (lp + rp) >> 1;     if (p <= mid) return query(p, x << 1, lp, mid);     long long tmp = query(p, x << 1 | 1, mid + 1, rp);     if (tmp >= LLINF)       return t[x << 1].rv;     else       return tmp;   }   long long askmn(int l, int r, int x = 1, int lp = 1, int rp = n) {     if (l > rp || lp > r) return LLINF;     if (lp >= l && rp <= r) return t[x].mn;     int mid = (lp + rp) >> 1;     push_down(x);     return min(askmn(l, r, x << 1, lp, mid),                askmn(l, r, x << 1 | 1, mid + 1, rp));   }   void modify(int l, int r, long long v, int x = 1, int lp = 1, int rp = n) {     if (l > rp || lp > r) return;     if (lp >= l && rp <= r) {       puttag(x, v);       return;     }     int mid = (lp + rp) >> 1;     push_down(x);     modify(l, r, v, x << 1, lp, mid), modify(l, r, v, x << 1 | 1, mid + 1, rp);     push_up(x);   }   void doit(int x = 1, int lp = 1, int rp = n) {     if (t[x].mn >= 0) return;     if (lp == rp) {       while (t[x].mn < 0) {         ++sta[lp];         t[x].mn = rec[sta[lp]] - t[x].rv;       }       return;     }     int mid = (lp + rp) >> 1;     push_down(x);     doit(x << 1, lp, mid);     doit(x << 1 | 1, mid + 1, rp);     push_up(x);   }   void fill(int l, int r, int x = 1, int lp = 1, int rp = n) {     if (l > rp || lp > r) return;     if (lp >= l && rp <= r) {       putrec(x);       return;     }     int mid = (lp + rp) >> 1;     push_down(x);     fill(l, r, x << 1, lp, mid);     fill(l, r, x << 1 | 1, mid + 1, rp);     push_up(x);   }   void setval(int p, long long v, int x = 1, int lp = 1, int rp = n) {     if (lp == rp) {       t[x].rv = v;       t[x].tag = 0, t[x].rec = 0;       t[x].mn = rec[0] - v;       sta[lp] = 0;       return;     }     int mid = (lp + rp) >> 1;     push_down(x);     if (p <= mid)       setval(p, v, x << 1, lp, mid);     else       setval(p, v, x << 1 | 1, mid + 1, rp);     push_up(x);   }   void build(int x = 1, int lp = 1, int rp = n) {     t[x].tag = t[x].rec = 0;     if (lp == rp) {       t[x].rv = val[lp];       t[x].mn = rec[sta[lp]] - val[lp];       return;     }     int mid = (lp + rp) >> 1;     build(x << 1, lp, mid);     build(x << 1 | 1, mid + 1, rp);     push_up(x);   }   int main() {     int tp, x, y, z;     read(n), read(q);     for (int i = (1); i <= (n); ++i) read(val[i]);     rec[0] = 1;     for (int i = 1; rec[i - 1] <= LLINF / 42; ++i) rec[i] = rec[i - 1] * 42;     build();     for (int i = (1); i <= (q); ++i) {       read(tp);       if (tp == 1) {         read(x);         printf( %lld n , query(x));       } else if (tp == 2) {         read(x), read(y), read(z);         if (y < n) setval(y + 1, query(y + 1));         setval(x, z);         if (x < y) fill(x + 1, y);       } else {         read(x), read(y), read(z);         setval(x, query(x));         if (y < n) setval(y + 1, query(y + 1));         while (true) {           modify(x, y, z);           doit();           if (askmn(x, y) > 0) break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 3;   struct tree {     long long sum;     int tag;     int lc;     int rc;   } a[N << 1];   long long s[N];   int n, m, t = 1;   long long ans;   long long win;   vector<pair<long long, long long> > other, change;   inline int read() {     int v = 0, c = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) c = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       v = v * 10 + ch - 48;       ch = getchar();     }     return v * c;   }   inline void pushup(int u) { a[u].sum = a[a[u].lc].sum + a[a[u].rc].sum; }   inline void pushdown(int u, int l, int r) {     if (a[u].tag) {       int mid = (l + r) >> 1;       a[a[u].lc].sum = mid - l + 1 - a[a[u].lc].sum;       a[a[u].lc].tag ^= 1;       a[a[u].rc].sum = r - mid - a[a[u].rc].sum;       a[a[u].rc].tag ^= 1;     }     a[u].tag = 0;   }   void build(int u, int l, int r) {     if (l == r) return;     int mid = (l + r) >> 1;     a[u].lc = ++t;     build(a[u].lc, l, mid);     a[u].rc = ++t;     build(a[u].rc, mid + 1, r);     pushup(u);   }   long long query(int u, int l, int r, int ll, int rr, int w) {     if (l == ll && r == rr) {       if (w == 1) return a[u].sum;       return r - l + 1 - a[u].sum;     }     pushdown(u, l, r);     int mid = (l + r) >> 1;     if (rr <= mid)       return query(a[u].lc, l, mid, ll, rr, w);     else if (ll > mid)       return query(a[u].rc, mid + 1, r, ll, rr, w);     else       return query(a[u].lc, l, mid, ll, mid, w) +              query(a[u].rc, mid + 1, r, mid + 1, rr, w);   }   void updata(int u, int l, int r, int ll, int rr) {     if (l == ll && r == rr) {       a[u].sum = r - l + 1 - a[u].sum;       a[u].tag ^= 1;       return;     }     pushdown(u, l, r);     int mid = (l + r) >> 1;     if (rr <= mid)       updata(a[u].lc, l, mid, ll, rr);     else if (ll > mid)       updata(a[u].rc, mid + 1, r, ll, rr);     else {       updata(a[u].lc, l, mid, ll, mid);       updata(a[u].rc, mid + 1, r, mid + 1, rr);     }     pushup(u);   }   int main() {     n = read(), m = read();     for (int i = 1; i <= n; ++i) s[i] = (long long)read();     sort(s + 1, s + n + 1);     for (int i = 1; i <= m; ++i) {       int l = read(), r = read();       l = lower_bound(s + 1, s + n + 1, l) - s;       r = upper_bound(s + 1, s + n + 1, r) - s - 1;       if (l > r) continue;       change.push_back(make_pair(l, r));       other.push_back(make_pair(r, l));     }     build(1, 1, n);     sort(change.begin(), change.end());     sort(other.begin(), other.end());     ans = (long long)(n - 2) * (n - 1) * n / 6;     for (int i = 1, r = 0, l = 0; i <= n; ++i) {       while (l < change.size() && change[l].first == i) {         updata(1, 1, n, change[l].first, change[l].second);         l++;       }       win = 0;       if (i != 1) win += query(1, 1, n, 1, i - 1, 0);       if (i != n) win += query(1, 1, n, i + 1, n, 1);       ans -= win * (win - 1) / 2;       while (r < other.size() && other[r].first == i) {         updata(1, 1, n, other[r].second, other[r].first);         r++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   const double PI = acos(-1);   template <class A, class B>   std::ostream& operator<<(std::ostream& st, const std::pair<A, B> p) {     st <<  (  << p.first <<  ,   << p.second <<  ) ;     return st;   }   using namespace std;   const int mod = 998244353;   int add(int a, int b) {     a += b;     if (a >= mod) a -= mod;     if (a < 0) a += mod;     return a;   }   int mul(int a, int b) { return (a * (long long)b) % mod; }   const int MAX = 500 * 1000 + 10;   int n, K, m;   int L[MAX], R[MAX], X[MAX];   int cnt[MAX];   int val[MAX];   int tail[MAX];   int p2[MAX];   int dp[MAX], sum[MAX];   int prv[MAX], nxt[MAX];   int solve(int b) {     static std::vector<std::pair<int, int> > V;     for (int i = 0; i < (n + 1); i++) cnt[i] = 0;     V.clear();     for (int i = 0; i < (m); i++) {       int x = (X[i] >> b) & 1;       if (x) {         cnt[L[i]]++;         cnt[R[i] + 1]--;       } else {         V.emplace_back(L[i], R[i]);       }     }     for (int i = 1; i <= n; i++) {       cnt[i] += cnt[i - 1];       val[i] = (cnt[i] > 0) + val[i - 1];     }     for (int i = 1, v = -1; i <= n; i++) {       if (!cnt[i]) v = i;       prv[i] = v;     }     for (int i = n, v = -1; i >= 1; i--) {       if (!cnt[i]) v = i;       nxt[i] = v;     }     for (int i = 1; i <= n; i++) tail[i] = -1;     for (auto p : V) {       int l = p.first, r = p.second;       int ctr = val[r] - val[l - 1];       if (ctr == r - l + 1) return 0;       l = nxt[l];       r = prv[r];       l = l - val[l];       r = r - val[r];       tail[r] = max(tail[r], l);     }     int m = n - val[n];     if (!m) return 1;     dp[0] = 1;     sum[0] = 1;     int L = 0;     for (int i = 1; i <= m; i++) {       dp[i] = add(sum[i - 1], L ? -sum[L - 1] : 0);       sum[i] = add(dp[i], sum[i - 1]);       L = max(L, tail[i]);     }     int ret = add(sum[m], L ? -sum[L - 1] : 0);     return ret;   }   int main() {     p2[0] = 1;     for (int i = 1; i < MAX; i++) p2[i] = mul(2, p2[i - 1]);     scanf( %d %d %d , &n, &K, &m);     for (int i = 0; i < (m); i++) scanf( %d %d %d , L + i, R + i, X + i);     int ans = 1;     for (int b = 0; b < (K); b++) {       ans = mul(ans, solve(b));       if (!ans) break;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   const int dx[] = {1, 0, -1, 0};   const int dy[] = {0, 1, 0, -1};   using namespace std;   const long long MOD = 1000000009;   const int MAXM = 100010;   int m;   long long pow_mod[MAXM];   int X[MAXM], Y[MAXM];   map<pair<int, int>, int> ma;   set<int> S;   set<int> up[MAXM], down[MAXM];   void init() {     pow_mod[0] = 1;     for (int i = 1; i < m; i++) {       pow_mod[i] = (pow_mod[i - 1] * m) % MOD;     }   }   bool ok(int k) {     for (auto el : up[k]) {       if (down[el].size() <= 1) return false;     }     return true;   }   int main() {     cin >> m;     init();     for (int i = 0; i < m; i++) {       cin >> X[i] >> Y[i];       ma.insert(make_pair(pair<int, int>(X[i], Y[i]), i));     }     for (auto el : ma) {       int y = el.first.second;       int x = el.first.first;       for (int d = -1; d <= 1; d++) {         pair<int, int> tmp = pair<int, int>(x + d, y + 1);         if (ma.count(tmp)) {           up[el.second].insert(ma[tmp]);           down[ma[tmp]].insert(el.second);         }       }     }     for (int i = 0; i < m; i++)       if (ok(i)) S.insert(i);     long long ret = 0;     for (int i = 0; i < m; i++) {       int er;       if (i % 2 == 0) {         er = *S.rbegin();       } else {         er = *S.begin();       }       ret += pow_mod[m - i - 1] * (long long)er % MOD;       ret %= MOD;       S.erase(er);       for (auto el : up[er]) {         down[el].erase(er);         if (ok(el))           S.insert(el);         else           S.erase(el);         for (auto ell : down[el]) {           if (ok(ell))             S.insert(ell);           else             S.erase(ell);         }       }       for (auto el : down[er]) {         up[el].erase(er);         if (ok(el))           S.insert(el);         else           S.erase(el);       }     }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5 * 10000;   const long long mod = 1000000007;   void solve() {     int n;     cin >> n;     vector<int> a(n), b(n), c(n), d(n), e(n);     string str;     for (int i = 0; i < n; i++) {       cin >> str;       int qtd[] = {0, 0, 0, 0, 0};       for (char c : str) {         qtd[c -  a ]++;       }       a[i] = 2 * qtd[0] - str.length();       b[i] = 2 * qtd[1] - str.length();       c[i] = 2 * qtd[2] - str.length();       d[i] = 2 * qtd[3] - str.length();       e[i] = 2 * qtd[4] - str.length();     }     sort(a.rbegin(), a.rend());     sort(b.rbegin(), b.rend());     sort(c.rbegin(), c.rend());     sort(d.rbegin(), d.rend());     sort(e.rbegin(), e.rend());     int ans = 0;     int sum = 0, i = 0;     while (i < n && sum + a[i] > 0) {       sum += a[i];       i++;     }     ans = max(ans, i);     sum = 0, i = 0;     while (i < n && sum + b[i] > 0) {       sum += b[i];       i++;     }     ans = max(ans, i);     sum = 0, i = 0;     while (i < n && sum + c[i] > 0) {       sum += c[i];       i++;     }     ans = max(ans, i);     sum = 0, i = 0;     while (i < n && sum + d[i] > 0) {       sum += d[i];       i++;     }     ans = max(ans, i);     sum = 0, i = 0;     while (i < n && sum + e[i] > 0) {       sum += e[i];       i++;     }     ans = max(ans, i);     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int tt = 1;     cin >> tt;     while (tt--) solve();     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   using ll = long long;   using pii = pair<int, int>;   using vi = vector<int>;   using ld = long double;   struct Node {     vi to;     int lk;     ll w;     Node() : to(14), lk(0), w(0) {}   };   vector<Node> au(1);   int sz = 1;   void add(const string& s, int w) {     int v = 0;     for (char c : s) {       c -=  a ;       if (!au[v].to[c]) {         au[v].to[c] = sz++;         au.push_back(Node());       }       v = au[v].to[c];     }     au[v].w += w;   }   void build() {     queue<int> q;     q.push(0);     while (!q.empty()) {       int v = q.front();       q.pop();       au[v].w += au[au[v].lk].w;       for (char kek =  a ; kek <=  n ; kek++) {         auto c = kek -  a ;         if (au[v].to[c]) {           int u = au[v].to[c];           au[u].lk = (v ? au[au[v].lk].to[c] : 0);           q.push(u);         } else {           au[v].to[c] = au[au[v].lk].to[c];         }       }     }   }   ll dp[1 << 14][1001];   ll dp_new[1 << 14][1001];   const ll inf = 1e18 + 228;   int32_t main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int k;     cin >> k;     for (int i = 0; i < k; i++) {       string s;       int w;       cin >> s >> w;       add(s, w);     }     build();     string s;     cin >> s;     string KEK = s;     int n = (int)((s).size());     const ll inf = 1e18 + 228;     vi bpc(1 << 14);     for (int msk = 1; msk < (1 << 14); msk++) {       bpc[msk] = 1 + bpc[msk & (msk - 1)];     }     vi qps;     ll ans_add = 0;     int v = 0;     int lst = -sz;     for (int i = 0; i < n; i++) {       if (s[i] ==  ? )         qps.push_back(i), v = 0, lst = i;       else         v = au[v].to[s[i] -  a ];       if (i - lst >= sz) ans_add += au[v].w;     }     for (int i = 0; i < (1 << 14); i++) {       for (int j = 0; j < sz; j++) {         dp[i][j] = dp_new[i][j] = -inf;       }     }     dp[0][0] = 0;     int pi = (qps.empty() || qps[0] < sz ? 0 : qps[0] - sz);     int yet = 0;     vector<vi> msks(15);     for (int m = 0; m < (1 << 14); m++) {       msks[bpc[m]].push_back(m);     }     for (int i : qps) {       pi = max(pi, i - sz);       for (int m : msks[yet]) {         for (int s = 0; s < sz; s++) {           dp_new[m][s] = -inf;         }       }       for (int s = 0; s < sz; s++) {         int ss = s;         for (int j = pi; j < i; j++) {           ss = au[ss].to[KEK[j] -  a ];         }         for (int m : msks[yet]) {           if (dp[m][s] == -inf) continue;           dp_new[m][ss] = max(dp_new[m][ss], dp[m][s]);         }       }       memcpy(dp, dp_new, sizeof(dp));       for (int m : msks[yet]) {         for (int b = 0; b < 14; b++) {           if (m >> b & 1) continue;           int mm = m | (1 << b);           for (int s = 0; s < sz; s++) {             if (dp[m][s] == -inf) continue;             int ss = au[s].to[b];             dp[mm][ss] = max(dp[mm][ss], dp[m][s] + au[ss].w);           }         }       }       yet++;       pi = i + 1;       while (pi < n && pi - i < sz && s[pi] !=  ? ) pi++;       for (int s = 0; s < sz; s++) {         int ss = s;         ll add = 0;         for (int j = i + 1; j < pi; j++) {           ss = au[ss].to[KEK[j] -  a ];           add += au[ss].w;         }         for (int m : msks[yet]) {           if (dp[m][s] == -inf) continue;           dp_new[m][ss] = max(dp_new[m][ss], dp[m][s] + add);         }       }       memcpy(dp, dp_new, sizeof(dp));     }     ll ans = -inf;     for (int i = 0; i < (1 << 14); i++) {       if (bpc[i] != (int)((qps).size())) continue;       for (int j = 0; j < sz; j++) {         if (ans < dp[i][j]) {           ans = dp[i][j];         }       }     }     cout << ans + ans_add;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char ch1, ch2, ch;     int i, j, k, tc, res, total, temp, mx, num, num1, num2;     int n, a, b;     {       scanf( %d %d %d , &n, &a, &b);       if (b == 0)         printf( %d n , a);       else if (b > 0) {         while (b--) {           if (a == n)             a = 1;           else             a++;         }         printf( %d n , a);       } else {         b = (-1 * b);         while (b--) {           if (a == 1)             a = n;           else             a--;         }         printf( %d n , a);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, aa, bb;     cin >> a >> b;     a += 100;     b += 100;     cin >> aa >> bb;     aa += 100;     bb += 100;     if (aa != a && bb != b) {       int tmp = (abs(a - aa));       int tmp2 = (abs(b - bb));       cout << 2 * abs(tmp2 + tmp) + 4;     } else       cout << max(8, (6 + (abs(a - aa) + abs(b - bb)) * 2));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     vector<int> a(n);     for (auto &i : a) scanf( %d , &i);     map<int, int> dp;     int ans = 0;     int last = 0;     for (int i = 0; i < n; i++) {       int x = a[i];       dp[x] = dp[x - 1] + 1;       if (ans < dp[x]) {         ans = dp[x];         last = x;       }     }     vector<int> res;     for (int i = n - 1; i >= 0; i--) {       if (a[i] == last) {         res.push_back(i);         last--;       }     }     reverse(res.begin(), res.end());     printf( %d n , ans);     for (auto &i : res) printf( %d  , i + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int id = 0;   vector<int> group(2e5 + 5, -1);   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int N;     cin >> N;     vector<int> A(N);     for (int i = 0; i < N; i++) cin >> A[i];     string S;     cin >> S;     for (int i = 0; i < N; i++) {       if (group[A[i]] != -1) continue;       group[A[i]] = id;       int j = i;       while (j + 1 < N && S[j] ==  1 ) group[A[++j]] = id;       id++;     }     for (int i = 0; i < N; i++) {       if (A[i] == i + 1) continue;       if (group[i + 1] != group[A[i]]) {         cout <<  NO n ;         return 0;       }     }     cout <<  YES n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[200005];   int alph[26];   int main() {     scanf( %s , &s);     for (int i = 0; s[i]; i++) alph[s[i] -  a ]++;     for (int i = 0; i < 26; i++)       for (int j = 26 - 1; j > i; j--)         if (alph[i] & 1)           if (alph[j] & 1) alph[i]++, alph[j]--;     int odd = -1;     for (int i = 0; i < 26; i++)       if (alph[i] & 1) odd = i;     for (int i = 0; i < 26; i++)       for (int j = 1; j <= alph[i] / 2; j++) printf( %c , i +  a );     if (odd != -1) printf( %c , odd +  a );     for (int i = 26 - 1; i >= 0; i--)       for (int j = 1; j <= alph[i] / 2; j++) printf( %c , i +  a );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using db = double;   using str = string;   using pi = pair<int, int>;   using pl = pair<ll, ll>;   using pd = pair<db, db>;   using vi = vector<int>;   using vb = vector<bool>;   using vl = vector<ll>;   using vd = vector<db>;   using vs = vector<str>;   using vpi = vector<pi>;   using vpl = vector<pl>;   using vpd = vector<pd>;   template <class T>   using V = vector<T>;   template <class T, size_t SZ>   using AR = array<T, SZ>;   template <class T>   using PR = pair<T, T>;   const int MOD = 1e9 + 7;   const int mxN = 1e3 + 5;   const ll INF = 1e18;   const ld PI = acos((ld)-1);   const int tSZ = (1 << 21);   const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};   mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());   constexpr int pcnt(int x) { return __builtin_popcount(x); }   constexpr int bits(int x) { return 31 - __builtin_clz(x); }   ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }   ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }   ll add(ll first, ll second) {     first += second;     if (first >= MOD) first %= MOD;     if (first < 0) first += MOD;     return first;   }   ll mul(ll first, ll second) {     first *= second;     if (first >= MOD) first %= MOD;     return first;   }   ll pw(ll x, ll y) {     ll ans = 1, ml = x;     while (y) {       if (y & 1) ans = mul(ans, ml);       ml = mul(ml, ml);       y >>= 1;     }     return ans;   }   ll inv(ll x) { return pw(x, MOD - 2); }   template <class T>   void remDup(vector<T>& x) {     sort(begin(x), end(x));     x.erase(unique(begin(x), end(x)), end(x));   }   template <class T, class U>   void erase(T& x, const U& v) {     auto it = x.find(v);     assert(it != end(x));     x.erase(v);   }   template <class T>   void re(complex<T>& c);   template <class T, class U>   void re(pair<T, U>& p);   template <class T>   void re(vector<T>& v);   template <class T, size_t SZ>   void re(array<T, SZ>& a);   template <class T>   void re(T& x) {     cin >> x;   }   void re(db& d) {     str t;     re(t);     d = stod(t);   }   void re(ld& d) {     str t;     re(t);     d = stold(t);   }   template <class T, class... U>   void re(T& t, U&... u) {     re(t);     re(u...);   }   template <class T>   void re(complex<T>& c) {     T a, b;     re(a, b);     c = {a, b};   }   template <class T, class U>   void re(pair<T, U>& p) {     re(p.first, p.second);   }   template <class T>   void re(vector<T>& x) {     for (auto& a : x) re(a);   }   template <class T, size_t SZ>   void re(array<T, SZ>& x) {     for (auto& a : x) re(a);   }   template <class T>   void rv(int& n, vector<T>& x) {     re(n);     x.resize(n);     for (auto& a : x) re(a);   }   str to_string(char c) { return str(1, c); }   str to_string(const char* second) { return (str)second; }   str to_string(str second) { return second; }   str to_string(bool b) { return to_string((int)b); }   template <class T>   str to_string(complex<T> c) {     stringstream ss;     ss << c;     return ss.str();   }   str to_string(vector<bool> v) {     str res =  { ;     for (int i = (0); i < ((int)(v).size()); i++) res += char( 0  + v[i]);     res +=  } ;     return res;   }   template <size_t SZ>   str to_string(bitset<SZ> b) {     str res =   ;     for (int i = (0); i < (SZ); i++) res += char( 0  + b[i]);     return res;   }   template <class T, class U>   str to_string(pair<T, U> p);   template <class T>   str to_string(T v) {     bool fst = 1;     str res =   ;     for (const auto& x : v) {       if (!fst) res +=    ;       fst = 0;       res += to_string(x);     }     return res;   }   template <class T, class U>   str to_string(pair<T, U> p) {     return to_string(p.first) +     + to_string(p.second);   }   template <class T>   void pr(T v) {     cout << to_string(v);   }   template <class T, class... U>   void pr(const T& t, const U&... u) {     pr(t);     pr(u...);   }   void ps() { pr(  n ); }   template <class T, class... U>   void ps(const T& t, const U&... u) {     pr(t);     if (sizeof...(u)) pr(   );     ps(u...);   }   void setIn(str second) { freopen(second.c_str(),  r , stdin); }   void setOut(str second) { freopen(second.c_str(),  w , stdout); }   void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }   void setIO(str st =   ) {     unsyncIO();     if ((int)(st).size()) {       setIn(st +  .in ), setOut(st +  .out );     }   }   void DBG() { cerr <<  ]  << endl; }   template <class T, class... U>   void DBG(const T& t, const U&... u) {     cerr << to_string(t);     if (sizeof...(u)) cerr <<  ,  ;     DBG(u...);   }   int n, k;   str matr[mxN * mxN];   vector<str> vv;   int graph[30][30];   int c_br[mxN * mxN];   bool ok = 1;   void calc() {     for (int i = (0); i < ((int)(vv).size() - 1); i++) {       str tmp = vv[i];       str nxt = vv[i + 1];       int k = 0;       int zz = min((int)(tmp).size(), (int)(nxt).size());       while (k < zz && tmp[k] == nxt[k]) {         k++;       }       if (k < zz) {         graph[tmp[k] -  a ][nxt[k] -  a ] = 1;       } else if ((int)(tmp).size() > (int)(nxt).size()) {         ok = 0;       }     }   }   void solve() {     vi vis(27, 0);     set<int> alphabet;     for (int i = (0); i < (n * k); i++) {       if ((int)(matr[i]).size() != 0) {         vv.push_back(matr[i]);         for (auto& x : matr[i]) alphabet.insert(x -  a );       }     }     for (int i = (0); i < ((int)(vv).size()); i++) {       c_br[i] = (int)(vv[i]).size();     }     calc();     vi ans;     for (int i = (0); i < (27); i++) {       for (int j = (0); j < (27); j++) {         if (vis[j] || !alphabet.count(j)) continue;         int cnt = 0;         for (int z = (0); z < (27); z++) {           cnt += graph[z][j];         }         if (cnt == 0) {           ans.push_back(j);           vis[j] = 1;           for (int z = (0); z < (27); z++) {             graph[j][z] = 0;           }         }       }     }     if (!ok || (int)(ans).size() != (int)(alphabet).size()) {       ps( IMPOSSIBLE );       return;     }     for (auto& x : ans) {       pr(char(x +  a ));     }     ps();   }   int main() {     setIO();     re(n, k);     int c;     for (int i = (0); i < (n); i++) {       re(c);       for (int j = (0); j < (k); j++) {         re(matr[c * n + j]);       }     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t;     cin >> n >> t;     if (n == 1 && t == 10)       cout << -1 << endl;     else {       cout << t;       if (t == 10) n--;       for (int i = 0; i < n - 1; i++) cout << 0;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char tbl[209][209];   int vid[209][209];   int hid[209][209];   int n, m;   vector<int> G[100009];   int pr[100009];   int was[100009];   int wcnt;   bool vert(int x, int y) { return vid[x][y] > 0; }   bool improve(int x) {     was[x] = wcnt;     for (int nb : G[x]) {       if (was[nb] < wcnt) {         if (pr[nb] == nb) {           pr[nb] = x;           pr[x] = nb;           return true;         }         was[nb] = wcnt;         if (improve(pr[nb])) {           pr[nb] = x;           pr[x] = nb;           return true;         }       }     }     return false;   }   int h2 = 0;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> tbl[i][j];         if (tbl[i][j] ==  # ) h2++;       }     }     int cnt = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j < m; j++) {         if (tbl[i][j] ==  #  && tbl[i][j + 1] ==  # ) {           cnt++;           vid[i][j] = cnt;         }       }     }     int rows = cnt;     for (int i = 1; i < n; i++) {       for (int j = 1; j <= m; j++) {         if (tbl[i][j] ==  #  && tbl[i + 1][j] ==  # ) {           cnt++;           hid[i][j] = cnt;         }       }     }     for (int i = 1; i < n; i++) {       for (int j = 1; j <= m; j++) {         if (tbl[i][j] ==  #  && tbl[i + 1][j] ==  # ) {           if (vert(i, j - 1)) {             G[hid[i][j]].push_back(vid[i][j - 1]);             G[vid[i][j - 1]].push_back(hid[i][j]);           }           if (vert(i, j)) {             G[hid[i][j]].push_back(vid[i][j]);             G[vid[i][j]].push_back(hid[i][j]);           }           if (vert(i + 1, j - 1)) {             G[hid[i][j]].push_back(vid[i + 1][j - 1]);             G[vid[i + 1][j - 1]].push_back(hid[i][j]);           }           if (vert(i + 1, j)) {             G[hid[i][j]].push_back(vid[i + 1][j]);             G[vid[i + 1][j]].push_back(hid[i][j]);           }         }       }     }     for (int i = 1; i <= cnt; i++) {       pr[i] = i;     }     int ans = 0;     for (int i = 1; i <= rows; i++) {       if (pr[i] == i) {         wcnt++;         if (improve(i)) ans++;       }     }     cout << h2 - (cnt - ans) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const unsigned int MOD = 1000000007;   void solve() {     string s;     cin >> s;     string t;     cin >> t;     vector<char> v;     bool check1 = false;     bool check2 = false;     bool check3 = false;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  a ) {         check1 = true;       }       if (s[i] ==  b ) {         check2 = true;       }       if (s[i] ==  c ) {         check3 = true;       }     }     if (t[0] ==  a  && t[1] ==  b  && t[2] ==  c  && check1 && check2 && check3) {       for (int j = 0; j < s.length(); j++) {         if (s[j] ==  a ) {           v.push_back(s[j]);         }       }       for (int j = 0; j < s.length(); j++) {         if (s[j] ==  c ) {           v.push_back(s[j]);         }       }       for (int j = 0; j < s.length(); j++) {         if (s[j] ==  b ) {           v.push_back(s[j]);         }       }     } else {       for (int j = 0; j < s.length(); j++) {         if (s[j] ==  a ) {           v.push_back(s[j]);         }       }       for (int j = 0; j < s.length(); j++) {         if (s[j] ==  b ) {           v.push_back(s[j]);         }       }       for (int j = 0; j < s.length(); j++) {         if (s[j] ==  c ) {           v.push_back(s[j]);         }       }     }     sort(s.begin(), s.end());     for (int i = 0; i < s.length(); i++) {       if (s[i] !=  a  && s[i] !=  b  && s[i] !=  c ) {         v.push_back(s[i]);       }     }     for (auto it : v) {       cout << it;     }     cout << endl;   }   signed main() {     int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 220, maxm = 220 * 220;   struct edge {     int s, t, next;   } e[maxm];   struct vtx {     int dfn, low, belong, leave;     bool instack;   } ver[maxn];   int hd[maxn], cnt, ind, sta[maxn], top, bcnt;   void insert(int a, int b) {     e[cnt].next = hd[a];     hd[a] = cnt;     e[cnt].t = b;     e[cnt].s = a;     cnt++;   }   vector<int> bcc[maxn];   void tarjanSCC(int u) {     ver[u].dfn = ver[u].low = ind++;     ver[u].instack = true;     sta[top++] = u;     for (int i = hd[u]; i != -1; i = e[i].next) {       int v = e[i].t;       if (!ver[v].dfn) {         tarjanSCC(v);         ver[u].low = min(ver[u].low, ver[v].low);       }       if (ver[v].instack) ver[u].low = min(ver[u].low, ver[v].dfn);     }     if (ver[u].dfn == ver[u].low) {       bcnt++;       int j = -1;       do {         j = sta[--top];         ver[j].belong = bcnt;         bcc[bcnt].push_back(j);         ver[j].instack = false;       } while (j != u);     }     ver[u].leave = ind;   }   void tarjanInit() {     memset(hd, -1, sizeof hd);     memset(ver, 0, sizeof ver);     ind = top = 1;     cnt = 0;   }   int ord[maxn];   bool cmp(int i, int j) { return ver[i].leave < ver[j].leave; }   bool twoSat(int n, int *sel) {     for (int i = 0; i < 2 * n; i++) bcc[i].clear();     for (int i = 0; i < 2 * n; i++)       if (!ver[i].dfn) tarjanSCC(i);     for (int i = 0; i < n; i++)       if (ver[i * 2].belong == ver[i * 2 + 1].belong) return false;     for (int i = 0; i < 2 * n; i++) ord[i] = i;     sort(ord, ord + 2 * n, cmp);     memset(sel, -1, n * sizeof(int));     for (int i = 0; i < 2 * n; i++) {       if (sel[ord[i] >> 1] != -1)         continue;       else {         int c = ver[ord[i]].belong;         for (int j = 0; j < (int)bcc[c].size(); j++)           sel[bcc[c][j] >> 1] = (bcc[c][j] & 1);       }     }     return true;   }   bool intersect(int a1, int a2, int b1, int b2) {     if (a1 > a2) swap(a1, a2);     if (b1 > b2) swap(b1, b2);     return a1 == b1 ? 0                     : (a1 < b1 ? (a2 - b1) * (a2 - b2) < 0                                : (b2 - a1) * (b2 - a2) < 0);   }   int ans[110];   int main() {     int n, m, p[110][2];     scanf( %d%d , &n, &m);     tarjanInit();     for (int i = 0; i < m; i++) {       scanf( %d%d , &p[i][0], &p[i][1]);     }     for (int i = 0; i < m; i++) {       for (int j = 0; j < i; j++) {         if (intersect(p[i][0], p[i][1], p[j][0], p[j][1])) {           insert(i * 2, j * 2 + 1);           insert(i * 2 + 1, j * 2);           insert(j * 2, i * 2 + 1);           insert(j * 2 + 1, i * 2);         }       }     }     if (twoSat(m, ans)) {       for (int i = 0; i < m; i++) {         if (ans[i])           printf( i );         else           printf( o );       }     } else       printf( Impossible );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ull = unsigned long long;   using vi = vector<int>;   using ii = pair<int, int>;   using vii = vector<pair<int, int>>;   void solve() {     int n;     cin >> n;     int r = 0;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         int x;         cin >> x;         if (i == j) r += x;       }     }     r %= 2;     string res;     int q;     cin >> q;     while (q--) {       int t;       cin >> t;       if (t == 3)         res += to_string(r);       else if (t == 1 || t == 2) {         int waste;         cin >> waste;         r ^= 1;       }     }     cout << res <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int tc = 1;     while (tc--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   void solve() {     int n;     cin >> n;     vector<set<int>> v(n + 1);     for (int i = 0; i < n - 2; i++) {       int x, y, z;       cin >> x >> y >> z;       v[x].insert(y);       v[x].insert(z);       v[y].insert(x);       v[y].insert(z);       v[z].insert(x);       v[z].insert(y);     }     vector<int> res(n);     for (int i = 1; i < n + 1; i++) {       if (((int)(v[i]).size()) == 2) {         res[0] = i;         break;       }     }     vector<bool> been(n + 1);     int num1 = res[0], num2;     int can1 = *v[num1].begin(), can2 = *v[num1].rbegin();     if (((int)(v[can1]).size()) == 3)       num2 = can1;     else       num2 = can2;     res[1] = num2;     been[num1] = been[num2] = 1;     for (int i = 2; i < n - 2; i++) {       vector<int> can;       for (auto& j : v[num1]) {         if (been[j]) continue;         can.push_back(j);       }       for (auto& j : v[num2]) {         if (been[j]) continue;         can.push_back(j);       }       sort(can.begin(), can.end());       int num = 0;       for (int j = 0; j < ((int)(can).size()) - 1; j++) {         if (can[j] == can[j + 1]) {           num = can[j];           break;         }       }       been[num] = 1;       res[i] = num;       num1 = num2;       num2 = num;     }     for (int i = 1; i < n + 1; i++) {       if (((int)(v[i]).size()) == 3 && !been[i]) {         res[n - 2] = i;       } else if (((int)(v[i]).size()) == 2 && !been[i]) {         res[n - 1] = i;       }     }     for (auto& i : res) cout << i <<    ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     solve();     return 0;   }