#include <bits/stdc++.h>   #pragma GCC optimize( Ofast ,  unroll-loops ,  omit-frame-pointer ,  inline )   #pragma GCC option( arch=native ,  tune=native ,  no-zero-upper )   #pragma GCC target( avx2 )   using namespace std;   template <typename T>   void maxtt(T& t1, T t2) {     t1 = max(t1, t2);   }   template <typename T>   void mintt(T& t1, T t2) {     t1 = min(t1, t2);   }   bool debug = 0;   int n, m, k;   int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};   string direc =  RDLU ;   const long long MOD2 = (long long)1000000007 * (long long)1000000007;   long long ln, lk, lm;   void etp(bool f = 0) {     puts(f ?  YES  :  NO );     exit(0);   }   void addmod(int& x, int y, int mod = 1000000007) {     x += y;     if (x >= mod) x -= mod;     if (x < 0) x += mod;     assert(x >= 0 && x < mod);   }   void et(int x = -1) {     printf( %d n , x);     exit(0);   }   long long fastPow(long long x, long long y, int mod = 1000000007) {     long long ans = 1;     while (y > 0) {       if (y & 1) ans = (x * ans) % mod;       x = x * x % mod;       y >>= 1;     }     return ans;   }   long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }   vector<int> sufPaliLen[(100035)];   int a[(100035)], p[(100035)], q[(100035)], pos, c[(100035)], xyx;   void add(int pos) {     sufPaliLen[pos].clear();     if (a[pos] == a[pos - 1]) sufPaliLen[pos].push_back(2);     for (int x : sufPaliLen[pos - 1]) {       if (a[pos - 1 - x] == a[pos]) sufPaliLen[pos].push_back(x + 2);     }   }   bool ckEvenPali(int l, int r) {     for (int z : sufPaliLen[r]) {       if (z == r - l + 1) return 1;     }     return 0;   }   int qy() {     int ans = xyx + c[pos];     int cur = pos;     while (p[cur] && q[pos] <= cur - p[cur] / 2) {       cur -= p[cur] / 2;       ans++;     }     return ans;   }   void fmain(int tid) {     scanf( %d , &n);     q[0] = 1;     for (int(i) = 1; (i) <= (int)(n); (i)++) {       pos++;       scanf( %d , a + pos);       bool fg = 0;       add(pos);       if (!sufPaliLen[pos].empty()) {         int len = sufPaliLen[pos][0];         if (ckEvenPali(pos - len / 2 - len + 1, pos - len / 2)) {           pos -= len;           xyx += 2;           fg = 1;         }       }       if (!fg) {         p[pos] = sufPaliLen[pos].empty() ? 0 : sufPaliLen[pos][0];         q[pos] = q[pos - 1];         c[pos] = c[pos - 1];         if (ckEvenPali(q[pos], pos)) {           q[pos] += (pos - q[pos] + 1) / 2;           c[pos]++;         }       }       printf( %d  , qy());     }   }   int main() {     int t = 1;     for (int(i) = 1; (i) <= (int)(t); (i)++) {       fmain(i);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   const int M = 1000005;   int go[M][4], fa[M], sum[M], val[M], lzy[M], hsc[M], tsz;   void pull(int x) {     if (!x) return;     sum[x] = val[x] + sum[go[x][0]] + sum[go[x][1]];     if (go[x][2]) sum[x] += min(sum[go[x][2]], sum[go[x][3]]);     hsc[x] = go[x][2] || hsc[go[x][0]] || hsc[go[x][1]];   }   int make(int v) {     ++tsz;     val[tsz] = v;     pull(tsz);     return tsz;   }   void rev(int x) {     if (!x) return;     swap(go[x][0], go[x][1]);     lzy[x] ^= 1;   }   void push(int x) {     if (lzy[x]) {       rev(go[x][0]), rev(go[x][1]);       if (go[x][2]) rev(go[x][2]), rev(go[x][3]);       lzy[x] = 0;     }   }   int is2(int x) { return go[fa[x]][0] == x || go[fa[x]][1] == x; }   int pd2(int x) { return go[fa[x]][0] == x ? 0 : go[fa[x]][1] == x ? 1 : -1; }   int is4(int x) { return go[fa[x]][2] == x || go[fa[x]][3] == x; }   int pd4(int x) { return go[fa[x]][2] == x ? 2 : go[fa[x]][3] == x ? 3 : -1; }   int isl(int x) { return !go[x][0] && !go[x][1]; }   void rot(int x) {     int y = fa[x], z = fa[y], p = pd2(x), w = go[x][p ^ 1];     if (is2(y))       go[z][pd2(y)] = x;     else if (is4(y))       go[z][pd4(y)] = x;     fa[x] = z;     go[x][p ^ 1] = y;     fa[y] = x;     go[y][p] = w;     if (w) fa[w] = y;     pull(y);   }   void cl(int x) {     if (pd2(x) != -1) cl(fa[x]);     push(x);   }   void splay(int x, int y = 0) {     for (cl(x); pd2(x) != -1 && fa[x] != y; rot(x)) {       if (pd2(fa[x]) != -1 && fa[fa[x]] != y)         rot(pd2(fa[x]) == pd2(x) ? fa[x] : x);     }     pull(x);   }   int fir(int x) {     while (go[x][0]) push(x), x = go[x][0];     return x;   }   void cyc(int c, int x) {     int d = pd4(x);     splay(c);     push(x);     int o = fir(go[c][1]);     splay(o, c);     go[c][1] = 0;     go[c][d] = go[x][0];     fa[go[c][d]] = c;     go[x][0] = 0;     rev(go[x][1]);     go[o][1] = go[x][1];     fa[go[o][1]] = o;     go[x][1] = 0;     go[o][0] = go[c][d ^ 1];     fa[go[o][0]] = o;     go[c][d ^ 1] = o;     fa[o] = c;     pull(o);     pull(c);   }   void access(int x) {     int y = 0, z = x;     for (; x; y = x, x = fa[x]) {       splay(x);       if (fa[x] && go[fa[x]][2]) cyc(fa[x], x);       go[x][1] = y;       pull(x);     }     splay(z);   }   void mkrt(int x) {     access(x);     rev(x);   }   void path(int x, int y) {     mkrt(x);     access(y);     splay(x, y);   }   int get(int x, int y) {     if (x == y) return 0;     path(x, y);     if (fa[x] != y) return -1;     return sum[y];   }   bool link(int x, int y, int w) {     if (x == y) return false;     path(x, y);     if (fa[x] != y) {       int e = make(w);       fa[x] = e;       fa[e] = y;       return true;     }     if (hsc[y]) return false;     int e = make(w), c = make(0);     go[c][2] = go[x][1];     fa[go[c][2]] = c;     go[c][3] = e;     fa[e] = c;     go[x][1] = c;     fa[c] = x;     pull(c);     pull(x);     pull(y);     return true;   }   bool cut(int x, int y, int w) {     if (x == y) return false;     path(x, y);     if (fa[x] != y) return false;     if (!isl(go[x][1])) return false;     int c = go[x][1];     push(c);     if (go[c][2]) {       int d = 0;       if (isl(go[c][2]) && val[go[c][2]] == w) d = 2;       if (isl(go[c][3]) && val[go[c][3]] == w) d = 3;       if (!d) return false;       go[x][1] = go[c][d ^ 1];       fa[go[x][1]] = x;       pull(x);       pull(y);       return true;     } else {       if (val[c] != w) return false;       go[x][1] = 0;       go[y][0] = 0;       fa[x] = 0;       pull(x);       pull(y);       return true;     }   }   int n, m;   int U[500005], V[500005];   long long ans;   int main() {     scanf( %d%d , &n, &m);     tsz = n;     for (int i = 1; i <= m; i++) scanf( %d%d , &U[i], &V[i]);     for (int i = 1, j = 0; i <= m; i++) {       if (j < i) {         j = i;         link(U[j], V[j], 0);       }       while (j < m && link(U[j + 1], V[j + 1], 0)) j++;       cut(U[i], V[i], 0);       ans += j - i + 1;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 202;   const int C = 6000;   int dp[N][C];   int n, k;   int a[N][2];   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) {       long long x;       scanf( %lld , &x);       while (x % 5 == 0) {         a[i][0]++;         x /= 5;       }       while (x % 2 == 0) {         a[i][1]++;         x /= 2;       }     }     for (int i = 0; i <= k; i++)       for (int j = 0; j < C; j++) dp[i][j] = -1;     dp[0][0] = 0;     for (int i = 0; i < n; i++)       for (int j = k - 1; j >= 0; j--)         for (int x = 0; x < C; x++) {           if (dp[j][x] == -1) continue;           dp[j + 1][x + a[i][0]] =               max(dp[j + 1][x + a[i][0]], dp[j][x] + a[i][1]);         }     int ans = 0;     for (int i = 0; i < C; i++) ans = max(ans, min(dp[k][i], i));     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     int cnt = 0;     int num;     for (int i = 0; i != n; ++i) {       scanf( %d , &num);       if (num % 2 == 1) ++cnt;     }     if (cnt >= 1)       printf( First n );     else       printf( Second n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, maxx;   vector<int> v, cnt;   map<int, int> ma;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     maxx = min(2, n);     v.resize(n + 1);     cnt.resize(100001);     for (int i = 1; i <= n; i++) {       cin >> v[i];       map<int, int>::iterator it;       it = ma.find(cnt[v[i]]);       if (it != ma.end()) {         it->second -= 1;         if (it->second == 0) ma.erase(cnt[v[i]]);       }       cnt[v[i]] += 1;       it = ma.find(cnt[v[i]]);       if (it == ma.end())         ma.insert(pair<int, int>(cnt[v[i]], 1));       else         it->second += 1;       int chk = 1;       if (ma.size() == 1) {         chk = 0;         pair<int, int> ff;         ff = *ma.begin();         if (ff.first == 1)           chk = 1;         else if (ff.second == 1)           chk = 1;       }       if (ma.size() > 2) chk = 0;       if (ma.size() == 2) {         chk = 0;         pair<int, int> ff, ss;         it = ma.begin();         ff = *it;         it++;         ss = *it;         if ((ff.first == 1 && ff.second == 1) ||             (ss.first == ff.first + 1 && ss.second == 1))           chk = 1;       }       if (chk == 1) maxx = max(maxx, i);     }     cout << maxx;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());   mt19937 rnf(2106);   const int N = 200005;   int n, m;   int x[N], y[N];   int d[N];   queue<int> q[N];   int dist(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); }   int ans[N];   bool stg(int s) {     for (int i = 1; i <= m; ++i) {       while (!q[d[i]].empty()) q[d[i]].pop();     }     for (int i = 1; i <= m; ++i) q[d[i]].push(i);     for (int i = 0; i < n; ++i) ans[i] = -1;     for (int i = s; i < n; i += 2) {       int dd = dist(x[i], y[i], x[(i - 1 + n) % n], y[(i - 1 + n) % n]) +                dist(x[i], y[i], x[(i + 1) % n], y[(i + 1) % n]);       if (q[dd].empty()) return false;       ans[i] = q[dd].front();       q[dd].pop();     }   }   void solv() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) scanf( %d%d , &x[i], &y[i]);     for (int i = 1; i <= m; ++i) {       scanf( %d , &d[i]);     }     if (stg(0)) {       printf( YES n );       for (int i = 0; i < n; ++i) printf( %d  , ans[i]);       printf(  n );     } else if (stg(1)) {       printf( YES n );       for (int i = 0; i < n; ++i) printf( %d  , ans[i]);       printf(  n );     } else       printf( NO n );   }   int main() {     solv();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long calWhite(long long x1, long long y1, long long x2, long long y2) {     if (x1 == 0 || y1 == 0 || x2 == 0 || y2 == 0) {       return 0;     }     long long tmp = (abs(x1 - x2) + 1) * (abs(y1 - y2) + 1);     long long res = tmp / 2;     if (tmp % 2 == 1 && (x1 * y1 % 2 == 1 || (x1 % 2 == 0 && y1 % 2 == 0))) {       res++;     }     return res;   }   long long calBlack(long long x1, long long y1, long long x2, long long y2) {     if (x1 == 0 || y1 == 0 || x2 == 0 || y2 == 0) {       return 0;     }     return (x2 - x1 + 1) * (y2 - y1 + 1) - calWhite(x1, y1, x2, y2);   }   int main() {     long long t;     scanf( %I64d , &t);     while (t--) {       long long n, m;       long long x1, x2, y1, y2;       long long x3, x4, y3, y4;       scanf( %I64d%I64d , &n, &m);       scanf( %I64d%I64d%I64d%I64d , &x1, &y1, &x2, &y2);       scanf( %I64d%I64d%I64d%I64d , &x3, &y3, &x4, &y4);       long long answ = calWhite(1, 1, n, m);       answ += calBlack(x1, y1, x2, y2);       answ -= calWhite(x3, y3, x4, y4);       long long x5, y5, x6, y6;       if (x1 <= x3 && x3 <= x2) {         x5 = x3;         x6 = min(x2, x4);       } else if (x1 <= x4 && x4 <= x2) {         x5 = max(x1, x3);         x6 = x4;       } else if (x3 <= x1 && x2 <= x4) {         x5 = x1;         x6 = x2;       } else {         x5 = x6 = 0;       }       if (y1 <= y3 && y3 <= y2) {         y5 = y3;         y6 = min(y2, y4);       } else if (y1 <= y4 && y4 <= y2) {         y5 = max(y1, y3);         y6 = y4;       } else if (y3 <= y1 && y2 <= y4) {         y5 = y1;         y6 = y2;       } else {         y5 = y6 = 0;       }       answ -= calBlack(x5, y5, x6, y6);       printf( %I64d %I64d n , answ, n * m - answ);     }   }


#include <bits/stdc++.h>   using namespace std;   const int P = 1000000007;   const int N = 1010000;   int fail[N];   int go[N][26], len[N], diff[N], anc[N], lst;   int n;   char str[N];   int p;   int s[N];   int f[N], g[N];   void addChar(int c, int ww) {     int x = lst;     while (s[ww] != s[ww - len[x] - 1]) {       x = fail[x];     }     if (!go[x][c]) {       len[p] = len[x] + 2;       int k = fail[x];       while (s[ww] != s[ww - len[k] - 1]) k = fail[k];       fail[p] = go[k][c];       go[x][c] = p;       diff[p] = len[p] - len[fail[p]];       if (diff[p] == diff[fail[p]])         anc[p] = anc[fail[p]];       else         anc[p] = fail[p];       p++;     }     lst = go[x][c];   }   void init() {     p = 2;     len[0] = 0;     len[1] = -1;     fail[0] = 1;     fail[1] = 0;     f[0] = 1;     lst = 1;   }   int main() {     scanf( %s , str + 1);     n = strlen(str + 1);     if (n & 1) {       printf( 0 n );       return 0;     }     for (int i = (int)1; i <= (int)n / 2; i++) {       s[i * 2 - 1] = str[i] -  a ;       s[i * 2] = str[n + 1 - i] -  a ;     }     s[0] = -1;     init();     for (int i = (int)1; i <= (int)n; i++) {       addChar(s[i], i);       for (int x = lst; x; x = anc[x]) {         g[x] = f[i - (len[anc[x]] + diff[x])];         if (anc[x] != fail[x]) {           g[x] = (g[x] + g[fail[x]]) % P;         }         if (i % 2 == 0) f[i] = (f[i] + g[x]) % P;       }     }     printf( %d n , f[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 250000;   int n, dfn[N], size[N], cnt, Q;   struct node {     int tag, sum;   } t[N << 2];   vector<int> V[N];   void dfs(int x) {     dfn[x] = ++cnt;     size[x] = 1;     for (int i = 0, s = V[x].size(); i < s; i++)       dfs(V[x][i]), size[x] += size[V[x][i]];   }   void pushdown(int i, int l, int r) {     if (!t[i].tag) return;     int mid = (l + r) >> 1, ls = i << 1, rs = ls | 1;     t[ls].tag ^= 1;     t[rs].tag ^= 1;     t[ls].sum = (mid - l + 1 - t[ls].sum);     t[rs].sum = (r - mid - t[rs].sum);     t[i].tag = 0;   }   void update(int i, int l, int r, int x, int y) {     if (l >= x && r <= y) {       t[i].tag ^= 1;       t[i].sum = (r - l + 1 - t[i].sum);       return;     }     pushdown(i, l, r);     int mid = (l + r) >> 1;     if (mid >= x) update(i << 1, l, mid, x, y);     if (mid < y) update(i << 1 | 1, mid + 1, r, x, y);     t[i].sum = t[i << 1].sum + t[i << 1 | 1].sum;   }   int query(int i, int l, int r, int x, int y) {     if (l >= x && r <= y) return t[i].sum;     pushdown(i, l, r);     int mid = (l + r) >> 1, ans = 0;     if (mid >= x) ans += query(i << 1, l, mid, x, y);     if (mid < y) ans += query(i << 1 | 1, mid + 1, r, x, y);     return ans;   }   int main() {     scanf( %d , &n);     for (int i = 2; i <= n; i++) {       int x;       scanf( %d , &x);       V[x].push_back(i);     }     dfs(1);     for (int i = 1; i <= n; i++) {       char s = getchar();       while (s <  0  || s >  1 ) s = getchar();       if (s ==  1 ) update(1, 1, n, dfn[i], dfn[i]);     }     scanf( %d , &Q);     while (Q--) {       int x, opt;       char s = getchar();       while (!isalpha(s)) s = getchar();       if (s ==  g )         opt = 2;       else         opt = 1;       while (isalpha(s)) s = getchar();       scanf( %d , &x);       if (opt == 1)         update(1, 1, n, dfn[x], dfn[x] + size[x] - 1);       else         printf( %d n , query(1, 1, n, dfn[x], dfn[x] + size[x] - 1));     }   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 1e5 + 7;   const long long INF = 1e9 + 7;   vector<long long> g[MAXN];   bool bad[MAXN];   long long best = -1;   long long besth = -1;   void jfs(long long u, long long parent, long long h) {     if (besth < h) {       best = u;       besth = h;     }     for (long long v : g[u]) {       if (v == parent) continue;       if (bad[v]) continue;       jfs(v, u, h + 1);     }   }   pair<long long, long long> getd(long long u, long long parent, long long h) {     pair<long long, long long> res = {h, u};     for (long long v : g[u]) {       if (v == parent) continue;       if (bad[v]) continue;       pair<long long, long long> nr = getd(v, u, h + 1);       res = max(res, nr);     }     return res;   }   bool go(long long u, long long parent, long long want) {     if (u == want) {       bad[u] = true;       return true;     }     for (long long v : g[u]) {       if (v == parent) continue;       if (go(v, u, want)) {         bad[u] = true;         return true;       }     }     return false;   }   long long dist[MAXN];   void calc(long long u, long long parent, long long h) {     dist[u] = h;     for (long long v : g[u]) {       if (v == parent) continue;       if (bad[v]) continue;       calc(v, u, h + 1);     }   }   long long l[MAXN], r[MAXN], par[MAXN], pos[MAXN];   vector<long long> e;   void build(long long u, long long parent) {     par[u] = parent;     pos[u] = e.size();     e.push_back(u);     l[u] = e.size();     for (long long v : g[u]) {       if (v == parent) continue;       build(v, u);     }     r[u] = e.size() - 1;   }   pair<long long, long long> tree[MAXN * 4];   long long add[MAXN * 4];   void buildt(long long v, long long tl, long long tr) {     if (tl == tr) {       tree[v] = {dist[e[tl]], e[tl]};       return;     }     long long tm = (tl + tr) / 2;     buildt(v * 2 + 1, tl, tm);     buildt(v * 2 + 2, tm + 1, tr);     tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]);   }   void push(long long v) {     tree[v * 2 + 1].first += add[v];     add[v * 2 + 1] += add[v];     tree[v * 2 + 2].first += add[v];     add[v * 2 + 2] += add[v];     add[v] = 0;   }   void relax(long long v) { tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]); }   void upd(long long v, long long tl, long long tr, long long l, long long r,            long long x) {     if (tr < l || r < tl) return;     if (l <= tl && tr <= r) {       tree[v].first += x;       add[v] += x;       return;     }     long long tm = (tl + tr) / 2;     push(v);     upd(v * 2 + 1, tl, tm, l, r, x);     upd(v * 2 + 2, tm + 1, tr, l, r, x);     relax(v);   }   void del(long long v, long long tl, long long tr, long long p) {     if (tl == tr) {       tree[v].first = -INF;       return;     }     long long tm = (tl + tr) / 2;     push(v);     if (p <= tm) {       del(v * 2 + 1, tl, tm, p);     } else {       del(v * 2 + 2, tm + 1, tr, p);     }     relax(v);   }   void make_bad(long long u, long long n) {     bad[u] = true;     del(0, 0, n - 1, pos[u]);     upd(0, 0, n - 1, l[u], r[u], -1);   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long n;     cin >> n;     if (n == 1) {       cout <<  1 n ;       return 0;     }     for (long long i = 0; i < n - 1; ++i) {       long long u, v;       cin >> u >> v;       g[u - 1].push_back(v - 1);       g[v - 1].push_back(u - 1);     }     pair<long long, long long> t1 = getd(0, 0, 0);     pair<long long, long long> t2 = getd(t1.second, t1.second, 0);     go(t1.second, t1.second, t2.second);     long long curr = t2.first + 1;     cout <<  1   << curr <<    ;     for (long long i = 0; i < n; ++i) dist[i] = -INF;     for (long long i = 0; i < n; ++i) {       if (!bad[i]) continue;       for (long long j : g[i]) {         if (bad[j]) continue;         calc(j, j, 0);       }     }     long long root = t1.second;     build(root, root);     buildt(0, 0, n - 1);     for (long long it = 3; it <= n; ++it) {       if (curr == n) {         for (long long i = 0; i < n - it + 1; ++i) cout << n <<    ;         break;       }       pair<long long, long long> t = tree[0];       curr += t.first + 1;       cout << curr <<    ;       long long u = t.second;       while (!bad[u]) {         make_bad(u, n);         u = par[u];       }     }     cout <<   n ;     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, ans = 0, a, b = 0, N;     cin >> N;     for (long long i = 1; i <= 1e6; i++) {       n = N;       a = i * 2;       n -= a;       b = ((i - 1) * (i * 3) / 2);       n -= b;       if (n < 0) break;       if (n % 3 == 0) ans++;     }     cout << ans;     return 0;   }


/* 2013-08-11 10:03:33.914034 */#include<iostream>   #include<cstdio>   #include<cstdlib>   #include<ctime>   #include<cmath>   #include<cctype>   #include<cstring>   #include<algorithm>      #include<map>   #include<set>   #include<vector>   #include<string>   #include<queue>      using namespace std;      char s[1011][1011];   bool u[1011][1011];   char c[1011][1011];   int n,m;   const int dx[]={1,0,-1,0};   const int dy[]={0,1,0,-1};   inline bool fitr(int x,int y){    return s[x][y+1]== b &&s[x][y+2]== w &&!u[x][y+1]&&!u[x][y+2];   }   inline bool fitc(int x,int y){    return s[x+1][y]== b &&s[x+2][y]== w &&!u[x+1][y]&&!u[x+2][y];   }   char choosec(int a,int b,int ax,int ay){    int opt=0;    for(int i=0;i<3;++i)    {     for(int k=0;k<4;++k)     {      int nx=a+dx[k],ny=b+dy[k];      if(nx>0&&nx<=n&&ny>0&&ny<=m)      {       if(c[nx][ny]!= . ) opt|=1<<(c[nx][ny]- a );      }     }     a+=ax,b+=ay;    }    for(int i=0;;++i)    {     if(((1<<i)&opt)==0) return i+ a ;    }   }   int main(){    scanf( %d%d ,&n,&m);    for(int i=1;i<=n;++i)    {     scanf( %s ,s[i]+1);     for(int j=1;j<=m;++j) c[i][j]= . ;    }    for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(!u[i][j]&&s[i][j]!= . )    {     if(s[i][j]== b )     {      printf( NO n );      return 0;     }     else     {      if(fitr(i,j))      {       u[i][j]=u[i][j+1]=u[i][j+2]=1;       char co=choosec(i,j,0,1);       c[i][j]=c[i][j+1]=c[i][j+2]=co;      }      else      {       if(fitc(i,j))       {        u[i][j]=u[i+1][j]=u[i+2][j]=1;        char co=choosec(i,j,1,0);        c[i][j]=c[i+1][j]=c[i+2][j]=co;       }       else       {        printf( NO n );        return 0;       }      }     }    }    printf( YES n );    for(int i=1;i<=n;++i) printf( %s n ,c[i]+1);    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a;     if (a == 1) {       cout <<  1 ;       return 0;     }     if (a == 2) {       cout <<  13 ;       return 0;     }     long long sol = 1;     sol += 1LL * 6 * a * (a + 1) / 2;     sol += 1LL * 6 * ((a - 2) * (a - 1) / 2 - 1);     cout << sol <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, x[3], y[3], xx, yy, jump[111111];   pair<long long, long long> dist[111111];   long long maxR2[111111], ans = LONG_LONG_MAX;   int main() {     scanf( %d %d %d %d %d , &N, &x[1], &y[1], &x[2], &y[2]);     for (int i = 1; i <= N; i++) {       scanf( %d %d , &xx, &yy);       dist[i].first =           1LL * (xx - x[1]) * (xx - x[1]) + 1LL * (yy - y[1]) * (yy - y[1]);       dist[i].second =           1LL * (xx - x[2]) * (xx - x[2]) + 1LL * (yy - y[2]) * (yy - y[2]);     }     sort(dist + 1, dist + N + 1);     for (int i = N; i >= 0; i--) {       maxR2[i] = max(maxR2[i + 1], dist[i].second);       jump[i] = (dist[i].first == dist[i + 1].first) ? jump[i + 1] : (i + 1);     }     for (int i = 0; i <= N; i++) ans = min(ans, dist[i].first + maxR2[jump[i]]);     printf( %I64d , ans);   }


#include <bits/stdc++.h>   using namespace std;   string run(vector<string> &vec) {     vector<bool> used(vec.size(), false);     vector<char> ans(100,  1 );     for (int i = 0; i < vec[0].size(); ++i) {       ans[50 + i] = vec[0][i];     }     used[0] = true;     while (true) {       int count = 0;       for (int i = 1; i < vec.size(); ++i) {         if (not used[i]) {           bool thr = false;           for (int p = 0; p < vec[i].size() && !thr; ++p) {             for (int k = 0; k < ans.size() && !thr; ++k) {               if (ans[k] == vec[i][p]) {                 int in = k - p;                 for (int np = 0; np < vec[i].size(); ++np) {                   ans[in + np] = vec[i][np];                 }                 used[i] = true;                 count += 1;                 thr = true;               }             }           }         }       }       if (count == 0) break;     }     int sz = 0;     for (int i = 0; i < vec.size(); ++i) {       if (not used[i]) {         vec[sz] = vec[i];         ++sz;       }     }     vec.resize(sz);     string ret;     for (int i = 0; i < ans.size(); ++i) {       if (ans[i] !=  1 ) {         ret += ans[i];       }     }     return ret;   }   int main() {     int n;     cin >> n;     vector<string> vs(n);     for (int i = 0; i < n; ++i) {       cin >> vs[i];     }     string ans;     while (not vs.empty()) {       string c1 = run(vs);       ans += c1;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 998244353;   const long long int MAXN = 3e3 + 5;   const long long int inf = 1e17;   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     ;     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       vector<char> v(n);       for (int i = 0; i < n; i++) cin >> v[i];       long long int a = v.back() -  0 ;       while (a % 2 == 0) {         v.pop_back();         if (v.size() > 0)           a = v.back() -  0 ;         else           break;       }       if (v.size() < 2) {         cout <<  -1  << endl;         continue;       }       long long int sum = 0;       for (int i = 0; i < v.size(); i++) {         a = v[i] -  0 ;         sum += a;       }       if (sum % 2 == 0) {         for (auto i : v) cout << i;         cout << endl;         continue;       }       long long int ind = -1;       for (int i = v.size() - 2; i > -1; i--) {         a = v[i] -  0 ;         if (a % 2) {           ind = i;           break;         }       }       if (ind == 0 && v[1] ==  0 ) ind = -1;       if (ind != 0 && v[0] ==  0 ) ind = -1;       if (ind == -1)         cout <<  -1  << endl;       else {         for (int i = 0; i < v.size(); i++) {           if (i != ind) cout << v[i];         }         cout << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const double pi = 4.0 * atan(1.0);   int64_t num;   queue<int64_t> qu;   bool ifgood(int64_t x) {     int64_t a, b;     a = 0;     b = 0;     while (x) {       if (x % 10 == 4)         ++a;       else if (x % 10 == 7)         ++b;       else         return false;       x /= 10;     }     if (a == b) return true;     return false;   }   void bfs() {     while (!qu.empty()) qu.pop();     int64_t a, b, k;     a = 0;     qu.push(a);     while (!qu.empty()) {       b = qu.front();       if (b > num && ifgood(b)) return;       qu.pop();       k = b;       b = b * 10 + 4;       qu.push(b);       k = k * 10 + 7;       qu.push(k);     }     return;   }   int main() {     cin >> num;     if (ifgood(num))       cout << num << endl;     else {       bfs();       cout << qu.front() << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios::sync_with_stdio(false);     long long A, B, n;     cin >> A >> B >> n;     while (n--) {       long long l, t, m;       cin >> l >> t >> m;       if (t < A + (l - 1) * B) {         cout << -1 << endl;         continue;       }       long long low = l, high = 1e7 * 1ll;       while (low < high) {         long long mi = (low + high + 1) / 2;         long long res = (mi - l + 1) * A;         res += (((mi - 1) * mi) / 2 - max(0ll, ((l - 2) * (l - 1)) / 2)) * B;         if (res <= t * m && A + (mi - 1) * B <= t) {           low = mi;         } else {           high = mi - 1;         }       }       cout << low << endl;     }     return 0;   }


#include <bits/stdc++.h>   struct f {     int pos;     int num;   } a[108];   int cmp(const void *a, const void *b) {     return (*(struct f *)a).num - (*(struct f *)b).num;   }   int main() {     int n, k;     while (scanf( %d%d , &n, &k) == 2) {       int i;       for (i = 0; i < n; i++) {         scanf( %d , &a[i].num);         a[i].pos = i + 1;       }       qsort(a, n, sizeof(a[0]), cmp);       int sum = 0;       int count = 0;       for (i = 0; i < n; i++) {         if (i == 0 && a[i].num > k) break;         sum += a[i].num;         if (sum > k) break;         count++;       }       printf( %d n , count);       for (i = 0; i < count; i++) {         if (i != 0)           printf(  %d , a[i].pos);         else           printf( %d , a[i].pos);       }       if (count) printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, max = -100000;     int a[200], inter[200], exter[200];     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       for (int j = i; j < n; j++) {         int sum = 0;         vector<int> inter, exter;         inter.clear();         exter.clear();         for (int m = i; m < j + 1; m++) {           sum += a[m];           inter.push_back(a[m]);         }         for (int m = 0; m < i; m++) exter.push_back(a[m]);         for (int m = j + 1; m < n; m++) exter.push_back(a[m]);         sort(inter.begin(), inter.end());         sort(exter.begin(), exter.end());         for (int m = 0; m < k; m++) {           if (m >= inter.size() || exter.size() < m + 1) break;           if (exter[exter.size() - 1 - m] > inter[m])             sum += exter[exter.size() - 1 - m] - inter[m];         }         max = max > sum ? max : sum;       }     }     cout << max << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct _d {     int r1, c1, r2, c2;   };   int n, m;   char arr[500][501];   bool ans[600000];   _d queries[600000];   bitset<500> DPU[500][500], DPD[500][500];   void dnc(int l, int r, vector<int> &qid) {     if (l > r) return;     int mid = (l + r) / 2;     vector<int> ql, qr;     for (int i = 0; i < qid.size(); i++) {       if (queries[qid[i]].r2 < mid) {         ql.push_back(qid[i]);         qid[i] = qid.back(), qid.pop_back(), --i;       } else if (queries[qid[i]].r1 > mid) {         qr.push_back(qid[i]);         qid[i] = qid.back(), qid.pop_back(), --i;       }     }     if (!qid.empty()) {       for (int i = m - 1; i >= 0; i--) {         DPU[mid][i] = i != m - 1 && arr[mid][i + 1] ==  .  ? DPU[mid][i + 1]                                                            : bitset<500>();         DPU[mid][i][i] = 1;       }       for (int i = 0; i < m; i++) {         DPD[mid][i] =             i && arr[mid][i - 1] ==  .  ? DPD[mid][i - 1] : bitset<500>();         DPD[mid][i][i] = 1;       }       for (int i = mid - 1; i >= l; i--) {         for (int j = m - 1; j >= 0; j--) {           DPU[i][j] = arr[i + 1][j] ==  .  ? DPU[i + 1][j] : bitset<500>();           if (j != m - 1 && arr[i][j + 1] ==  . ) DPU[i][j] |= DPU[i][j + 1];         }       }       for (int i = mid + 1; i <= r; i++) {         for (int j = 0; j < m; j++) {           DPD[i][j] = arr[i - 1][j] ==  .  ? DPD[i - 1][j] : bitset<500>();           if (j && arr[i][j - 1] ==  . ) DPD[i][j] |= DPD[i][j - 1];         }       }       for (int i : qid) {         auto cq = queries[i];         ans[i] = (DPU[cq.r1][cq.c1] & DPD[cq.r2][cq.c2]).any();       }     }     if (!ql.empty()) dnc(l, mid - 1, ql);     if (!qr.empty()) dnc(mid + 1, r, qr);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %s , arr[i]);     int q;     scanf( %d , &q);     vector<int> qid(q);     for (int i = 0; i < q; i++) {       int r1, c1, r2, c2;       scanf( %d%d%d%d , &r1, &c1, &r2, &c2);       queries[i] = {r1 - 1, c1 - 1, r2 - 1, c2 - 1};       qid[i] = i;     }     dnc(0, n - 1, qid);     for (int i = 0; i < q; i++) printf(ans[i] ?  Yes n  :  No n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const int N = 1e5 + 7;   int pre[N], sz[N], cnt[N], n, lencnt, dx, one;   pair<int, int> a[N];   void init() { memset(pre, -1, sizeof(pre)); }   int fd(int n) { return pre[n] == -1 ? n : pre[n] = fd(pre[n]); }   void mg(int a, int b) {     a = fd(a), b = fd(b);     if (a != b) {       pre[b] = a;       sz[a] += sz[b];       sz[b] = 0;     }   }   void solve(int x) {     int v = x + 1;     if (v <= n && sz[fd(v)]) {       if (--cnt[sz[fd(v)]] == 0) --lencnt;       if (--cnt[sz[x]] == 0) --lencnt;       mg(x, v);       if (++cnt[sz[fd(v)]] == 1) ++lencnt;     }     v = x - 1;     if (v >= 1 && sz[fd(v)]) {       if (--cnt[sz[fd(v)]] == 0) --lencnt;       if (--cnt[sz[fd(x)]] == 0) --lencnt;       mg(x, v);       if (++cnt[sz[fd(v)]] == 1) ++lencnt;     }     one = cnt[sz[fd(x)]];   }   int main(void) {     int i;     scanf( %d , &n);     init();     for (i = 1; i <= n; ++i) {       scanf( %d , &a[i].first);       a[i].second = i;     }     sort(a + 1, a + 1 + n);     int ans = 1;     for (i = 1; i <= n; ++i) {       sz[a[i].second] = 1;       if (++cnt[1] == 1) ++lencnt;       solve(a[i].second);       if (lencnt == 1 && one > dx) {         ans = a[i].first + 1;         dx = one;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   const int G = 3;   const int K = 40001;   const int inv2 = 499122177;   const int MaxN = 6e5 + 10;   const int mod = 998244353;   int n, k, lim, w[MaxN], rev[MaxN], _inv[MaxN];   void Mod(int &x) { x += x >> 31 & mod; }   void __init() {     _inv[1] = 1;     for (int i = 2; i < MaxN; i++)       _inv[i] = (unsigned long long)(mod - mod / i) * _inv[mod % i] % mod;   }   int fast_pow(int a, int b) {     int ret = 1;     while (b) {       if (b & 1) ret = (unsigned long long)ret * a % mod;       a = (unsigned long long)a * a % mod, b >>= 1;     }     return ret;   }   void init(int n) {     int l = 32 - __builtin_clz(n - 1);     lim = (1 << l);     for (int i = 0, ri = 0; i < lim; i++) {       rev[i] = ri;       for (int k = lim >> 1; (ri ^= k) < k;) k >>= 1;     }     int wn = fast_pow(G, (mod - 1) >> l);     w[lim >> 1] = 1;     for (int i = (lim >> 1) + 1; i < lim; i++)       w[i] = (unsigned long long)w[i - 1] * wn % mod;     for (int i = (lim >> 1) - 1; i; --i) w[i] = w[i << 1];     lim = l;   }   struct poly {     std::vector<int> v;     inline poly() {}     inline poly(int n) : v(n) {}     inline poly(const poly &r) : v(r.v) {}     inline poly(const std::vector<int> &r) : v(r) {}     inline size_t size() { return v.size(); }     inline bool empty() { return v.empty(); }     inline void resize(int n) { v.resize(n); }     inline void clear() { v.clear(); }     inline void shl() { v.insert(v.begin(), 0); }     void shrink() {       while (!v.empty() && !v.back()) v.pop_back();     }     inline int &operator[](int i) { return v[i]; }     inline static int len(int n) { return 1 << (32 - __builtin_clz(n - 1)); }     void deriv() {       for (int i = 1; i < v.size(); i++)         v[i - 1] = (unsigned long long)i * v[i] % mod;       v.pop_back();     }     void intg() {       shl();       for (int i = 1; i < v.size(); i++)         v[i] = (unsigned long long)v[i] * _inv[i] % mod;     }     void dft(int n) {       static unsigned long long tmp[MaxN];       int ofs = lim - __builtin_ctz(n);       resize(n);       if (n <= 1) return;       for (int i = 0; i < n; i++) tmp[rev[i] >> ofs] = v[i];       for (int i = 0; i < n; i += 2) {         int x = tmp[i], y = tmp[i + 1];         tmp[i] = x + y, tmp[i + 1] = x + mod - y;       }       for (int i = 2; i < n; i <<= 1)         for (int j = 0; j < n; j += (i << 1))           for (int k = 0; k < i; ++k) {             int y = (unsigned long long)tmp[i + j + k] * w[i + k] % mod;             tmp[i + j + k] = tmp[j + k] + mod - y, tmp[j + k] += y;           }       for (int i = 0; i < n; i++) v[i] = tmp[i] % mod;     }     void idft(int n) {       dft(n);       if (n <= 1) return;       std::reverse(v.begin() + 1, v.end());       int tmp = mod - (mod - 1) / n;       for (int i = 0; i < n; i++) v[i] = (unsigned long long)v[i] * tmp % mod;     }     void mul(int k) {       int n = size();       for (int i = 0; i < n; i++)         v[i] = (unsigned long long)(v[i] * k % mod + mod) % mod;     }     void add(poly r) {       int n = std::max(size(), r.size());       resize(n);       for (int i = 0; i < n; i++) v[i] = (unsigned long long)(v[i] + r[i]) % mod;       shrink();     }     void mul(poly r) {       int n = len(size() + r.size() - 1);       dft(n), r.dft(n);       for (int i = 0; i < n; i++) v[i] = (unsigned long long)v[i] * r[i] % mod;       idft(n), shrink();     }     void inv() {       std::vector<int> va(1);       int n = size(), m = len(n);       va[0] = fast_pow(v[0], mod - 2);       poly a;       v.resize(m), v.swap(va);       for (int i = 1; i <= m; i <<= 1) {         a.resize(i);         for (int j = 0; j < i; j++) a[j] = va[j];         a.dft(i << 1), dft(i << 1);         for (int j = 0; j < (i << 1); j++)           v[j] = v[j] * (2 + mod - (unsigned long long)v[j] * a[j] % mod) % mod;         idft(i << 1), resize(i);       }       resize(n);     }     void ln() {       poly f0 = *this;       int n = size();       deriv(), f0.inv(), mul(f0);       resize(n), intg(), resize(n);     }     void exp() {       std::vector<int> va(1);       va[0] = 1;       poly a;       int n = size(), m = len(n);       v.resize(m), v.swap(va);       for (int i = 1; i <= m; i *= 2) {         a = *this, a.resize(i), a.ln();         for (int j = 0; j < i; j++) a[j] = (va[j] + !j + mod - a[j]) % mod;         mul(a), resize(i);       }       resize(n);     }     void pw(int k) {       ln();       int n = size();       for (int i = 0; i < n; i++) v[i] = (unsigned long long)v[i] * k % mod;       exp();     }     void sqrt() {       std::vector<int> va(1);       int n = size(), m = len(n);       poly a, b;       va[0] = ::sqrt(v[0]);       v.resize(m), v.swap(va);       for (int i = 1; i <= m; i <<= 1) {         a.resize(i), b.resize(i);         for (int j = 0; j < i; j++) a[j] = va[j], b[j] = v[j];         b.inv(), a.mul(b);         for (int j = 0; j < i; j++)           v[j] = (unsigned long long)(v[j] + a[j]) % mod * inv2 % mod;         resize(i << 1);       }       resize(n);     }   } a, b, c, f, g, h;   void extend() {     f = g, g = h, h[0] = g[0];     f.resize(K), g.resize(K), h.resize(K);     for (int i = 1; i < K; i++)       h[i] = (((f[i - 1]) + ((((g[i - 1]) + (g[i])) % mod))) % mod);   }   void Double() {     int len = poly::len(2 * K), A, B, C, w = 1, wn = fast_pow(3, (mod - 1) / len);     a = f, a.dft(len), b = g, b.dft(len), c = h, c.dft(len);     for (int i = 0; i < len; i++, w = (unsigned long long)w * wn % mod) {       A = a[i], B = b[i], C = c[i];       a[i] =           (unsigned long long)(1ll * B * B % mod + 1ll * A * A % mod * w % mod) %           mod;       b[i] =           (unsigned long long)(1ll * B * C % mod + 1ll * A * B % mod * w % mod) %           mod;       c[i] =           (unsigned long long)(1ll * C * C % mod + 1ll * B * B % mod * w % mod) %           mod;     }     a.idft(len), f = a, b.idft(len), g = b, c.idft(len), h = c;     f.resize(K), g.resize(K), h.resize(K);   }   inline int read() {     unsigned long long x = 0;     char ch = getchar();     while (ch >  9  || ch <  0 ) ch = getchar();     while (ch <=  9  && ch >=  0 ) x = ((x * 10) + (ch ^ 48)), ch = getchar();     return x;   }   signed main() {     __init(), init(300000), n = read(), k = read(), f.resize(K);     g.resize(K), h.resize(K), g[0] = 1, h[0] = 1, h[1] = 1;     int lg = log2(n);     for (int i = lg - 1; ~i; i--) {       Double();       if (n & (1 << i)) extend();     }     for (int i = 1; i <= k; i++) printf( %d  , h[i]);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 11;   const long long oo = 1e17 + 123;   int tcases = 1;   int n, m = 0;   long long a[N], b[N], s[N], res[N];   pair<int, int> q[N];   struct Point {     double x, y;     Point() { x = y = 0; }     Point(double _x, double _y) : x(_x), y(y) {}   };   struct line {     double a, b;     line() {       a = 1;       b = 0;     }     line(double _a, double _b) {       a = _a;       b = _b;     }     long long val(double x) { return (long long)a >= oo ? oo : x * a + b; }     double getIntersection(line ll) { return (ll.b - b) / (a - ll.a); }   } l0[N];   struct LiChaoTree {     vector<int> coor;     vector<int> queries;     vector<line> f;     int m;     void addline(int k, int l, int r, line ll) {       if (l == r) {         if (f[k].val(coor[l - 1]) > ll.val(coor[l - 1])) f[k] = ll;         return;       }       int mid = (l + r) >> 1;       bool sl = ll.val(coor[l - 1]) < f[k].val(coor[l - 1]),            smid = ll.val(coor[mid] - 1) < f[k].val(coor[mid] - 1);       if (smid) swap(f[k], ll);       if (sl == smid) {         addline(k << 1 | 1, mid + 1, r, ll);       } else {         addline(k << 1, l, mid, ll);       }     }     long long cal(int k, int l, int r, long long x) {       if (coor[l - 1] > x || coor[r - 1] < x) return oo;       if (l == r) return f[k].val(x);       long long rr = f[k].val(x);       int mid = (l + r) >> 1;       return min(rr, min(cal(k << 1, l, mid, x), cal(k << 1 | 1, mid + 1, r, x)));     }     void init(int l, int r) {       coor.push_back(-n);       coor.push_back(0);       for (int i : queries) {         coor.push_back(q[i].first - q[i].second - 1);       }       sort((coor).begin(), (coor).end());       coor.erase(unique((coor).begin(), (coor).end()), coor.end());       m = coor.size() - 1;       f.assign(m * 4 + 11, line(oo, oo));       for (int i = l; i <= r; ++i) {         addline(1, 1, m, l0[i]);       }     }     void getMin(int l, int r) {       init(l, r);       for (int i : queries) {         int x = q[i].first, y = q[i].second;         res[i] = min(res[i], cal(1, 1, m, x - y - 1) + s[y]);       }     }   };   struct IT {     LiChaoTree f[N * 4];     void insert(int k, int l, int r, int L, int R, int i) {       if (L > r || l > R) return;       if (L <= l && r <= R) {         f[k].queries.push_back(i);         return;       }       int mid = (l + r) >> 1;       insert(k << 1, l, mid, L, R, i);       insert(k << 1 | 1, mid + 1, r, L, R, i);     }     void getMin(int k, int l, int r) {       f[k].getMin(l, r);       if (l < r) {         int mid = (l + r) >> 1;         getMin(k << 1, l, mid);         getMin(k << 1 | 1, mid + 1, r);       }     }   } segmentTree;   void testcase() {     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       s[i] = s[i - 1] + a[i];       l0[i] = line(a[i], a[i] * i - s[i - 1]);     }     cin >> m;     for (int i = 1; i <= m; ++i) {       int x, y;       cin >> x >> y;       segmentTree.insert(1, 1, n, y - x + 1, y, i);       q[i] = make_pair(x, y);     }     for (int i = 1; i <= m; ++i) res[i] = oo;     segmentTree.getMin(1, 1, n);     for (int i = 1; i <= m; ++i) cout << res[i] <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie();     cout.tie();     for (int i = 1; i <= tcases; ++i) testcase();     cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 34;   int n, m;   int f[MAXN][MAXN], s[MAXN][MAXN];   int work2(int j) {     s[m][m + 1 + j] = s[m][j] * s[m][m];     int tot = s[m][j] * f[m][j] + s[m][m + 1 + j] * f[m][m + 1 + j];     for (int i = 0; i < m; ++i) {       int x = f[i][j] + s[i][m] * f[i][m + j + 1] + s[m][j] * f[m + i + 1][j] +               s[i][m] * s[m][m + j + 1] * f[m + i + 1][m + j + 1];       tot += abs(x);     }     return tot;   }   int work1() {     int tot = 0;     for (int i = 0, x1, x2; i < m; ++i) {       s[m][i] = 1, x1 = work2(i);       s[m][i] = -1, x2 = work2(i);       tot += max(x1, x2);     }     return tot;   }   int main() {     scanf( %d , &n), m = n >> 1;     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) scanf( %d , f[i] + j);     int mx = 0;     for (int mask = 0, lim = 1 << (m + 1); mask < lim; ++mask) {       for (int i = 0; i <= m; ++i) s[i][m] = (mask & 1 << i) ? -1 : 1;       for (int i = m + 1; i < n; ++i) s[i][m] = s[m][m] * s[i - m - 1][m];       int cc = 0;       for (int i = 0; i < n; ++i) cc += s[i][m] * f[i][m];       mx = max(mx, work1() + cc);     }     printf( %d n , mx);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   void PLAY() {     cout << fixed << setprecision(10);     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int max(int a, int b) {     if (a > b) return a;     return b;   }   int min(int a, int b) {     if (a < b) return a;     return b;   }   long long fx4[] = {-1, 1, 0, 0};   long long fy4[] = {0, 0, -1, 1};   long long fx8[] = {-1, -1, -1, 0, 1, 1, 1, 0};   long long fy8[] = {-1, 0, 1, 1, 1, 0, -1, -1};   const double PI = 3.141592653589793;   const long long N = 2e5 + 5;   const long long mod = 1000000007;   long long m, n;   long long ar[N];   vector<long long> arr;   long long dp[5005][5005];   long long func(long long l, long long r) {     if (l == r)       return 0;     else if (l + 1 == r)       return 1;     long long &ans = dp[l][r];     if (ans != -1) return ans;     ans = 0;     long long op1 = INT_MAX;     long long op2 = INT_MAX;     long long op3 = INT_MAX;     if (arr[l] == arr[r])       op1 = 1 + func(l + 1, r - 1);     else {       op2 = 1 + func(l, r - 1);       op3 = 1 + func(l + 1, r);     }     ans = min(op1, min(op2, op3));     return ans;   }   int32_t main() {     PLAY();     cin >> m;     for (long long i = 0; i < m; i++) cin >> ar[i];     arr.push_back(ar[0]);     for (long long i = 1; i < m;) {       while (ar[i] == ar[i - 1]) i++;       if (i < m) arr.push_back(ar[i]);       i++;     }     memset(dp, -1, sizeof(dp));     n = arr.size();     cout << func(0, n - 1) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   void _dbg(string) { cout << endl; }   template <class H, class... T>   void _dbg(string s, H h, T... t) {     int l = s.find( , );     cout << s.substr(0, l) <<   =   << h <<  ,  ;     _dbg(s.substr(l + 1), t...);   }   template <class T, class U>   ostream& operator<<(ostream& o, const pair<T, U>& p) {     o <<  (  << p.first <<  ,  << p.second <<  ) ;     return o;   }   template <class T>   ostream& operator<<(ostream& o, const vector<T>& v) {     o <<  [ ;     for (T t : v) {       o << t <<  , ;     }     o <<  ] ;     return o;   }   const int N = 100000;   int n, m;   bool used[N + 5];   char s[N + 5];   int hoge(char c) {     int crnt = 0;     int last = -1;     int ret = 0;     for (int i = (int)(0); i < (int)(n); i++) {       crnt++;       if (s[i] == c) last = i;       if (used[i]) crnt = 0;       if (crnt == m) {         if (i - last >= m) return -1;         crnt = i - last;         used[last] = true;         ret++;       }     }     return ret;   }   int main() {     cin >> m >> s;     n = strlen(s);     for (int i = (int)(0); i < (int)(26); i++) {       char c = i +  a ;       int r = hoge(c);       if (r == -1) {         r = 0;         for (int j = (int)(0); j < (int)(n); j++)           if (s[j] == c) {             r++;             used[j] = true;           }       } else {         i = 100;       }       for (int j = (int)(0); j < (int)(r); j++) printf( %c , c);     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void c_p_c() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   bool isPrime(long long n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long i = 5; i * i <= n; i += 6) {       if (n % i == 0 || n % (i + 2) == 0) return false;     }     return true;   }   vector<bool> Sieve(long long n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (long long i = 2; i * i <= n; i++) {       if (prime[i]) {         for (long long j = i * i; j <= n; j += i) {           prime[j] = false;         }       }     }     vector<bool> primes(n + 1);     primes[0] = false;     primes[1] = false;     for (long long i = 2; i <= n; i++) {       if (prime[i]) primes[i] = true;     }     return primes;   }   set<long long> primeFactors(long long n) {     set<long long> factors;     while (n % 2 == 0) {       n /= 2;       factors.insert(2);     }     for (long long i = 3; i <= sqrt(n); i += 2) {       while (n % i == 0) {         factors.insert(i);         n /= i;       }     }     if (n > 2) factors.insert(n);     return factors;   }   bool cmp(string a, string b) { return a.length() < b.length(); }   int32_t main() {     c_p_c();     long long n, k;     cin >> n >> k;     for (long long i = n; i >= n - k + 1; i--) cout << i <<    ;     for (long long i = 1; i <= n - k; i++) cout << i <<    ;     cout <<   n ;   }


#include <bits/stdc++.h>   int main() {     char s[105];     int i, n, j;     scanf( %d , &n);     getchar();     for (i = 0; i < n; i++) {       scanf( %c , &s[i]);     }     for (i = 0; i < n; i++) {       if (s[i] ==  o  && s[i + 1] ==  g  && s[i + 2] ==  o ) {         printf( *** );         for (j = i + 3; j < n; j += 2) {           if (s[j] ==  g  && s[j + 1] ==  o ) {             continue;           } else {             break;           }         }         i = j - 1;       } else         printf( %c , s[i]);     }     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long K;   vector<int> E[200000 + 5];   void addEdge(int u, int v) {     E[u].push_back(v);     E[v].push_back(u);   }   int sz[200000 + 5], deep[200000 + 5], fa[200000 + 5];   int msz[200000 + 5], root;   void dfs1(int x, int f) {     sz[x] = 1;     msz[x] = 0;     for (int y : E[x]) {       if (y != f) {         dfs1(y, x);         sz[x] += sz[y];         msz[x] = max(msz[x], sz[y]);       }     }     msz[x] = max(msz[x], n - sz[x]);     if (root == 0 || msz[x] < msz[root]) root = x;   }   set<pair<int, int> > S[200000 + 5];   long long minans = 0, maxans = 0;   int sons[200000 + 5];   void dfs2(int x, int f, int rt) {     sz[x] = 1;     fa[x] = f;     deep[x] = deep[f] + 1;     for (int y : E[x]) {       if (y != f) {         sons[x]++;         dfs2(y, x, rt);         sz[x] += sz[y];       }     }     if (sz[x] > 1) S[rt].insert(make_pair(deep[x], x));     minans += sz[x] % 2;     maxans += sz[x];   }   bool vis[200000 + 5];   int getSon(int x) {     for (int t : E[x]) {       if (t != fa[x] && !vis[t]) {         vis[t] = 1;         return t;       }     }     return x;   }   void del(int x, int rt) {     vis[x] = 1;     sons[fa[x]]--;     if (sons[fa[x]] == 0) S[rt].erase(make_pair(deep[fa[x]], fa[x]));   }   priority_queue<pair<int, int> > q;   vector<int> ans;   void dfs3(int x) {     if (!vis[x]) ans.push_back(x);     for (int y : E[x])       if (y != fa[x]) dfs3(y);   }   int main() {     scanf( %d %lld , &n, &K);     for (int i = 1, u, v; i < n; i++) {       scanf( %d %d , &u, &v);       addEdge(u, v);     }     dfs1(1, 0);     for (int y : E[root]) {       dfs2(y, root, y);       q.push(make_pair(sz[y], y));     }     if (K >= minans && K <= maxans && (maxans - K) % 2 == 0) {       puts( YES );       K = (maxans - K) / 2;       while (K) {         int rt = q.top().second;         q.pop();         int lc = S[rt].rbegin()->second;         int d = deep[lc];         if (K >= d) {           K -= d;           int u = getSon(lc), v = getSon(lc);           del(u, rt);           del(v, rt);           printf( %d %d n , u, v);         } else {           int u = S[rt].lower_bound(make_pair(K, 0))->second;           int v = getSon(u);           del(u, rt);           del(v, rt);           printf( %d %d n , u, v);           break;         }         sz[rt] -= 2;         if (sz[rt] > 1) q.push(make_pair(sz[rt], rt));       }       dfs3(root);       for (int i = 0; i < (int)ans.size() / 2; i++)         printf( %d %d n , ans[i], ans[i + (int)ans.size() / 2]);     } else       puts( NO );   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200000 + 5, inf = 2e9;   const long double EPS = 1e-9L;   vector<pair<int, int> > pairs[26];   vector<int> mn[26];   int leftSub[N], rightSub[N];   void computeSub(string& a, string& b, int ans[]) {     for (int i = 0; i < (int)(a).size(); ++i) {       int prv = 0;       if (i != 0) prv = ans[i - 1];       if (prv == (int)(b).size() || a[i] != b[prv])         ans[i] = ans[i - 1];       else         ans[i] = 1 + ans[i - 1];     }   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s, t;     cin >> s >> t;     for (int i = 0; i < (int)(t).size(); ++i) {       int l = i, r = (int)(t).size() - 1 - i;       pairs[t[i] -  a ].push_back({l, r});     }     for (int c = 0; c < 26; ++c) {       sort(pairs[c].begin(), pairs[c].end());       for (int i = 0; i < (int)(pairs[c]).size(); ++i) {         if (i == 0)           mn[c].push_back(pairs[c][i].second);         else           mn[c].push_back(min(pairs[c][i].second, mn[c][i - 1]));       }     }     computeSub(s, t, leftSub);     reverse(s.begin(), s.end()), reverse(t.begin(), t.end());     computeSub(s, t, rightSub);     reverse(s.begin(), s.end()), reverse(rightSub, rightSub + (int)(s).size());     for (int i = 0; i < (int)(s).size(); ++i) {       int a = 0, b = 0;       if (i != 0) a = leftSub[i - 1];       if (i != (int)(s).size() - 1) b = rightSub[i + 1];       pair<int, int> p = {a, inf};       if (pairs[s[i] -  a ].empty() || pairs[s[i] -  a ][0] > p) {         cout <<  No n ;         return 0;       }       int ind =           upper_bound(pairs[s[i] -  a ].begin(), pairs[s[i] -  a ].end(), p) -           begin(pairs[s[i] -  a ]);       if (mn[s[i] -  a ][ind - 1] > b) {         cout <<  No n ;         return 0;       }     }     cout <<  Yes n ;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   int T, n, k;   long long ans, p;   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d%d , &n, &k);       ans = 0;       p = 1;       for (int i = 0; i <= 31; i++) {         if (((long long)(1 << i) & k) != 0) ans = (ans + p) % MOD;         p = p * n % MOD;       }       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T gcd(T a, T b) {     return b ? gcd(b, a % b) : a;   }   template <typename T>   inline T lowbit(T x) {     return x & -x;   }   const int N = 2e5 + 10;   int n, m;   int q;   int res;   int les[N], more[N];   bool vis[N];   void solve() {     cin >> n >> m;     while (m--) {       int u, v;       scanf( %d%d , &u, &v);       if (u > v)         les[u]++, more[v]++;       else         les[v]++, more[u]++;     }     for (int i = 1; i <= n; ++i) {       if (!more[i]) res++, vis[i] = 1;     }     cin >> q;     while (q--) {       int x;       scanf( %d , &x);       int a, b;       if (x == 1) {         scanf( %d%d , &a, &b);         if (a > b)           les[a]++, more[b]++;         else           les[b]++, more[a]++;         if (vis[a] && more[a]) vis[a] = 0, res--;         if (vis[b] && more[b]) vis[b] = 0, res--;       }       if (x == 2) {         scanf( %d%d , &a, &b);         if (a > b)           les[a]--, more[b]--;         else           les[b]--, more[a]--;         if (!vis[a] && !more[a]) res++, vis[a] = 1;         if (!vis[b] && !more[b]) res++, vis[b] = 1;       }       if (x == 3) {         cout << res << endl;       }     }   }   signed main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int nmax = 100005;   const long long inf = 1e9;   const double eps = 1e-6;   long long dp[1005][1005];   long long gp[1005][1005];   long long a[1005][1005];   long long rdp[1005][1005];   long long rgp[1005][1005];   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         cin >> a[i][j];       }     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + a[i][j];       }     }     for (int i = n; i; --i) {       for (int j = m; j; --j) {         rdp[i][j] = max(rdp[i][j + 1], rdp[i + 1][j]) + a[i][j];       }     }     for (int i = n; i; --i) {       for (int j = 1; j <= m; ++j) {         gp[i][j] = max(gp[i + 1][j], gp[i][j - 1]) + a[i][j];       }     }     for (int i = 1; i <= n; ++i) {       for (int j = m; j; --j) {         rgp[i][j] = max(rgp[i - 1][j], rgp[i][j + 1]) + a[i][j];       }     }     long long ans = 0;     for (int i = 2; i < n; ++i) {       for (int j = 2; j < m; ++j) {         ans =             max(ans, dp[i][j - 1] + rdp[i][j + 1] + gp[i + 1][j] + rgp[i - 1][j]);         ans =             max(ans, dp[i - 1][j] + rdp[i + 1][j] + gp[i][j - 1] + rgp[i][j + 1]);       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5e5 + 5;   template <typename T>   void chkmax(T& x, T y) {     if (y > x) x = y;   }   template <typename T>   void chkmin(T& x, T y) {     if (y < x) x = y;   }   int N, c[MAXN];   long long ans;   int main() {     scanf( %d , &N);     for (int i = 1; i <= N; ++i) scanf( %d , &c[i]);     for (int i = 1; i <= N; i += 2) {       long long minv = 0, sum = 0;       for (int j = i + 1; j <= N; ++j) {         long long l = -sum - c[i], r = -sum - max(1ll, -minv);         if (j % 2 == 1)           chkmax(l, 1ll), chkmin(r, (long long)c[j]);         else           chkmax(l, (long long)-c[j]), chkmin(r, -1ll);         ans += max(0ll, r - l + 1);         sum += j % 2 == 1 ? c[j] : -c[j];         chkmin(minv, sum);       }     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   const int B = 250;   int p1 = 5000000, p2 = 0;   char buf[5000005], wbuf[5000005];   int gc() {     if (p1 >= 5000000) fread(buf, 1, 5000000, stdin), p1 = 0;     return buf[p1++];   }   int rd() {     int x = 0, ch = gc();     while (!isdigit(ch)) ch = gc();     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = gc();     return x;   }   struct T {     int x, id;   } tmp[100005];   int bx[100005], bid[100005], cx[100005], cid[100005], nowans[100005];   int a[100005], pre[100005], bel[100005], S[100005], E[100005], dfn[100005],       tag[100005];   int size[100005], son[100005], n, m, fa[100005], is[100005], top[100005], sign,       sum = 1, ts = 0;   vector<int> g[100005];   const bool operator<(const T i, const T j) { return i.x < j.x; }   void dfs(int x) {     size[x] = 1;     for (int y : g[x]) {       dfs(y), size[x] += size[y];       if (size[y] > size[son[x]]) son[x] = y;     }   }   void dfs2(int x, int tp) {     top[x] = tp, dfn[x] = ++sign;     if (son[x])       dfs2(son[x], tp);     else {       int l = dfn[top[x]], r = dfn[x], t = 100;       if (r - l < 10) {         for (int i = l; i <= r; i++) bel[i] = sum;         if (r - ts >= 250) sum++, ts = r;         return;       }       if (sqrt(r - l + 1) >= t) t = sqrt(r - l + 1), t = min(t, 250);       for (int i = l; i <= r; i++) bel[i] = (i - l) / t + sum;       sum = bel[r] + 1, ts = r;     }     for (int y : g[x])       if (y ^ son[x]) dfs2(y, y);   }   void Make(int x) {     for (int s = S[bel[x]], e = E[bel[x]], i = s; i <= e; i++) {       if (bid[i] == x) {         int dlt = 1 - 2 * is[x];         while (i <= e) pre[i] += dlt, i++;       }     }     is[x] ^= 1;   }   void Addb(int p, int l, int r, int x) {     int tc = 0, s = S[p], e = E[p];     for (int i = s; i <= e; i++) bx[i] += tag[p];     tag[p] = 0, nowans[p] = -1;     int L = s, R = e;     while (bid[L] < l || bid[L] > r) L++;     while (bid[R] < l || bid[R] > r) R--;     while (L > s && bx[L - 1] == bx[L]) L--;     while (R < e && bx[R + 1] == bx[R]) R++;     for (int i = L, j; i <= R; i = j + 1) {       j = i;       while (j < R && bx[j + 1] == bx[i]) j++;       if (x == 1) {         for (int k = i; k <= j; k++)           if (bid[k] < l || bid[k] > r) cid[++tc] = bid[k], cx[tc] = bx[k];         for (int k = i; k <= j; k++)           if (bid[k] >= l && bid[k] <= r) cid[++tc] = bid[k], cx[tc] = bx[k] + 1;       } else {         for (int k = i; k <= j; k++)           if (bid[k] >= l && bid[k] <= r) cid[++tc] = bid[k], cx[tc] = bx[k] - 1;         for (int k = i; k <= j; k++)           if (bid[k] < l || bid[k] > r) cid[++tc] = bid[k], cx[tc] = bx[k];       }     }     for (int i = L; i <= R; i++) bx[i] = cx[i - L + 1], bid[i] = cid[i - L + 1];     for (int i = L; i <= e; i++) pre[i] = (i == s ? 0 : pre[i - 1]) + is[bid[i]];   }   void Add(int l, int r, int x) {     Addb(bel[l], l, r, x);     if (bel[l] == bel[r]) return;     Addb(bel[r], l, r, x);     for (int i = bel[l] + 1; i < bel[r]; i++) tag[i] += x, nowans[i] = -1;   }   int Query() {     int ans = 0;     for (int i = 1; i <= bel[n]; i++) {       if (nowans[i] != -1) {         ans += nowans[i];         continue;       }       int pos = lower_bound(bx + S[i], bx + E[i] + 1, -tag[i]) - bx - 1;       if (pos >= S[i])         ans += pre[pos], nowans[i] = pre[pos];       else         nowans[i] = 0;     }     return ans;   }   void Upd(int x) {     int p = dfn[x];     while (x) Add(dfn[top[x]], dfn[x], 1 - 2 * is[p]), x = fa[top[x]];     Make(p);   }   int main() {     n = rd(), m = rd();     for (int i = 2, x; i <= n; i++) x = rd(), fa[i] = x, g[x].push_back(i);     dfs(1), dfs2(1, 1);     for (int i = 1; i <= n; i++)       a[i] = rd(), is[i] = 1, tmp[dfn[i]] = {a[i], dfn[i]};     for (int i = 1; i <= n; i++) E[bel[i]] = i;     for (int i = n; i >= 1; i--) S[bel[i]] = i;     for (int i = 1; i <= bel[n]; i++) {       nowans[i] = -1;       int s = S[i], e = E[i];       sort(tmp + s, tmp + e + 1);       for (int j = s; j <= e; j++)         pre[j] = j - s + 1, bx[j] = tmp[j].x, bid[j] = tmp[j].id;     }     while (m--) {       int x = rd();       Upd(x);       printf( %d  , Query());     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   char s1[N], s2[N], s[N];   int n, lst, nod, len[N], fail[N], diff[N], anc[N], ch[N][26];   int f[N], pos[N], pre[N];   void init() {     fail[0] = fail[1] = nod = 1;     len[1] = -1;   }   void extend(int c, int i) {     int p = lst;     while (s[i - len[p] - 1] != s[i]) p = fail[p];     if (!ch[p][c]) {       int x = ++nod, k = fail[p];       while (s[i - len[k] - 1] != s[i]) k = fail[k];       fail[x] = ch[k][c];       ch[p][c] = x;       len[x] = len[p] + 2;       diff[x] = len[x] - len[fail[x]];       anc[x] = (diff[x] == diff[fail[x]]) ? anc[fail[x]] : fail[x];     }     lst = ch[p][c];   }   int main() {     scanf( %s%s , s1 + 1, s2 + 1);     n = strlen(s1 + 1);     init();     for (int i = 1; i <= n * 2; i++) s[i] = (i & 1) ? s1[(i + 1) / 2] : s2[i / 2];     for (int i = 1; i <= n * 2; i++) f[i] = 1e9;     for (int i = 1; i <= n * 2; i++) {       extend(s[i] -  a , i);       for (int k = lst; k; k = anc[k]) {         pos[k] = i - len[anc[k]] - diff[k];         if (anc[k] != fail[k] && f[pos[fail[k]]] < f[pos[k]])           pos[k] = pos[fail[k]];         if (!(i & 1) && f[i] > f[pos[k]] + 1)           f[i] = f[pos[k]] + 1, pre[i] = pos[k];       }       if (!(i & 1) && s[i] == s[i - 1] && f[i - 2] <= f[i])         f[i] = f[i - 2], pre[i] = i - 2;     }     if (f[n * 2] >= 1e9) return puts( -1 ), 0;     printf( %d n , f[n * 2]);     for (int i = n * 2; i; i = pre[i])       if (i - pre[i] > 2) printf( %d %d n , pre[i] / 2 + 1, i >> 1);   }


#include <bits/stdc++.h>   using namespace std;   int grid[33][33];   int main() {     int test;     scanf( %d , &test);     for (int _T = 1; _T <= test; _T++) {       int n, m;       cin >> n >> m;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cin >> grid[i][j];         }       }       int ans = 0;       for (int dist = 0; dist < (n + m - 1) / 2; dist++) {         int cnt1 = 0, cnt2 = 0;         for (int i = 0; i < n; i++) {           for (int j = 0; j < m; j++) {             if (i + j == dist) {               cnt1 += (grid[i][j] == 0);               cnt2 += (grid[i][j] == 1);             }           }         }         for (int i = 0; i < n; i++) {           for (int j = 0; j < m; j++) {             if ((n - 1 - i) + (m - 1 - j) == dist) {               cnt1 += (grid[i][j] == 0);               cnt2 += (grid[i][j] == 1);             }           }         }         ans += min(cnt1, cnt2);       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m, ave1, ave2, ans1, ans2;   int a[567890], sum;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), sum += a[i];     sort(a + 1, a + n + 1);     int ave1 = sum / n;     if (ave1 * n == sum)       ave2 = ave1;     else       ave2 = ave1 + 1;     for (int i = 1; i <= n; i++) {       if (a[i] < ave1) ans1 += (ave1 - a[i]);       if (a[i] > ave2) ans2 += (a[i] - ave2);     }     printf( %d , ans1 > ans2 ? ans1 : ans2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int GCD(int a, int b) {     if (b == 0) return a;     return (a % b == 0 ? b : GCD(b, a % b));   }   long long int POW(long long int base, long long int exp) {     long long int val;     val = 1;     while (exp > 0) {       if (exp % 2 == 1) {         val = (val * base) % 1000000007;       }       base = (base * base) % 1000000007;       exp = exp / 2;     }     return val;   }   vector<int> fib;   vector<int> ans;   int main() {     int s, k, i, n;     long long till;     scanf( %d%d , &s, &k);     fib.push_back(1);     till = 1;     for (i = 1; fib[i - 1] <= s; i++) {       fib.push_back(till);       if (i >= k) {         till = till + fib[i] - fib[i - k];       } else {         till = till + fib[i];       }     }     n = fib.size();     ans.push_back(0);     for (i = n - 1; i >= 1; i--) {       if (s >= fib[i]) {         s = s - fib[i];         ans.push_back(fib[i]);       }     }     n = ans.size();     printf( %d n , n);     for (i = 0; i < n; i++) {       printf( %d  , ans[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, m;     scanf( %d %d , &n, &m);     int c[n], a[m];     int i;     for (i = 0; i < n; i++) {       scanf( %d , &c[i]);     }     for (i = 0; i < m; i++) {       scanf( %d , &a[i]);     }     int j = 0, count = 0;     int t = 0;     for (i = 0; i < m;) {       if (j >= n) break;       for (j = t; j < n; j++) {         if (i >= m) {           break;         }         if (a[i] >= c[j]) {           i = i + 1;           t = j + 1;           count = count + 1;         }       }     }     printf( %d n , count);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   int c[maxn], ans[maxn];   int k, n, maxb, t;   int b[maxn];   void update(int x, int d) {     for (; x <= maxb; x += x & -x) {       if (d > c[x]) c[x] = d;     }   }   int get(int x) {     int ans = 0;     for (; x > 0; x -= x & -x)       if (c[x] > ans) ans = c[x];     return ans;   }   void solve() {     if (t >= maxb) {       sort(b, b + n);       cout << unique(b, b + n) - b << endl;     } else {       int x;       memset(c, 0, sizeof(c));       memset(ans, 0, sizeof(ans));       for (int i = 0; i < t; i++) {         for (int j = 0; j < n; j++) {           x = get(b[j] - 1) + 1;           if (x > ans[j]) {             ans[j] = x;             update(b[j], x);           }         }       }       cout << *max_element(ans, ans + n) << endl;     }   }   int main() {     cin >> k >> n >> maxb >> t;     for (int i = 0; i < k; i++) {       for (int j = 0; j < n; j++) scanf( %d , b + j);       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 10;   int e[maxn], ne[maxn], h[maxn], idx = 0;   int sz[maxn];   int ans = 0;   void add(int a, int b) {     e[idx] = b, ne[idx] = h[a];     h[a] = idx++;   }   void dfs(int now, int fa) {     sz[now] = 1;     for (int i = h[now]; i != -1; i = ne[i]) {       int j = e[i];       if (j == fa) continue;       dfs(j, now);       sz[now]++;     }   }   int dfs2(int now, int fa) {     int d1 = 0, d2 = 0;     for (int i = h[now]; i != -1; i = ne[i]) {       int j = e[i];       if (j == fa) continue;       int d = dfs2(j, now);       if (d > d1)         d2 = d1, d1 = d;       else if (d > d2)         d2 = d;     }     if (d1 >= 1 && d2 >= 1) {       if (now != 1)         ans = max(ans, d1 + d2 + sz[now] - 1);       else         ans = max(ans, d1 + d2 + sz[now] - 2);     } else if (d2 == 0 && d1 >= 1) {       if (now != 1)         ans = max(ans, d1 + sz[now]);       else         ans = max(ans, d1 + sz[now] - 1);     }     return (d1 == 0 ? 1 : d1 + sz[now] - 1);   }   int main() {     int t;     scanf( %d , &t);     memset(h, -1, sizeof h);     while (t--) {       int n;       scanf( %d , &n);       idx = 0;       ans = 0;       for (int i = 1; i <= n - 1; i++) {         int a, b;         scanf( %d%d , &a, &b);         add(a, b);         add(b, a);       }       dfs(1, -1);       dfs2(1, -1);       cout << ans << endl;       for (int i = 1; i <= n; i++) h[i] = -1, sz[i] = 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int sum = 0, mx = 0;     node *left = nullptr, *right = nullptr;     node *getLeft() {       if (left == nullptr) left = new node;       return left;     }     node *getRight() {       if (right == nullptr) right = new node;       return right;     }     void setValue(int k, int v, int a = 0, int b = 1 << 30) {       if (b == a + 1)         mx = sum = v;       else if (k < (a + b) / 2) {         getLeft()->setValue(k, v, a, (a + b) / 2);         mx = ::max(mx, left->mx + sum);       } else {         getRight()->setValue(k, v, (a + b) / 2, b);         mx = ::max(mx, right->mx + sum);       }     }     void insert(int pa, int pb, int a = 0, int b = 1 << 30) {       if (a >= pb || b <= pa) return;       if (a >= pa && b <= pb) {         sum++;         mx++;       } else {         mx = 0;         if (left) {           left->insert(pa, pb, a, (a + b) / 2);           mx = ::max(mx, left->mx);         }         if (right) {           right->insert(pa, pb, (a + b) / 2, b);           mx = ::max(mx, right->mx);         }         mx += sum;       }     }     int max(int pa, int pb, int a = 0, int b = 1 << 30) {       if (a >= pb || b <= pa) return 0;       if (a >= pa && b <= pb) return mx;       int ans = 0;       if (left) ans = ::max(ans, left->max(pa, pb, a, (a + b) / 2));       if (right) ans = ::max(ans, right->max(pa, pb, (a + b) / 2, b));       return ans + sum;     }   };   int main() {     int n, r, h, t;     node *tree = new node;     priority_queue<pair<int, int> > pq;     scanf( %d %d , &n, &r);     for (int i = 0; i < n; i++) {       scanf( %d %d , &h, &t);       if (t < 0) continue;       h = max(h, 0);       pq.push(make_pair(-t, -h));       pq.push(make_pair(-h, 2));       pq.push(make_pair(-(t + 1), 2));     }     int prev = -1;     while (!pq.empty()) {       int a = -pq.top().second, b = -pq.top().first;       pq.pop();       if (a < 0) {         if (b == prev) continue;         int v = tree->max(0, b - r + 1);         if (a == -2 || v > tree->max(prev, prev + 1)) {           tree->setValue(b, v);           prev = b;           if (b + r <= 1000000000) pq.push(make_pair(-(b + r), 1));         }       } else         tree->insert(a, b + 1);       if (tree->mx > n) puts( wtf );     }     printf( %d n , tree->mx);   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   const double eps = 0.01;   void solution() {     int n;     cin >> n;     vector<int> h(n + 1);     map<pair<int, int>, int> data;     map<pair<int, int>, int>::iterator iter;     for (int i = 1; i <= n; i++) {       cin >> h[i];       data[make_pair(h[i], i)]++;     }     vector<pair<int, int> > coll;     int place = 0;     for (iter = data.begin(); iter != data.end(); iter++) {       for (int j = 0; j < iter->second; j++) {         place++;         coll.push_back(make_pair(place, iter->first.second));       }     }     for (int i = 0; i < n; i++) {       if (coll[i].first > coll[i].second) {         swap(coll[i].first, coll[i].second);       }     }     sort(coll.begin(), coll.end());     int L = coll[0].first;     int R = coll[0].second;     int answer = 0;     for (int i = 1; i < n; i++) {       int l2 = coll[i].first;       int r2 = coll[i].second;       if (l2 <= R) {         R = max(R, r2);       } else {         L = l2;         R = r2;         answer++;       }     }     cout << answer + 1 <<   n ;   }   void include_file() {     freopen( input.txt ,  r , stdin);     freopen( output.txt ,  w , stdout);   }   int main() {     solution();     return 0;   }


#include <bits/stdc++.h>   int grid[1001][1001];   int start[100000];   int main(void) {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %d , &grid[i][j]);       }     }     for (int i = 0; i < k; i++) {       scanf( %d , &start[i]);     }     for (int i = 0; i < k; i++) {       int r = 1;       int c = start[i];       while (r <= n) {         int rr, cc;         switch (grid[r][c]) {           case 1:             rr = r;             cc = c + 1;             break;           case 2:             rr = r + 1;             cc = c;             break;           case 3:             rr = r;             cc = c - 1;             break;         }         grid[r][c] = 2;         r = rr;         c = cc;       }       printf( %d  , c);     }   }


#include <bits/stdc++.h>   using namespace std;   static const double EPS = 1e-10;   const int MODULO = 1000000007;   typedef struct {     int i, m, v;   } P;   vector<P> mv;   bool mLt(const P& x, const P& y) {     return (x.m != y.m) ? less<int>()(x.m, y.m) : less<int>()(x.v, y.v);   }   int n, k, h;   bool C(double t) {     int i = 1;     for (__typeof__((mv).begin()) it = (mv).begin(); it != (mv).end(); ++it) {       if ((double)i * h <= (*it).v * t) i++;       if (i > k) break;     }     return i > k;   }   int main() {     cin >> n >> k >> h;     mv.resize(n);     for (int i = 0; i < n; i++) mv[i].i = i + 1;     for (int i = 0; i < n; i++) cin >> mv[i].m;     for (int i = 0; i < n; i++) cin >> mv[i].v;     sort(mv.begin(), mv.end(), mLt);     double l = 0, r = 1e10;     double t;     for (int i = 0; i < 100; i++) {       t = (l + r) / 2;       if (C(t))         r = t;       else         l = t;     }     int cnt = 1;     for (__typeof__((mv).begin()) it = (mv).begin(); it != (mv).end(); ++it) {       if ((double)cnt * h <= (*it).v * t * (1 + EPS)) {         printf( %d  , (*it).i);         cnt++;       }       if (cnt > k) break;     }     puts(  n );     return 0;   }


#include <bits/stdc++.h>   int bus_bin(int[], int, int, int);   int main() {     int n, k, q, i, j, l, y, aux, existeB;     scanf( %d%d , &n, &k);     int monedas[n];     for (i = 0; i < n; i++) scanf( %d , &monedas[i]);     scanf( %d , &q);     int cant[q];     for (i = 0; i < q; i++) scanf( %d , &cant[i]);     for (i = 0; i < q; i++) {       aux = cant[i];       existeB = bus_bin(monedas, 0, n - 1, aux);       if (existeB) {         cant[i] = 1;       } else {         cant[i] = k + 1;         for (y = 0; y < n && monedas[y] <= aux; y++) {           for (j = 1; j <= k && j * monedas[y] <= aux; j++) {             if (aux - j * monedas[y] == 0) {               if (j < cant[i]) cant[i] = j;             } else {               for (l = 1; l + j <= k; l++) {                 existeB = bus_bin(monedas, 0, n - 1, (aux - j * monedas[y]) / l);                 if ((aux - j * monedas[y]) % l == 0 && existeB) {                   if (j + l < cant[i]) cant[i] = j + l;                   break;                 }               }             }           }         }       }     }     for (i = 0; i < q; i++) {       printf( %d n , cant[i] > k ? -1 : cant[i]);     }     return 0;   }   int bus_bin(int v[], int ini, int fin, int x) {     int medio;     medio = (ini + fin) / 2;     if (v[medio] == x)       return 1;     else if (ini > fin)       return 0;     else if (x > v[medio])       return bus_bin(v, medio + 1, fin, x);     else if (x < v[medio])       return bus_bin(v, ini, medio - 1, x);   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   bool IsPowerOfTwo(long long int x) { return (x != 0) && ((x & (x - 1)) == 0); }   long long int poer(long long int a, long long int b) {     long long int rs = 1;     while (b > 0) {       if (b & 1) rs *= a;       a *= a;       b /= 2;     }     return rs;   }   long long int mod_poer(long long int a, long long int b, long long int mo) {     long long int rs = 1;     while (b > 0) {       if (b & 1) rs = (rs * a) % mo;       a = (a * a) % mo;       b /= 2;     }     return rs % mo;   }   long long int d, x, y;   void ext_euclid(long long int a, long long int b) {     if (b == 0) {       d = a;       x = 1;       y = 0;     } else {       ext_euclid(b, a % b);       long long int temp = x;       x = y;       y = temp - (a / b) * y;     }   }   long long int mod_inverse1(long long int a, long long int m) {     ext_euclid(a, m);     return (x % m + m) % m;   }   long long int mod_inverse2(long long int a, long long int m) {     return mod_poer(a, m - 2, m);   }   long long int gcd(long long int a, long long int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   long long int fact[200005];   void fac() {     fact[1] = fact[0] = 1;     for (long long int i = 2; i <= 200005; ++i) {       fact[i] = ((fact[i - 1] % mod) * i) % mod;     }   }   long long int combi(long long int a, long long int b) {     if (b > a) return 0;     if (b == a)       return 1;     else {       long long int x = mod_poer(fact[b], mod - 2, mod) % mod;       long long int y = mod_poer(fact[a - b], mod - 2, mod) % mod;       return ((((fact[a]) * x) % mod) * y) % mod;     }   }   long long int len[54];   string str1 =  What are you doing while sending   ;   string str2 =   ? Are you busy? Will you send   ;   string str3 =   ? ;   long long int len1 = str1.size();   long long int len2 = str2.size();   long long int len3 = str3.size();   string f0 =        What are you doing at the end of the world? Are you busy? Will you save          us? ;   char get(long long int n, long long int k) {     if (n > 54) {       if (k <= len1) return str1[k - 1];       return get(n - 1, k - len1);     }     if (n < 54 && k > len[n]) return  . ;     if (n == 0) return f0[k - 1];     if (k <= len1) return str1[k - 1];     if (k <= len1 + len[n - 1]) return get(n - 1, k - len1);     if (k <= len1 + len[n - 1] + len2) return str2[k - len1 - len[n - 1] - 1];     if (k <= len1 + len[n - 1] * 2 + len2)       return get(n - 1, k - len1 - len[n - 1] - len2);     return str3[k - len1 - len[n - 1] * 2 - len2 - 1];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     len[0] = f0.size();     for (long long int i = 1; i < 54; ++i) {       len[i] = (len[i - 1] * 2) + len1 + len2 + len3;     }     long long int t;     cin >> t;     while (t--) {       long long int n, k;       cin >> n >> k;       cout << get(n, k);     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void getint(int &v) {     char ch, fu = 0;     for (ch =  * ; (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar())       ;     if (ch ==  - ) fu = 1, ch = getchar();     for (v = 0; ch >=  0  && ch <=  9 ; ch = getchar()) v = v * 10 + ch -  0 ;     if (fu) v = -v;   }   int n, k, a[1000010], ans;   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     for (int i = 1; i <= k; ++i) ans += a[i];     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[28 * 2];   bitset<1250005> bleh[28][28 * 2];   int sm;   vector<int> in[2];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 0; i < (signed)(n); i++) {       cin >> a[i];       sm += a[i];     }     for (int i = 0; i < (signed)(n); i++) {       cin >> a[i + n];       sm += a[i + n];     }     sort(a, a + 2 * n);     bleh[0][1][0] = true;     for (int i = (2); i <= (signed)(2 * n - 1); i++) {       bleh[0][i] = bleh[0][i - 1];       for (int j = (n - 1); j >= 1; j--) {         bleh[j][i] = bleh[j][i - 1] | (bleh[j - 1][i - 1] << a[i]);         for (int k = (0); k <= (signed)(400); k++) {           if (bleh[j][i][k]) {           }         }       }     }     int tot = sm - a[0] - a[1];     int wtmoo = tot / 2;     while (!bleh[n - 1][2 * n - 1][wtmoo]) {       --wtmoo;     }     int cur = wtmoo;     int cnt = n - 1;     for (int i = (2 * n - 2); i >= 1; i--) {       if (bleh[cnt][i][cur]) {         in[0].push_back(a[i + 1]);       } else {         in[1].push_back(a[i + 1]);         --cnt;         cur -= a[i + 1];       }     }     in[0].push_back(a[0]);     in[1].push_back(a[1]);     sort(in[0].begin(), in[0].end());     sort(in[1].begin(), in[1].end(), greater<int>());     for (int i = 0; i < (signed)(2); i++) {       for (int j = 0; j < (signed)(n); j++) {         cout << in[i][j] <<    ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = 3.14159265358979323846;   const double eps = (1e-5);   long long isGreater(long double x, long double y) {     if (abs(x - y) < eps) return 0;     if (x > y) return 1;     return -1;   }   long long n;   bool valid(long long i) { return i >= 0 && i < n; }   long long getSum(const vector<long long> &cum, long long l, long long r) {     long long ret = 0;     if (valid(r)) ret += cum[r];     if (valid(l - 1)) ret -= cum[l - 1];     return ret;   }   long long bestAns;   long long solve(vector<long long> a, long long d) {     long long sum = 0;     for (long long i = 0; i < (long long)(a).size(); ++i) {       a[i] = a[i] % d;       sum += a[i];       sum %= d;     }     if (sum != 0) return LLONG_MAX;     sum = 0;     long long size = 0;     long long ans = 0;     for (long long i = 0; i < (long long)(a).size(); ++i) {       sum += a[i];       size++;       if (sum >= d) {         long long extra = sum - d;         a[i] -= extra;         long long st = i - size + 1, en = i;         long long minSum = LLONG_MAX, temp = 0, inc = 0, dec = d;         for (long long j = st; j <= en; ++j) temp += 1LL * a[j] * (j - st + 1);         for (long long mid = st; mid <= en; ++mid) {           temp += inc;           temp -= dec;           dec -= a[mid];           inc += a[mid];           minSum = min(minSum, temp);         }         ans += minSum;         if (ans > bestAns) break;         a[i] = extra;         size = 0;         sum = 0;         --i;       }     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     vector<long long> a(n);     long long sum = 0;     for (long long i = 0; i < n; ++i) {       cin >> a[i];       sum += a[i];     }     if (sum == 1) {       cout << -1;       return 0;     }     bestAns = solve(a, sum);     for (long long i = 2; i <= sum / i; ++i) {       if (sum % i == 0) {         bestAns = min(bestAns, solve(a, i));         while (sum % i == 0) {           sum /= i;         }       }     }     if (sum > 1) bestAns = min(bestAns, solve(a, sum));     cout << bestAns;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   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 << 3) + (x << 1) + f * (c -  0 ), c = getchar();     return x;   }   const long long MAXN = 1e5 + 10;   long long n, m;   double f[MAXN];   double ans;   void ycl() {     long long x = n;     double a[MAXN], b[MAXN];     for (long long i = 1; i <= m; i++) a[i] = (double)i / m, b[i] = 1;     while (x > 0) {       if (x % 2 == 1) {         for (long long i = 1; i <= m; i++) b[i] *= a[i];       }       for (long long i = 1; i <= m; i++) a[i] *= a[i];       x /= 2;     }     for (long long i = 1; i <= m; i++) f[i] = b[i];   }   signed main() {     m = read();     n = read();     ycl();     for (long long i = 1; i <= m; i++) ans += (double)(f[i] - f[i - 1]) * i;     printf( %lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v(110, 0);   int main() {     int n, a, flag = 0, b;     vector<int> m;     cin >> n;     b = n;     while (n--) {       cin >> a;       m.push_back(a);       v[a]++;     }     sort(m.begin(), m.end());     for (int i = m[0]; i <= m[b - 1]; i++) {       for (int j = m[0]; j <= m[b - 1]; j++) {         if (i != j && v[i] == v[j] && (v[i] + v[j]) == b) {           flag = 1;           cout <<  YES  << endl << i <<     << j << endl;           break;         }       }       if (flag) break;     }     if (!flag) cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int time = INT_MAX, check = 0;     for (int i = 0; i < n; i++) {       check = 0;       int no = a[i];       for (int j = 0; j < no; j++) {         int item;         cin >> item;         check += item * 5;       }       check += no * 15;       time = min(time, check);     }     cout << time;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (1 << 12);   int cnt[maxn];   pair<int, int> res[maxn][maxn];   int pr[maxn][maxn];   int w[13];   int len;   int bsearch(int i, int k) {     int l = 0, r = len;     while (r - l > 1) {       int mid = (r + l) / 2;       if (res[i][mid].first <= k)         l = mid;       else         r = mid;     }     return l;   }   bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }   void counting_sort(int i) {     vector<int> cnts[1205];     for (int j = 0; j < len; j++) {       cnts[res[i][j].first].push_back(res[i][j].second);     }     int j = 0;     for (int k = 0; k < 1204; k++) {       while (!cnts[k].empty()) {         res[i][j] = {k, cnts[k].back()};         cnts[k].pop_back();         j++;       }     }   }   int main() {     int n, m, q;     scanf( %d %d %d , &n, &m, &q);     len = (1 << n);     for (int i = 0; i < n; i++) {       scanf( %d , &w[n - i - 1]);     }     for (int i = 0; i < m; i++) {       int num = 0;       for (int j = 0; j < n; j++) {         char c = getchar();         while (c !=  0  && c !=  1 ) {           c = getchar();         }         num *= 2;         num += (c -  0 );       }       cnt[num]++;     }     for (int i = 0; i < (1 << n); i++) {       for (int j = 0; j < (1 << n); j++) {         int ans = 0;         for (int k = 0; k < n; k++) {           if (((1 << k) & i) == ((1 << k) & j)) ans += w[k];         }         res[i][j] = {ans, cnt[j]};       }       counting_sort(i);       pr[i][0] = res[i][0].second;       for (int j = 1; j < (1 << n); j++) {         pr[i][j] = pr[i][j - 1] + (res[i][j].second);       }     }     for (int i = 0; i < q; i++) {       int num = 0;       for (int j = 0; j < n; j++) {         char c = getchar();         while (c !=  0  && c !=  1 ) {           c = getchar();         }         num *= 2;         num += (c -  0 );       }       int k;       scanf( %d , &k);       int ind = bsearch(num, k);       printf( %d n , pr[num][ind]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int m, n, i;   long long int a[1000000], ans, sum[1000000] = {0}, x;   int check(long long int k) {     x = 0;     for (i = 1; i <= n; i++) {       if (a[i] - (i - 1) / k > 0) x = x + a[i] - (i - 1) / k;     }     if (x >= m)       return 1;     else       return 0;   }   int binsearch(long long int low, long long int high) {     while (low < high) {       long long int mid = (low + high) / 2;       if (check(mid))         high = mid;       else         low = mid + 1;     }     if (!check(low)) low = -1;     return low;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + 1 + n, greater<int>());     for (i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];     ans = binsearch(1, n);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string str[101];     int n;     cin >> n;     for (int i = 0; i < n; ++i) cin >> str[i];     int maxi = 1;     for (int i = 0; i < n - 1; ++i) {       int m = 1;       for (int j = i + 1; j < n; ++j) {         if (str[i] == str[j]) ++m;       }       maxi = max(m, maxi);     }     cout << maxi << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 3e5 + 5, INF = 1e9;   string s2, s;   struct data {     int next[30];     int cnt;     bool least;   };   data t[N];   int sz = 1, ans;   string p;   inline void add(string s) {     int v = 0;     t[v].cnt++;     for (int i = 0; i < s.size(); ++i) {       int x = s[i] -  a ;       if (t[v].next[x] == -1) {         t[v].next[x] = sz;         sz++;       }       v = t[v].next[x];       t[v].cnt++;     }     t[v].least = true;   }   map<string, bool> usd;   bool flag;   inline void f(int &pos) {     int v = 0;     int c = -1;     int k = 0;     if (t[v].next[s[pos] -  a ] == -1) {       pos--;       return;     }     while (s[pos] >=  a  && s[pos] <=  z ) {       int x = s[pos] -  a ;       if (t[v].next[x] == -1) break;       k++;       if (k >= 2 && t[v].cnt == 1) c++;       p += s[pos];       v = t[v].next[x];       pos++;     }     pos--;     ans += k;     if (c != -1 && t[v].least == 1) ans -= c;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     for (int i = 0; i <= 300000; ++i) {       for (int j = 0; j <= 26; ++j) {         t[i].next[j] = -1;       }       t[i].cnt = 0;       t[i].least = false;     }     while (getline(cin, s2)) {       s += s2;       s +=    ;     }     for (int i = 0; i < s.size(); ++i) {       if (!flag && s[i] >=  a  && s[i] <=  z )         flag = true, f(i);       else {         ans++;         if (s[i] >=  a  && s[i] <=  z )           p += s[i];         else if (p !=   ) {           if (!usd[p]) add(p), usd[p] = true;           p =   ;         }         if (s[i] <  a  || s[i] >  z ) flag = false;       }     }     cout << ans;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx )   using namespace std;   const int MAXN = 1e5 + 9;   const int MOD = (int)(1e9 + 7);   const int LOG2MAXN = 17;   const int INF = 1e9;   const long double eps = 1e-9;   int di[] = {0, 1, 0, -1};   int dj[] = {1, 0, -1, 0};   char ddir[] = { R ,  U ,  L ,  D };   mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());   int n, m;   vector<vector<int> > gg;   bool used[MAXN];   void cry() {     cout <<  NO ;     exit(0);   }   int ans[111][111];   char cc[111][111];   void solve() {     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) cin >> cc[i][j];     int mx = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         bool ok = 1;         for (int tmp = 0; tmp < k; tmp++)           if (i + tmp >= n || cc[i + tmp][j] ==  # ) ok = 0;         if (ok)           for (int tmp = 0; tmp < k; tmp++) ans[i + tmp][j]++;         ok = 1;         for (int tmp = 0; tmp < k; tmp++)           if (j + tmp >= n || cc[i][j + tmp] ==  # ) ok = 0;         if (ok)           for (int tmp = 0; tmp < k; tmp++) ans[i][j + tmp]++;         mx = max(mx, ans[i][j]);       }     }     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         if (ans[i][j] == mx) {           cout << i + 1 <<     << j + 1 << endl;           return;         }   }   void input() {}   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     input();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, f[105], s[105], ans;   int Getf(int x) { return (f[x] == x ? x : Getf(f[x])); }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) f[i] = i, s[i] = 1;     int x, y;     for (int i = 0; i < m; i++) {       scanf( %d%d , &x, &y);       int tx = Getf(x), ty = Getf(y);       if (tx == ty && (s[ty] & 1)) ans++;       f[tx] = ty;       s[ty] += s[tx];     }     if ((n - ans) % 2 != 0) ans++;     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, i, left, right, p[52];     vector<long long> bin;     cin >> n >> m;     m--;     while (m) {       if (m & 1)         bin.push_back(1);       else         bin.push_back(0);       m /= 2;     }     for (i = bin.size(); i < n - 1; i++) bin.push_back(0);     reverse(bin.begin(), bin.end());     left = 0;     right = n - 1;     for (i = 0; i < n - 1; i++) {       if (bin[i]) {         p[right] = i + 1;         right--;       } else {         p[left] = i + 1;         left++;       }     }     p[left] = i + 1;     for (i = 0; i < n - 1; i++) cout << p[i] <<    ;     cout << p[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, l, r;     cin >> n >> l >> r;     long long mn[l], t1 = 1, mx[r], sum1 = 0, sum2 = 0;     for (int i = 0; i < l; i++) {       mn[i] = t1;       sum1 += mn[i];       t1 *= 2;     }     for (int i = 0; i < n - l; i++) {       sum1 += mn[0];     }     t1 = 1;     for (int i = 0; i < r; i++) {       mx[i] = t1;       t1 *= 2;       sum2 += mx[i];     }     for (int i = 0; i < n - r; i++) {       sum2 += mx[r - 1];     }     cout << sum1 <<     << sum2;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m;   void solve() {     cin >> n >> m;     vector<vector<long long int>> v(m, vector<long long int>(n));     vector<long long int> sum(n);     for (long long int i = 0; i < (m); i++) {       for (long long int j = 0; j < (n); j++) {         cin >> v[i][j];       }     }     vector<long long int> ans(m);     for (long long int i = 1; i < (m + 1); i++) ans[i - 1] = i;     for (long long int i = 0; i < (n - 1); i++) {       vector<pair<long long int, long long int>> p(m);       long long int diff = 0;       for (long long int j = 0; j < (m); j++) {         p[j] = make_pair(v[j][i] - v[j][n - 1], j);         diff += p[j].first;       }       sort((p).begin(), (p).end());       vector<long long int> newans;       for (long long int w = 0; w < (m); w++) {         if (diff >= 0) break;         newans.push_back(p[w].second + 1);         diff -= p[w].first;       }       if (newans.size() < ans.size()) {         ans = newans;       }     }     cout << ans.size() << endl;     for (long long int x : ans) {       cout << x <<    ;     }     cout << endl;   }   int main() {     long long int t;     t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long A, B, C, D, X, Y;   inline long long gcd(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     long long res = gcd(b, a % b, x, y);     long long t = x;     x = y;     y = t - (a / b) * y;     return res;   }   int main() {     scanf( %lld %lld %lld , &A, &B, &C);     C = -C;     D = gcd(A, B, X, Y);     if (C % D)       puts( -1 );     else {       C /= D;       printf( %lld %lld n , X * C, Y * C);     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   set<pair<int, int> > z[200010], f[200010];   map<int, int> use[200010];   int n, m, k, dir;   int sx, sy;   inline void insert(int x, int y) {     z[x - y + m + 1].insert(make_pair(x, y));     f[x + y].insert(make_pair(x, y));     use[x][y] = 1;   }   void init() {     char s[3];     scanf( %d%d%d , &n, &m, &k);     for (int i = 1, x, y; i <= k; i++) {       scanf( %d%d , &x, &y);       insert(x, y);     }     scanf( %d%d%s , &sx, &sy, s);     dir = (s[1] ==  E ) | ((s[0] ==  S ) << 1);     for (int i = 0; i <= n + 1; i++) insert(i, 0), insert(i, m + 1);     for (int i = 0; i <= m + 1; i++) insert(0, i), insert(n + 1, i);   }   void go(int x, int y, int d, int &x1, int &y1, int &d1, int &px, int &py) {     int a, b;     if (d == 0) {       set<pair<int, int> >::iterator p =           z[x - y + m + 1].lower_bound(make_pair(x, y));       p--;       px = p->first;       py = p->second;       a = use[px + 1].count(py);       b = use[px].count(py + 1);       if (!a && b) {         x1 = px + 1;         y1 = py;         d1 = 2;       } else if (a && !b) {         x1 = px;         y1 = py + 1;         d1 = 1;       } else {         x1 = px + 1;         y1 = py + 1;         d1 = 3;       }     } else if (d == 1) {       set<pair<int, int> >::iterator p = f[x + y].lower_bound(make_pair(x, y));       p--;       px = p->first;       py = p->second;       a = use[px + 1].count(py);       b = use[px].count(py - 1);       if (!a && b) {         x1 = px + 1;         y1 = py;         d1 = 3;       } else if (a && !b) {         x1 = px;         y1 = py - 1;         d1 = 0;       } else {         x1 = px + 1;         y1 = py - 1;         d1 = 2;       }     } else if (d == 2) {       set<pair<int, int> >::iterator p = f[x + y].upper_bound(make_pair(x, y));       px = p->first;       py = p->second;       a = use[px - 1].count(py);       b = use[px].count(py + 1);       if (!a && b) {         x1 = px - 1;         y1 = py;         d1 = 0;       } else if (a && !b) {         x1 = px;         y1 = py + 1;         d1 = 3;       } else {         x1 = px - 1;         y1 = py + 1;         d1 = 1;       }     } else {       set<pair<int, int> >::iterator p =           z[x - y + m + 1].upper_bound(make_pair(x, y));       px = p->first;       py = p->second;       a = use[px - 1].count(py);       b = use[px].count(py - 1);       if (!a && b) {         x1 = px - 1;         y1 = py;         d1 = 1;       } else if (a && !b) {         x1 = px;         y1 = py - 1;         d1 = 2;       } else {         x1 = px - 1;         y1 = py - 1;         d1 = 0;       }     }   }   void solve() {     long long ans = 0;     int rep = 0;     int xx, yy, dd, px, py;     go(sx, sy, dir, sx, sy, dir, px, py);     for (int x = sx, y = sy, d = dir;; x = xx, y = yy, d = dd) {       go(x, y, d, xx, yy, dd, px, py);       ans += abs(x - px);       if (xx == sx && yy == sy && dd == dir) break;       if (dd + d == 3) rep = 1;     }     if (rep) ans >>= 1;     printf( %I64d n , ans);   }   int main() {     init();     solve();     return 0;   }