#include <bits/stdc++.h>   using namespace std;   const long long oo = (long long)1e4;   long long n, k;   vector<pair<int, int> > l, r;   int length(int x) { return x < 10 ? 1 : length(x / 10) + 1; }   pair<int, int> checkWavy(int x) {     if (x < 10) return make_pair(1, 0);     int digits[8] = {x % 10, x / 10 % 10}, len = 2, sign = digits[1] > digits[0];     if (digits[1] == digits[0]) return make_pair(0, 0);     x /= 100;     while (x) {       digits[len] = x % 10;       x /= 10;       if (digits[len] == digits[len - 1]) return make_pair(0, 0);       int newSign = digits[len] > digits[len - 1];       if (newSign == sign) return make_pair(0, 0);       sign = newSign;       len++;     }     if (len < 6) return make_pair(1, 0);     if (len == 7) return make_pair(1, 1);     return make_pair(1, sign);   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> k;     for (int i = 1; i < int(1e7); i++) {       pair<int, int> u = checkWavy(i);       if (!u.first) continue;       if (i % n == 0)         if (!--k) {           cout << i << endl;           return 0;         }       l.push_back(make_pair(i, i % n));       if (u.second) r.push_back(make_pair(i % n, i));     }     sort(l.begin(), l.end());     sort(r.begin(), r.end());     for (int i = 0; i < int(l.size()); i++) {       if ((n - 1LL * l[i].second * int(1e7) % n) % n > int(1e7)) continue;       int mod = (n - 1LL * l[i].second * int(1e7) % n) % n;       int x = l[i].first, lastDigit = x % 10;       int from = lower_bound(r.begin(), r.end(), make_pair(mod, 0)) - r.begin();       int to = lower_bound(r.begin(), r.end(), make_pair(mod + 1, 0)) - r.begin();       if (from == to) continue;       for (int digit = 0; digit < 10 && from < to; digit++) {         int bound = lower_bound(r.begin() + from, r.begin() + to,                                 make_pair(mod, (digit + 1) * int(1e6))) -                     r.begin();         if (digit < lastDigit) {           if (x < 10 || x / 10 % 10 < lastDigit) {             int low = (digit * 10 + digit + 1) * int(1e5);             int idLow = lower_bound(r.begin() + from, r.begin() + bound,                                     make_pair(mod, low)) -                         r.begin();             if (bound - idLow >= k) {               cout << x;               for (int q = length(r[from + k - 1].second); q < 7; q++) cout << 0;               cout << r[idLow + k - 1].second << endl;               return 0;             } else               k -= bound - idLow;           }         }         if (digit > lastDigit) {           if (x < 10 || x / 10 % 10 > lastDigit) {             int high = (digit * 10 + digit) * int(1e5);             int idHigh = lower_bound(r.begin() + from, r.begin() + bound,                                      make_pair(mod, high)) -                          r.begin();             if (idHigh - from >= k) {               cout << x;               for (int q = length(r[from + k - 1].second); q < 7; q++) cout << 0;               cout << r[from + k - 1].second << endl;               return 0;             } else               k -= idHigh - from;           }         }         from = bound;       }     }     cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 505;   int a[N];   int b[N];   int dp[N];   int pv[N];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     int m;     cin >> m;     for (int i = 1; i <= m; i++) {       cin >> b[i];     }     int k;     for (int i = 1; i <= n; i++) {       k = 0;       for (int j = 1; j <= m; j++) {         if (a[i] == b[j]) {           if (dp[j] < dp[k] + 1) {             dp[j] = dp[k] + 1;             pv[j] = k;           }         } else if (b[j] < a[i]) {           if (dp[j] > dp[k]) {             k = j;           }         }       }     }     int d = 0;     for (int i = 1; i <= m; i++) {       if (dp[i] > dp[d]) d = i;     }     vector<int> ans;     while (d != 0) {       ans.push_back(b[d]);       d = pv[d];     }     reverse(ans.begin(), ans.end());     cout << ans.size() <<   n ;     for (auto x : ans) cout << x <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200010;   int main() {     int t;     cin >> t;     while (t--) {       int a, b;       cin >> a >> b;       int s = abs(a - b);       int ans = s / 5;       s %= 5;       if (s > 2)         ans += 2;       else if (s != 0)         ans++;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NN = (1 << 15);   int pre[NN], suf[NN];   map<string, int> mp;   int main() {     int n;     cin >> n;     for (int i = 1, x; i <= n; i++) {       cin >> x;       pre[i] = x >> 15;       suf[i] = x & (NN - 1);     }     for (int i = 0; i < NN; i++) {       string tmp;       for (int j = 2; j <= n; j++) {         tmp.push_back(__builtin_popcount(i ^ pre[j]) -                       __builtin_popcount(i ^ pre[1]));       }       if (!mp.count(tmp)) mp[tmp] = i;     }     for (int i = 0; i < NN; i++) {       string tmp;       for (int j = 2; j <= n; j++) {         tmp.push_back(__builtin_popcount(i ^ suf[1]) -                       __builtin_popcount(i ^ suf[j]));       }       if (mp.count(tmp)) {         cout << (mp[tmp] << 15) + i << endl;         return 0;       }     }     puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int m, n;   int mat[105][105];   int main() {     cin >> m >> n;     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         cin >> mat[i][j];       }     }     if (m % 2 != 0) {       cout << m << endl;     } else {       int aux = m;       bool flag = true;       int cont = 0;       bool flag2 = true;       while (flag) {         aux = aux / 2;         if (aux == 1) {           for (int j = 0; j < n; j++) {             if (mat[0][j] != mat[1][j]) {               flag2 = false;             }           }           if (flag2) {             cont++;           }           flag = false;           break;         }         for (int i = 0; i < aux; i++) {           for (int j = 0; j < n; j++) {             if (mat[aux - i - 1][j] != mat[aux + i][j]) {               flag = false;             }           }         }         if (flag) cont++;         if (aux % 2 != 0) break;       }       for (int i = 0; i < cont; i++) {         m = m / 2;       }       cout << m << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, m;     cin >> n >> m;     double num;     num = (double)m / n;     if (n == m) {       cout <<  0 ;     } else {       long long int x = num, counter1 = 0, counter2 = 0;       if (ceil(num) == num) {         if (x % 2 == 0 || x % 3 == 0) {           while (x % 2 == 0) {             x = x / 2;             counter1++;           }           while (x % 3 == 0) {             x = x / 3;             counter2++;           }           if (pow(2, counter1) * pow(3, counter2) == num)             cout << counter1 + counter2;           else             cout <<  -1 ;         } else           cout <<  -1 ;       } else         cout <<  -1 ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long calc(int n, int d, long long m, int l) {     for (long long i = 0; i < n; i++) {       long long high = i * m + l;       long long x = high % d;       if (d < (x + m - l)) {         return high + (d - x);       }     }     long long high = (n - 1) * m + l;     long long x = high % d;     return high + (d - x);   }   int main() {     int n, d, m, l;     scanf( %d %d %d %d , &n, &d, &m, &l);     cout << calc(n, d, m, l) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     if (a.second == b.second) return (a.first < b.first);     return (a.second < b.second);   }   int main(void) {     long long a, b;     cin >> a >> b;     string str = to_string(b);     reverse(str.begin(), str.end());     long long d = atoi(str.c_str());     cout << d + a;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   void finish(int x) {     printf( ! %d n , x);     fflush(stdout);     exit(0);   }   int query(int x) {     int a, b, mult = 1;     if (x > n / 2) {       x -= n / 2;       mult = -1;     }     printf( ? %d n , x);     fflush(stdout);     scanf( %d , &a);     printf( ? %d n , x + n / 2);     fflush(stdout);     scanf( %d , &b);     if (a == b) finish(x);     return mult * (b - a);   }   int main() {     scanf( %d , &n);     if (n % 4 != 0) finish(-1);     int offset = 0, l = 1, r = n / 2;     int dif = query(1);     if (dif < 0) offset += n / 2;     while (l < r) {       int m = (l + r) / 2;       if (query(m + offset) > 0)         l = m + 1;       else         r = m - 1;     }     if (query(l) != 0) finish(-1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500005;   int n, k, i, t, j, m;   char c[N];   long long ans, s;   struct str {     int l, r;   } p[N], a[N];   bool cmp(str a, str b) { return a.r - a.l > b.r - b.l; }   bool cmp2(str a, str b) { return a.r < b.r; }   int main() {     scanf( %d , &n);     scanf( %s , c + 1);     for (i = 1; i <= n; ++i)       if (c[i] ==  1 ) {         for (j = i; c[j] ==  1 ; ++j)           ;         p[++k] = (str){i, j - 1};         i = j;       }     sort(p + 1, p + 1 + k, cmp);     for (i = 1; i <= n; ++i) {       int las = 0;       for (j = 1; j <= k; ++j)         if (p[j].r - p[j].l + 1 >= i)           a[j] = p[j];         else           break;       m = j - 1;       sort(a + 1, a + 1 + m, cmp2);       s = las = 0;       for (j = 1; j <= m; ++j) {         if (las != 0)           s += 1ll * (i + a[j].l + i - 1 - las) * (a[j].l - las) / 2;         else           s += 1ll * (a[j].l + i - 2) * (a[j].l + i - 1) / 2;         las = a[j].r - i + 2;         s += 1ll * (a[j].r - a[j].l + 2 - i) * (i - 1);       }       if (m != 0)         s += 1ll * (i + n - las + 1) * (n - a[m].r) / 2;       else         s = 1ll * n * (n + 1) / 2;       ans += 1ll * n * (n + 1) / 2 - s;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 9, B = 511;   inline long long read() {     long long res = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       res = (res << 3) + (res << 1) + ch -  0 ;       ch = getchar();     }     return res * f;   }   struct query {     long long p, id;     query(long long _p = 0, long long _id = 0) { p = _p, id = _id; }   };   long long m, n, tri[N][27], fail[N], cnt = 1, siz[N], ind, dfn[N];   long long len[N], fa[N], ed[N], tr[N], ans[N], a[N];   char s[N];   vector<long long> G[N];   vector<query> L[N], R[N], ll[N], rr[N];   inline bool cmp(query a, query b) { return a.p > b.p; }   inline void insert(long long x) {     long long p = 1;     for (long long i = 1; i <= len[x]; i++) {       long long v = s[i] -  a ;       if (!tri[p][v]) tri[p][v] = ++cnt, fa[tri[p][v]] = p;       p = tri[p][v];     }     ed[x] = p;   }   inline void build_fail() {     queue<long long> Q;     for (long long i = 0; i < 26; i++)       if (tri[1][i])         Q.push(tri[1][i]), fail[tri[1][i]] = 1;       else         tri[1][i] = 1;     while (!Q.empty()) {       long long u = Q.front();       Q.pop();       for (long long i = 0; i < 26; i++)         if (tri[u][i])           Q.push(tri[u][i]), fail[tri[u][i]] = tri[fail[u]][i];         else           tri[u][i] = tri[fail[u]][i];     }   }   void dfs(long long u) {     for (auto v : G[u]) dfs(v), a[u] += a[v];   }   void Dfs(long long u) {     siz[u] = 1;     dfn[u] = ++ind;     for (auto v : G[u]) Dfs(v), siz[u] += siz[v];   }   inline void add(long long x, long long c) {     while (x <= ind) tr[x] += c, x += (x & -x);   }   inline long long ask(long long x) {     long long res = 0;     while (x) res += tr[x], x -= (x & -x);     return res;   }   signed main() {     n = read(), m = read();     for (long long i = 1; i <= n; i++) {       scanf( %s , s + 1);       len[i] = strlen(s + 1);       insert(i);     }     build_fail();     for (long long i = 2; i <= cnt; i++) G[fail[i]].push_back(i);     for (long long i = 1; i <= m; i++) {       long long l, r, k;       l = read(), r = read(), k = read();       if (len[k] > B)         L[k].push_back(query(l, i)), R[k].push_back(query(r, i));       else         ll[l].push_back(query(k, i)), rr[r].push_back(query(k, i));     }     for (long long i = 1; i <= n; i++)       if (L[i].size() || R[i].size()) {         sort(L[i].begin(), L[i].end(), cmp);         sort(R[i].begin(), R[i].end(), cmp);       }     for (long long i = 1; i <= n; i++)       if (len[i] > B) {         long long p = ed[i];         while (p != 1) a[p] = 1, p = fa[p];         dfs(1);         for (long long j = 1, val = 0; j <= n; j++) {           while (L[i].size() && L[i].back().p == j)             ans[L[i].back().id] -= val, L[i].pop_back();           val += a[ed[j]];           while (R[i].size() && R[i].back().p == j)             ans[R[i].back().id] += val, R[i].pop_back();         }         memset(a, 0, sizeof a);       }     Dfs(1);     for (long long i = 1; i <= n; i++) {       for (auto j : ll[i]) {         long long p = ed[j.p];         while (p != 1) ans[j.id] -= ask(dfn[p]), p = fa[p];       }       add(dfn[ed[i]], 1), add(dfn[ed[i]] + siz[ed[i]], -1);       for (auto j : rr[i]) {         long long p = ed[j.p];         while (p != 1) ans[j.id] += ask(dfn[p]), p = fa[p];       }     }     for (long long i = 1; i <= m; i++) printf( %lld n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   const int MAX_MEM = 1e8;   int mpos = 0;   char mem[MAX_MEM];   inline void *operator new(size_t n) {     assert((mpos += n) <= MAX_MEM);     return (void *)(mem + mpos - n);   }   inline void operator delete(void *) noexcept {}   using namespace std;   struct Ex {     int s, d, c, i;   };   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     vector<Ex> data(m + 1);     unordered_map<int, int> table(m + 1);     for (int i = 1; i <= m; i++) {       int s, d, c;       cin >> s >> d >> c;       data[i] = {s, d, c, i};       table[d] = i;     }     vector<int> answer(n + 1);     vector<int> cnt(m + 1);     for (int i = 1; i <= n; i++) {       if (table[i]) {         if (cnt[table[i]] == data[table[i]].c) {           answer[i] = m + 1;         } else {           cout << -1;           return 0;         }       } else {         int best_ex = -1;         int day = 1000;         for (int j = 1; j <= m; j++) {           if (data[j].d < day and cnt[data[j].i] < data[j].c and data[j].s <= i) {             best_ex = data[j].i;             day = data[j].d;           }         }         if (best_ex == -1) {           answer[i] = 0;         } else {           answer[i] = best_ex;           cnt[best_ex]++;         }       }     }     for (int i = 1; i <= n; i++) {       cout << answer[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> sieve(long long n) {     long long *arr = new long long[n + 1]();     vector<long long> vect;     for (long long i = 2; i <= n; i++)       if (arr[i] == 0) {         vect.push_back(i);         for (long long j = 2 * i; j <= n; j += i) arr[j] = 1;       }     return vect;   }   long long binpow(long long a, long long b, long long m) {     a = a % m;     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a % m;       a = a * a % m;       b >>= 1;     }     return res;   }   class cmp {    public:     bool operator()(pair<long long, long long> &a,                     pair<long long, long long> &b) {       if (a.second == b.second)         return a.first < b.first;       else         return a.second < b.second;     }   };   signed main() {     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       vector<long long> v(n);       for (long long i = 0; i < n; i++) cin >> v[i];       long long x = k;       sort(v.begin(), v.end());       long long ans = 0;       k *= 2;       long long j = n - 1;       vector<long long> vec;       while (k--) {         vec.push_back(v[j]);         j--;       }       sort(vec.begin(), vec.end());       for (long long i = 0; i < vec.size() / 2; i++) {         ans += (vec[i] / vec[i + x]);       }       for (long long i = 0; i <= j; i++) ans += v[i];       cout << ans <<   n ;       ;     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:100000000000,100000000000 )   const long long INF = 1000000007;   const double cp = 2 * asin(1.0);   const double eps = 1e-9;   const long long mod = 1000000007;   using namespace std;   int main() {     cin.tie(NULL);     ios_base::sync_with_stdio(false);     string s, ans;     bool ok = false;     cin >> s;     for (char q =  a ; q <=  z ; q++) {       string h;       for (int(i) = 0; (i) < (s.size() + 1); i++) {         h =   ;         for (int(j) = 0; (j) < (i); j++) h += s[j];         h += q;         if (i != s.size())           for (int(j) = (i); (j) <= (s.size() - 1); j++) h += s[j];         string t = h;         reverse((t).begin(), (t).end());         if (t == h) {           return cout << t, 0;           ans = t, ok = true;         }       }     }     if (!ok)       cout <<  NA ;     else       cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000000 + 100, logN = 23;   long long read() {     long long x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = x * 10 + c -  0 ;       c = getchar();     }     return x * f;   }   int f[N][logN + 1];   int query(int l, int r) {     long long ans = 0;     for (int i = logN - 1; i >= 0; --i) {       if (f[l][i] < r) {         l = f[l][i];         ans += (1 << i);       }     }     l = f[l][0];     ans++;     if (l < r) return -1;     return ans;   }   int main() {     int n = read(), m = read(), mx = 0;     for (int i = 1; i <= n; ++i) {       int l = read() + 1, r = read() + 1;       f[l][0] = max(f[l][0], r);       mx = max(mx, r);     }     for (int i = 1; i <= mx; ++i) f[i][0] = max(f[i][0], max(i, f[i - 1][0]));     for (int j = 1; j < logN; ++j)       for (int i = 1; i <= mx; ++i) f[i][j] = f[f[i][j - 1]][j - 1];     while (m--) {       int l = read() + 1, r = read() + 1;       printf( %d n , query(l, r));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long fastModPow(long long a, long long n, long long mod) {     if (mod == 1) return 0;     if (n == 1) return a % mod;     long long res = fastModPow(a, n / 2, mod);     res = res * res % mod;     if (n & 1) res = res * a % mod;     return res;   }   long long fastMaxPow(long long a, long long n, long long mx) {     if (n == 1) return min(a, mx);     long long res = fastMaxPow(a, n / 2, mx);     res = min(res * res, mx);     if (n & 1) res = min(res * a, mx);     return res;   }   int main() {     ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     long long mod = 1000000009;     long long m1 = fastMaxPow(2, m, 100001);     if (n >= m1) {       cout << 0;       return 0;     }     long long mult = fastModPow(2, m, mod);     long long res = 1;     for (int i = 1; i <= n; i++) {       mult--;       if (mult <= 0) mult += mod;       res = res * mult % mod;     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 100 * 1000 + 5;   const int lim = 1000 * 1000 * 1000;   const int inf = 1000 * 1000 * 1000 + 5;   int f[3 * maxN];   vector<pair<int, string> > a[maxN];   string t;   int ans;   void dfs(int v, int F) {     for (auto u : a[v]) {       int tmp = F;       string s = u.second;       for (int i = 0; i < s.size(); i++) {         while (tmp && (tmp == t.size() || t[tmp] != s[i])) tmp = f[tmp - 1];         if (t[tmp] == s[i]) tmp++;         if (tmp == t.size()) ans++;       }       dfs(u.first, tmp);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 1; i < n; i++) {       int par;       string s;       cin >> par >> s;       par--;       a[par].push_back(make_pair(i, s));     }     cin >> t;     int tmp = 0;     for (int i = 1; i < t.size(); i++) {       while (tmp && t[tmp] != t[i]) tmp = f[tmp - 1];       if (t[tmp] == t[i]) tmp++;       f[i] = tmp;     }     dfs(0, 0);     cout << ans << endl;     return 0;   }


//#include <bits/stdc++.h>   //using namespace std;   #pragma GCC optimize(2)   #pragma GCC optimize(3)   #pragma GCC target( avx )   #pragma GCC optimize( Ofast )   #include <cstdio>      #define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)   #define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i)   #define print(x,y) write(x),putchar(y)      template <class T> inline T read(const T sample) {       T x=0; int f=1; char s;       while((s=getchar())> 9 ||s< 0 ) if(s== - ) f=-1;       while(s>= 0 &&s<= 9 ) x=(x<<1)+(x<<3)+(s^48),s=getchar();       return x*f;   }   template <class T> inline void write(const T x) {       if(x<0) return (void) (putchar( - ),write(-x));       if(x>9) write(x/10);       putchar(x%10^48);   }   template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}   template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}      const int maxn=2e4+4;      int n,q,lg[maxn],a[maxn],st[32][18][maxn],dp[18][32][maxn],ans[maxn],f[maxn][32],tmp[32];   struct node {    int l,r,k;   } s[maxn];      void init() {    rep(i,2,n) lg[i]=lg[i>>1]+1;   }      int Query(int k,int l,int r) {    int d=lg[r-l+1];    return Max(st[k][d][l],st[k][d][r-(1<<d)+1]);   }      int main() {    n=read(9),q=read(9); init();    int lgn=lg[n]+1;    rep(i,1,n) a[i]=read(9);    rep(k,0,30) rep(i,1,n) dp[0][k][i]=Min(n,i+a[i]+k);    rep(j,1,lgn) {     rep(k,0,30) rep(i,1,n) st[k][0][i]=dp[j-1][k][i];     rep(k,0,30) rep(i,1,lgn) rep(p,1,n)      if(p+(1<<i)-1<=n)       st[k][i][p]=Max(st[k][i-1][p],st[k][i-1][p+(1<<i-1)]);      else break;     rep(k1,0,30) rep(k2,0,30) {      if(k1+k2>30) break;      rep(i,1,n) dp[j][k1+k2][i]=Max(dp[j][k1+k2][i],Query(k1,i,dp[j-1][k2][i]));     }    }    rep(i,1,q) {     s[i].l=read(9),s[i].r=read(9),s[i].k=read(9);     if(s[i].l==s[i].r) ans[i]=-1;     else rep(j,0,s[i].k) f[i][j]=s[i].l;    }    fep(j,lgn,0) {     rep(k,0,30) rep(i,1,n) st[k][0][i]=dp[j][k][i];     rep(k,0,30) rep(i,1,lgn) rep(p,1,n)      if(p+(1<<i)-1<=n)       st[k][i][p]=Max(st[k][i-1][p],st[k][i-1][p+(1<<i-1)]);      else break;     rep(i,1,q) {      if(ans[i]==-1) continue;      rep(k,0,s[i].k) tmp[k]=0;      rep(k1,0,s[i].k) rep(k2,0,s[i].k) {       if(k1+k2>s[i].k) break;       tmp[k1+k2]=Max(tmp[k1+k2],Query(k1,s[i].l,f[i][k2]));      }      if(tmp[s[i].k]<s[i].r) {       ans[i]+=(1<<j);       rep(k,0,s[i].k) f[i][k]=tmp[k];      }     }    }    rep(i,1,q) print(ans[i]+1,  n );    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, t;   char G[500005];   int solve(int k) {     int last = n, i;     int hs, ss;     hs = ss = 0;     for (i = 1; i <= n; i++) {       if (G[i] ==  H ) hs++;       if (G[i] ==  S ) ss++;       if (G[i] ==  H ) last = i;       if (hs - ss >= k && G[i] !=  . ) last = i;     }     if (hs - ss > k) {       return 0;     }     if (t < last) return 0;     int remT = t - 1;     int pos, val = k, twice, st;     for (pos = 1; pos <= last; pos++) {       if (remT < 0) return 0;       if (G[pos] ==  H ) {         if (val > 0)           val--;         else {           twice = (last - pos) << 1;           if (twice <= remT) {             return 1;           } else {             st = pos;             ss = hs = 0;             for (; pos <= last; pos++) {               if (G[pos] ==  H )                 hs++;               else if (G[pos] ==  S )                 ss++;               if (hs <= ss) break;             }             if (pos > last) {               return 0;             }             twice = (pos - st) * 3;             remT -= twice;             if (remT < 0) return 0;           }         }       } else if (G[pos] ==  S )         val++;       remT--;     }     return 1;   }   int main() {     scanf( %d%d , &n, &t);     scanf( %s , G + 1);     int high = n + 1;     int low = 0;     int mid;     while (low < high) {       mid = (low + high) / 2;       if (solve(mid)) {         high = mid;       } else {         low = mid + 1;       }     }     if (high <= n)       printf( %d , high);     else       printf( -1 );     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   template <class T, size_t N>   ostream& operator<<(ostream& os, const array<T, N>& v);   template <class T1, class T2>   ostream& operator<<(ostream& os, const pair<T1, T2>& p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <class T>   ostream& operator<<(ostream& os, const vector<T>& v) {     bool f = 1;     os <<  [ ;     for (auto& i : v) {       if (!f) os <<  ,  ;       os << i;       f = 0;     }     return os <<  ] ;   }   template <class T, size_t N>   ostream& operator<<(ostream& os, const array<T, N>& v) {     bool f = 1;     os <<  [ ;     for (auto& i : v) {       if (!f) os <<  ,  ;       os << i;       f = 0;     }     return os <<  ] ;   }   template <class T1, class T2>   ostream& operator<<(ostream& os, const set<T1, T2>& v) {     bool f = 1;     os <<  [ ;     for (auto& i : v) {       if (!f) os <<  ,  ;       os << i;       f = 0;     }     return os <<  ] ;   }   template <class T1, class T2>   ostream& operator<<(ostream& os, const multiset<T1, T2>& v) {     bool f = 1;     os <<  [ ;     for (auto& i : v) {       if (!f) os <<  ,  ;       os << i;       f = 0;     }     return os <<  ] ;   }   template <class T1, class T2, class T3>   ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) {     bool f = 1;     os <<  [ ;     for (auto& ii : v) {       if (!f) os <<  ,  ;       os <<  (  << ii.first <<   ->   << ii.second <<  )  ;       f = 0;     }     return os <<  ] ;   }   template <class T1, class T2>   ostream& operator<<(ostream& os, const multimap<T1, T2>& v) {     bool f = 1;     os <<  [ ;     for (auto& ii : v) {       if (!f) os <<  ,  ;       os <<  (  << ii.first <<   ->   << ii.second <<  )  ;       f = 0;     }     return os <<  ] ;   }   template <class T1, class T2>   ostream& operator<<(ostream& os, priority_queue<T1, T2> v) {     bool f = 1;     os <<  [ ;     while (!v.empty()) {       auto x = v.top();       v.pop();       if (!f) os <<  ,  ;       f = 0;       os << x;     }     return os <<  ] ;   }   template <class T1, class T2>   void printarray(const T1& a, T2 sz, T2 beg = 0) {     for (T2 i = beg; i < sz; i++) cout << a[i] <<    ;     cout << endl;   }   void cio() {     ios::sync_with_stdio(false);     cin.tie(NULL);   }   const long long mod = 1e9 + 9;   void die() {     printf( -1 n );     exit(0);   }   int N;   int p[500];   void Do(int R) {     for (int i = 1; i <= N; i++)       if (p[i] < R) return;     bool odd = false;     int sum = 0;     for (int i = 1; i <= N; i++) sum += p[i] - R;     if (sum % 2) odd = true;     int X;     vector<pair<int, int> > ansv;     set<pair<int, int> > S;     for (int i = 1; i <= N; i++) S.insert({p[i] - R, i});     if (odd) {       auto it = S.end();       it--;       pair<int, int> pp = *it;       X = pp.second;       S.erase(it);       S.insert({pp.first - 1, pp.second});       sum--;     }     if (S.rbegin()->first * 2 > sum) return;     while (true) {       auto it1 = S.end();       it1--;       S.erase(it1);       auto it2 = S.end();       it2--;       S.erase(it2);       pair<int, int> p1 = *it1, p2 = *it2;       if (p1.first == 0 || p2.first == 0) break;       ansv.push_back({p1.second, p2.second});       S.insert({p1.first - 1, p1.second});       S.insert({p2.first - 1, p2.second});     }     if (odd && (int)ansv.size() == 0) return;     if (odd) {       bool flag = false;       for (auto e : ansv) {         if (e.first != X && e.second != X) flag = true;       }       if (!flag) return;     }     printf( %d n , R);     printf( %d n , (int)ansv.size());     for (auto e : ansv) {       for (int i = 1; i <= N; i++) {         if (e.first == i || e.second == i)           printf( 1 );         else if (odd && (e.first != X && e.second != X) && i == X) {           odd = false;           printf( 1 );         } else           printf( 0 );       }       printf(  n );     }     exit(0);   }   int main() {     scanf( %d , &N);     for (int i = 1; i <= N; i++) scanf( %d , &p[i]);     for (int x = 100; x >= 1; x--) {       Do(x);     }     printf( 0 n );     vector<pair<int, int> > ansv;     while (p[1] > 0 && p[2] > 0) {       p[1]--;       p[2]--;       ansv.push_back({1, 2});     }     int X = 1;     if (p[2] == 0) X = 2;     for (int i = 1; i <= N; i++)       if (i != X) {         while (p[i] > 0) {           p[i]--;           ansv.push_back({X, i});         }       }     printf( %d n , (int)ansv.size());     for (auto e : ansv) {       for (int i = 1; i <= N; i++) {         if (e.first == i || e.second == i)           printf( 1 );         else           printf( 0 );       }       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 0x3f3f3f3f;   const double eps = 1e-6;   const long long mod = 998244353;   inline string getstr(string &s, long long l, long long r) {     string ret =   ;     for (long long i = l; i <= r; i++) ret.push_back(s[i]);     return ret;   }   long long modpow(long long x, long long y, long long md = mod) {     if (y == 0) return 1;     long long ret = modpow(x, y >> 1, md);     ret = (long long)ret * ret % md;     if (y & 1) ret = (long long)ret * x % md;     return ret;   }   string s;   vector<pair<long long, long long> > g[2005];   long long value[2005];   long long LEN = 0;   bool error = 0;   void add_edge(long long x, long long y, long long cost) {     g[x].push_back(make_pair(y, cost));     g[y].push_back(make_pair(x, cost));   }   void dfs(long long x) {     if (error) return;     for (auto p : g[x]) {       if (~value[p.first] && value[p.first] != value[x] ^ p.second) error = 1;       if (error) return;       if (~value[p.first]) continue;       value[p.first] = value[x] ^ p.second;       dfs(p.first);     }   }   long long calc(long long len) {     LEN = len;     memset(value, -1, sizeof value);     long long Maxa = (len) / 2;     long long Maxb = ((long long)s.size() - 1) / 2;     swap(Maxa, Maxb);     for (long long i = 0; i <= 2002; i++) g[i].clear();     value[0] = 1;     value[1001] = 1;     for (long long i = 0; i < s.size(); i++) {       if (s[i] ==  ? ) continue;       long long posa = (i <= Maxa ? i : ((long long)s.size() - i - 1));       long long posb = (i <= Maxb ? i : (len - i));       if (i > len) {         if (~value[posa] && value[posa] != s[i] -  0 ) return 0;         value[posa] = s[i] -  0 ;         continue;       }       add_edge(posa, posb + 1001, s[i] -  0 );     }     long long ret = 1;     error = 0;     for (long long i = 1; i <= 2002; i++) {       if (~value[i]) dfs(i);     }     for (long long i = 1; i <= 2002; i++) {       if (((0 <= i && i <= Maxa) || (1001 <= i && i <= 1001 + Maxb)) &&           value[i] == -1) {         value[i] = 0;         (ret *= 2) %= mod;         dfs(i);       }       if (error) return 0;     }     return ret;   }   signed main() {     cin >> s;     reverse(s.begin(), s.end());     long long ans = 0;     for (long long len = 0; len < s.size(); len++) {       (ans += calc(len)) %= mod;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y;     cin >> x >> y;     int l = 0, r = 0, u = 0, d = 0;     int tx = 0, ty = 0, dir = 0, px = 0, py = 0, k = 0;     while (true) {       px = tx;       py = ty;       if (dir == 0) {         r = tx = r + 1;         dir = 1;       } else if (dir == 1) {         u = ty = u + 1;         dir = 2;       } else if (dir == 2) {         l = tx = l - 1;         dir = 3;       } else if (dir == 3) {         d = ty = d - 1;         dir = 0;       }       int rx1 = min(tx, px), ry1 = min(ty, py);       int rx2 = max(tx, px), ry2 = max(ty, py);       if ((x >= rx1 && x <= rx2) && (y >= ry1 && y <= ry2)) {         cout << k;         return 0;       }       k++;     }   }


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


#include <bits/stdc++.h>   using namespace std;   void read(int &a) {     a = 0;     char c = getchar();     while (c <  0  || c >  9 ) {       c = getchar();     }     while (c >=  0  && c <=  9 ) {       a = (a << 1) + (a << 3) + (c ^ 48);       c = getchar();     }   }   const int Maxn = 200000;   struct Segment {     int l, r, t;     friend bool operator<(Segment p, Segment q) {       if (p.r == q.r) {         return p.l < q.l;       }       return p.r < q.r;     }   } a[Maxn + 5];   int f[Maxn + 5];   int n;   int d[Maxn << 1 | 5], d_len;   struct Segment_Node {     int lson, rson;     int val, lazy;   } seg[Maxn << 4 | 5];   int Root[2];   int id_tot;   void insert(int l, int r, int x, int &root, int left = 1, int right = d_len) {     if (l > right || r < left) {       return;     }     if (root == 0) {       root = ++id_tot;     }     if (l <= left && r >= right) {       seg[root].val += x;       seg[root].lazy += x;       return;     }     int mid = (left + right) >> 1;     insert(l, r, x, seg[root].lson, left, mid);     insert(l, r, x, seg[root].rson, mid + 1, right);     seg[root].val =         max(seg[seg[root].lson].val, seg[seg[root].rson].val) + seg[root].lazy;   }   int query(int l, int r, int root, int left = 1, int right = d_len) {     if (l > right || r < left) {       return 0;     }     if (root == 0) {       return 0;     }     if (l <= left && r >= right) {       return seg[root].val;     }     int mid = (left + right) >> 1;     return max(query(l, r, seg[root].lson, left, mid),                query(l, r, seg[root].rson, mid + 1, right)) +            seg[root].lazy;   }   void update(int x, int a, int &root, int left = 1, int right = d_len) {     if (root == 0) {       root = ++id_tot;     }     a -= seg[root].lazy;     if (left == right) {       seg[root].val = max(seg[root].val, a);       return;     }     int mid = (left + right) >> 1;     if (x <= mid) {       update(x, a, seg[root].lson, left, mid);     } else {       update(x, a, seg[root].rson, mid + 1, right);     }     seg[root].val =         max(seg[seg[root].lson].val, seg[seg[root].rson].val) + seg[root].lazy;   }   int main() {     read(n);     for (int i = 1; i <= n; i++) {       read(a[i].l), read(a[i].r), read(a[i].t);       a[i].t--;       d[++d_len] = a[i].l;       d[++d_len] = a[i].r;     }     sort(d + 1, d + 1 + d_len);     d_len = unique(d + 1, d + 1 + d_len) - d - 1;     for (int i = 1; i <= n; i++) {       a[i].l = lower_bound(d + 1, d + 1 + d_len, a[i].l) - d + 1;       a[i].r = lower_bound(d + 1, d + 1 + d_len, a[i].r) - d + 1;     }     d_len++;     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n; i++) {       f[i] = query(1, a[i].l - 1, Root[a[i].t]) + 1;       insert(1, a[i].l - 1, 1, Root[a[i].t]);       update(a[i].r, f[i], Root[a[i].t ^ 1]);     }     int ans = 0;     for (int i = 1; i <= n; i++) {       ans = max(ans, f[i]);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int problem, time;     cin >> problem >> time;     int sum = 240;     int j = 0;     for (int i = 1; i <= problem; ++i) {       sum -= i * 5;       if (sum < time) {         break;       }       j++;     }     cout << j;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100 + 20;   const int LG = 15;   int a[N], cnt[N];   map<vector<int>, int> mp;   int main() {     ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       cnt[i] = __builtin_popcount(a[i]);     }     for (int mask = 0; mask < (1 << LG); mask++) {       vector<int> vec(n + 20, 0);       for (int i = 0; i < n; i++) {         for (int j = 0; j < LG; j++) {           if (!((mask >> j) & 1)) continue;           vec[i] += (((a[i] >> j) & 1) ? -1 : 1);         }         vec[i] += cnt[i];       }       mp[vec] = mask;     }     for (int mask = 0; mask < (1 << 30); mask += (1 << LG)) {       vector<int> vec(n + 20, 0);       for (int i = 0; i < n; i++) {         for (int j = LG; j < 30; j++) {           if (!((mask >> j) & 1)) continue;           vec[i] += (((a[i] >> j) & 1) ? -1 : 1);         }         vec[i] *= -1;       }       for (int i = 0; i <= 30; i++) {         auto it = mp.find(vec);         if (it != mp.end()) return cout << (mask | it->second) << endl, 0;         for (int j = 0; j < n; j++) vec[j]++;       }     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 7, maxn = 3e5 + 500;   vector<pair<int, int> > v[maxn];   bool visited[maxn], deg[maxn];   int d[maxn];   set<int> s;   void dfs(int u, int par) {     visited[u] = 1;     int idx;     for (int i = 0; i < v[u].size(); i++) {       int w = v[u][i].first;       if (!visited[w]) dfs(w, u);       if (w == par) idx = v[u][i].second;     }     if (deg[u] != d[u]) {       deg[par] ^= 1;       s.insert(idx);     }   }   int main() {     ios_base::sync_with_stdio(false);     int n, m, cntt = 0, cnt = 0;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> d[i];       if (d[i] == 1) cnt++;       if (d[i] == -1) cntt++;     }     for (int i = 1; i <= m; i++) {       int u, w;       cin >> u >> w;       v[u].push_back(make_pair(w, i));       v[w].push_back(make_pair(u, i));     }     if (cnt % 2 == 1 and cntt == 0) return cout << -1 << endl, 0;     if (cnt % 2 == 1) {       for (int i = 1; i <= n; i++) {         if (d[i] == -1) {           d[i] = 1;           break;         }       }     }     for (int i = 1; i <= n; i++)       if (d[i] == -1) d[i] = 0;     dfs(1, -1);     cout << s.size() << endl;     for (auto it : s) cout << it <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int x[100010];   int main() {     int n, a;     cin >> n >> a;     if (n == 1) {       cout << 0 << endl;       return 0;     }     for (int i = 0; i < n; ++i) {       cin >> x[i];     }     sort(x, x + n);     int l = 1, r = n - 1;     int ans = 0;     ans = min(abs(a - x[r]) + x[r] - x[l], abs(a - x[l]) + x[r] - x[l]);     l = 0, r = n - 2;     ans = min(ans, min(abs(a - x[r]) + x[r] - x[l], abs(a - x[l]) + x[r] - x[l]));     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, op, i, index = 0;     cin >> n >> k;     char ss[n - 1];     op = k;     i = 97;     while (op--) {       ss[index] = char(i);       i++;       index++;     }     if (n - k != 0) {       for (int d = 0; d < n - k; d++) {         ss[index] = ss[d];         index++;       }     }     for (int c = 0; c < n; c++) cout << ss[c];   }


#include <bits/stdc++.h>   using namespace std;   inline int in() {     int x;     scanf( %d , &x);     return x;   }   inline void priv(vector<int> a) {     for (int i = 0; i < ((int)(a).size()); ++i)       printf( %d%c , a[i], i == (int)(a).size() - 1 ?   n  :    );   }   template <typename T>   istream& operator>>(istream& i, vector<T>& v) {     for (int j = 0; j < ((int)(v).size()); ++j) i >> v[j];     return i;   }   template <typename T>   string join(vector<T>& v) {     stringstream s;     for (int i = 0; i < ((int)(v).size()); ++i) s <<     << v[i];     return s.str().substr(1);   }   template <typename T>   ostream& operator<<(ostream& o, vector<T>& v) {     if ((int)(v).size()) o << join(v);     return o;   }   template <typename T1, typename T2>   istream& operator>>(istream& i, pair<T1, T2>& v) {     return i >> v.first >> v.second;   }   template <typename T1, typename T2>   ostream& operator<<(ostream& o, pair<T1, T2>& v) {     return o << v.first <<  ,  << v.second;   }   const int MX = 100005, INF = 1001001001;   const long long int LINF = 1e18;   const double eps = 1e-10;   struct Maxflow {     int n;     vector<int> to, lim, next, head, dist, it;     Maxflow() {}     Maxflow(int n) : n(n), head(n, -1), it(n) {}     void add(int a, int b, int c = 1) {       next.push_back(head[a]);       head[a] = to.size();       to.push_back(b);       lim.push_back(c);       next.push_back(head[b]);       head[b] = to.size();       to.push_back(a);       lim.push_back(0);     }     void bfs(int sv) {       dist = vector<int>(n, INF);       queue<int> q;       dist[sv] = 0;       q.push(sv);       while (!q.empty()) {         int v = q.front();         q.pop();         for (int i = head[v]; i != -1; i = next[i]) {           if (lim[i] && dist[to[i]] == INF) {             dist[to[i]] = dist[v] + 1;             q.push(to[i]);           }         }       }     }     int dfs(int v, int tv, int nf = INF) {       if (v == tv) return nf;       for (; it[v] != -1; it[v] = next[it[v]]) {         int u = to[it[v]], f;         if (!lim[it[v]] || dist[v] >= dist[u]) continue;         if (f = dfs(u, tv, min(nf, lim[it[v]])), f) {           lim[it[v]] -= f;           lim[it[v] ^ 1] += f;           return f;         }       }       return 0;     }     int solve(int sv, int tv) {       int flow = 0, f;       while (1) {         bfs(sv);         if (dist[tv] == INF) return flow;         for (int i = 0; i < (n); ++i) it[i] = head[i];         while (f = dfs(sv, tv), f) flow += f;       }     }   };   int n, m;   vector<vector<int> > to, co;   vector<int> used, vs, es;   vector<vector<int> > g;   void dfs(int v, int p = -1) {     used[v] = 1;     for (int i = 0; i < ((int)(to[v]).size()); ++i) {       int u = to[v][i];       if (u == p) continue;       if (used[u]) {         if (used[u] == 1) {           g.push_back(vector<int>(1, co[v][i]));           for (int i = (int)(vs).size() - 1;; --i) {             g.back().push_back(es[i]);             es[i] = -1;             if (vs[i] == u) break;           }         }       } else {         vs.push_back(v);         es.push_back(co[v][i]);         dfs(u, v);         if (es.back() != -1) g.push_back(vector<int>(1, es.back()));         vs.pop_back();         es.pop_back();       }     }     used[v] = 2;   }   int main() {     scanf( %d%d , &n, &m);     to = co = vector<vector<int> >(n);     for (int i = 0; i < (m); ++i) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       --a;       --b;       --c;       to[a].push_back(b);       co[a].push_back(c);       to[b].push_back(a);       co[b].push_back(c);     }     used = vector<int>(n);     dfs(0);     int l = (int)(g).size(), sv = m + l, tv = sv + 1;     Maxflow mf(tv + 1);     for (int i = 0; i < (m); ++i) mf.add(sv, i);     for (int i = 0; i < (l); ++i)       mf.add(m + i, tv, max(1, (int)(g[i]).size() - 1));     for (int i = 0; i < (l); ++i) {       for (int j : g[i]) mf.add(j, m + i);     }     cout << mf.solve(sv, tv) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 3;   int n;   int main() {     cin >> n;     cout << (int)log2(n) + 1;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, m, n, rstart, rend, cstart, cend;     scanf( %d %d %d %d , &n, &m, &a, &b);     if ((m >= n) || (m == 1))       printf( 1 n );     else {       rstart = (a / m) + 1;       if ((a % m) == 0) --rstart;       cstart = a - ((rstart - 1) * m);       rend = (b / m) + 1;       if ((b % m) == 0) --rend;       cend = b - ((rend - 1) * m);       if (((cstart == 1) && ((cend == m) || (b == n))) || (rstart == rend) ||           ((a == 1) && (b == n)))         printf( 1 n );       else {         if (((cstart != 1) && ((cend == m) || (b == n))) ||             ((cstart == 1) && (cend != m)))           printf( 2 n );         else if ((rend - rstart) == 1)           printf( 2 n );         else if ((cstart - cend) == 1)           printf( 2 n );         else           printf( 3 n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, ans, fa[100001];   bool mark[100001];   int find(int x) {     if (x == fa[x]) return x;     return fa[x] = find(fa[x]);   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       int fx = find(x), fy = find(y);       if (fx != fy) {         fa[fx] = fy;         if (mark[fx] || mark[x] || mark[fy] || mark[y])           mark[fx] = mark[x] = mark[fy] = mark[y] = 1;       } else         mark[fx] = mark[x] = mark[fy] = mark[y] = 1;     }     for (int i = 1; i <= n; i++)       if (find(i) == i && !mark[i]) ans++;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using uint = unsigned int;   template <class T>   using V = vector<T>;   template <class T>   using VV = vector<vector<T>>;   template <class T, class U>   void chmax(T& x, U y) {     if (x < y) x = y;   }   template <class T, class U>   void chmin(T& x, U y) {     if (y < x) x = y;   }   template <class T>   void mkuni(V<T>& v) {     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());   }   template <class S, class T>   ostream& operator<<(ostream& o, const pair<S, T>& p) {     return o <<  (  << p.first <<  ,  << p.second <<  ) ;   }   template <class T>   ostream& operator<<(ostream& o, const vector<T>& vc) {     o <<  { ;     for (const T& v : vc) o << v <<  , ;     o <<  } ;     return o;   }   constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }   unsigned int mod = 1;   struct ModInt {     using uint = unsigned int;     using ll = long long;     using ull = unsigned long long;     uint v;     ModInt() : v(0) {}     ModInt(ll _v) : v(normS(_v % mod + mod)) {}     explicit operator bool() const { return v != 0; }     static uint normS(const uint& x) { return (x < mod) ? x : x - mod; }     static ModInt make(const uint& x) {       ModInt m;       m.v = x;       return m;     }     ModInt operator+(const ModInt& b) const { return make(normS(v + b.v)); }     ModInt operator-(const ModInt& b) const { return make(normS(v + mod - b.v)); }     ModInt operator-() const { return make(normS(mod - v)); }     ModInt operator*(const ModInt& b) const { return make((ull)v * b.v % mod); }     ModInt operator/(const ModInt& b) const { return *this * b.inv(); }     ModInt& operator+=(const ModInt& b) { return *this = *this + b; }     ModInt& operator-=(const ModInt& b) { return *this = *this - b; }     ModInt& operator*=(const ModInt& b) { return *this = *this * b; }     ModInt& operator/=(const ModInt& b) { return *this = *this / b; }     ModInt& operator++(int) { return *this = *this + 1; }     ModInt& operator--(int) { return *this = *this - 1; }     ll extgcd(ll a, ll b, ll& x, ll& y) const {       ll p[] = {a, 1, 0}, q[] = {b, 0, 1};       while (*q) {         ll t = *p / *q;         for (int i = 0; i < int(3); i++) swap(p[i] -= t * q[i], q[i]);       }       if (p[0] < 0)         for (int i = 0; i < int(3); i++) p[i] = -p[i];       x = p[1], y = p[2];       return p[0];     }     ModInt inv() const {       ll x, y;       extgcd(v, mod, x, y);       return make(normS(x + mod));     }     ModInt pow(ll p) const {       if (p < 0) return inv().pow(-p);       ModInt a = 1;       ModInt x = *this;       while (p) {         if (p & 1) a *= x;         x *= x;         p >>= 1;       }       return a;     }     bool operator==(const ModInt& b) const { return v == b.v; }     bool operator!=(const ModInt& b) const { return v != b.v; }     friend istream& operator>>(istream& o, ModInt& x) {       ll tmp;       o >> tmp;       x = ModInt(tmp);       return o;     }     friend ostream& operator<<(ostream& o, const ModInt& x) { return o << x.v; }   };   using mint = ModInt;   V<mint> fact, ifact, invs;   mint Choose(int a, int b) {     if (b < 0 || a < b) return 0;     return fact[a] * ifact[b] * ifact[a - b];   }   void InitFact(int N) {     N++;     fact.resize(N);     ifact.resize(N);     invs.resize(N);     fact[0] = 1;     for (int i = 1; i <= int(N - 1); i++) fact[i] = fact[i - 1] * i;     ifact[N - 1] = fact[N - 1].inv();     for (int i = N - 2; i >= 0; i--) ifact[i] = ifact[i + 1] * (i + 1);     for (int i = 1; i <= int(N - 1); i++) invs[i] = fact[i - 1] * ifact[i];   }   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     cout << fixed << setprecision(20);     int N, M;     cin >> N >> M >> mod;     InitFact(100000);     V<mint> way(N + 1), sum(N + 1);     way[0] = 1;     for (int n = 1; n <= int(N); n++) {       for (int i = 0; i < int(n); i++) {         way[n] += way[i] * way[n - 1 - i] * Choose(n - 1, i) * (n + 1);         sum[n] += sum[i] * way[n - 1 - i] * Choose(n - 1, i) * (n + 1);         sum[n] += way[i] * sum[n - 1 - i] * Choose(n - 1, i) * (n + 1);         auto f = [&](int x) { return x * (x + 1) / 2; };         sum[n] +=             way[i] * way[n - 1 - i] * Choose(n - 1, i) * (f(i) + f(n - 1 - i));       }     }     VV<mint> f(N + 2, V<mint>(N + 1));     f[0][0] = 1;     VV<mint> g(N + 2, V<mint>(N + 1));     for (int n = 0; n < int(N + 1); n++)       for (int k = 0; k < int(n + 1); k++) {         for (int dn = 1; dn <= int(N); dn++) {           if (n + dn > N || k + dn > M) break;           f[n + dn + 1][k + dn] += f[n][k] * way[dn] * Choose(k + dn, k);           g[n + dn + 1][k + dn] +=               (f[n][k] * sum[dn] + g[n][k] * way[dn]) * Choose(k + dn, k);         }         f[n + 1][k] += f[n][k];         g[n + 1][k] += g[n][k];       }     cout << g[N + 1][M] << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5;   const int INF = 1e9;   long long MOD = 998244353;   long long sum(long long n) {     long long sm = 0;     while (n) {       sm += n % 10;       n /= 10;     }     return sm;   }   int main() {     list<int> a;     list<int> b;     long long n;     cin >> n;     int ans = 0;     vector<long long> res;     for (long long i = max(n - 100, 0LL); i <= n; i++) {       if (sum(i) + i == n) {         ans++;         res.push_back(i);       }     }     cout << ans <<   n ;     for (int i = 0; i < res.size(); i++) {       cout << res[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[514];   long long int dp[514][514];   long long int mod = 1000000007ll;   long long int mdp(int s, int e) {     if (s == e) return 1;     if (dp[s][e] != -1) return dp[s][e];     long long int& ans = dp[s][e];     if (s + 1 == e) return ans = 1;     ans = mdp(s + 1, e);     for (int i = s + 1; i < e; i++) {       if (a[s] < a[i]) {         ans += mdp(s + 1, i) * mdp(i, e) % mod;       }     }     return ans = ans % mod;   }   int sol() {     memset(dp, -1, sizeof(dp));     int n;     if (scanf( %d , &n) == EOF) return 0;     for (int i = 0; i < n; i++) {       scanf( %d , a + i);     }     printf( %I64d n , n == 1 ? 1 : mdp(1, n));     return 1;   }   int main() {     while (sol())       ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, a, b, Q;   long long W[(500000)], S[(500000)], O[(500000)];   map<pair<int, int>, vector<pair<int, int> > > M;   void pro(int t, int b, long long* g, int& l) {     *g = 0, l = 1;     while (b < N) {       long long S = W[b] + g[l - 1];       g[l++] = S, b += t;     }   }   int main() {     scanf( %d , &N);     for (int i(0); i < N; i++) scanf( %lld , W + i);     scanf( %d , &Q);     for (int i(0); i < Q; i++)       scanf( %d%d , &a, &b), --a, M[{b, a % b}].push_back({a, i});     for (auto& h : M) {       pro(h.first.first, h.first.second, S, a);       for (auto& w : h.second)         O[w.second] = S[a - 1] - S[w.first / h.first.first];     }     for (int i(0); i < Q; i++) printf( %lld n , O[i]);   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   int chkmax(T& a, T b) {     if (b > a) {       a = b;       return 1;     }     return 0;   }   template <class T>   int chkmin(T& a, T b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   template <class iterator>   void output(iterator begin, iterator end, ostream& out = cerr) {     while (begin != end) {       out << (*begin) <<    ;       begin++;     }     out <<   n ;   }   template <class T>   void output(T x, ostream& out = cerr) {     output(x.begin(), x.end(), out);   }   const int MOD = 1e9 + 7;   const int INF = 1e9;   int n, flag, ans = 1;   set<int> buy, sell, middle;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int max_buy = -INF, min_sell = INF;     cin >> n;     for (int i = 0; i < n; ++i) {       int x;       string s;       cin >> s >> x;       if (flag) continue;       if (s ==  ADD ) {         if (x <= max_buy) buy.insert(x);         if (x >= min_sell) sell.insert(x);         if (x > max_buy && x < min_sell) middle.insert(x);       } else {         if (x < max_buy || x > min_sell) {           cout << 0 <<   n ;           flag = 1;           continue;         }         if (x > max_buy && x < min_sell) ans = (ans + ans) % MOD;         for (auto val : middle) {           if (val < x)             buy.insert(val);           else             sell.insert(val);         }         middle.clear();         if (buy.find(x) != buy.end())           buy.erase(x);         else           sell.erase(x);         max_buy = -INF;         min_sell = INF;         if (!buy.empty()) max_buy = (*prev(buy.end()));         if (!sell.empty()) min_sell = (*sell.begin());       }     }     if (flag) return 0;     ans = ((long long)ans * (long long)(middle.size() + 1)) % (long long)MOD;     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d;     cin >> d >> n;     long long int ilosc = 0;     for (int i = 0; i < n - 1; i++) {       int p;       cin >> p;       ilosc += (d - p);     }     cin >> d;     cout << ilosc <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long fastpow(long long base, long long power, long long M) {     if (power == 1) return base;     long long val = fastpow(base, power / 2, M);     return (power % 2 == 0) ? (val * val) % M : (((val * val) % M) * base) % M;   }   inline void debugMode() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   const int N = 1e5 + 1;   const int MOD = 1e9 + 7;   int main() {     debugMode();     int s, b;     cin >> s >> b;     int arr[s];     for (int i = 0; i < s; i++) cin >> arr[i];     vector<pair<int, int>> base(b);     for (int i = 0; i < b; i++) cin >> base[i].first >> base[i].second;     sort((base).begin(), (base).end());     for (int i = 1; i < b; i++) base[i].second += base[i - 1].second;     for (int i = 0; i < s; i++) {       int left = -1, right = b;       while (left + 1 < right) {         int mid = (left + right) / 2;         if (base[mid].first <= arr[i])           left = mid;         else           right = mid;       }       if (left == -1)         cout << 0 <<    ;       else         cout << base[left].second <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void cmax(T& a, T b) {     a = max(a, b);   }   template <typename T>   void cmin(T& a, T b) {     a = min(a, b);   }   void _BG(const char* s) {}   template <typename T, typename... TT>   void _BG(const char* s, T a, TT... b) {     for (int c = 0; *s && (c || *s !=  , ); ++s) {       cerr << *s;       switch (*s) {         case  ( :         case  [ :         case  { :           ++c;           break;         case  ) :         case  ] :         case  } :           --c;           break;       }     }     cerr <<   =   << a;     if (*s) {       cerr <<  ,  ;       _BG(++s, b...);     } else       cerr << endl;   }   bool RD(void) { return true; }   bool RD(char& a) { return scanf(  %c , &a) == 1; }   bool RD(char* a) { return scanf( %s , a) == 1; }   bool RD(double& a) { return scanf( %lf , &a) == 1; }   bool RD(int& a) { return scanf( %d , &a) == 1; }   bool RD(long long& a) { return scanf( %lld , &a) == 1; }   template <typename T, typename... TT>   bool RD(T& a, TT&... b) {     return RD(a) && RD(b...);   }   void PT(const char a) { putchar(a); }   void PT(const char* a) { fputs(a, stdout); }   void PT(char* a) { fputs(a, stdout); }   void PT(const double a) { printf( %.16f , a); }   void PT(const int a) { printf( %d , a); }   void PT(const long long a) { printf( %lld , a); }   template <char sep =    , char end =   n >   void PL(void) {     if (end) PT(end);   }   template <char sep =    , char end =   n , typename T, typename... TT>   void PL(const T a, const TT... b) {     PT(a);     if (sizeof...(b) && sep) PT(sep);     PL<sep, end>(b...);   }   const long long inf = 4e10;   const int N = 106;   int n, r, a[N];   pair<int, int> p[N];   long long d[N];   bool used[N];   long long dis(int i, int j) {     return r * (abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second)) -            a[i];   }   int main() {     RD(n, r);     for (int i(2), _B((n - 1) + 1); i < _B; ++i) RD(a[i]);     for (int i(1), _B((n) + 1); i < _B; ++i) RD(p[i].first, p[i].second);     fill_n(d, n + 1, inf);     d[1] = 0;     for (int u = 1, v; u != n; u = v) {       used[u] = 1;       v = 0;       for (int i(1), _B((n) + 1); i < _B; ++i)         if (!used[i]) {           cmin(d[i], d[u] + dis(u, i));           if (d[i] < d[v]) v = i;         }     }     PL(d[n]);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1003;   int n, k;   int pos[5][maxn];   int dp[maxn];   bool vis[maxn];   vector<int> v[maxn];   int dfs(int cur) {     if (vis[cur]) return dp[cur];     vis[cur] = true;     for (int i = 0; i < v[cur].size(); i++) {       int nxt = v[cur][i];       dp[cur] = max(dp[cur], 1 + dfs(nxt));     }     return dp[cur];   }   int main() {     cin >> n >> k;     for (__typeof(k) i = 0; i < (k); i++) {       for (__typeof(n) j = 0; j < (n); j++) {         int x;         cin >> x;         pos[i][--x] = j;       }     }     for (__typeof(n) i = 0; i < (n); i++) {       for (__typeof(n) j = 0; j < (n); j++) {         bool flag = 1;         for (__typeof(k) a = 0; a < (k); a++) {           if (pos[a][i] <= pos[a][j]) {             flag = 0;             break;           }         }         if (flag) v[i].push_back(j);       }     }     for (__typeof(n) i = 0; i < (n); i++) dfs(i);     cout << *max_element(dp, dp + n) + 1;   }


#include <bits/stdc++.h>   using namespace std;   string imp =  Impossible n ;   const int N = 2e5 + 5;   int bfstim[N];   long long MOD = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int tests;     tests = 1;     cin >> tests;     while (tests--) {       int n;       cin >> n;       if (n > 2) {         cout << n - 2 <<   n ;       } else         cout <<  0 n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int UNDEF = -1;   const long long INF = 1e18;   template <typename T>   inline bool chkmax(T &aa, T bb) {     return aa < bb ? aa = bb, true : false;   }   template <typename T>   inline bool chkmin(T &aa, T bb) {     return aa > bb ? aa = bb, true : false;   }   int rint();   char rch();   long long rlong();   void makeunique(vector<int> &vx) {     sort(vx.begin(), vx.end());     auto it = unique(vx.begin(), vx.end());     vx.resize(std::distance(vx.begin(), it));   }   int bins(vector<int> &vx, int x) {     int imin = 0, imax = vx.size();     while (imin < imax) {       int imid = (imin + imax) >> 1;       if (vx[imid] < x)         imin = imid + 1;       else         imax = imid;     }     return imin;   }   const int mn = 2e7 + 2;   int f[mn];   int longest_strictly_increasing_subsequence(vector<int> &p, int maxval,                                               int repeats) {     memset(f, 0, sizeof(int) * (maxval + 1));     for (int r = 0; r < repeats; r++) {       for (auto &x : p) {         int best = f[x - 1] + 1;         for (int y = x; y <= maxval; y++) {           if (f[y] < best)             f[y] = best;           else             break;         }       }     }     return f[maxval];   }   int compress(vector<int> &vinp) {     vector<int> bintable = vinp;     makeunique(bintable);     for (auto &w : vinp) {       w = bins(bintable, w) + 1;     }     return (int)bintable.size();   }   int n, t;   vector<int> b;   void solve() {     for (int i = 0; i < n; i++) b[i] = rint();     int maxval = compress(b);     int ans = longest_strictly_increasing_subsequence(b, maxval, min(t, maxval));     printf( %d n , ans);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int numtests, maxb;     numtests = rint(), n = rint(), maxb = rint(), t = rint();     b.resize(n);     for (int test = 0; test < numtests; test++) solve();   }   static char stdinBuffer[1024];   static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer);   static const char *stdinPos = stdinDataEnd;   void readAhead(size_t amount) {     size_t remaining = stdinDataEnd - stdinPos;     if (remaining < amount) {       memmove(stdinBuffer, stdinPos, remaining);       size_t sz = fread(stdinBuffer + remaining, 1,                         sizeof(stdinBuffer) - remaining, stdin);       stdinPos = stdinBuffer;       stdinDataEnd = stdinBuffer + remaining + sz;       if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0;     }   }   int rint() {     readAhead(16);     int x = 0;     bool neg = false;     while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos;     if (*stdinPos ==  - ) {       ++stdinPos;       neg = true;     }     while (*stdinPos >=  0  && *stdinPos <=  9 ) {       x *= 10;       x += *stdinPos -  0 ;       ++stdinPos;     }     return neg ? -x : x;   }   char rch() {     readAhead(16);     while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos;     char ans = *stdinPos;     ++stdinPos;     return ans;   }   long long rlong() {     readAhead(32);     long long x = 0;     bool neg = false;     while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos;     if (*stdinPos ==  - ) {       ++stdinPos;       neg = true;     }     while (*stdinPos >=  0  && *stdinPos <=  9 ) {       x *= 10;       x += *stdinPos -  0 ;       ++stdinPos;     }     return neg ? -x : x;   }


#include <bits/stdc++.h>   using namespace std;   using ll = int;   vector<pair<ll, string>> con[101010];   int main() {     ios_base::sync_with_stdio(false);     cout.tie(NULL);     int n, m;     cin >> n >> m;     int reg, sc;     string name;     while (n--) {       cin >> name;       cin >> reg;       cin >> sc;       con[reg].push_back(make_pair(sc, name));     }     for (int i = 1; i <= m; i++) {       sort(con[i].begin(), con[i].end());       reverse(con[i].begin(), con[i].end());       if ((int)con[i].size() > 2 && con[i][1].first == con[i][2].first) {         cout <<  ? n ;       } else {         cout << con[i][0].second <<     << con[i][1].second << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, x, y, a, l, w, z, ss, t, k, e, c, total, nnn, yes,       mx = 531, mn = -1, r, xx, yy, last, cas;   map<string, int> wow;   int main() {     string s;     cin >> s;     wow[s] = 1;     x = 1;     l = s.size();     for (int i = 0; i < l; i++) {       char te = s[l - 1];       for (int j = l - 1; j > 0; j--) {         s[j] = s[j - 1];       }       s[0] = te;       if (wow[s] == 0) {         x++;         wow[s]++;       }     }     cout << x;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   int n, p1l, p1r, p2l, p2r;   string s, t, ans;   int main() {     ios_base::sync_with_stdio(false);     cin >> s >> t;     sort(s.begin(), s.end());     sort(t.begin(), t.end());     ans.resize(s.length(), 0);     p1l = 0;     p2r = (int)s.length() - 1;     p1r = ((int)s.length() + 1) / 2 - 1;     p2l = p2r - (int)s.length() / 2 + 1;     n = (int)s.length() - 1;     for (int i = 0, nm = 0; i <= n; ++i, ++nm) {       if (t[p2r] <= s[p1l]) {         if (nm % 2 == 0)           ans[n] = s[p1r--];         else           ans[n] = t[p2l++];         n -= 1;         i -= 1;       } else {         if (nm % 2 == 0)           ans[i] = s[p1l++];         else           ans[i] = t[p2r--];       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> finger;   int seq[100];   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> seq[i];     for (int i = 0; i < m; i++) {       int x;       cin >> x;       finger.push_back(x);     }     for (int i = 0; i < n; i++) {       for (vector<int>::iterator itr = finger.begin(); itr != finger.end();            itr++) {         if (*itr == seq[i]) {           cout << *itr <<    ;           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int u, v, w;     cin >> u >> v;     if (u > v || (u + v) % 2 != 0)       cout << -1 << endl;     else if (u == v && u == 0)       cout << 0 << endl;     else if (u == v)       cout << 1 << endl << u << endl;     else {       w = (v - u) / 2;       if ((u & w) == 0)         cout << 2 << endl << u + w <<     << w << endl;       else         cout << 3 << endl << u <<     << w <<     << w << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 2e5 + 10;   long long ans[M << 2], tag[M << 2];   int a[M], p[M];   void push_up(int p) { ans[p] = min(ans[p << 1], ans[p << 1 | 1]); }   void ps(int p, int l, int r, long long x) {     ans[p] += x;     tag[p] += x;   }   void push_down(int p, int l, int r) {     if (!tag[p]) return;     int mid = l + r >> 1;     ps(p << 1, l, mid, tag[p]);     ps(p << 1 | 1, mid + 1, r, tag[p]);     tag[p] = 0;   }   void add(int p, int p_l, int p_r, int l, int r, int x) {     if (r < l) return;     if (p_l >= l && p_r <= r) {       ans[p] += x;       tag[p] += x;       return;     }     push_down(p, p_l, p_r);     int mid = p_l + p_r >> 1;     if (l <= mid) add(p << 1, p_l, mid, l, r, x);     if (r > mid) add(p << 1 | 1, mid + 1, p_r, l, r, x);     push_up(p);   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &p[i]);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       add(1, 1, n, p[i] + 1, n, a[i]);     }     long long sum = min(a[1], a[n]);     for (int i = 1; i < n; i++) {       add(1, 1, n, p[i] + 1, n, -a[i]);       add(1, 1, n, 1, p[i] - 1, a[i]);       sum = min(ans[1], sum);     }     printf( %lld n , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 9;   const double eps = 1e-11;   const int inf = (int)2e9;   const int mod = (int)1e9 + 7;   const double pi = 3.1415926535897932384626433832795;   int a[N];   int main() {     int n, k;     scanf( %d %d , &n, &k);     int ex = 0;     for (int i = 1; i <= n; i++) {       a[i] = i;       ex += i * (n - i + 1);     }     int cnt = 0;     do {       int cur = 0;       for (int i = 1; i <= n; i++) {         int mn = inf;         for (int j = i; j <= n; j++) {           mn = min(mn, a[j]);           cur += mn;         }       }       if (cur == ex) {         k--;         if (!k) {           for (int i = 1; i <= n; i++) {             printf( %d%c , a[i],    n [i == n]);           }         }       }     } while (next_permutation(a + 1, a + n + 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int h, n, a, c = 0;     cin >> n >> h;     for (int l = 0; l < n; ++l) {       cin >> a;       if (a <= h) {         c++;       } else         c += 2;     }     cout << c << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int dir[405][405], dic[405][405];   int main() {     memset(dir, 63, sizeof(dir));     memset(dic, 63, sizeof(dic));     scanf( %d %d , &n, &m);     for (int i = 1, x, y; i <= m; i++) {       scanf( %d %d , &x, &y);       dir[x][y] = dir[y][x] = 1;     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (dir[i][j] == 1061109567) dic[i][j] = 1;     for (int k = 1; k <= n; k++) {       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++)           dir[i][j] = min(dir[i][k] + dir[k][j], dir[i][j]);       }     }     for (int k = 1; k <= n; k++) {       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           int res = dic[i][k] + dic[k][j];           if (res == dir[i][j]) continue;           dic[i][j] = min(dic[i][j], res);         }       }     }     int ans = max(dic[1][n], dir[1][n]);     if (ans == 1061109567) ans = -1;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, d[100001], b[100001], x, ans = 0;   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       scanf( %d , &x);       d[i] = d[--x] + 1;       b[d[i]]++;     }     for (int i = 1; i < n; i++) ans += b[i] % 2;     printf( %d , ans + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   char promocode[1100][10];   int strdif(char a[], char b[]) {     int cnt = 0;     for (int ii = 0; ii < 6; ii++) {       if (a[ii] != b[ii]) cnt++;     }     return cnt;   }   int main() {     scanf( %d n , &n);     for (int ii = 1; ii <= n; ii++) {       scanf( %s , promocode[ii]);     }     int ans = 6;     for (int ii = 1; ii <= n - 1; ii++) {       for (int jj = ii + 1; jj <= n; jj++) {         ans = min(ans, (strdif(promocode[ii], promocode[jj]) - 1) / 2);       }     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     while (cin >> n) {       int y = 1;       for (; y <= n / 2 + 1; y++) {         for (int x = 1; x <= n; x++) {           if (x <= n / 2 + 1 - y || x >= n / 2 + 1 + y)             cout <<  * ;           else             cout <<  D ;         }         cout <<   n ;       }       for (y = n / 2; y > 0; y--) {         for (int x = 1; x <= n; x++) {           if (x <= n / 2 + 1 - y || x >= n / 2 + 1 + y)             cout <<  * ;           else             cout <<  D ;         }         cout <<   n ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     long long ans;     ans = (n - 2) * (n - 2);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1605;   int n, m, tot;   int a[MAXN][MAXN];   int sz[MAXN * MAXN];   int ans[MAXN * MAXN];   int cnt[MAXN * MAXN];   int fa1[MAXN * MAXN];   int fa2[MAXN * MAXN];   double v[MAXN][MAXN];   double t[MAXN][MAXN];   int getroot(int *fa, int u) {     return u == fa[u] ? u : fa[u] = getroot(fa, fa[u]);   }   void run() {     memcpy(t, v, sizeof(t));     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j]) {           double sum = 0, cnt = 0;           for (int k = i - 4; k <= i + 4; k++)             for (int l = j - 4; l <= j + 4; l++)               if (k >= 1 && k <= n && l >= 1 && l <= m &&                   (i - k) * (i - k) + (j - l) * (j - l) <= 25)                 sum += t[k][l], cnt++;           v[i][j] = sum / cnt;         }   }   void merge(int *fa, int xa, int ya, int xb, int yb) {     if (!a[xa][ya] || !a[xb][yb]) return;     fa[getroot(fa, (xa - 1) * m + ya)] = getroot(fa, (xb - 1) * m + yb);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         fa1[(i - 1) * m + j] = fa2[(i - 1) * m + j] = (i - 1) * m + j;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j]) {           v[i][j] = 1;           if (i > 1) merge(fa1, i, j, i - 1, j);           if (i < n) merge(fa1, i, j, i + 1, j);           if (j > 1) merge(fa1, i, j, i, j - 1);           if (j < m) merge(fa1, i, j, i, j + 1);         }     run();     run();     run();     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] && v[i][j] < 0.2) {           if (i > 1 && v[i - 1][j] < 0.2) merge(fa2, i, j, i - 1, j);           if (i < n && v[i + 1][j] < 0.2) merge(fa2, i, j, i + 1, j);           if (j > 1 && v[i][j - 1] < 0.2) merge(fa2, i, j, i, j - 1);           if (j < m && v[i][j + 1] < 0.2) merge(fa2, i, j, i, j + 1);         }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] && v[i][j] < 0.2) sz[getroot(fa2, (i - 1) * m + j)]++;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] && v[i][j] < 0.2 &&             getroot(fa2, (i - 1) * m + j) == (i - 1) * m + j &&             sz[(i - 1) * m + j] >= 5)           cnt[getroot(fa1, (i - 1) * m + j)]++;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] && getroot(fa1, (i - 1) * m + j) == (i - 1) * m + j)           ans[++tot] = cnt[(i - 1) * m + j];     sort(ans + 1, ans + tot + 1);     printf( %d n , tot);     for (int i = 1; i <= tot; i++) printf( %d  , ans[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   int main() {     int n, k;     scanf( %d %d , &n, &k);     vector<int> A(n), acc(n);     for (int i = (0); i < (n); ++i) cin >> A[i];     acc[0] = A[0];     for (int i = (1); i < (n); ++i) acc[i] = A[i] + acc[i - 1];     int rsp = -inf;     for (int i = (0); i < (n); ++i) {       for (int j = (i); j < (n); ++j) {         int sum = acc[j] - (i ? acc[i - 1] : 0);         rsp = max(rsp, sum);         vector<int> v1, v2;         for (int kk = (0); kk < (n); ++kk) {           if (kk < i or kk > j) {             v1.push_back(A[kk]);           } else {             v2.push_back(A[kk]);           }         }         sort(v1.rbegin(), v1.rend());         sort(v2.begin(), v2.end());         int lim = min(v1.size(), v2.size());         for (int kk = (0); kk < (min(k, lim)); ++kk) {           sum -= v2[kk];           sum += v1[kk];           rsp = max(rsp, sum);         }       }     }     printf( %d n , rsp);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 10;   struct node {     int v, w, next;   } edges[maxn << 2];   int head[maxn], cnt;   int n, k;   void add_edge(int u, int v, int w) {     edges[++cnt] = {v, w, head[u]};     head[u] = cnt;   }   long long dp[maxn][2];   void dfs(int rt, int fa) {     vector<long long> vv;     long long ans = 0;     for (int i = head[rt]; i; i = edges[i].next) {       if (edges[i].v == fa) continue;       dfs(edges[i].v, rt);       vv.push_back(dp[edges[i].v][0] + edges[i].w - dp[edges[i].v][1]);       ans += dp[edges[i].v][1];     }     sort(vv.begin(), vv.end(), greater<long long>());     if (k == 1) dp[rt][0] = ans;     for (int i = 0; i < vv.size(); ++i) {       if (i < k && vv[i] > 0) ans += vv[i];       if (i == k - 1 - 1) dp[rt][0] = ans;       if (i == k - 1) dp[rt][1] = ans;     }     if (dp[rt][0] == -1) dp[rt][0] = ans;     if (dp[rt][1] == -1) dp[rt][1] = ans;   }   int main() {     int q;     scanf( %d , &q);     while (q--) {       scanf( %d%d , &n, &k);       memset(head, 0, sizeof(int) * (n + 1));       memset(dp, -1, sizeof(long long) * (n + 1) * 2);       cnt = 0;       for (int i = 0, u, v, w; i < n - 1; ++i) {         scanf( %d%d%d , &u, &v, &w);         add_edge(u, v, w);         add_edge(v, u, w);       }       dfs(1, 0);       printf( %lld n , max(dp[1][0], dp[1][1]));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool chkmin(T &a, const T &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T>   inline bool chkmax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   inline int read() {     int res = 0, fl = 1;     char r = getchar();     for (; !isdigit(r); r = getchar())       if (r ==  - ) fl = -1;     for (; isdigit(r); r = getchar()) res = (res << 3) + (res << 1) + r - 48;     return res * fl;   }   const int Maxn = 2e5 + 10;   vector<pair<int, int> > g[Maxn];   long double val[Maxn];   int siz[Maxn];   void dfs(int now, int pa) {     siz[now] = 1;     for (int i = g[now].size() - 1; i >= 0; --i) {       int nxt = g[now][i].first;       if (pa == nxt) continue;       val[nxt] += g[now][i].second;       dfs(nxt, now);       val[now] += val[nxt];       siz[now] += siz[nxt];     }   }   long long Time, t[Maxn];   bool cmp(pair<int, int> A, pair<int, int> B) {     return val[A.first] > val[B.first];   }   void Dfs(int now, int pa) {     for (int i = g[now].size() - 1; i >= 0; --i) {       int nxt = g[now][i].first;       if (nxt == pa) continue;       val[nxt] = 10000.00 * val[nxt] / siz[nxt];     }     sort(g[now].begin(), g[now].end(), cmp);     t[now] = Time;     for (int i = g[now].size() - 1; i >= 0; --i) {       int nxt = g[now][i].first;       if (pa == nxt) continue;       Time += g[now][i].second;       Dfs(nxt, now);       Time += g[now][i].second;     }   }   int main() {     int n = read();     for (int i = 1; i < n; ++i) {       int x = read(), y = read(), z = read();       g[x].push_back(make_pair(y, z));       g[y].push_back(make_pair(x, z));     }     dfs(1, 0);     Dfs(1, 0);     long double ans = 0;     for (int i = 1; i <= n; ++i) ans += t[i];     printf( %.6Lf n , 1.00 * ans / (n - 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     node *bit[2];     int cnt = 0;     int index = -1;   };   node *root;   void insert(string &s, int id) {     node *cur = root;     for (char x : s) {       int ch = x -  0 ;       if (!cur->bit[ch]) cur->bit[ch] = new node();       cur = cur->bit[ch];       cur->cnt++;     }     cur->index = id;   }   int already(string &s) {     node *cur = root;     for (char x : s) {       int ch = x -  0 ;       if (!cur->bit[ch])         return -1;       else if (cur->bit[ch]->cnt < 1)         return -1;       cur = cur->bit[ch];     }     return cur->index;   }   void remove(string &s) {     node *cur = root;     for (char x : s) {       int ch = x -  0 ;       cur = cur->bit[ch];       cur->cnt--;     }     cur->index = -1;   }   void test_case() {     root = new node();     int n;     cin >> n;     int a[n];     vector<bool> can(n, true);     int both_zero = 0, both_one = 0;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       a[i] = (s[0] -  0 ) * 2 + (s.back() -  0 );       if (s[0] != s.back()) {         int index = already(s);         if (index != -1) {           can[index] = false;           can[i] = false;           remove(s);         } else {           reverse(s.begin(), s.end());           insert(s, i);         }       } else {         can[i] = false;       }     }     vector<int> possible[2];     vector<int> type(4, 0);     for (int i = 0; i < n; i++) {       if (can[i] == true) {         possible[a[i] - 1].push_back(i + 1);       } else {         type[a[i]]++;       }     }     vector<int> ans[2];     vector<int> valid(2, true);     for (int t = 0; t < 2; t++) {       vector<int> cnt = type;       vector<int> p[2];       p[0] = possible[0];       p[1] = possible[1];       int last = t;       for (int i = 0; i < n; i++) {         bool found = false;         for (int j = 0; j < 4; j++) {           if (last == j / 2 && cnt[j] > 0) {             cnt[j]--;             found = true;             last = j % 2;             break;           }         }         if (found) continue;         if (!p[last].empty()) {           p[last].pop_back();           last = 1 - last;           continue;         }         if (p[1 - last].empty()) {           valid[t] = false;           break;         }         ans[t].push_back(p[1 - last].back());         p[1 - last].pop_back();         last = 1 - last;       }     }     if (valid[0] == false && valid[1] == false) {       cout <<  -1 n ;       return;     }     int id = 0;     if (valid[0] == true && valid[1] == false) {       id = 0;     } else if (valid[0] == false && valid[1] == true) {       id = 1;     } else {       if (ans[0].size() < ans[1].size())         id = 0;       else         id = 1;     }     cout << (int)ans[id].size() <<   n ;     for (int i : ans[id]) cout << i <<    ;     cout <<   n ;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     while (t--) test_case();   }


#include <bits/stdc++.h>   using namespace std;   int a[200020], b[200020], c[200020];   int main(void) {     int n, k;     int m;     int i;     int o;     scanf( %d %d , &n, &k);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     for (i = 1; i <= n; i++) scanf( %d , &b[i]);     for (i = 1, m = o = 0; i <= n; i++)       if (a[i] < b[i]) {         o += a[i];         --k;       } else {         o += b[i];         c[m++] = a[i] - b[i];       }     if (k > 0) {       sort(c, c + m);       for (i = 0; i < k; i++) o += c[i];     }     printf( %d n , o);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char ans[100010 * 100];   char s[100010 * 10];   int f[100010 * 100];   int getf(int k) { return k == f[k] ? k : f[k] = getf(f[k]); }   int main() {     int n, k, LEN = 0, id;     cin >> n;     for (int i = 0; i < 100010 * 100; i++) f[i] = i;     for (int i = 0; i < n; i++) {       scanf( %s %d , s, &k);       int len = strlen(s);       while (k--) {         scanf( %d , &id);         for (int i = getf(id); i < id + len; i = f[i] = getf(i + 1)) {           ans[i] = s[i - id];         }         LEN = max(LEN, id + len);       }     }     for (int i = 1; i < LEN; i++) {       if (ans[i] ==   0 ) ans[i] =  a ;     }     puts(ans + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void foobar() {     long long int n, i;     cin >> n;     vector<long long int> arr(n);     long long int mm = INT_MAX;     for (i = 0; i < n; ++i) {       cin >> arr[i];       mm = min(mm, arr[i]);     }     for (i = 0; i < n; ++i) {       arr[i] -= mm;       if (arr[i] % 2) {         cout <<  NO  << endl;         return;       }     }     cout <<  YES  << endl;   }   int main() {     long long int test;     cin >> test;     while (test--) foobar();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ofstream out( output.txt );     auto start = chrono::high_resolution_clock::now();     long long t = 1;     for (long long test = 1; test <= t; test++) {       long long n;       cin >> n;       vector<long long> a(n);       pair<long long, long long> b[n];       for (long long i = 0; i <= n - 1; i++)         cin >> a[i], b[i].first = a[i], b[i].second = i;       sort(b, b + n);       long long ord[n];       long long shts = 0, I = 0;       for (long long i = n - 1; i >= 0; i--) {         shts += (b[i].first * I + 1);         ord[I] = b[i].second + 1;         I++;       }       cout << shts <<   n ;       for (long long i = 0; i <= n - 1; i++) cout << ord[i] <<    ;       cout <<   n ;     }     auto stop = chrono::high_resolution_clock::now();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500043;   const int K = 750;   int a[N];   int sum[K][K];   int main() {     int q;     scanf( %d , &q);     for (int i = 0; i < q; i++) {       int t, x, y;       scanf( %d %d %d , &t, &x, &y);       if (t == 1) {         a[x] += y;         for (int i = 1; i < K; i++) sum[i][x % i] += y;       } else {         if (x >= K) {           int ans = 0;           for (int i = y; i <= 500000; i += x) ans += a[i];           printf( %d n , ans);         } else           printf( %d n , sum[x][y]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool cmin(T& a, const T& b) {     return a > b ? a = b, 1 : 0;   }   template <typename T>   inline bool cmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   int read() {     int x = 0, f = 1;     char ch;     for (ch = getchar(); !isdigit(ch); ch = getchar())       if (ch ==  - ) f = -1;     for (; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar())       ;     return x * f;   }   const int MaxN = 48, MaxM = 1 << (MaxN / 2);   int n, m, F[MaxN];   int find(int x) { return F[x] == x ? x : F[x] = find(F[x]); }   char c[MaxN][MaxN];   int sz[MaxN], b[MaxM], h[MaxM], g[MaxM], f[MaxM];   int main() {     int i, j, k;     n = read();     for (i = 0; i < n; i++) F[i] = i;     for (i = 0; i < n; i++) {       scanf( %s , c[i]);       for (j = 0; j < i; j++)         if (c[i][j] ==  A ) F[find(i)] = find(j);     }     for (i = 0; i < n; i++) sz[find(i)]++;     for (i = 0; i < n; i++)       if (find(i) == i && sz[i] > 1) h[i] = 1 << m, m++;     for (i = 0; i < n; i++)       for (j = 0; j < i; j++)         if (c[i][j] ==  X ) {           int x = find(i), y = find(j);           if (x == y) return puts( -1 ), 0;           if (h[x] && h[y]) f[h[x] | h[y]] = 1;         }     if (!m) return printf( %d n , n - 1), 0;     for (i = 0; i < (1 << m); i++)       for (j = 0; j < m; j++)         if (i >> j & 1) f[i] |= f[i ^ (1 << j)];     for (i = 0; i < (1 << m); i++) {       b[i] = b[i >> 1] + (i & 1);       f[i] = !f[i];       g[i] = 1;     }     for (j = 0; j < m; j++)       for (i = 0; i < (1 << m); i++)         if (i >> j & 1) f[i] += f[i ^ (1 << j)];     for (i = 1; i <= m; i++) {       int t = 0;       for (j = 0; j < (1 << m); j++) {         g[j] = (long long)g[j] * f[j];         if (m - b[j] & 1)           t -= g[j];         else           t += g[j];       }       if (t) return printf( %d n , n + i - 1), 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, t;     cin >> s >> t;     int n = s.length();     int x = t.length();     vector<int> lft(x);     vector<int> rgt(x);     int l = 0;     for (int i = 0; i < n && l < x; ++i) {       if (s[i] == t[l]) {         lft[l++] = i;       }     }     l = x - 1;     for (int i = n - 1; i >= 0 && l >= 0; --i) {       if (s[i] == t[l]) {         rgt[l--] = i;       }     }     int mx = 0;     mx = max(mx, lft[0]);     mx = max(mx, rgt[0]);     mx = max(mx, n - 1 - lft[x - 1]);     mx = max(mx, n - 1 - rgt[x - 1]);     for (int i = 1; i < x; ++i) mx = max(mx, rgt[i] - lft[i - 1] - 1);     cout << mx;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, i;     cin >> a >> b;     int arr[a + 1];     for (i = 1; i <= a; i++) cin >> arr[i];     for (i = 1; i <= a; i++) {       b -= (86400 - arr[i]);       if (b <= 0) break;     }     cout << i << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline long long gcd(long long a, long long b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   signed main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     string s;     int n, a, b;     cin >> n >> a >> b >> s;     for (int i = 0; i <= n / a; i++) {       if ((n - i * a) % b == 0) {         cout << i + (n - i * a) / b << endl;         for (int j = 0; j < i * a; j++) {           cout << s[j];           if ((j + 1) % a == 0) cout << endl;         }         for (int j = i * a, k = 0; j < n; j++, k++) {           cout << s[j];           if ((k + 1) % b == 0) cout << endl;         }         return 0;       }     }     cout <<  -1 n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> primes;   int dp[101][1 << 18], n, a[111], man, ansd[111], loc[100];   vector<int> fact[100];   int rec(int i, int mask) {     if (i == n) return 0;     if (dp[i][mask] != 1000000000) return dp[i][mask];     int dif = abs(a[i] - 1);     int ans = rec(i + 1, mask) + dif;     for (int j = 2; j <= 60; j++) {       int k;       for (k = 0; k < fact[j].size(); k++)         if (mask & (1 << fact[j][k])) break;       if (k != (int)fact[j].size()) continue;       int d = abs(a[i] - j);       if (d <= dif) {         int msk = mask;         for (k = 0; k < fact[j].size(); k++) msk = msk | (1 << fact[j][k]);         ans = min(ans, rec(i + 1, msk) + d);       }     }     return dp[i][mask] = ans;   }   void reccons(int i, int mask, int m) {     if (i == n) return;     int dif = abs(a[i] - 1);     if (rec(i + 1, mask) == m - dif) {       ansd[i] = 1;       reccons(i + 1, mask, m - dif);       return;     }     for (int j = 2; j <= 60; j++) {       int k;       for (k = 0; k < fact[j].size(); k++)         if (mask & (1 << fact[j][k])) break;       if (k != (int)fact[j].size()) continue;       int d = abs(a[i] - j);       if (d > dif) continue;       int msk = mask;       for (k = 0; k < fact[j].size(); k++) msk = msk | (1 << fact[j][k]);       if (rec(i + 1, msk) == m - d) {         ansd[i] = j;         reccons(i + 1, msk, m - d);         return;       }     }   }   int main() {     int i, j, t;     for (i = 2; i <= 60; i++) {       for (j = 2; j < i; j++)         if (i % j == 0) break;       if (j == i) primes.push_back(i);     }     for (i = 0; i < 17; i++) loc[primes[i]] = i;     for (i = 2; i <= 60; i++) {       for (j = 0; j < 17; j++)         if (i % primes[j] == 0) fact[i].push_back(j);     }     scanf( %d , &n);     for (i = 0; i < n; i++) scanf( %d , &a[i]);     for (i = 0; i <= n; i++)       for (j = 0; j < (1 << 17); j++) dp[i][j] = 1000000000;     man = rec(0, 0);     reccons(0, 0, man);     for (i = 0; i < n; i++) printf( %d  , ansd[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100 + 10;   string a[maxn], ans[maxn];   int main() {     int n;     while (cin >> n) {       int cnt[2][26];       memset(cnt, 0, sizeof cnt);       for (int i = 0; i < n; ++i) cin >> a[i], ++cnt[0][a[i][0] -  A ];       multiset<string> Q;       for (int i = 0; i < n; ++i) {         string x;         cin >> x;         ++cnt[1][x[0] -  A ];         Q.insert(x);       }       sort(a, a + n);       int cur = 0;       for (int i = 0; i < n; ++i) {         string& t = a[i];         int id = t[0] -  A ;         for (multiset<string>::iterator it = Q.begin(); it != Q.end(); ++it) {           const string& now = *it;           int idx = now[0] -  A ;           if (now[0] == t[0]) {             ans[cur++] = t +     + now;             --cnt[0][id];             --cnt[1][idx];             Q.erase(it);             break;           } else if (cnt[0][idx] < cnt[1][idx] && cnt[0][id] > cnt[1][id]) {             ans[cur++] = t +     + now;             --cnt[0][id];             --cnt[1][idx];             Q.erase(it);             break;           }         }       }       sort(ans, ans + cur);       cout << ans[0];       for (int i = 1; i < cur; ++i) {         cout <<  ,   << ans[i];       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool check(int left, int right, int m) {     left ^= right;     if (left > m)       return false;     else       return true;   }   void solve() {     int n, m;     cin >> n >> m;     if (n > m) {       cout << 0 <<   n ;       return;     }     int ans = 0;     for (int i = 0; i < 31; ++i) {       if ((1 << i) & n)         continue;       else {         ans ^= (1 << i);         if ((ans ^ n) > m) break;       }     }     for (int i = 30; i >= 0; --i) {       if ((1 << i) & ans) {         int num = ans ^ (1 << i);         if ((num ^ n) > m) {           ans ^= (1 << i);         }       }     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int test;     cin >> test;     while (test--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100 * 1000 + 10;   const int inf = (int)1e9;   const double eps = 1e-6;   int main() {     int r, g, b;     cin >> r >> g >> b;     int rr = (((r + 1) / 2) - 1) * 3 + 30;     int gg = (((g + 1) / 2) - 1) * 3 + 31;     int bb = (((b + 1) / 2) - 1) * 3 + 32;     cout << max(rr, max(bb, gg)) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int SOURCE = 0, SINK = 1;   struct edge {     int to, idx, cap;   };   vector<vector<edge>> adj;   vector<int> lvl, ptr;   int totalflow;   int dfs(int n, int f) {     if (n == SINK) {       totalflow += f;       return f;     }     if (lvl[n] == lvl[SINK]) return 0;     while (ptr[n] < (int)adj[n].size()) {       edge& e = adj[n][ptr[n]];       ptr[n]++;       if (lvl[e.to] == lvl[n] + 1 && e.cap > 0) {         int nf = dfs(e.to, min(f, e.cap));         if (nf) {           e.cap -= nf;           adj[e.to][e.idx].cap += nf;           return nf;         }       }     }     return 0;   }   bool runMaxFlow() {     lvl.assign(adj.size(), -1);     ptr.assign(adj.size(), 0);     lvl[SOURCE] = 0;     queue<int> bfs;     bfs.push(SOURCE);     while (!bfs.empty()) {       int t = bfs.front();       bfs.pop();       for (edge& e : adj[t]) {         if (lvl[e.to] != -1 || e.cap <= 0) continue;         lvl[e.to] = lvl[t] + 1;         bfs.push(e.to);       }     }     if (lvl[SINK] == -1) return false;     while (dfs(SOURCE, 1 << 30)) {     }     return true;   }   void initMaxFlow(int nodes) {     totalflow = 0;     adj.clear();     adj.resize(nodes);   }   void addEdge(int a, int b, int w) {     adj[a].push_back(edge{b, (int)adj[b].size(), w});     adj[b].push_back(edge{a, (int)adj[a].size() - 1, 0});   }   const int dr[] = {0, 0, -1, 1}, dc[] = {-1, 1, 0, 0};   int R, C, M, F, S, fw[22][22][22][22];   char gd[23][23];   struct scayger {     int r, c, t, g;   } sg[22 * 22 * 2 + 1];   bool try1(long long t, bool sgm) {     initMaxFlow(M + F + R * C * 2 + 3);     for (int r = 0; r < int(R); r++)       for (int c = 0; c < int(C); c++)         addEdge(M + F + 3 + (r * C + c) * 2, M + F + 3 + (r * C + c) * 2 + 1, 1);     for (int i = 0; i < int(M); i++) addEdge(0, 3 + i, 1);     for (int i = 0; i < int(F); i++) addEdge(M + 3 + i, 1, 1);     if (sgm)       addEdge(0, 2, 1);     else       addEdge(2, 1, 1);     for (int i = 0; i < int(S); i++) {       scayger& s = sg[i];       int g = s.g;       if (g == 0) g = (sgm) ? 1 : 2;       for (int r = 0; r < int(R); r++)         for (int c = 0; c < int(C); c++) {           long long d = fw[r][c][s.r][s.c];           if (d == -1) continue;           if (t >= d * s.t) {             if (g == 1)               addEdge(2 + i, M + F + 3 + (r * C + c) * 2, 1);             else               addEdge(M + F + 3 + (r * C + c) * 2 + 1, 2 + i, 1);           }         }     }     while (runMaxFlow()) {     }     return totalflow == S / 2;   }   bool tryit(long long t) { return try1(t, true) || try1(t, false); }   int main() {     scanf( %d%d%d%d , &R, &C, &M, &F);     for (int r = 0; r < int(R); r++) scanf( %s , gd[r]);     S = M + F + 1;     for (int i = 0; i < int(S); i++) {       scayger& s = sg[i];       scanf( %d%d%d , &s.r, &s.c, &s.t);       s.r--;       s.c--;       if (i == 0)         s.g = 0;       else if (i <= M)         s.g = 1;       else         s.g = 2;     }     memset(fw, -1, sizeof fw);     for (int r = 0; r < int(R); r++)       for (int c = 0; c < int(C); c++)         if (gd[r][c] ==  . ) {           for (int i = 0; i < int(4); i++) {             int r2 = r + dr[i], c2 = c + dc[i];             if (r2 < 0 || r2 >= R || c2 < 0 || c2 >= C || gd[r2][c2] ==  # )               continue;             fw[r][c][r2][c2] = fw[r2][c2][r][c] = 1;           }           fw[r][c][r][c] = 0;         }     for (int kr = 0; kr < int(R); kr++)       for (int kc = 0; kc < int(C); kc++)         if (gd[kr][kc] ==  . )           for (int ir = 0; ir < int(R); ir++)             for (int ic = 0; ic < int(C); ic++)               if (gd[ir][ic] ==  . )                 for (int jr = 0; jr < int(R); jr++)                   for (int jc = 0; jc < int(C); jc++)                     if (gd[jr][jc] ==  . ) {                       int &d1 = fw[ir][ic][kr][kc], &d2 = fw[kr][kc][jr][jc],                           &d = fw[ir][ic][jr][jc];                       if (d1 == -1 || d2 == -1) continue;                       if (d == -1 || d1 + d2 < d) d = d1 + d2;                     }     if (S % 2 != 0 || !tryit(LLONG_MAX)) {       puts( -1 );       return 0;     }     long long t0 = -1, t1 = 1000000000LL * R * C;     while (t1 - t0 > 1) {       long long t = (t0 + t1) / 2;       if (tryit(t))         t1 = t;       else         t0 = t;     }     printf( %lld n , t1);   }


#include <bits/stdc++.h>   using namespace std;   class TwoSAT {    private:     const static int maxm = 100010 * 2;     int S[maxm], c;     vector<int> G[maxm];     bool DFS(int u) {       if (vis[u ^ 1]) return false;       if (vis[u]) return true;       vis[u] = 1;       S[c++] = u;       for (auto &v : G[u])         if (!DFS(v)) return false;       return true;     }       public:     int N;     bool vis[maxm];     void Clear() {       for (int i = 2; i < (N + 1) * 2; ++i) G[i].clear();       memset(vis, 0, sizeof(bool) * (N + 1) * 2);     }     void AddClause(int x, int xv, int y, int yv) {       x = x * 2 + xv;       y = y * 2 + yv;       G[x].push_back(y);       G[y].push_back(x);     }     bool Solve() {       for (int i = 2; i < (N + 1) * 2; i += 2)         if (!vis[i] && !vis[i + 1]) {           c = 0;           if (!DFS(i)) {             while (c > 0) vis[S[--c]] = 0;             if (!DFS(i + 1)) return false;           }         }       return true;     }   };   int n, m;   bool r[100010];   int f[100010];   TwoSAT ts;   int main() {     ios::sync_with_stdio(false);     int i, j, x, y;     cin >> n >> m;     ts.N = m + 1;     for (i = 1; i <= n; ++i) cin >> r[i];     for (i = 1; i <= m; ++i) {       cin >> x;       for (j = 0; j < x; ++j) {         cin >> y;         if (f[y]) {           if (r[y]) {             ts.AddClause(f[y], 1, i, 1);             ts.AddClause(f[y], 0, i, 0);           } else {             ts.AddClause(f[y], 1, i, 0);             ts.AddClause(f[y], 0, i, 1);           }         } else           f[y] = i;       }     }     if (ts.Solve())       puts( YES );     else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double d, h, v, e;   int main() {     ios_base::sync_with_stdio(false);     cin >> d >> h >> v >> e;     v = v / (acos(-1) * d * d / 4);     double t = h / (v - e);     if (t < 0 || fabs(v - e) < 1e-8) {       printf( NO n );       return 0;     }     printf( YES n%.12lf n , t);     return 0;   }