#include <bits/stdc++.h>   using namespace std;   const long long N = 1000006, MOD = 1e9 + 7, INF = 1e16;   long long a[N], b, n, m, k, g[105][105], q, x, ans, Ans, pre[N], l, r;   string s;   pair<int, int> p[N];   int main() {     ios_base::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 >> g[i][j];     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (g[i][j] < 1) x = 1;     while (!x) {       Ans++;       q += min(n, m);       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++) {           g[i][j]--;           if (g[i][j] < 1) x = 1;         }     }     ans = 0;     x = 0;     for (int i = 1; i <= n; i++)       if (!g[i][1]) x = 1;     while (!x) {       ans++, r++;       for (int i = 1; i <= n; i++) {         g[i][1]--;         if (!g[i][1]) x = 1;       }     }     x = 0;     for (int i = 1; i <= m; i++)       if (!g[1][i]) x = 1;     while (!x) {       ans++, l++;       for (int i = 1; i <= m; i++) {         g[1][i]--;         if (!g[1][i]) x = 1;       }     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (g[i][j] != g[1][j] + g[i][1]) {           cout << -1;           return 0;         }     for (int i = 1; i <= n; i++) ans += g[i][1];     for (int i = 2; i <= m; i++) ans += g[1][i];     cout << ans + q << endl;     for (int i = 1; i <= l; i++)       cout <<  row             <<     << 1 << endl;     for (int i = 1; i <= r; i++)       cout <<  col             <<     << 1 << endl;     for (int i = 1; i <= n; i++)       for (int u = 1; u <= g[i][1]; u++)         cout <<  row               <<     << i << endl;     for (int i = 1; i <= m; i++)       for (int u = 1; u <= g[1][i]; u++)         cout <<  col               <<     << i << endl;     if (n < m)       s =  row ;     else       s =  col ;     for (int i = 0; i < min(n, m) * Ans; i++)       cout << s <<     << i % min(n, m) + 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a[100010];   long long t;   int main() {     cin >> n >> m;     a[0] = 1;     for (int i = 1; i <= m; i++) {       cin >> a[i];       if (a[i] > a[i - 1]) t += (a[i] - a[i - 1]);       if (a[i] < a[i - 1]) t += (n - a[i - 1] + a[i]);     }     cout << t;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     int n, k;     cin >> n >> k;     int i = 1;     for (i = k - 1; i > 0; i--) {       if ((n % i) == 0) break;     }     if (i == 0) i = 1;     int x = (n / i) * k;     for (; x % k != i; x++) {     }     cout << x;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll INF = 4e18;   const int MAXN = 1.1e5;   ll T[MAXN];   int main() {     ios::sync_with_stdio(0), cin.tie(0);     ll N, M, A, D;     cin >> N >> M >> A >> D;     for (ll i = 0; i < M; i++) {       cin >> T[i];     }     sort(T, T + M);     T[M] = INF;     auto getEmployee = [&](ll i) -> ll { return i <= N ? i * A : INF; };     ll est = 1;     ll cst = 0;     ll ans = 0;     while (est <= N || cst < M) {       ll nxt = D + min(getEmployee(est), T[cst]);       ll een = min(N + 1, (nxt / A) + 1);       ll cen = ll(upper_bound(T, T + M, nxt) - T);       ans++;       if (cst == cen) {         ll delta = een - est;         assert(delta > 0);         ll tgt = min(N + 1, T[cst] / A);         ll extra = max(tgt - een, ll(0)) / delta;         ans += extra;         een += extra * delta;       }       assert(een > est || cen > cst);       assert(est <= een && cst <= cen);       tie(est, cst) = {een, cen};     }     cout << ans <<   n ;     return 0;   }


#include <iostream>   #include <cstring>   using namespace std;   int a[10005],s[10005];   signed main() {     int T;     cin >> T;     while(T--)     {       int n;       cin >> n;       memset(s,0,sizeof s);       for(int i=1;i<=n;i++) cin >> a[i];       long long ans=0;       for(int i=1;i<=n;i++)       {         ans+=max(0,a[i]-s[i]-1);         s[i]=max(s[i],a[i]-1);         for(int j=i+2;j<=min(n,i+a[i]);j++)           ++s[j];         s[i+1]+=max(0,s[i]-a[i]+1);       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int l[51], r[51], a[51], c[51], dp[51][51][51];   void inline FilterRestrictions(int i, int j, int h, vector<int> &v) {     vector<int> u;     for (int x : v) {       if (r[x] < i || j < l[x] || a[x] >= h) {         continue;       }       u.push_back(x);     }     v = u;     u.clear();     sort(v.begin(), v.end(), [](int i, int j) { return l[i] > l[j]; });   }   int Solve(int i, int j, int h, vector<int> v) {     if (j < i) {       return 0;     }     if (dp[i][j][h] != -1) {       return dp[i][j][h];     }     FilterRestrictions(i, j, h, v);     int ans = Solve(i, j, h - 1, v), cost = h * h;     vector<vector<int> > V(51 + 1);     vector<int> v1;     for (int x = i; x <= j; ++x) {       for (; !v.empty() && l[v.back()] == x; v.pop_back()) {         int k = v.back();         cost -= c[k];         V[r[k] + 1].push_back(k);       }       for (auto k : V[x]) {         cost += c[k];         v1.push_back(k);       }       int ansx = cost + Solve(i, x - 1, h, v1) + Solve(x + 1, j, h, v);       ans = max(ans, ansx);     }     dp[i][j][h] = ans;     return ans;   }   int main() {     int n, h, m;     cin >> n >> h >> m;     for (int i = 1; i <= m; ++i) {       cin >> l[i] >> r[i] >> a[i] >> c[i];     }     vector<int> v;     for (int i = 1; i <= m; ++i) {       v.push_back(i);     }     for (int i = 1; i <= n; ++i) {       for (int j = i; j <= n; ++j) {         for (int x = 1; x <= h; ++x) {           dp[i][j][x] = -1;         }       }     }     cout << Solve(1, n, h, v) <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native )   using namespace std;   template <typename T1, typename T2>   void ckmin(T1 &a, T2 b) {     if (a > b) a = b;   }   template <typename T1, typename T2>   void ckmax(T1 &a, T2 b) {     if (a < b) a = b;   }   int read() {     int x = 0, f = 0;     char ch = getchar();     while (!isdigit(ch)) f |= ch ==  - , ch = getchar();     while (isdigit(ch)) x = 10 * x + ch -  0 , ch = getchar();     return f ? -x : x;   }   template <typename T>   void print(T x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10) print(x / 10);     putchar(x % 10 +  0 );   }   template <typename T>   void print(T x, char let) {     print(x), putchar(let);   }   const int mlen = 1 << 20;   const int N = mlen + 5;   const int mod = 998244353;   unsigned long long omgs[N];   int rev[N];   int qpow(int a, int b = mod - 2) {     int res = 1;     while (b) {       if (b & 1) res = 1ll * res * a % mod;       a = 1ll * a * a % mod;       b >>= 1;     }     return res;   }   const int G = qpow(3, (mod - 1) / mlen);   void setup() {     omgs[mlen / 2] = 1;     for (int i = mlen / 2 + 1; i < mlen; i++) omgs[i] = G * omgs[i - 1] % mod;     for (int i = mlen / 2 - 1; i > 0; i--) omgs[i] = omgs[i << 1];   }   int getR(int len) {     int lim = 1, L = 0;     while (lim <= len) lim <<= 1, L++;     for (int i = 0; i < lim; i++)       rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << L - 1);     return lim;   }   inline void dft(vector<int> &a, int lim) {     a.resize(lim);     static unsigned long long A[N];     for (int i = 0; i < lim; i++) A[rev[i]] = a[i];     for (int mid = 1; mid < lim; mid <<= 1) {       for (int i = 0; i < lim; i += mid << 1) {         unsigned long long *W = omgs + mid;         for (int j = 0; j < mid; j++) {           unsigned long long tmp = *(W++) * A[i + j + mid] % mod;           A[i + j + mid] = A[i + j] + mod - tmp;           A[i + j] += tmp;         }       }     }     for (int i = 0; i < lim; i++) a[i] = A[i] % mod;   }   inline void idft(vector<int> &a, int lim) {     a.resize(lim);     dft(a, lim), reverse(a.begin() + 1, a.end());     int linv = mod - (mod - 1) / lim;     for (int i = 0; i < lim; i++) a[i] = 1ll * a[i] * linv % mod;   }   vector<int> operator*(vector<int> a, vector<int> b) {     int len = (int(a.size())) + (int(b.size())) - 1, lim = getR(len);     dft(a, lim), dft(b, lim);     for (int i = 0; i < lim; i++) a[i] = 1ll * a[i] * b[i] % mod;     idft(a, lim), a.resize(len);     return a;   }   vector<int> adj[N];   int n;   int son[N];   void dfs1(int u, int fa) {     for (auto v : adj[u]) {       if (v == fa) continue;       son[u]++;       dfs1(v, u);     }   }   int fac[N], ifac[N];   inline int C(int n, int m) {     return 1ll * fac[n] * ifac[n - m] % mod * ifac[m] % mod;   }   int main() {     setup();     n = read();     fac[0] = 1;     for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;     ifac[n] = qpow(fac[n]);     for (int i = n; i >= 1; i--) ifac[i - 1] = 1ll * ifac[i] * i % mod;     for (int i = 1; i <= n - 1; i++) {       int u = read(), v = read();       adj[u].push_back(v), adj[v].push_back(u);     }     dfs1(1, 0);     vector<int> f = {1};     sort(son + 1, son + n + 1, greater<int>());     for (int i = 1; i <= n; i++) {       int j = i;       while (j < n && son[i] == son[j + 1]) j++;       int up = j - i + 1;       vector<int> g(up + 1);       int cf = 1;       for (int i = 0; i <= up; i++) {         int coef = 1ll * C(up, i) * cf % mod;         g[i] = coef;         cf = 1ll * cf * (mod - son[j]) % mod;       }       f = f * g;       i = j;     }     int ans = 0;     for (int i = 0; i <= n - 1; i++) ans = (ans + 1ll * fac[n - i] * f[i]) % mod;     print(ans,   n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct MinMax {     int min, max;     MinMax() : min(INT_MAX), max(INT_MIN){};     MinMax(int min, int max) : min(min), max(max){};   };   int main(int argc, char const *argv[]) {     ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     MinMax a[n];     for (int i = 0; i < m; i++) {       int c;       cin >> c;       a[c - 1].min = min(a[c - 1].min, i);       a[c - 1].max = max(a[c - 1].max, i);     }     int sum = 0;     for (int i = 0; i < n; i++) {       int cnt = 3;       if (i == 0 || i == n - 1) cnt--;       if (a[i].min < INT_MAX) cnt--;       if (i < n - 1) {         if (a[i].min < a[i + 1].max) cnt--;       }       if (i > 0) {         if (a[i - 1].max > a[i].min) cnt--;       }       sum += cnt;     }     if (n == 1) sum = 0;     cout << sum << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t, n, i, j, k, h = 0;     cin >> n;     long long int a[n], b[n];     for (i = 0; i < n; ++i) {       cin >> a[i] >> b[i];     }     if (n == 1) cout << a[0] << endl;     if (n > 1) {       set<long long int> s1, s, s3;       set<long long int>::iterator jt, it;       j = a[0];       while (j % 2 == 0) {         s.insert(2);         j /= 2;       }       for (k = 3; k <= sqrt(j); k = k + 2) {         while (j % k == 0) {           s.insert(k);           j /= k;         }       }       if (j > 2) s.insert(j);       j = b[0];       while (j % 2 == 0) {         s.insert(2);         j /= 2;       }       for (k = 3; k <= sqrt(j); k = k + 2) {         while (j % k == 0) {           s.insert(k);           j /= k;         }       }       if (j > 2) s.insert(j);       j = a[1];       while (j % 2 == 0) {         s1.insert(2);         j /= 2;       }       for (k = 3; k <= sqrt(j); k = k + 2) {         while (j % k == 0) {           s1.insert(k);           j /= k;         }       }       if (j > 2) s1.insert(j);       j = b[1];       while (j % 2 == 0) {         s1.insert(2);         j /= 2;       }       for (k = 3; k <= sqrt(j); k = k + 2) {         while (j % k == 0) {           s1.insert(k);           j /= k;         }       }       if (j > 2) s1.insert(j);       for (it = s.begin(); it != s.end(); ++it) {         for (jt = s1.begin(); jt != s1.end(); jt++) {           if ((*jt) % (*it) == 0) {             s3.insert(*it);           } else if ((*it) % (*jt) == 0)             s3.insert(*jt);         }       }       s.clear();       for (i = 2; i < n; ++i) {         for (it = s3.begin(); it != s3.end(); ++it) {           if (a[i] % (*it) == 0) s.insert(*it);           if (b[i] % (*it) == 0) s.insert(*it);         }         s3.clear();         for (it = s.begin(); it != s.end(); ++it) {           s3.insert(*it);         }         s.clear();       }       if (s3.size() > 0) {         for (it = s3.begin(); it != s3.end(); ++it) {           cout << *it << endl;           break;         }       } else         cout <<  -1 n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     if (n == 0)       cout << 1;     else {       n %= 4;       if (n == 0) n = 4;       n = pow(8, n);       cout << n % 10;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<double> a(n), b(n);     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] <= 1) {         printf( -1 n );         exit(0);       }     }     for (int i = 0; i < n; i++) {       cin >> b[i];       if (b[i] <= 1) {         printf( -1 n );         exit(0);       }     }     double s = m;     s += s / (b[0] - 1);     for (int i = n - 1; i >= 1; i--) {       s += s / (a[i] - 1);       s += s / (b[i] - 1);     }     s += s / (a[0] - 1);     printf( %.10f n , s - m);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ull = unsigned long long;   const int MAX_N = 1e6 + 5;   int fen[MAX_N];   int n;   int lowbit(int x) { return x & (-x); }   void add(int x, int val) {     for (int i = x; i <= n; i += lowbit(i)) fen[i] += val;   }   int sum(int x) {     int s = 0;     for (int i = x; i; i -= lowbit(i)) s += fen[i];     return s;   }   int sum(int l, int r) { return sum(r) - sum(l); }   map<int, int> cnt;   int pre[MAX_N];   int back[MAX_N];   int arr[MAX_N];   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> arr[i];       cnt[arr[i]]++;       pre[i] = cnt[arr[i]];     }     cnt.clear();     for (int i = n - 1; i >= 0; i--) {       cnt[arr[i]]++;       back[i] = cnt[arr[i]];       add(back[i], 1);     }     ll ans = 0;     for (int i = 0; i < n; i++) {       add(back[i], -1);       ans += sum(pre[i] - 1);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 9;   bool p[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     vector<int> k;     for (int i = 2; i <= 1e6; i++) {       if (p[i]) continue;       k.push_back(i);       for (int j = i + i; j <= 1e6; j += i) {         p[j] = 1;       }     }     int t;     cin >> t;     while (t--) {       long long x;       cin >> x;       long long m = sqrtl(x);       long long r = upper_bound(k.begin(), k.end(), x) - k.begin();       long long l = upper_bound(k.begin(), k.end(), m) - k.begin();       cout << r - l + 1 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000007;   const int N = 100100;   int n;   int main() {     cin >> n;     for (int i = n + 1; i <= n + n; i++) cout << i <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-9;   struct DDMY {     ostringstream o;     template <class T>     DDMY &operator,(const T &t) {       o << t <<  , ;       return *this;     }     string operator*() { return o.str().substr(0, o.str().size() - 1); }   };   template <class T>   std::ostream &operator<<(std::ostream &o, const std::vector<T> &v) {     o <<  [ ;     for (std::size_t i = 0; i < v.size(); i++)       o << v[i] << (i < v.size() - 1 ?  ,  :   );     return o <<  ] ;   }   template <class T, class U>   std::ostream &operator<<(std::ostream &o, const std::pair<T, U> &v) {     return o << v.first <<  :  << v.second;   }   template <class T, class U>   std::ostream &operator<<(std::ostream &o, const std::map<T, U> &v) {     o <<  { ;     typename std::map<T, U>::const_iterator i = v.begin(), j = i;     ++j;     for (; i != v.end(); ++i, ++j) o << *i << (j != v.end() ?  ,  :   );     return o <<  } ;   }   template <class T>   std::ostream &operator<<(std::ostream &o, const std::set<T> &v) {     o <<  { ;     typename std::set<T>::const_iterator i = v.begin(), j = i;     ++j;     for (; i != v.end(); ++i, ++j) o << *i << (j != v.end() ?  ,  :   );     return o <<  } ;   }   void error() {     cout << -1 << endl;     exit(0);   }   int main(int argc, char *argv[]) {     ios::sync_with_stdio(false);     int a1, a2, a3, a4;     cin >> a1 >> a2 >> a3 >> a4;     if (max(a3, a4) > a1 || max(a3, a4) > a2) {       error();     }     string line;     if (a3 >= a4) {       for (int i = 0; i < a3; i++) {         line +=  47 ;       }       a1 -= a3;       a2 -= a3;       a4 -= a3 - 1;       if (a1 < 0 || a2 < 0 || a4 < 0) {         error();       }       if (a4 == 1) {         if (a1 == 0) {           if (a2 > 0) {             line =  7  + line;             a2--;             if (a2 > 0) line += string(a2,  7 );           } else {             error();           }         } else {           line += string(a2,  7 );           line +=  4 ;           a1--;         }       } else if (a4 == 0) {         line += string(a2,  7 );       } else {         error();       }       if (a1 > 0) line = string(a1,  4 ) + line;     } else {       for (int i = 0; i < a4; i++) {         line +=  74 ;       }       a1 -= a4;       a2 -= a4;       a3 -= a4 - 1;       if (a1 < 0 || a2 < 0 || a4 < 0) {         error();       }       if (a3 == 0) {         if (a4 > 1) {           line = line.substr(0, line.size() - 2);           line = line.substr(2);           line =  7  + string(a1 + 1,  4 ) + line;           line += string(a2 + 1,  7 ) +  4 ;         } else if (a4 == 1) {           a1 += a4;           a2 += a4;           if (a2 > 0 && a1 > 0) {             line = string(a2,  7 ) + string(a1,  4 );           } else {             error();           }         } else if (a4 == 0) {           if (a1 == 0 || a2 == 0) {             if (a1 > 0) {               line = string(a1,  4 );             } else if (a2 > 0) {               line = string(a2,  7 );             }           } else {             error();           }         } else {           error();         }       } else {         error();       }     }     cout << line << endl;     return 0;   }


#include <bits/stdc++.h>      template <class T>   inline void read(T &res)   {    res = 0; bool bo = 0; char c;    while (((c = getchar()) <  0  || c >  9 ) && c !=  - );    if (c ==  - ) bo = 1; else res = c - 48;    while ((c = getchar()) >=  0  && c <=  9 )     res = (res << 3) + (res << 1) + (c - 48);    if (bo) res = ~res + 1;   }      const int N = 505;      int n, m, a[N][N];      int main()   {    read(n); read(m);    for (int i = 1; i <= n; i++)     for (int j = 1; j <= m; j++)      read(a[i][j]);    for (int i = 1; i <= n; i++)    {     for (int j = 1; j <= m; j++)      printf( %d  , i + j & 1 ? 720720 : 720720 - a[i][j]       * a[i][j] * a[i][j] * a[i][j]);     puts(  );    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double pi = 3.141592653589793238462643383279502884197;   vector<long long int> v(14);   void Compute() {     for (long long int i = 1; i < 14; i++) {       v[i] = pow(2, i) + v[i - 1];     }   }   void solve() {     long long int n;     cin >> n;     long long int ans = 0;     string s = to_string(n);     long long int si = s.size();     ans += v[si - 1];     for (long long int i = 0; i < s.size(); i++) {       if (s[i] ==  7 ) ans += pow(2, (si - 1 - i));     }     ans++;     cout << ans << endl;   }   int main() {     Compute();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int V = 100000 + 5;   int n, num[V];   int main() {     int i, j;     scanf( %d , &n);     for (i = 0; i < n; ++i) scanf( %d , &num[i]);     sort(num, num + n);     printf( %d  , num[n - 1]);     for (i = 1; i < n - 1; ++i) printf( %d  , num[i]);     printf( %d n , num[0]);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   const double pi = acos(-1.0);   struct Complex {     double x, y;     Complex(double x = 0.0, double y = 0.0) {       this->x = x;       this->y = y;     }     Complex operator-(const Complex &b) const {       return Complex(x - b.x, y - b.y);     }     Complex operator+(const Complex &b) const {       return Complex(x + b.x, y + b.y);     }     Complex operator*(const Complex &b) const {       return Complex(x * b.x - y * b.y, x * b.y + y * b.x);     }   };   void change(Complex y[], int len) {     for (int i = 1, j = len / 2; i < len - 1; i++) {       if (i < j) swap(y[i], y[j]);       int k = len / 2;       while (j >= k) {         j -= k;         k /= 2;       }       if (j < k) j += k;     }   }   void fft(Complex y[], int len, int on) {     change(y, len);     for (int h = 2; h <= len; h <<= 1) {       Complex wn(cos(-on * 2 * pi / h), sin(-on * 2 * pi / h));       for (int j = 0; j < len; j += h) {         Complex w(1, 0);         for (int k = j; k < j + h / 2; k++) {           Complex u = y[k];           Complex t = w * y[k + h / 2];           y[k] = u + t;           y[k + h / 2] = u - t;           w = w * wn;         }       }     }     if (on == -1)       for (int i = 0; i < len; i++) y[i].x /= len;   }   int n, m, k, i, j, p[30];   Complex A[N], B[N], C[N];   int a[N], b[N];   char sa[N], sb[N];   int main() {     for (i = 1; i <= 26; i++) scanf( %d , &p[i]);     scanf( %s %s , sa, sb);     m = strlen(sa), n = strlen(sb);     for (i = 0, j = m - 1; i < j; i++, j--) swap(sa[i], sa[j]);     for (i = 0; i < m; i++) a[i] = sa[i] -  a  + 1;     for (i = 0; i < n; i++) b[i] = sb[i] -  a  + 1;     for (k = 1; k < n; k <<= 1)       ;     for (i = 0; i < k; i++)       A[i] = Complex(2 * a[i] * p[a[i]] * (a[i] + p[a[i]]), 0),       B[i] = Complex(b[i], 0);     for (fft(A, k, 1), fft(B, k, 1), i = 0; i < k; i++) C[i] = C[i] - A[i] * B[i];     for (i = 0; i < k; i++)       A[i] = Complex(a[i] * a[i] + p[a[i]] * p[a[i]] + 4 * a[i] * p[a[i]], 0),       B[i] = Complex(b[i] * b[i], 0);     for (fft(A, k, 1), fft(B, k, 1), i = 0; i < k; i++) C[i] = C[i] + A[i] * B[i];     for (i = 0; i < k; i++)       A[i] = Complex(2 * (a[i] + p[a[i]]), 0),       B[i] = Complex(b[i] * b[i] * b[i], 0);     for (fft(A, k, 1), fft(B, k, 1), i = 0; i < k; i++) C[i] = C[i] - A[i] * B[i];     for (i = 0; i < k; i++) {       if (i < m)         A[i] = Complex(1, 0);       else         A[i] = Complex(0, 0);       B[i] = Complex(b[i] * b[i] * b[i] * b[i], 0);     }     for (fft(A, k, 1), fft(B, k, 1), i = 0; i < k; i++) C[i] = C[i] + A[i] * B[i];     for (i = 0; i < k; i++) {       A[i] = Complex(a[i] * a[i] * p[a[i]] * p[a[i]], 0);       if (i < n)         B[i] = Complex(1, 0);       else         B[i] = Complex(0, 0);     }     for (fft(A, k, 1), fft(B, k, 1), i = 0; i < k; i++) C[i] = C[i] + A[i] * B[i];     fft(C, k, -1);     for (int i = 0; i < n - m + 1; i++)       printf( %d , ((long long)(C[i + m - 1].x + 0.5) == 0));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int& n) {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       x = (x << 1) + (x << 3) + ch -  0 , ch = getchar();     n = x * f;   }   int outputarray[20];   inline void write(int k) {     int num = 0;     if (k < 0) putchar( - ), k = -k;     do {       outputarray[++num] = k % 10, k /= 10;     } while (k);     while (num) putchar(outputarray[num--] +  0 );     puts(  );   }   const int N = 50;   int n, m, sg[N][N][N][N][2];   char s[N][N];   int dfs(int x0, int x2, int y0, int y2, int type) {     if (sg[x0][x2][y0][y2][type] >= 0) return sg[x0][x2][y0][y2][type];     int a[N * N];     memset(a, 0, sizeof(a));     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (((i + j) & 1) == type) {           int x = i + j, y = i - j + m;           if (x >= x0 && x < x2 && y >= y0 && y < y2) {             int tmp = 0;             if (s[i][j] ==  L )               tmp = dfs(x0, x, y0, y2, type) ^ dfs(x + 1, x2, y0, y2, type);             if (s[i][j] ==  R )               tmp = dfs(x0, x2, y0, y, type) ^ dfs(x0, x2, y + 1, y2, type);             if (s[i][j] ==  X )               tmp = dfs(x0, x, y0, y, type) ^ dfs(x0, x, y + 1, y2, type) ^                     dfs(x + 1, x2, y0, y, type) ^ dfs(x + 1, x2, y + 1, y2, type);             a[tmp] = 1;           }         }     while (a[++sg[x0][x2][y0][y2][type]])       ;     return sg[x0][x2][y0][y2][type];   }   int main() {     memset(sg, -1, sizeof(sg));     read(n), read(m);     for (int i = 0; i < n; i++) scanf( %s , s[i]);     if (dfs(0, n + m, 0, n + m, 0) ^ dfs(0, n + m, 0, n + m, 1))       puts( WIN );     else       puts( LOSE );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, ans = 0;     cin >> n >> m;     while (n != m) {       if (n < m) {         if (m % 2 == 0)           m /= 2, ans++;         else           m++, ans++;       } else {         ans++;         m++;       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int arr[4005];   int nxt[4005], prv[4005];   map<int, int> M;   map<int, int> Q;   int main() {     int n;     cin >> n;     int ans = 0;     for (int i = 0; i < n; ++i) {       cin >> arr[i];       if (M.find(arr[i]) != M.end()) {         nxt[M[arr[i]]] = i;         prv[i] = M[arr[i]];         Q[arr[i]]++;         ans = max(ans, Q[arr[i]]);       } else {         prv[i] = -1;         Q[arr[i]]++;         ans = max(ans, Q[arr[i]]);       }       M[arr[i]] = i;       nxt[i] = -1;     }     for (int i = 0; i < n; ++i) {       if (prv[i] != -1) {         continue;       }       for (int j = i + 1; j < n; ++j) {         if (prv[j] != -1) {           continue;         }         int cur = 2;         int t1 = i, t2 = j;         while (t1 != -1 && t2 != -1) {           if (t1 < t2) {             if (nxt[t1] > t2) {               ++cur;             }             t1 = nxt[t1];           } else {             if (nxt[t2] > t1) {               ++cur;             }             t2 = nxt[t2];           }         }         ans = max(ans, cur);       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int b = 3e6;   long long a[b];   void ans(long long x, long long y, long long k, long long i) {     long long sidl = k + k * y, ans = k;     long long ez1 = (sidl - 3 + x) / (x - 1);     ans += ez1;     a[i] = ans;   }   int main() {     long long t, x, y, k, j;     cin >> t;     for (int i = 0; i < t; i++) {       cin >> x >> y >> k;       ans(x, y, k, i);     }     for (int i = 0; i < t; i++) cout << a[i] << endl;     return 0;   };


#include <bits/stdc++.h>   using namespace std;   int test() {     long long n;     cin >> n;     long long cL = 0, cR = 0;     long long si = 3 * 100001;     long long *right = new long long[si], *left = new long long[si];     for (long long i = 0; i < si; i++) {       right[i] = 0;       left[i] = 0;     }     long long prop = 0, l = 0, r = 0;     string temp;     for (long long i = 0; i < n; i++) {       cin >> temp;       long long tempS = temp.size();       for (long long i = 0; i < tempS; i++) {         if (temp[i] ==  ( ) {           cL++;           l++;         } else {           cR++;           if (l != 0)             l--;           else             r++;         }       }       if (r != 0 && l != 0) {       } else if (cR == cL)         prop++;       else if (cR > cL)         right[cR - cL]++;       else         left[cL - cR]++;       l = 0;       r = 0;       cL = 0;       cR = 0;     }     long long S = 0;     for (long long i = 0; i < si; i++) {       if (right[i] != 0 && left[i] != 0) {         S += right[i] * left[i];       }     }     S += prop * prop;     cout << S;     return 0;   }   int hacker() { return 0; }   int main() {     while (true) {       return test();       cout <<   n n-- ;       hacker();       cout <<   n n n----------------------------- n n n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-6;   double n, r;   bool check(double R) {     double x1 = r + R;     double y1 = 0;     double x2 = cos(2 * acos(-1) / n) * (r + R);     double y2 = sin(2 * acos(-1) / n) * (r + R);     double len = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));     if (len <= 2 * R)       return true;     else       return false;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> r;     double l1 = 0, r1 = 10000;     for (int i = 0; i < 500; i++) {       double mm = (l1 + r1) / 2.0;       if (check(mm))         r1 = mm;       else         l1 = mm;     }     cout.fixed;     cout.precision(15);     cout << l1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c;     cin >> a;     cin >> b;     cin >> c;     int d = a + b - c;     int e = b + c - a;     int f = a + c - b;     if (d < 0 || e < 0 || f < 0 || e % 2 || d % 2 || f % 2) {       cout <<  Impossible ;       return 0;     }     cout << d / 2 <<     << e / 2 <<     << f / 2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int l, r, num;   } a[100005];   int n, ans[100005];   bool cmp(node x, node y) {     if (x.l == y.l) return x.r < y.r;     return x.l < y.l;   }   void solve() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i].l >> a[i].r;       a[i].num = i + 1;     }     sort(a + 1, a + n + 1, cmp);     set<int> F;     int mx = -1;     for (int i = 1; i <= n - 1; i++) {       F.insert(a[i].num);       mx = max(mx, a[i].r);       if (mx < a[i + 1].l) {         for (int j = 1; j <= n; j++) {           if (F.count(a[j].num))             ans[a[j].num] = 1;           else             ans[a[j].num] = 2;         }         for (int j = 2; j <= n + 1; j++) cout << ans[j] <<    ;         cout << endl;         return;       }     }     cout << -1 << endl;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int T;     cin >> T;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, a, b, c, d, e, f, g, h;     cin >> n;     vector<long long int> values(n), kalues(n), malues;     for (int i = 0; i < n; i++) {       cin >> values[i];       cin >> kalues[i];       kalues[i]--;       malues.push_back(values[i]);       malues.push_back(kalues[i]);     }     sort(values.begin(), values.end());     sort(kalues.begin(), kalues.end());     sort(malues.begin(), malues.end());     long long int mini = -5, year = -1;     malues.erase(unique(malues.begin(), malues.end()), malues.end());     for (int i = 0; i < 2 * n; i++) {       long long int helebele =           lower_bound(kalues.begin(), kalues.end(), malues[i]) - kalues.begin();       helebele += n - (upper_bound(values.begin(), values.end(), malues[i]) -                        values.begin());       if (mini < n - helebele) mini = n - helebele, year = malues[i];     }     cout << year <<     << mini << endl;   }


#include <bits/stdc++.h>   using namespace std;   char s[55];   int l;   int df[55][10];   int f(int pos, int c) {     if (pos == l) return 1;     if (df[pos][c] != -1) return df[pos][c];     int res = 0;     int nn = c + s[pos];     res += f(pos + 1, nn / 2);     if (nn & 1) res += f(pos + 1, (nn + 1) / 2);     return df[pos][c] = res;   }   bool good() {     for (int i = 1; i < l; i++) {       int nn = s[i] + s[i - 1];       if (nn & 1) {         if (nn / 2 != s[i] && (nn + 1) / 2 != s[i]) return false;       } else {         if (nn / 2 != s[i]) return false;       }     }     return true;   }   int main() {     memset(df, -1, sizeof(df));     scanf( %s , s);     l = strlen(s);     for (int i = 0; i < l; i++) s[i] -=  0 ;     long long ans = 0;     for (int i = 0; i < 10; i++) ans += f(1, i);     if (good()) ans--;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   const int INF = 1e9;   using namespace std;   int R, C, frow, fcol, row, col, srow, scol, nxcol, nxrow;   int ans, intest[1005][1005], goal;   char grid[1005][1005];   queue<pair<int, int> > q;   const int dr[4] = {-1, 0, 1, 0};   const int dc[4] = {0, -1, 0, 1};   bool inside(int r, int c) { return r > 0 && c > 0 && r <= R && c <= C; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> R >> C;     for (int r = 1; r <= R; r++) {       for (int c = 1; c <= C; c++) {         cin >> grid[r][c];         intest[r][c] = INF;         if (grid[r][c] ==  S )           srow = r, scol = c;         else if (grid[r][c] ==  E )           frow = r, fcol = c;       }     }     intest[frow][fcol] = 0;     q.push(make_pair(frow, fcol));     while (!q.empty()) {       pair<int, int> cur = q.front();       q.pop();       row = cur.first;       col = cur.second;       for (int i = 0; i <= 3; i++) {         nxrow = row + dr[i];         nxcol = col + dc[i];         if (!inside(nxrow, nxcol)) continue;         if (grid[nxrow][nxcol] ==  T ) continue;         if (intest[nxrow][nxcol] != INF) continue;         intest[nxrow][nxcol] = intest[row][col] + 1;         q.push(make_pair(nxrow, nxcol));       }     }     goal = intest[srow][scol];     for (int i = 1; i <= R; i++) {       for (int j = 1; j <= C; j++) {         if (grid[i][j] -  0  > 0 && grid[i][j] < 58) {           srow = i, scol = j;           if (intest[srow][scol] <= goal) ans += (grid[i][j] -  0 );         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     for (int i = 0; i < n; i++) cout << a[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int x, y, z;   int _x, _y, _z;   int a[6];   long long int ans = 0LL;   int main() {     ios::sync_with_stdio(false);     cin >> x >> y >> z;     cin >> _x >> _y >> _z;     for (int i = 0; i < 6; i++) cin >> a[i];     if (y < 0) ans += a[0];     if (y > _y) ans += a[1];     if (z < 0) ans += a[2];     if (z > _z) ans += a[3];     if (x < 0) ans += a[4];     if (x > _x) ans += a[5];     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int SZ = 8e6 + 10;   const int INF = 1e9 + 10;   const int mod = 998244353;   const long double eps = 1e-8;   long long read() {     long long n = 0;     char a = getchar();     bool flag = 0;     while (a >  9  || a <  0 ) {       if (a ==  - ) flag = 1;       a = getchar();     }     while (a <=  9  && a >=  0 ) {       n = n * 10 + a -  0 , a = getchar();     }     if (flag) n = -n;     return n;   }   int a[SZ], f[1010][1010], b[SZ], c[SZ];   int main() {     int n = read(), k = read();     for (int i = 1; i <= n; i++) a[i] = read();     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         b[++b[0]] = a[j] - a[i];       }     }     sort(b + 1, b + 1 + b[0]);     b[0] = unique(b + 1, b + 1 + b[0]) - b - 1;     int ans = 0;     for (int j = 1; j <= b[0]; j++) {       int minx = b[j];       if (minx > 1e5 / (k - 1)) break;       for (int i = 1; i <= n; i++) f[i][1] = f[i - 1][1] + 1;       for (int l = 2; l <= k; l++) {         for (int i = 2, lst = 0; i <= n; i++) {           while (lst < i && a[lst + 1] <= a[i] - minx) lst++;           f[i][l] = f[lst][l - 1];         }         for (int i = 1; i <= n; i++) (f[i][l] += f[i - 1][l]) %= mod;       }       c[j] = f[n][k];     }     for (int j = 1; j <= b[0]; j++)       (ans += 1ll * b[j] * (c[j] - c[j + 1] + mod) % mod) %= mod;     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int book[3];   char s[105], rs[105];   int main() {     int t, ans, n;     scanf( %d , &t);     for (int i = 0; i < t; i++) {       scanf( %d , &n);       ans = 0;       memset(rs,   0 , sizeof(rs));       for (int i = 0; i < 3; i++) {         scanf( %d , &book[i]);       }       scanf( %s , s);       for (int i = 0; i < n; i++) {         if (s[i] ==  R ) {           if (book[1] > 0) {             ans++;             book[1]--;             rs[i] =  P ;           }         } else if (s[i] ==  P ) {           if (book[2] > 0) {             ans++;             book[2]--;             rs[i] =  S ;           }         } else {           if (book[0] > 0) {             ans++;             book[0]--;             rs[i] =  R ;           }         }       }       if (ans >= (n + 1) / 2) {         int k = 0;         for (int i = 0; i < n; i++) {           if (rs[i] ==   0 ) {             while (book[k] == 0) k++;             if (k == 0)               rs[i] =  R ;             else if (k == 1)               rs[i] =  P ;             else               rs[i] =  S ;             book[k]--;           }         }         printf( YES n%s n , rs);       } else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   long long le[N], ri[N], mid, lee, rii;   long long a[N], b[N], c[N], mp[35], ans, n, answ, idx, idx1, pw[35], bb;   int main() {     std::ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     pw[0] = 1;     for (int i = 1; i <= 35; i++) {       pw[i] = pw[i - 1] * 2;     }     for (int i = 0; i <= 30; i++) {       for (int j = 1; j <= n; j++) {         b[j] += a[j] & pw[i];         c[j] = b[j];       }       std::stable_sort(c + 1, c + n + 1);       idx = n;       for (int j = 1; j <= n; j++) {         idx = max(idx, j + 1LL);         bb = 0;         while (c[j] + c[idx] >= pw[i] && idx > j) {           idx--;           bb = 1;         }         if (bb == 1) idx++;         if (c[j] + c[idx] < pw[i] || idx > n)           le[j] = -1;         else           le[j] = idx;       }       idx = n;       for (int j = 1; j <= n; j++) {         idx = max(idx, j + 1LL);         while (c[j] + c[idx] >= pw[i + 1] && idx > j + 1) {           idx--;         }         if (c[j] + c[idx] >= pw[i + 1] || idx > n)           ri[j] = -1;         else           ri[j] = idx;       }       for (int j = 1; j <= n; j++) {         if (le[j] != -1 && ri[j] != -1) mp[i] += ri[j] - le[j] + 1;       }       idx = n;       for (int j = 1; j <= n; j++) {         idx = max(idx, j + 1LL);         bb = 0;         while (c[j] + c[idx] >= pw[i + 1] + pw[i] && idx > j) {           bb = 1;           idx--;         }         if (bb == 1) idx++;         if (c[j] + c[idx] < pw[i + 1] + pw[i] || idx > n)           le[j] = -1;         else           le[j] = idx;       }       idx = n;       for (int j = 1; j <= n; j++) {         idx = max(idx, j + 1LL);         while (c[j] + c[idx] >= pw[i + 2] && idx > j + 1) {           idx--;         }         if (c[j] + c[idx] >= pw[i + 2] || idx > n)           ri[j] = -1;         else           ri[j] = idx;       }       for (int j = 1; j <= n; j++) {         if (le[j] != -1 && ri[j] != -1) mp[i] += ri[j] - le[j] + 1;       }     }     for (int i = 0; i <= 30; i++) {       if (mp[i] % 2) answ += (1LL << i);     }     cout << answ << endl;   }


#include <bits/stdc++.h>   using namespace std;   int a[20000], num[20000];   int dp[400];   int n, m, t, k, ans, maxm;   int main() {     scanf( %d%d , &n, &m);     maxm = 0;     ans = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       num[a[i]]++;       maxm = max(maxm, num[a[i]]);     }     t = min(n, m);     k = t;     int temp = 0;     while (--k) {       temp += n;       for (int i = 1; i <= n; i++) a[temp + i] = a[i];     }     for (int i = 1; i <= n * t; i++) {       dp[a[i]]++;       for (int j = a[i] - 1; j >= 0; j--) dp[a[i]] = max(dp[a[i]], dp[j] + 1);       ans = max(dp[a[i]], ans);     }     ans += maxm * (m - t);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   const int inf = 1500000000;   const int d = 1e9 + 7;   using namespace std;   struct triple {     int a;     int b;     int c;     bool flag;     triple() { flag = true; }   };   int main() {     int n;     cin >> n;     vector<triple> t(n - 1, triple());     vector<int> adj[n + 1];     for (int i = 1; i <= n - 2; i++) {       cin >> t[i].a >> t[i].b >> t[i].c;       adj[t[i].a].push_back(i);       adj[t[i].b].push_back(i);       adj[t[i].c].push_back(i);     }     int first;     triple f;     for (int i = 1; i <= n; i++) {       if (adj[i].size() == 1) {         first = i;         f = t[adj[i][0]];         break;       }     }     int second;     if (adj[f.a].size() == 2) {       second = f.a;     }     if (adj[f.b].size() == 2) {       second = f.b;     }     if (adj[f.c].size() == 2) {       second = f.c;     }     vector<int> perm;     perm.push_back(first);     perm.push_back(second);     while (perm.size() != n) {       int third;       for (int i = 0; i < adj[first].size(); i++) {         int ind = adj[first][i];         if (t[ind].flag == true) {           t[ind].flag = false;           third = (t[ind].a + t[ind].b + t[ind].c) - first - second;           perm.push_back(third);           first = second;           second = third;           break;         }       }     }     for (int i = 0; i < perm.size(); i++) cout << perm[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int k;   string s;   int main() {     cin >> k;     cin >> s;     int n = s.length();     for (int i = 0; i < n; ++i) {       if (s[i] ==  ?  && s[n - i - 1] ==  ? ) {         continue;       } else if (s[i] ==  ? ) {         s[i] = s[n - i - 1];       } else if (s[n - i - 1] ==  ? ) {         s[n - i - 1] = s[i];       } else if (s[i] != s[n - i - 1]) {         cout <<  IMPOSSIBLE ;         return 0;       }     }     bool flag[26];     memset(flag, 0, sizeof(flag));     for (int i = 0; i < n; ++i) {       if (s[i] !=  ? ) {         flag[s[i] -  a ] = true;       }     }     int miss = 0;     for (int c = 0; c < k; ++c) {       if (!flag[c]) {         ++miss;       }     }     int have = 0;     for (int i = 0; i <= n - i - 1; ++i) {       if (s[i] ==  ? ) {         ++have;       }     }     if (miss > have) {       cout <<  IMPOSSIBLE ;       return 0;     }     for (int i = 0; i < n; ++i) {       if (s[i] ==  ? ) {         if (have > miss) {           s[i] = s[n - i - 1] =  a ;         } else {           for (int c = 0; c < k; ++c) {             if (!flag[c]) {               s[i] = s[n - i - 1] = c +  a ;               flag[c] = true;               break;             }           }           --miss;         }         --have;       }     }     cout << s << endl;     return 0;   }


#include <bits/stdc++.h>   const long double EPS = 1e-10;   const long long int MOD = 1000000007ll;   const long long int mod1 = 1000000009ll;   const long long int mod2 = 1100000009ll;   int INF = (int)1e9;   long long int INFINF = (long long int)1e18;   int debug = 0;   long double PI = acos(-1.0);   using namespace std;   long long int bit_count(long long int _x) {     long long int _ret = 0;     while (_x) {       if (_x % 2 == 1) _ret++;       _x /= 2;     }     return _ret;   }   long long int bit(long long int _mask, long long int _i) {     return (_mask & (1 << _i)) == 0 ? 0 : 1;   }   long long int powermod(long long int _a, long long int _b,                          long long int _m = MOD) {     long long int _r = 1;     while (_b) {       if (_b % 2 == 1) _r = (_r * _a) % _m;       _b /= 2;       _a = (_a * _a) % _m;     }     return _r;   }   long long int add(long long int a, long long int b, long long int m = MOD) {     long long int x = a + b;     while (x >= m) x -= m;     while (x < 0) x += m;     return x;   }   long long int sub(long long int a, long long int b, long long int m = MOD) {     long long int x = a - b;     while (x < 0) x += m;     while (x >= m) x -= m;     return x;   }   long long int mul(long long int a, long long int b, long long int m = MOD) {     long long int x = a * 1ll * b;     x %= m;     if (x < 0) x += m;     return x;   }   int N, M, K;   vector<int> G[1010];   bool spec[1010];   bool vis[1010];   int tot, cnt;   vector<int> sizes;   void dfs(int u) {     tot++;     cnt++;     vis[u] = true;     for (int v : G[u]) {       if (!vis[v]) {         dfs(v);       }     }   }   int main() {     if (0) {       freopen( input.txt ,  r , stdin);       debug = 1;     }     srand(time(NULL));     scanf( %d , &N);     scanf( %d , &M);     scanf( %d , &K);     for (int i = 1; i <= K; i++) {       int x;       scanf( %d , &x);       spec[x] = true;     }     for (int i = 1; i <= M; i++) {       int u, v;       scanf( %d , &u);       scanf( %d , &v);       G[u].push_back(v);       G[v].push_back(u);     }     for (int i = 1; i <= N; i++) {       if (spec[i]) {         cnt = 0;         dfs(i);         sizes.push_back(cnt);       }     }     sort(sizes.begin(), sizes.end());     reverse(sizes.begin(), sizes.end());     sizes[0] += (N - tot);     long long int ans = 0;     for (int it : sizes) {       ans += (it * (it - 1)) / 2;     }     cout << ans - M;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Q>   void inin(Q &x) {     x = 0;     int f = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = 1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     x = f ? -x : x;   }   int n, a[100010], b[100010];   long long sum[100010], ans[100010], tot[100010];   int erfen(int x, int l, int r) {     int ll = l, rr = r, mid;     while (ll < rr) {       mid = (ll + rr) >> 1;       if (sum[mid] - sum[l - 1] < x)         ll = mid + 1;       else         rr = mid;     }     if (ll == n && sum[n] - sum[l - 1] < x) return n + 1;     return ll;   }   int main() {     inin(n);     for (int i = (1); i <= (n); i++) inin(a[i]);     for (int i = (1); i <= (n); i++) inin(b[i]), sum[i] = sum[i - 1] + b[i];     for (int i = (1); i <= (n); i++) {       int x = erfen(a[i], i, n);       tot[x]--;       ans[x] += a[i] - (sum[x - 1] - sum[i - 1]);       tot[i] += tot[i - 1] + 1;       ans[i] += 1LL * b[i] * tot[i];     }     for (int i = (1); i <= (n); i++) printf( %lld  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long long sze = 1e5 + 2;   const long long inf = 0x3f3f3f3f;   void solve() {     long long n, k;     cin >> n;     cin >> k;     long long swap = k;     long long last = n;     long long i = 1;     bool turn = true;     while (swap) {       if (turn) {         cout << i <<    ;         i++;         turn = false;       } else {         cout << last <<    ;         last--;         turn = true;       }       swap--;     }     if (k % 2 == 0) {       while (last >= i) {         cout << last <<    ;         last--;       }       return;     }     while (i <= last) {       cout << i <<    ;       i++;     }   }   signed main() {     long long test = 1;     while (test--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     vector<pair<int, int> > v, re;     scanf( %d %d  , &a, &b);     v.push_back(make_pair(a, b));     scanf( %d %d  , &a, &b);     v.push_back(make_pair(a, b));     scanf( %d %d  , &a, &b);     v.push_back(make_pair(a, b));     sort(v.begin(), v.end());     pair<int, int> A = v[0], B = v[1], C = v[2];     int x = A.first, y = A.second;     re.push_back(make_pair(x, y));     if (C.second > A.second) {       int dy = max(min(B.second, C.second), A.second);       for (int i = A.second + 1; i <= dy; i++) {         re.push_back(make_pair(x, i));       }       y = dy;       for (int i = A.first + 1; i <= C.first; i++) {         re.push_back(make_pair(i, y));       }       x = C.first;       for (int i = y + 1; i <= C.second; i++) {         re.push_back(make_pair(x, i));       }     } else {       int dy = min(max(B.second, C.second), A.second);       for (int i = A.second - 1; i >= dy; i--) {         re.push_back(make_pair(x, i));       }       y = dy;       for (int i = A.first + 1; i <= C.first; i++) {         re.push_back(make_pair(i, y));       }       x = C.first;       for (int i = y - 1; i >= C.second; i--) {         re.push_back(make_pair(x, i));       }     }     if (B.second > y) {       for (int i = y + 1; i <= B.second; i++) {         re.push_back(make_pair(B.first, i));       }     } else {       for (int i = y - 1; i >= B.second; i--) {         re.push_back(make_pair(B.first, i));       }     }     printf( %d n , re.size());     for (int i = 0; i < re.size(); i++) {       printf( %d %d n , re[i].first, re[i].second);     }   }


#include <bits/stdc++.h>   using namespace std;   int len, pos;   void insert(int x) {     len++;     if (x <= pos) pos++;   }   void erase(int x) {     if (x >= pos)       len = x;     else       len -= x, pos -= x;   }   int main() {     int m, t;     scanf( %d%d%d%d , &len, &pos, &m, &t);     while (t--) {       int type, x;       scanf( %d%d , &type, &x);       if (type == 1)         insert(x);       else         erase(x);       printf( %d %d n , len, pos);     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int sz = 5e5 + 10;   int ar[sz];   int main() {     int n, ans;     while (cin >> n) {       for (int i = 0; i < n; i++) cin >> ar[i];       sort(ar, ar + n);       ans = n;       for (int i = n - 1, j = (n / 2) - 1; j >= 0; j--) {         if (ar[i] >= ar[j] * 2) {           ans--;           i--;         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int DX[8] = {1, -1, 0, 0, 1, 1, -1, -1};   const int DY[8] = {0, 0, 1, -1, 1, -1, 1, -1};   const int intmax = 0x7fffffff;   map<int, int> mp;   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int a;       scanf( %d , &a);       mp[a]++;     }     map<int, int>::iterator it = mp.begin();     int deg = (it->second) * 2;     int last = it->first;     it++;     for (; it != mp.end(); it++) {       if ((it->first) != last + 1) {         puts( NO );         return 0;       }       if (!deg) {         puts( NO );         return 0;       }       deg = it->second * 2 - deg;       if (deg < 0) {         puts( NO );         return 0;       }       last = it->first;     }     if (!deg)       puts( YES );     else       puts( NO );   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = acos(-1);   const long double eps = 0.0000000001;   const long long INF = 0x3fffffffffffffff;   long long a[200005], sum[200005], n, q, x, y;   long long lowbit(long long now) { return now & -now; }   void add(long long pos, long long v) {     while (pos <= n) {       sum[pos] += v;       pos += lowbit(pos);     }   }   long long query(long long now) {     long long res = 0;     while (now) {       res += sum[now];       now ^= lowbit(now);     }     return res;   }   long long findnext(long long now) {     long long l = now + 1, r = n + 1, v = query(now);     while (l < r) {       long long mid = (l + r) >> 1;       if (query(mid) < v << 1) {         l = mid + 1;       } else {         r = mid;       }     }     return l;   }   signed main() {     ios::sync_with_stdio(false);     cin >> n >> q;     for (long long i = 1; i <= n; ++i) {       cin >> a[i];       add(i, a[i]);     }     for (long long i = 1; i <= q; ++i) {       cin >> x >> y;       add(x, -a[x]);       a[x] = y;       add(x, a[x]);       long long now = 1;       bool flag = false;       if (a[1] == 0) {         cout << 1 << endl;         continue;       }       while (now <= n) {         now = findnext(now);         if (now <= n && query(now) == a[now] << 1) {           cout << now << endl;           flag = true;           break;         }       }       if (!flag) {         cout << -1 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, p, q, b, l;   long long int gcd(long long int a, long long int c) {     if (!c) return a;     return gcd(c, a % c);   }   long long int read_ll() {     char ch;     long long int num = 0;     ch = getchar();     while (!isdigit(ch)) {       ch = getchar();     }     while (isdigit(ch)) {       num = num * 10 + (long long int)ch - (long long int) 0 ;       ch = getchar();     }     return num;   }   int main() {     scanf( %ld , &n);     while (n--) {       p = read_ll();       q = read_ll();       b = read_ll();       long long int d = gcd(p, q);       p = p / d;       q = q / d;       while (1) {         b = gcd(q, b);         if (b == 1) {           if (q == 1)             printf( Finite n );           else             printf( Infinite n );           break;         }         while (q % b == 0) q /= b;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y, z;     cin >> x >> y >> z;     int total = x - y;     if (total < 0) {       if (total + z < 0) {         cout <<  - ;       } else {         cout <<  ? ;       }     }     if (total > 0) {       if (total - z > 0) {         cout <<  + ;       } else {         cout <<  ? ;       }     }     if (total == 0) {       if (total + z == 0) {         cout <<  0 ;       } else {         cout <<  ? ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     cin >> n;     if (n == 1)       cout <<  -1 ;     else       cout << n <<     << n + 1 <<     << n * n + n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:100000000 )   int n;   double l;   pair<int, int> mas[1010];   int lst[1010];   double dp[1010];   bool check(double r) {     for (int i = 0; i <= n; i++) lst[i] = -1;     for (int i = 1; i <= n; i++) dp[i] = -1e15;     for (int i = 0; i < n; i++) {       if (lst[i] == -1 && i != 0) continue;       for (int j = i + 1; j <= n; j++) {         double p = sqrt(abs(l - (double)(mas[j].first - mas[i].first)));         if (dp[j] < dp[i] + (double)mas[j].second * r - p) {           lst[j] = i;           dp[j] = dp[i] + (double)mas[j].second * r - p;         }       }     }     if (lst[n] != -1 && dp[n] > -1e-9)       return true;     else       return false;   }   int main() {     scanf( %d %lf , &n, &l);     for (int i = 1; i <= n; i++) scanf( %d %d , &mas[i].first, &mas[i].second);     double l = 0, r = 1e9;     for (int i = 0; i < 100; i++) {       double av = (l + r) / 2.0;       if (check(av))         r = av;       else         l = av;     }     check(l);     vector<int> ans;     int cur = n;     while (cur != 0) {       ans.push_back(cur);       cur = lst[cur];     }     for (int i = (int)(ans).size() - 1; i >= 0; i--) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int m = 31;   const int maxn = 31;   long long n, k;   long long f[maxn + 5], g[maxn + 5];   void init() {     f[1] = g[1] = 1;     for (int i = 2; i <= m; i++) {       f[i] = (1LL << i * 2 - 2) + f[i - 1];       g[i] = (1LL << i) - 1 + g[i - 1];     }   }   int main() {     init();     long long T;     cin >> T;     while (T--) {       cin >> n >> k;       if (n > m) {         printf( YES %I64d n , n - 1);         continue;       }       int flg = -1;       for (int i = 0; i <= n; i++) {         if (k >= g[i] &&             k <= f[i] + f[n - i] * ((1ll << i) - 1) * ((1ll << i) - 1)) {           flg = i;           break;         }       }       if (~flg)         printf( YES %I64d n , n - flg);       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     int n, m;     cin >> n >> m;     cin >> s;     for (int i = 0; i < m; i++) {       int l, r;       char c1, c2;       scanf( %d %d %c %c , &l, &r, &c1, &c2);       for (int j = l - 1; j < r; j++) {         if (s[j] == c1) {           s[j] = c2;         }       }     }     cout << s << endl;     cin >> n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long h, n;     cin >> h >> n;     long long l = 1, r = (1LL << h), ret = 0, cur = 0, dir = 0;     while (cur < h) {       long long size = (1LL << (h - cur)), mid = (l + r) / 2;       if (dir == 0) {         if (n <= mid)           ++ret, r = mid, dir = 1;         else           ret += size, l = mid + 1;       } else {         if (n <= mid)           ret += size, r = mid;         else           ++ret, l = mid + 1, dir = 0;       }       ++cur;     }     cout << ret << endl;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx )   template <class T = int>   inline T readInt();   inline double readDouble();   inline int readUInt();   inline int readChar();   inline void readWord(char *s);   inline bool readLine(char *s);   inline bool isEof();   inline int getChar();   inline int peekChar();   inline bool seekEof();   inline void skipBlanks();   inline std::string readString();   template <class T>   inline void writeInt(T x, char end = 0, int len = -1);   inline void writeChar(int x);   inline void writeWord(const char *s);   inline void writeDouble(double x, int len = 0);   inline void flush();   static struct buffer_flusher_t {     ~buffer_flusher_t() { flush(); }   } buffer_flusher;   using namespace std;   template <typename T>   inline void pv(const std::vector<T> &vector, char separator =    ,                  char end =   n ) {     for (int i = 0; i < vector.size(); ++i) {       if (i != 0) {         writeChar(separator);       }       writeInt(vector[i]);     }     writeChar(end);   }   inline void init() {     srand(time(0));     ios_base::sync_with_stdio(0);     cin.tie(0);   }   static const int buf_size = 4 * 4096;   static unsigned char buf[buf_size];   static int buf_len = 0, buf_pos = 0;   inline string readString() {     const int MAX = 1e6 + 42;     static char buf[MAX];     readWord(buf);     return string(buf);   }   inline bool isEof() {     if (buf_pos == buf_len) {       buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);       if (buf_pos == buf_len) return 1;     }     return 0;   }   inline int getChar() { return isEof() ? -1 : buf[buf_pos++]; }   inline int peekChar() { return isEof() ? -1 : buf[buf_pos]; }   inline bool seekEof() {     int c;     while ((c = peekChar()) != -1 && c <= 32) buf_pos++;     return c == -1;   }   inline void skipBlanks() {     while (!isEof() && buf[buf_pos] <= 32U) buf_pos++;   }   inline int readChar() {     int c = getChar();     while (c != -1 && c <= 32) c = getChar();     return c;   }   inline int readUInt() {     int c = readChar(), x = 0;     while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar();     return x;   }   template <class T>   inline T readInt() {     int second = 1, c = readChar();     T x = 0;     if (c ==  - )       second = -1, c = getChar();     else if (c ==  + )       c = getChar();     while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar();     return second == 1 ? x : -x;   }   inline double readDouble() {     int second = 1, c = readChar();     double x = 0;     if (c ==  - ) second = -1, c = getChar();     while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar();     if (c ==  . ) {       c = getChar();       double coef = 1;       while ( 0  <= c && c <=  9 ) x += (c -  0 ) * (coef *= 1e-1), c = getChar();     }     return second == 1 ? x : -x;   }   inline void readWord(char *second) {     int c = readChar();     while (c > 32) *second++ = c, c = getChar();     *second = 0;   }   inline bool readLine(char *second) {     int c = getChar();     while (c !=   n  && c != -1) *second++ = c, c = getChar();     *second = 0;     return c != -1;   }   static int write_buf_pos = 0;   static char write_buf[buf_size];   inline void writeChar(int x) {     if (write_buf_pos == buf_size)       fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;     write_buf[write_buf_pos++] = x;   }   inline void flush() {     if (write_buf_pos)       fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;   }   template <class T>   inline void writeInt(T x, char end, int output_len) {     if (x < 0) writeChar( - ), x = -x;     char second[24];     int n = 0;     while (x || !n) second[n++] =  0  + x % 10, x /= 10;     while (n < output_len) second[n++] =  0 ;     while (n--) writeChar(second[n]);     if (end) writeChar(end);   }   inline void writeWord(const char *second) {     while (*second) writeChar(*second++);   }   inline void writeDouble(double x, int output_len) {     if (x < 0) writeChar( - ), x = -x;     int t = (int)x;     writeInt(t), x -= t;     writeChar( . );     for (int i = output_len - 1; i > 0; i--) {       x *= 10;       t = std::min(9, (int)x);       writeChar( 0  + t), x -= t;     }     x *= 10;     t = std::min(9, (int)(x + 0.5));     writeChar( 0  + t);   }   const int MAXN = 3e5 + 42;   const int MOD = 1e9 + 7;   void solve() {     int n = readInt();     vector<pair<char, int>> oper;     vector<int> pos;     for (int i = 0; i < 2 * n; ++i) {       char c = readChar();       if (false) cerr <<  c =   << c << endl;       if (c ==  + ) {         oper.push_back({ + , 0});         pos.push_back(oper.size() - 1);         continue;       } else {         int cost = readInt();         oper.push_back({ - , cost});         if (pos.empty()) {           writeWord( NO n );           return;         }         int x = pos.back();         if (false) cerr << x << endl;         pos.pop_back();         oper[x].second = cost;       }     }     set<int> have;     vector<int> ans;     for (auto [op, c] : oper) {       if (false) cerr << op <<     << c << endl;       if (op ==  + ) {         have.insert(c);         ans.push_back(c);       } else {         auto u = *have.begin();         if (u != c) {           writeWord( NO n );           return;         }         have.erase(have.begin());       }     }     writeWord( YES n );     pv(ans);   }   signed main() {     init();     init();     solve();   }


#include <bits/stdc++.h>   using ull = unsigned long long;   using ll = long long;   using ld = long double;   using namespace std;   const ll mod = 998244353;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int n, m, k, t;     cin >> n >> k >> m >> t;     deque<bool> cur(n);     cur[k - 1] = 1;     while (t--) {       int f, pos;       cin >> f >> pos;       if (f == 1) {         cur.insert(cur.begin() + (pos - 1), 0);         for (int i = 0; i < ((int)(cur).size()); i++) {           if (cur[i]) {             cout << ((int)(cur).size()) <<     << i + 1 <<   n ;             break;           }         }       } else {         int loc = -1;         for (int i = 0; i < ((int)(cur).size()); i++) {           if (cur[i]) {             loc = i + 1;             break;           }         }         if (loc <= pos)           while (((int)(cur).size()) > pos) cur.pop_back();         else           while (pos--) cur.pop_front();         for (int i = 0; i < ((int)(cur).size()); i++) {           if (cur[i]) {             cout << ((int)(cur).size()) <<     << i + 1 <<   n ;             break;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int maze[1500][1500];   pair<int, int> was[1500][1500];   int n, m;   int sr, sc;   inline int rem(int x, int y) {     x %= y;     return (x < 0) ? x + y : x;   }   bool dfs(int r, int c) {     int rm = rem(r, n), cm = rem(c, m);     if (maze[rm][cm] == 1) {       return was[rm][cm] != pair<int, int>(r, c);     }     int rmn, cmn;     if (maze[rm][cm] == 0) {       maze[rm][cm] = 1;       was[rm][cm] = pair<int, int>(r, c);       rmn = rem(r, n), cmn = rem(c - 1, m);       if (maze[rmn][cmn] != -1 && dfs(r, c - 1)) return true;       rmn = rem(r, n), cmn = rem(c + 1, m);       if (maze[rmn][cmn] != -1 && dfs(r, c + 1)) return true;       rmn = rem(r + 1, n), cmn = rem(c, m);       if (maze[rmn][cmn] != -1 && dfs(r + 1, c)) return true;       rmn = rem(r - 1, n), cmn = rem(c, m);       if (maze[rmn][cmn] != -1 && dfs(r - 1, c)) return true;     }     return false;   }   void TheSolution(istream& input, ostream& output) {     input >> n >> m;     string s;     memset(maze, -1, sizeof(maze));     for (int i = 0; i < n; i++) {       input >> s;       for (int j = 0; j < m; j++) {         if (s[j] ==  . ) maze[i][j] = 0;         if (s[j] ==  S ) {           sr = i;           sc = j;           maze[i][j] = 0;         }         was[i][j] = pair<int, int>(-1000000, -1000000);       }     }     output << (dfs(sr, sc) ?  Yes  :  No );   }   int main() {     TheSolution(cin, cout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   struct node {     int cnt;     node* bit[3];     node() {       bit[0] = bit[1] = NULL;       cnt = 0;     }   };   node* root;   void Insertt(int x) {     node* cur = root;     for (int i = 28; i >= 0; --i) {       if (cur->bit[(x >> i) & 1] == NULL) cur->bit[(x >> i) & 1] = new node();       cur = cur->bit[(x >> i) & 1];       ++(cur->cnt);     }   }   void Erasee(int x) {     node* cur = root;     for (int i = 28; i >= 0; --i) {       --(cur->bit[(x >> i) & 1]->cnt);       if (cur->bit[(x >> i) & 1]->cnt == 0) {         cur->bit[(x >> i) & 1] = NULL;         return;       } else         cur = cur->bit[(x >> i) & 1];     }   }   int Query(int p, int l) {     int res = 0;     node* cur = root;     for (int i = 28; i >= 0; --i) {       if (((l >> i) & 1) == 0) {         if (cur->bit[(p >> i) & 1] != NULL)           cur = cur->bit[(p >> i) & 1];         else           return res;       } else {         if (cur->bit[(p >> i) & 1] != NULL) res += cur->bit[(p >> i) & 1]->cnt;         if (cur->bit[(1 ^ ((p >> i) & 1))] != NULL)           cur = cur->bit[(1 ^ ((p >> i) & 1))];         else           return res;       }     }     return res;   }   int main() {     scanf( %d , &n);     root = new node();     for (int i = 1; i <= n; ++i) {       int type, p, l;       scanf( %d%d , &type, &p);       if (type == 1) Insertt(p);       if (type == 2) Erasee(p);       if (type == 3) {         scanf( %d , &l);         printf( %d n , Query(p, l));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long labs(long long a) { return a < 0 ? (-a) : a; }   long long max(long long a, long long b) { return a > b ? a : b; }   long long min(long long a, long long b) { return a < b ? a : b; }   struct Fenwik {     vector<long long> t;     long long n;     void Init(long long nn) {       n = nn;       t.assign(n, 0);     }     long long Sum(long long r) {       long long result = 0;       for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r];       return result;     }     void Inc(long long i, long long delta) {       for (; i < n; i = (i | (i + 1))) t[i] += delta;     }     long long Sum(long long l, long long r) { return Sum(r) - Sum(l - 1); }     void Init(const vector<long long>& a) {       Init(a.size());       for (int(i) = 0; (i) < (n); (i)++) Inc(i, a[i]);     }   };   vector<Fenwik> f;   vector<int> g;   vector<int> gn;   long long gs;   map<long long, pair<int, int> > ids;   long long h, m, n;   long long gt;   long long F(long long x) {     long long pos = (gn[gt] + x) % gs;     long long sum = 0;     if (pos < gn[gt]) {       sum = f[g[gt]].Sum(gn[gt], gs - 1) + f[g[gt]].Sum(0, pos);     } else {       sum = f[g[gt]].Sum(gn[gt], pos);     }     return x + 1 - sum;   }   long long UpperBound(long long l, long long r, long long f) {     while (l < r) {       long long m = (l + r) / 2;       if (!(f < F(m))) {         l = m + 1;       } else {         r = m;       }     }     return l;   }   long long res = 0;   void Add(long long id, long long t) {     gt = t;     long long lb = UpperBound(0, gs - 1, 0);     res += lb;     long long gg = g[t];     long long nn = (gn[t] + lb) % gs;     f[gg].Inc(nn, 1);     ids[id] = make_pair(gg, nn);   }   void Delete(long long id) { f[ids[id].first].Inc(ids[id].second, -1); }   int main() {     cin >> h >> m >> n;     g.resize(h, -1);     gn.resize(h, 0);     long long group = 0;     for (int(i) = 0; (i) < (h); (i)++) {       if (g[i] != -1) continue;       g[i] = group;       gs = 1;       for (long long j = 1; (i + j * m) % h != i; j++) {         g[(i + j * m) % h] = group;         gn[(i + j * m) % h] = j;         gs = j + 1;       }       group++;     }     f.resize(group);     for (int(i) = 0; (i) < (group); (i)++) {       f[i].Init(gs);     }     char c;     long long id;     long long t;     for (int(i) = 0; (i) < (n); (i)++) {       cin >> c >> id;       if (c ==  + ) {         cin >> t;         Add(id, t);       } else {         Delete(id);       }     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const int MAXN = 1e6 + 7;   const double EPS = 1e-8;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m;     cin >> n >> m;     vector<long long> cnt(m, 0);     long long d = n % m;     for (long long i = 0; i < m; i++) {       cnt[i * i % m] += n / m;       if (i && i <= d) {         cnt[i * i % m]++;       }     }     long long res = 0;     for (long long i = 0; i < m; i++) {       res += cnt[i] * cnt[(m - i) % m];     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   char buffer[200];   int main() {     int n;     string data;     while (scanf( %d , &n) != EOF) {       getchar();       for (int i = 0; i < n; i++) {         gets(buffer);         data = string(buffer);         int firstM = data.find( miao. );         int lastL = data.rfind( lala. );         if (firstM != data.npos &&             (lastL == data.npos || lastL != data.size() - 5) && firstM == 0) {           printf( Rainbow s n );         } else if ((firstM == data.npos || firstM != 0) && lastL != data.npos &&                    lastL == data.size() - 5) {           printf( Freda s n );         } else {           printf( OMG>.< I don t know! n );         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const long double PI = 3.1415926535898;   const long long LLMAX = LLONG_MAX;   int a[(int)2e5 + 5], b[(int)2e5 + 5], c[(int)2e5 + 5];   int n;   vector<vector<int> > v((int)2e5 + 5);   int type10 = 0;   int type01 = 0;   long long ans = 0;   int one[(int)2e5 + 5] = {0};   int zero[(int)2e5 + 5] = {0};   void solve(int parent, int node) {     if (b[node] != c[node]) {       if (b[node])         one[node]++;       else         zero[node]++;     }     if (parent) a[node] = min(a[node], a[parent]);     for (int i = 0; i < v[node].size(); i++) {       if (v[node][i] == parent)         continue;       else {         solve(node, v[node][i]);         one[node] += one[v[node][i]];         zero[node] += zero[v[node][i]];       }     }     int dec = min(one[node], zero[node]);     one[node] -= dec;     zero[node] -= dec;     ans += (long long)a[node] * 2 * dec;   }   int main() {     int x, y;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i] >> b[i] >> c[i];       if (b[i] == 0 && c[i] == 1)         type01++;       else if (b[i] == 1 && c[i] == 0)         type10++;     }     for (int i = 1; i < n; i++) {       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     solve(0, 1);     if (type01 != type10) {       cout << -1 <<   n ;     } else {       cout << ans <<   n ;     }   }


#include<bits/stdc++.h>   using namespace std;   #define mp make_pair   #define fi first   #define se second   #define pb push_back   #define pf push_front   #define ppb pop_back   #define ppf pop_front   #define sz(v) (v).size()   #define all(v) (v).begin(),(v).end()   #define REP(i,a,b) for(int (i)=(a);(i)<(b);(i)++)   #define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)   #define Time (double)clock()/CLOCKS_PER_SEC   typedef long long ll;   typedef unsigned long long ull;   typedef double db;   typedef long double ldb;   typedef pair<int,int> pi;   template <class T> void chmax(T &x,T y) {x=x>y?x:y;return;}   template <class T> void chmin(T &x,T y) {x=x<y?x:y;return;}   template <class T> T sqr(T x) {return x*x;}   template <class T> T gcd(T x,T y) {return y==0?x:gcd(y,x%y);}   template <class T> T lcm(T x,T y) {return x/gcd(x,y)*y;}   const int inf=1000000000,mod=1000000007;   const ll infll=1000000000000000000ll,modll=ll(mod);   const db maxdb=db(infll),eps=1e-14,PI=acos(-1.0);   //#define mydef   //#define LOCAL   #ifdef mydef   #define foreach(v,it) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)   #endif   #define debug   #define putnum(x) cout<<#x<< :  <<x<<endl   #define putvec(v) cout<<#v<< :  ;REP(i,0,sz(v)) cout<<v[i]<<   ;cout<<endl   template <class T> inline void read(T &x){    T w=1;x=0;char c=getchar();    while(c< 0 ||c> 9 ) {if(c== - )w=-1;c=getchar();}    while(c>= 0 &&c<= 9 ) {x=(x<<3)+(x<<1)+(c&15);c=getchar();}    x*=w;   }   template <class T> inline void write(T x) {    if(x<0) putchar( - ),x=~x+1;    if(x) write(x/10),putchar(x%10+ 0 );   }      const int maxn=200005;   int n;   ll sum[2],a[maxn];   vector<ll> odd,even;      void solve() {    odd.clear();    even.clear();    cin>>n;    for(int i=1;i<=n;i++) {     cin>>a[i];     if(a[i]&1ll) odd.pb(a[i]);     else even.pb(a[i]);    }    sort(all(odd));    sort(all(even));        sum[0]=sum[1]=0ll;    int p=0;    for(int i=0;i<n;i++) {     ll A=even.empty()?-infll:even.back();     ll B=odd.empty()?-infll:odd.back();       if(A>B) {      if(!p) sum[p]+=A;      even.ppb();     } else {      if(p) sum[p]+=B;      odd.ppb();     }     p^=1;    }    cout<<(sum[0]>sum[1]? Alice :(sum[0]==sum[1]? Tie : Bob ))<<endl;    return;   }      int main() {    #ifdef LOCAL     freopen( input.in , r ,stdin);     freopen( output.out , w ,stdout);    #endif    int T;    cin>>T;    while(T--) solve();    return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using vpi = vector<pair<int, int>>;   using pi = pair<int, int>;   const int nax = 1e5 + 7;   const int mod = 1e9 + 7;   const int inf = numeric_limits<int>::max() - (int)9e6;   struct monotonic_queue {     queue<int> qu;     deque<int> mx, mn;     void push(int v) {       qu.push(v);       while (mx.size() && mx.back() < v) mx.pop_back();       mx.push_back(v);       while (mn.size() && mn.back() > v) mn.pop_back();       mn.push_back(v);     }     void pop() {       if (mx.front() == qu.front()) mx.pop_front();       if (mn.front() == qu.front()) mn.pop_front();       qu.pop();     }     int front() const { return qu.front(); }     int max() const { return mx.front(); }     int min() const { return mn.front(); }     int size() const { return qu.size(); }     bool empty() { return qu.empty(); }   };   int n;   void test() {     cin >> n;     int s, l;     cin >> s >> l;     vi v(n);     for (int i = 0; i < (int)n; i++) cin >> v[i];     monotonic_queue q;     vi g(n, n);     for (int i = n - 1, j = n - 1; i >= 0; i--) {       while (j >= 0 and (q.empty() or (((q.max() - q.min()) <= s)))) {         q.push(v[j--]);       }       g[i] = (((q.max() - q.min()) <= s) ? j : j + 1);       if (((int)(q).size())) q.pop();     }     while (((int)(q).size())) q.pop();     vi dp(n + 1, inf);     dp[0] = 0;     for (int i = l, j = 0; i <= n; i++) {       q.push(dp[i - l]);       while (((int)(q).size()) and j <= g[i - 1]) q.pop(), j++;       if (((int)(q).size())) dp[i] = q.min() + 1;     }     cout << (dp[n] >= inf ? -1 : dp[n]);   }   signed main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     int T = 1;     while (T--) {       test();       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int a[20];   long long ans, p[] = {1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29}, limit = 1e18;   void dfs(int num, int len) {     if (num == 1) {       long long res = 1;       for (int i = 1; i <= len; ++i) {         for (int j = 1; j < a[i]; ++j) {           if (limit / p[i] <= res) {             return;           }           res *= p[i];         }       }       ans = min(ans, res);       return;     }     for (int i = 2; i <= num; ++i) {       if (num % i == 0) {         a[len + 1] = i;         dfs(num / i, len + 1);       }     }   }   int main() {     int n;     cin >> n;     if (n == 1) {       cout << 1 << endl;       return 0;     } else if (n == 2) {       cout << 2 << endl;       return 0;     }     ans = limit;     for (int i = 1; i <= n; ++i) {       if (n % i == 0) {         a[1] = i;         dfs(n / i, 1);       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long oo = 0x3f3f3f3f3f3f3f3fLL;   struct Edge {     int u, v;     long long cap[2], flow;   };   vector<int> d, act;   bool bfs(int s, int t, vector<vector<int>>& adj, vector<Edge>& E,            long long lim = 1) {     queue<int> Q;     d = vector<int>(adj.size(), -1);     d[t] = 0;     Q.push(t);     for (Q.push(t); Q.size() and d[s] == -1; Q.pop()) {       int u = Q.front();       for (int i = (0); i < int(adj[u].size()); i++) {         int e = adj[u][i], v = (E[e].u ^ E[e].v ^ u);         if (((E[e].u == v) ? E[e].cap[0] - E[e].flow : E[e].cap[1] + E[e].flow) >=                 lim and             d[v] == -1) {           d[v] = d[u] + 1;           Q.push(v);         }       }     }     return d[s] >= 0;   }   long long dfs(int u, int t, long long bot, vector<vector<int>>& adj,                 vector<Edge>& E) {     if (bot == 0) return 0;     if (u == t) return bot;     for (; act[u] < int(adj[u].size()); ++act[u]) {       int e = adj[u][act[u]];       if (d[u] == d[(E[e].u ^ E[e].v ^ u)] + 1) {         long long inc = dfs((E[e].u ^ E[e].v ^ u), t,                             min(bot, ((E[e].u == u) ? E[e].cap[0] - E[e].flow                                                     : E[e].cap[1] + E[e].flow)),                             adj, E);         if (inc) {           E[e].flow += ((E[e].u == u) ? (inc) : (-(inc)));           return inc;         }       }     }     return 0;   }   long long maxflow(int s, int t, vector<vector<int>>& adj, vector<Edge>& E,                     int F = 0) {     for (auto& e : E) e.flow = 0;     long long flow = 0, bot;     for (int lim = (1 << F); lim >= 1; lim >>= 1) {       while (bfs(s, t, adj, E, lim)) {         act = vector<int>(adj.size(), 0);         while ((bot = dfs(s, t, oo, adj, E))) flow += bot;       }     }     return flow;   }   inline void addEdge(int x, int y, vector<vector<int>>& adj, vector<Edge>& E,                       long long c, long long rc = 0) {     Edge e;     e.u = x;     e.v = y;     e.flow = 0;     e.cap[0] = c;     e.cap[1] = rc;     adj[x].push_back(int((E).size()));     adj[y].push_back(int((E).size()));     E.push_back(e);   }   long long anss = 0;   void gomory_hu(vector<vector<int>>& adj, vector<Edge>& E, vector<int>& parent,                  vector<vector<long long>>& ans) {     int n = int((adj).size());     parent.assign(n, 0);     ans.assign(n, vector<long long>(n, oo));     for (int i = (1); i < int(n); i++) {       long long f = maxflow(i, parent[i], adj, E);       anss += f;       bfs(i, parent[i], adj, E);       for (int j = (i + 1); j < int(n); j++)         if (d[j] == -1 and parent[j] == parent[i]) parent[j] = i;       ans[i][parent[i]] = ans[parent[i]][i] = f;       for (int j = (0); j < int(i); j++)         ans[i][j] = ans[j][i] = min(f, ans[parent[i]][j]);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<vector<int>> adj(n);     vector<Edge> E;     for (int i = (0); i < int(m); i++) {       int a, b, c;       cin >> a >> b >> c;       --a, --b;       addEdge(a, b, adj, E, c, c);     }     vector<int> par;     vector<vector<long long>> d;     gomory_hu(adj, E, par, d);     cout << anss << endl;     int best = 0;     vector<int> res(n);     for (int i = 0; i < n; ++i) {       res[i] = i;     }     for (int i = 0; i < 1; ++i) {       vector<int> order;       order.push_back(i);       vector<int> used(n, 0);       used[i] = true;       int score = 0;       while (int((order).size()) != n) {         int best = -1, index = -1;         for (int j = 0; j < n; ++j) {           if (!used[j] && d[order.back()][j] > best) {             best = d[order.back()][j];             index = j;           }         }         score += best;         order.push_back(index);         used[index] = true;       }       if (score > best) {         best = score;         res = order;       }     }     for (int i = 0; i < res.size(); ++i) {       printf( %d  , res[i] + 1);     }     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const long long INF = 0x3f3f3f3f3f3f3f3fll;   const int maxn = 1000010;   const long long mod = 1e9 + 7;   char str1[maxn], str2[maxn];   int ans[maxn];   long long L1[maxn], R1[maxn], L2[maxn], R2[maxn];   int main() {     while (scanf( %s , str1 + 1) != -1) {       scanf( %s , str2 + 1);       int len1 = strlen(str1 + 1);       int len2 = strlen(str2 + 1);       if (len1 - 1 != len2) {         printf( 0 n );         continue;       }       L1[0] = 0;       L2[0] = 0;       R1[len1 + 1] = 0;       R2[len2 + 1] = 0;       for (int i = 1; i <= len1; i++) L1[i] = (L1[i - 1] * 26 + str1[i]) % mod;       for (int i = len1; i >= 1; i--) R1[i] = (R1[i + 1] * 26 + str1[i]) % mod;       for (int i = 1; i <= len2; i++) L2[i] = (L2[i - 1] * 26 + str2[i]) % mod;       for (int i = len2; i >= 1; i--) R2[i] = (R2[i + 1] * 26 + str2[i]) % mod;       int len = 0;       for (int i = 1; i <= len1; i++) {         if (L1[i - 1] == L2[i - 1] && R1[i + 1] == R2[i]) ans[len++] = i;       }       printf( %d n , len);       for (int i = 0; i < len; i++) {         if (i == len - 1)           printf( %d n , ans[i]);         else           printf( %d  , ans[i]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   const int MAXN = 100000;   const int MAXQ = 200000;   int nq;   int qkind[MAXQ], qx[MAXQ], qy[MAXQ], qans[MAXQ];   int n;   map<pair<int, int>, int> mp;   int px[MAXN], py[MAXN], pl[MAXN], pr[MAXN];   map<long long, vector<int>> byrad;   map<pair<int, int>, vector<pair<int, int>>> matches;   map<pair<int, int>, vector<pair<int, int>>> singles;   map<pair<int, int>, vector<int>> queries;   void run() {     scanf( %d , &nq);     for (int i = (0); i < (nq); ++i) scanf( %d%d%d , &qkind[i], &qx[i], &qy[i]);     n = 0;     mp.clear();     for (int i = (0); i < (nq); ++i) {       if (qkind[i] == 1) {         int id = n++;         pair<int, int> key = make_pair(qx[i], qy[i]);         assert(!mp.count(key));         mp[key] = id;         px[id] = qx[i], py[id] = qy[i], pl[id] = i, pr[id] = -1;       }       if (qkind[i] == 2) {         pair<int, int> key = make_pair(qx[i], qy[i]);         assert(mp.count(key));         int id = mp[key];         mp.erase(key);         pr[id] = i;       }       if (qkind[i] == 3) {         qans[i] = ((int)(mp).size());       }     }     for (auto it = mp.begin(); it != mp.end(); ++it) {       int id = it->second;       pr[id] = nq;     }     mp.clear();     for (int i = (0); i < (n); ++i)       byrad[(long long)px[i] * px[i] + (long long)py[i] * py[i]].push_back(i);     for (auto it = byrad.begin(); it != byrad.end(); ++it) {       vector<int> lst = it->second;       for (int ii = (0); ii < (((int)(lst).size())); ++ii)         for (int jj = (ii + 1); jj < (((int)(lst).size())); ++jj) {           int i = lst[ii], j = lst[jj];           int l = max(pl[i], pl[j]), r = min(pr[i], pr[j]);           if (l > r) continue;           assert(px[i] != px[j] || py[i] != py[j]);           int cx = px[i] + px[j], cy = py[i] + py[j], g = gcd(cx, cy);           cx /= g, cy /= g;           matches[make_pair(cx, cy)].push_back(make_pair(l, r));         }     }     for (int i = (0); i < (n); ++i) {       int cx = px[i], cy = py[i], g = gcd(cx, cy);       cx /= g, cy /= g;       singles[make_pair(cx, cy)].push_back(make_pair(pl[i], pr[i]));     }     for (int i = (0); i < (nq); ++i)       if (qkind[i] == 3) {         int cx = qx[i], cy = qy[i], g = gcd(cx, cy);         cx /= g, cy /= g;         queries[make_pair(cx, cy)].push_back(i);       }     for (auto it = queries.begin(); it != queries.end(); ++it) {       vector<int> cqueries = it->second;       vector<pair<int, int>> cmatches = matches[it->first];       vector<pair<int, int>> csingles = singles[it->first];       vector<pair<int, int>> evt;       for (int i = (0); i < (((int)(cmatches).size())); ++i)         evt.push_back(make_pair(cmatches[i].first, -2)),             evt.push_back(make_pair(cmatches[i].second + 1, +2));       for (int i = (0); i < (((int)(csingles).size())); ++i)         evt.push_back(make_pair(csingles[i].first, -1)),             evt.push_back(make_pair(csingles[i].second + 1, +1));       sort(cqueries.begin(), cqueries.end());       sort(evt.begin(), evt.end());       int at = 0, sum = 0;       for (int i = (0); i < (((int)(cqueries).size())); ++i) {         int id = cqueries[i];         while (at < ((int)(evt).size()) && id >= evt[at].first)           sum += evt[at++].second;         qans[id] += sum;       }     }     for (int i = (0); i < (nq); ++i)       if (qkind[i] == 3) printf( %d n , qans[i]);   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>   int main(void) {     int i, j, k, m, n, max_length, length;     char string[101];     for (n = 0, scanf( %s , string); string[n]; n++)       ;     for (m = 1, max_length = 0; m < n; m++) {       for (i = m - 1; i >= 0; i--) {         for (j = i, k = m, length = 0; k && string[j] == string[k];              j--, k--, length++)           ;         if (max_length < length) {           max_length = length;         }       }     }     printf( %d , max_length);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int K;     cin >> K;     int sinir = 2 * K * K;     int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 27, 31, 37, 39};     set<int> mynums;     int i;     for (i = 0; i < sizeof(primes) / sizeof(int); i++) {       mynums.clear();       int t1 = 1, t2 = 1, t = 1;       for (int j = 0; j <= i; j++) t *= primes[j];       for (int j = 0; j <= i / 4; j++) t1 *= primes[j] * primes[i - j];       for (int j = i / 4 + 1; j < i - i / 4; j++) t2 *= primes[j];       mynums.insert(t);       for (int y = 0; y < 30; y++) {         for (int j = 0; j <= i; j++) {           for (set<int>::iterator it = mynums.begin(); it != mynums.end(); it++) {             if (*it * primes[j] <= sinir) {               mynums.insert(*it * primes[j]);             }           }         }       }       if (mynums.size() > K) break;       if (t1 > 1) mynums.insert(t1);       if (t2 > 1) mynums.insert(t2);       for (int y = 0; y < 30; y++) {         for (int j = 0; j <= i; j++) {           for (set<int>::iterator it = mynums.begin(); it != mynums.end(); it++) {             if (*it * primes[j] <= sinir) {               mynums.insert(*it * primes[j]);             }           }         }       }       if (mynums.size() <= K / 2) continue;       if (mynums.size() > K) break;     }     set<int>::iterator it = mynums.end();     it--;     cout << *it;     i = 1;     for (it--; it != mynums.begin() && i < K; it--, i++) cout <<     << *it;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   char s[504][504];   int vis[504][504];   vector<pair<char, pair<int, int> > > v;   vector<pair<int, int> > ord;   int dx[] = {1, 0, -1, 0};   int dy[] = {0, 1, 0, -1};   void dfs(int x, int y) {     ord.push_back(make_pair(x, y));     for (int i = 0; i < 4; i++)       if (x + dx[i] < n and y + dy[i] < m and x + dx[i] >= 0 and           y + dy[i] >= 0 and s[x + dx[i]][y + dy[i]] !=  #  and           !vis[x + dx[i]][y + dy[i]]) {         vis[x + dx[i]][y + dy[i]] = 1;         dfs(x + dx[i], y + dy[i]);       }   }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < n; i++) scanf( %s , &s[i]);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (s[i][j] ==  . ) v.push_back(make_pair( B , make_pair(i, j)));     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (!vis[i][j] and s[i][j] !=  # ) {           ord.clear();           vis[i][j] = 1;           dfs(i, j);           for (int k = ord.size() - 1; k >= 1; k--) {             v.push_back(make_pair( D , ord[k]));             v.push_back(make_pair( R , ord[k]));           }         }     printf( %d n , v.size());     for (int i = 0; i < v.size(); i++)       printf( %c %d %d n , v[i].first, v[i].second.first + 1,              v[i].second.second + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     char S[110];     scanf( %s , S);     int len = strlen(S);     sort(S, S + len);     for (int i = 0; i < len - 1; i++) {       if (S[i + 1] - S[i] != 1) {         printf( NO n );         return;       }     }     printf( YES n );   }   int main() {     int T;     scanf( %d , &T);     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int sqr(long long int x) { return x * x; }   inline long long int add(long long int a, long long int b) {     long long int c = a + b;     if (c >= 1000000007) c -= 1000000007;     return c;   }   inline long long int mul(long long int a, long long int b) {     return (a * 1ll * b) % 1000000007;   }   inline long long int sub(long long int a, long long int b) {     long long int c = a - b;     if (c < 0) c += 1000000007;     return c;   }   const int nax = 1000 * 1000 + 10;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     auto ok = [](vector<int> &v, int pos) {       swap(v[pos + 1], v[pos + 2]);       swap(v[pos], v[pos + 1]);     };     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> v(n);       vector<pair<int, int>> x(n);       for (int i = 0; i < n; ++i) {         cin >> v[i];         x[i] = {v[i], i};       }       sort(x.begin(), x.end());       for (int i = 0; i < n; ++i) {         v[x[i].second] = i;       }       int cnt = 0;       for (int i = 0; i < n; ++i) {         for (int j = i + 1; j < n; ++j) {           cnt += (v[j] < v[i]);         }       }       if (cnt & 1) {         for (int i = 0; i < n - 1; ++i) {           if (x[i].first == x[i + 1].first) {             swap(v[x[i].second], v[x[i + 1].second]);             break;           }         }       }       vector<int> ans;       for (int i = 0; i < n - 2; ++i) {         int pos = min_element(v.begin() + i, v.end()) - v.begin();         while (pos > i + 1) {           ok(v, pos - 2);           ans.push_back(pos - 2);           pos -= 2;         }         if (pos != i) {           ok(v, i);           ok(v, i);           ans.push_back(i);           ans.push_back(i);         }       }       for (int i = 0; i < 3; ++i) {         if (is_sorted(v.begin(), v.end())) break;         ok(v, n - 3);         ans.push_back(n - 3);       }       if (!is_sorted(v.begin(), v.end())) {         cout << -1 <<   n ;       } else {         cout << ans.size() <<   n ;         for (int i = 0; i < ans.size(); ++i) cout << ans[i] + 1 <<    ;         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     while (cin >> n) {       string str;       cin >> str;       bool flag = true;       if (n < 3) {         if (n == 1) {           if (str[0] ==  1 )             cout <<  Yes  << endl;           else             cout <<  No  << endl;         } else {           if (str !=  00  && str !=  11 )             cout <<  Yes  << endl;           else             cout <<  No  << endl;         }         continue;       }       for (int i = 0; i < n - 2; i++) {         int ans = 0;         for (int j = i; j < i + 3; j++) {           ans += str[j] -  0 ;         }         if (ans == 1)           continue;         else if (ans == 0 || ans == 3) {           flag = false;           break;         } else {           if (str[i + 1] !=  0 ) {             flag = false;             break;           }         }       }       if (str[0] ==  0  && str[1] ==  0  && str[2] ==  1 ) flag = false;       if (str[n - 1] ==  0  && str[n - 2] ==  0  && str[n - 3] ==  1 )         flag = false;       if (flag)         cout <<  Yes  << endl;       else         cout <<  No  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   struct SCC {     vector<pair<int, int>> minmax;     int size;     SCC(vector<int> v) {       minmax.resize(v.size());       for (int i = 0; i < (int)v.size(); i++) {         minmax[i].first = v[i];         minmax[i].second = v[i];       }       size = 1;     }     bool operator<(SCC const& other) const {       for (int i = 0; i < (int)minmax.size(); i++) {         if (other.minmax[i].first < minmax[i].second) return false;       }       return true;     }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     set<SCC> s;     for (int i = 0; i < n; i++) {       vector<int> v(k);       for (int i = 0; i < k; i++) {         cin >> v[i];       }       SCC scc(v);       auto a = s.lower_bound(scc);       auto b = s.upper_bound(scc);       if (a != b) {         auto x = a;         while (x != b) {           for (int K = 0; K < k; K++) {             scc.minmax[K].first = min(scc.minmax[K].first, x->minmax[K].first);             scc.minmax[K].second = max(scc.minmax[K].second, x->minmax[K].second);           }           scc.size += x->size;           x++;         }         s.erase(a, b);       }       s.insert(scc);       cout << s.rbegin()->size <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   struct he {     int ind;     int val;     he(int ind_ = 0, int val_ = 0) : ind(ind_), val(val_) {}     bool operator<(const he oth) const { return val > oth.val; }   };   int n, m, k;   vector<int> p[200200], c[200200];   int minv[200200], maxv[200200];   int np[200200], nc[200200];   int ans[200200];   queue<int> q;   priority_queue<he> d_cand, id_cand;   int main() {     cin >> n >> m >> k;     for (int i = 1; i <= n; ++i) {       int a;       cin >> a;       if (a == 0) {         minv[i] = 0;         maxv[i] = k + 1;       } else {         minv[i] = a;         maxv[i] = a;       }     }     for (int i = 0; i < m; ++i) {       int a, b;       cin >> a >> b;       p[b].push_back(a);       c[a].push_back(b);     }     for (int i = 1; i <= n; ++i) {       np[i] = p[i].size();       if (np[i] == 0) {         maxv[i] = min(k, maxv[i]);         q.push(i);       }     }     int reached = 0;     while (!q.empty()) {       int ind = q.front();       q.pop();       ++reached;       for (int x : c[ind]) {         maxv[x] = min(maxv[x], maxv[ind] - 1);         --np[x];         if (np[x] == 0) q.push(x);       }     }     if (reached < n) {       cout <<  -1 n ;       return 0;     }     for (int i = 1; i <= n; ++i) {       nc[i] = c[i].size();       if (nc[i] == 0) {         id_cand.push(he(i, minv[i]));       }     }     for (int step = 1; step <= k; ++step) {       while (!id_cand.empty() && id_cand.top().val <= step) {         int x = id_cand.top().ind;         id_cand.pop();         d_cand.push(he(x, maxv[x]));       }       if (d_cand.empty()) {         cout <<  -1 n ;         return 0;       }       int added = 0;       while (!d_cand.empty() && d_cand.top().val == step) {         ++added;         int x = d_cand.top().ind;         d_cand.pop();         ans[x] = step;         for (int y : p[x]) {           --nc[y];           minv[y] = max(minv[y], step + 1);           if (nc[y] == 0) {             id_cand.push(he(y, minv[y]));           }         }       }       if (added == 0) {         int x = d_cand.top().ind;         d_cand.pop();         if (maxv[x] < step) {           cout <<  -1 n ;           return 0;         }         ans[x] = step;         for (int y : p[x]) {           --nc[y];           minv[y] = max(minv[y], step + 1);           if (nc[y] == 0) {             id_cand.push(he(y, minv[y]));           }         }       }     }     for (int i = 1; i <= n; ++i) {       cout << ans[i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> mp;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       int num;       cin >> num;       mp[num]++;     }     if (mp.size() != 2) {       cout <<  NO ;       exit(0);     }     if (mp.begin()->second != mp.rbegin()->second) {       cout <<  NO ;       exit(0);     }     cout <<  YES n ;     cout << mp.begin()->first <<     << mp.rbegin()->first;     return 0;   }


#include<cstdio>   #include<iostream>   #include<cstring>   #include<string>   #include<cmath>   #include<map>   #include<algorithm>   #include<vector>   #include<set>   #define Quick ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)   #define ll long long   #define INF 0x3f3f3f3f   #define PI acos(-1)   #define mem(a, b) memset(a, b, sizeof a)   using namespace std;   const int N = 1e6 + 3;   const int mod =1e9 + 7;    int a[N];   ll dp[N];   int vis[N];   int tot = 0;      int main()   {    Quick;    int n; cin >> n;    cout << (n - 1) % 9 + 1 << endl;;   }