#include <bits/stdc++.h>   using namespace std;   int nxt[500010 << 1], to[500010 << 1], head[500010], cnt;   int n, m;   struct LCT {     int ch[500010][2], fa[500010], cnt;     long long val[500010], sum[500010], s[500010];     void init(int n) {       for (int i = 1; i <= n; i++) s[i] = 1;     }     bool is_root(int u) {       return !fa[u] || (ch[fa[u]][0] != u && ch[fa[u]][1] != u);     }     void update(int u) {       s[u] = val[u] + 1;       if (ch[u][0]) s[u] += s[ch[u][0]];       if (ch[u][1]) s[u] += s[ch[u][1]];     }     void rotate(int u) {       int f = fa[u], ff = fa[f];       int k = ch[f][1] == u, v = ch[u][!k];       if (!is_root(f)) ch[ff][ch[ff][1] == f] = u;       ch[f][k] = v;       ch[u][!k] = f;       if (v) fa[v] = f;       fa[f] = u;       fa[u] = ff;       update(f), update(u);     }     void splay(int x) {       while (!is_root(x)) {         int f = fa[x], ff = fa[f];         if (!is_root(f)) rotate((ch[ff][1] == f) ^ (ch[f][1] == x) ? x : f);         rotate(x);       }     }     void access(int x) {       for (int y = 0; x; y = x, x = fa[x]) {         splay(x);         int &v = ch[x][1];         if (v) val[x] += s[v], sum[x] += 1ll * s[v] * s[v];         v = y;         if (v) val[x] -= s[v], sum[x] -= 1ll * s[v] * s[v];       }     }     int find_root(int x) {       access(x);       splay(x);       while (ch[x][0]) x = ch[x][0];       splay(x);       return x;     }     void link(int x, int y) {       access(y);       splay(y);       splay(x);       fa[x] = y;       val[y] += s[x];       sum[y] += 1ll * s[x] * s[x];       update(y);     }     void cut(int x, int y) {       access(y);       splay(y);       splay(x);       fa[x] = 0;       val[y] -= s[x];       sum[y] -= 1ll * s[x] * s[x];       update(y);     }   } t;   int fa[500010];   void dfs(int u, int pre) {     fa[u] = pre;     for (int i = head[u]; i; i = nxt[i])       if (to[i] != pre) dfs(to[i], u);   }   void add(int u, int v) {     nxt[++cnt] = head[u];     to[cnt] = v;     head[u] = cnt;   }   vector<pair<int, int> > q[500010];   bool df[500010];   long long answer(int u) {     int f = t.find_root(u);     if (df[f])       return 1l * t.s[t.ch[f][1]] * t.s[t.ch[f][1]];     else       return 1ll * t.s[f] * t.s[f];   }   long long change(int u) {     df[u] ^= 1;     if (df[u]) {       if (u == 1) {         t.access(1);         return t.sum[1] - 1ll * t.s[1] * t.s[1];       }       long long r = -answer(u);       t.cut(u, fa[u]);       t.access(u);       r += t.sum[u];       if (!df[fa[u]]) r += answer(fa[u]);       return r;     } else {       if (u == 1) {         t.access(1);         return 1ll * t.s[1] * t.s[1] - t.sum[1];       }       t.access(u);       long long r = -t.sum[u];       if (!df[fa[u]]) r -= answer(fa[u]);       t.link(u, fa[u]);       r += answer(u);       return r;     }   }   long long ans[500010];   void solve(int col) {     for (int i = 0; i < q[col].size(); i++)       ans[q[col][i].first] += change(q[col][i].second);     for (int i = (int)q[col].size() - 1; i >= 0; i--) change(q[col][i].second);   }   int col[500010];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &col[i]);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d%d , &u, &v);       add(u, v), add(v, u);     }     dfs(1, 0);     t.init(n);     for (int i = 1; i <= n; i++) t.link(i, fa[i]);     for (int i = 1; i <= n; i++) q[col[i]].push_back(make_pair(0, i));     for (int i = 1; i <= m; i++) {       int u, v;       scanf( %d%d , &u, &v);       q[col[u]].push_back(make_pair(i, u));       col[u] = v;       q[col[u]].push_back(make_pair(i, u));     }     for (int i = 1; i <= n; i++) solve(i);     for (int i = 1; i <= m; i++) ans[i] += ans[i - 1];     for (int i = 0; i <= m; i++) printf( %lld n , -ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long f(long long n) {     if (n < 10) return n;     long long last_digit = n % 10;     long long ret = n / 10 + 9;     long long tmp = n;     while (tmp >= 10) tmp = tmp / 10;     if (tmp > last_digit) ret--;     return ret;   }   int main() {     long long i, j, k, n, m, d, a, b;     while (cin >> a >> b) {       cout << f(b) - f(a - 1) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 400010, inf = 1e18;   long long q, lastans, cnt = 1, w[maxn];   long long fa[maxn][21], sum[maxn][21];   void add(long long x, long long v) {     w[++cnt] = v;     if (w[cnt] <= w[x])       fa[cnt][0] = x;     else {       int y = x;       for (int i = 20; i >= 0; i--) {         if (w[fa[y][i]] < w[cnt]) y = fa[y][i];       }       fa[cnt][0] = fa[y][0];     }     if (fa[cnt][0] == 0)       sum[cnt][0] = inf;     else       sum[cnt][0] = w[fa[cnt][0]];     for (int i = 1; i <= 20; i++) {       fa[cnt][i] = fa[fa[cnt][i - 1]][i - 1];       if (fa[cnt][i] == 0)         sum[cnt][i] = inf;       else         sum[cnt][i] = sum[cnt][i - 1] + sum[fa[cnt][i - 1]][i - 1];     }   }   long long query(long long x, long long v) {     if (w[x] > v) return 0;     v -= w[x];     long long res = 1;     for (int i = 20; i >= 0; i--) {       if (v >= sum[x][i]) {         v -= sum[x][i];         res += (1 << i);         x = fa[x][i];       }     }     return res;   }   int main() {     w[0] = inf;     for (int i = 0; i <= 20; i++) sum[1][i] = inf;     scanf( %lld , &q);     while (q--) {       int id;       long long a, b;       scanf( %d , &id);       scanf( %lld%lld , &a, &b);       a ^= lastans;       b ^= lastans;       if (id == 1)         add(a, b);       else         printf( %lld n , lastans = query(a, b));     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     vector<bool> sub(n, false);     int c = 0;     for (int i = 0; i < n; i++) {       int t;       cin >> t;       if (t < 0) {         sub[i] = true;         c++;       }     }     if (c > k) {       cout <<  -1 n ;       return 0;     }     bool f = c;     k -= c;     vector<int> r(n + 1, 0);     c = 0;     int p = -1;     bool s = true;     int last = -1;     for (int i = 0; i < n; i++) {       if (sub[i]) {         if (s) {           c++;           s = false;           if (p != -1) {             r[i - p]++;           }         }       } else {         if (!s) {           p = i;           s = true;           c++;         }       }     }     if (f && !sub[n - 1]) last = n - p;     for (int i = 1; i < n + 1; i++) {       while (k - i >= 0 && r[i]) {         c -= 2;         k -= i;         r[i] -= 1;       }       if (k - i < 0) {         if (last != -1 && k - last >= 0) {           c--;           last = -1;         }         break;       }     }     if (last != -1 && k - last >= 0) c--;     cout << c <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int p[8505];   int wh[8505];   int ans[8505];   int level[8505];   int nxt[9][8505];   int prv[9][8505];   pair<int, int> bros[9][8505];   int svd, lsz, l;   inline void go(int &f) {     int id = f, t = 0;     f = nxt[l][f];     if (wh[f] < wh[id]) id = f, t = 1;     f = nxt[l][f];     if (wh[f] < wh[id]) id = f, t = 2;     f = nxt[l][f];     if (t == 2) bros[l - 1][id] = {prv[l][prv[l][id]], prv[l][id]};     if (t == 1) bros[l - 1][id] = {prv[l][id], nxt[l][id]};     if (t == 0) bros[l - 1][id] = {nxt[l][id], nxt[l][nxt[l][id]]};     level[id]--, lsz++, nxt[l - 1][svd] = id, nxt[l - 1][id] = n + 1,                                 prv[l - 1][id] = svd;     svd = id;   }   bool comp(int a, int b) { return wh[a] < wh[b]; }   struct genQ {     int lvl, pl, pr;   };   genQ s10[25000];   int s10size;   int pos[4];   int possize;   inline void solve2(int &l) {     if (possize < 4) {       for (int i = 0; i < possize; i++) {         if (ans[pos[i]] == 0) {           for (int j = l; j <= l + 2; j++) {             int t = 1;             for (int k = 0; k < possize; k++)               if (ans[pos[k]] == j) t = 0;             if (t) {               ans[pos[i]] = j;               break;             }           }         }       }       return;     }     int lst = 0;     for (int i = 1; i < 4; i++)       if (wh[pos[i]] > wh[pos[lst]]) lst = i;     if (ans[pos[lst]] == 0) {       if (ans[pos[3 - lst]] == 0) {         if (lst == 0)           ans[pos[0]] = l + 2, ans[pos[1]] = l, ans[pos[2]] = l + 1,           ans[pos[3]] = l + 2;         if (lst == 1)           ans[pos[0]] = l, ans[pos[1]] = l + 2, ans[pos[2]] = l + 1,           ans[pos[3]] = l + 2;         if (lst == 2)           ans[pos[0]] = l, ans[pos[1]] = l + 1, ans[pos[2]] = l,           ans[pos[3]] = l + 2;         if (lst == 3)           ans[pos[0]] = l, ans[pos[1]] = l + 1, ans[pos[2]] = l + 2,           ans[pos[3]] = l;       } else {         if (lst > 1) {           lst = 3 - lst;           reverse(pos, pos + 4);         }         if (lst == 0) {           ans[pos[1]] = 3 * l + 3 - ans[pos[2]] - ans[pos[3]];           ans[pos[0]] = ans[pos[1]] + 1;           if (ans[pos[0]] == l + 3) ans[pos[0]] -= 3;         }         if (lst == 1) {           ans[pos[1]] = ans[pos[3]];           ans[pos[0]] = 3 * l + 3 - ans[pos[1]] - ans[pos[2]];         }       }       return;     }     if (lst > 1) {       lst = 3 - lst;       reverse(pos, pos + 4);     }     if (lst == 1) {       if (wh[pos[2]] < wh[pos[3]] || wh[pos[2]] < wh[pos[0]]) {         ans[pos[3]] = ans[pos[0]];         ans[pos[2]] = 3 * l + 3 - ans[pos[0]] - ans[pos[1]];       } else {         ans[pos[3]] = ans[pos[1]];         ans[pos[2]] = 3 * l + 3 - ans[pos[0]] - ans[pos[1]];       }     } else {       ans[pos[2]] = ans[pos[1]] + 1;       if (ans[pos[2]] == l + 3) ans[pos[2]] -= 3;       ans[pos[3]] = ans[pos[2]] + 1;       if (ans[pos[3]] == l + 3) ans[pos[3]] -= 3;     }   }   inline void solve1() {     int pl = s10[s10size].pl;     int pr = s10[s10size].pr;     int lvl = s10[s10size].lvl;     s10size--;     possize = 0;     if (pl != 0) {       if (bros[lvl][pl].first > pl) pos[possize++] = (bros[lvl][pl].first);       if (bros[lvl][pl].second > pl) pos[possize++] = (bros[lvl][pl].second);     }     if (pr != n + 1) {       if (bros[lvl][pr].first < pr) pos[possize++] = (bros[lvl][pr].first);       if (bros[lvl][pr].second < pr) pos[possize++] = (bros[lvl][pr].second);     }     int l = 3 * lvl + 1;     int t = 0;     for (int i = 0; i < possize; i++) {       if (ans[pos[i]] == 0) t = 1;     }     if (!t) return;     solve2(l);   }   void solve() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &p[i]);     for (int i = 1; i <= n; i++)       wh[p[i]] = i, ans[i] = 0, nxt[8][i] = i + 1, level[i] = 8,       prv[8][i] = i - 1;     for (int i = 1; i <= 8; i++) nxt[i][0] = 1, nxt[i][n + 1] = n + 1;     wh[n + 1] = n + 1;     if (n > 4)       for (l = 8; l >= 1; l--) {         int f = nxt[l][0];         lsz = 0, svd = 0;         while (f != n + 1) go(f);         if (lsz <= 4) break;       }     for (int i = n; i >= 1; i--) {       for (int gl = level[p[i]]; gl < 8; gl++) {         int LL = prv[gl][p[i]], RR = nxt[gl][p[i]];         s10[++s10size] = {gl, LL, RR};         s10[++s10size] = {gl, LL, p[i]};         s10[++s10size] = {gl, p[i], RR};         prv[gl][RR] = LL;         nxt[gl][LL] = RR;       }     }     while (s10size > -1) solve1();     for (int lvl = 1; lvl <= 8; lvl++) {       possize = 0;       for (int i = 1; i <= n; i++)         if (lvl == level[i] && ans[i] == 0) pos[possize++] = i;       int c = lvl * 3 - 2;       solve2(c);     }     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     scanf( %d , &t);     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     scanf( %d %d , &n, &m);     char s[n + 5];     scanf( %s , s);     vector<int>* vp[30];     for (int i = 0; i <= 25; i++) vp[i] = new vector<int>();     for (int i = 0; s[i]; i++) {       int now = s[i] -  a ;       vp[now]->push_back(i);     }     while (m--) {       getchar();       char x, y;       scanf( %c %c , &x, &y);       int id1 = x -  a ;       int id2 = y -  a ;       swap(vp[id1], vp[id2]);     }     for (int i = 0; i <= 25; i++) {       for (vector<int>::iterator it = vp[i]->begin(); it != vp[i]->end(); it++) {         s[*it] = i +  a ;       }     }     printf( %s n , s);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   constexpr long long INF = (sizeof(long long) == 4 ? 1e9 : 2e18) + 1e5;   constexpr long long MOD = 1e9 + 7;   constexpr long long MAXN = 2e5 + 3;   void solve() {     long long n;     cin >> n;     string s;     cin >> s;     long long w = 0;     queue<long long> arr;     for (long long i = 0; i < n; ++i) {       if (i < n && s[i] == s[i + 1]) {         arr.emplace(i + 1);       }     }     long long ans = 0;     long long l = 0;     while (l < n) {       ++ans;       while (arr.size() && arr.front() < l) arr.pop();       if (arr.size() && arr.front() >= l) {         arr.pop();       } else {         while (l + 1 < n && s[l] == s[l + 1]) ++l;         ++l;       }       while (l + 1 < n && s[l] == s[l + 1]) ++l;       ++l;     }     cout << ans <<   n ;   }   signed main() {     const string FILE_NAME =   ;     ios_base::sync_with_stdio(false);     cin.tie(nullptr), cout.tie(nullptr);     long long t = 1;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   struct info {     int d, f, t, cst;   } inp[100006];   int forw[1000005];   int forw2[1000005];   long long cforw[1000005];   long long cforw2[1000005];   int ok[1000006];   int ok2[1000006];   bool cmp(info a, info b) { return a.d < b.d; }   void process1(int prev, int day) {     while (prev < day) {       cforw[prev + 1] = cforw[prev];       forw[prev + 1] = forw[prev];       prev++;     }   }   void process2(int prev, int day) {     while (prev > day) {       cforw2[prev - 1] = cforw2[prev];       forw2[prev - 1] = forw2[prev];       prev--;     }   }   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     for (int i = 1; i <= m; i++) {       scanf( %d %d %d %d , &inp[i].d, &inp[i].f, &inp[i].t, &inp[i].cst);     }     sort(inp + 1, inp + m + 1, cmp);     int prev = 0;     for (int i = 1; i <= m; i++) {       int day = inp[i].d;       process1(prev, day);       prev = day;       if (inp[i].f != 0) {         int pos = inp[i].f;         if (ok[pos] == 0) {           ok[pos] = inp[i].cst;           cforw[day] += inp[i].cst;           forw[day]++;         } else if (ok[pos] > inp[i].cst) {           cforw[day] += (-ok[pos] + inp[i].cst);           ok[pos] = inp[i].cst;         }       }     }     process1(prev, 1000000);     prev = n + 1;     for (int i = m; i >= 1; i--) {       if (inp[i].f != 0) continue;       int pos = inp[i].t;       int day = inp[i].d;       process2(prev, day);       prev = day;       if (ok2[pos] == 0) {         ok2[pos] = inp[i].cst;         cforw2[day] += inp[i].cst;         forw2[day]++;       } else if (ok2[pos] > inp[i].cst) {         cforw2[day] += (-ok2[pos] + inp[i].cst);         ok2[pos] = inp[i].cst;       }     }     process2(prev, 1);     long long ans = -1;     for (int i = 1; i <= 1000000 - k - 1; i++) {       int t = i + k + 1;       if (forw[i] != n || (forw2[t] != n)) continue;       long long tot = cforw[i] + cforw2[t];       if (ans == -1)         ans = tot;       else         ans = min(ans, tot);     }     cout << ans << endl;   }


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


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000005;   struct tr {     int l, r, c, p;   } a[maxn], b[maxn];   int n, k, m, maxr = 0;   long long ans = 0, num[maxn << 2], sum[maxn << 2];   bool cmpl(tr a, tr b) { return a.l < b.l; }   bool cmpr(tr a, tr b) { return a.r < b.r; }   void update(int o, int l, int r, int c, int p) {     if (l == r) {       num[o] += c;       sum[o] += 1LL * c * l;       return;     }     int mid = (l + r) >> 1;     if (p <= mid)       update(o << 1, l, mid, c, p);     else       update(o << 1 | 1, mid + 1, r, c, p);     num[o] = num[o << 1] + num[o << 1 | 1];     sum[o] = sum[o << 1] + sum[o << 1 | 1];   }   long long query(int o, int l, int r, int p) {     if (l == r) {       if (p <= num[o]) return 1LL * l * p;       return sum[o];     }     int mid = (l + r) >> 1;     if (num[o << 1] >= p) return query(o << 1, l, mid, p);     return query(o << 1 | 1, mid + 1, r, p - num[o << 1]) + sum[o << 1];   }   int main() {     scanf( %d%d%d , &n, &k, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d%d%d , &a[i].l, &a[i].r, &a[i].c, &a[i].p);       b[i] = a[i];     }     for (int i = 1; i <= m; i++) maxr = max(maxr, a[i].p);     sort(a + 1, a + m + 1, cmpl);     sort(b + 1, b + m + 1, cmpr);     for (int i = 1, j = 1, o = 1; i <= n; i++) {       while (j <= m && a[j].l == i) update(1, 1, maxr, a[j].c, a[j].p), j++;       ans += query(1, 1, maxr, k);       while (o <= m && b[o].r == i) update(1, 1, maxr, -b[o].c, b[o].p), o++;     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 10;   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     int x[4], y[4];     for (int i = 1; i <= 3; i++) {       cin >> x[i] >> y[i];     }     pair<int, pair<int, int> > e = make_pair(INF, make_pair(INF, INF));     for (int i = 0; i <= 1000; i++) {       for (int j = 0; j <= 1000; j++) {         int w = 0;         for (int u = 1; u <= 3; u++) {           w += abs(x[u] - i) + abs(y[u] - j) + 1;         }         e = min(e, make_pair(w - 2, make_pair(i, j)));       }     }     cout << e.first << endl;     for (int i = 1; i <= 3; i++) {       if (x[i] == e.second.first && y[i] == e.second.second) {         continue;       }       cout << x[i] <<     << y[i] << endl;       while (x[i] > e.second.first) {         x[i]--;         if (x[i] == e.second.first && y[i] == e.second.second) {           break;         }         cout << x[i] <<     << y[i] << endl;       }       while (x[i] < e.second.first) {         x[i]++;         if (x[i] == e.second.first && y[i] == e.second.second) {           break;         }         cout << x[i] <<     << y[i] << endl;       }       while (y[i] > e.second.second) {         y[i]--;         if (x[i] == e.second.first && y[i] == e.second.second) {           break;         }         cout << x[i] <<     << y[i] << endl;       }       while (y[i] < e.second.second) {         y[i]++;         if (x[i] == e.second.first && y[i] == e.second.second) {           break;         }         cout << x[i] <<     << y[i] << endl;       }     }     cout << e.second.first <<     << e.second.second;   }


#include <bits/stdc++.h>   using namespace std;   long long n, a[100010];   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     unsigned long long ans = 0;     for (int i = 0; i < n; i++) {       unsigned long long j, k = 0;       for (j = i; j < n; j++) {         if (a[i] != a[j]) break;         k++;       }       ans += (k * (k + 1)) / 2;       i = j - 1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double dp[5][5005];   double p[5005], subp[5005];   int t[5005];   double qpow(double a, int k) {     double res = 1;     while (k) {       if (k & 1) res *= a;       a *= a;       k >>= 1;     }     return res;   }   int main(int argc, const char* argv[]) {     int n, T;     scanf( %d%d , &n, &T);     for (int i = 1; i <= n; ++i) {       scanf( %lf%d , &p[i], &t[i]);       p[i] /= 100;       subp[i] = 1 - p[i];     }     int cur = 1;     dp[cur ^ 1][0] = 1;     double ans = 0;     for (int i = 1; i <= n; ++i, cur ^= 1) {       double pp = qpow(subp[i], t[i] - 1);       dp[cur][0] = 0;       for (int j = 1; j <= T; ++j) {         double tmp = dp[cur][j - 1];         if (j - 1 >= t[i]) tmp -= dp[cur ^ 1][j - 1 - t[i]] * pp;         tmp *= subp[i];         if (j >= t[i]) tmp += dp[cur ^ 1][j - t[i]] * subp[i] * pp;         tmp += dp[cur ^ 1][j - 1] * p[i];         dp[cur][j] = tmp;         ans += tmp;       }     }     printf( %.10f n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110000;   vector<int> adj[N];   long long p[N], s[N];   int main() {     ios::sync_with_stdio(0);     int n;     cin >> n;     for (int i = 2; i <= n; i++) cin >> p[i];     for (int i = 1; i <= n; i++) cin >> s[i];     for (int i = 1; i <= n; i++) {       if (~s[i]) {         int j = p[p[i]];         int k = p[i];         if (s[i] < s[j]) return puts( -1 ), 0;         adj[k].push_back(s[i] - s[j]);       }     }     long long dap = s[1];     for (int i = 1; i <= n; i++) {       if (!adj[i].empty()) {         sort(adj[i].begin(), adj[i].end());         dap += adj[i][0];         for (int x : adj[i]) dap += (x - adj[i][0]);       }     }     cout << dap << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX_N = 200 * 1000 + 24, INF = (1 << 30);   int n;   int a[MAX_N], cnt[MAX_N], mark[MAX_N];   vector<int> G[MAX_N], vec, avb;   int main() {     cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i], a[i]--, cnt[a[i]]++;     for (int i = 0; i < n; i++)       if (cnt[i] == 0) vec.push_back(i);     for (int i = 0; i < n; i++) {       if (cnt[a[i]] > 1) {         G[a[i]].push_back(i), avb.push_back(i);         mark[a[i]] = true;       }     }     memset(mark, 0, sizeof mark);     int j = 0;     for (int i = 0; i < avb.size(); i++) {       int p = avb[i];       if (mark[a[p]] == false and           (a[p] < vec[j] or p == G[a[p]][G[a[p]].size() - 1])) {         mark[a[p]] = true;       } else {         a[p] = vec[j++];       }     }     cout << vec.size() << endl;     for (int i = 0; i < n; i++) {       cout << a[i] + 1 <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   void sublime() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   int main() {     sublime();     long long n, i, j;     cin >> n;     long long arr[n];     for (i = 0; i < n; i++) cin >> arr[i];     if (n <= 2) {       cout << 0 <<   n ;       for (i = 0; i < n; i++) cout << arr[i] <<    ;       cout <<   n ;       return 0;     } else {       sort(arr, arr + n);       vector<long long> v;       i = 0, j = (n - 1) / 2;       if (n & 1) {         while (i < (n - 1) / 2 || j < n) {           v.push_back(arr[j]);           j++;           if (i < (n - 1) / 2) {             v.push_back(arr[i]);             i++;           }         }       } else {         j = n / 2;         while (i < (n - 1) / 2 || j < n) {           v.push_back(arr[j]);           j++;           if (i < (n - 1) / 2) {             v.push_back(arr[i]);             i++;           }         }         v.push_back(arr[(n - 1) / 2]);       }       long long ans = 0;       for (i = 1; i < v.size() - 1; i += 2) {         if (v[i] < v[i + 1] && v[i] < v[i - 1]) ans++;       }       cout << ans <<   n ;       for (auto x : v) cout << x <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T modinv(T a, T n) {     T i = n, v = 0, d = 1;     while (a > 0) {       T t = i / a, x = a;       a = i % x;       i = x;       x = d;       d = v - t * x;       v = x;     }     return (v + n) % n;   }   long long modpow(long long n, long long k, long long mod) {     long long ans = 1;     while (k > 0) {       if (k & 1) ans = (ans * n) % mod;       k >>= 1;       n = (n * n) % mod;     }     return ans % mod;   }   template <class T>   string str(T Number) {     string Result;     ostringstream convert;     convert << Number;     Result = convert.str();     return Result;   }   int StringToNumber(const string &Text) {     istringstream ss(Text);     int result;     return ss >> result ? result : 0;   }   template <class T>   inline vector<pair<T, int> > FACTORISE(T n) {     vector<pair<T, int> > R;     for (T i = 2; n > 1;) {       if (n % i == 0) {         int C = 0;         for (; n % i == 0; C++, n /= i)           ;         R.push_back(make_pair(i, C));       }       i++;       if (i > n / i) i = n;     }     if (n > 1) R.push_back(make_pair(n, 1));     return R;   }   template <class T>   inline T TOTIENT(T n) {     vector<pair<T, int> > R = FACTORISE(n);     T r = n;     for (int i = 0; i < R.size(); i++) r = r / R[i].first * (R[i].first - 1);     return r;   }   template <class T>   inline T gcd(T a, T b) {     return b ? gcd(b, a % b) : a;   }   double rnd(float d) { return floor(d + 0.49); }   int edge[3300][3300], Pairs[3300][3300];   int match[3300];   bool seen[3300];   vector<pair<int, int> > groups;   bool bfs(int v) {     seen[v] = 1;     for (int i = 0; i < ((int)(groups).size()); i++) {       if (edge[v][i] && !seen[i]) {         seen[i] = 1;         if (match[i] == -1 || bfs(match[i])) {           match[v] = i;           match[i] = v;           return true;         }       }     }     seen[v] = 0;     return false;   }   int main() {     int n, m;     cin >> n >> m;     vector<int> A(n);     for (int i = 0; i < n; i++) {       cin >> A[i];       int x = A[i];       for (int j = 2; j * j <= A[i]; j++) {         if (j > x) break;         while (x % j == 0) {           x = x / j;           groups.push_back(make_pair(j, i));         }       }       if (x > 1) groups.push_back(make_pair(x, i));     }     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       --a, --b;       Pairs[a][b] = 1, Pairs[b][a] = 1;     }     memset(edge, 0, sizeof(edge));     for (int i = 0; i < ((int)(groups).size()); i++)       for (int j = i + 1; j < ((int)(groups).size()); j++) {         if (Pairs[groups[i].second][groups[j].second] &&             groups[i].first == groups[j].first)           edge[i][j] = 1, edge[j][i] = 1;       }     memset(match, -1, sizeof(match));     int matchings = 0;     for (int i = 0; i < ((int)(groups).size()); i++) {       if (match[i] != -1) continue;       memset(seen, false, sizeof(seen));       if (bfs(i)) matchings++;     }     cout << matchings << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int dp[205][205][205];   long long int recur(string &a, string &b, long long int index, long long int k,                       long long int curr) {     if (index == a.size()) {       return 0;     }     if (dp[index][k][curr] != -1) {       return dp[index][k][curr];     }     if (k > 0) {       if (a[index] == b[0]) {         return dp[index][k][curr] =                    max(recur(a, b, index + 1, k, curr + 1),                        curr + recur(a, b, index + 1, k - 1, curr));       } else if (a[index] == b[1]) {         return dp[index][k][curr] = max(curr + recur(a, b, index + 1, k, curr),                                         recur(a, b, index + 1, k - 1, curr + 1));       } else {         return dp[index][k][curr] =                    max(recur(a, b, index + 1, k, curr),                        max(curr + recur(a, b, index + 1, k - 1, curr),                            recur(a, b, index + 1, k - 1, curr + 1)));       }     } else {       if (a[index] == b[1]) {         return dp[index][k][curr] = curr + recur(a, b, index + 1, k, curr);       } else if (a[index] == b[0]) {         return dp[index][k][curr] = recur(a, b, index + 1, k, curr + 1);       } else {         return dp[index][k][curr] = recur(a, b, index + 1, k, curr);       }     }   }   void solve() {     long long int n, k;     cin >> n >> k;     string a, b;     cin >> a >> b;     memset(dp, -1, sizeof(dp));     if (b[0] == b[1]) {       long long int curr = 0;       for (int i = 0; i < a.size(); i++) {         if (a[i] == b[0]) {           curr++;         }       }       curr = min(curr + k, n);       long long int res = (curr) * (curr - 1);       res = res >> 1;       cout << res <<   n ;     } else {       cout << recur(a, b, 0, k, 0) <<   n ;     }     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 70 + 10;   int n, dist[MAXN * MAXN * MAXN], u[3];   char arr[MAXN][MAXN];   int a[MAXN * MAXN * MAXN], b[MAXN * MAXN * MAXN];   struct node {     int v[3] = {-1, -1, -1};     int A = -1, B = -1;     node(int v1 = -1, int v2 = -1, int v3 = -1, int A_ = -1, int B_ = -1) {       v[0] = v1;       v[1] = v2;       v[2] = v3;       A = A_;       B = B_;       sort(this->v, this->v + 3);     }     int number() { return v[0] + n * v[1] + n * n * v[2]; }     vector<node> adj() {       vector<node> adj_;       for (int j = 0; j < n; j++) {         if (j == v[0] || j == v[1] || j == v[2]) continue;         if (arr[v[1]][v[2]] == arr[v[0]][j])           adj_.push_back(node(v[1], v[2], j, v[0], j));         if (arr[v[0]][v[2]] == arr[v[1]][j])           adj_.push_back(node(v[0], v[2], j, v[1], j));         if (arr[v[1]][v[0]] == arr[v[2]][j])           adj_.push_back(node(v[1], v[0], j, v[2], j));       }       return adj_;     }   } Q[MAXN * MAXN * MAXN], par[MAXN * MAXN * MAXN];   int main() {     ios::sync_with_stdio(0);     cin >> n;     cin >> u[0] >> u[1] >> u[2];     u[0]--;     u[1]--;     u[2]--;     memset(dist, 63, sizeof(dist));     dist[node(u[0], u[1], u[2]).number()] = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         cin >> arr[i][j];       }     }     int L = 0, R = 0;     Q[R++] = node(u[0], u[1], u[2]);     while (L < R) {       node tp = Q[L++];       for (auto i : tp.adj())         if (dist[i.number()] == dist[MAXN * MAXN * MAXN - 1]) {           dist[i.number()] = dist[tp.number()] + 1;           Q[R++] = i;           par[i.number()] = tp;           a[i.number()] = i.A;           b[i.number()] = i.B;         }     }     if (dist[node(0, 1, 2).number()] != dist[MAXN * MAXN * MAXN - 1]) {       cout << dist[node(0, 1, 2).number()] <<   n ;       vector<pair<int, int>> answer;       node me = node(0, 1, 2);       for (int i = 0; i < dist[node(0, 1, 2).number()]; i++) {         answer.push_back(pair<int, int>(a[me.number()], b[me.number()]));         me = par[me.number()];       }       reverse(answer.begin(), answer.end());       for (auto i : answer) cout << i.first + 1 <<     << i.second + 1 <<   n ;     } else       cout << -1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> prime;   int y;   int checker(int n) {     for (int i = 0; i < y && i < n; i++) {       if (prime[i] + prime[i + 1] + 1 == n) return 1;       if (prime[i] + prime[i + 1] + 1 > n) return 0;     }   }   int isprime(int n) {     int x = sqrt(n);     for (int i = 2; i <= x; i++) {       if (n % i == 0) return 0;     }     return 1;   }   int main() {     int n, k, counter = 0;     cin >> n >> k;     prime.push_back(2);     prime.push_back(3);     prime.push_back(5);     y = 3;     for (int i = 7; i <= n; i++) {       if (isprime(i)) {         prime.push_back(i);         y++;         if (checker(i)) counter++;         if (counter >= k) break;       }     }     if (counter >= k)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t, n, m, r, l, h, i, ii, j, x[1000001], y[1000001], z, g, a, b, c, d,       k, mod = 1e9 + 7, q;   vector<long long> v;   vector<pair<long long, long long> > vv;   queue<pair<long long, long long> > qq;   pair<long long, long long> p[1000001];   map<long long, long long> mp, mpp, mppp;   set<pair<long long, long long> > se[1000001];   multiset<long long> sw;   multiset<long long>::iterator it;   string s;   char cc;   int main() {     ios::sync_with_stdio(0);     cin >> t;     while (t--) {       mp.clear();       cin >> n;       b = 0;       for (i = 0; i < n; i++) cin >> x[i];       sort(x, x + n);       for (i = 0; i < n; i++) {         mp[x[i] % 2]++;         if (x[i] == x[i - 1] + 1 && i > 0) b = 1;       }       a = mp[0] % 2;       c = mp[1] % 2;       if ((a == c && a == 0) || (a == c && a == 1 && b == 1))         cout <<  YES ;       else         cout <<  NO ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool comp1(int& a, int& b);   int64_t gcd(int64_t a, int64_t b);   bool comp2(int& a, int& b);   bool comp3(pair<int, int>& a, pair<int, int>& b);   bool comp4(pair<int, int>& a, pair<int, int>& b);   bool isPrime(int& x);   void input(vector<int>& v, int& n) {     for (int i = 0; i < n; ++i) {       cin >> v[i];     }   }   int sum(vector<int>& v) {     int s = 0;     for (auto& el : v) {       s += el;     }     return s;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     int64_t ans = 1;     set<char> simbols;     int cntQ = 0;     ;     for (size_t i = 0; i < s.size(); ++i) {       if (s[i] ==  ? ) {         cntQ++;       } else if (s[i] >=  A  && s[i] <=  J ) {         simbols.insert(s[i]);       }     }     int k = 10;     int size = simbols.size();     if (s[0] ==  ? ) {       ans = 9;       cntQ--;     } else if (s[0] >=  A  && s[0] <=  J ) {       size--;       k--;       ans = 9;     }     for (int i = 0; i < size; ++i) {       ans *= k;       k--;     }     string g;     while (ans > 0) {       g += (ans % 10 +  0 );       ans /= 10;     }     reverse(g.begin(), g.end());     for (int i = 0; i < cntQ; ++i) {       g +=  0 ;     }     cout << g;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e9 + 7;   const long long mxN = 1e5 + 1;   vector<long long> dat;   long long arr[4000], n;   bool dp[2001];   void solve() {     cin >> n;     n *= 2;     long long p = 0, temp = 0;     for (long long i = 0; i < n; i++) {       cin >> arr[i];     }     dat.clear();     long long i = 0;     while (i < n) {       long long j = i + 1;       while (j < n && arr[i] > arr[j]) {         j++;       }       dat.push_back(j - i);       i = j;     }     memset(dp, 0, sizeof(dp));     dp[0] = true;     for (long long i = 0; i < dat.size(); i++) {       for (long long j = n / 2; j >= 0; j--) {         if (j + dat[i] <= n / 2) dp[j + dat[i]] = dp[j] | dp[j + dat[i]];       }     }     dp[n / 2] ? cout <<  YES n  : cout <<  NO n ;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long T = 1, t = 0;     cin >> T;     while (t++ < T) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int a[10000], b[10000], f[10000000];   int main() {     int n, ans = 0;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       f[a[i]] = 1;     }     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       f[b[i]] = 1;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (f[a[i] ^ b[j]]) ans++;       }     }     if (ans % 2 == 0)       printf( Karen n );     else       printf( Koyomi n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double CHANGE = 1e-8;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c* x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug& operator<<(const c&) {       return *this;     }   };   int main() {     puts( YES );     int n;     scanf( %d , &n);     while (n--) {       int x1, y1, x2, y2;       scanf( %d%d%d%d , &x1, &y1, &x2, &y2);       int ans = 0;       if (abs(min(x1, x2)) % 2) ans ^= 1;       if (abs(min(y1, y2)) % 2) ans ^= 2;       printf( %d n , ans + 1);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     if (n % 2 == 0) {       cout <<  NO  <<   n ;       return 0;     }     int arr[200005] = {0};     deque<int> d;     for (int i = 0; i < 2 * n; i++) d.push_back(i + 1);     for (int i = 0; i < n; i++) {       if (i % 2 == 0) {         arr[i] = d.front();         d.pop_front();         arr[i + n] = d.front();         d.pop_front();       } else {         arr[i] = d.back();         d.pop_back();         arr[i + n] = d.back();         d.pop_back();       }     }     cout <<  YES  <<   n ;     for (int i = 0; i < 2 * n; i++) cout << arr[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   int n;   int fa[maxn];   bool vis[maxn];   int cnt_loop = 0;   vector<int> loop[maxn];   void get(int x, int id) {     vis[x] = 1;     loop[id].push_back(x);     if (vis[fa[x]]) return;     get(fa[x], id);   }   bool num[maxn];   int rid[maxn];   int main() {     scanf( %d , &n);     for (register int i = 1; i <= n; i++) {       scanf( %d , &fa[i]);     }     for (register int i = 1; i <= n; i++) {       if (!vis[i]) get(i, ++cnt_loop);     }     bool fg2 = 0;     for (register int i = 1; i <= cnt_loop; i++) {       num[loop[i].size()] = 1;       rid[loop[i].size()] = i;       if (((int)loop[i].size()) & 1) {         fg2 = 1;       }     }     if (num[1]) {       printf( YES n );       for (register int i = 1; i <= cnt_loop; i++) {         if (rid[1] == i) continue;         for (register int j = 0; j < (int)loop[i].size(); j++) {           printf( %d %d n , loop[rid[1]][0], loop[i][j]);         }       }     } else if (num[2] && !fg2) {       printf( YES n );       for (register int i = 1; i <= cnt_loop; i++) {         if (rid[2] == i) {           printf( %d %d n , loop[i][0], loop[i][1]);           continue;         }         for (register int j = 0; j < (int)loop[i].size(); j++) {           printf( %d %d n , loop[rid[2]][j & 1], loop[i][j]);         }       }     } else {       printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   template <typename T>   inline T Bigmod(T base, T power, T MOD) {     T ret = 1;     while (power) {       if (power & 1) ret = (ret * base) % MOD;       base = (base * base) % MOD;       power >>= 1;     }     return ret;   }   bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {     return (a.first > b.first);   }   int main() {     ios::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int arr[n];       map<int, int> ind;       for (int i = 0; i < n; i++) {         cin >> arr[i];         ind[arr[i]] = i + 1;       }       int l, r;       l = ind[1];       string s;       s.push_back( 1 );       if (n > 1) {         int in = ind[2];         if (in > l)           r = in;         else {           r = l;           l = in;         }         if ((r - l) <= 1)           s.push_back( 1 );         else           s.push_back( 0 );       }       for (int i = 3; i <= n; i++) {         int in = ind[i];         if (in < l) {           if ((r - in) < i) {             s.push_back( 1 );           } else {             s.push_back( 0 );           }           l = in;         } else if (in > r) {           if ((in - l) < i) {             s.push_back( 1 );           } else {             s.push_back( 0 );           }           r = in;         } else {           if ((r - l) < i)             s.push_back( 1 );           else             s.push_back( 0 );         }       }       cout << s << endl;     }     return 0;   }


#include <bits/stdc++.h>   int n, m[20], r[20], c;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &m[i]);     for (int i = 0; i < n; i++) scanf( %d , &r[i]);     for (int i = 0; i < 720720; i++) {       bool you = false;       for (int j = 0; j < n; j++)         if (i % m[j] == r[j]) you = true;       if (you) c++;     }     printf( %.6lf n , c / 720720.);     scanf(  n );     return 0;   }


#include <bits/stdc++.h>   template <class T>   using pt = std::pair<T, T>;   using namespace std;   struct poledia {     int lx, ly, rx, ry;     int cnt = 0;     poledia() : lx(- -1e9), ly(- -1e9), ry(-1e9), rx(-1e9){};     poledia(bool is_dw) : lx(- -1e9), ly(-1e9), ry(- -1e9), rx(-1e9){};   };   struct poleos {     int x1, y1, x2, y2;     int cnt = 0;     poleos() : x1(0), x2(0), y1(-1e9), y2(- -1e9){};     poleos(bool is_vert) : x1(- -1e9), x2(-1e9), y1(0), y2(0){};   };   struct ferz {     int x, y;   };   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     vector<ferz> a(m);     vector<poledia> diag1(2 * 1e5 + 1);     vector<poledia> diag2(2 * 1e5 + 1);     vector<poleos> ossx(1e5 + 1);     vector<poleos> ossy(1e5 + 1);     for (int i = 0; i < m; i++) {       cin >> a[i].x >> a[i].y;       int k = a[i].x + a[i].y - 1;       if (diag1[k].cnt == 0) {         diag1[k] = poledia(true);       }       diag1[k].lx = min(a[i].x, diag1[k].lx);       diag1[k].ly = max(a[i].y, diag1[k].ly);       diag1[k].rx = max(a[i].x, diag1[k].rx);       diag1[k].ry = min(a[i].y, diag1[k].ry);       diag1[k].cnt++;       k = n - a[i].x + a[i].y;       diag2[k].lx = min(diag2[k].lx, a[i].x);       diag2[k].ly = min(diag2[k].ly, a[i].y);       diag2[k].rx = max(diag2[k].rx, a[i].x);       diag2[k].ry = max(diag2[k].ry, a[i].y);       diag2[k].cnt++;       k = a[i].y;       if (ossx[k].cnt == 0) {         ossx[k] = poleos(true);       }       ossx[k].x1 = min(ossx[k].x1, a[i].x);       ossx[k].x2 = max(ossx[k].x2, a[i].x);       ossx[k].cnt++;       k = a[i].x;       ossy[k].y1 = max(ossy[k].y1, a[i].y);       ossy[k].y2 = min(ossy[k].y2, a[i].y);       ossy[k].cnt++;     }     vector<int> ans(9);     for (int i = 0; i < m; i++) {       int sum = 0;       int k1 = a[i].x + a[i].y - 1, k2 = n - a[i].x + a[i].y;       if (diag1[k1].cnt != 1) {         if (a[i].x == diag1[k1].lx && a[i].y == diag1[k1].ly ||             a[i].x == diag1[k1].rx && a[i].y == diag1[k1].ry)           sum++;         else           sum += 2;       }       if (diag2[k2].cnt != 1) {         if (a[i].x == diag2[k2].lx && a[i].y == diag2[k2].ly ||             a[i].x == diag2[k2].rx && a[i].y == diag2[k2].ry)           sum++;         else           sum += 2;       }       if (ossx[a[i].y].cnt != 1) {         if (a[i].x == ossx[a[i].y].x1 || a[i].x == ossx[a[i].y].x2)           sum++;         else           sum += 2;       }       if (ossy[a[i].x].cnt != 1) {         if (a[i].y == ossy[a[i].x].y1 || a[i].y == ossy[a[i].x].y2)           sum++;         else           sum += 2;       }       ans[sum]++;     }     for (int i = 0; i < 9; i++) {       cout << ans[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void read(T& x) {     T res = 0, f = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       res = (res << 3) + (res << 1) + c -  0 ;       c = getchar();     }     x = res * f;   }   const long long N = 300000 + 10;   long long n, m, mod = 998244353, fac[N], inv[N], mx = 300000;   long long sum[N][55];   int L[N], R[N], cnt[N];   long long fpow(long long x, long long p) {     long long ans = 1;     while (p) {       if (p & 1) ans = ans * x % mod;       x = x * x % mod;       p >>= 1;     }     return ans;   }   long long C(int x, int y) {     if (y < 0 || x < y) return 0;     return (fac[x] * inv[y] % mod) * inv[x - y] % mod;   }   void init() {     fac[0] = 1;     for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod;     inv[n] = fpow(fac[n], mod - 2);     for (int i = n - 1; i >= 0; i--) {       inv[i] = inv[i + 1] * (i + 1) % mod;     }   }   struct node {     int a, b;   } nod[50];   int main() {     read(n), read(m);     init();     for (int i = 1; i <= n; i++)       read(L[i]), read(R[i]), cnt[L[i]]++, cnt[R[i] + 1]--;     for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1];     for (int i = 0; i < m; i++) {       read(nod[i].a), read(nod[i].b);     }     for (int i = 0; i <= 2 * m; i++)       for (int j = 1; j <= n; j++)         sum[j][i] = (sum[j - 1][i] + C(cnt[j] - i, j - i)) % mod;     int mx = (1 << m) - 1;     long long ans = 0;     for (int i = 0; i <= mx; i++) {       int l = 1, r = n;       set<int> in;       for (int j = 0; j < m; j++) {         if ((i >> j) & 1) {           in.insert(nod[j].a);           in.insert(nod[j].b);           l = max(l, L[nod[j].a]);           l = max(l, L[nod[j].b]);           r = min(r, R[nod[j].a]);           r = min(r, R[nod[j].b]);         }       }       int num = in.size();       long long temp = 0;       if (l <= r) temp = (sum[r][num] - sum[l - 1][num]) % mod;       if (__builtin_popcount(i) & 1) {         ans = (ans - temp) % mod;       } else {         ans = (ans + temp) % mod;       }     }     printf( %lld n , (ans % mod + mod) % mod);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int LIM = 200 * 1000, MAXN = 300 * 1000;   int fen[MAXN];   void add(int pos, int val) {     pos++;     while (pos < MAXN) {       fen[pos] += val;       pos += pos & -pos;     }   }   int sum(int pos) {     pos++;     int tot = 0;     while (pos) {       tot += fen[pos];       pos -= pos & -pos;     }     return tot;   }   bool by_id(pair<pair<int, int>, pair<int, int> > a,              pair<pair<int, int>, pair<int, int> > b) {     return a.second.first < b.second.first;   }   vector<int> occ[MAXN];   int pt[MAXN], a[MAXN], n, q;   pair<pair<int, int>, pair<int, int> > seg[MAXN];   int main() {     cin >> n >> q;     for (int i = 0; i < int(n); i++) {       cin >> a[i];       if (a[i] < LIM) occ[a[i]].push_back(i);     }     for (int i = 1; i < int(LIM); i++)       if (i <= (int((occ[i]).size()))) {         pt[i] = i - 1;         add(occ[i][pt[i]], 1);         if (pt[i] + 1 < (int((occ[i]).size()))) add(occ[i][pt[i] + 1], -1);       } else         pt[i] = (int((occ[i]).size()));     cerr <<  Salam :)  << endl;     for (int i = 0; i < int(q); i++) {       seg[i].second.first = i;       cin >> seg[i].first.first >> seg[i].first.second;       seg[i].first.first--;       seg[i].first.second--;     }     sort(seg, seg + q);     int cur = 0;     for (int i = 0; i < int(n); i++) {       while (cur < q && seg[cur].first.first == i) {         seg[cur].second.second = sum(seg[cur].first.second);         cur++;       }       int x = a[i];       if (x < LIM && pt[x] < (int((occ[x]).size()))) {         add(occ[x][pt[x]], -1);         if (pt[x] + 1 < (int((occ[x]).size()))) add(occ[x][pt[x] + 1], +1);         pt[x]++;         if (pt[x] < (int((occ[x]).size()))) {           add(occ[x][pt[x]], 1);           if (pt[x] + 1 < (int((occ[x]).size()))) add(occ[x][pt[x] + 1], -1);         }       }     }     sort(seg, seg + q, by_id);     for (int i = 0; i < int(q); i++) cout << seg[i].second.second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t = 1;     cin >> t;     while (t--) {       long long int n, i, j;       cin >> n;       long long int a = n / 3, b = n / 3;       if (n % 3 == 1)         cout << a + 1 <<     << b;       else if (n % 3 == 2)         cout << a <<     << b + 1;       else         cout << a <<     << b;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 20005;   const int M = 400005;   vector<pair<int, int> > E[N];   int dep[N], fa[N][16], fav[N][16];   int n, m, S, T, nd, jud[N * 2];   int head[M], vis[M], tot = 1;   int dis[M], q[M];   struct edge {     int to, next, f;   } e[M * 5];   void add(int x, int y, int f) {     e[++tot] = (edge){y, head[x], f};     head[x] = tot;     e[++tot] = (edge){x, head[y], 0};     head[y] = tot;   }   bool bfs() {     for (int i = (int)(1); i <= (int)(nd); i++) dis[i] = -1;     int h = 0, t = 1;     q[1] = S;     dis[S] = 0;     while (h != t) {       int x = q[++h];       for (int i = head[x]; i; i = e[i].next)         if (dis[e[i].to] == -1 && e[i].f) {           dis[e[i].to] = dis[x] + 1;           if (e[i].to == T) return 1;           q[++t] = e[i].to;         }     }     return 0;   }   int dfs(int x, int flow) {     if (x == T) return flow;     int k, rest = flow;     for (int i = head[x]; i && rest; i = e[i].next)       if (dis[e[i].to] == dis[x] + 1 && e[i].f) {         k = dfs(e[i].to, min(rest, e[i].f));         e[i].f -= k;         e[i ^ 1].f += k;         rest -= k;       }     if (rest) dis[x] = -1;     return flow - rest;   }   void dfs(int x) {     for (auto i : E[x])       if (i.first != fa[x][0]) {         dep[i.first] = dep[x] + 1;         fa[i.first][0] = x;         fav[i.first][0] = i.second;         dfs(i.first);       }   }   int LCA(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     int tmp = dep[x] - dep[y];     for (int i = (int)(14); i >= (int)(0); i--)       if (tmp & (1 << i)) x = fa[x][i];     for (int i = (int)(14); i >= (int)(0); i--)       if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];     return x == y ? x : fa[x][0];   }   int main() {     scanf( %d%d , &n, &m);     for (int i = (int)(1); i <= (int)(n - 1); i++) {       int x, y;       scanf( %d%d , &x, &y);       E[x].push_back(pair<int, int>(y, i));       E[y].push_back(pair<int, int>(x, i));     }     S = nd = n;     T = ++nd;     for (int i = (int)(1); i <= (int)(n - 1); i++) add(S, i, 1), jud[i] = tot;     for (int i = (int)(1); i <= (int)(m); i++) add(++nd, T, 1), jud[i + n] = tot;     dfs(1);     for (int i = (int)(1); i <= (int)(14); i++)       for (int j = (int)(1); j <= (int)(n); j++) {         fa[j][i] = fa[fa[j][i - 1]][i - 1];         if (dep[j] >= 1 << i) {           fav[j][i] = ++nd;           add(fav[j][i - 1], fav[j][i], 1 << 30);           add(fav[fa[j][i - 1]][i - 1], fav[j][i], 1 << 30);         }       }     for (int i = (int)(1); i <= (int)(m); i++) {       int p = n + 1 + i, x, y;       scanf( %d%d , &x, &y);       int L = LCA(x, y);       for (int j = (int)(14); j >= (int)(0); j--) {         if (dep[x] - dep[L] >= 1 << j)           add(fav[x][j], n + 1 + i, 1 << 30), x = fa[x][j];         if (dep[y] - dep[L] >= 1 << j)           add(fav[y][j], n + 1 + i, 1 << 30), y = fa[y][j];       }       assert(x == L && y == L);     }     int tmp = 0;     for (; bfs(); tmp += dfs(S, 1 << 30))       ;     vector<int> ans1, ans2;     for (int i = (int)(1); i <= (int)(n - 1); i++)       if (dis[i] == -1) ans1.push_back(i);     for (int i = (int)(1); i <= (int)(m); i++)       if (dis[i + n + 1] != -1) ans2.push_back(i);     assert(ans1.size() + ans2.size() == tmp);     printf( %d n , ans1.size() + ans2.size());     printf( %d  , ans2.size());     for (auto i : ans2) printf( %d  , i);     puts(  );     printf( %d  , ans1.size());     for (auto i : ans1) printf( %d  , i);     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   map<pair<int, int>, int> M;   set<pair<int, int> > tmp;   char s[77777][11];   int main() {     int n;     scanf( %d , &n);     for (int ii = 0; ii < n; ii++) {       scanf( %s , s[ii]);       for (int i = 0; i < 9; i++) {         for (int j = i; j < 9; j++) {           int ans = 0;           for (int k = i; k <= j; k++) ans = ans * 10 + s[ii][k] -  0 ;           tmp.insert(make_pair(j - i + 1, ans));         }       }       for (set<pair<int, int> >::iterator it = tmp.begin(); it != tmp.end();            it++) {         M[*it]++;       }       tmp.clear();     }     for (int ii = 0; ii < n; ii++) {       int di, dj, res = 10;       for (int i = 0; i < 9; i++) {         for (int j = i; j < 9; j++) {           int ans = 0;           for (int k = i; k <= j; k++) ans = ans * 10 + s[ii][k] -  0 ;           if (M[make_pair(j - i + 1, ans)] == 1 && res > j - i + 1) {             res = j - i + 1;             di = i, dj = j;           }         }       }       for (int k = di; k <= dj; k++) putchar(s[ii][k]);       puts(  );     }   }


#include <bits/stdc++.h>   using namespace std;   const int b = 1e4 + 200;   int ans[b];   int n = 0, ez1, ez2;   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> ez1 >> ez2;       ans[i] = (ez2 - (ez1 % ez2)) % ez2;     }     for (int i = 0; i < n; i++) {       cout << ans[i] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 10;   const long long inf = 1e18;   long long n, m, Q, E[N][3], l[N], r[N], op[N];   long long hd[N], ht = 1, tf[N], id[N];   long long cnt = 0;   struct edge {     long long v, w, n;   } e[N * 2];   void addedge(long long u, long long v, long long w) {     e[++ht] = (edge){v, w, hd[u]};     hd[u] = ht;   }   long long diss[N], dist[N];   priority_queue<pair<long long, long long> > q;   pair<long long, long long> qtmp;   void Dijkstra(long long S, long long T, bool typed, long long typef,                 long long u = 0) {     memset((typed ? dist : diss), 0x3f, sizeof((typed ? dist : diss)));     (typed ? dist : diss)[S] = 0;     q.push(make_pair(0, S));     while (!q.empty()) {       qtmp = q.top();       q.pop();       if (qtmp.first != -(typed ? dist : diss)[u = qtmp.second]) continue;       for (long long i = hd[u], v; i; i = e[i].n) {         v = e[i].v;         if ((typed ? dist : diss)[v] > (typed ? dist : diss)[u] + e[i].w) {           tf[v] = i >> 1;           (typed ? dist : diss)[v] = (typed ? dist : diss)[u] + e[i].w;           if (typef == 1 && !op[v]) l[v] = l[u];           if (typef == 2 && !op[v]) r[v] = r[u];           q.push(make_pair(-(typed ? dist : diss)[v], v));         }       }     }   }   long long t[N << 3];   void build(long long k, long long l, long long r) {     t[k] = inf;     if (l == r) return;     long long mid = (l + r) >> 1;     build(k << 1, l, mid);     build(k << 1 | 1, mid + 1, r);   }   void modify(long long k, long long l, long long r, long long L, long long R,               long long x) {     if (R < l || r < L) return;     if (L <= l && r <= R) {       t[k] = min(t[k], x);       return;     }     long long mid = (l + r) >> 1;     modify(k << 1, l, mid, L, R, x);     modify(k << 1 | 1, mid + 1, r, L, R, x);   }   long long query(long long k, long long l, long long r, long long x) {     if (l == r) return t[k];     long long mid = (l + r) >> 1;     if (x <= mid)       return min(t[k], query(k << 1, l, mid, x));     else       return min(t[k], query(k << 1 | 1, mid + 1, r, x));   }   signed main() {     scanf( %lld%lld%lld , &n, &m, &Q);     memset(id, -1, sizeof(id));     for (long long i = 1; i <= m; i++) {       scanf( %lld%lld%lld , &E[i][0], &E[i][1], &E[i][2]);       addedge(E[i][0], E[i][1], E[i][2]);       addedge(E[i][1], E[i][0], E[i][2]);       id[i] = -1;     }     Dijkstra(n, 1, 1, 0);     op[1] = 1, l[1] = r[1] = 0;     for (long long i = 1, x = 1, tmp; x != n; i++) {       id[tmp = tf[x]] = i, ++cnt;       op[x ^= E[tmp][0] ^ E[tmp][1]] = 1;       l[x] = r[x] = i;     }     Dijkstra(1, n, 0, 1);     Dijkstra(n, 1, 1, 2);     build(1, 1, cnt);     for (long long i = 1; i <= m; i++)       if (!~id[i]) {         modify(1, 1, cnt, l[E[i][0]] + 1, r[E[i][1]],                diss[E[i][0]] + E[i][2] + dist[E[i][1]]);         modify(1, 1, cnt, l[E[i][1]] + 1, r[E[i][0]],                diss[E[i][1]] + E[i][2] + dist[E[i][0]]);       }     long long ans, tmpw;     for (long long i = 1, tmp; i <= Q; i++) {       scanf( %lld%lld , &tmp, &tmpw);       ans = diss[n];       if (id[tmp] != -1) {         ans -= E[tmp][2] - tmpw;         if (tmpw > E[tmp][2]) ans = min(ans, query(1, 1, cnt, id[tmp]));       } else {         if (tmpw < E[tmp][2]) {           ans = min(ans, diss[E[tmp][0]] + tmpw + dist[E[tmp][1]]);           ans = min(ans, diss[E[tmp][1]] + tmpw + dist[E[tmp][0]]);         }       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   #pragma GCC optimize( unroll-loops )   using namespace std;   const long double eps = 1e-7;   const int inf = 1000000010;   const long long INF = 10000000000000010LL;   const int mod = 1000000007;   const int MAXN = 2010, LOG = 20;   int n, m, k, u, v, x, y, t, a, b, ans;   string S[MAXN];   int lcp[MAXN];   int dp[MAXN][MAXN], N = 1;   int solve(int l, int r) {     if (l == r) return 0;     int id = N++, mid = l;     for (int i = l + 1; i < r; i++)       if (lcp[i] < lcp[mid]) mid = i;     int L = solve(l, mid), sl = mid - l + 1;     int R = solve(mid + 1, r), sr = r - mid;     for (int i = 0; i <= sl; i++)       for (int j = 0; j <= sr; j++)         dp[id][i + j] =             max(dp[id][i + j], dp[L][i] + dp[R][j] + i * j * lcp[mid]);     return id;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> S[i];     sort(S + 1, S + n + 1);     for (int i = 1; i < n; i++)       while (lcp[i] < min(S[i].size(), S[i + 1].size()) &&              S[i][lcp[i]] == S[i + 1][lcp[i]])         lcp[i]++;     solve(1, n);     cout << dp[1][k] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int MOD = 1000000007;   int L;   ll sum[8040];   int len[8040];   int last[8040];   int lazy[8040];   void init(int v, int l, int r, const vector<int> &xs) {     len[v] = xs[r] - xs[l - 1];     if (l < r) {       int md = (l + r) >> 1;       init(v << 1, l, md, xs);       init(v << 1 | 1, md + 1, r, xs);     }   }   void reset(int v, int l, int r, const vector<int> &go) {     lazy[v] = -1;     if (l == r) {       sum[v] = ll(len[v]) * (L - go[l]);       last[v] = go[l];       return;     }     int md = (l + r) >> 1;     reset(v << 1, l, md, go);     reset(v << 1 | 1, md + 1, r, go);     sum[v] = sum[v << 1] + sum[v << 1 | 1];     last[v] = last[v << 1 | 1];   }   void push(int v, int l, int r) {     if (lazy[v] != -1) {       last[v] = lazy[v];       sum[v] = ll(len[v]) * (L - lazy[v]);       if (l < r) {         lazy[v << 1] = lazy[v];         lazy[v << 1 | 1] = lazy[v];       }       lazy[v] = -1;     }   }   void modify(int v, int l, int r, int L, int R, int qv) {     push(v, l, r);     if (L > r || R < l) return;     if (L <= l && r <= R) {       lazy[v] = qv;       push(v, l, r);       return;     }     int md = (l + r) >> 1;     modify(v << 1, l, md, L, R, qv);     modify(v << 1 | 1, md + 1, r, L, R, qv);     sum[v] = sum[v << 1] + sum[v << 1 | 1];     last[v] = last[v << 1 | 1];   }   int walk(int v, int l, int r, int qv) {     push(v, l, r);     if (last[v] <= qv) return -1;     if (l == r) return l;     int md = (l + r) >> 1;     int ans = walk(v << 1, l, md, qv);     if (ans == -1) ans = walk(v << 1 | 1, md + 1, r, qv);     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int N, K;     cin >> N >> K >> L;     vector<int> X(N), Y(N), C(N);     vector<int> xs = {-1, L};     vector<int> ys = {-1, L};     for (int i = 0; i < N; ++i) {       cin >> X[i] >> Y[i] >> C[i];       --C[i];       xs.emplace_back(X[i]);       ys.emplace_back(Y[i]);     }     sort(xs.begin(), xs.end());     xs.resize(unique(xs.begin(), xs.end()) - xs.begin());     sort(ys.begin(), ys.end());     ys.resize(unique(ys.begin(), ys.end()) - ys.begin());     int NX = xs.size();     int NY = ys.size();     {       vector<int> order(N);       iota(order.begin(), order.end(), 0);       sort(order.begin(), order.end(), [&](int i, int j) {         return make_pair(Y[i], -X[i]) > make_pair(Y[j], -X[j]);       });       vector<int> newX(N), newY(N), newC(N);       for (int i = 0; i < N; ++i) {         newX[i] = X[order[i]];         newY[i] = Y[order[i]];         newC[i] = C[order[i]];       }       X.swap(newX), Y.swap(newY), C.swap(newC);     }     init(1, 1, NX - 2, xs);     int ans = 0;     for (int yr = 1; yr + 1 < NY; ++yr) {       vector<vector<int>> addAt(NX);       for (int i = 0; i < N; ++i) {         if (Y[i] <= ys[yr]) {           int xi = lower_bound(xs.begin(), xs.end(), X[i]) - xs.begin();           addAt[xi].emplace_back(C[i]);         }       }       int bad = K;       vector<int> cnts(K);       auto inc = [&](int z) {         if (++cnts[z] == 1) --bad;       };       auto dec = [&](int z) {         if (--cnts[z] == 0) ++bad;       };       vector<int> go(NX);       int ptr = 0;       for (int i = 1; i + 1 < NX; ++i) {         while (bad && ptr + 2 < NX) {           ptr++;           for (int z : addAt[ptr]) inc(z);         }         if (bad)           go[i] = L;         else           go[i] = xs[ptr];         for (int z : addAt[i]) dec(z);       }       reset(1, 1, NX - 2, go);       vector<int> prv(N);       vector<int> nxt(N);       vector<map<int, int>> mp(K);       for (int i = 0; i < N; ++i) {         if (Y[i] <= ys[yr]) {           auto it = mp[C[i]].lower_bound(X[i]);           if (it == mp[C[i]].end()) {             nxt[i] = -1;           } else {             nxt[i] = it->second;           }           it = mp[C[i]].upper_bound(X[i]);           if (it == mp[C[i]].begin()) {             prv[i] = -1;           } else {             prv[i] = prev(it)->second;           }           mp[C[i]][X[i]] = i;         }       }       auto remove = [&](int i) {         int xprv = (prv[i] == -1 ? -1 : X[prv[i]]);         int xcur = X[i];         int xnxt = (nxt[i] == -1 ? L : X[nxt[i]]);         int l = lower_bound(xs.begin(), xs.end(), xprv) - xs.begin() + 1;         int r = walk(1, 1, NX - 2, xnxt);         if (r == -1) r = NX - 1;         --r;         r = min(r, int(lower_bound(xs.begin(), xs.end(), xcur) - xs.begin()));         if (l <= r) modify(1, 1, NX - 2, l, r, xnxt);       };       ptr = N - 1;       for (int yl = 1; yl <= yr; ++yl) {         ll add = sum[1] % MOD * (ys[yr + 1] - ys[yr]) % MOD *                  (ys[yl] - ys[yl - 1]) % MOD;         ans = (ans + add) % MOD;         while (ptr >= 0 && Y[ptr] == ys[yl]) remove(ptr--);       }       assert(sum[1] == 0);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, next = 0;     scanf( %d , &n);     int a[n], b[n];     for (int i = 0; i < n; i++) {       scanf( %d %d , &a[i], &b[i]);     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (a[j] == b[i] && j != i) {           next++;           a[j] = 0;         }       }     }     printf( %d , n - next);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     return (a.second < b.second);   }   int main() {     int t, n, x;     cin >> n;     vector<pair<int, int>> arr;     vector<int> arr2(n + 2, 0);     set<int, greater<int>> st;     for (int i = 1; i <= n; ++i) {       cin >> x;       arr2[x] = 1;       arr.push_back(make_pair(i, x));     }     for (int i = 1; i < arr2.size() - 1; ++i) {       if (arr2[i] == 0) {         st.insert(i);       }     }     sort(arr.begin(), arr.end(), sortbysec);     for (int i = 0; i < arr.size(); ++i) {       for (auto j = st.begin(); j != st.end(); ++j) {         if (*j == arr[i].first) {           if (i == 0)             swap(arr[i].first, arr[i + 1].first);           else             swap(arr[i].first, arr[i - 1].first);         }         arr[i].second = *j;         st.erase(j);         break;       }     }     sort(arr.begin(), arr.end());     for (int i = 0; i < arr.size(); ++i) {       cout << arr[i].second <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, ans = 0, a, b;     cin >> n;     ans = 1;     for (int i = sqrt(n); i >= 1; i--) {       if (n % i == 0) {         k = i;         break;       }     }     cout << k <<     << n / k << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long a, count = 0;       cin >> a;       for (long long i = 0; i < 31; i++) {         if (a & (1 << i)) {           count++;         }       }       long long c = powl(2, count);       cout << c << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   const ll MAX = 1e18 + 1;   const int NN = 1e5 + 5;   const int mod = 1e9 + 7;   void solve() {     string s1;     cin >> s1;     char vow[] = { a ,  e ,  o ,  u ,  i };     vector<int> ans;     int i = 0;     while (i < s1.size()) {       set<char> s;       for (int j = i; j < min(i + 3, (int)s1.size()); j++) {         s.insert(s1[j]);       }       int cnt = 0;       if (s.size() != 1) {         for (auto ch : s) {           for (int y = 0; y < 5; y++) {             if (ch == vow[y]) cnt++;           }         }         if (cnt == 0) {           ans.push_back(i + 1);           i++;         }       }       i++;     }     int k = 0;     for (int y = 0; y < s1.size(); y++) {       cout << s1[y];       if (k < ans.size() && y == ans[k]) {         cout <<    ;         k++;       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int tt = 1;     while (tt--) solve();     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int Day12[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   int prime100[] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37,  41, 43,                     47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103};   template <typename T>   inline bool isLeap(T y) {     return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false);   }   template <typename T>   inline T BIGMOD(T A, T B, T M = 1000000007) {     T ret = 1;     while (B) {       if (B & 1) ret = (ret * A) % M;       A = (A * A) % M;       B = B >> 1;     }     return ret;   }   template <typename T>   inline T BigMod(T A, T B, T M) {     T ret = 1;     while (B) {       if (B & 1) ret = (ret * A) % M;       A = (A * A) % M;       B = B >> 1;     }     return ret;   }   long long int MySqrt(long long int n) {     long long int p = sqrt(n);     if ((p + 1) * (p + 1) <= n)       return p + 1;     else if (p * p <= n)       return p;     else       return p - 1;   }   long long int MyPow(long long int x, long long int n) {     if (n == 0)       return 1;     else if (n % 2 == 0)       return MyPow(x * x, n / 2);     else       return x * MyPow(x * x, ((n - 1) / 2));   }   long long int modInverse(long long int n) {     return BIGMOD(n, (long long int)1000000007 - 2) % 1000000007;   }   int a[105][105];   int main() {     int n, d;     scanf( %d%d , &(n), &(d));     for (int i = 1; i <= d; i++) {       for (int j = 1; j <= n; j++) {         char ch;         cin >> ch;         a[i][j] = ch -  0 ;       }     }     int mx = INT_MIN;     int cnt = 0;     for (int i = 1; i <= d; i++) {       int present = 1;       for (int j = 1; j <= n; j++) {         present = present & a[i][j];       }       if (!present) {         cnt++;       } else         cnt = 0;       mx = max(mx, cnt);     }     cout << mx <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 5;   int n, m, W, H, a[N][N], b[N][N];   long long s[N][N];   int q[N];   inline long long S(int x1, int y1, int x2, int y2) {     return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];   }   struct BIT {     int c[N][N];     inline void add(int x1, int y1, int x2, int y2) {       add(x2, y2);       add(x1, y2);       add(x2, y1);       add(x1, y1);     }     inline void add(int x, int y) {       for (int i = x + 1; i <= n + 1; i += i & -i)         for (int j = y + 1; j <= m + 1; j += j & -j) c[i][j]++;     }     inline int qry(int x, int y) {       int ret = 0;       for (int i = x + 1; i; i -= i & -i)         for (int j = y + 1; j; j -= j & -j) ret += c[i][j];       return ret;     }     inline int qry(int x1, int y1, int x2, int y2) {       return qry(x2, y2) - qry(x1 - 1, y2) - qry(x2, y1 - 1) +              qry(x1 - 1, y1 - 1);     }   } bit;   int tot, X[N * N], Y[N * N];   long long V[N * N];   int main() {     scanf( %d%d%d%d , &n, &m, &W, &H);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         scanf( %d , &a[i][j]),             s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];     for (int i = 1; i <= n; i++) {       int h = 1, t = 0;       for (int j = 1; j <= m; j++) {         while (h <= t && j - q[h] >= H) h++;         while (h <= t && a[i][j] <= a[i][q[t]]) t--;         q[++t] = j;         b[i][j] = a[i][q[h]];       }     }     for (int j = 1; j <= m; j++) {       int h = 1, t = 0;       for (int i = 1; i <= n; i++) {         while (h <= t && i - q[h] >= W) h++;         while (h <= t && b[i][j] <= b[q[t]][j]) t--;         q[++t] = i;         a[i][j] = b[q[h]][j];       }     }     vector<pair<long long, int> > v;     for (int i = W; i <= n; i++)       for (int j = H; j <= m; j++)         v.push_back(pair<long long, int>(             S(i - W + 1, j - H + 1, i, j) - 1ll * W * H * a[i][j],             (i - 1) * m + j));     sort(v.begin(), v.end());     for (int i = 0, sz = v.size(); i < sz; i++) {       int y = (v[i].second - 1) % m + 1, x = (v[i].second - y) / m + 1;       if (bit.qry(x - W + 1, y - H + 1, x, y)) continue;       tot++;       X[tot] = x - W + 1;       Y[tot] = y - H + 1;       V[tot] = v[i].first;       bit.add(x - W + 1, y - H + 1, x, y);     }     printf( %d n , tot);     for (int i = 1; i <= tot; i++) printf( %d %d %lld n , X[i], Y[i], V[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 500000;   template <typename T>   class graph {    public:     struct edge {       int from;       int to;       T cost;     };     vector<edge> edges;     vector<vector<int>> g;     int n;     graph(int _n) : n(_n) { g.resize(n); }     virtual int add(int from, int to, T cost) = 0;   };   template <typename T>   class undigraph : public graph<T> {    public:     using graph<T>::edges;     using graph<T>::g;     using graph<T>::n;     undigraph(int _n) : graph<T>(_n) {}     int add(int from, int to, T cost = 1) {       assert(0 <= from && from < n && 0 <= to && to < n);       int id = (int)edges.size();       g[from].push_back(id);       g[to].push_back(id);       edges.push_back({from, to, cost});       return id;     }   };   template <typename T>   class cycle_finder {    public:     vector<int> was;     vector<int> st;     vector<vector<int>> cycles;     const graph<T> &g;     int bound_cnt;     int bound_size;     int total_size = 0;     cycle_finder(graph<T> &_g, int _bound_cnt = 1 << 30,                  int _bound_size = 1 << 30)         : g(_g), bound_cnt(_bound_cnt), bound_size(_bound_size) {       was.assign(g.n, -1);     }     void dfs(int v, int pe) {       if ((int)cycles.size() >= bound_cnt || total_size >= bound_size) {         return;       }       was[v] = (int)st.size();       for (int id : g.g[v]) {         if (id == pe) {           continue;         }         auto &e = g.edges[id];         int to = e.from ^ e.to ^ v;         if (was[to] >= 0) {           vector<int> cycle(1, id);           for (int j = was[to]; j < (int)st.size(); j++) {             cycle.push_back(st[j]);           }           cycles.push_back(cycle);           total_size += (int)cycle.size();           if ((int)cycles.size() >= bound_cnt || total_size >= bound_size) {             return;           }           continue;         }         if (was[to] == -1) {           st.push_back(id);           dfs(to, id);           st.pop_back();         }       }       was[v] = -2;     };     void find_cycles() {       for (int i = 0; i < g.n; i++) {         if (was[i] == -1) {           dfs(i, -1);         }       }     }   };   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     if (fopen( input.in ,  r )) {       freopen( input.in ,  r , stdin);       freopen( output.out ,  w , stdout);     }     int n, m;     cin >> n >> m;     undigraph<int> g(n);     int dp[maxn];     int subtractI[maxn];     for (int i = 0; i < n; i++) dp[i] = 1;     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       a--, b--;       g.add(a, b, i + 1);       subtractI[i] = -1;     }     cycle_finder<int> cycles(g);     cycles.find_cycles();     for (auto &cycle : cycles.cycles) {       int pos = (int)(min_element(cycle.begin(), cycle.end()) - cycle.begin());       rotate(cycle.begin(), cycle.begin() + pos, cycle.end());       pos = (int)(max_element(cycle.begin(), cycle.end()) - cycle.begin());       bool subCycle = true;       for (int i = 0; i < pos - 1; i++) {         int costA = g.edges[cycle[i]].cost;         int costB = g.edges[cycle[i + 1]].cost;         if (costA > costB) {           subCycle = false;           break;         }       }       for (int i = pos; i < cycle.size() - 1; i++) {         int costA = g.edges[cycle[i]].cost;         int costB = g.edges[cycle[i + 1]].cost;         if (costA < costB) {           subCycle = false;           break;         }       }       if (subCycle) {         subtractI[cycle[0]] = cycle[pos];       }     }     int hist[maxn];     for (int i = m - 1; i >= 0; i--) {       int a = g.edges[i].from, b = g.edges[i].to;       int sum = dp[a] + dp[b];       if (subtractI[i] != -1) sum -= hist[subtractI[i]];       dp[a] = sum;       dp[b] = sum;       hist[i] = sum;     }     for (int i = 0; i < n; i++) cout << dp[i] - 1 <<    ;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[3005][3005];   long long zkw[3005 << 2], M;   inline long long query(long long l, long long r) {     long long ret = 1234567891;     for (l += M - 1, r += M + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {       if (~l & 1) ret = min(ret, zkw[l ^ 1]);       if (r & 1) ret = min(ret, zkw[r ^ 1]);     }     return ret;   }   long long brr[3005][3005];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n, m, a, b;     cin >> n >> m >> a >> b;     long long x, y, z;     cin >> arr[1][1] >> x >> y >> z;     long long px, py;     for (long long i = 1; i <= n; i++)       for (long long j = 1; j <= m; j++) {         if (i != 1 || j != 1) arr[i][j] = (arr[px][py] * x + y) % z;         px = i, py = j;       }     for (M = 1; M - 2 < n; M <<= 1)       ;     for (long long j = 1; j <= m; j++) {       for (long long i = 1; i <= n; i++) zkw[i + M] = arr[i][j];       for (long long i = M - 1; i; i--)         zkw[i] = min(zkw[i << 1], zkw[i << 1 | 1]);       for (long long i = 1; i + a - 1 <= n; i++) brr[i][j] = query(i, i + a - 1);     }     long long ans = 0;     for (M = 1; M - 2 < m; M <<= 1)       ;     for (long long i = 1; i + a - 1 <= n; i++) {       for (long long j = 1; j <= m; j++) zkw[j + M] = brr[i][j];       for (long long j = M - 1; j; j--)         zkw[j] = min(zkw[j << 1], zkw[j << 1 | 1]);       for (long long r = b; r <= m; r++) {         long long l = r - b + 1;         ans += query(l, r);       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (a == 0 || b == 0) {       return 0;     }     long long s;     while (a != 0) {       b = b % a;       s = a;       a = b;       b = s;     }     return b;   }   int main() {     long long a, b;     cin >> a >> b;     long long g = gcd(a, b);     long long lcm = (a * b) / g;     long long toa = 0;     long long tob = 0;     long long pos = 0;     long long nexta = a;     long long nextb = b;     while (pos < lcm) {       if (nexta == nextb) {         if (a < b) {           tob += (nextb - pos);           pos = nextb;           nextb += b;         } else {           toa += (nexta - pos);           pos = nexta;           nexta += a;         }       } else if (nexta < nextb) {         toa += (nexta - pos);         pos = nexta;         nexta += a;       } else {         tob += (nextb - pos);         pos = nextb;         nextb += b;       }     }     if (toa == tob) {       cout <<  Equal  << endl;     } else if (toa > tob) {       cout <<  Dasha  << endl;     } else {       cout <<  Masha  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, mx = 0, my = (1 << 31) - 1, m2x = 0, m2y = (1 << 31) - 1, a, b;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d %d , &a, &b);       mx = max(mx, a);       my = min(my, b);     }     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d %d , &a, &b);       m2x = max(m2x, a);       m2y = min(m2y, b);     }     int res = 0;     if (m2x > my) res = max(res, m2x - my);     if (mx > m2y) res = max(res, mx - m2y);     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve(long long tc) {     long long n, m, k;     cin >> n >> m >> k;     vector<long long> v(n);     for (long long i = (0), ed = (n); i < (ed); ++i) {       cin >> v[i];     }     sort(v.begin(), v.end());     unsigned long long mx1 = v[n - 1], mx2 = v[n - 2];     if (0)       cout <<  :>>                <<  mx1             <<   :   << mx1 <<   n ,           cout <<  :>>                    <<  mx2                 <<   :   << mx2 <<   n ;     unsigned long long ans = mx2 * (m / (k + 1));     if (0)       cout <<  :>>                <<  ans             <<   :   << ans <<   n ;     m = m - (m / (k + 1));     ans += mx1 * m;     cout << ans <<   n ;   }   int32_t main() {     solve(1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int get() {     int f = 0, v = 0;     char ch;     while (!isdigit(ch = getchar()))       if (ch ==  - ) break;     if (ch ==  - )       f = 1;     else       v = ch -  0 ;     while (isdigit(ch = getchar())) v = v * 10 + ch -  0 ;     if (f)       return -v;     else       return v;   }   const int maxn = 100005, inf = 314000000;   vector<int> rule[maxn], g[maxn], belong[maxn];   struct data {     int t, v;     data() {}     data(int _t, int _v) { t = _t, v = _v; }     inline bool operator<(const data &b) const { return v > b.v; }   };   priority_queue<data> Q;   int T[maxn], cnt[maxn], n, m, a[maxn], b[maxn], val[maxn], vis[maxn];   void init() {     m = get(), n = get();     memset(a, 60, sizeof(a));     for (int i = 1; i <= m; i++) {       int x = get(), y = get(), t;       g[x].push_back(i), rule[i].push_back(x);       while (y--)         if ((t = get()) < 0)           val[i]++;         else           rule[i].push_back(t), belong[t].push_back(i), T[i]++;       if (!T[i]) a[x] = min(a[x], val[i]);       cnt[i] = val[i];     }   }   int dfs(int x) {     if (vis[x]) return b[x];     vis[x] = 1;     for (int i = 0; i < g[x].size(); i++) {       int id = g[x][i], flag = 1, tot = cnt[id];       for (int j = 1; j < rule[id].size() && flag; j++)         if (a[rule[id][j]] > inf) flag = 0;       if (!flag) continue;       flag = 0;       for (int j = 1; j < rule[id].size(); j++) {         int p = rule[id][j], v;         if (vis[p] == 1) {           flag = 1;           break;         }         v = dfs(p);         if (v == -2) {           flag = 1;           break;         }         tot = min(tot + v, inf);       }       if (flag) {         b[x] = -2;         break;       }       b[x] = max(b[x], tot);     }     vis[x] = 2;     return b[x];   }   void work() {     for (int i = 1; i <= n; i++)       if (a[i] < 1e9) Q.push(data(i, a[i]));     while (!Q.empty()) {       data tp = Q.top();       Q.pop();       int t = tp.t, v = tp.v;       if (a[t] != v) continue;       for (int i = 0; i < belong[t].size(); i++) {         int p = belong[t][i], s = rule[p][0];         val[p] = min(val[p] + v, inf);         T[p]--;         if (!T[p] && a[s] > val[p]) Q.push(data(s, a[s] = val[p]));       }     }     for (int i = 1; i <= n; i++)       if (a[i] <= inf) dfs(i);     for (int i = 1; i <= n; i++)       if (a[i] > inf)         puts( -1 -1 );       else         printf( %d %d n , a[i], b[i]);   }   int main() {     init();     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Set(int N, int pos) { return N |= (1LL << pos); }   int Reset(int N, int pos) { return N &= ~(1LL << pos); }   bool Check(int N, int pos) { return N & (1LL << pos); }   int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1};   inline void cn(long &n) {     n = 0;     long ch = getchar();     int sign = 1;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) sign = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       n = (n << 3) + (n << 1) + ch -  0 , ch = getchar();     n = n * sign;   }   template <class T>   void cmin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   void cmax(T &a, T b) {     if (b > a) a = b;   }   template <class T>   int len(const T &c) {     return (int)c.size();   }   template <class T>   int len(char c[]) {     return (int)strlen(c);   }   string itos(long n) {     string s;     while (n) {       s += (n % 10 + 48);       n /= 10;     }     reverse(s.begin(), s.end());     return s;   }   long stoi(string s) {     long n = 0;     for (int i(0), _n(len(s)); i < _n; ++i) n = n * 10 + (s[i] - 48);     return n;   }   int ar[100005][3];   int main() {     string s, second;     int q, l, r, x, y, z, m;     cin >> s;     for (int i(0), _n(len(s)); i < _n; ++i) {       ar[i + 1][0] = ar[i][0];       ar[i + 1][1] = ar[i][1];       ar[i + 1][2] = ar[i][2];       ar[i + 1][s[i] -  x ]++;     }     cin >> q;     for (int i(0), _n(q); i < _n; ++i) {       cin >> l >> r;       x = ar[r][0] - ar[l - 1][0];       y = ar[r][1] - ar[l - 1][1];       z = ar[r][2] - ar[l - 1][2];       int sr[3];       sr[0] = x;       sr[1] = y;       sr[2] = z;       sort(sr, sr + 3);       if (sr[2] - sr[0] <= 1 || (r - l) < 2)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void out() {     cout.flush();     cerr << endl <<  OUT  << endl;     int bred;     cin >> bred;     exit(0);   }   void updMax(int& x, int y) {     if (y > x) x = y;   }   int sum10(int x, int y) {     x += y;     if (x >= 10) x -= 10;     return x;   }   int cs[10];   int n, m;   string a, bs[1024];   int dp[1024][1024], sorted[1024][1024], h[10], whereGo[1024][10], res[1024][10],       curc[1024];   int getCipher(const string& s, int ind) {     if (ind >= s.length()) return 0;     return s[ind] -  0 ;   }   void buildSorted() {     for (int j = 0; j < n; ++j) sorted[0][j] = j;     for (int i = 0; i < 1002; ++i) {       memset(h, 0, sizeof(h));       for (int j = 0; j < n; ++j) h[getCipher(bs[j], i)]++;       for (int j = 1; j < 10; ++j) h[j] += h[j - 1];       for (int j = n - 1; j >= 0; --j) {         int v = sorted[i][j];         sorted[i + 1][--h[getCipher(bs[v], i)]] = v;       }     }   }   int ps[1024], qs[1024], revps[1024], revqs[1024];   void calcWhereGo(int pos) {     for (int i = 0; i < n; ++i) {       ps[i] = sorted[pos][i];       qs[i] = sorted[pos + 1][i];       revps[ps[i]] = i;       revqs[qs[i]] = i;     }     for (int c = 0; c <= 9; ++c) {       int curper = n;       for (int i = 0; i < n; ++i)         if (getCipher(bs[ps[i]], pos) + c >= 10)           curper = min(curper, revqs[ps[i]]);       whereGo[n][c] = curper;       for (int i = n - 1; i >= 0; --i) {         if (getCipher(bs[ps[i]], pos) + c + 1 >= 10)           curper = min(curper, revqs[ps[i]]);         whereGo[i][c] = curper;       }     }   }   void calcRes(int pos) {     for (int c = 0; c <= 9; ++c) {       int sum = 0;       for (int i = 0; i < n; ++i) {         curc[i] = sum10(getCipher(bs[sorted[pos][i]], pos), c);         if (curc[i] == 0 && pos >= bs[sorted[pos][i]].length())           curc[i] = 0;         else           curc[i] = cs[curc[i]];         sum += curc[i];       }       res[n][c] = sum;       int cc = sum10(c, 1);       for (int i = n - 1; i >= 0; --i) {         sum -= curc[i];         curc[i] = sum10(getCipher(bs[sorted[pos][i]], pos), cc);         sum += cs[curc[i]];         res[i][c] = sum;       }     }   }   void calcDynamics() {     for (int i = 0; i <= 1001; ++i)       for (int j = 0; j <= n; ++j) dp[i][j] = -1000000000;     dp[0][n] = 0;     for (int i = 0; i <= 1000; ++i) {       calcWhereGo(i);       calcRes(i);       int l, r;       if (i >= m)         l = r = 0;       else if (a[i] ==  ? ) {         r = 9;         l = (i == m - 1);       } else         l = r = a[i] -  0 ;       for (int j = 0; j <= n; ++j)         for (int c = l; c <= r; ++c)           updMax(dp[i + 1][whereGo[j][c]], dp[i][j] + res[j][c]);     }   }   void solve() {     cin >> a >> n;     reverse(a.begin(), a.end());     m = a.length();     for (int i = 0; i < n; ++i) {       cin >> bs[i];       reverse(bs[i].begin(), bs[i].end());       while (bs[i].length() < m) bs[i].push_back( 0 );     }     for (int i = 0; i < 10; ++i) cin >> cs[i];     buildSorted();     calcDynamics();     int ans = 0;     for (int i = 0; i <= n; ++i) ans = max(ans, dp[1001][i]);     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     solve();     out();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const int mof = 1000000007;   const int inf = 1000000010;   const long long llinf = 1000000007000000007;   const double pi = 3.14159265359;   int a[211111];   int a2[211111];   int t[211111];   int t2[211111];   int n, k, A, B, q;   int sum(int r) {     int result = 0;     for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r];     return result;   }   void inc(int i, int delta) {     if (a[i] + delta > B) {       delta = B - a[i];       a[i] = B;     } else {       a[i] = a[i] + delta;     }     for (; i < n; i = (i | (i + 1))) t[i] += delta;   }   int sum(int l, int r) { return sum(r) - sum(l - 1); }   void init(int a[]) {     for (unsigned i = 0; i < n; i++) inc(i, a[i]);   }   int sum2(int r) {     int result = 0;     for (; r >= 0; r = (r & (r + 1)) - 1) result += t2[r];     return result;   }   void inc2(int i, int delta) {     if (a2[i] + delta > A) {       delta = A - a2[i];       a2[i] = A;     } else {       a2[i] = a2[i] + delta;     }     for (; i < n; i = (i | (i + 1))) t2[i] += delta;   }   int sum2(int l, int r) { return sum2(r) - sum2(l - 1); }   void init2(int a[]) {     for (unsigned i = 0; i < n; i++) inc2(i, a[i]);   }   int main() {     scanf( %d%d%d%d%d , &n, &k, &A, &B, &q);     while (q--) {       int type;       scanf( %d , &type);       if (type == 1) {         int d, a;         scanf( %d%d , &d, &a);         inc(d - 1, a);         inc2(d - 1, a);       } else {         int p;         scanf( %d , &p);         printf( %d n , sum(0, p - 2) + sum2(p + k - 1, n - 1));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 210;   const double eps = 1e-9;   int n;   int lsh[maxn * 2], ln, K;   map<int, int> mt;   int To[maxn];   int li[maxn], ri[maxn];   double f[2][maxn][maxn];   double ans[maxn][maxn];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++)       scanf( %d%d , &li[i], &ri[i]), lsh[++ln] = li[i], lsh[++ln] = ri[i];     sort(lsh + 1, lsh + ln + 1);     lsh[0] = lsh[1] - 1;     K = 0;     for (int i = 1; i <= ln; i++)       if (lsh[i] != lsh[i - 1]) To[mt[lsh[i]] = ++K] = lsh[i];     for (int i = 1; i <= n; i++) li[i] = mt[li[i]], ri[i] = mt[ri[i]];     for (int i = 1; i <= n; i++) {       for (int x = li[i]; x < ri[i]; x++) {         double inow = (double)(To[x + 1] - To[x]) / (To[ri[i]] - To[li[i]]);         for (int x1 = 0; x1 <= n; x1++)           for (int x2 = 0; x2 <= n; x2++) f[0][x1][x2] = f[1][x1][x2] = 0;         int now = 1;         f[now][0][0] = 1.0;         for (int j = 1; j <= n; j++) {           if (i == j) continue;           int L = To[li[j]], R = To[ri[j]];           now = !now;           if (x < li[j]) {             for (int x1 = 0; x1 < j; x1++)               for (int x2 = 0; x2 < j - x1; x2++)                 if (f[!now][x1][x2] > eps) {                   double tmp = f[!now][x1][x2];                   f[!now][x1][x2] = 0;                   f[now][x1][x2] += tmp;                 }           } else if (ri[j] <= x) {             for (int x1 = 0; x1 < j; x1++)               for (int x2 = 0; x2 < j - x1; x2++)                 if (f[!now][x1][x2] > eps) {                   double tmp = f[!now][x1][x2];                   f[!now][x1][x2] = 0;                   f[now][x1 + 1][x2] += tmp;                 }           } else {             double p1 = (double)(To[x] - L) / (R - L),                    p2 = (double)(To[x + 1] - To[x]) / (R - L),                    p3 = (double)(R - To[x + 1]) / (R - L);             for (int x1 = 0; x1 < j; x1++)               for (int x2 = 0; x2 < j - x1; x2++)                 if (f[!now][x1][x2] > eps) {                   double tmp = f[!now][x1][x2];                   f[!now][x1][x2] = 0;                   f[now][x1 + 1][x2] += tmp * p1;                   f[now][x1][x2 + 1] += tmp * p2;                   f[now][x1][x2] += tmp * p3;                 }           }         }         for (int x1 = 0; x1 < n; x1++)           for (int x2 = 0; x2 < n - x1; x2++)             if (f[now][x1][x2] > eps) {               double tmp = f[now][x1][x2], temp = 1.0 / (x2 + 1.0);               for (int l = 0; l <= x2; l++)                 ans[i][x1 + l + 1] += inow * tmp * temp;             }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j < n; j++) printf( %.10lf  , ans[i][j]);       printf( %.10lf n , ans[i][n]);     }     return 0;   }


#include <bits/stdc++.h>   int ans[1000010], p[1000010], mem[1000010], q[1000010];   int main() {     int n, t, i, j;     memset(q, 0, sizeof(q));     scanf( %d , &n);     for (i = 0; i < n; i++) scanf( %d , &p[i]);     scanf( %d , &t);     for (i = 0; i < t; i++) {       scanf( %d , &j);       q[j - 1] = 1;     }     int cnt = 0, num = n - 1;     if (n % 2 == 1) {       printf( NO n );       return 0;     }     for (i = n - 1; i >= 0; i--) {       if (!cnt || q[i] == 1 || mem[cnt] != p[i]) {         mem[++cnt] = p[i];         ans[i] = -p[i];       } else {         ans[i] = p[i];         cnt--;       }     }     if (cnt != 0)       printf( NO n );     else {       printf( YES n );       for (i = 0; i < n - 1; i++) printf( %d  , ans[i]);       printf( %d n , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, n, k;   char s[4000];   int opx[4000];   int opy[4000];   int count2;   void Reverse(char *s, int a, int b) {     for (int i = a, j = b; i < j; i++, j--) {       char c = s[i];       s[i] = s[j];       s[j] = c;     }   }   int main() {     cin >> t;     while (t--) {       cin >> n >> k;       cin >> s;       int count1 = 0;       while (count1 < 2 * k - 3) {         if (s[count1] ==  ( ) {           for (int i = count1 + 1; i < n; i++) {             if (s[i] ==  ) ) {               count2++;               opx[count2] = count1 + 2;               opy[count2] = i + 1;               Reverse(s, count1 + 1, i);               break;             }           }         } else {           for (int i = count1 + 1; i < n; i++) {             if (s[i] ==  ( ) {               count2++;               opx[count2] = count1 + 1;               opy[count2] = i + 1;               Reverse(s, count1, i);               break;             }           }         }         count1 += 2;       }       while (count1 < n && s[count1] ==  ( ) count1++;       while (count1 < n) {         int count3 = count1;         while (count3 < n && s[count3] ==  ) ) count3++;         if (count3 < n) {           count2++;           opx[count2] = count1 + 1;           opy[count2] = count3 + 1;           Reverse(s, count1, count3);           count1++;         } else {           break;         }       }       cout << count2 << endl;       for (int i = 1; i <= count2; i++) {         cout << opx[i] <<     << opy[i] << endl;       }       count2 = 0;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5004;   int main() {     int a = 1, b = 1, n;     string s;     cin >> n;     while (n--) {       cin >> s;       if (s ==  UL  || s ==  DR )         ++a;       else if (s.size() == 2)         ++b;       else         ++a, ++b;     }     printf( %lld n , 1ll * a * b);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k = 8;     int t = 4;     int p = 2;     int s = 6;     int n;     while (cin >> n) {       if (n == 0)         cout << 1 << endl;       else if (n % 4 == 1)         cout << k << endl;       else if (n % 4 == 2)         cout << t << endl;       else if (n % 4 == 3)         cout << p << endl;       else if (n % 4 == 0)         cout << s << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = (int)(1e5) + 10;   int n, m, a, b;   vector<pair<int, pair<int, int> > > ed;   pair<int, int> res[NMAX];   int last;   int now[NMAX];   bool func(pair<int, pair<int, int> > i, pair<int, pair<int, int> > j) {     if (i.first == j.first)       return (i.second.first > j.second.first);     else       return (i.first < j.first);   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d %d , &a, &b);       ed.push_back(make_pair(a, make_pair(b, i)));     }     sort(ed.begin(), ed.end(), func);     int i = 1;     last = 1;     for (int kk = 0; kk <= (int)ed.size() - 1; kk++) {       int val = ed[kk].first;       int type = ed[kk].second.first;       int vt = ed[kk].second.second;       if (type == 1) {         res[vt] = make_pair(last, 1 + last);         last++;         now[last] = last;       } else {         bool co = 0;         while (!co) {           for (int j = min(i - 2, now[i] - 1); j >= 1; j--) {             res[vt] = make_pair(j, i);             co = 1;             now[i] = j;             break;           }           if (!co) i++;           if (i > last) {             cout << -1;             return 0;           }         }       }     }     if (last != n) {       cout << -1;       return 0;     }     for (int i = 1; i <= m; i++)       cout << res[i].first <<     << res[i].second << endl;   }


#include <bits/stdc++.h>   using namespace std;   int st[610], aim[(300010)], nxt[(300010)], ln;   int flow[(300010)], con[(300010)], cost[(300010)];   int inq[610], dis[610];   int g[610], e[610];   int q[610];   int a[610];   int d[610 << 3], tag[610 << 3], dn;   int num[610];   int N, source, remit;   int ans;   int n, m;   void in_edge(int x, int y, int f, int c) {     aim[ln] = y;     nxt[ln] = st[x];     flow[ln] = 0;     con[ln] = f;     cost[ln] = c;     st[x] = ln++;     aim[ln] = x;     nxt[ln] = st[y];     flow[ln] = con[ln] = 0;     cost[ln] = -c;     st[y] = ln++;   }   int calc(int x) {     int ret = 0;     while (x) ret += (x & 1), x >>= 1;     return ret;   }   int spfa() {     int qn = 1;     for (int i = 0; i <= remit; i++) dis[i] = (10000000);     memset(inq, 0, sizeof(inq));     dis[source] = 0;     inq[source] = 1;     q[0] = source;     for (int p = 0; p != qn;) {       int u = q[p];       p++;       if (p == 610) p = 0;       inq[u] = 0;       for (int i = st[u]; i != -1; i = nxt[i])         if (flow[i] < con[i]) {           int v = aim[i];           if (dis[u] + cost[i] < dis[v]) {             dis[v] = dis[u] + cost[i];             g[v] = u;             e[v] = i;             if (!inq[v]) {               inq[v] = 1;               q[qn] = v;               ++qn;               if (qn == 610) qn = 0;             }           }         }     }     if (dis[remit] == (10000000)) return 0;     int delta = (10000000);     for (int i = remit; i != source; i = g[i])       delta = min(delta, con[e[i]] - flow[e[i]]);     for (int i = remit; i != source; i = g[i]) {       int x = e[i];       flow[x] += delta;       flow[x ^ 1] -= delta;     }     ans += delta * dis[remit];     return 1;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %d , a + i);     source = n + n + m;     remit = source + 1;     memset(st, -1, sizeof(st));     ln = 0;     for (int i = 0; i < m; i++) in_edge(source, (n << 1) + i, 1, 0);     for (int i = 0; i < n; i++) {       in_edge(source, n + i, 1, 0);       in_edge(i, remit, 1, 0);     }     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) in_edge((n << 1) + i, j, 1, calc(a[j]));     }     for (int i = 0; i < n; i++)       for (int j = i + 1; j < n; j++)         if (a[i] == a[j])           in_edge(n + i, j, 1, 0);         else           in_edge(n + i, j, 1, calc(a[j]));     ans = 0;     while (spfa())       ;     int dn = 0;     for (int i = 0; i < n; i++) {       int flag = 0;       for (int j = st[i]; j != -1; j = nxt[j])         if (flow[j] < con[j]) {           int v = aim[j];           if (v >= (n << 1) && v < source) {             tag[dn] = 0;             d[dn++] = i;             tag[dn] = 1;             d[dn++] = v - (n << 1);             num[i] = v - (n << 1);           } else if (v >= n && v <= (n << 1)) {             int p = v - n;             if (a[p] != a[i]) {               tag[dn] = 0;               d[dn++] = i;             }             tag[dn] = 1;             d[dn++] = num[p];             num[i] = num[p];           }         }     }     printf( %d %d n , dn, ans);     for (int i = 0; i < dn; i++)       if (!tag[i]) {         printf( %c=%d n ,  a  + num[d[i]], a[d[i]]);       } else {         printf( print(%c) n ,  a  + d[i]);       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool poate[5020][510];   pair<int, int> from[5020][510];   int last[5020][510];   queue<pair<int, int>> de_calc;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int d, s;     cin >> d >> s;     for (int i = 1; i <= 9; i++) {       poate[i][i % d] = 1;       from[i][i % d] = {-1, -1};       last[i][i % d] = i;       de_calc.push({i, i % d});     }     bool found = 0;     while (!de_calc.empty() && !found) {       int i = de_calc.front().first, j = de_calc.front().second;       de_calc.pop();       if (i > s) continue;       for (int cif = 0; cif <= 9; cif++) {         int j2 = (10 * j + cif) % d, i2 = i + cif;         if (poate[i2][j2]) continue;         poate[i2][j2] = 1;         from[i2][j2] = {i, j};         last[i2][j2] = cif;         de_calc.push({i2, j2});         if (i2 == s && j2 == 0) {           found = 1;           break;         }       }     }     if (!poate[s][0]) {       cout <<  -1 n ;       return 0;     }     string ans =   ;     for (int i = s, j = 0;;) {       ans += last[i][j] +  0 ;       if (from[i][j].first == -1) break;       int j2 = from[i][j].second;       i = from[i][j].first;       j = j2;     }     reverse(ans.begin(), ans.end());     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 10;   signed main() {     ios::sync_with_stdio(false);     long long n, a, b;     cin >> n >> a >> b;     long long ans = 0;     vector<long long> x(n), y(n);     for (long long i = 0; i < n; i++) {       cin >> x[i] >> y[i];       for (long long j = 0; j < i; j++) {         for (long long _ = 2; _--;) {           for (long long __ = 2; __--;) {             for (long long ___ = 2; ___--;) {               if (x[i] + x[j] <= a && max(y[i], y[j]) <= b) {                 ans = max(ans, x[i] * y[i] + x[j] * y[j]);               }               swap(x[j], y[j]);             }             swap(x[i], y[i]);           }           swap(a, b);         }       }     }     cout << ans <<   n ;     return 0;   }