#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void umax(T &a, T b) {     if (a < b) a = b;   }   const int maxn = 4000 + 5;   const int INF = 1e9 + 7;   const double eps = 1e-8;   string a[10];   int dp[4][105];   int n, k;   int valid(int x, int y) {     if (x >= 0 && x < n && y >= 0 && y < 3) return true;     return false;   }   int rec(int posy, int posx) {     if (posy < 0 || posy >= 3) return 0;     if (posx >= n - 1) return 1;     if (dp[posy][posx] != -1) return dp[posy][posx];     int &ret = dp[posy][posx];     ret = 0;     if (posx >= n - 1) {       ret = 1;       return ret;     }     if (a[posy][posx + 1] !=  . ) {       ret = 0;       return ret;     }     for (int i = (-1); i < (2); i++) {       if (posy + i < 0 || posy + i >= 3) continue;       if (a[posy + i][posx + 1] !=  . ) continue;       if (a[posy + i][posx + 2] !=  . ) continue;       if (a[posy + i][posx + 3] !=  . ) continue;       ret |= rec(posy + i, posx + 3);     }     return ret;   }   inline void solve(void) {     int t;     cin >> t;     while (t--) {       memset(dp, -1, sizeof dp);       cin >> n >> k;       for (int i = (0); i < (3); i++) cin >> a[i];       for (int i = (0); i < (3); i++)         for (int j = (0); j < (7); j++) a[i].push_back( . );       int posy, posx;       for (int i = (0); i < (3); i++)         for (int j = (0); j < (n); j++)           if (a[i][j] ==  s ) posx = j, posy = i;       if (rec(posy, posx)) {         cout <<  YES n ;       } else         cout <<  NO n ;     }     return;   }   void init() {     ios::sync_with_stdio(false);     cin.tie(nullptr);   }   int main(int argc, const char *argv[]) {     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace std {   template <typename T, typename U>   struct hash<pair<T, U>> {     hash<T> t;     hash<U> u;     size_t operator()(const pair<T, U>& p) const {       return t(p.first) ^ (u(p.second) << 7);     }   };   }  // namespace std   template <typename T, typename F>   T bsh(T l, T h, const F& f) {     T r = -1, m;     while (l <= h) {       m = (l + h) / 2;       if (f(m)) {         l = m + 1;         r = m;       } else {         h = m - 1;       }     }     return r;   }   template <typename T, typename F>   T bsl(T l, T h, const F& f) {     T r = -1, m;     while (l <= h) {       m = (l + h) / 2;       if (f(m)) {         h = m - 1;         r = m;       } else {         l = m + 1;       }     }     return r;   }   template <typename T>   T gcd(T a, T b) {     if (a < b) swap(a, b);     return b ? gcd(b, a % b) : a;   }   int main(int, char**) {     ios_base::sync_with_stdio(false);     int N;     cin >> N;     ;     int S;     cin >> S;     ;     vector<int> A(N);     for (auto& _cinv : A) cin >> _cinv;     ;     int err = 0;     if (A[S - 1] != 0) {       ++err;       A[S - 1] = 0;     }     vector<int> Q(N, 0);     for (int i = 0; i < N; i++) {       Q[A[i]]++;     }     int i = 1;     for (; i < N && (Q[0] > 1 || Q[i] > 0); ++i) {       if (Q[i] == 0) {         --Q[0];         ++Q[i];         ++err;       }     }     int j = N - 1;     while (i < j) {       if (Q[j] == 0)         --j;       else if (Q[i] > 0)         ++i;       else {         Q[i]++;         Q[j]--;         err++;       }     }     cout << err << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5000 + 10;   bitset<MAXN> kna;   vector<int> G[MAXN];   int n, a, b, c, d, p[MAXN], sze[MAXN];   bool vis[MAXN];   bool check(int a, int b) {     memset(vis, 0, sizeof(vis));     kna.reset();     kna.flip(0);     for (int u = a; p[u]; u = p[u]) vis[u] = true;     for (int u = b; p[u]; u = p[u]) vis[u] = true;     for (int u = a; u; u = p[u])       for (auto i : G[u])         if (!vis[i]) {           kna |= kna << sze[i];         }     return kna.test(sze[1] / 2 - 1);   }   int main() {     scanf( %d%d%d%d%d , &n, &a, &b, &c, &d);     for (int i = 1; i <= n; i++) G[i].clear();     for (int i = 2; i <= n; i++) {       scanf( %d , &p[i]);       G[p[i]].push_back(i);     }     for (int u = n; u > 1; u--) {       sze[u] = max(sze[u], 1);       sze[p[u]] += sze[u];     }     if ((sze[1] % 2 == 0) && check(a, b) && check(c, d))       puts( YES n );     else       puts( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int k, i, cn = 0, cnt = 0, j;     cin >> k;     string s;     cin >> s;     map<long long int, long long int> ans;     long long int a[s.length()], n = s.length();     for (i = 0; i < s.length(); i++) {       if (s[i] ==  1 ) cnt++;     }     if (k > cnt)       cout <<  0  << endl;     else {       a[0] = (s[0] -  0 );       for (i = 1; i < s.length(); i++) {         a[i] = a[i - 1] + (s[i] -  0 );       }       i = 0;       j = 0;       ans[0] = 1;       for (i = 0; i < n; i++) {         if (a[i] >= k) cn += ans[a[i] - k];         ans[a[i]]++;       }       cout << cn << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k, p, a;     cin >> n >> k >> p;     if (n % 2 && k) {       n--;       k--;     }     if (2 * k > n) {       long long t = 2 * k - n;       n -= t;       k -= t;     }     while (p--) {       cin >> a;       if ((a > n) || ((a % 2 == 0) && ((n - a) / 2 < k)))         putchar( X );       else         putchar( . );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, nu, ul, ka, c1ul, c1ka, c2ul, c2ka;     cin >> n;     c1ul = c1ka = c2ul = c2ka = 0;     for (i = 0; i < n; i++) {       cin >> nu >> ul >> ka;       if (nu == 1) {         c1ul += ul;         c1ka += ka;       } else {         c2ul += ul;         c2ka += ka;       }     }     if (c1ul >= (c1ul + c1ka) / 2) {       cout <<  LIVE  << endl;     } else {       cout <<  DEAD  << endl;     }     if (c2ul >= (c2ul + c2ka) / 2) {       cout <<  LIVE  << endl;     } else {       cout <<  DEAD  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int shr[10], mor[10], shrc[10], morc[10], n, i;     cin >> n;     for (i = 0; i < 10; i++) {       shr[i] = 0;       mor[i] = 0;       shrc[i] = 0;       morc[i] = 0;     }     char sher[n + 1], mr[n + 1];     cin >> sher >> mr;     for (i = 0; i < n; i++) {       shr[sher[i] -  0 ]++;       mor[mr[i] -  0 ]++;       shrc[sher[i] -  0 ]++;       morc[mr[i] -  0 ]++;     }     long sherlockmax = 0, sheri = 0, mori = 0, minmori = 0;     while (sheri < 10 && mori < 10) {       while (shr[sheri] == 0 && sheri < 10) sheri++;       while (mor[mori] == 0 && mori < 10) mori++;       while (sheri >= mori && mori < 10) {         mori++;       }       if (mori < 10 && sheri < 10 && mori > sheri) {         sherlockmax += min(shr[sheri], mor[mori]);         long k = min(shr[sheri], mor[mori]);         shr[sheri] -= k;         mor[mori] -= k;       }     }     sheri = 0;     mori = 0;     while (sheri < 10 && mori < 10) {       while (shrc[sheri] == 0 && sheri < 10) sheri++;       while (morc[mori] == 0 && mori < 10) mori++;       while (sheri > mori && mori < 10) {         minmori += morc[mori];         mori++;       }       if (mori < 10 && sheri < 10 && mori >= sheri) {         long k = min(shrc[sheri], morc[mori]);         shrc[sheri] -= k;         morc[mori] -= k;       }     }     cout << minmori << endl;     cout << sherlockmax << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char a;   int n, sum, flag = 1;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a;       if (a !=  0 )         sum++, flag = 1;       else if (flag == 1 && a ==  0 ) {         cout << sum;         sum = 0;         flag = 0;       } else if (a ==  0  && flag == 0)         cout <<  0 ;     }     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int a[4], index;     node() { memset(a, 0, sizeof(a)); }   } no1[100005];   int n;   int ans = 0, flag = 1, indexa, indexb;   vector<pair<int, int> > ve;   map<pair<int, int>, int> mp;   int l = 0;   set<pair<int, int> > se[1000000];   void update(int a, int b, int c, int index) {     set<pair<int, int> >::iterator it;     int pre = mp[make_pair(a, b)];     it = se[pre].end();     if (it != se[pre].begin()) {       it--;       if (min(min(a, b), it->first + c) > ans) {         ans = min(min(a, b), it->first + c);         flag = 2;         indexa = it->second;         indexb = index;       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int a, b, c;       scanf( %d%d%d , &no1[i].a[0], &no1[i].a[1], &no1[i].a[2]);       a = no1[i].a[0];       b = no1[i].a[1];       c = no1[i].a[2];       ve.push_back(make_pair(min(a, b), max(a, b)));       ve.push_back(make_pair(min(a, c), max(a, c)));       ve.push_back(make_pair(min(c, b), max(c, b)));       no1[i].index = i + 1;       if (min(no1[i].a[0], min(no1[i].a[1], no1[i].a[2])) > ans) {         ans = min(no1[i].a[0], min(no1[i].a[1], no1[i].a[2]));         indexa = i + 1;       }     }     sort(ve.begin(), ve.end());     mp[ve[0]] = l;     for (int i = 1; i < ve.size(); i++) {       if (ve[i].first != ve[i - 1].first || ve[i].second != ve[i - 1].second) l++;       mp[ve[i]] = l;     }     for (int i = 0; i < n; i++) {       int a = no1[i].a[0];       int b = no1[i].a[1];       int c = no1[i].a[2];       update(min(a, b), max(a, b), c, i + 1);       update(min(a, c), max(a, c), b, i + 1);       update(min(b, c), max(b, c), a, i + 1);       se[mp[make_pair(min(a, b), max(a, b))]].insert(make_pair(c, i + 1));       se[mp[make_pair(min(c, b), max(c, b))]].insert(make_pair(a, i + 1));       se[mp[make_pair(min(a, c), max(a, c))]].insert(make_pair(b, i + 1));     }     printf( %d n , flag);     if (flag == 1) {       printf( %d n , indexa);     } else {       printf( %d %d n , indexa, indexb);     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   const int N = 1e6 + 10;   const long long mod = 1e9 + 7;   const long long mod2 = 998244353;   const long long inf = 8e18;   const int LOG = 22;   long long pw(long long a, long long b, long long M) {     return (!b ? 1                : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M                         : pw(a * a % M, b / 2, M)));   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     string s;     cin >> s;     int n = (int)s.size();     long long ans = 0;     for (int l = 0; l < n; l++) {       ans += max(0, n - l - 10);       if (l + 2 > n) continue;       for (int r = l + 2; r < min(l + 10, n); r++) {         bool is_OK = 0;         for (int x1 = l; x1 <= r; x1++) {           for (int x2 = x1 + 1; x2 <= r; x2++) {             if (s[x1] == s[x2] && 2 * x2 - x1 <= r && s[2 * x2 - x1] == s[x1]) {               is_OK = 1;             }           }         }         ans += is_OK;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> edge[100005];   int visited[100005][2];   vector<int> ans;   int draw = 1;   int visit(int verId, int isPetyaTurn) {     visited[verId][isPetyaTurn] = 1;     if (edge[verId].size() == 0) {       draw = 0;       if (isPetyaTurn == 0) {         ans.push_back(verId);         return 1;       }     }     for (int i = 0; i < edge[verId].size(); ++i) {       int nextVerId = edge[verId][i];       if (visited[nextVerId][1 - isPetyaTurn] == 0 &&           visit(nextVerId, 1 - isPetyaTurn)) {         ans.push_back(verId);         return 1;       }     }     return 0;   }   int canDrawVisited[100005];   int canDraw(int verId, int step) {     if (canDrawVisited[verId] == 1) {       return 1;     }     if (step == 1000000) {       return 1;     }     canDrawVisited[verId] = 1;     for (int i = 0; i < edge[verId].size(); ++i) {       int nextVerId = edge[verId][i];       if (canDraw(nextVerId, step + 1)) {         return 1;       }     }     canDrawVisited[verId] = 0;     return 0;   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) {       int nEdge;       scanf( %d , &nEdge);       while (nEdge--) {         int tmp;         scanf( %d , &tmp);         edge[i].push_back(tmp);       }     }     int begVerId;     scanf( %d , &begVerId);     int res = visit(begVerId, 1);     if (draw) {       puts( Draw );     } else if (res) {       puts( Win );       for (int i = ans.size() - 1; i >= 0; --i) {         printf( %d%c , ans[i],    n [i == 0]);       }     } else {       int draw = canDraw(begVerId, 0);       if (draw) {         puts( Draw );       } else {         puts( Lose );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 2000000000;   static inline int Rint() {     struct X {       int dig[256];       X() {         for (int i =  0 ; i <=  9 ; ++i) dig[i] = 1;         dig[ - ] = 1;       }     };     static X fuck;     int s = 1, v = 0, c;     for (; !fuck.dig[c = getchar()];)       ;     if (c ==  - )       s = 0;     else if (fuck.dig[c])       v = c ^ 48;     for (; fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48))       ;     return s ? v : -v;   }   template <typename T>   static inline void cmax(T& a, const T& b) {     if (b > a) a = b;   }   template <typename T>   static inline void cmin(T& a, const T& b) {     if (b < a) a = b;   }   struct Pt {     int x, y;   };   int cmp_x_min(const Pt& a, const Pt& b) {     if (a.x != b.x) return a.x < b.x;     return a.y < b.y;   }   Pt data[100005];   int main() {     int n = Rint(), m = Rint();     for (int i = 0; i < m; ++i) data[i].x = Rint(), data[i].y = Rint();     sort(data, data + m, cmp_x_min);     vector<int> last;     last.push_back(1);     last.push_back(1);     int row = 0;     for (int now = 0; now < m;) {       int j = now + 1;       while (j < m && data[j].x == data[now].x) ++j;       if (last.size() && data[now].x > row + 1) {         vector<int> next;         next.push_back(last.front()), next.push_back(n);         last.swap(next);         row = row + 1;       }       const int size = ((int)(last).size());       vector<int> next;       for (int k = now, pos = 0; k <= j; ++k) {         int r = k == j ? n : data[k].y - 1;         int l = k == now ? 1 : data[k - 1].y + 1;         if (l > r) continue;         while (pos < size && last[pos + 1] < l) pos += 2;         if (pos < size) {           if (last[pos] > r) continue;           if (last[pos] <= l) {             next.push_back(l);             next.push_back(r);           } else if (last[pos] <= r) {             next.push_back(last[pos]);             next.push_back(r);           }         }       }       last.swap(next);       row = data[now].x;       now = j;     }     if (last.size() && data[m - 1].x < n) {       vector<int> next;       next.push_back(last.front()), next.push_back(n);       last.swap(next);       row = row + 1;     }     if (last.size()) {       const int size = ((int)(last).size());       if (last[size - 2] <= n && last[size - 1] >= n) {         printf( %d n , 2 * n - 2);         return 0;       }     }     puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long a;     cin >> a;     bitset<22> b(a);     long long i, j, k;     long long c = 0;     vector<long long> v;     for (i = 0; i <= 40; i++) {       long long temp = a;       temp++;       if ((a & temp) == 0) {         break;       }       if (i % 2)         a++;       else {         bitset<22> z(a);         for (j = 21; j >= 0; j--) {           if (z[j] == 1) break;         }         for (; j >= 0; j--)           if (z[j] == 0) break;         v.push_back(j + 1);         a = a ^ (long long)(pow(2, j + 1) - 1);       }     }     cout << i << endl;     for (auto g : v) cout << g <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   vector<int> eum;   int w[100005];   int Euler(int num) {     int p = 2, ans = 1;     while (p * p <= num) {       int ct = 1;       while (num % p == 0) {         ct *= p;         num /= p;       }       if (ct > 1) ct = ct / p * (p - 1);       ans *= ct;       p++;     }     if (num > 1) ans *= num - 1;     return ans;   }   long long power(int x, int t, long long m) {     if (t == 0) return 1;     long long cc = power(x, t >> 1, m);     cc = cc * cc % m;     if (t & 1) cc = cc * x % m;     return cc;   }   int gcd(int m, int n) { return n == 0 ? m : gcd(n, m % n); }   int solve(int l, int r, int mid) {     if (l > r) return 1;     if (eum[mid] == 1) return 0;     vector<int> cc;     for (int k = l + 1; k <= r && k <= l + 4; k++) cc.push_back(w[k]);     cc.push_back(1);     for (int k = 0; k < (cc.size()); k++)       if (cc[k] == 1) {         long long cur = 1LL;         for (int j = k - 1; j >= 0; j--) {           if (cc[j] >= 30 || cc[j] >= 4 && cur >= 4) {             cur = 30LL;             break;           }           cur = power(cc[j], cur, 1LL << 62);           if (cur > 30LL) {             cur = 30LL;             break;           }         }         if (cur < 30LL)           return power(w[l], cur, eum[mid]) % eum[mid];         else           return power(w[l], solve(l + 1, r, mid + 1) + eum[mid + 1], eum[mid]);       }   }   int main() {     int n, m, q, x, l, r;     scanf( %d%d , &n, &m);     eum.push_back(m);     while ((x = eum.back()) > 1) eum.push_back(Euler(x));     for (int k = 0; k < (n); k++) scanf( %d , &w[k]);     scanf( %d , &q);     for (int k = 0; k < (q); k++) {       scanf( %d%d , &l, &r);       l--;       r--;       printf( %d n , solve(l, r, 0));     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1000000000 )   #pragma GCC optimize( Ofast,no-stack-protector )   using namespace std;   const int maxn = (int)5e5 + 10, maxlog = 19;   vector<pair<int, int> > ed[maxn];   int par[maxlog][maxn];   int wup[maxn];   int h[maxn];   int pr[maxn];   int getp(int x) { return (pr[x] == x ? x : pr[x] = getp(pr[x])); }   int x[maxn], y[maxn];   int inc[maxn], aksldal[maxn];   void dfs(int v, int p, int we, int hh) {     h[v] = hh;     par[0][v] = p;     wup[v] = we;     for (int i = 1; i < maxlog; i++) {       par[i][v] = par[i - 1][par[i - 1][v]];     }     for (auto [u, num] : ed[v]) {       if (u != p) {         if (v == 0 || num > we) {           aksldal[u] = aksldal[v] + 1;         } else {           aksldal[u] = 1;         }         if (v == 0 || num < we) {           inc[u] = inc[v] + 1;         } else {           inc[u] = 1;         }         dfs(u, v, num, hh + 1);       }     }   }   int lca(int x, int y) {     if (h[x] < h[y]) {       swap(x, y);     }     for (int i = 0; i < maxlog; i++) {       if (((h[x] - h[y]) >> i) & 1) {         x = par[i][x];       }     }     if (x == y) {       return x;     }     for (int i = maxlog - 1; i >= 0; i--) {       if (par[i][x] != par[i][y]) {         x = par[i][x];         y = par[i][y];       }     }     return par[0][x];   }   int up(int x, int d) {     for (int i = 0; i < maxlog; i++) {       if ((d >> i) & 1) {         x = par[i][x];       }     }     return x;   }   bool tr[maxn];   int f[maxn];   int val[maxn];   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       pr[i] = i;     }     for (int i = 0; i < m; i++) {       scanf( %d %d , &x[i], &y[i]);       x[i]--;       y[i]--;     }     for (int i = m - 1; i >= 0; i--) {       int a = x[i];       int b = y[i];       int pa = getp(a);       int pb = getp(b);       if (pa != pb) {         ed[a].push_back(make_pair(b, i));         ed[b].push_back(make_pair(a, i));         pr[pa] = pb;         tr[i] = true;       }     }     dfs(0, 0, -1, 0);     for (int i = 0; i < n; i++) {       val[i] = 1;     }     for (int i = m - 1; i >= 0; i--) {       int a = x[i];       int b = y[i];       if (tr[i]) {         f[i] = val[a] + val[b];         val[a] = val[b] = f[i];         continue;       } else {         int l = lca(a, b);         int x = val[a] + val[b];         if (b == l) {           swap(a, b);         }         if (a == l) {           int d = inc[b];           if (h[b] - h[a] <= d) {             d = h[b] - h[a];           }           int m = up(b, d - 1);           if (aksldal[m] >= h[m] - h[a]) {             x -= f[wup[m]];           }         } else {           int da = inc[a];           int db = inc[b];           if (h[a] - h[l] <= da && h[b] - h[l] <= db) {             int pa = up(a, h[a] - h[l] - 1);             int pb = up(b, h[b] - h[l] - 1);             x -= f[max(wup[pa], wup[pb])];           } else {             if (h[a] - h[l] <= da) {               swap(a, b);               swap(da, db);             }             if (h[b] - h[l] <= db) {               int m = up(a, da - 1);               if (aksldal[m] >= h[m] - h[l]) {                 int pa = up(a, h[a] - h[l] - 1);                 int pb = up(b, h[b] - h[l] - 1);                 if (wup[pb] < wup[pa]) {                   x -= f[wup[m]];                 }               }             }           }         }         f[i] = x;         val[a] = val[b] = x;       }     }     for (int i = 0; i < n; i++) {       printf( %d  , val[i] - 1);     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1024000000,1024000000 )   using namespace std;   const int mxn = 400005, maxnn = 100005;   const int mod = 1e9 + 7;   long long ans;   int n, m, num, cir, r, cnt;   int head[maxnn], que[maxnn];   struct edge {     int to, next;     long long w;   } f[mxn << 2];   long long p[70], circle[mxn], dis[maxnn], dig[2], pw[105];   inline void add(int u, int v, long long w) {     f[++cnt].to = v, f[cnt].w = w, f[cnt].next = head[u], head[u] = cnt;   }   inline void dfs(int u, int fa, long long now) {     dis[u] = now, que[++num] = u;     for (int i = head[u]; i; i = f[i].next) {       int v = f[i].to;       if (v == fa) continue;       if (dis[v] == -1)         dfs(v, u, dis[u] ^ f[i].w);       else         circle[++cir] = dis[u] ^ dis[v] ^ f[i].w;     }   }   inline void init() {     int i, j;     r = 0;     memset(p, 0, sizeof p);     for (i = 1; i <= cir; i++) {       long long x = circle[i];       for (j = 62; j >= 0; j--) {         if (!(x >> j)) continue;         if (!p[j]) {           p[j] = x;           break;         }         x ^= p[j];       }     }     for (j = 0; j <= 62; j++)       if (p[j]) r++;   }   inline void calc() {     init();     int i, j;     for (j = 0; j <= 62; j++) {       bool flag = 0;       dig[0] = dig[1] = 0;       for (i = 1; i <= num; i++) dig[(dis[que[i]] >> j) & 1]++;       for (i = 0; i <= 62; i++)         if ((p[i] >> j) & 1) {           flag = 1;           break;         }       long long now =           (dig[0] * (dig[0] - 1) / 2 + dig[1] * (dig[1] - 1) / 2) % mod;       if (flag) {         if (r) now = now * pw[r - 1] % mod;         now = now * pw[j] % mod;         ans = (ans + now) % mod;       }       now = dig[0] * dig[1] % mod;       if (flag) {         if (r) now = now * pw[r - 1] % mod;       } else         now = now * pw[r] % mod;       now = now * pw[j] % mod;       ans = (ans + now) % mod;     }   }   int main() {     int i, j, u, v;     long long w;     memset(dis, -1, sizeof dis);     pw[0] = 1;     for (j = 1; j <= 100; j++) pw[j] = pw[j - 1] * 2 % mod;     scanf( %d%d , &n, &m);     for (i = 1; i <= m; i++) {       scanf( %d%d%lld , &u, &v, &w);       add(u, v, w), add(v, u, w);     }     for (i = 1; i <= n; i++)       if (dis[i] == -1) {         num = cir = 0;         dfs(i, 0, 0);         calc();       }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<long long> x(n);     for (long long i = 0; i < n; i++) cin >> x[i];     unordered_map<int, int> m;     for (auto num : x) {       m[num]++;     }     if (m.size() < 3) {       cout << 0;       return 0;     }     int ans = INT_MAX;     for (auto num : m) {       if (ans > num.second) {         ans = num.second;       }     }     printf( %d n , ans);     vector<vector<int>> v(4);     for (int i = 0; i < n; i++) {       if (x[i] == 1) v[1].push_back(i + 1);       if (x[i] == 2) v[2].push_back(i + 1);       if (x[i] == 3) v[3].push_back(i + 1);     }     for (long long i = 0; i < ans; i++) {       printf( %d %d %d n , v[1][i], v[2][i], v[3][i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct pokr {     double p1;     double p2;     double len;     pokr(double _p1, double _p2, double _l) : p1(_p1), p2(_p2), len(_l) {}     pokr() { p1 = p2 = len = 0; }   } d[2100][2100];   int x[2100];   int c[2100][2100];   int a[2100];   int b[2100];   double p, q;   int h;   pokr get_ans(int l, int r) {     pokr res;     if (l > r) return pokr(0, 0, 0);     if (l == r) return pokr(p, q, 0);     if (c[l][r]) return d[l][r];     pokr temp = get_ans(l + 1, r);     res.p1 += p / 2;     res.p2 += temp.p2 * p / 2;     res.len += p / 2 * temp.len;     res.len += p / 2 * temp.p1 * min(x[l + 1] - x[l], h);     int next = b[l];     if (next <= r) {       pokr temp = get_ans(next, r);       res.p2 += q / 2 * temp.p2;       res.len += q / 2 * temp.len;       res.len += q / 2 * (x[next - 1] + h - x[l]);       res.len += q / 2 * temp.p1 * min(x[next] - x[next - 1] - h, h);     } else {       res.p2 += q / 2;       res.len += q / 2 * (x[r] - x[l]);     }     temp = get_ans(l, r - 1);     res.p2 += q / 2;     res.p1 += temp.p1 * q / 2;     res.len += q / 2 * temp.len;     res.len += q / 2 * temp.p2 * min(x[r] - x[r - 1], h);     int prev = a[r];     if (prev >= l) {       pokr temp = get_ans(l, prev);       res.p1 += p / 2 * temp.p1;       res.len += p / 2 * temp.len;       res.len += p / 2 * (x[r] - x[prev + 1] + h);       res.len += p / 2 * temp.p2 * min(x[prev + 1] - x[prev] - h, h);     } else {       res.p1 += p / 2;       res.len += p / 2 * (x[r] - x[l]);     }     c[l][r] = 1;     d[l][r] = res;     return res;   }   int main() {     int n;     cin >> n >> h;     cin >> p;     q = 1 - p;     for (int i = 0; i < n; i++) cin >> x[i];     sort(x, x + n);     a[0] = -1;     for (int i = 1; i < n; i++)       if (x[i] - x[i - 1] < h)         a[i] = a[i - 1];       else         a[i] = i - 1;     b[n - 1] = n;     for (int i = n - 2; i >= 0; i--)       if (x[i + 1] - x[i] < h)         b[i] = b[i + 1];       else         b[i] = i + 1;     pokr r = get_ans(0, n - 1);     printf( %.12f , (r.p1 + r.p2) * h + r.len);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,unroll-loops )   #pragma GCC target( avx,avx2,fma )   using namespace std;   template <typename T>   using vc = vector<T>;   template <typename T>   using uset = unordered_set<T>;   template <typename A, typename B>   using umap = unordered_map<A, B>;   template <typename T>   using pq = priority_queue<T>;   template <typename T>   using minpq = priority_queue<T, vc<T>, greater<T>>;   template <typename T>   inline void sort(vc<T>& v) {     sort(v.begin(), v.end());   }   template <typename T, typename Comp>   inline void sort(vc<T>& v, Comp c) {     sort(v.begin(), v.end(), c);   }   using ll = long long;   using ull = unsigned long long;   using pii = pair<ll, ll>;   using vi = vc<ll>;   using vii = vc<pii>;   constexpr char el =   n ;   constexpr char sp =    ;   constexpr ll INF = 0x3f3f3f3f;   constexpr ll LLINF = 0x3f3f3f3f3f3f3f3fLL;   ll xh, yh, xs, ys, n;   inline bool evan(ll a, ll b) { return a < 0 && b > 0 || a > 0 && b < 0; }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout << fixed;     cin >> xh >> yh;     cin >> xs >> ys;     cin >> n;     ll ans = 0;     while (n--) {       ll a, b, c;       cin >> a >> b >> c;       if (evan(a * xh + b * yh + c, a * xs + b * ys + c)) ans++;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 26;   vector<int> kmp;   string txt, pattern;   vector<vector<int> > nxt, dp;   void build() {     kmp.resize(pattern.size() + 10);     kmp[0] = 0;     for (int i = 1, j = 0; i < pattern.size();) {       if (pattern[i] == pattern[j])         kmp[i++] = ++j;       else if (!j)         kmp[i++] = 0;       else         j = kmp[j - 1];     }     nxt.resize(pattern.size() + 10);     for (int i = 0; i < pattern.size(); ++i) nxt[i].resize(N + 10);     for (int j = 0; j < N; ++j) nxt[0][j] = (pattern[0] -  a  == j);     for (int i = 1; i < pattern.size(); ++i)       for (int j = 0; j < N; ++j)         nxt[i][j] = (pattern[i] -  a  == j) ? i + 1 : nxt[kmp[i - 1]][j];     dp.resize(txt.size() + 10);     for (int i = 0; i < dp.size(); ++i) dp[i].resize(pattern.size() + 10);     for (int i = 0; i < dp.size(); ++i)       for (int j = 0; j < dp[i].size(); ++j) dp[i][j] = -1;   }   int solve(const int &i, const int &j) {     if (i == txt.size()) return 0;     if (dp[i][j] != -1) return dp[i][j];     int ans = 0;     if (txt[i] ==  ? ) {       for (int k = 0; k < N; ++k) {         int new_j = nxt[j][k];         if (new_j == pattern.size())           ans = max(ans, 1 + solve(i + 1, kmp[new_j - 1]));         else           ans = max(ans, solve(i + 1, new_j));       }     } else {       int k = txt[i] -  a ;       int new_j = nxt[j][k];       if (new_j == pattern.size())         ans = max(ans, 1 + solve(i + 1, kmp[new_j - 1]));       else         ans = max(ans, solve(i + 1, new_j));     }     return dp[i][j] = ans;   }   int main() {     cin >> txt >> pattern;     build();     printf( %d n , solve(0, 0));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> mp;   set<int> s;   int main(void) {     int q;     scanf( %d , &q);     while (q--) {       mp.clear();       int x;       scanf( %d , &x);       int t = x;       int lim = sqrt(t);       for (int i = 2; i <= lim; i++) {         while (t % i == 0) {           mp[i]++;           t /= i;         }       }       if (1 < t) mp[t]++;       s.clear();       for (int i = 2; i <= lim; i++) {         if (x % i == 0) {           s.insert(i);           s.insert(x / i);         }       }       s.insert(x);       vector<int> rep;       for (auto e : mp) rep.push_back(e.first);       if (rep.size() == 2) {         int mult = 1;         for (auto e : mp) mult *= e.first;         vector<int> ds;         if (mult == x) {           printf( %d  , mult);           for (int p : rep) {             for (int div : s) {               if (div % p == 0) {                 if (div != mult) {                   printf( %d  , div);                 }                 ds.push_back(div);               }             }             for (int e : ds) s.erase(e);             ds.clear();           }           printf(  n1 n );         } else {           printf( %d  , x);           vector<int> ds;           for (int p : rep) {             for (int div : s) {               if (div % p == 0) {                 if (div != mult) {                   if (div != x) {                     printf( %d  , div);                   }                 }                 ds.push_back(div);               }             }             for (int e : ds) s.erase(e);             ds.clear();             if (p != rep.back()) printf( %d  , mult);           }           printf(  n0 n );         }       } else if (rep.size() == 1) {         for (int div : s) {           printf( %d  , div);         }         printf(  n0 n );       } else {         map<int, int> joint;         for (int i = 0; i < rep.size(); i++) {           joint[rep[i] * rep[(i + 1) % rep.size()]] = true;         }         vector<int> ds;         for (int i = 0; i < rep.size(); i++) {           for (int div : s) {             if (div % rep[i] == 0) {               if (joint.count(div) == 0) {                 printf( %d  , div);               }               ds.push_back(div);             }           }           printf( %d  , rep[i] * rep[(i + 1) % rep.size()]);           for (int e : ds) s.erase(e);           ds.clear();         }         printf(  n0 n );       }     }   }


#include <bits/stdc++.h>   using namespace std;   bool isSquare(long long n) {     const long long s = (long long)ceil(sqrt(n));     return (s * s) == n;   }   long long calcCostToSquare(long long n) {     if (isSquare(n)) return 0;     const auto f1 = (long long)ceil(sqrt(n));     const auto f2 = (long long)floor(sqrt(n));     return min((long long)abs(f1 * f1 - n), (long long)abs(f2 * f2 - n));   }   long long calcCostToNonSquare(long long n) {     if (n == 0) return 2;     if (isSquare(n)) return 1;     return 0;   }   int main() {     long long n;     cin >> n;     long long numSquare = 0;     multiset<long long> toMakeSquare;     multiset<long long> toMakeNonSquare;     for (long long i = 0; i < n; ++i) {       long long a;       cin >> a;       if (isSquare(a)) ++numSquare;       toMakeSquare.insert(calcCostToSquare(a));       toMakeNonSquare.insert(calcCostToNonSquare(a));     }     n /= 2;     long long result = 0;     if (numSquare < n) {       auto it = toMakeSquare.upper_bound(0);       while (numSquare < n) {         result += *it;         ++it;         ++numSquare;       }     } else {       auto it = toMakeNonSquare.upper_bound(0);       while (numSquare > n) {         result += *it;         ++it;         --numSquare;       }     }     cout << result << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long num_to_sig(long long x) {     vector<int> v;     while (x > 0) {       v.push_back((x % 10) % 2);       x /= 10;     }     int n = v.size();     long long ret = 0;     for (int i = n - 1; i >= 0; i--) {       ret *= 10;       ret += v[i];     }     return ret;   }   int main() {     int Q;     scanf( %d , &Q);     map<long long, int> cnt;     while (Q--) {       char c;       scanf(  %c , &c);       long long x;       if (c ==  + ) {         scanf( %lld , &x);         cnt[num_to_sig(x)]++;       } else if (c ==  - ) {         scanf( %lld , &x);         cnt[num_to_sig(x)]--;       } else {         scanf( %lld , &x);         printf( %d n , cnt[num_to_sig(x)]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> sp;   vector<pair<long long, long long>> bases;   int main() {     int s, b;     cin >> s >> b;     long long num, num2;     for (int i = 0; i < s; i++) {       cin >> num;       sp.push_back(num);     }     for (int i = 0; i < b; i++) {       cin >> num >> num2;       bases.push_back({num, num2});     }     sort(bases.begin(), bases.end());     long long sum[b + 5];     for (int i = 0; i < b; i++) {       sum[i] = bases[i].second;       if (i > 0) sum[i] += sum[i - 1];     }     for (int i = 0; i < s; i++) {       long long bas = 0;       long long start = 0, end = b, mid, idx = -1;       while (start <= end) {         mid = (start + end) / 2;         if (bases[mid].first <= sp[i]) {           idx = mid;           start = mid + 1;         } else {           end = mid - 1;         }       }       if (idx == b) idx--;       if (idx == -1)         cout <<  0  ;       else         cout << sum[idx] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   const double pie = 3.14159265358979323846;   const char nl =   n ;   template <typename Type>   istream &operator>>(istream &in, vector<Type> &v) {     long long int n = v.size();     for (int i = 0; i < n; i++) in >> v[i];     return in;   }   template <typename Type>   ostream &operator<<(ostream &out, vector<Type> &v) {     for (auto value : v) out << value <<    ;     out << endl;     return out;   }   void solve() {     long long int n;     cin >> n;     vector<long long int> a(4 * n);     cin >> a;     sort(a.begin(), a.end());     long long int area = a[0] * a.back();     long long int c = 1, j = 4 * n - 1;     for (long long int i = 0; c <= n;) {       if (a[i] != a[i + 1] or a[j] != a[j - 1] or a[i] * a[j] != area) {         cout <<  No n ;         return;       }       ++c;       i += 2;       j -= 2;     }     cout <<  Yes n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n, a[N], fa[N], head[N], p, deep[N];   long long Chuan1[N], Chuan2[N], Ans;   queue<int> q;   struct wfh {     int to, last;   } d[N << 1];   inline void add(int a, int b) { d[++p] = (wfh){b, head[a]}, head[a] = p; }   inline long long max(long long A, long long B) { return A > B ? A : B; }   inline long long min(long long A, long long B) { return A < B ? A : B; }   void dfs(int u, int fa) {     for (int i = head[u], v; i; i = d[i].last) {       v = d[i].to;       if (v == fa) continue;       dfs(v, u);       Chuan1[u] = max(Chuan1[u], Chuan1[v]);       Chuan2[u] = max(Chuan2[u], Chuan2[v]);     }     a[u] += Chuan2[u] - Chuan1[u];     if (a[u] >= 0)       Chuan1[u] += a[u];     else       Chuan2[u] -= a[u];   }   int main() {     scanf( %d , &n);     for (int i = 1, u, v; i < n; i++) scanf( %d%d , &u, &v), add(u, v), add(v, u);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     dfs(1, -1);     printf( %lld n , Chuan1[1] + Chuan2[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[111111];   int b[111111];   int main() {     int n, m, c;     scanf( %d%d%d , &n, &m, &c);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 0; i < m; i++) scanf( %d , &b[i]);     for (int i = 1; i < m; i++) {       b[i] = (b[i] + b[i - 1]) % c;     }     for (int i = 0; i < n; i++) {       int f = max(0, m - (n - i));       int t = min(i, m - 1);       int s = b[t] - (f == 0 ? 0 : b[f - 1]);       s = (s % c + c) % c;       a[i] = (a[i] + s) % c;       cout << a[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long score(long long a, long long b) {     if (a == b)       return 0;     else if ((a == 3 && b == 2) || (a == 2 && b == 1) || (a == 1 && b == 3))       return 1;     else       return 0;   }   int main(void) {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long k, a, b, ca, cb, ncl, cl, csa, csb, pa, pb;     long long d[4][4][4] = {};     long long ch[4][4][4];     long long sc[100][2] = {};     cin >> k >> a >> b;     for (long long k = 1; k <= 2; k++)       for (long long i = 1; i <= 3; i++)         for (long long j = 1; j <= 3; j++) cin >> ch[i][j][k];     d[a][b][0] = 1;     d[a][b][1] = score(a, b);     d[a][b][2] = score(b, a);     sc[1][0] = score(a, b);     sc[1][1] = score(b, a);     ca = a;     cb = b;     while (true) {       long long na, nb;       na = ch[ca][cb][1];       nb = ch[ca][cb][2];       if (d[na][nb][0] == 0) {         d[na][nb][0] = d[ca][cb][0] + 1;         d[na][nb][1] = d[ca][cb][1] + score(na, nb);         d[na][nb][2] = d[ca][cb][2] + score(nb, na);         sc[d[na][nb][0]][0] = d[na][nb][1];         sc[d[na][nb][0]][1] = d[na][nb][2];         ca = na;         cb = nb;       } else {         ncl = d[na][nb][0] - 1;         cl = d[ca][cb][0] - d[na][nb][0] + 1;         csa = d[ca][cb][1] - d[na][nb][1] + score(na, nb);         csb = d[ca][cb][2] - d[na][nb][2] + score(nb, na);         break;       }     }     if (k <= ncl) {       pa = sc[k][0];       pb = sc[k][1];     } else {       long long km = (k - ncl) % cl;       pa = sc[ncl + km][0] + ((k - ncl) / cl) * csa;       pb = sc[ncl + km][1] + ((k - ncl) / cl) * csb;     }     cout << pa <<     << pb << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool sortin(const pair<long long int, long long int> &e,               const pair<long long int, long long int> &f) {     return (e.first < f.first);   }   long long int i, j, k, l, m, n, c, p, q, ts, mn = 10e17;   long long int a[200002], b[200002], x[200002];   int main() {     {       ios_base::sync_with_stdio(false);       cin.tie(0);       cout.tie(0);     };     cin >> n;     cin >> m;     l = n - m;     string s, t, u, v;     cin >> s;     cin >> t;     v = s;     for (long long int i = 0; i <= n - 1; i++) u +=  a ;     for (long long int i = 0; i <= n - 1; i++) {       if (!l) break;       if (s[i] == t[i])         u[i] = s[i], v[i] =  . , l--;       else         u[i] =  a ;     }     if (l) {       for (long long int i = 0; i <= n - 1; i++) {         if (v[i] ==  . ) continue;         if (c % 2)           u[i] = s[i], v[i] =  . ;         else           u[i] = t[i], v[i] =  . ;         c++;         if (c % 2 == 0) l--;         if (!l) break;       }     }     if (l) {       cout << -1;       return 0;     }     for (long long int i = 0; i <= n - 1; i++) {       if (v[i] ==  . ) {         if (s[i] != u[i]) p++;         if (t[i] != u[i]) q++;         continue;       }       if (s[i] ==  a ) {         u[i] =  b ;         if (t[i] ==  b ) u[i] =  c ;       } else if (t[i] ==  a ) {         u[i] =  b ;         if (s[i] ==  b ) u[i] =  c ;       }       if (s[i] != u[i]) p++;       if (t[i] != u[i]) q++;     }     if (p != m || q != m) {       cout << -1;       return 0;     }     cout << u;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 100000 + 10;   long long n, K, a[N], b[N];   struct Node {     long long v, id;   } p[N];   bool cmp(Node x, Node y) { return x.v > y.v; }   long long get(long long x, long long v) { return -3ll * (x * x + x) + v + 1; }   long long func(long long v, long long y) {     long long l = 0, r = v, mid, ret = 0;     while (l <= r) {       mid = (l + r) >> 1ll;       if (get(mid, v) >= y)         ret = mid, l = mid + 1;       else         r = mid - 1;     }     return ret;   }   long long calc(long long y) {     long long sum = 0;     for (long long i = 1; i <= n; ++i) sum += func(a[i], y);     return sum;   }   long long ans, sum;   void solve() {     long long l = -4e18, r = 1e18, mid;     while (l <= r) {       mid = (l + r) >> 1ll;       if (calc(mid) <= K)         ans = mid, r = mid - 1;       else         l = mid + 1;     }   }   signed main() {     scanf( %lld%lld , &n, &K);     for (long long i = 1; i <= n; ++i) scanf( %lld , &a[i]);     solve();     sum = 0;     for (long long i = 1; i <= n; ++i) {       b[i] = func(a[i], ans);       sum += b[i];       p[i].v = get(b[i], a[i]);       p[i].id = i;     }     sort(p + 1, p + n + 1, cmp);     for (long long i = 1; i <= n; ++i)       if (sum < K && a[p[i].id] > b[p[i].id]) sum++, b[p[i].id]++;     for (long long i = 1; i <= n; ++i) printf( %lld  , b[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[6];   int main() {     for (int i = 0; i < 6; ++i) {       cin >> arr[i];     }     long long res = 0;     for (int i = 0; i < 3; ++i) {       res += arr[i];     }     res *= res;     for (int i = 0; i < 6; i += 2) {       res -= arr[i] * arr[i];     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   signed solve();   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int tests = 1;     while (tests--) solve();     return 0;   }   signed solve() {     int n, k;     cin >> n >> k;     if (n <= 1ll * k * (k - 1)) {       cout <<  YES n ;       for (int i = 0; i < n; ++i) {         cout << (i % k) + 1 <<     << (i / k + i % k + 1) % k + 1 <<   n ;       }     } else {       cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   ostream &operator<<(ostream &os, const pair<A, B> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T_container, typename T = typename enable_if<                                       !is_same<T_container, string>::value,                                       typename T_container::value_type>::type>   ostream &operator<<(ostream &os, const T_container &v) {     os <<  { ;     string sep;     for (const T &x : v) os << sep << x, sep =  ,  ;     return os <<  } ;   }   void dbg_out() { cerr << endl; }   template <typename Head, typename... Tail>   void dbg_out(Head H, Tail... T) {     cerr <<     << H;     dbg_out(T...);   }   const int MAX_N = 1e5 + 5;   const long long MOD = 1e9 + 7;   const long long INF = 1e9;   const long double EPS = 1e-9;   void solve() {     string str;     cin >> str;     int len = ((int)str.size());     if (len & 1)       cout <<  NO  << endl;     else {       for (int i = 0; i < len / 2; ++i) {         if (str[i] != str[i + len / 2]) {           cout <<  NO  << endl;           return;         }       }       cout <<  YES  << endl;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int tc = 1;     cin >> tc;     for (int t = 1; t <= tc; t++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[123456];     int avg[123456];     for (int i = 0; i < n; i++) cin >> a[i];     long long count = 0;     for (int i = 0; i < n; i++) count += a[i];     int Max = count / n;     int rem = count % n;     long long c = 0;     for (int i = 0; i < n; i++) avg[i] = Max;     sort(a, a + n);     for (int i = n - 1; i >= 0; i--) {       if (!rem) continue;       avg[i]++;       rem--;     }     for (int i = 0; i < n; i++)       if (a[i] > avg[i]) c += a[i] - avg[i];     cout << c << endl;     return 0;   }


#include <bits/stdc++.h>   using std::min;   const int MAXL = 3000000 + 9, MAXM = 5000 + 9, M = 4194304;   int tr[M + M];   int ask(int k) {     int i;     for (i = 1; i < M;)       if (k > tr[i + i]) {         k -= tr[i + i];         i += i + 1;       } else         i += i;     return i - M;   }   void change(int u, int v) {     for (u += M; u; u >>= 1) tr[u] += v;   }   int l[MAXM], r[MAXM];   int fa[MAXL];   char s[MAXL], ans[MAXL];   int main() {     int k, m, i, j, t, tot;     int ll, rr, ori;     char *p;     scanf( %s%d%d , s + 1, &k, &m);     for (i = 1; i <= m; ++i) scanf( %d%d , l + i, r + i);     for (i = 1; i <= k; ++i) change(i, 1);     tot = k;     for (i = m; i; --i) {       if (r[i] + 1 > tot) continue;       ll = r[i] - l[i] + 1;       rr = min(tot, r[i] + ll);       for (j = 1; j <= rr - r[i]; ++j) {         m = ll >> 1;         ori = j <= m ? l[i] - 1 + j + j : l[i] - 2 + 2 * (j - m);         fa[t = ask(r[i] + 1)] = ask(ori);         change(t, -1);       }       tot -= rr - r[i];     }     p = s;     for (i = 1; i <= k; ++i) ans[i] = fa[i] ? ans[fa[i]] : *++p;     puts(ans + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int sn() {     int n = 0;     int ch = getchar();     while (ch < 48) ch = getchar();     while (ch > 47) n = (n << 3) + (n << 1) + ch -  0 , ch = getchar();     return n;   }   double dp[2005][2005];   int main() {     int i, j, n, t;     double p;     scanf( %d %lf %d , &n, &p, &t);     dp[0][0] = 1;     for (i = 1; i <= t; i++) {       dp[i][0] = pow(1 - p, i);       dp[i][n] = dp[i - 1][n] + dp[i - 1][n - 1] * p;       for (j = 1; j <= n - 1; j++) {         dp[i][j] = dp[i - 1][j] * (1 - p) + dp[i - 1][j - 1] * p;       }     }     double ans = 0;     for (i = 1; i <= n; i++) {       ans = ans + i * dp[t][i];     }     printf( %0.6lf  , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const double EPS = 1e-8;   const int mod = 1e9 + 7;   const int N = 3e6 + 10;   const long long INF = 1e18;   long long power(long long x, long long y) {     long long t = 1;     while (y > 0) {       if (y % 2)         y -= 1, t = t * x % mod;       else         y /= 2, x = x * x % mod;     }     return t;   }   long long dis[N];   vector<pair<long long, long long> > G[N];   int n, s, q;   void make(int C, int s, int e) {     if (s == e) {       G[C + n].push_back(make_pair(s, 0));       G[s].push_back(make_pair(C + n + 6 * n, 0));       return;     }     int m = (s + e) >> 1;     make(C * 2, s, m);     make(C * 2 + 1, m + 1, e);     G[C + n].push_back(make_pair(C * 2 + n, 0));     G[C + n].push_back(make_pair(C * 2 + 1 + n, 0));     G[C * 2 + n + 6 * n].push_back(make_pair(C + n + 6 * n, 0));     G[C * 2 + 1 + n + 6 * n].push_back(make_pair(C + n + 6 * n, 0));   }   void upd1(int C, int S, int E, int s, int e, int v, int w) {     if (s > E || e < S) return;     if (s <= S && E <= e) {       G[v].push_back(make_pair(C + n, w));       return;     }     int m = (S + E) >> 1;     upd1(C * 2, S, m, s, e, v, w);     upd1(C * 2 + 1, m + 1, E, s, e, v, w);   }   void upd2(int C, int S, int E, int s, int e, int v, int w) {     if (s > E || e < S) return;     if (s <= S && E <= e) {       G[C + n + 6 * n].push_back(make_pair(v, w));       return;     }     int m = (S + E) >> 1;     upd2(C * 2, S, m, s, e, v, w);     upd2(C * 2 + 1, m + 1, E, s, e, v, w);   }   priority_queue<pair<long long, long long> > P;   int main() {     scanf( %d%d%d , &n, &q, &s);     s--;     make(1, 0, n - 1);     for (int i = 0; i < q; ++i) {       int c;       scanf( %d , &c);       if (c == 1) {         int v, u, w;         scanf( %d%d%d , &v, &u, &w);         v--;         u--;         G[v].push_back(make_pair(u, w));       } else if (c == 2) {         int v, l, r, w;         scanf( %d%d%d%d , &v, &l, &r, &w);         v--;         l--;         r--;         upd1(1, 0, n - 1, l, r, v, w);       } else {         int v, l, r, w;         scanf( %d%d%d%d , &v, &l, &r, &w);         v--;         l--;         r--;         upd2(1, 0, n - 1, l, r, v, w);       }     }     for (int i = 0; i < N; ++i) dis[i] = INF;     dis[s] = 0;     P.push(make_pair(0, s));     while (!P.empty()) {       pair<long long, long long> nx = P.top();       P.pop();       nx.first = -nx.first;       if (dis[nx.second] < nx.first) continue;       for (int i = 0; i < G[nx.second].size(); ++i) {         long long cst = nx.first + G[nx.second][i].second;         int v = G[nx.second][i].first;         if (dis[v] > cst) {           dis[v] = cst;           P.push(make_pair(-cst, v));         }       }     }     for (int i = 0; i < n; ++i) {       if (dis[i] >= INF) dis[i] = -1;       printf( %lld%c , dis[i], (i + 1 == n) ?   n  :    );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     char ch = getchar();     long long p = 1, data = 0;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) p = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       data = data * 10 + (ch ^ 48);       ch = getchar();     }     return p * data;   }   inline long long qpow(long long a, long long b) {     long long r = 1;     while (b) {       if (b & 1) r = a * r;       a = a * a;       b >>= 1;     }     return r;   }   inline long long gcd(long long a, long long b) {     while (b ^= a ^= b ^= a %= b)       ;     return a;   }   inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }   void exgcd(long long a, long long b, long long &x, long long &y) {     if (!b)       x = 1, y = 0;     else {       exgcd(b, a % b, y, x);       y -= x * (a / b);     }   }   const int mod = 1e9 + 7, maxn = 1e5 + 5;   char s[maxn], ans1[maxn], ans2[maxn];   int num[10];   int main() {     scanf( %s , s + 1);     int n = strlen(s + 1);     for (int i = 1; i <= n; i++) num[s[i] -  0 ]++;     int m = n;     int mx = 0, t1 = 0, t2 = 0;     for (int i = 1; i <= 5; i++) {       int t = 10 - i, now = 0;       if (!num[i] || !num[t]) continue;       int tn[10][2];       for (int j = 0; j <= 9; j++) tn[j][1] = tn[j][0] = num[j];       tn[i][1]--, tn[t][0]--;       for (int j = 0; j <= 9; j++) now += min(tn[j][1], tn[9 - j][0]);       if (now >= mx) {         t1 = i;         t2 = t;         mx = now;       }     }     int tn[10][2];     for (int j = 0; j <= 9; j++) tn[j][1] = tn[j][0] = num[j];     int pre = 0;     if (t1 || t2) {       pre = 1;       ans1[m] = t1 +  0 ;       ans2[m] = t2 +  0 ;       tn[t1][0]--;       tn[t2][1]--;       m--;     }     int flag = 0;     for (int i = m; i >= 1 + flag; i--) {       int OK = 0, f1 = 0, f2 = 0;       if (mx) {         mx--;         for (int j = 0; j <= 9; j++)           if (min(tn[j][0], tn[9 - j][1])) {             tn[j][0]--;             tn[9 - j][1]--;             f1 = j;             f2 = 9 - j;             ans1[i] = j +  0 ;             ans2[i] = 9 - j +  0 ;             OK = 1;             break;           }         pre = 1;       } else {         if (pre) {           int vis[10][2];           for (int j = 0; j <= 9; j++)             if (tn[j][0] && tn[9 - j][1]) vis[j][0] = vis[9 - j][1] = 1;           int ok = 0;           for (int j = 0; j <= 9; j++)             if (tn[j][0] && !vis[j][0]) {               f1 = j;               ans1[i] = j +  0 ;               ok++;               break;             }           for (int j = 0; j <= 9; j++)             if (tn[j][1] && !vis[j][1]) {               f2 = j;               ans2[i] = j +  0 ;               ok++;               break;             }           if (ok != 2) {             ok = 0;             for (int j = 0; j <= 9; j++)               if (tn[j][0]) {                 f1 = j;                 ans1[i] = j +  0 ;                 ok++;                 break;               }             for (int j = 0; j <= 9; j++)               if (tn[j][1]) {                 f2 = j;                 ans2[i] = j +  0 ;                 ok++;                 break;               }           }           tn[f1][0]--;           tn[f2][1]--;         } else {           int ok = 0;           for (int j = 0; j <= 9; j++) {             if (j == 0 && tn[j][0] && tn[j][1]) {               f1 = 0, f2 = 0, ok = 1, OK = 1;               tn[j][0]--, tn[j][1]--;               ans1[i] =  0 , ans2[i] =  0 ;               break;             } else if (j != 0 && tn[j][0] && tn[10 - j][1]) {               f1 = j, f2 = 10 - j, ok = 1, OK = 1;               tn[j][0]--, tn[10 - j][1]--;               ans1[i] = f1 +  0 , ans2[i] = f2 +  0 ;               break;             }           }           if (!ok) {             for (int j = 0; j <= 9; j++)               if (tn[j][0]) {                 tn[j][0]--;                 ans1[i] = j +  0 ;                 f1 = j;                 ok++;                 break;               }             for (int j = 0; j <= 9; j++)               if (tn[j][1]) {                 tn[j][1]--;                 ans2[i] = j +  0 ;                 f2 = j;                 ok++;                 break;               }           }           if (ok == 2) OK = 1;         }         pre = (f1 + f2 + 1 >= 10 ? 1 : 0);       }       if (f1 == 0 && f2 == 0) {         flag++;         i++;       }     }     printf( %s , ans1 + flag + 1);     for (int i = 1; i <= flag; i++) printf( 0 );     puts(  );     printf( %s , ans2 + flag + 1);     for (int i = 1; i <= flag; i++) printf( 0 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 1e6;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       long long n, h;       cin >> h >> n;       long long arr[n];       for (int i = 0; i < n; i++) cin >> arr[i];       long long ans = 0, cp = h;       for (int i = 1; i < n; i++) {         if (cp == 0) break;         if (cp == (arr[i] + 1)) {           if ((i + 1) < n && cp == (arr[i + 1] + 2)) {             cp = arr[i + 1];             ++i;           } else {             if ((i + 1) < n) {               cp = arr[i + 1] + 1;               ++ans;             } else if (i == n - 1) {               if (cp == 2)                 continue;               else                 ++ans;             }           }         } else {           cp = arr[i] + 1;           --i;         }       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 220000;   const int MAXM = 2 * MAXN;   const int MAXS = 26;   struct SAM {     struct Node {       int nxt[MAXS], par;       int len, siz;     } st[MAXM];     int m, root, lst;     inline int newNode(int l) {       int x = ++m;       memset(st[x].nxt, 0, sizeof st[x].nxt);       st[x].par = 0;       st[x].len = l;       st[x].siz = 0;       return x;     }     void extend(char c, bool k) {       int x = c -  a ;       int p = lst, np = newNode(st[p].len + 1);       lst = np;       st[np].siz = k;       while (p && !st[p].nxt[x]) st[p].nxt[x] = np, p = st[p].par;       if (!p) {         st[np].par = root;         return;       }       int q = st[p].nxt[x];       if (st[q].len == st[p].len + 1) {         st[np].par = q;         return;       }       int nq = newNode(st[p].len + 1);       memcpy(st[nq].nxt, st[q].nxt, sizeof st[q].nxt);       st[nq].par = st[q].par;       st[np].par = st[q].par = nq;       while (p && st[p].nxt[x] == q) st[p].nxt[x] = nq, p = st[p].par;     }     void build(int n, char* s, char* t) {       static int c[MAXN], d[MAXM];       m = 0;       lst = root = newNode(0);       for (int i = 0; i < n; i++) extend(s[i], t[i] ^  1 );       memset(c, 0, sizeof c);       for (int i = 1; i <= m; i++) c[st[i].len]++;       for (int i = 1; i <= n; i++) c[i] += c[i - 1];       for (int i = 1; i <= m; i++) d[c[st[i].len]--] = i;       for (int i = m; i >= 2; i--) st[st[d[i]].par].siz += st[d[i]].siz;     }     long long query() {       long long r = 0;       for (int i = 1; i <= m; i++) r = max(r, 1ll * st[i].len * st[i].siz);       return r;     }   } sam;   char s[MAXN], t[MAXN];   int main() {     int n;     scanf( %d , &n);     scanf( %s%s , s, t);     sam.build(n, s, t);     printf( %lld n , sam.query());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2e6 + 100;   const long long inf = 0x3f3f3f3f;   const long long iinf = 1 << 30;   const long long linf = 2e18;   const long long mod = 998244353;   const double eps = 1e-7;   template <class T = long long>   T chmin(T &a, T b) {     return a = min(a, b);   }   template <class T = long long>   T chmax(T &a, T b) {     return a = max(a, b);   }   template <class T = long long>   inline void red(T &x) {     x -= mod, x += x >> 31 & mod;   }   template <class T = long long>   T read() {     T f = 1, a = 0;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       a = (a << 3) + (a << 1) + ch -  0 ;       ch = getchar();     }     return a * f;   }   long long n, sqrt_n = 300;   long long ans, val;   long long a[maxn], cnt[maxn], sum[maxn], t[maxn], cnt1[maxn], cnt2[maxn];   vector<long long> pos;   void modify(long long x, long long pos) {     cnt2[cnt1[x]]--;     cnt1[x] += pos;     cnt2[cnt1[x]]++;   }   signed main() {     n = read();     for (long long i = (1); i <= (n); ++i) a[i] = read(), cnt[a[i]]++;     long long maxx = 0, tot = 0, val = 0;     for (long long i = (1); i <= (n); ++i) {       if (cnt[i] == maxx) tot++;       if (cnt[i] > maxx) tot = 1, maxx = cnt[i], val = i;     }     if (tot > 1) {       printf( %lld n , n);       return 0;     }     for (long long i = (1); i <= (n); ++i)       if (cnt[i] > sqrt_n && i != val) pos.push_back(i);     for (long long i : pos) {       for (long long j = (1); j <= (n); ++j)         sum[j] = sum[j - 1] + (a[j] == i ? -1 : (a[j] == val ? 1 : 0));       for (long long j = (-n); j <= (n); ++j) t[j + n] = linf;       t[n] = 0;       for (long long j = (1); j <= (n); ++j) {         if (t[sum[j] + n] != linf)           chmax(ans, j - t[sum[j] + n]);         else           t[sum[j] + n] = j;       }     }     for (long long i = (1); i <= (sqrt_n); ++i) {       for (long long j = (1); j <= (n); ++j) cnt1[j] = cnt2[j] = 0;       long long l = 1, r = 0;       while (r < n) {         modify(a[++r], 1);         while (cnt1[a[r]] > i) modify(a[l++], -1);         if (cnt2[i] >= 2) chmax(ans, r - l + 1);       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[100005], l = -1000000000, r = 1000000000;   string b;   int main() {     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     cin >> b;     for (int i = 4; i < n; ++i) {       if (b[i] ==  0  && b[i - 1] ==  1 ) {         int minim = a[i];         for (int j = i - 4; j < i; ++j) minim = min(minim, a[j]);         r = min(r, minim - 1);       } else if (b[i] ==  1  && b[i - 1] ==  0 ) {         int maxim = a[i];         for (int j = i - 4; j < i; ++j) maxim = max(maxim, a[j]);         l = max(l, maxim + 1);       }     }     cout << l <<     << r <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int>> adj;   bool vis[200000 + 9];   int cntnode;   map<int, int> colorcnt;   int c[200000 + 9];   int mx;   void dfs(int node) {     if (vis[node]) {       return;     }     cntnode++;     colorcnt[c[node]]++;     mx = max(mx, colorcnt[c[node]]);     vis[node] = 1;     for (int i = 0; i < (int)adj[node].size(); i++) {       int ch = adj[node][i];       dfs(ch);     }   }   int main() {     int n, m, k;     cin >> n >> m >> k;     adj.resize(n);     for (int i = 0; i < n; i++) {       cin >> c[i];     }     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       adj[x - 1].push_back(y - 1);       adj[y - 1].push_back(x - 1);     }     int sum = 0;     for (int i = 0; i < n; i++) {       cntnode = 0;       colorcnt.clear();       mx = 0;       if (!vis[i]) {         dfs(i);       }       sum += (cntnode - mx);     }     cout << sum;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   struct vertex {     long long inside, edge;     int size = 1;     vector<int> child;   };   int ans = 0;   multiset<long long> st;   vertex g[100009];   void dfs(int v, bool flag, long long deep) {     if (flag) {       for (int i = 0; i < g[v].child.size(); i++) {         dfs(g[v].child[i], 1, 0);         g[v].size += g[g[v].child[i]].size;       }       return;     }     if (deep - g[v].inside > *st.begin()) flag = 1;     st.insert(deep);     for (int i = 0; i < g[v].child.size(); i++) {       dfs(g[v].child[i], flag, deep + g[g[v].child[i]].edge);       g[v].size += g[g[v].child[i]].size;     }     if (flag) ans += g[v].size;     flag = 0;     st.erase(deep);   }   int main() {     ios_base::sync_with_stdio(0);     int n;     st.insert(100000000000000000);     cin >> n;     for (int i = 1; i <= n; i++) cin >> g[i].inside;     for (int i = 2, c; i <= n; i++) {       cin >> c >> g[i].edge;       g[c].child.push_back(i);     }     dfs(1, 0, 0);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const double pi = 3.141593;   int main() {     ios::sync_with_stdio(false);     int n, t = 0, ans = 0;     map<int, int> m;     cin >> n;     for (int i = 0; i < 2 * n; i++) {       int x;       cin >> x;       m[x]++;       t++;       if (m[x] == 2) t -= 2;       ans = max(ans, t);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c* x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug& operator<<(const c&) {       return *this;     }   };   int main() {     int n, m;     cin >> n >> m;     vector<vector<pair<int, int>>> mat(m, vector<pair<int, int>>(3));     set<int> white, red, blue;     for (int i = 0; i < m; i++) {       int x, y, z;       cin >> x >> y >> z;       mat[i][0].first = x;       mat[i][1].first = y;       mat[i][2].first = z;       bool xWhite = (white.find(x) != white.end());       bool xRed = (red.find(x) != red.end());       bool xBlue = (blue.find(x) != blue.end());       bool yWhite = (white.find(y) != white.end());       bool yRed = (red.find(y) != red.end());       bool yBlue = (blue.find(y) != blue.end());       bool zWhite = (white.find(z) != white.end());       bool zRed = (red.find(z) != red.end());       bool zBlue = (blue.find(z) != blue.end());       if (xWhite) {         white.insert(x);         red.insert(y);         blue.insert(z);       } else if (xRed) {         red.insert(x);         white.insert(y);         blue.insert(z);       } else if (xBlue) {         blue.insert(x);         white.insert(y);         red.insert(z);       } else if (yWhite) {         white.insert(y);         red.insert(x);         blue.insert(z);       } else if (yRed) {         red.insert(y);         white.insert(x);         blue.insert(z);       } else if (yBlue) {         blue.insert(y);         white.insert(x);         red.insert(z);       } else if (zWhite) {         white.insert(z);         red.insert(x);         blue.insert(y);       } else if (zRed) {         red.insert(z);         blue.insert(x);         white.insert(y);       } else if (zBlue) {         blue.insert(z);         red.insert(x);         white.insert(y);       } else {         debug() <<   [                  <<  x                      :                   << (x)                 <<  ]                        [                  <<  y                      :                   << (y)                 <<  ]                        [                  <<  z                      :                   << (z) <<  ]  ;         white.insert(x);         red.insert(y);         blue.insert(z);       }     }     for (int i = 1; i <= n; i++) {       if (white.find(i) != white.end()) {         cout <<  1  ;       } else if (red.find(i) != red.end()) {         cout <<  2  ;       } else {         cout <<  3  ;       }     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   template<typename A, typename B = A> using pr = pair<A, B>;   #define vec vector   #define ar array   #define len(a) ((int)((a).size()))   #define all(a) (a).begin(), (a).end()      int q;   vec<int> used;   int timer;      inline void merge(vec<pr<int>> &a, vec<pr<int>> &b, vec<pr<int>> &res) {       timer++;       int j = 0;       auto insert = [&](pr<int> p) {           if (used[p.first] == timer)               return;           used[p.first] = timer;           res.push_back(p);       };       for (int i = 0; i < len(a); i++) {           while (len(res) <= q && j < len(b) && b[j].second >= a[i].second) {               insert(b[j]);               j++;           }           if (len(res) <= q)               insert(a[i]);           else               break;       }       while (len(res) <= q && j < len(b)) {           insert(b[j]);           j++;       }   }      struct my_stack {       vec<pr<vec<pr<int>>>> st;          my_stack() {}          inline void pop_back() {           assert(len(st));           st.pop_back();       }          inline void push_back(vec<pr<int>> &a) {           if (!len(st)) {               st.emplace_back(a, a);               return;           }           st.emplace_back(vec<pr<int>>{}, a);           merge(st.end()[-2].first, a, st.back().first);       }          inline void rev() {           assert(len(st));           reverse(all(st));           st[0].first = st[0].second;           for (int i = 1; i < len(st); i++) {               st[i].first.clear();               merge(st[i].second, st[i - 1].first, st[i].first);           }       }          inline int get_min() {           assert(len(st));           if (len(st.back().first) <= q)               return -1;           return st.back().first.back().second;       }   };      struct my_queue {       my_stack front, back;          my_queue() {}          inline void pop() {           if (!len(front.st)) {               swap(front, back);               front.rev();           }           front.pop_back();       }          inline void push(vec<pr<int>> &a) {           back.push_back(a);       }          inline int get_min() {           assert(len(front.st) + len(back.st));           if (!len(front.st))               return back.get_min();           if (!len(back.st))               return front.get_min();           vec<pr<int>> cur;           merge(front.st.back().first, back.st.back().first, cur);           if (len(cur) <= q)               return -1;           return cur.back().second;       }   };      int main() {       ios::sync_with_stdio(false), cin.tie(nullptr);          int n;       cin >> n >> q;       vec<vec<int>> a(n, vec<int>(n));       for (auto &xs : a)           for (auto &x : xs)               cin >> x;          vec<vec<pr<int>>> left(n);       used.resize(n * n + 1);       vec<int> ans(n);       for (int j = 0; j < n; j++) {           my_queue que;           int prev = 0;           for (int i = 0; i < n; i++) {               vec<pr<int>> nleft{{a[i][j], j}};               for (auto [x, pos] : left[i])                   if (x != a[i][j] && len(nleft) <= q)                       nleft.emplace_back(x, pos);               left[i] = nleft;                  que.push(left[i]);               while (que.get_min() >= j - (i - prev)) {                   que.pop();                   prev++;               }               // cerr << i <<     << j <<  :   << i - prev << endl;               ans[i - prev]++;           }       }          for (int i = n - 2; i >= 0; i--)           ans[i] += ans[i + 1];       for (auto x : ans)           cout << x <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     string s, t, ss;     cin >> n >> s;     ss = s;     t =  abacaba ;     int cnt = 0;     for (int i = 0; i < n - 6; i++) {       int j, k = 0;       for (j = i; j < i + 7; j++) {         if (s[j] != t[k]) break;         k++;       }       if (j == i + 7) cnt++;     }     if (cnt == 1) {       cout <<  Yes             <<   n ;       for (int i = 0; i < n; i++) {         if (s[i] ==  ? ) s[i] =  z ;       }       cout << s <<   n ;     } else if (cnt > 1) {       cout <<  No             <<   n ;       return;     } else {       cnt = 0;       for (int i = 0; i < n - 6; i++) {         cnt = 0;         int j, k = 0;         for (j = i; j < i + 7; j++) {           if (s[j] != t[k] && s[j] !=  ? ) break;           s[j] = t[k];           k++;         }         if (j == i + 7) {           for (int x = 0; x < n - 6; x++) {             int y, z = 0;             for (y = x; y < x + 7; y++) {               if (s[y] != t[z]) break;               z++;             }             if (y == x + 7) cnt++;           }           if (cnt == 1) {             cout <<  Yes                   <<   n ;             for (int i = 0; i < n; i++) {               if (s[i] ==  ? ) s[i] =  z ;             }             cout << s <<   n ;             return;           } else {             for (int i = 0; i < n; i++) s[i] = ss[i];           }         } else {           for (int i = 0; i < n; i++) s[i] = ss[i];         }       }       cout <<  No             <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long tt;     cin >> tt;     while (tt--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<int> id(n + 1);     for (int i = 0; i < n; i++) {       int x;       cin >> x;       id[x] = i;     }     vector<int> dp(n + 1, 1);     for (int i = 2; i <= n; i++) {       if (id[i] > id[i - 1]) {         dp[i] += dp[i - 1];       }     }     cout << n - *max_element(dp.begin(), dp.end());   }


#include<bits/stdc++.h>   #define fo(i,a,b) for(int i=a;i<=b;i++)   #define fd(i,a,b) for(int i=a;i>=b;i--)   using namespace std;      typedef long long LL;   typedef pair<int,int> pr;      const int maxn=505;      int n,m;   char mp[maxn][maxn];      int T;   int main()   {    scanf( %d ,&T);    while (T--)    {     scanf( %d %d ,&n,&m);     fo(i,1,n) scanf( %s ,mp[i]+1);          fo(i,1,n) if (i%3==1)      fo(j,1,m) mp[i][j]= X ;     fo(i,1,n) if (i%3==1 && i+3<=n)     {      bool pd=0;      fo(j,1,m) if (mp[i+1][j]== X )      {       mp[i+2][j]= X ;       pd=1;       break;      }      if (pd) continue;      fo(j,1,m) if (mp[i+2][j]== X )      {       mp[i+1][j]= X ;       pd=1;       break;      }      if (pd) continue;      mp[i+1][1]=mp[i+2][1]= X ;     }     if (n%3==0)     {      fo(j,1,m) if (mp[n][j]== X ) mp[n-1][j]= X ;     }          fo(i,1,n) printf( %s n ,mp[i]+1);    }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T sqr(T x) {     return x * x;   }   const double EPS = 1e-6;   const int INF = 0x3fffffff;   const long long LINF = INF * 1ll * INF;   const double PI = acos(-1.0);   using namespace std;   char g[505][505];   int cnt[505];   int dp[2][505][505];   int C[505][505];   int mod;   void Add(int &ans, int v) {     ans += v;     if (ans >= mod) ans -= mod;   }   int main(void) {     int n, m;     scanf( %d %d %d , &n, &m, &mod);     C[0][0] = 1;     for (int i = 1; i <= n; i++) {       C[i][0] = 1;       for (int j = 1; j <= i; j++)         C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;     }     for (int i = 0; i < (m); ++i) {       scanf( %s , g[i]);       for (int j = 0; j < (n); ++j) cnt[j] += g[i][j] -  0 ;     }     int cnt0 = 0, cnt1 = 0;     for (int i = 0; i < (n); ++i) {       if (cnt[i] > 2) {         puts( 0 );         return 0;       }       if (cnt[i] == 1)         cnt1++;       else if (cnt[i] == 0)         cnt0++;     }     int now = 0;     dp[now][cnt0][cnt1] = 1;     for (int i = m; i < n; i++) {       memset(dp[now ^ 1], 0, sizeof(dp[now ^ 1]));       for (int j = 0; j <= n; j++) {         for (int k = 0; k + j <= n; k++) {           if (dp[now][j][k] == 0) continue;           if (j >= 2)             Add(dp[now ^ 1][j - 2][k + 2], 1ll * C[j][2] * dp[now][j][k] % mod);           if (k >= 2)             Add(dp[now ^ 1][j][k - 2], 1ll * C[k][2] * dp[now][j][k] % mod);           if (j && k)             Add(dp[now ^ 1][j - 1][k],                 1ll * C[k][1] * C[j][1] % mod * dp[now][j][k] % mod);         }       }       now ^= 1;     }     printf( %d n , dp[now][0][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 21;   const long long MOD = 1000000007;   int n;   long long m;   long long C[MAXN][MAXN];   long long g[MAXN], s[MAXN], ten[MAXN], eight[MAXN];   inline void update(long long& x, long long y) { y %= MOD, x += y, x %= MOD; }   inline long long P(const long long& x, int y) {     if (x < y) return 0;     long long res = 1;     for (long long i = x; i >= x - y + 1; --i) res = (res * i) % MOD;     return res;   }   inline pair<int, int> get(long long num, int x) {     int res4 = 0, res7 = 0;     for (int i = 1; i <= x; ++i) num /= 10;     for (long long i = num; i > 0; i /= 10)       if (i % 10 == 4)         res4++;       else if (i % 10 == 7)         res7++;     return pair<int, int>(res4, res7);   }   inline long long F(const long long& num, int x, int a, int b) {     pair<int, int> p = get(num, x);     a -= p.first, b -= p.second;     if (a < 0 || b < 0 || a + b > x) return 0;     return (((C[x][a] * C[x - a][b]) % MOD) * eight[x - a - b]) % MOD;   }   inline long long G(long long num, int x, int a, int b) {     long long res = 0;     for (; num + ten[x] <= m; num += ten[x]) update(res, F(num, x, a, b));     if (x > 0) {       if (num <= m) update(res, G(num, x - 1, a, b));     } else       update(res, F(num, x, a, b));     return res;   }   inline void preProcess() {     ten[0] = eight[0] = 1;     for (int i = 1; i < MAXN; ++i)       ten[i] = ten[i - 1] * 10, eight[i] = eight[i - 1] * 8;     while (ten[n] <= m) n++;     C[0][0] = 1;     for (int i = 1; i < MAXN; ++i) {       C[i][0] = C[i][i] = 1;       for (int j = 1; j < i; ++j) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;     }     for (int i = 0; i <= n; ++i)       for (int j = 0; j <= i; ++j) update(g[i], G(0, n - 1, j, i - j));     g[0]--;   }   inline long long dp(int s, int x, int y, int z) {     if (y < 0 || z < 0) return 0;     if (x == s) return (y == 0) ? 1 : 0;     long long res = 0;     for (int i = 0; i <= y; ++i) {       long long k = (C[y][i] * P(g[x], i)) % MOD;       update(res, k * dp(s, x + 1, y - i, z - i * x));     }     return res;   }   int main() {     scanf( %I64d n , &m);     preProcess();     long long res = 0;     for (int s = 1; s <= n; ++s) update(res, g[s] * dp(s, 0, 6, s - 1));     printf( %I64d n , res);     fclose(stdin);     fclose(stdout);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(3)   using namespace std;   const int Mod = 1e9 + 7;   const int N = 5e5 + 100;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3f;   inline long long dpow(long long a, long long b) {     long long r = 1, t = a;     while (b) {       if (b & 1) r = (r * t) % Mod;       b >>= 1;       t = (t * t) % Mod;     }     return r;   }   inline long long fpow(long long a, long long b) {     long long r = 1, t = a;     while (b) {       if (b & 1) r = (r * t);       b >>= 1;       t = (t * t);     }     return r;   }   struct node {     int id, w;     bool operator<(const node &oth) const { return w > oth.w; }   };   struct Ask {     int op, x;   } qi[N];   pair<int, int> eg[N];   set<node> s[N];   int fa[N], sz[N], n, m, q;   int a[N];   bool vis[N];   stack<pair<int, int> > st;   void Init() {     for (int i = 1; i <= n; i++) fa[i] = i, sz[i] = 1;   }   int Find(int x) {     while (x != fa[x]) x = fa[x];     return x;   }   void Merge(int x, int y) {     x = Find(x);     y = Find(y);     if (x == y) {       st.push({0, 0});       return;     }     if (sz[x] > sz[y]) swap(x, y);     fa[x] = y;     st.push({x, y});     sz[y] += sz[x];     for (auto it : s[x]) s[y].insert(it);   }   void Cancle() {     int x = st.top().first, y = st.top().second;     st.pop();     if (!x && !y) return;     fa[x] = x;     sz[y] -= sz[x];     for (auto it : s[x]) {       if (s[y].find(it) != s[y].end()) s[y].erase(it);     }   }   int main() {     cin >> n >> m >> q;     Init();     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), s[i].insert({i, a[i]});     for (int i = 1; i <= m; i++) scanf( %d %d , &eg[i].first, &eg[i].second);     for (int i = 1; i <= q; i++) {       scanf( %d %d , &qi[i].op, &qi[i].x);       if (qi[i].op == 2) vis[qi[i].x] = true;     }     for (int i = 1; i <= m; i++)       if (!vis[i]) Merge(eg[i].first, eg[i].second);     for (int i = q; i >= 1; i--) {       if (qi[i].op == 1) continue;       int u = eg[qi[i].x].first;       int v = eg[qi[i].x].second;       Merge(u, v);     }     for (int i = 1; i <= q; i++) {       int op = qi[i].op, x = qi[i].x;       if (op == 1) {         int u = Find(x);         while (!s[u].empty() && !a[s[u].begin()->id]) s[u].erase(s[u].begin());         if (s[u].empty())           puts( 0 );         else {           printf( %d n , s[u].begin()->w);           a[s[u].begin()->id] = 0;         }       } else         Cancle();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 1e6 + 1;   const int Mod = 1e9 + 7;   const long long int MOD = 1000000007LL;   const long long int MAX = 100010LL;   template <typename T>   T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <typename T>   T power(T x, T y, long long int m = MOD) {     T ans = 1;     x %= m;     while (y > 0) {       if (y & 1ll) ans = (ans * x) % m;       y >>= 1ll;       x = (x * x) % m;     }     return ans % m;   }   template <typename T, typename T1>   T mod(T x, T1 p) {     x %= p;     if (x < 0) x += p;     return x;   }   void leastPrimeFactor(vector<int> &least_prime, int n) {     least_prime[1] = 1;     for (int i = 2; i <= n; i++) {       if (least_prime[i] == 0) {         least_prime[i] = i;         for (int j = 2 * i; j <= n; j += i)           if (least_prime[j] == 0) least_prime[j] = i;       }     }   }   bool dfs(vector<vector<int>> &graph, vector<int> &g, int i) {     int prev = 0;     queue<int> q;     q.push(i);     while (!q.empty()) {       int n = q.size();       while (n--) {         int t = q.front();         q.pop();         if (g[t] == -1) {           g[t] = prev;           for (int j : graph[t]) q.push(j);         }         if (g[t] != prev) return 0;       }       prev ^= 1;     }     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<vector<int>> adj(n);     vector<vector<int>> mat(n, vector<int>(n, 0));     int cnt = n;     for (int j = 0; j < n; j++) {       string s;       cin >> s;       for (int i = 0; i < s.size(); i++)         if (s[i] ==  1 ) adj[j].push_back(i);     }     int m;     cin >> m;     vector<int> p(m);     for (int i = 0; i < m; i++) cin >> p[i], p[i]--;     for (int i = 0; i < n; i++) {       queue<int> q;       q.push(i);       int dis = 0;       vector<int> vis(n, 0);       vis[i] = 1;       while (!q.empty()) {         int nu = q.size();         while (nu--) {           int no = q.front();           q.pop();           mat[i][no] = dis;           for (int &ne : adj[no])             if (!vis[ne]) vis[ne] = 1, q.push(ne);         }         dis++;       }     }     vector<int> ans;     ans.push_back(p[0]);     int i = 0;     while (i < p.size()) {       int next = i + 1;       for (int j = 0; j < n; j++)         if (i + mat[p[i]][j] < p.size() && p[i + mat[p[i]][j]] == j)           next = max(next, i + mat[p[i]][j]);       if (next < p.size()) ans.push_back(p[next]);       i = next;     }     cout << ans.size() << endl;     for (int &i : ans) cout << i + 1 <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t;     cin >> n;     set<long long int> s;     while (n--) {       cin >> t;       s.insert(t);     }     if (s.size() >= 4)       cout <<  NO ;     else {       if (s.size() == 3) {         auto it1 = s.begin();         auto it2 = it1;         auto it3 = it2;         it2++;         it3++;         it3++;         if (2 * (*it2) == *it1 + *it3)           cout <<  YES ;         else           cout <<  NO ;       } else         cout <<  YES ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long exp(long long x, long long y, long long p) {     long long res = 1;     while (y) {       if (y % 2) res = (res * x % p) % p;       x = (x * x) % p;       y /= 2;     }     return res;   }   long long exp(long long x, long long y) {     long long res = 1;     while (y) {       if (y % 2) res = (res * x);       x = (x * x);       y /= 2;     }     return res;   }   int main() {     long long a, b, c;     long long sum1 = 0, sum2 = 0, sum3 = 0;     long long t;     cin >> t;     while (t--) {       cin >> a >> b >> c;       sum1 += a;       sum2 += b;       sum3 += c;     }     if (!sum1 && !sum2 && !sum3)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, k, flag = 0;     cin >> n >> k;     if (3 * n > k) {       cout << 3 * n - k <<   n ;     } else {       cout <<  0             <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int a, ord;   } p[10005];   int b[10005];   bool cmp(node a, node b) { return a.a < b.a; }   int main() {     int n;     while (~scanf( %d , &n)) {       memset(b, 0, sizeof(b));       for (int i = 0; i < n; i++) {         scanf( %d , &p[i].a);         b[p[i].a]++;         p[i].ord = i + 1;       }       sort(p, p + n, cmp);       int sum = 1;       int ss[2], A = 0;       int sss = -1;       int flag = 1;       int i;       for (i = 1; i < n; i++) {         if (p[i].a == p[i - 1].a)           flag++;         else {           if (flag >= 3) {             sss = p[i - 1].a;             flag = -1;             break;           } else if (flag == 2)             ss[A++] = p[i - 1].a;           flag = 1;           if (A == 2) {             flag = -2;             break;           }         }       }       if (flag >= 3) {         sss = p[i - 1].a;         flag = -1;       } else if (flag == 2)         ss[A++] = p[i - 1].a;       flag = 1;       if (A == 2) {         flag = -2;       }       if (sss != -1) flag = -1;       if (flag > 0)         cout <<  NO  << endl;       else {         cout <<  YES  << endl;         if (flag == -1) {           for (int i = 0; i < 3; i++) {             flag = 1;             for (int j = 0; j < n; j++) {               if (j != 0) printf(   );               if (flag && p[j].a == sss) {                 if (i == 0)                   printf( %d %d %d , p[j].ord, p[j + 1].ord, p[j + 2].ord);                 else if (i == 1)                   printf( %d %d %d , p[j + 1].ord, p[j + 2].ord, p[j].ord);                 else                   printf( %d %d %d , p[j + 2].ord, p[j].ord, p[j + 1].ord);                 j += 2;                 flag = 0;               } else                 printf( %d , p[j].ord);             }             printf(  n );           }         } else {           for (int i = 0; i < 3; i++) {             for (int j = 0; j < n; j++) {               if (j != 0) printf(   );               if (i == 0 && p[j].a == ss[0]) {                 printf( %d %d , p[j].ord, p[j + 1].ord);                 j++;               } else if (i == 1 && p[j].a == ss[0]) {                 printf( %d %d , p[j + 1].ord, p[j].ord);                 j++;               } else if (i == 2 && p[j].a == ss[1]) {                 printf( %d %d , p[j + 1].ord, p[j].ord);                 j++;               } else                 printf( %d , p[j].ord);             }             printf(  n );           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   int check_year_age(int y1, int y2) {     if (y1 - y2 > 18) return 2;     if (y1 - y2 == 18) return 1;     return 0;   }   bool check_month_day_age(int d1, int d2, int m1, int m2, int y1, int y2) {     if (check_year_age(y1, y2) == 1) {       if (m1 == m2) {         if (d1 >= d2) return 1;         return 0;       }       if (m1 > m2) return 1;       return 0;     }     if (check_year_age(y1, y2) == 2) return 1;     return 0;   }   bool check_day_month_year_valid(int d2, int m2, int y2) {     if (m2 > 12) return 0;     if (y2 % 4 != 0 || m2 != 2) {       if (m2 == 2) {         if (d2 > 28) return 0;       } else {         if (m2 == 1 || m2 == 3 || m2 == 5 || m2 == 7 || m2 == 8 || m2 == 10 ||             m2 == 12) {           if (d2 > 31) return 0;         } else if (d2 > 30)           return 0;       }     } else if (d2 > 29)       return 0;     return 1;   }   int chartoint(char x1, char x2) { return (x1 -  0 ) * 10 + (x2 -  0 ); }   char s[8];   int main() {     int d1, m1, y1, d2, m2, y2;     bool OK = 0;     scanf( %s , s);     d1 = chartoint(s[0], s[1]);     m1 = chartoint(s[3], s[4]);     y1 = chartoint(s[6], s[7]);     scanf( %s , s);     d2 = chartoint(s[0], s[1]);     m2 = chartoint(s[3], s[4]);     y2 = chartoint(s[6], s[7]);     if (check_month_day_age(d1, d2, m1, m2, y1, y2) &&         check_day_month_year_valid(d2, m2, y2)) {       OK = 1;     }     if (check_month_day_age(d1, m2, m1, d2, y1, y2) &&         check_day_month_year_valid(m2, d2, y2)) {       OK = 1;     }     if (check_month_day_age(d1, y2, m1, m2, y1, d2) &&         check_day_month_year_valid(y2, m2, d2)) {       OK = 1;     }     if (check_month_day_age(d1, y2, m1, d2, y1, m2) &&         check_day_month_year_valid(y2, d2, m2)) {       OK = 1;     }     if (check_month_day_age(d1, d2, m1, y2, y1, m2) &&         check_day_month_year_valid(d2, y2, m2)) {       OK = 1;     }     if (check_month_day_age(d1, m2, m1, y2, y1, d2) &&         check_day_month_year_valid(m2, y2, d2)) {       OK = 1;     }     if (OK)       printf( YES n );     else       printf( NO n );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     long long a, b;     cin >> t;     while (t--) {       cin >> a >> b;       long long temp = a + b;       if (a - b == 1) {         int flag = 0;         long long i;         int n = sqrt(temp);         if (temp <= 1) {           cout <<  NO  << endl;           continue;         }         if (temp <= 3) {           cout <<  YES  << endl;           continue;         }         if (temp % 2 == 0) {           cout <<  NO  << endl;           continue;         }         for (i = 3; i <= n + 1; i += 2) {           if ((temp % (1ll * i)) == 0) {             flag = 1;             break;           }         }         if (flag)           cout <<  NO  << endl;         else           cout <<  YES  << endl;       } else         cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     ;     string a;     int n;     cin >> a >> n;     for (int i = 0; i < a.length() && n; ++i) {       char x = a[i];       int y = i;       for (int j = i + 1; j <= min((int)a.length() - 1, i + n) && n; ++j)         if (x < a[j]) x = a[j], y = j;       if (y != i) {         n -= y - i;         while (i != y) {           swap(a[y - 1], a[y]);           y--;         }       }     }     cout << a << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = (long long)1e9 + 7;   const int inf = (int)1e9 + 7;   const double eps = 1e-7;   const double pi = 3.14159265359;   const int N = (int)1e4;   const int K = 4;   long long n, k;   long long dp[N][K];   signed main() {     scanf( %lld%lld , &n, &k);     if (k == 1) printf( 1 );     if (k == 2) printf( %lld , n * (n - 1) / 2 + 1);     if (k == 3) printf( %lld , n * (n - 1) * (n - 2) / 3 + n * (n - 1) / 2 + 1);     if (k == 4)       printf( %lld , n * (n - 1) * (n - 2) * (n - 3) / 8 * 3 +                          n * (n - 1) * (n - 2) / 3 + n * (n - 1) / 2 + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   bool a[1001][1001];   bool used[1001][1001];   struct T {     int i, j, d;     T(int i, int j, int d) : i(i), j(j), d(d) {}   };   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         char ch;         cin >> ch;         if (ch ==  # ) a[i][j] = true;       }     queue<T> q;     q.push(T(n, 0, 0));     used[n][0] = true;     while (!q.empty()) {       int i = q.front().i, j = q.front().j, d = q.front().d;       q.pop();       if (i == 1) {         cout << d;         return 0;       }       if (i != 0)         for (int k = 1; k <= m; k++)           if (a[i][k] && !used[0][k]) {             q.push(T(0, k, d + 1));             used[0][k] = true;           }       if (j != 0)         for (int k = 1; k <= n; k++)           if (a[k][j] && !used[k][0]) {             q.push(T(k, 0, d + 1));             used[k][0] = true;           }     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int nod(int a, int b) {     while (a && b) {       int c = a % b;       a = b;       b = c;     }     return a | b;   }   int Abs(int a) { return a >= 0 ? a : -a; }   int main() {     int n, m;     cin >> n >> m;     vector<vector<int> > e(n);     for (int i = 0; i < m; ++i) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       e[a].push_back(b);     }     vector<int> d(n, -1);     queue<int> q;     q.push(0);     d[0] = 0;     int r = 0;     while (!q.empty()) {       int i = q.front();       q.pop();       int dd = d[i] + 1;       for (int j = 0; j < (int)e[i].size(); ++j) {         int a = e[i][j];         if (d[a] >= 0)           r = nod(r, Abs(d[a] - dd));         else {           q.push(a);           d[a] = dd;         }       }     }     int c = 0;     for (int i = 0; i < n; ++i)       if (d[i] >= 0 && d[i] % r == 0) c++;     printf( %d n%d n , r, c);     for (int i = 0; i < n; ++i)       if (d[i] >= 0 && d[i] % r == 0) printf( %d  , i + 1);     return 0;   }


#include <bits/stdc++.h>   const int N = 500000;   int n, k;   char s[N + 1], t[N + 1];   int main() {     scanf( %d%d%s%s , &n, &k, s, t);     int r = 0;     long long ans = 0;     for (int i = 0; i < n; ++i) {       if (r < k) {         r *= 2;         r += t[i] - s[i];       }       r = std::min(r, k);       ans += r == k ? r : r + 1;     }     printf( %lld n , ans);     return 0;   }


#include<bits/stdc++.h>   #define inf 2000000000   #define eb emplace_back   #define pb push_back   #define fi first   #define se second   #define fastio ios::sync_with_stdio(false);cin.tie(NULL)   #define rng_23 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())   using namespace std;   typedef long long ll;   typedef pair<int,int> pii;   typedef vector<int> vi;   typedef vector<pair<int,int>> vii;   typedef set<int> si;   typedef multiset<int> msi;   typedef map<int,int> mii;   int ara[100][100],lcnt[100],wcnt[100];   int main()   {       int t;       int i,j,k;       int n,res;       cin>>t;       while(t--){           cin>>n;           for(i=0;i<n;i++)               for(j=0;j<n;j++)                   ara[i][j]=2;           for(i=0;i<n;i++){               wcnt[i]=(n-1)/2;               lcnt[i]=n-1-(n-1)/2;               ara[i][i]=0;           }              if(n%2==0){               for(i=0;i<n;i++)                   lcnt[i]--;               for(i=0;i<n;i+=2){                   ara[i][i+1]=0;                   ara[i+1][i]=0;                  }              }           //for(i=0;i<n;i++){            //       cout<<wcnt[i]<<   <<lcnt[i]<<endl;                  //}               for(i=0;i<n;i++){                   //cout<<wcnt[i]<<   <<lcnt[i]<<endl;                   for(j=0;j<n;j++){                       if(ara[i][j]==2 && wcnt[i] && lcnt[j]){                           wcnt[i]--;                           lcnt[j]--;                           ara[i][j]=1;                           ara[j][i]=-1;                       }                   }               }               for(i=0;i<n-1;i++){                   for(j=i+1;j<n;j++){                       cout<<ara[i][j]<<   ;                   }               }               cout<<  n ;                }   }


#include <bits/stdc++.h>   using namespace std;   constexpr int inf = 1e9 + 7;   constexpr long long md = 1e6;   void solve(int testi) {     int n;     cin >> n;     if (n == 0) {       cout << 1 <<   n ;       ;       return;     }     vector<int> digits = {6, 8, 4, 2};     cout << digits[n % digits.size()] <<   n ;     ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     long long testn = 1;     for (long long testi = 1; testi <= testn; testi++) {       solve(testi);     }   }


#include <bits/stdc++.h>   using namespace std;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c* x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug& operator<<(const c&) {       return *this;     }   };   void solve() {     int n, m;     cin >> n >> m;     vector<vector<char> > v(n + 1, vector<char>(m + 1));     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         cin >> v[i][j];       }     }     vector<vector<int> > pf(n + 1, vector<int>(m + 1));     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         pf[i][j] = pf[i - 1][j] + pf[i][j - 1] - pf[i - 1][j - 1];         pf[i][j] += ((v[i][j] ==  X ) ? 1 : 0);       }     }     auto get = [&](int x, int y, int x1, int y1) {       return (pf[x1][y1] - pf[x1][y - 1] - pf[x - 1][y1] + pf[x - 1][y - 1]);     };     auto check = [&](int side) {       vector<vector<int> > add(n + 1, vector<int>(m + 1));       for (int i = 1; i + 2 * side <= n; ++i) {         for (int j = 1; j + 2 * side <= m; ++j) {           long long area = (2 * side + 1) * (2 * side + 1);           if (get(i, j, i + 2 * side, j + 2 * side) == area) {             ++add[i][j];             if (i + 2 * side < n) --add[i + 2 * side + 1][j];             if (j + 2 * side < m) --add[i][j + 2 * side + 1];             if (i + 2 * side < n && j + 2 * side < m)               ++add[i + 2 * side + 1][j + 2 * side + 1];           }         }       }       vector<vector<int> > pf1(n + 1, vector<int>(m + 1));       for (int i = 1; i <= n; ++i) {         for (int j = 1; j <= m; ++j) {           pf1[i][j] =               pf1[i - 1][j] + pf1[i][j - 1] - pf1[i - 1][j - 1] + add[i][j];           int a = !!pf1[i][j], b = ((v[i][j] ==  X ) ? 1 : 0);           if (a ^ b) return 0;         }       }       return 1;     };     int l = 0, r = min(n, m), mid, ans = 1;     while (l <= r) {       mid = l + r >> 1;       if (check(mid))         ans = mid, l = mid + 1;       else         r = mid - 1;     }     cout << ans <<   n ;     int side = ans;     vector<vector<char> > res(n + 1, vector<char>(m + 1,  . ));     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (i + 2 * side <= n && j + 2 * side <= m) {           long long area = (2 * side + 1) * (2 * side + 1);           if (get(i, j, i + 2 * side, j + 2 * side) == area)             res[i + side][j + side] =  X ;         }       }     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) cout << res[i][j];       cout <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int q = 1;     while (q--) solve();   }