#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       vector<long long> p;       vector<long long> pre;       for (long long i = 0; i < n; i++) {         p.push_back(i + 1);       }       long long sum = 0;       for (long long i = 0; i < n; i++) {         sum += p[i];         pre.push_back(sum);       }       long long ind = lower_bound(pre.begin(), pre.end(), k) - pre.begin();       long long x = p[ind];       long long y = n - x - 1;       long long bb;       bb = y + (pre[ind] - k) + 1;       vector<char> ans;       for (long long i = 0; i < n; i++) {         if (i == y || i == bb)           ans.push_back( b );         else           ans.push_back( a );       }       for (int i = 0; i < n; i++) {         cout << ans[i];       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 100005;   const int D = 1005;   struct data {     int ind;     long long Sum;     data(int _ind, long long _Sum) {       ind = _ind;       Sum = _Sum;     }   };   bool compare(data A, data B) {     if (A.Sum < B.Sum) return true;     if (A.Sum == B.Sum && A.ind < B.ind) return true;     return false;   }   long long f(long long a, long long b, long long c) {     return (((a - b) * (a - b)) + (c * c));   }   long long a[MAXN], S[MAXN];   vector<data> Q;   int main() {     int N;     cin >> N;     S[0] = 0;     for (int i = 1; i <= N; i++) {       cin >> a[i];       S[i] = S[i - 1] + a[i];       data r(i, S[i]);       Q.push_back(r);     }     long long ans = MAXN * MAXN;     for (long long i = 2; i <= N; i++) {       for (long long j = i; j <= i + D && j <= N; j++) {         long long res = f(S[i - 1], S[j], j - i + 1);         ans = min(ans, res);       }     }     sort(Q.begin(), Q.end(), compare);     for (int i = 0; i < Q.size(); i++) {       for (int j = i + 1; j < Q.size() && j < i + D; j++) {         ans = min(ans, f(Q[i].Sum, Q[j].Sum, abs(Q[i].ind - Q[j].ind)));       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   const int inf = INT_MAX;   const int MAX = 1e3 + 8;   const long long MOD = 1e9 + 7;   const int TOT_PRIMES = 1e6 + 9;   const int MAX_A = 71;   const int LN = 20;   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int a[32];     memset(a, 0, sizeof(a));     for (int i = 0; i < n; i++) {       int res = 0;       for (int j = 0; j < k; j++) {         int x;         cin >> x;         res = x + res * 2;       }       a[res] = 1;     }     int f = 0;     for (int i = 0; i < (1 << k); i++) {       for (int j = 0; j < (1 << k); j++) {         if ((!(i & j)) && a[i] && a[j]) f = 1;       }     }     if (f)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     string s;     cin >> s;     vector<set<int>> v(26);     for (int i = 0, l = s.size(); i < l; i++) {       v[s[i] -  a ].insert(i);     }     int q;     cin >> q;     int t;     while (q--) {       cin >> t;       if (t == 1) {         int pos;         char c;         cin >> pos >> c;         pos--;         v[s[pos] -  a ].erase(pos);         s[pos] = c;         v[c -  a ].insert(pos);       } else if (t == 2) {         int l, r, cnt = 0;         cin >> l >> r;         l--;         for (int i = 0; i < 26; i++) {           auto itr = v[i].lower_bound(l);           if (itr != v[i].end() && *itr < r) {             cnt++;           }         }         cout << cnt << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       long long n, k;       cin >> n >> k;       long long ans = 0;       while (n != 0) {         if (n % k == 0) {           n /= k;           ans++;         } else {           ans += n % k;           n -= n % k;         }       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1005;   int N, K;   long long L[MAXN], R[MAXN];   double dp[MAXN][MAXN];   double calc(long long l, long long r) {     long long ret = 0, cur = 1;     for (; cur <= r; cur *= 10) {       if (cur == 1e18) {         ret++;         break;       }       if (cur * 10 <= l) continue;       if (cur <= l && cur * 10 > l) {         int a = l / cur;         if (a == 1) {           if (cur * 10 <= r)             ret += cur * 2 - l;           else {             int b = r / cur;             if (b == 1)               ret += r - l + 1;             else               ret += cur * 2 - l;           }         }       } else if (cur * 10 > r) {         int b = r / cur;         if (b == 1)           ret += r - cur + 1;         else           ret += cur;       } else         ret += cur;     }     return double(ret) / (r - l + 1);   }   int main() {     if (fopen( input.txt ,  r )) freopen( input.txt ,  r , stdin);     cin >> N;     for (int i = 0; i < N; i++) cin >> L[i] >> R[i];     cin >> K;     K = ceil(K / 100. * N);     dp[0][0] = 1;     for (int i = 0; i < N; i++) {       double p = calc(L[i], R[i]);       for (int j = 0; j < N; j++) {         dp[i + 1][j + 1] += dp[i][j] * p;         dp[i + 1][j] += dp[i][j] * (1 - p);       }     }     double ans = 0;     for (int k = K; k <= N; k++) ans += dp[N][k];     cout << fixed << setprecision(10) << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int b[100];   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       b[a[i]]++;     }     int f = 0, h = -1;     for (int i = 1; i <= 100; i++) {       if (b[i] == 1) {         f++;       } else if (b[i] > 2) {         h = i;       }     }     if (f % 2 == 0) {       cout <<  YES n ;       int r = f / 2;       for (int i = 0; i < n; i++) {         if (b[a[i]] == 1) {           if (r > 0) {             cout <<  A ;             r--;           } else {             cout <<  B ;           }         } else {           cout <<  A ;         }       }     } else {       if (h == -1) {         return cout <<  NO , 0;       }       int y = 0;       cout <<  YES n ;       int r = f / 2;       for (int i = 0; i < n; i++) {         if ((a[i] != h) || (a[i] == h && y != 0)) {           if (b[a[i]] == 1) {             if (r > 0) {               cout <<  A ;               r--;             } else {               cout <<  B ;             }           } else {             cout <<  B ;           }         } else {           cout <<  A ;           y++;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       if (n % 4 != 0) {         cout <<  NO n ;         continue;       }       cout <<  YES n ;       for (int i = 2; i <= n; i += 2) cout << i <<    ;       for (int i = 1; i < n - 1; i += 2) cout << i <<    ;       cout << (n - 1) + (n / 2) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   vector<T> uniq(vector<T> v) {     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     return v;   }   template <typename T>   struct RMQ {     using F = function<T(const T &, const T &)>;     int n;     vector<T> t;     F f;     RMQ(int n, F f, T df = 0) : n(n), f(f), t(vector<T>(n << 1, df)) {}     void modify(int p, T v) {       for (t[p += n] = v; p > 1; p >>= 1) t[p >> 1] = f(t[p], t[p ^ 1]);     }     T query(int l, int r, T res = 0) {       for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {         if (l & 1) res = f(res, t[l++]);         if (r & 1) res = f(res, t[--r]);       }       return res;     }   };   int32_t main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<int> l(n + 1, 0), r(n + 1, 0);     vector<int> a(n);     for (int &x : a) cin >> x;     vector<int> b;     for (int i = 0; i < n; i++) {       b.push_back(a[i] - i);       b.push_back(a[i] - i + 1);     }     b = uniq(b);     int ptr = 0;     map<int, int> mp;     for (int x : b) mp[x] = ++ptr;     RMQ<int> ST(b.size() + 10, [&](int p, int q) { return max(p, q); });     for (int i = 0; i < n; i++) {       l[i] = 1 + ST.query(1, mp[a[i] - i]);       ST.modify(mp[a[i] - i], l[i]);     }     ST = RMQ<int>(b.size() + 10, [&](int p, int q) { return max(p, q); });     for (int i = n - 1; i >= 0; i--) {       r[i] = 1 + ST.query(mp[a[i] - i], b.size() + 9);       if (i + 1 < n) {         int x = 1 + ST.query(mp[a[i + 1] - (i + 1) + 1], b.size() + 9);         ST.modify(mp[a[i + 1] - (i + 1) + 1], x);       }     }     int ans = 0;     for (int i = 0; i < n; i++) ans = max(ans, l[i] + r[i] - 1);     cout << n - 1 - min(n - 1, ans) << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 1;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;     return x * f;   }   const long long MAXN = 100010;   const long long INF = 1e9;   const long long Mod = 1e9 + 7;   long long N;   set<pair<long long, long long> > S;   long long P;   long long last = 0;   long long h[9][MAXN + 1];   long long inv[MAXN + 1];   long long top;   long long f[MAXN + 1];   long long g[9];   pair<long long, long long> sta[MAXN + 1];   const long long inv2 = (Mod + 1) / 2;   inline void Add(long long x, long long fn) {     if (top && fn == sta[top].second)       S.erase(sta[top]), sta[top].first = x, S.insert(sta[top]);     else       sta[++top] = make_pair(x, fn), S.insert(sta[top]);   }   inline long long Qf(long long x) {     return (*S.upper_bound(make_pair(x - 1, 3))).second;   }   inline long long OPR(long long i) {     long long l = 1, r = i + 1, d = (r - l) / 3;     long long a = Qf(d), b = Qf((r - d) - l), fx = 0;     fx = 0;     while (fx == a || fx == b) ++fx;     return fx;   }   long long ifac[MAXN + 1];   int main() {     N = read();     P = read();     long long now = 2;     f[1] = f[2] = 0;     Add(2, 0);     for (long long i = 3;; i++) {       long long l = 1, r = i + 1, d = (r - l) / 3;       long long a = f[d], b = f[(r - d) - l];       f[i] = 0;       while (f[i] == a || f[i] == b) ++f[i];       if (f[i] != f[i - 1]) {         last = i, Add(i - 1, f[i - 1]);         if (i > 1000) {           now = i - 1;           break;         }       }     }     long long lena = 1, lenb = 1;     for (; now <= P;) {       ++now;       while (sta[lena].first * 3 < now) ++lena;       while ((sta[lenb].first * 3) / 2 < now) ++lenb;       now = max(min((sta[lenb].first * 3 / 2) - 3, sta[lena].first * 3 - 3), now);       for (long long j = 1; j <= 10; j++) Add(now, OPR(now)), ++now;     }     --P;     for (long long i = 1; i <= top; i++) {       long long len = min(sta[i].first, P) - min(sta[i - 1].first, P);       (g[sta[i].second] += len * (P + 1) % Mod -                            (min(sta[i].first, P) + min(sta[i - 1].first, P) + 1) *                                len % Mod * inv2 % Mod +                            Mod) %= Mod;     }     inv[1] = 1;     ifac[0] = 1;     for (long long i = 2; i <= N; i++)       inv[i] = (Mod - (Mod / i) * inv[Mod % i] % Mod) % Mod;     for (long long i = 1; i <= N; i++) ifac[i] = ifac[i - 1] * inv[i] % Mod;     for (long long i = 0; i < 3; i++) {       long long ans = 1;       for (long long j = 0; j <= N; j++) {         if ((!(j & 1)) || (!i))           for (long long k = N - j; k >= 0; k--)             (h[i][k + j] +=              (i ? h[i - 1][k] : (!k ? 1 : 0)) * ans % Mod * ifac[j] % Mod) %= Mod;         ans = ans * g[i] % Mod;       }     }     long long fac = 1, pw = 1, sum = (g[0] + g[1] + g[2]) % Mod;     for (long long i = 1; i <= N; i++) fac = fac * i % Mod, pw = pw * sum % Mod;     printf( %lld n , (pw - h[2][N] * fac % Mod + Mod) % Mod);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m;   long long x, y, z;   vector<pair<long long, long long> > v[200005];   long long dp[300];   bool used[2000005];   long long len[100005];   void dfs(long long x) {     used[x] = true;     for (long long i = 0; i < v[x].size(); ++i) {       long long to = v[x][i].first;       if (used[to]) {         long long l = len[x] ^ len[to] ^ v[x][i].second;         for (long long j = 32; j >= 0; --j)           if ((!dp[j] || dp[j] > l) && l & (1 << j)) {             dp[j] = l;           } else             l = min(l, l ^ dp[j]);       } else {         len[to] = len[x] ^ v[x][i].second;         dfs(to);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (long long i = 1; i <= m; ++i) {       cin >> x >> y >> z;       v[x].push_back(make_pair(y, z));       v[y].push_back(make_pair(x, z));     }     dfs(1);     long long ans = len[n];     for (long long i = 32; i >= 0; --i) ans = min(ans, ans ^ dp[i]);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     char m =  b ;     int ans = 0;     int k = 0, bm = 0, rm = 0;     while (k < n) {       if (s[k] == m) {         if (m ==  b )           m =  r ;         else           m =  b ;         k++;       } else {         if (s[k] ==  b )           bm++;         else           rm++;         if (m ==  b )           m =  r ;         else           m =  b ;         k++;       }     }     int ans1 = min(bm, rm) + abs(rm - bm);     m =  r ;     k = 0, bm = 0, rm = 0;     while (k < n) {       if (s[k] == m) {         if (m ==  b )           m =  r ;         else           m =  b ;         k++;       } else {         if (s[k] ==  b )           bm++;         else           rm++;         if (m ==  b )           m =  r ;         else           m =  b ;         k++;       }     }     int ans2 = min(bm, rm) + abs(rm - bm);     cout << min(ans1, ans2) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> t;   void upd(int v, int x) {     for (int i = v; i < t.size(); i |= (i + 1)) {       t[i] += x;     }   }   int get(int v) {     int sum = 0;     for (int i = v; i >= 0; i = (i & (i + 1)) - 1) {       sum += t[i];     }     return sum;   }   int low_b(int s) {     int k = 0;     for (int i = 20; i > -1; i--) {       if (k + (1 << i) - 1 >= t.size()) {         continue;       }       if (t[k + (1 << i) - 1] < s) {         s -= t[k + (1 << i) - 1];         k += (1 << i);       }     }     return k;   }   const int k = 600;   bool cmp(const vector<int>& a, const vector<int>& b) {     if (a[0] / k == b[0] / k) return a[1] / k > b[1] / k;     return a[0] / k < b[0] / k;   }   int main() {     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     vector<vector<int>> pos(n);     for (int i = 0; i < n; ++i) {       pos[i].push_back(-1);       pos[i].push_back(n);     }     for (int i = 0; i < n; ++i) {       pos[a[i] - 1].push_back(i);     }     for (int i = 0; i < n; ++i) {       sort(pos[i].begin(), pos[i].end());     }     vector<vector<int>> queries;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < pos[i].size() - 1; ++j) {         int l = pos[i][j] + 1;         int r = pos[i][j + 1] - 1;         if (l <= r) queries.push_back({l, r, i + 1});       }     }     queries.push_back({0, n - 1, n + 1});     sort(queries.begin(), queries.end(), cmp);     t.resize(n + 2);     for (int i = 0; i < n + 2; ++i) {       upd(i, 1);     }     vector<int> cnt(n + 1);     int L = 0, R = -1;     for (int i = 0; i < queries.size(); ++i) {       int l = queries[i][0];       int r = queries[i][1];       while (L > l) {         L--;         cnt[a[L] - 1]++;         if (cnt[a[L] - 1] == 1) {           upd(a[L] - 1, -1);         }       }       while (L < l) {         cnt[a[L] - 1]--;         if (cnt[a[L] - 1] == 0) {           upd(a[L] - 1, 1);         }         L++;       }       while (R > r) {         cnt[a[R] - 1]--;         if (cnt[a[R] - 1] == 0) {           upd(a[R] - 1, 1);         }         R--;       }       while (R < r) {         R++;         cnt[a[R] - 1]++;         if (cnt[a[R] - 1] == 1) {           upd(a[R] - 1, -1);         }       }       queries[i].push_back(low_b(1) + 1);     }     vector<bool> ok(n + 2, false);     for (auto elem : queries) {       if (elem[2] == elem[3]) {         ok[elem[2] - 1] = 1;       }     }     int answe = 0;     for (; answe <= n + 1; ++answe) {       if (!ok[answe]) break;     }     cout << answe + 1;   }


#include<bits/stdc++.h>   using namespace std;      int main(){       int t;       cin>>t;              while(t--){           int n;           cin>>n;                      int A[n],B[n];           vector<int>pos,neg;           for(int i=0;i<n;++i){               cin>>A[i];           }                      int z=0;           int m=0;           for(int i=0;i<n;++i){               cin>>B[i];               z+=(B[i]-A[i]);               if(B[i]>A[i]){                   m+=(B[i]-A[i]);                   neg.push_back(i);               }               if(B[i]<A[i]){                   pos.push_back(i);               }           }                      if(z==0){                               cout<<m<<  n ;               int i1=0,i2=0;               for(int i=0;i<m;++i){                   cout<<(pos[i1]+1)<<   <<(neg[i2]+1)<<  n ;                   A[pos[i1]]--;                   A[neg[i2]]++;                                      if(A[pos[i1]]==B[pos[i1]])i1++;                   if(A[neg[i2]]==B[neg[i2]])i2++;               }           }           else{               cout<< -1 n ;           }                  }   }


#include <bits/stdc++.h>   using namespace std;   int arr[3];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     for (int i = 0; i < t; ++i) {       int x;       cin >> x;       arr[i % 3] += x;     }     int count = 0;     for (int i = 0; i < 3; i++) {       count = max(count, arr[i]);     }     if (arr[0] == count) {       cout <<  chest             <<   n ;     }     if (arr[1] == count) {       cout <<  biceps             <<   n ;     }     if (arr[2] == count) {       cout <<  back             <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int arr[n + 1];     for (int i = 1; i <= n; i++) cin >> arr[i];     int x;     cin >> x;     int w, l;     while (x--) {       cin >> w >> l;       if (w == 1) {         arr[2] += arr[1] - l;         arr[1] = 0;         continue;       }       if (w == n) {         arr[n - 1] += l - 1;         arr[n] = 0;         continue;       }       arr[w - 1] += l - 1;       arr[w + 1] += arr[w] - l;       arr[w] = 0;     }     for (int i = 1; i <= n; i++) {       cout << arr[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     register int X = 0;     register char ch = 0;     while (ch < 48 || ch > 57) ch = getchar();     while (ch >= 48 && ch <= 57) X = X * 10 + (ch ^ 48), ch = getchar();     return X;   }   long long powM(long long a, int t = 998244353 - 2) {     long long ans = 1;     while (t) {       if (t & 1) ans = ans * a % 998244353;       a = a * a % 998244353;       t >>= 1;     }     return ans;   }   int r[135000 << 2];   long long invG = powM(3), fac[135000], inv[135000];   void NTT(long long *f, bool op, int n) {     for (int i = 0; i < n; i++)       if (r[i] < i) swap(f[r[i]], f[i]);     for (int len = 1; len < n; len <<= 1) {       int w = powM(op == 1 ? 3 : invG, (998244353 - 1) / len / 2);       for (int p = 0; p < n; p += len + len) {         long long buf = 1;         for (int i = p; i < p + len; i++) {           int sav = f[i + len] * buf % 998244353;           f[i + len] = f[i] - sav;           if (f[i + len] < 0) f[i + len] += 998244353;           f[i] = f[i] + sav;           if (f[i] >= 998244353) f[i] -= 998244353;           buf = buf * w % 998244353;         }       }     }   }   long long g[135000 << 1];   void times(long long *f, long long *gg, int len, int lim) {     int m = len + len, n;     for (int i = 0; i < len; i++) g[i] = gg[i];     for (n = 1; n < m; n <<= 1)       ;     for (int i = len; i < n; i++) g[i] = 0;     for (int i = 0; i < n; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0);     NTT(f, 1, n);     NTT(g, 1, n);     for (int i = 0; i < n; ++i) f[i] = f[i] * g[i] % 998244353;     NTT(f, 0, n);     long long invn = powM(n);     for (int i = 0; i < lim; ++i) f[i] = f[i] * invn % 998244353;     for (int i = lim; i < n; ++i) f[i] = 0;   }   long long f[135000 << 1], p[135000 << 1], m;   int n;   int main() {     n = read() + 1;     scanf( %I64d , &m);     m %= (998244353 - 1);     fac[0] = 1;     for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % 998244353;     inv[n] = powM(fac[n]);     for (int i = n; i; i--) inv[i - 1] = inv[i] * i % 998244353;     for (int i = 0; i < n; i++) f[i] = read();     for (int i = 0; i < n; i++) p[n - i - 1] = f[i] * fac[i] % 998244353;     for (int i = 0; i < n; i++) f[i] = inv[i] % 998244353;     times(p, f, n, n);     for (int i = 0; i < n; i++) f[n - i - 1] = p[i] * inv[n - i - 1] % 998244353;     for (int i = 0; i < n; i++)       f[i] = f[i] * powM(i + 1, 998244353 - 1 - m) % 998244353;     for (int i = 0; i < n; i++)       if (i & 1)         p[n - i - 1] = 998244353 - (f[i] * fac[i] % 998244353);       else         p[n - i - 1] = f[i] * fac[i] % 998244353;     for (int i = 0; i < n; i++) f[i] = inv[i] % 998244353;     times(p, f, n, n);     for (int i = 0; i < n; i++) f[n - i - 1] = p[i] * inv[n - i - 1] % 998244353;     for (int i = 0; i < n; i++)       printf( %I64d  , (i & 1) ? (998244353 - f[i]) % 998244353 : f[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 20071027;   const double pi = acos(1.0 * (-1));   int p[1001111];   long long fun(long long x) {     long long dt = 1 + 8 * x;     long long t = sqrt(dt * 1.0) + 0.0000001;     if (t * t != dt) return -1;     return (1 + t) / 2;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n00, n01, n10, n11;     cin >> n00 >> n01 >> n10 >> n11;     long long n1, n0;     n1 = fun(n11);     n0 = fun(n00);     if (n00 + n01 + n10 + n11 == 0) {       cout <<  1 ;       return 0;     }     if (n1 == -1 || n0 == -1) {       cout <<  Impossible ;       return 0;     }     if (n01 + n10 == 0) {       if (n0 == 1)         for (int i = 0; i < n1; i++) cout <<  1 ;       else if (n1 == 1)         for (int i = 0; i < n0; i++) cout <<  0 ;       else         cout <<  Impossible ;       return 0;     }     if (n01 + n10 != n0 * n1) {       cout <<  Impossible ;       return 0;     }     if (n01 > n0) {       p[n0] = n01 / n0;       n01 -= n0 * p[n0];       n1 -= p[n0];     }     if (n1) {       p[n01]++;       n1--;     }     p[0] += n1;     for (int i = 0; i < n0; i++) {       for (int j = 0; j < p[i]; j++) cout <<  1 ;       cout <<  0 ;     }     for (int i = 0; i < p[n0]; i++) cout <<  1 ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( unroll-loops )   using namespace std;   int main() {     int q;     cin >> q;     while (q--) {       int n;       cin >> n;       long long a[n + 5], m = 0;       vector<int> idx;       for (int i = 0; i < n; i++) {         cin >> a[i];       }       long long ans = 0;       for (int i = 0; i < n - 1; i++) {         ans = max(ans, a[i] * a[i + 1]);       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int b, k, a[100005], sum, tmp;   int main() {     cin >> b >> k;     for (int i = 1; i <= k; ++i) {       scanf( %d , &a[i]);       a[i] %= 2;     }     tmp = 1;     for (int i = k; i >= 1; --i) {       sum = (sum + tmp * a[i]) % 2;       tmp = tmp * b % 2;     }     if (sum & 1) {       cout <<  odd  << endl;     } else {       cout <<  even  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     scanf( %d %d , &n, &m), ++n;     long long ans = 0;     for (int a = m + 1; a <= n / 2; ++a) {       int tmp = 0;       for (int b = m + 1, c = n - m - 1; b < n - m && c > m; ++b) {         while (c > m && a * ((a << 1) + b - (n << 1) - c) >= -c * (n + b)) --c;         tmp += c - m;       }       if (a != n - a) tmp *= 2;       ans += tmp;     }     cout << ans * 3 << endl;   }


#include <iostream>   #include <iosfwd>   #include <iomanip>   #include <cstdio>   #include <cstring>   #include <cstdlib>   #include <ctime>   #include <cmath>   #include <cassert>   #include <cctype>   #include <climits>   #include <vector>   #include <bitset>   #include <set>   #include <queue>   #include <math.h>   #include <stack>   #include <map>   #include <deque>   #include <string>   #include <list>   #include <iterator>   #include <sstream>   #include <complex>   #include <fstream>   #include <functional>   #include <numeric>   #include <utility>   #include <algorithm>   #include <assert.h>   #include <unordered_map>   #include<ctime>   #include <chrono>   #pragma GCC optimize ( -O3 )   using namespace std;      #define tolower(s) transform(s.begin(),s.end(),s.begin(),::tolower);   #define toupper(s) transform(s.begin(),s.end(),s.begin(),::toupper);   #define ll long long int   #define pii pair <ll , ll >   #define pb push_back   #define all(s) s.begin(),s.end()   ll gcd(ll a, ll b) { if (a == 0) return b; return b == 0 ? a : gcd(b, a % b);}   #define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(),  , ,    ); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr <<   n ; }   void err(istream_iterator<string> it) {}   template<typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) { cerr <<  [   << *it <<   =   << a <<   ] ; err(++it, args...);}      /**************************************************************************/   const int x4[] = {1, -1, 0, 0};   const int y4[] = {0, 0, 1, -1};   const int x8[] = {1, 1, 1, -1, -1, -1, 0, 0};   const int y8[] = {1, 0, -1, 1, 0, -1, 1, -1};   /***************************************************************************/   const ll MOD = 1e9 + 7;   const ll MOD2 = 998244353;   const ll INF = 9223372036854775800;   const long double PI = 3.141592653589793;   const int PRECISION = 10;   /***************************************************************************/   const int DIGITSIZE = 18;   const int MAXN = 100001;   const int LOGN = 22;   const int MAXBIT = 62;   /***************************************************************************/   const bool take_test_case = 1;   const bool print_case_no = 0;   /****************************************************************************/      void getac(int T) {    int n;    cin >> n;    if (n == 2) {     cout << -1 <<   n ;     return;    }    int curr = 1;    int sq = n * n;    for (int i = 0; i < n; i++) {     for (int i = 0; i < n; i++) {      cout << curr <<    ;      curr += 2;      if (curr > sq) {       curr -= sq - (n % 2 == 0);      }     }     cout <<   n ;    }      }   int  main()   {   #ifndef ONLINE_JUDGE    freopen( input.txt ,  r , stdin);    freopen( output.txt ,  w , stdout);    freopen( error.txt ,  w , stderr);   #endif    ios_base::sync_with_stdio(false);    cin.tie(NULL);    cout.tie(NULL);    cerr.tie(NULL);    auto start_time = std::chrono::high_resolution_clock::now();       int test_cases = 1;    if (take_test_case) cin >> test_cases;    for (int test_case_no = 1; test_case_no <= test_cases; test_case_no++) {     if (print_case_no)      cout <<  Case #  << test_case_no <<  :  ;     getac(test_case_no);    }    cerr <<  Time elapsed   << chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start_time).count();    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int c[1200000], save[5500];   bool flag[1200000];   int main() {     int n, k;     while (scanf( %d%d , &n, &k) != EOF) {       for (int i = 1; i <= n; i++) scanf( %d , &save[i]);       sort(save + 1, save + n + 1);       memset(c, 0, sizeof(c));       for (int i = 1; i <= n; i++)         for (int j = i + 1; j <= n; j++) {           save[j] > save[i] ? c[save[j] - save[i]]++ : c[save[i] - save[j]]++;         }       int K = k * (k + 1) / 2;       for (int i = 1; i <= save[n] + 1; i++) {         int temp = 0;         for (int j = i; j <= save[n] + 1; j += i) {           temp += c[j];           if (temp > K) break;         }         if (temp <= K) {           int cnt = 0;           memset(flag, 0, sizeof(flag));           for (int j = 1; j <= n; j++) {             int tt = save[j] % i;             if (flag[tt]) {               cnt++;               if (cnt > k) break;             }             flag[tt] = 1;           }           if (cnt <= k) {             printf( %d n , i);             break;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using VI = vector<int>;   using VI2 = vector<VI>;   using UM = unordered_map<int, int>;   using UM2 = unordered_map<int, UM>;   using M = map<int, int>;   using M2 = map<int, M>;   using u64 = unsigned long long;   using i64 = long long;   using u32 = unsigned int;   using USI = unordered_set<int>;   using USS = unordered_set<string>;   using VUS = vector<USI>;   int main() {     int n, m;     cin >> n >> m;     VI a(n, 1);     for (int j = 0; j < m; j++) {       int ind = -1;       int ind2 = -1;       bool f = false;       for (int i = 0; i < n; i++) {         if (!a[i]) continue;         ind2 = i;         int ip1 = (i + 1) % n;         int im1 = !i ? n - 1 : i - 1;         int sc = 0;         if (a[ip1]) sc++;         if (a[im1]) sc++;         if (sc == 2) {           a[i] = 0;           f = true;           break;         }         if (ind >= 0 && sc == 1) {           ind = i;           break;         }       }       if (ind >= 0) {         a[ind] = 0;         continue;       }       if (!f && ind2 < 0) {         cout <<  0 n ;         return 0;       }       if (!f) a[ind2] = 0;     }     int mgn = 1;     if (a[0]) a[0] = mgn++;     for (int i = 1; i < n; i++) {       if (!a[i]) continue;       if (a[i - 1])         a[i] = a[i - 1];       else         a[i] = mgn++;     }     mgn--;     if (a[0] && a[n - 1] && a[0] != a[n - 1]) {       mgn--;     }     cout << mgn <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, tmp = 1;   int sum(int a) {     int res = 0;     while (a) {       res += a % 10;       a /= 10;     }     return res;   }   int main() {     cin >> n >> m;     if (2 * n > 2220) {       int nn = n / 2 + n % 2;       for (int i = 1; i <= nn; ++i) {         cout << 2;       }       cout << endl;       for (int i = 1; i <= nn - 1; ++i) {         cout << 7;       }       cout << 8 << endl;       return 0;     }     for (int i = 1; i <= n; ++i) {       cout << 1;     }     cout << endl;     for (int i = 1; i <= n - 1; ++i) {       cout << 8;     }     cout << 9 << endl;   }


#include <bits/stdc++.h>   using lli = long long int;   using li = long int;   using namespace std;   const lli MOD{1000000007};   const lli INF{(false ? (INT_MAX - 1) / 2 : (lli)1e18 + 5)};   const lli N{200005};   int main() {     cout << setprecision(15);     cin.tie(nullptr)->sync_with_stdio(false);     li T{1};     cin >> T;     while (T--) {       lli n{}, x{}, y{};       cin >> n >> x >> y;       lli z{x + y - 1};       lli done{n - z};       if (done > 0)         cout << 1 <<    ;       else         cout << min(2 - done, n) <<    ;       done = max((lli)0, done);       cout << n - done <<   n ;       if (z <= n) done -= n - z + 1;     }     return 0;   }


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


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   template <class T>   inline void chkmin(T &a, T b) {     if (a > b) a = b;   }   template <class T>   inline void chkmax(T &a, T b) {     if (a < b) a = b;   }   int main() {     int n, m;     cin >> n >> m;     --n;     int l = 1, r = 1000;     for (int i = 0; i < m; ++i) {       int k, first;       cin >> k >> first;       chkmax(l, (k + first - 1) / first);       if (first != 1) chkmin(r, (k - 1) / (first - 1));     }     int f1 = n / l + 1;     int f2 = n / r + 1;     if (f1 != f2)       cout <<  -1 n ;     else       cout << f1 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   struct Fenwick {     int n;     long long tree[200002];     void init(int _n) {       n = _n;       fill(tree + 1, tree + n + 1, 0LL);     }     void upd(int x, long long val) {       while (x <= n) {         tree[x] += val;         x += x & -x;       }     }     long long sum(int x) {       long long ret = 0;       while (x) {         ret += tree[x];         x -= x & -x;       }       return ret;     }   } tree;   struct Query {     int u, v;     long long cost;     Query() {}     Query(int u, int v, long long cost) : u(u), v(v), cost(cost) {}   };   int n, m;   int par[200002];   vector<int> child[200002];   int depth[200002], LCADB[200002][20];   long long DP[200002];   int sz[200002];   vector<Query> queryVec[200002];   int in[200002], top[200002], idx[200002];   long long DPSum[200002];   void makeTree(int x) {     sz[x] = 1;     for (auto &y : child[x]) {       depth[y] = depth[x] + 1;       makeTree(y);       sz[x] += sz[y];       if (sz[y] > sz[child[x][0]]) swap(child[x][0], y);     }   }   int in_cnt;   void dfs_hld(int x) {     in[x] = ++in_cnt;     idx[in[x]] = x;     for (auto y : child[x]) {       top[y] = (child[x][0] == y) ? top[x] : y;       dfs_hld(y);     }   }   int getLCA(int x, int y) {     if (depth[x] < depth[y]) swap(x, y);     for (int d = 0; d < 20; d++)       if ((depth[x] - depth[y]) & (1 << d)) x = LCADB[x][d];     if (x == y) return x;     for (int d = 19; d >= 0; d--)       if (LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];     return par[x];   }   int kth_parent(int x, int k) {     for (int d = 0; d < 20; d++) {       if ((k >> d) & 1) x = LCADB[x][d];     }     return x;   }   long long pathQuery(int x, int topD) {     long long ret = 0;     while (depth[x] >= topD) {       int tp = in[top[x]];       int tStart = max(topD, depth[top[x]]) - depth[top[x]] + tp;       int tEnd = depth[x] - depth[top[x]] + tp;       ret += tree.sum(tEnd) - tree.sum(tStart - 1);       x = par[top[x]];     }     return ret;   }   void dfs(int x) {     long long dpSum = 0;     for (auto y : child[x]) {       dfs(y);       dpSum += DP[y];     }     DPSum[x] = DP[x] = dpSum;     for (auto y : child[x]) {       tree.upd(in[y], dpSum - DP[y]);     }     for (auto query : queryVec[x]) {       int tx = query.u, ty = query.v;       long long tv = query.cost;       assert(depth[tx] <= depth[ty]);       long long val;       if (tx == x) {         val = dpSum + tv;         val -= DP[kth_parent(ty, depth[ty] - depth[tx] - 1)];         val += pathQuery(ty, depth[x] + 2);         val += DPSum[ty];       } else {         val = dpSum + tv;         val -= DP[kth_parent(tx, depth[tx] - depth[x] - 1)];         val -= DP[kth_parent(ty, depth[ty] - depth[x] - 1)];         val += pathQuery(tx, depth[x] + 2);         val += pathQuery(ty, depth[x] + 2);         val += DPSum[tx];         val += DPSum[ty];       }       DP[x] = max(DP[x], val);     }   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 2; i <= n; i++) {       scanf( %d , &par[i]);       LCADB[i][0] = par[i];       child[par[i]].push_back(i);     }     makeTree(1);     top[1] = 1;     dfs_hld(1);     for (int d = 1; d < 20; d++)       for (int i = 1; i <= n; i++) LCADB[i][d] = LCADB[LCADB[i][d - 1]][d - 1];     for (int i = 1; i <= m; i++) {       Query tmp;       scanf( %d %d %lld , &tmp.u, &tmp.v, &tmp.cost);       if (depth[tmp.u] > depth[tmp.v]) swap(tmp.u, tmp.v);       int z = getLCA(tmp.u, tmp.v);       queryVec[z].push_back(tmp);     }     tree.init(n);     dfs(1);     printf( %lld , DP[1]);   }


#include <bits/stdc++.h>   using namespace std;   long long t[500005], mid;   long long l[500005], r[500005], h[500005];   struct st {     long long h, cnt;   };   stack<st> q;   int main() {     cin.sync_with_stdio(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> t[i];     q.push({-1, 1});     for (int i = 1; i <= n; i++) {       long long c = 0, h = 0;       while (q.top().h >= t[i]) {         c += q.top().cnt;         h += q.top().cnt * q.top().h;         q.pop();       }       l[i] = l[i - 1] - h + (t[i] * (c + 1));       q.push({t[i], c + 1});     }     while (q.size()) q.pop();     q.push({0, 1});     for (int i = n; i > 0; i--) {       long long c = 0, h = 0;       while (q.top().h >= t[i]) {         c += q.top().cnt;         h += q.top().cnt * q.top().h;         q.pop();       }       r[i] = r[i + 1] - h + (t[i] * (c + 1));       q.push({t[i], c + 1});     }     while (q.size()) q.pop();     long long ms = 0;     for (int i = 1; i <= n; i++)       if (l[i] + r[i] - t[i] > ms) ms = l[i] + r[i] - t[i], mid = i;     h[mid] = t[mid];     for (int i = mid + 1; i <= n; i++) {       h[i] = min(h[i - 1], t[i]);     }     for (int i = mid - 1; i > 0; i--) {       h[i] = min(h[i + 1], t[i]);     }     for (int i = 1; i <= n; i++) cout << h[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int a, b, t;   void read_input() { cin >> a >> b; }   void solve() {     int res = abs(a - b);     int ans = res / 5;     res = res % 5;     ans += res / 2;     res %= 2;     ans += res;     cout << ans << endl;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> t;     while (t--) read_input(), solve();     return 0;   }


#include<bits/stdc++.h>   using namespace std;   long long a[1000010];   bool cmp(long long a,long long b)   {    return a>b;   }   int main() {       long long m,n,i,j,x,y,suma=0,sumb=0;    cin>>n;    while(n--)    {     cin>>m;     for(i=0;i<m;i++)     cin>>a[i];     sort(a,a+m,cmp);     for(i=0;i<m;)     {     if(a[i]%2==0)     suma+=a[i];     i++;     if(a[i]%2!=0)     sumb+=a[i];     i++;         }     if(sumb==suma)     cout<< Tie <<endl;     if(suma>sumb)     cout<< Alice <<endl;     if(suma<sumb)     cout<< Bob <<endl;     suma=0;     sumb=0;    }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int ar[n + 2], tr1[n + 2], tr2[n + 2], kl[k + 2][102];     memset(kl, 0, sizeof(kl));     vector<pair<int, int>> vec;     for (int i = 0; i < n; i++) {       cin >> ar[i];       tr1[i] = ar[i];       tr2[i] = ar[i];     }     sort(ar, ar + n);     sort(tr1, tr1 + n);     if (ar[n - 1] - ar[0] > k) {       cout <<  NO n ;       return 0;     }     cout <<  YES n ;     int mn = 101;     for (int i = 0; i < k; i++) {       int j = 0;       for (; j < n; j++) {         if (ar[j] > 0) {           mn = ar[j];           break;         }       }       for (; j < n; j++) {         if (i == 0) {           kl[i][j] = min(mn + 1, ar[j]);           ar[j] -= min(mn + 1, ar[j]);         } else {           kl[i][j] = 1;           ar[j] -= 1;         }       }     }     for (int i = 0; i < n; i++) {       int f = tr2[i];       int zz;       for (int z = 0; z < n; z++) {         if (tr1[z] == f) {           zz = z;           break;         }       }       for (int j = 0; j < k; j++) {         if (kl[j][zz] == 0) {           continue;         }         for (int l = 0; l < kl[j][zz]; l++) {           cout << j + 1 <<    ;         }       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void sol();   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     long long ml = 1, ans = 0;     vector<int> ls(n);     map<long long, int> pre, mp;     mp[0] = 1;     vector<long long> an;     while (ml < 1e15) {       if (!pre[ml])         an.push_back(ml), pre[ml] = 1;       else         break;       ml *= k;       if ((ml > 1e15 || -ml > 1e15) || (ml % k != 0)) break;     }     for (int &x : ls) cin >> x;     long long sm = 0ll;     for (int x : ls) {       sm += x;       for (long long y : an)         if (mp[sm - y]) ans += mp[sm - y];       mp[sm]++;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   void redondear(string &s, int pto, int idx) {     if (idx == (int)(s.size())) {       cout << s << endl;       return;     }     s.resize(idx);     bool unoAdelante = false;     for (int i = (idx)-1; i >= (int)(0); i--) {       if (i == pto) {         continue;       }       if (i == 0 && s[i] ==  9 ) {         s[i] =  0 ;         unoAdelante = true;         continue;       }       if (s[i] ==  9 ) {         s[i] =  0 ;       } else {         s[i]++;         break;       }     }     if (unoAdelante) cout <<  1 ;     if (s[s.size() - 1] ==  . ) {       s.resize(s.size() - 1);     }     cout << s << endl;   }   int main() {     int largo, segundos;     cin >> largo >> segundos;     string nota;     cin >> nota;     int punto = 0;     for (int i = (0); i < (int)(nota.size()); i++) {       if (nota[i] ==  . ) {         punto = i;         break;       }     }     int minimoIndice = largo;     int turnosUsados = 0;     for (int i = (largo)-1; i >= (int)(punto + 1); i--) {       if (nota[i] >=  5 ) {         minimoIndice = i;         turnosUsados = 1;       } else if (nota[i] ==  4  && turnosUsados < segundos && turnosUsados > 0) {         minimoIndice = i;         turnosUsados++;       } else {         turnosUsados = 0;       }     }     redondear(nota, punto, minimoIndice);   }


#include <bits/stdc++.h>   using namespace std;   int n;   int l, r;   void did(int &x, int y) {     printf( ? %d n , y);     fflush(stdout);     scanf( %d , &x);   }   int main() {     scanf( %d , &n);     if ((n / 2) & 1) {       printf( ! -1 );       fflush(stdout);       return 0;     }     while (1) {       printf( ? 1 n );       fflush(stdout);       int x, y;       int l = 1, r = n / 2;       scanf( %d , &x);       printf( ? %d n , n / 2 + 1);       fflush(stdout);       scanf( %d , &y);       if (x == y) {         printf( ! %d n , 1);         fflush(stdout);         return 0;       }       int xx = x, yy = y;       while (l <= r) {         int mid = (l + r) / 2;         did(xx, mid);         did(yy, mid + n / 2);         if (xx == yy) {           printf( ! %d n , mid);           fflush(stdout);           return 0;         }         if ((xx < yy) == (x < y))           l = mid + 1;         else           r = mid - 1;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 3010;   int n, x[M];   char d[M][6];   int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};   vector<long long> X, Y;   int g[M][M], fw1, fw2;   queue<pair<int, int> > Q;   const long long INF = 20000000005LL, MINF = -INF;   struct point {     long long lasx, lasy, nowx, nowy;     point() {}     point(int _lasx, int _lasy, int _nowx, int _nowy)         : lasx(_lasx), lasy(_lasy), nowx(_nowx), nowy(_nowy) {}   } ways[M];   inline int renumber(int x, vector<long long> _find) {     return find(_find.begin(), _find.end(), x) - _find.begin();   }   inline void BFS() {     while (!Q.empty()) Q.pop();     Q.push(make_pair(0, 0));     g[0][0] = 2;     fw1 = X.size(), fw2 = Y.size();     while (!Q.empty()) {       pair<int, int> top = Q.front();       Q.pop();       for (int i = 0; i < 4; ++i) {         pair<int, int> x;         x.first = top.first + dx[i], x.second = top.second + dy[i];         if (x.first >= 0 && x.first < fw1 && x.second >= 0 && x.second < fw2 &&             g[x.first][x.second] == 0) {           g[x.first][x.second] = 2;           Q.push(x);         }       }     }   }   int main() {     scanf( %d , &n);     long long nowx, nowy;     nowx = nowy = 0;     X.push_back(MINF);     X.push_back(INF);     Y.push_back(MINF);     Y.push_back(INF);     for (int i = 1; i <= n; ++i) {       int face;       long long tx, ty;       scanf( %s%d , d[i], &x[i]);       if (d[i][0] ==  R ) face = 0;       if (d[i][0] ==  L ) face = 1;       if (d[i][0] ==  D ) face = 2;       if (d[i][0] ==  U ) face = 3;       tx = nowx + dx[face] * x[i];       ty = nowy + dy[face] * x[i];       int Xmin = min(nowx, tx), Xmax = max(nowx, tx) + 1;       int Ymin = min(nowy, ty), Ymax = max(nowy, ty) + 1;       X.push_back(Xmin);       X.push_back(Xmax);       Y.push_back(Ymin);       Y.push_back(Ymax);       ways[i] = point(Xmin, Ymin, Xmax, Ymax);       nowx = tx, nowy = ty;     }     sort(X.begin(), X.end());     sort(Y.begin(), Y.end());     X.resize(unique(X.begin(), X.end()) - X.begin());     Y.resize(unique(Y.begin(), Y.end()) - Y.begin());     for (int i = 1; i <= n; ++i) {       int tx1 = renumber(ways[i].lasx, X), tx2 = renumber(ways[i].nowx, X),           ty1 = renumber(ways[i].lasy, Y), ty2 = renumber(ways[i].nowy, Y);       for (int j = tx1; j < tx2; ++j)         for (int k = ty1; k < ty2; ++k) g[j][k] = 1;     }     BFS();     long long ans = 0;     for (int i = 0; i <= fw1 - 2; ++i)       for (int j = 0; j <= fw2 - 2; ++j)         if (g[i][j] != 2)           ans = ans + (long long)(X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 60;   int n, l;   char s[maxn][maxn], t[maxn];   bool eq(int x, int i, int j) {     for (int k = 0; k < l; k++) {       if (s[x][i] != t[j]) return false;       i = (i + 1) % l;       j = (j + 1) % l;     }     return true;   }   int dist(int x, int j) {     for (int i = 0; i < l; i++)       if (eq(x, i, j)) return i;     return -1;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %s , s[i]);     l = strlen(s[0]);     strcpy(t, s[0]);     int ans = 0x3f3f3f3f;     for (int j = 0; j < l; j++) {       int t = 0;       for (int x = 0; x < n; x++) {         int d = dist(x, j);         if (d == -1) {           printf( -1 n );           return 0;         }         t += d;       }       if (t < ans) ans = t;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, p[1000001];   long long ans;   int main() {     scanf( %d%d , &n, &k);     if (k == 1) {       printf( 3 );       return 0;     }     for (int i = 1; i <= n; i++) p[i] = i;     for (int i = 2; i <= n; i++)       if (p[i] == i)         for (int j = i; j <= n; j += i) p[j] -= p[j] / i;     sort(p + 1, p + 1 + n);     for (int i = 1; i <= k + 2; i++) ans += p[i];     printf( %I64d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int num[10010];   int dp[1 << 18];   int bad[1 << 18];   int g[100][100];   int tmp[1 << 18];   string s;   int get(int l, int r) {     int t = 0;     for (int i = l; i <= r; ++i) {       t |= 1 << (s[i] -  a );     }     return t;   }   int main() {     int n, p;     scanf( %d%d , &n, &p);     cin >> s;     memset(num, 0, sizeof(num));     memset(g, 0, sizeof(g));     memset(bad, 0, sizeof(bad));     for (int i = 0; i < s.size(); ++i) {       num[s[i] -  a ]++;     }     for (int i = 0; i < p; ++i) {       for (int j = 0; j < p; ++j) {         scanf( %d , &g[i][j]);       }     }     for (int i = 0; i < p; ++i) {       for (int j = 0; j < p; ++j) {         if (g[i][j]) continue;         memset(tmp, 0, sizeof(tmp));         int l = -1;         for (int k = 0; k < s.size(); ++k) {           if (s[k] -  a  == i || s[k] -  a  == j) {             if (l == -1) {               l = k;             } else {               int a = s[l] -  a , b = s[k] -  a ;               if ((a == i || a == j) && (b == i || b == j)) {                 if ((a == i && b == j) || (b == j && a == i)) {                   int m = get(l + 1, k - 1);                   tmp[m] = 1;                 }                 l = k;               }             }           }         }         for (int ii = 0; ii < (1 << p); ++ii) {           if ((ii & (1 << i)) || (ii & (1 << j))) continue;           for (int k = 0; k < p; ++k) {             if (k == i || k == j) continue;             tmp[ii | (1 << k)] |= tmp[ii];           }           if (tmp[ii]) bad[ii] = tmp[ii];         }       }     }     memset(dp, 0, sizeof(dp));     dp[0] = 1;     int ans = 0;     for (int i = 0; i < (1 << p); ++i) {       if (bad[i]) continue;       for (int j = 0; j < p; ++j) {         if ((1 << j) & i) {           if (dp[i ^ (1 << j)]) dp[i] = 1;         }       }       if (dp[i]) {         int s = 0;         for (int j = 0; j < p; ++j) {           if ((1 << j) & i) s += num[j];         }         ans = max(ans, s);       }     }     printf( %d n , n - ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     scanf( %lld , &n);     if (n == 1 || n == 2) {       puts( -1 );       return 0;     }     if (n & 1) {       n -= 1;       n /= 2;       printf( %lld %lld n , 2 * n * n + 2 * n, 2 * n * n + 2 * n + 1);     } else {       n /= 2;       printf( %lld %lld n , n * n - 1, n * n + 1);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int s, a, b, c;     cin >> s >> a >> b >> c;     double x = 0.0, y = 0.0, z = 0.0;     if (a || b || c) {       x = s * a / (double)(a + b + c);       y = s * b / (double)(a + b + c);       z = s * c / (double)(a + b + c);     }     printf( %.12f %.12f %.12f , x, y, z);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int x = 0;     int f = 1, ch = getchar();     for (; !isdigit(ch) && ch != EOF; ch = getchar())       if (ch ==  - ) f = -1;     for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^  0 );     return x * f;   }   const int N = 500005;   int n, La, Lb, Lc, smx[N];   struct card {     int b, c;   };   vector<card> d[N];   long long ans, sum[N];   int main() {     n = read(), La = read(), Lb = read(), Lc = read();     for (register int i = 1; i <= n; ++i) {       int a = min(La, read()), b = min(Lb, read()), c = min(Lc, read());       d[a].push_back((card){b, c});       smx[b] = max(smx[b], c);     }     for (register int i = Lb - 1; i; --i) smx[i] = max(smx[i], smx[i + 1]);     for (register int i = 1; i <= Lb; ++i) sum[i] = sum[i - 1] + smx[i];     int mxb = 0, mxc = 0, k = Lb + 1;     for (register int x = La; x; --x) {       for (register int i = 0; i < d[x].size(); ++i) {         int y = d[x][i].b, z = d[x][i].c;         mxb = max(mxb, y), mxc = max(mxc, z);         while (k > mxb + 1 && smx[k - 1] < mxc) --k;       }       k = max(k, mxb + 1);       ans += 1ll * (Lc - mxc) * (Lb - k + 1) + 1ll * Lc * (k - mxb - 1) -              sum[k - 1] + sum[mxb];     }     printf( %lld , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a;     cin >> b;     int lena = a.length(), lenb = b.length();     if (lena != lenb)       cout <<  NO ;     else {       int count = 0;       char old1, old2;       for (int i = 0; i < lena && count < 3; i++) {         if (a[i] != b[i]) {           count++;           if (count == 1) {             old1 = b[i];             old2 = a[i];           }           if (count == 2 && (a[i] != old1 || b[i] != old2)) count = 3;         }       }       if (count == 2)         cout <<  YES ;       else         cout <<  NO ;     }     return 0;   }


#include<bits/stdc++.h>   namespace my_std{    using namespace std;    #define pii pair<int,int>    #define fir first    #define sec second    #define MP make_pair    #define rep(i,x,y) for (int i=(x);i<=(y);i++)    #define drep(i,x,y) for (int i=(x);i>=(y);i--)    #define go(x) for (int i=head[x];i;i=edge[i].nxt)    #define templ template<typename T>    #define sz 111    #define mod 1000000007ll    typedef long long ll;    typedef double db;    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}    templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}    templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}    templ inline void read(T& t)    {     t=0;char f=0,ch=getchar();double d=0.1;     while(ch> 9 ||ch< 0 ) f|=(ch== - ),ch=getchar();     while(ch<= 9 &&ch>= 0 ) t=t*10+ch-48,ch=getchar();     if(ch== . ){ch=getchar();while(ch<= 9 &&ch>= 0 ) t+=d*(ch^48),d*=0.1,ch=getchar();}     t=(f?-t:t);    }    template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}    char __sr[1<<21],__z[20];int __C=-1,__zz=0;    inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}    inline void print(int x)    {     if(__C>1<<20)Ot();if(x<0)__sr[++__C]= - ,x=-x;     while(__z[++__zz]=x%10+48,x/=10);     while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]=  n ;    }    void file()    {     #ifdef NTFOrz     freopen( a.in , r ,stdin);     #endif    }    inline void chktime()    {     #ifdef NTFOrz     cerr<<clock()/1000.0<<  n ;     #endif    }    #ifdef mod    ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}    ll inv(ll x){return ksm(x,mod-2);}    #else    ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}    #endif   // inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}   }   using namespace my_std;      int n;   int c[sz],b[sz],B[sz];      ll pre[sz][sz*sz],pr;   void init()   {    pre[0][0]=1;    rep(i,0,n-1) rep(k,0,100*i) rep(t,0,c[i+1]) (pre[i+1][k+t]+=pre[i][k])%=mod;    pr=1; rep(i,1,n) pr=pr*(c[i]+1)%mod;   }      ll dp[sz][sz*sz];   ll work(int x)   {    static int lim[sz];    rep(i,1,n) lim[i]=i*x+B[i];    int p=1; while (p<=n&&lim[p]<=0) ++p; if (p>n) return pr;    rep(i,p,n) rep(j,0,i*100) dp[i][j]=0; rep(i,lim[p],p*100) dp[p][i]=pre[p][i];    rep(i,p,n-1)    {     rep(k,lim[i],i*100) { int l=k,r=k+c[i+1]; if (r<lim[i+1]) continue; chkmax(l,lim[i+1]); dp[i+1][l]+=dp[i][k],dp[i+1][r+1]+=mod-dp[i][k]; }     dp[i+1][lim[i+1]]%=mod;     rep(k,lim[i+1]+1,(i+1)*100) (dp[i+1][k]+=dp[i+1][k-1])%=mod;    }    ll res=0; rep(i,lim[n],n*100) res+=dp[n][i]; return res%mod;   }      int main()   {    file();    read(n);    rep(i,1,n) read(c[i]);    rep(i,2,n) read(b[i]),B[i]=b[i]+B[i-1]; rep(i,2,n) B[i]+=B[i-1];    init();    int Q; read(Q); map<int,ll>vis;    while (Q--)    {     int x; read(x);     if (x>100) { puts( 0 ); continue; }     if (x<-6000) { printf( %lld n ,pr); continue; }     if (vis.count(x)) { printf( %lld n ,vis[x]); continue; }     ll ans=work(x); printf( %lld n ,vis[x]=ans);    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int ans;   map<int, int> f;   int main() {     cin >> s;     int n = s.length();     f[0] = 1;     int cur = 0;     for (int i = 1; i < n; i++) {       if (s[i] == s[i - 1]) {         f[cur]++;       } else {         f[i]++;         cur = i;       }     }     while (f.size() > 1) {       int _min = 10000000;       for (auto i = f.begin(); i != f.end(); ++i) {         if (i == f.begin() || next(i) == f.end())           cur = i->second;         else           cur = ceil(i->second / 2.0);         _min = min(_min, cur);       }       ans += _min;       for (auto i = f.begin(); i != f.end(); ++i) {         cur = _min;         auto ci = i;         if (i == f.begin() || next(i) == f.end()) {           i->second -= cur;         } else {           i->second -= cur + cur;         }         ci = i;       }       for (auto i = f.begin(); i != f.end();) {         auto ci = i;         ++i;         if (ci->second < 1) f.erase(ci);       }       if (f.size())         for (auto it = f.begin(); it != prev(f.end());) {           if (s[it->first] == s[next(it)->first]) {             next(it)->second += it->second;             auto cit = it;             ++it;             f.erase(cit);           } else             ++it;         }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<long double, long double> a[100005];   long double isum = 0, min1 = 1000000000000, isum1, t[100005],               min2 = 1000000000000, min3 = 1000000000000, min4 = 1000000000000,               min5 = 1000000000000;   int i, j;   long long ax, ay, bx, by, tx, ty, n, x[100005], y[100005], h, k;   bool used[100005];   int main() {     cin >> ax >> ay >> bx >> by >> tx >> ty >> n;     for (i = 0; i < 100005; i++) used[i] = 0;     for (i = 0; i < n; i++) {       cin >> x[i];       cin >> y[i];     }     a[n].first = 0;     a[n].second = 0;     for (i = 0; i < n; i++) {       t[i] = sqrt((tx - x[i]) * (tx - x[i]) + (ty - y[i]) * (ty - y[i]));       a[i].first =           sqrt((ax - x[i]) * (ax - x[i]) + (ay - y[i]) * (ay - y[i])) - t[i];       a[i].second =           sqrt((bx - x[i]) * (bx - x[i]) + (by - y[i]) * (by - y[i])) - t[i];       isum += 2 * t[i];     }     sort(a, a + n);     for (i = 0; i < n; i++) {       min2 = min(min2, a[i].first);     }     for (i = 0; i < n; i++) {       if (a[i].first == min2) {         h = i;         break;       }     }     for (i = 0; i < n; i++) {       if (i != h) min3 = min(min3, a[i].first);     }     for (i = 0; i < n; i++) {       min4 = min(min4, a[i].second);     }     for (i = 0; i < n; i++) {       if (a[i].second == min4) {         k = i;         break;       }     }     for (i = 0; i < n; i++) {       if (i != k) min5 = min(min5, a[i].second);     }     if (h != k) {       min1 = min(min1, min2 + min4);     } else {       min1 = min(min1, min2 + min5);       min1 = min(min1, min3 + min4);       min1 = min(min1, min3 + min5);     }     for (i = 0; i < n; i++) min1 = min(a[i].first, min1);     for (i = 0; i < n; i++) min1 = min(a[i].second, min1);     cout << fixed << setprecision(12) << isum + min1;   }


#include <bits/stdc++.h>   using namespace std;   const char nl =   n ;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, v1, v2, t1, t2;     cin >> n >> v1 >> v2 >> t1 >> t2;     if (n * v1 + t1 * 2 < n * v2 + t2 * 2)       cout <<  First  << nl;     else if (n * v1 + t1 * 2 > n * v2 + t2 * 2)       cout <<  Second  << nl;     else       cout <<  Friendship  << nl;   }


#include <bits/stdc++.h>   using namespace std;   void swap(long long &a, long long &b) {     auto tm = a;     a = b;     b = tm;   }   const long long mod = 1000000009;   const long long mod2 = 998244353;   const long long INF = (1LL << 20) - 1;   const long long N = 2e5 + 25;   long long n, m, a, b;   vector<long long> v[N];   set<long long> c, d;   bool vt[N];   void dfs1(long long x) {     vt[x] = 1;     for (auto &(i) : (v[x])) {       if (i == b) {         c.insert(x);         continue;       }       if (vt[i] == 0) dfs1(i);     }   }   void dfs2(long long x) {     vt[x] = 1;     for (auto &(i) : (v[x])) {       if (i == a) {         d.insert(x);         continue;       }       if (vt[i] == 0) dfs2(i);     }   }   void bheja_fry() {     c.clear();     d.clear();     cin >> n >> m >> a >> b;     for (long long(i) = (0); (i) < (n + 1); ++(i)) {       v[i].clear();       vt[i] = 0;     }     long long x, y;     for (long long(i) = (0); (i) < (m); ++(i)) {       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     long long ct1 = 0, ct2 = 0;     dfs1(a);     for (long long(i) = (0); (i) < (n + 1); ++(i)) vt[i] = 0;     dfs2(b);     queue<long long> q;     for (auto &(i) : (v[b])) {       if (c.find(i) == c.end()) q.push(i);     }     for (long long(i) = (0); (i) < (n + 1); ++(i)) vt[i] = 0;     vt[b] = 1;     while (!q.empty()) {       x = q.front();       q.pop();       if (vt[x]) continue;       ct1++;       vt[x] = 1;       for (auto &(i) : (v[x])) {         if (vt[i] == 0) q.push(i);       }     }     for (auto &(i) : (v[a])) {       if (d.find(i) == d.end()) q.push(i);     }     for (long long(i) = (0); (i) < (n + 1); ++(i)) vt[i] = 0;     vt[a] = 1;     while (!q.empty()) {       x = q.front();       q.pop();       if (vt[x]) continue;       ct2++;       vt[x] = 1;       for (auto &(i) : (v[x])) {         if (vt[i] == 0) q.push(i);       }     }     cout << ct1 * ct2;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     for (long long(i) = (1); (i) < (t + 1); ++(i)) {       bheja_fry();       if (i < t) cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   void rd(int &x) {     x = 0;     int f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = getchar();     x *= f;   }   void lrd(long long &x) {     x = 0;     int f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = getchar();     x *= f;   }   const int INF = 1e9;   const long long LINF = 1e18;   const int inf = 1e5 + 10;   using namespace std;   int T;   long long n;   int m, a[inf];   int cnt[100];   map<int, int> S;   int sta[100], top;   int main() {     rd(T);     int now = 1;     for (int i = 0; i <= 30; i++) S[now] = i, now *= 2;     while (T--) {       lrd(n);       rd(m);       memset(cnt, 0, sizeof(cnt));       long long sum = 0;       for (int i = 1; i <= m; i++) rd(a[i]), cnt[S[a[i]]]++, sum += a[i];       if (sum < n)         printf( -1 n );       else {         int now = 0;         top = 0;         while (n) {           if (n & 1) {             if (cnt[now])               cnt[now]--;             else               sta[++top] = now;           }           now++;           n /= 2;         }         int ans = 0;         for (int i = 1; i <= top;) {           for (int j = 0; j < sta[i]; j++) cnt[j + 1] += cnt[j] / 2, cnt[j] %= 2;           for (int j = sta[i]; j <= 99; j++) {             if (cnt[j]) {               ans += j - sta[i];               cnt[j]--;               for (int k = sta[i]; k < j; k++) cnt[k]++;               while (i <= top && sta[i] <= j) i++;               break;             }           }         }         printf( %d n , ans);       }     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   #define lp(i,a,n) for(i=a;i<n;i++)   #define ld(i,a,n) for(i=n-1;i>=a;i--)   #define ll long long   #define dd double   #define mod 10000007   #define inf 1e18   #define inp(t) cin>>t; while(t--)   int main(){    int t;    ios::sync_with_stdio(false);    inp(t){     ll n,i,j,k;     cin>>n;     int arr[n];     lp(i,0,n)     cin>>arr[i];     int ans;     int sm=accumulate(arr,arr+n,0);     int chk=1;     lp(i,0,n){      k=n-i;      int val;      if(sm%k==0)      val=sm/k;      else      continue;      int smm=0;      lp(j,0,n){       smm+=arr[j];       if(smm<val)       continue;       else if(smm==val&&j!=n-1){        smm=0;        continue;       }       else if(smm==val&&j==n-1){        chk=0;        ans=i;        break;       }       else{        smm=0;        break;       }                    }      if(chk==0)       break;     }     cout<<ans<<endl;          }          }


#include <bits/stdc++.h>   using namespace std;   int n;   double V1;   double V2[100005];   double L[100005];   double D[100005];   double X[100005];   bool check(double T) {     bool ok1 = false;     bool ok1b = false;     bool ok2 = false;     bool ok2b = false;     double l = 0;     double r = 1000000;     vector<pair<double, double> > V;     V.clear();     for (int i = 1; i <= n; i++) {       if (D[i] == 1 && !ok1) {         if (L[i] <= V2[i] * T) {           ok1 = true;         } else {           double TS = T - L[i] / (V1 + V2[i]);           double MS = 1 / (V1 - V2[i]) - V2[i] / (V1 * V1 - V2[i] * V2[i]);           if (TS < 0) continue;           ok1b = true;           double x;           if (TS > MS * 10000000)             x = 1000000;           else             x = TS / MS;           if (x < 1) x = 0;           if (X[i] > l) l = X[i];           if (X[i] + x < r) r = X[i] + x;           V.push_back(make_pair(X[i], X[i] + x));         }       }     }     for (int i = 1; i <= n; i++) {       if (D[i] == 2 && !ok2) {         if (L[i] <= V2[i] * T) {           ok2 = true;         } else {           double TS = T - L[i] / (V1 + V2[i]);           double MS = 1 / (V1 - V2[i]) - V2[i] / (V1 * V1 - V2[i] * V2[i]);           if (TS < 0) continue;           ok2b = true;           double x;           if (TS > MS * 10000000)             x = 1000000;           else             x = TS / MS;           if (x < 1) x = 0;           if (X[i] - x > l) l = X[i] - x;           if (X[i] < r) r = X[i];           for (int j = 0; j < V.size(); j++)             if ((V[j].first <= X[i] && X[i] <= V[j].second) ||                 (V[j].first <= (X[i] - x) && (X[i] - x) <= V[j].second)) {               return true;             }         }       }     }     return ((ok1 && ok2b) || (ok2 && ok1b) || (ok1 && ok2));   }   int main() {     cin >> n >> V1;     for (int i = 1; i <= n; i++) {       cin >> X[i] >> V2[i] >> D[i];       if (D[i] == 1)         L[i] = X[i];       else         L[i] = 1000000 - X[i];     }     double l = 0;     double r = 10000000;     while (0.0000001 <= r - l) {       double m = (l + r) / 2;       if (check(m))         r = m - 0.00000001;       else         l = m + 0.00000001;     }     printf( %0.8f n , r);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> pat[100000];   int main() {     int num, way;     scanf( %d%d , &num, &way);     set<pair<int, int> > se;     for (int i = 0; i < way; i++) {       int za, zb;       scanf( %d%d , &za, &zb);       za--;       zb--;       pat[za].push_back(zb);       pat[zb].push_back(za);       se.insert(make_pair(za, zb));       se.insert(make_pair(zb, za));     }     if (num <= 9) {       vector<int> vec;       for (int i = 0; i < num; i++) {         vec.push_back(i);       }       for (;;) {         bool f = true;         for (int i = 0; i < way; i++) {           if (se.find(make_pair(vec[i], vec[(i + 1) % num])) != se.end()) {             f = false;             break;           }         }         if (f) {           for (int i = 0; i < way; i++) {             printf( %d %d n , vec[i] + 1, vec[(i + 1) % num] + 1);           }           return 0;         }         if (!next_permutation(vec.begin(), vec.end())) {           printf( -1 n );           return 0;         }       }     }     vector<pair<int, int> > ans;     int now = 0;     set<int> rem;     for (int i = 1; i < num; i++) {       rem.insert(i);     }     for (int i = 0; i < num - 9; i++) {       set<int>::iterator it = rem.begin();       for (;;) {         if (se.find(make_pair(now, (*it))) == se.end()) {           ans.push_back(make_pair(now, (*it)));           now = *it;           rem.erase(it);           break;         }         it++;       }     }     set<int>::iterator it = rem.begin();     vector<int> vec;     for (int i = num - 8; i < num; i++) {       vec.push_back((*it));       it++;     }     for (;;) {       bool f = true;       for (int i = 0; i < 7; i++) {         if (se.find(make_pair(vec[i], vec[i + 1])) != se.end()) {           f = false;           break;         }       }       if (se.find(make_pair(ans[ans.size() - 1].second, vec[0])) != se.end() ||           se.find(make_pair(vec[vec.size() - 1], ans[0].first)) != se.end()) {         f = false;       }       if (f) {         ans.push_back(make_pair(ans[ans.size() - 1].second, vec[0]));         for (int i = 0; i < 7; i++) {           ans.push_back(make_pair(vec[i], vec[i + 1]));         }         ans.push_back(make_pair(vec[vec.size() - 1], ans[0].first));         break;       }       if (!next_permutation(vec.begin(), vec.end())) {         abort();         return 0;       }     }     for (int i = 0; i < way; i++) {       printf( %d %d n , ans[i].first + 1, ans[i].second + 1);     }   }


#include <bits/stdc++.h>   using namespace std;   string s1, s2;   int a, b;   int nxt[205], cnt[205];   int main() {     scanf( %d%d , &a, &b);     cin >> s1 >> s2;     for (int i = 0; i < s2.size(); i++) {       int pos = i;       for (int r = 0; r < s1.size(); r++) {         if (s2[pos] == s1[r]) {           pos++;           if (pos == s2.size()) cnt[i]++, pos = 0;         }       }       nxt[i] = pos;     }     int ans = 0;     int poss = 0;     for (int i = 1; i <= a; i++) ans += cnt[poss], poss = nxt[poss];     cout << ans / b;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 500005;   const int MC = 2;   int get(char c) {     if (c ==  ( ) return 0;     return 1;   }   struct Node {     int l, r, pardp;     Node *par, *sLink;     Node *chd[MC];     bool isLeaf;     int st;     Node() {       l = r = pardp = 0;       par = sLink = NULL;       st = 0;       isLeaf = true;       memset(chd, 0, sizeof chd);     }     int len() { return r - l; }     int depth() { return pardp + len(); }     bool inEdge(int pos) { return pos >= pardp && pos < depth(); }     void setEdge(Node *child, int l, int r, char *S) {       child->par = this;       child->pardp = depth();       chd[get(S[l])] = child;       child->l = l, child->r = r;       isLeaf = false;     }     void print(char *S) {       for (int i = l; i < r; i++) putchar(S[i]);       puts(  );     }   };   struct Data {     int l, r, x;     Data(int l = 0, int r = 0, int x = 0) : l(l), r(r), x(x) {}     bool operator<(const Data &b) const { return l < b.l; }   };   vector<Data> que;   struct STree {     Node *root, *needSLink, *cur;     int jj, m, size;     bool needWalk;     vector<Node> nodes;     char *S;     STree(char *str) {       m = strlen(str);       S = str;       nodes.reserve(m * 2 + 10);       jj = 1, size = 0;       root = newNode();       cur = newNode();       root->setEdge(cur, 0, m, S);       needSLink = NULL;       needWalk = true;       for (int i = 0; i < m - 1; i++) extend(i);     }     Node *newNode() {       nodes.push_back(Node());       return &nodes[size++];     }     Node *walk_down(Node *c, int j, int i) {       if (i - j + 1 > 0)         for (int h = j + c->depth(); !c->inEdge(i - j); h += c->len())           c = c->chd[get(S[h])];       return c;     }     void addSLink(Node *c) {       if (needSLink) needSLink->sLink = c;       needSLink = NULL;     }     void extend(int i) {       char c = S[i + 1];       Node *leaf, *split;       int k, pos;       for (; jj <= i + 1; jj++) {         if (needWalk) {           if (!cur->sLink && cur->par) cur = cur->par;           cur = (cur->sLink) ? cur->sLink : root;           cur = walk_down(cur, jj, i);         }         needWalk = true;         k = i + 1 - jj;         if (cur->depth() == k) {           addSLink(cur);           if (cur->chd[get(c)]) {             cur = cur->chd[get(c)];             needWalk = false;             break;           } else {             leaf = newNode();             cur->setEdge(leaf, i + 1, m, S);           }         } else {           pos = cur->l + k - cur->pardp;           if (S[pos] == c) {             addSLink(cur);             needWalk = false;             break;           } else {             leaf = newNode();             split = newNode();             cur->par->setEdge(split, cur->l, pos, S);             split->setEdge(cur, pos, cur->r, S);             split->setEdge(leaf, i + 1, m, S);             addSLink(split);             if (split->depth() == 1)               split->sLink = root;             else               needSLink = split;             cur = split;           }         }       }     }     void calc(Node *c) {       int bf = 0;       int l = c->l - c->pardp, r = c->r;       if (r) que.push_back(Data(l, r, 1));       for (int i = 0; i < MC; i++)         if (c->chd[i]) {           if (r) que.push_back(Data(l, r, -1));           calc(c->chd[i]);           bf++;         }     }   };   char A[MX];   int val[MX * 4], cnt[MX * 4], dn[MX * 4];   int p[MX];   void add(pair<int, int> &p, pair<int, int> q) {     p.first = min(p.first, q.first);     p.second += q.second;   }   void up(int u) {     int l = u * 2, r = u * 2 + 1;     val[u] = min(val[l], val[r]);     cnt[u] = 0;     if (val[u] == val[l]) cnt[u] += cnt[l];     if (val[u] == val[r]) cnt[u] += cnt[r];   }   void renew(int u, int x) {     val[u] += x;     dn[u] += x;   }   void down(int u) {     val[u] += dn[u];     renew(u * 2, dn[u]);     renew(u * 2 + 1, dn[u]);     dn[u] = 0;   }   void build(int u, int l, int r) {     if (l == r) {       val[u] = p[l];       cnt[u] = 1;       dn[u] = 0;       return;     }     int md = (l + r) / 2;     build(u * 2, l, md);     build(u * 2 + 1, md + 1, r);     up(u);   }   void add(int u, int l, int r, int st, int en, int x) {     if (st <= l && r <= en) {       renew(u, x);       return;     }     int md = (l + r) / 2;     down(u);     if (st <= md) add(u * 2, l, md, st, en, x);     if (md < en) add(u * 2 + 1, md + 1, r, st, en, x);     up(u);   }   int bf = false;   pair<int, int> get(int u, int l, int r, int st, int en) {     pair<int, int> ret(0, 0);     if (st <= l && r <= en) {       if (val[u] == 0) return pair<int, int>(val[u], cnt[u]);       if (val[u] > 0) return pair<int, int>(val[u], 0);       if (l == r) return pair<int, int>(val[u], 0);       down(u);       int md = (l + r) / 2;       ret = get(u * 2, l, md, st, en);       if (ret.first >= 0) {         add(ret, get(u * 2 + 1, md + 1, r, st, en));       }       up(u);       return ret;     }     int md = (l + r) / 2;     down(u);     if (st <= md) ret = get(u * 2, l, md, st, en);     if (md < en)       if (ret.first >= 0) add(ret, get(u * 2 + 1, md + 1, r, st, en));     up(u);     return ret;   }   int main() {     int n;     scanf( %d%s , &n, A);     STree tr(A);     int dp = 0;     for (int i = 0; i < n; i++) {       if (A[i] ==  ( )         dp++;       else         dp--;       p[i + 1] = dp;     }     build(1, 1, n);     tr.calc(tr.root);     sort(que.begin(), que.end());     int lft = 0;     long long ans = 0;     for (int i = 0; i < que.size();) {       while (lft < que[i].l) {         if (A[lft] ==  ( )           add(1, 1, n, 1, n, -1);         else           add(1, 1, n, 1, n, 1);         lft++;       }       while (i < que.size() && que[i].l == lft) {         pair<int, int> ret = get(1, 1, n, lft + 1, que[i].r);         ans += ret.second * que[i].x;         i++;       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 7005;   const int Maxm = 1000006;   int n, m;   int a[Maxn], b[Maxn], c[Maxn], d[Maxn], e[Maxn];   int mn[Maxn], mx[Maxn];   bool has[Maxm];   vector<int> tk;   int main() {     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d %d %d , &a[i], &b[i], &c[i]);       if (a[i] == 1)         scanf( %d %d , &d[i], &e[i]);       else {         mn[b[i]] = mx[b[i]] = c[i];         int tomin = 0, tomax = 0;         for (int l = b[i] + 1; l <= n; l++) {           while (1 << tomin <= mn[l - 1] - 1) tomin++;           mn[l] = mn[l - 1] - 1 + tomin + 1;           while (1 << tomax <= mx[l - 1] - 1) tomax++;           mx[l] = mx[l - 1] - 1 + tomax + 1 + (mx[l - 1] == 1 << tomax);         }         for (int j = 0; j < i; j++)           if (b[i] <= b[j] && max(mn[b[j]], c[j]) <= min(mx[b[j]], d[j]) &&               !has[e[j]]) {             has[e[j]] = true;             tk.push_back(e[j]);           }         printf( %d n , tk.size());         for (int i = 0; i < tk.size(); i++) has[tk[i]] = false;         tk.clear();       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   struct Node {     int pos, val, sit;   } a[305];   bool cmp(Node x, Node y) {     if (x.val == y.val) {       return x.pos > y.pos;     }     return x.val < y.val;   }   bool cmp2(Node x, Node y) { return x.pos < y.pos; }   int main() {     int T;     std::cin >> T;     while (T--) {       cin >> n >> m;       for (int i = 1; i <= m; i++) {         scanf( %d , &a[i].val);         a[i].pos = i;       }       sort(a + 1, a + 1 + m, cmp);       for (int i = 1; i <= m; i++) {         a[i].sit = i;       }       sort(a + 1, a + 1 + m, cmp2);       int ans = 0;       for (int i = 2; i <= m; i++) {         for (int j = i - 1; j >= 1; j--) {           if (a[j].sit < a[i].sit) ans++;         }       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INTINF = 0x3f3f3f3f;   const long long INF = 0x3f3f3f3f3f3f3f3f;   const int MAXN = 55;   const int MAXM = (1 << 20) + 1;   long long dp[MAXM], C[MAXN][MAXN];   int n, m;   char a[MAXN][MAXN];   double ans = 0;   int count(long long x) {     int res = 0;     while (x) x &= (x - 1), res++;     return res;   }   void init() {     for (int i = 0; i <= m; i++)       for (int j = C[i][0] = 1; j <= i; j++)         C[i][j] = C[i - 1][j - 1] + C[i - 1][j];   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %s , a[i]);     m = strlen(a[0]);     init();     for (long long i = 0; i < n; i++)       for (long long j = 0; j < i; j++) {         int same_bit = 0;         for (int k = 0; k < m; k++)           if (a[i][k] == a[j][k]) same_bit |= 1 << k;         dp[same_bit] |= (1ll << i) | (1ll << j);       }     for (int s = (1 << m) - 1; ~s; s--)       for (int i = 0; i < m; i++)         if ((1 << i) & s) dp[s ^ (1 << i)] |= dp[s];     for (int s = 0; s < (1 << m); s++) {       ans += (double)count(dp[s]) / n / C[m][count(s)];     }     printf( %.10lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, s, t, l, rest;   long long d[1010], d2[1010], w[10010];   bool zeredg[10010];   pair<long long, long long> edg[10010];   vector<vector<long long> > edglist;   void dijkstra(long long dist[], bool sec) {     set<pair<long long, long long> > najbl;     dist[s] = 0;     najbl.insert({0, s});     for (long long i = 0; i < n; ++i) {       if (s != i) {         dist[i] = 1e18;         najbl.insert({dist[i], i});       }     }     while (najbl.size()) {       long long v = najbl.begin()->second;       najbl.erase(najbl.begin());       for (auto i : edglist[v]) {         long long u = edg[i].first + edg[i].second - v;         if (sec and dist[u] > dist[v] + w[i] and zeredg[i] and             w[i] < d[u] + rest - dist[v]) {           w[i] = d[u] + rest - dist[v];         }         if (dist[u] > dist[v] + w[i]) {           najbl.erase({dist[u], u});           dist[u] = dist[v] + w[i];           najbl.insert({dist[u], u});         }       }     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m >> l >> s >> t;     vector<long long> vi;     edglist.resize(n, vi);     for (long long i = 0; i < m; ++i) {       long long u, v, e;       cin >> u >> v >> e;       edglist[u].push_back(i);       edglist[v].push_back(i);       edg[i] = {u, v};       if (e) {         w[i] = e;       } else {         w[i] = 1;         zeredg[i] = 1;       }     }     dijkstra(d, 0);     if (d[t] > l) {       cout <<  NO ;       return 0;     } else if (d[t] == l) {       cout <<  YES n ;       for (long long i = 0; i < m; ++i) {         cout << edg[i].first <<     << edg[i].second <<     << w[i] <<   n ;       }       return 0;     }     rest = l - d[t];     dijkstra(d2, 1);     if (d2[t] != l) {       cout <<  NO n ;       return 0;     }     cout <<  YES n ;     for (long long i = 0; i < m; ++i) {       cout << edg[i].first <<     << edg[i].second <<     << w[i] <<   n ;     }   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   int a[200007];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     sort(a, a + n);     int c = n / 2 - 1;     int l = 0, r = n - 1;     int mn = 1e9 + 77;     for (int i = 0; i <= c; i++) {       mn = min(mn, a[r - c + i] - a[l + i]);     }     cout << mn;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class _T>   inline _T sqr(const _T &first) {     return first * first;   }   template <class _T>   inline string tostr(const _T &a) {     ostringstream os(  );     os << a;     return os.str();   }   const long double PI = 3.1415926535897932384626433832795L;   const long double EPS = 1e-12;   char TEMPORARY_CHAR;   const int INF = 1e9;   inline void fft(vector<complex<long double> > &a, bool invert) {     int n = (int)a.size();     for (int i = 1, j = 0; i < n; ++i) {       int bit = n >> 1;       for (; j >= bit; bit >>= 1) j -= bit;       j += bit;       if (i < j) swap(a[i], a[j]);     }     for (int len = 2; len <= n; len <<= 1) {       long double ang = 2 * PI / len * (invert ? -1 : 1);       complex<long double> wlen(cos(ang), sin(ang));       for (int i = 0; i < n; i += len) {         complex<long double> w(1);         for (int j = 0; j < len / 2; ++j) {           complex<long double> u = a[i + j], v = a[i + j + len / 2] * w;           a[i + j] = u + v;           a[i + j + len / 2] = u - v;           w *= wlen;         }       }     }     if (invert)       for (int i = 0; i < n; ++i) a[i] /= n;   }   inline void input(int &a) {     a = 0;     while (((TEMPORARY_CHAR = getchar()) >  9  || TEMPORARY_CHAR <  0 ) &&            (TEMPORARY_CHAR !=  - )) {     }     char neg = 0;     if (TEMPORARY_CHAR ==  - ) {       neg = 1;       TEMPORARY_CHAR = getchar();     }     while (TEMPORARY_CHAR <=  9  && TEMPORARY_CHAR >=  0 ) {       a = (a << 3) + (a << 1) + TEMPORARY_CHAR -  0 ;       TEMPORARY_CHAR = getchar();     }     if (neg) a = -a;   }   inline void out(long long a) {     if (!a) putchar( 0 );     if (a < 0) {       putchar( - );       a = -a;     }     char s[20];     int i;     for (i = 0; a; ++i) {       s[i] =  0  + a % 10;       a /= 10;     }     for (int j = (i)-1; j >= 0; j--) putchar(s[j]);   }   inline int nxt() {     int(ret);     input((ret));     ;     return ret;   }   struct lnum {     vector<int> a;     int base;     lnum(int num = 0, int base = 1000000000) : base(base) {       if (!num) a.resize(1);       while (num) {         a.push_back(num % base);         num /= base;       }     }     inline int len() const { return a.size(); }     lnum &operator=(const lnum &l) {       if (this != &l) {         a = l.a;         base = l.base;       }       return *this;     }     inline friend lnum operator+(const lnum &l, const lnum &r) {       lnum ret(0, l.base);       int base = l.base;       int ln = l.len(), rn = r.len();       int n = max(ln, rn);       ret.a.resize(n);       int o = 0;       for (int i = 0; i < n; ++i) {         int s = o;         if (i < ln) s += l.a[i];         if (i < rn) s += r.a[i];         o = s >= base;         if (o) s -= base;         ret.a[i] = s;       }       if (o) ret.a.push_back(1);       return ret;     }     inline friend lnum operator-(const lnum &l, const lnum &r) {       lnum ret(0, l.base);       int base = l.base;       int n = l.len();       int rn = r.len();       ret.a.resize(n);       int o = 0;       for (int i = 0; i < n; ++i) {         int s = l.a[i] - o;         if (i < rn) s -= r.a[i];         o = s < 0;         if (o) s += base;         ret.a[i] = s;       }       if (ret.len() > 1 && !ret.a.back()) ret.a.pop_back();       return ret;     }     inline friend lnum operator*(const lnum &l, const lnum &r) {       lnum ret(0, l.base);       int base = l.base;       if (l.len() * r.len() > 1000000) {         vector<complex<long double> > fa(l.a.begin(), l.a.end()),             fb(r.a.begin(), r.a.end());         int n = 1;         while (n < max(l.len(), r.len())) n <<= 1;         n <<= 1;         fa.resize(n), fb.resize(n);         fft(fa, false), fft(fb, false);         for (int i = 0; i < n; ++i) fa[i] *= fb[i];         fft(fa, true);         ret.a.resize(n);         for (int i = 0; i < n; ++i) ret.a[i] = int(fa[i].real() + 0.5);         int carry = 0;         for (int i = 0; i < n; ++i) {           ret.a[i] += carry;           carry = ret.a[i] / base;           ret.a[i] %= base;         }       } else {         ret.a.resize(l.len() + r.len());         for (int i = 0; i < l.len(); ++i)           for (int j = 0, carry = 0; j < r.len() || carry; ++j) {             long long cur = ret.a[i + j] +                             (long long)l.a[i] * (j < r.len() ? r.a[j] : 0) +                             carry;             ret.a[i + j] = cur % base;             carry = cur / base;           }       }       while (ret.len() > 1 && !ret.a.back()) ret.a.pop_back();       return ret;     }     inline friend lnum operator/(const lnum &l, const int &r) {       lnum ret(0, l.base);       ret.a.resize(l.len());       int carry = 0;       for (int i = l.len() - 1; i >= 0; --i) {         long long cur = l.a[i] + (long long)carry * l.base;         ret.a[i] = cur / r;         carry = cur % r;       }       while (ret.len() > 1 && ret.a.back() == 0) ret.a.pop_back();       return ret;     }     inline friend bool operator<(const lnum &l, const lnum &r) {       if (l.len() < r.len()) return true;       if (l.len() > r.len()) return false;       int n = l.len();       for (int i = n - 1; i >= 0; --i) {         if (l.a[i] < r.a[i]) return true;         if (l.a[i] > r.a[i]) return false;       }       return false;     }     inline friend bool operator>(const lnum &l, const lnum &r) { return r < l; }     inline friend bool operator==(const lnum &l, const lnum &r) {       if (l.len() != r.len()) return false;       int n = l.len();       for (int i = n - 1; i; --i) {         if (l.a[i] != r.a[i]) return false;       }       return true;     }     inline friend bool operator!=(const lnum &l, const lnum &r) {       return !(l == r);     }     inline void print() {       if (base == 1000000000) {         printf( %d , a.back());         for (int i = a.size() - 2; i >= 0; --i) printf( %09d , a[i]);       } else {         for (int i = a.size() - 1; i >= 0; --i) printf( %d , a[i]);       }     }   };   vector<int> get(int n, int len) {     vector<int> ret;     ret.push_back(n);     ret.push_back(-n);     for (int i = 1, p = 10; i < len; ++i, p *= 10) {       vector<int> a = get(n % p, i);       vector<int> b = get(n / p, len - i);       for (int i = 0; i < (int)(a.size()); i++) {         for (int j = 0; j < (int)(b.size()); j++) {           ret.push_back(a[i] * b[j]);           ret.push_back(a[i] + b[j]);         }       }     }     sort((ret).begin(), (ret).end());     ret.resize(unique((ret).begin(), (ret).end()) - ret.begin());     return ret;   }   int main() {     int(k);     input((k));     ;     int(m);     input((m));     ;     for (int i = 0; i < (int)(10000); i++) {       vector<int> cur = get(i, 4);       for (int j = 0; j < (int)(cur.size()); j++) {         if (k - cur[j] < 10000 && k - cur[j] >= 0) {           printf( %04d%04d n , abs(k - cur[j]), i);           --m;           if (!m) return 0;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double long EPS = 1e-10;   struct wor {     double long x = 0, y = 0;   };   double long dist(wor a, wor b) {     double long answer =         sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));     return answer;   }   pair<vector<double long>, vector<long long>> djkstra(       long long from, long long n,       vector<vector<pair<double long, long long>>> &adj) {     vector<bool> used(n);     vector<double long> d(n, 1e15);     vector<long long> f(n);     d[from] = 0;     f[from] = from;     for (int i = 0; i < n; i++) {       long long mi = -1;       for (int i = 0; i < n; i++) {         if (!used[i]) {           mi = i;           break;         }       }       for (int j = 0; j < n; j++) {         if (!used[j] && d[j] < d[mi]) {           mi = j;         }       }       used[mi] = true;       for (auto e : adj[mi]) {         if (!used[e.second] && d[mi] + e.first < d[e.second]) {           d[e.second] = d[mi] + e.first;           f[e.second] = mi;         }       }     }     return {d, f};   }   struct Kun1 {     long long n;     vector<vector<long long>> adj;     vector<long long> used;     vector<bool> is_l;     vector<long long> mt;     Kun1(long long n) {       this->n = n;       adj.resize(n);       used.resize(n);       mt.resize(n);       is_l.resize(n);     }     void add_edge(long long u, long long v) {       adj[u].push_back(v);       adj[v].push_back(u);     }     void make_left(long long v) { is_l[v] = true; }     bool try_kun(long long v, long long cur) {       if (used[v] == cur) return false;       used[v] = cur;       for (auto e : adj[v]) {         if (mt[e] == -1 || try_kun(mt[e], cur)) {           mt[e] = v;           return true;         }       }       return false;     }     long long max_matching() {       vector<bool> usel(n);       for (int i = 0; i < n; i++) {         if (!is_l[i]) mt[i] = -1;       }       long long answer = 0;       for (int i = 0; i < n; i++) {         if (is_l[i]) {           for (auto e : adj[i]) {             if (mt[e] == -1) {               mt[e] = i;               answer++;               usel[i] = true;               break;             }           }         }       }       long long z = 1;       for (int i = 0; i < n; i++) {         if (is_l[i] && !usel[i] && try_kun(i, z++)) answer++;       }       return answer;     }   };   struct MCMF {     struct edge {       long long from, to, flow, cap, cost;     };     vector<edge> edges;     vector<vector<long long>> adj;     vector<long long> d;     vector<long long> p;     deque<long long> q;     vector<long long> state;     const long long levit_inf = 2e9;     long long n, s, t;     MCMF(long long _n, long long _s, long long _t) {       n = _n;       s = _s;       t = _t;       adj.resize(n);       d.resize(n);       p.resize(n);       state.resize(n);     }     void add_edge(long long from, long long to, long long cap, long long cost) {       edge e1 = {from, to, 0, cap, cost};       edge e2 = {to, from, 0, 0, -cost};       adj[from].push_back(edges.size());       edges.push_back(e1);       adj[to].push_back(edges.size());       edges.push_back(e2);     }     void relax(long long v, long long dist, long long from) {       if (d[v] <= dist) {         return;       }       d[v] = dist;       p[v] = from;       if (state[v] == 0 || (!q.empty() && d[q.front()] > d[v])) {         q.push_front(v);         state[v] = 1;       } else if (state[v] == 2) {         q.push_back(v);         state[v] = 1;       }     }     bool levit() {       fill(d.begin(), d.end(), levit_inf);       fill(p.begin(), p.end(), -1);       relax(s, 0, 0);       while (!q.empty()) {         long long v = q.front();         q.pop_front();         state[v] = 2;         for (auto i = 0; i < adj[v].size(); i++) {           auto e = adj[v][i];           if (edges[e].flow < edges[e].cap) {             relax(edges[e].to, d[v] + edges[e].cost, e);           }         }       }       return d[t] < levit_inf;     }     pair<long long, long long> max_flow() {       long long flow = 0;       long long cost = 0;       while (levit()) {         long long ed = p[t];         long long mi_fl = edges[ed].cap - edges[ed].flow;         while (edges[ed].from != s) {           mi_fl = min(mi_fl, edges[ed].cap - edges[ed].flow);           ed = p[edges[ed].from];         }         mi_fl = min(mi_fl, edges[ed].cap - edges[ed].flow);         ed = p[t];         while (edges[ed].from != s) {           edges[ed].flow += mi_fl;           edges[ed ^ 1].flow -= mi_fl;           cost += edges[ed].cost * mi_fl;           ed = p[edges[ed].from];         }         edges[ed].flow += mi_fl;         edges[ed ^ 1].flow -= mi_fl;         cost += edges[ed].cost * mi_fl;         flow += mi_fl;       }       return {cost, flow};     }   };   struct MCMF1 {     struct edge {       long long from, to, cap, flow, cost;     };     vector<edge> edges;     vector<vector<long long>> adj;     vector<long long> p;     vector<long long> d;     vector<long long> state;     const long long lenin_inf = 2e9;     long long n, s, t;     MCMF1(long long _n, long long _s, long long _t) {       n = _n;       s = _s;       t = _t;       adj.resize(n);       p.resize(n);       d.resize(n);       state.resize(n);     }     void add_edge(long long from, long long to, long long cap, long long cost) {       edge e1 = {from, to, cap, 0, cost};       edge e2 = {to, from, 0, 0, -cost};       adj[from].push_back(edges.size());       edges.push_back(e1);       adj[to].push_back(edges.size());       edges.push_back(e2);     }     deque<long long> q;     void relax(long long v, long long dist, long long from) {       if (d[v] <= dist) return;       d[v] = dist;       p[v] = from;       if (state[v] == 0 || (!q.empty() && d[v] < d[q.front()])) {         q.push_front(v);         state[v] = 1;       } else if (state[v] == 2) {         q.push_back(v);         state[v] = 1;       }     }     bool lenin() {       fill(d.begin(), d.end(), lenin_inf);       fill(p.begin(), p.end(), -1);       fill(state.begin(), state.end(), 0);       relax(s, 0, 0);       while (!q.empty()) {         long long v = q.front();         q.pop_front();         state[v] = 2;         for (auto e : adj[v]) {           if (edges[e].cap - edges[e].flow == 0) continue;           relax(edges[e].to, d[v] + edges[e].cost, e);         }       }       return d[t] < lenin_inf;     }     pair<long long, long long> max_flow() {       long long flow = 0;       long long cost = 0;       while (lenin()) {         long long ed = p[t];         long long ma = edges[ed].cap - edges[ed].flow;         while (edges[ed].from != s) {           ma = min(ma, edges[ed].cap - edges[ed].flow);           ed = p[edges[ed].from];         }         ma = min(ma, edges[ed].cap - edges[ed].flow);         ed = p[t];         while (edges[ed].from != s) {           edges[ed].flow += ma;           edges[ed ^ 1].flow -= ma;           cost += ma * edges[ed].cost;           ed = p[edges[ed].from];         }         edges[ed].flow += ma;         edges[ed ^ 1].flow -= ma;         cost += ma * edges[ed].cost;         flow += ma;       }       return {cost, flow};     }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string t;     cin >> t;     long long n;     cin >> n;     vector<pair<string, long long>> arr(n);     vector<vector<long long>> num(n);     long long sum = 0;     long long z = n + 1;     for (int i = 0; i < n; i++) {       cin >> arr[i].first >> arr[i].second;       for (int j = 0; j < arr[i].first.length(); j++) {         num[i].push_back(z++);       }       sum += arr[i].first.length();     }     MCMF mcmf(2 + t.length() + n + sum, 0, 2 + t.length() + n + sum - 1);     for (int i = 0; i < n; i++) {       mcmf.add_edge(0, i + 1, arr[i].second, 0);     }     for (int i = 1; i <= n; i++) {       for (auto e : num[i - 1]) {         mcmf.add_edge(i, e, 1, 0);       }     }     vector<long long> tn;     for (int i = 0; i < t.length(); i++) {       tn.push_back(z++);     }     for (int i = 1; i <= n; i++) {       for (int j = 0; j < arr[i - 1].first.length(); j++) {         for (int z = 0; z < t.length(); z++) {           if (t[z] == arr[i - 1].first[j]) {             mcmf.add_edge(num[i - 1][j], tn[z], 1, i);           }         }       }     }     for (auto e : tn) {       mcmf.add_edge(e, 2 + t.length() + n + sum - 1, 1, 0);     }     auto e = mcmf.max_flow();     if (e.second != t.length()) {       cout << -1;     } else {       cout << e.first;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int target;     int weight;     int o;   };   struct vertice {     long long time;     int city;     int edge_ID;     int edge_W;   };   struct mysort {     bool operator()(const vertice &l, const vertice &r) {       if (l.time == r.time)         return l.edge_W < r.edge_W;       else         return l.time < r.time;     }   };   bool sort_pair(const pair<int, long long> &l, const pair<int, long long> &r) {     return l.first < r.first;   }   int main() {     int n, m;     cin >> n >> m;     vector<vector<edge> > E(n + 1);     for (int i = 0; i < m; i++) {       edge a, b;       cin >> a.target >> b.target >> a.weight;       b.weight = a.weight;       a.o = b.o = i + 1;       E[a.target].push_back(b);       E[b.target].push_back(a);     }     vertice v;     cin >> v.city;     v.time = 0;     v.edge_ID = 0;     v.edge_W = 0;     vector<pair<int, long long> > used_edges;     vector<int> visited(n + 1, 0);     int counter = 0;     multiset<vertice, mysort> S;     S.insert(v);     while (counter < n) {       multiset<vertice, mysort>::iterator it = S.begin();       if (visited[it->city] == 0) {         visited[it->city] = 1;         counter++;         used_edges.push_back(pair<int, long long>(it->edge_ID, it->edge_W));         for (int i = 0; i < E[it->city].size(); i++) {           vertice c;           c.time = it->time + E[it->city][i].weight;           c.city = E[it->city][i].target;           c.edge_ID = E[it->city][i].o;           c.edge_W = E[it->city][i].weight;           S.insert(c);         }       }       S.erase(it);     }     sort(used_edges.begin(), used_edges.end(), sort_pair);     long long ans = 0;     for (int i = 1; i < used_edges.size(); i++) ans += used_edges[i].second;     cout << ans << endl;     for (int i = 1; i < used_edges.size(); i++)       cout << used_edges[i].first <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   const int MAX_N = (int)3e5 + 123;   const long long inf = (long long)1e18 + 123;   using namespace std;   char ans[2][30];   string second;   pair<long long, long long> do_it(int l, int r) {     int len = r - l;     int last = 13 - (len + 1) / 2;     pair<long long, long long> res = make_pair(last, (len % 2));     swap(res.first, res.second);     last--;     for (int i = 1, id = last, down = 1; i <= len; i++) {       if (down) {         if (id + 1 < 13)           id++;         else           down = 0, id = 12;       } else         id--;       ans[down][id] = second[l + i - 1];     }     return res;   }   int main() {     cin >> second;     for (int i = 0; i + 1 < int((second).size()); i++)       if (second[i] == second[i + 1]) {         cout <<  Impossible ;         return 0;       }     int l = -1, r = -1;     for (int i = 0; i < int((second).size()); i++)       for (int j = i + 1; j < int((second).size()); j++)         if (second[i] == second[j]) l = i, r = j;     pair<long long, long long> hhey = do_it(l, r);     for (int len = l - 1, id = hhey.second, down = 1; len >= 0; len--) {       if (down) {         if (id)           id--;         else           id = 0, down = 0;       } else         id++;       ans[down][id] = second[len];     }     for (int len = r + 1, id = (hhey.second + hhey.first), up = 1;          len < int((second).size()); len++) {       if (up) {         if (id)           id--;         else           id = 0, up = 0;       } else         id++;       ans[up ^ 1][id] = second[len];     }     for (int i = 0; i < 2; i++) {       for (int j = 0; j < 13; j++) cout << ans[i][j];       cout << endl;     }     return 0;   }