#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   string to_string(pair<A, B> p);   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p);   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p);   string to_string(const string& s) { return     + s +    ; }   string to_string(const char* s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   string to_string(vector<bool> v) {     bool first = true;     string res =  { ;     for (int i = 0; i < static_cast<int>(v.size()); i++) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(v[i]);     }     res +=  } ;     return res;   }   template <size_t N>   string to_string(bitset<N> v) {     string res =   ;     for (size_t i = 0; i < N; i++) {       res += static_cast<char>( 0  + v[i]);     }     return res;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto& x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ) ;   }   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int h, w;     cin >> h >> w;     vector<vector<int>> a(h, vector<int>(w));     vector<pair<int, int>> pos(h * w);     for (int i = 0; i < h; i++) {       for (int j = 0; j < w; j++) {         cin >> a[i][j];         --a[i][j];         pos[a[i][j]] = make_pair(i, j);       }     }     vector<vector<int>> b(h, vector<int>(w, -1));     int pi = -1;     int pj = -1;     vector<pair<int, int>> cells;     int ptr = 0;     for (int val = h * w - 1; val >= 0; val--) {       for (int i = 0; i < h; i++) 42;       42;       auto& at = pos[val];       int vi = at.first;       int vj = at.second;       bool row_max = true;       for (int j = 0; j < w; j++) {         row_max &= (a[vi][j] <= val);       }       bool col_max = true;       for (int i = 0; i < h; i++) {         col_max &= (a[i][vj] <= val);       }       42;       if (row_max && col_max) {         b[++pi][++pj] = val;         for (int j = pj - 1; j >= 0; j--) {           if (b[pi][j] == -1) {             cells.emplace_back(pi, j);           }         }         for (int i = pi - 1; i >= 0; i--) {           if (b[i][pj] == -1) {             cells.emplace_back(i, pj);           }         }         continue;       }       if (row_max) {         b[++pi][pj] = val;         for (int j = pj - 1; j >= 0; j--) {           if (b[pi][j] == -1) {             cells.emplace_back(pi, j);           }         }         continue;       }       if (col_max) {         b[pi][++pj] = val;         for (int i = pi - 1; i >= 0; i--) {           if (b[i][pj] == -1) {             cells.emplace_back(i, pj);           }         }         continue;       }       assert(ptr < (int)cells.size());       b[cells[ptr].first][cells[ptr].second] = val;       ++ptr;     }     for (int i = 0; i < h; i++) {       for (int j = 0; j < w; j++) {         if (j > 0) {           cout <<    ;         }         cout << b[i][j] + 1;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10, mod = 1e9 + 7;   int k, n, sz, last;   char s[N];   string ret;   long long cnt[2 * N], dp[2 * N];   char inp[N];   struct state {     int link, len, next[26];   };   state st[2 * N];   bool vis[2 * N];   void sa_init() {     st[0].len = 0;     st[0].link = -1;     for (int i = 0; i < 26; i++) st[0].next[i] = -1;     sz = 1;     last = 0;   }   void sa_extend(char ch) {     int cur = sz++;     int c = ch -  a ;     st[cur].len = st[last].len + 1;     for (int i = 0; i < 26; i++) st[cur].next[i] = -1;     int p = last;     while (p != -1 && st[p].next[c] == -1) {       st[p].next[c] = cur;       p = st[p].link;     }     if (p == -1)       st[cur].link = 0;     else {       int q = st[p].next[c];       if (st[q].len == st[p].len + 1)         st[cur].link = q;       else {         int clone = sz++;         for (int i = 0; i < 26; i++) st[clone].next[i] = -1;         st[clone] = st[q];         st[clone].len = st[p].len + 1;         while (p != -1 && st[p].next[c] == q) {           st[p].next[c] = clone;           p = st[p].link;         }         st[cur].link = st[q].link = clone;       }     }     last = cur;     cnt[cur] = 1;   }   bool cmp(int x, int y) { return st[x].len > st[y].len; }   void dfs(int u) {     vis[u] = 1;     dp[u] = cnt[u];     for (int j = 0; j < 26; j++) {       int v = st[u].next[j];       if (v == -1) continue;       if (!vis[v]) dfs(v);       dp[u] += dp[v];     }   }   void solve(int u) {     for (int j = 0; j < 26; j++) {       int v = st[u].next[j];       if (v == -1) continue;       if (dp[v] >= k) {         k -= cnt[v];         ret.push_back(j +  a );         if (k > 0)           solve(v);         else           k = 0;         break;       }       k -= dp[v];     }   }   int main() {     scanf( %s%d , &s, &k);     n = strlen(s);     sa_init();     for (int i = 0; i < n; i++) sa_extend(s[i]);     vector<int> v(sz);     iota(v.begin(), v.end(), 0);     sort(v.begin(), v.end(), cmp);     for (int i = 0; i < sz; i++) {       cnt[st[v[i]].link] += cnt[v[i]];     }     dfs(0);     solve(0);     if (!k)       cout << ret <<   n ;     else       cout <<  No such line. n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100010], b[100010];   map<string, bool> mark;   bool word(char a) {     if (a >=  a  and a <=  z ) return true;     if (a >=  A  and a <=  Z ) return true;     return false;   }   int main() {     int n;     cin >> n;     string a;     cin >> a;     int ind = 0;     int inside = 0;     int ans1 = 0;     int ans2 = 0;     while (ind < n) {       if (a[ind] ==  _ ) {         ind++;         continue;       }       if (a[ind] ==  ( ) {         inside++;         ind++;         continue;       }       if (a[ind] ==  ) ) {         inside--;         ind++;         continue;       }       int leng = 0;       string cur =   ;       while (word(a[ind]) == true) {         if (ind == n - 1) {           cur += a[ind];           if (inside > 0) {             ans2++;           }           if (inside == 0) {             ans1 = max(ans1, int(cur.size()));           }           ind++;           break;         }         cur += a[ind];         ind++;       }       if (ind == n) {         break;       }       if (inside > 0) {         ans2++;       }       if (inside == 0) {         ans1 = max(ans1, int(cur.size()));       }     }     cout << ans1 <<     << ans2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   const long long MOD = 1e9 + 7;   template <class T>   inline void chmax(T &a, const T &b) {     if ((a) < (b)) (a) = (b);   }   template <class T>   inline void chmin(T &a, const T &b) {     if ((a) > (b)) (a) = (b);   }   template <typename T>   using PQ = priority_queue<T, vector<T>, greater<T>>;   struct Fast {     Fast() {       std::cin.tie(0);       ios::sync_with_stdio(false);     }   } fast;   long long query() {     long long n;     cin >> n;     vector<long long> x(n);     vector<long long> y(n);     vector<long long> L(n);     vector<long long> U(n);     vector<long long> R(n);     vector<long long> D(n);     long long l = -1e5;     long long r = 1e5;     long long d = -1e5;     long long u = 1e5;     for (long long i = (long long)(0); i < (long long)(n); ++i) {       cin >> x[i] >> y[i] >> L[i] >> U[i] >> R[i] >> D[i];       if (L[i] == 1 && R[i] == 1) {       }       if (L[i] == 0 && R[i] == 1) {         chmax(l, x[i]);       }       if (L[i] == 1 && R[i] == 0) {         chmin(r, x[i]);       }       if (L[i] == 0 && R[i] == 0) {         chmax(l, x[i]);         chmin(r, x[i]);       }       if (D[i] == 1 && U[i] == 1) {       }       if (D[i] == 0 && U[i] == 1) {         chmax(d, y[i]);       }       if (D[i] == 1 && U[i] == 0) {         chmin(u, y[i]);       }       if (D[i] == 0 && U[i] == 0) {         chmax(d, y[i]);         chmin(u, y[i]);       }     }     42;     42;     if (l <= r && d <= u) {       42;       printf( 1 %lld %lld n , l, d);     } else {       printf( 0 n );     }     return 0;   }   long long solve() {     long long Q;     cin >> Q;     for (long long q = (long long)(0); q < (long long)(Q); ++q) {       query();     }     return 0;   }   signed main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void smin(T &a, const U &b) {     if (a > b) a = b;   }   template <typename T, typename U>   inline void smax(T &a, const U &b) {     if (a < b) a = b;   }   template <class T>   inline void gn(T &first) {     char c, sg = 0;     while (c = getchar(), (c >  9  || c <  0 ) && c !=  - )       ;     for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ;          c = getchar())       first = (first << 1) + (first << 3) + c -  0 ;     if (sg) first = -first;   }   template <class T1, class T2>   inline void gn(T1 &x1, T2 &x2) {     gn(x1), gn(x2);   }   template <class T1, class T2, class T3>   inline void gn(T1 &x1, T2 &x2, T3 &x3) {     gn(x1, x2), gn(x3);   }   template <class T>   inline void print(T first) {     if (first < 0) {       putchar( - );       return print(-first);     }     if (first < 10) {       putchar( 0  + first);       return;     }     print(first / 10);     putchar(first % 10 +  0 );   }   template <class T>   inline void println(T first) {     print(first);     putchar(  n );   }   template <class T>   inline void printsp(T first) {     print(first);     putchar(   );   }   template <class T1, class T2>   inline void print(T1 x1, T2 x2) {     printsp(x1), println(x2);   }   template <class T1, class T2, class T3>   inline void print(T1 x1, T2 x2, T3 x3) {     printsp(x1), printsp(x2), println(x3);   }   int power(int a, int b, int m, int ans = 1) {     for (; b; b >>= 1, a = (long long)a * a % m)       if (b & 1) ans = (long long)ans * a % m;     return ans;   }   int size(int n) {     int ans = 0;     while (n) {       n /= 7;       ans++;     }     smax(ans, 1);     return ans;   }   int f[11], p[11];   int calc(int st, int ed) {     int ans = 0;     int nn = 0;     for (int i = ed; i >= st; i--) {       ans += p[i] * f[nn];       nn++;     }     return ans;   }   int main() {     int n, m;     gn(n, m);     int N = size(n - 1), M = size(m - 1);     if (N + M > 7) return puts( 0 );     f[0] = 1;     for (int i = 1; i < 11; i++) f[i] = f[i - 1] * 7;     for (int i = 0; i < 7; i++) p[i] = i;     int ans = 0;     do {       int a = calc(0, N - 1), b = calc(N, N + M - 1);       if (a < n && b < m) ans++;     } while (next_permutation(p, p + 7));     int num = 1;     for (int i = 2; i <= 7 - N - M; i++) num *= i;     println(ans / num);   }


#include <bits/stdc++.h>   using namespace std;   struct operacion {     string name;     int x;     operacion() {}     operacion(string name, int x) : name(name), x(x) {}   };   int n;   priority_queue<int, vector<int>, greater<int> > q;   vector<operacion> v;   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int cas = 0; cas < n; cas++) {       string name;       cin >> name;       if (name ==  insert ) {         int x;         cin >> x;         v.push_back(operacion(name, x));         q.push(x);       } else if (name ==  getMin ) {         int x;         cin >> x;         while (int(q.size()) > 0 and q.top() < x) {           v.push_back(operacion( removeMin , 0));           q.pop();         }         if (int(q.size()) == 0 or q.top() != x) {           v.push_back(operacion( insert , x));           q.push(x);         }         v.push_back(operacion( getMin , x));       } else if (name ==  removeMin ) {         if (int(q.size()) == 0) {           v.push_back(operacion( insert , 1));           q.push(1);         }         v.push_back(operacion( removeMin , 0));         q.pop();       }     }     cout << int(v.size()) << endl;     for (int i = 0; i < int(v.size()); i++) {       cout << v[i].name;       if (v[i].name !=  removeMin ) cout <<     << v[i].x;       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 155;   const int INF = 123456789;   struct edge {     int to, lim, pri, rev;     edge() : to(0), lim(0), pri(0), rev(0){};     edge(int to, int lim, int pri, int rev)         : to(to), lim(lim), pri(pri), rev(rev){};   };   vector<edge> nei[N];   char ss[105];   int hv[26];   int n, sl, s, t, cst, flw;   int d[N], f[N], p[N], v[N];   queue<int> q;   void add_edge(int x, int y, int l, int p) {     nei[x].push_back(edge(y, l, p, int(nei[y].size())));     nei[y].push_back(edge(x, 0, -p, int(nei[x].size()) - 1));   }   int spfa() {     for (int i = s; i <= t; i++) {       v[i] = 0;       d[i] = f[i] = INF;     }     d[s] = 0, v[s] = 1;     while (!q.empty()) q.pop();     q.push(s);     while (!q.empty()) {       int x = q.front();       q.pop();       v[x] = 0;       for (int i = 0; i < nei[x].size(); i++) {         int to = nei[x][i].to;         int lim = nei[x][i].lim;         int pri = nei[x][i].pri;         int rev = nei[x][i].rev;         if (lim == 0) continue;         if (d[to] > d[x] + pri) {           p[to] = rev;           d[to] = d[x] + pri;           f[to] = min(f[x], lim);           if (!v[to]) {             v[to] = 1;             q.push(to);           }         }       }     }     return d[t] != INF;   }   void mcmf() {     while (spfa()) {       flw += f[t];       cst += d[t] * f[t];       int x = t;       while (x != s) {         int r = p[x];         int to = nei[x][r].to;         int rev = nei[x][r].rev;         nei[x][r].lim += f[t];         nei[to][rev].lim -= f[t];         x = to;       }     }   }   int main() {     scanf( %s , &ss);     sl = strlen(ss);     int Sl = sl;     for (int i = 0; i < sl; i++) hv[ss[i] -  a ]++;     scanf( %d , &n);     s = 1;     t = n + 28;     for (int i = 0; i < 26; i++)       if (hv[i]) {         add_edge(n + 2 + i, t, hv[i], 0);         hv[i] = 0;       }     for (int i = 1; i <= n; i++) {       int x;       scanf( %s%d , ss, &x);       sl = strlen(ss);       for (int j = 0; j < sl; j++) hv[ss[j] -  a ]++;       add_edge(s, i + 1, x, 0);       for (int j = 0; j < 26; j++)         if (hv[j]) {           add_edge(i + 1, n + 2 + j, hv[j], i);           hv[j] = 0;         }     }     mcmf();     if (Sl == flw)       printf( %d n , cst);     else       puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1000000000000000;   bool sortCct(pair<long long, long long> a, pair<long long, long long> b) {     return a.first > b.first;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, i, t, g, c = 0;     cin >> n;     vector<long long> ct(n);     vector<pair<long long, long long> > ctt(n);     map<long long, long long> fr;     for (i = 0; i < n; i++) cin >> ct[i];     for (i = 0; i < n; i++) {       cin >> t;       ctt[i] = pair<long long, long long>(t, ct[i]);     }     sort(ctt.begin(), ctt.end(), sortCct);     fr[INF] = 1;     for (i = 0; i < n; i++) {       pair<long long, long long> k = ctt[i];       auto it = fr.lower_bound(k.second);       g = max(k.second, it->second);       c += (g - k.second) * k.first;       if (g == it->second and g == it->first)         fr.erase(it);       else if (g == it->second)         it->second++;       else if (g == it->first) {         fr[it->first - 1] = it->second;         fr.erase(it);       } else {         fr[g - 1] = it->second;         it->second = g + 1;       }     }     cout << c <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     char ch[105][105], ss[105][105];     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> ch[i];     }     for (int i = 0; i < n; i++) {       string s = ch[i], gu;       if (i % 2 == 0) {         for (int j = 0; j < m; j++) {           if (j % 2 == 0 && ch[i][j] !=  - ) {             gu +=  B ;           } else if (ch[i][j] !=  - ) {             gu +=  W ;           } else {             gu +=  - ;           }         }       } else {         for (int j = 0; j < m; j++) {           if (j % 2 == 0 && ch[i][j] !=  - ) {             gu +=  W ;           } else if (ch[i][j] !=  - ) {             gu +=  B ;           } else {             gu +=  - ;           }         }       }       cout << gu <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e9 + 7, N1 = 1e7 + 5, N2 = 1e6 + 3, N3 = 1e5 + 3;   long long binpow(long long a, long long b) {     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a;       a = a * a;       b >>= 1;     }     return res;   }   void solve() {     long long x, y;     cin >> x >> y;     if (y < x * 2)       cout <<  YES ;     else       cout <<  NO ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t1 = 1;     cin >> t1;     while (t1--) {       solve();       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1 << 12 | 5, Q = 1 << 16 | 5;   int n, q, B, a[N], p[N], cnt, id[Q];   vector<pair<int, int>> opt;   struct node {     int n;     vector<int> pos;     vector<vector<int>> id;     node(int _n = 0) {       n = _n;       pos.resize(n);       id.resize(n);       for (auto &v : id) {         v.resize(n);       }     }     inline int get(int l, int r) {       l = lower_bound(pos.begin(), pos.end(), l) - pos.begin();       r = upper_bound(pos.begin(), pos.end(), r) - pos.begin() - 1;       return l <= r ? id[l][r] : 0;     }   } sqr[N];   inline int merge(int x, int y) {     int res;     if (x && y) {       opt.push_back({x, y});       res = ++cnt;     } else {       res = x | y;     }     return res;   }   node solve(int l, int r, int s) {     if (l == r) {       node res(1);       res.pos = {p[s + l]};       res.id = {{p[s + l]}};       return res;     }     int mid = l + r >> 1;     node x = solve(l, mid, s);     node y = solve(mid + 1, r, s);     node z(x.n + y.n);     merge(x.pos.begin(), x.pos.end(), y.pos.begin(), y.pos.end(), z.pos.begin());     for (int i = 0; i < z.n; ++i) {       for (int j = i; j < z.n; ++j) {         int l = z.pos[i], r = z.pos[j];         z.id[i][j] = merge(x.get(l, r), y.get(l, r));       }     }     return z;   }   int main() {     scanf( %d%d , &n, &q);     B = sqrt(q);     cnt = n;     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       p[a[i]] = i;     }     for (int i = 1, j = 1; i <= n; i += B, ++j) {       sqr[j] = solve(0, min(i + B - 1, n) - i, i);     }     for (int t = 1; t <= q; ++t) {       int l, r;       scanf( %d%d , &l, &r);       for (int i = 1, j = 1; i <= n; i += B, ++j) {         id[t] = merge(id[t], sqr[j].get(l, r));       }     }     printf( %d n , cnt);     for (auto x : opt) {       printf( %d %d n , x.first, x.second);     }     for (int t = 1; t <= q; ++t) {       printf( %d%c , id[t],    n [t == q]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n, o = 0, e = 0;     cin >> n;     vector<long long> a(n);     for (long long i = 0; i < n; ++i) {       cin >> a[i];     }     for (long long i = 0; i < n; ++i) {       if (a[i] % 2 != i % 2) {         if (a[i] % 2 == 0)           e++;         else           o++;       }     }     if (o == e) {       cout << o <<   n ;     } else {       cout << -1 <<   n ;     }   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1 << 18;   bool a[N];   vector<int> res;   int x, n;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> x;     res.push_back(0);     a[0] = true;     for (int i = 1; i <= int((1 << n) - 1); i++) {       if (a[i ^ x]) continue;       res.push_back(i);       a[i] = true;     }     cout << res.size() - 1 <<   n ;     for (int i = 1; i <= int(res.size() - 1); i++) {       int t = res[i] ^ res[i - 1];       cout << t <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> adj[100005];   long long sn[100005], an[100005], level[100005];   long long got = 1;   void dfs(long long u, long long par, long long carry) {     if (sn[u] != -1 && sn[u] < carry) got = 0;     level[u] = level[par] + 1;     if (sn[u] != -1) {       an[u] = sn[u] - carry;       carry = sn[u];     } else       an[u] = 0;     for (auto it : adj[u]) {       if (it != par) dfs(it, u, carry);     }   }   void dfs2(long long u, long long par, long long carry) {     if (level[u] & 1) {       if (u != 1) {         an[u] = sn[u] - carry;         carry = sn[u];       }     } else {       long long mn = 1e15 + 7;       for (auto it : adj[u])         if (it != par) mn = min(mn, sn[it]);       if (mn != 1e15 + 7) {         an[u] = mn - carry;         carry = mn;       } else         an[u] = 0;     }     for (auto it : adj[u])       if (it != par) dfs2(it, u, carry);   }   int main() {     long long i, j, k;     long long n, m;     cin >> n;     for (i = 2; i <= n; i++) {       cin >> m;       adj[m].push_back(i);       adj[i].push_back(m);     }     for (i = 1; i <= n; i++) cin >> sn[i];     dfs(1, 0, sn[1]);     if (!got) {       cout << -1 << endl;       return 0;     }     dfs2(1, 0, sn[1]);     an[1] = sn[1];     long long ans = 0;     for (i = 1; i <= n; i++) ans += an[i];     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class Graph {     vector<int>* adjList;     vector<bool> init;     vector<bool> goal;     bool* visited;     int n;     vector<int> operate;       public:     Graph(int n) {       adjList = new vector<int>[n];       visited = new bool[n]();       this->n = n;     }     void initialise(bool value) { init.push_back(value); }     void goals(bool value) { goal.push_back(value); }     void edge(int u, int v) {       adjList[u].push_back(v);       adjList[v].push_back(u);     }     void print() {       for (int j = 0; j < operate.size(); ++j) {         cout << operate[j] << endl;       }     }     int achieveGoal(int root, int op1, int op2) {       int total = 0;       visited[root] = true;       int operations = op1;       if (operations & 1 == 1) {         init[root] = !init[root];       }       if (init[root] != goal[root]) {         init[root] = !init[root];         ++operations;         total += 1;         operate.push_back(root + 1);       }       for (int i = 0; i < adjList[root].size(); ++i) {         if (!visited[adjList[root][i]])           total += achieveGoal(adjList[root][i], op2, operations);       }       return total;     }   };   int main() {     int n, u, v;     cin >> n;     Graph g(n);     for (int i = 1; i < n; ++i) {       cin >> u >> v;       g.edge(u - 1, v - 1);     }     for (int i = 0; i < n; ++i) {       cin >> u;       g.initialise(u);     }     for (int i = 0; i < n; ++i) {       cin >> v;       g.goals(v);     }     cout << g.achieveGoal(0, 0, 0) << endl;     g.print();   }


#include <bits/stdc++.h>   using namespace std;   int countdigits(int n) {     int i, b = 0;     while (n) {       b++;       n = n / 10;     }     return b;   }   int main() {     int t, i, n, x, cnt = 0;     cin >> t;     for (i = 0; i < t; i++) {       cin >> n;       cnt = countdigits(n);       n = n % 10;       if (cnt == 1) {         x = (n - 1) * 10 + 1;       }       if (cnt == 2) {         x = (n - 1) * 10 + 3;       }       if (cnt == 3) {         x = (n - 1) * 10 + 6;       }       if (cnt == 4) {         x = (n - 1) * 10 + 10;       }       cout << x << endl;       x = 0;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &x) {     static char _c;     static bool _f;     x = 0;     _f = 0;     _c = getchar();     while (_c <  0  ||  9  < _c) {       if (_c ==  - ) _f = true;       _c = getchar();     }     while ( 0  <= _c && _c <=  9 ) {       x = (x << 1) + (x << 3) + (_c & 15);       _c = getchar();     }     if (_f) x = -x;   }   template <typename T>   inline void Min(T &x, T y) {     if (y < x) x = y;   }   template <typename T>   inline void Max(T &x, T y) {     if (x < y) x = y;   }   const int INF = 0x3f3f3f3f;   const double pi = (double)acos(-1.0);   const double eps = (double)1e-8;   const int e5 = (int)1e5 + 5;   const int MOD = (int)1e9 + 7;   inline int sig(double x) { return x < -eps ? -1 : eps < x; }   long long fp(long long a, long long n, long long mod = MOD) {     if (n < 0) a = fp(a, mod - 2, mod), n = -n;     long long res = 1;     for (; n; n >>= 1, a = a * a % mod)       if (n & 1) res = res * a % mod;     return res;   }   struct Mint {     int x;     Mint() { x = 0; }     Mint(int _x) : x(_x) {       if (x < 0 || x >= MOD) x = (x % MOD + MOD) % MOD;     }     Mint(long long _x) : x(_x) {       if (x < 0 || x >= MOD) x = (x % MOD + MOD) % MOD;     }     Mint operator-() const { return Mint(MOD - x); }     Mint operator+(const Mint &rhs) const {       return Mint(x + rhs.x >= MOD ? x + rhs.x - MOD : x + rhs.x);     }     Mint operator-(const Mint &rhs) const {       return Mint(x - rhs.x < 0 ? x - rhs.x + MOD : x - rhs.x);     }     Mint operator*(const Mint &rhs) const {       return Mint((long long)x * rhs.x % MOD);     }     Mint operator/(const Mint &rhs) const {       return Mint(x * fp(rhs.x, -1) % MOD);     }     Mint &operator+=(const Mint &rhs) {       x += rhs.x;       if (x >= MOD) x -= MOD;       return *this;     }     Mint &operator*=(const Mint &rhs) {       x = ((long long)x * rhs.x) % MOD;       return *this;     }     bool operator==(const Mint &rhs) const { return x == rhs.x; }     bool operator!=(const Mint &rhs) const { return x != rhs.x; }     friend ostream &operator<<(ostream &out, const Mint &rhs) {       return out << rhs.x;     }     friend istream &operator>>(istream &in, Mint &rhs) { return in >> rhs.x; }   };   const int maxn = (int)2e5 + 20;   const int maxm = (int)1e6 + 20;   const int N = 500 + 5;   struct PQ_tree {     int n, tot;     int fail;     vector<int> G[N << 2];     int ty[N << 2];     int sz[N << 2];     int szc[N << 2];     bool s[N];     inline int getstate(int u) {       if (szc[u] == 0) return 0;       if (szc[u] == sz[u]) return 2;       return 1;     }     void addson(int x, int y) {       if (y) G[x].push_back(y);     }     void join(int x, int y) {       for (auto v : G[y]) G[x].push_back(v);     }     int mergeP(vector<int> &vec) {       if (vec.size() == 0) return 0;       if (vec.size() == 1) return vec[0];       G[++tot] = vec;       return tot;     }     void init(int _n) {       n = _n;       tot = n + 1;       memset(ty, 0, sizeof(ty));       for (int i = 1; i <= n; i++) G[n + 1].push_back(i);       fail = 0;     }     void dfs(int u) {       sz[u] = u <= n;       szc[u] = u <= n && s[u];       for (auto v : G[u]) {         dfs(v);         sz[u] += sz[v];         szc[u] += szc[v];       }     }     int check(int u, int t) {       if (fail) return 0;       vector<int> vec[3];       for (auto v : G[u]) vec[getstate(v)].push_back(v);       if (vec[1].size() > 2 || (t && vec[1].size() > 1)) return fail = 1, 0;       if (t == 0 && vec[1].size() == 1 && vec[2].size() == 0)         return check(vec[1][0], 0);       if (ty[u] == 0) {         int p2 = mergeP(vec[2]);         if (t == 0) {           G[u] = vec[0];           if (vec[1].size() == 0)             addson(u, p2);           else {             int tmp1 = check(vec[1][0], 2);             addson(tmp1, p2);             if (vec[1].size() == 2) join(tmp1, check(vec[1][1], 1));             addson(u, tmp1);           }           return u;         } else {           ty[u] = 1;           G[u].clear();           addson(u, p2);           if (vec[1].size() == 1) join(u, check(vec[1][0], 1));           addson(u, mergeP(vec[0]));           if (t == 2) reverse(G[u].begin(), G[u].end());         }         return u;       } else {         if (getstate(G[u].front()) > getstate(G[u].back()))           reverse(G[u].begin(), G[u].end());         int flag = 0;         vector<int> tG;         for (auto v : G[u]) {           int sta = getstate(v);           if (sta == 0) {             if (flag == 1) flag = 2;             tG.push_back(v);           } else if (sta == 2) {             if (flag == 0)               flag = 1;             else if (flag == 2)               return fail = 2, 0;             tG.push_back(v);           } else {             int p1;             if (flag == 0)               flag = 1, p1 = check(v, 2);             else if (flag == 1)               flag = 2, p1 = check(v, 1);             else               return fail = 3, 0;             for (auto x : G[v]) tG.push_back(x);           }         }         if (t && flag == 2) return fail = 4, 0;         if (t == 1) reverse(tG.begin(), tG.end());         G[u] = tG;         return u;       }     }     void dfs_permutation(int u, vector<int> &per) {       if (u <= n) per.push_back(u);       for (auto v : G[u]) dfs_permutation(v, per);     }     vector<int> get_permutation() {       vector<int> res;       dfs_permutation(n + 1, res);       return res;     }     void restrict(vector<int> res) {       for (int i = 1; i <= n; i++) s[i] = 0;       for (auto x : res) s[x] = 1;       dfs(n + 1);       check(n + 1, 0);     }   };   void work() {     int n;     cin >> n;     PQ_tree *pq = new PQ_tree;     pq->init(n);     vector<string> s(n);     for (int i = 0; i < n; i++) {       cin >> s[i];       vector<int> res;       for (int j = 0; j < n; j++)         if (s[i][j] ==  1 ) res.push_back(j + 1);       pq->restrict(res);     }     if (pq->fail) {       cout <<  NO  << endl;     } else {       cout <<  YES  << endl;       vector<int> perm = pq->get_permutation();       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) cout << s[i][perm[j] - 1];         cout << endl;       }     }     delete pq;   }   int main(int argc, char **argv) {     int tc = 1;     for (int ca = 1; ca <= tc; ca++) {       work();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a, b;   int used[100005], d[100005];   vector<int> g[100005];   void bfs(int node) {     memset(used, 0, sizeof(used));     memset(d, 0, sizeof(d));     queue<int> q;     q.push(node);     used[node] = 1;     while (!q.empty()) {       int cur = q.front();       q.pop();       for (int i = 0; i < g[cur].size(); i++) {         int to = g[cur][i];         if (!used[to]) {           q.push(to);           d[to] = d[cur] + 1;           used[to] = 1;         }       }     }   }   int main() {     scanf( %d , &n);     scanf( %d , &m);     while (m--) {       scanf( %d , &a);       scanf( %d , &b);       g[a].push_back(b);       g[b].push_back(a);     }     bfs(1);     int start = 1, dis = 0;     for (int i = 2; i <= n; i++) {       if (dis < d[i]) {         dis = d[i];         start = i;       }     }     bfs(start);     int ans = 0;     for (int i = 1; i <= n; i++) {       ans = max(ans, d[i]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct targ {     long long x, r;     int id, s;   } tar[10004];   bool _x(targ a, targ b) { return a.x < b.x; }   bool _id(targ a, targ b) { return a.id < b.id; }   int fd(int l, int r, long long x) {     if (l == r) return l;     int m = l + r >> 1;     if (tar[m].x < x)       return fd(m + 1, r, x);     else       return fd(l, m, x);   }   int cnt = 0;   void sl(int now, int p, long long x, long long y) {     if ((x - tar[p].x) * (x - tar[p].x) + y * y <= tar[p].r * tar[p].r)       tar[p].s = now, cnt++;   }   int main() {     int n, m;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld%lld , &tar[i].x, &tar[i].r);       tar[i].id = i;       tar[i].s = -1;     }     sort(tar + 1, tar + n + 1, _x);     scanf( %d , &m);     long long x, y;     for (int i = 1; i <= m; i++) {       scanf( %lld%lld , &x, &y);       int p = fd(1, n, x);       if (tar[p].s == -1) sl(i, p, x, y);       if (p - 1 && tar[p - 1].s == -1) sl(i, p - 1, x, y);     }     sort(tar + 1, tar + n + 1, _id);     cout << cnt << endl;     for (int i = 1; i <= n; i++) {       printf( %d%c , tar[i].s,    n [i == n]);     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10, mod = 998244353;   int add(int a, int b) {     a += b;     if (a >= mod) {       a -= mod;     }     return a;   }   int sub(int a, int b) {     a -= b;     if (a < 0) {       a += mod;     }     return a;   }   int mul(long long a, int b) { return (a * b) % mod; }   int pwr(int x, int e) {     int res = 1;     while (e) {       if (e & 1) {         res = mul(res, x);       }       x = mul(x, x);       e /= 2;     }     return res;   }   struct tournament {     vector<int> p;     int h = 0, i;   };   vector<tournament> get_pairs(int a, int l, int r);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int k, a, h;     cin >> k >> a >> h;     vector<tournament> v1, v2;     v1 = get_pairs(a, 1, (1 << (k - 1)));     v2 = get_pairs(a, (1 << (k - 1)) + 1, (1 << k));     for (int tc = 0; tc < 2; ++tc) {       int sz = v1.size();       map<int, int> mp;       for (int i = 0; i < sz; ++i) {         mp[sub(h, add(v1[i].h, mul(v1[i].i, a)))] = i;       }       for (int i = 0; i < sz; ++i) {         int val = add(v2[i].h, mul(v2[i].i, mul(a, a)));         if (mp.find(val) != mp.end()) {           int ti = mp[val], tj = i;           int winner = v1[ti].i;           int second = v2[tj].i;           if (tc == 1) {             swap(ti, tj);           }           vector<int> ans = v1[ti].p;           for (auto& pos : v2[tj].p) {             ans.push_back(pos);           }           ans[winner - 1] = 1;           ans[second - 1] = 2;           for (auto& pos : ans) {             cout << pos <<    ;           }           cout <<   n ;           return 0;         }       }       swap(v1, v2);     }     cout << -1 <<   n ;   }   vector<tournament> get_pairs(int a, int l, int r) {     int n = (r - l + 1), tot = (1 << (n - 1));     vector<int> ids(n);     vector<tournament> ans;     iota(ids.begin(), ids.end(), l);     for (int ch = 0; ch < tot; ++ch) {       int mask = ch;       tournament t;       t.p.resize(n, 0);       queue<int> q;       for (int i = 0; i < n; ++i) {         q.push(i);       }       int cur_rank = n + 1, rank_cnt = n / 2;       while (q.size() > 1) {         int p1 = q.front();         q.pop();         int p2 = q.front();         q.pop();         if (mask & 1) {           t.p[p2] = cur_rank;           t.h = add(t.h, mul(ids[p2], pwr(a, cur_rank)));           q.push(p1);         } else {           t.p[p1] = cur_rank;           t.h = add(t.h, mul(ids[p1], pwr(a, cur_rank)));           q.push(p2);         }         mask /= 2;         --rank_cnt;         if (rank_cnt == 0) {           cur_rank = ((cur_rank - 1) / 2) + 1;           rank_cnt = (cur_rank - 1) / 2;         }       }       t.i = ids[q.front()];       ans.push_back(t);     }     return ans;   }


#include <bits/stdc++.h>   using namespace std;   const int sz = 1e5 + 10, mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, i, j;     long long k, cost = 0;     string s;     cin >> n >> k >> s;     int last[n + 1][26];     long long dp[n][n + 1], f[n + 1];     memset(last, -1, sizeof(last));     memset(dp, 0, sizeof(dp));     memset(f, 0, sizeof(f));     for (i = 0; i <= n; i++) {       for (j = 0; j < i; j++) {         last[i][s[j] -  a ] = j;       }     }     dp[0][1] = 1;     for (i = 1; i < n; i++) {       for (int len = 1; len <= n; len++) {         if (i + 1 < len) break;         if (len == 1) {           dp[i][len] = 1;           continue;         }         dp[i][len] = 0;         for (j = 0; j < 26; j++) {           int x = last[i][j];           if (x != -1) {             dp[i][len] += dp[x][len - 1];           }         }       }     }     f[0] = 1;     for (i = 0; i < 26; i++) {       if (last[n][i] != -1) {         for (int len = 1; len <= n; len++) {           f[len] += dp[last[n][i]][len];         }       }     }     bool foo = false;     for (int len = n; len >= 0; len--) {       if (k - f[len] <= 0) {         cost += 1LL * k * (n - len);         foo = true;         break;       } else {         cost += 1LL * f[len] * (n - len);         k -= f[len];       }     }     if (foo)       cout << cost <<   n ;     else       cout <<  -1 n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a, b;     cin >> n >> m >> a >> b;     if (m * a <= b)       cout << n * a <<   n ;     else       cout << (n / m) * b + min((n % m) * a, b) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d, m, A[101];     while (cin >> n >> d) {       int sum = 0;       for (int i = 0; i < n; i++) {         cin >> A[i];       }       cin >> m;       sort(A, A + n);       if (m > n) {         for (int i = 0; i < n; i++) {           sum += A[i];         }         sum -= ((m - n) * d);       } else {         for (int i = 0; i < m; i++) {           sum += A[i];         }       }       cout << sum << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int res[100000];     memset(res, -1, sizeof(res));     cin >> n;     vector<pair<pair<long long, long long>, int> > v;     for (int(i) = 0; (i) < (n); (i)++) {       long long x, r;       cin >> x >> r;       v.push_back(pair<pair<long long, long long>, int>(           pair<long long, long long>(x, r), i));     }     sort(v.begin(), v.end());     int m;     cin >> m;     int ret = 0;     for (int(i) = 0; (i) < (m); (i)++) {       long long x, y;       cin >> x >> y;       int l = 0, r = n - 1;       while (l <= r) {         int mid = (l + r) / 2;         if (x < v[mid].first.first - v[mid].first.second) {           r = mid - 1;           continue;         }         if (x > v[mid].first.first + v[mid].first.second) {           l = mid + 1;           continue;         }         if ((x - v[mid].first.first) * (x - v[mid].first.first) + y * y <=             v[mid].first.second * v[mid].first.second) {           if (res[v[mid].second] == -1) {             ret++;             res[v[mid].second] = i + 1;           }         }         if (mid > 0) {           if ((x - v[mid - 1].first.first) * (x - v[mid - 1].first.first) +                   y * y <=               v[mid - 1].first.second * v[mid - 1].first.second) {             if (res[v[mid - 1].second] == -1) {               ret++;               res[v[mid - 1].second] = i + 1;             }           }         }         if (mid < n - 1) {           if ((x - v[mid + 1].first.first) * (x - v[mid + 1].first.first) +                   y * y <=               v[mid + 1].first.second * v[mid + 1].first.second) {             if (res[v[mid + 1].second] == -1) {               ret++;               res[v[mid + 1].second] = i + 1;             }           }         }         break;       }     }     cout << ret << endl;     for (int(i) = 0; (i) < (n); (i)++) {       cout << res[i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = 3.141592653589793238463;   int x[1009], y[1009], t[1009];   double p[1009];   double dp[1009][1009];   int n;   double sol(int i, int prev) {     if (i == n) return 0;     if (dp[i][prev] == dp[i][prev]) return dp[i][prev];     bool can = true;     if (prev == n)       can = true;     else {       int dt = t[i] - t[prev];       int dx = abs(x[i] - x[prev]);       int dy = abs(y[i] - y[prev]);       can = (dt >= hypot(dx, dy));     }     if (!can) return dp[i][prev] = sol(i + 1, prev);     return dp[i][prev] = max(sol(i + 1, prev), p[i] + sol(i + 1, i));   }   struct target {     int x, y, t;     double p;     bool operator<(const target &other) const { return t < other.t; }   };   int main() {     cin.tie(0);     cin.sync_with_stdio(0);     cin >> n;     vector<target> v;     for (int i = 0; i < (n); i++) {       int x, y, t;       cin >> x >> y >> t;       double p;       cin >> p;       v.push_back({x, y, t, p});     }     sort(v.begin(), v.end());     for (int i = 0; i < (n); i++)       x[i] = v[i].x, y[i] = v[i].y, t[i] = v[i].t, p[i] = v[i].p;     memset((dp), (-1), sizeof(dp));     printf( %.10f n , sol(0, n));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100005;   int cnt[MAX_N];   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<int> vec(n);     for (int i = 0; i < (int)(n); ++i) {       cin >> vec[i];     }     int m1 = -1, m2 = -2;     int id1 = -1, id2 = -1;     for (int i = 0; i < (int)(n); ++i) {       if (vec[i] > m1) {         cnt[i] -= 1;         m2 = m1, id2 = id1;         m1 = vec[i], id1 = i;       } else if (vec[i] > m2) {         cnt[id1] += 1;         m2 = vec[i], id2 = i;       }     }     vector<pair<int, int> > res;     for (int i = 0; i < (int)(n); ++i) {       res.push_back(pair<int, int>(cnt[i], -vec[i]));     }     sort((res).begin(), (res).end(), greater<pair<int, int> >());     cout << -res[0].second <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct data1 {     long long sum, index, position;   };   bool comp(data1 a, data1 b) { return a.sum < b.sum; }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(0);     long long k;     cin >> k;     vector<data1> arr;     long long sz = 0;     for (long long i = 0; i < k; i++) {       long long n;       cin >> n;       std::vector<long long> v(n);       for (auto &x : v) cin >> x;       long long sum = accumulate(v.begin(), v.end(), 0ll);       for (long long j = 0; j < n; j++) {         data1 d;         d.sum = sum - v[j];         d.index = i;         d.position = j;         arr.push_back(d);         sz++;       }     }     sort(arr.begin(), arr.end(), &comp);     bool flag = false;     for (long long i = 0; i < sz - 1; i++) {       if (arr[i].sum == arr[i + 1].sum and arr[i].index != arr[i + 1].index) {         cout <<  YES n ;         cout << arr[i].index + 1 <<     << arr[i].position + 1 <<   n ;         cout << arr[i + 1].index + 1 <<     << arr[i + 1].position + 1 <<   n ;         flag = true;         break;       }     }     if (flag) return 0;     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool f[200000] = {0};   string ans, s;   multiset<int> g[66000];   int n, in[66000] = {0}, out[66000] = {0}, kol1 = 0, kol2 = 0, start = 0;   void dfs(int v) {     while (!g[v].empty()) {       int tmp = *g[v].begin();       g[v].erase(g[v].begin());       dfs(tmp);     }     ans += char(v % 255);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> s;       int v = int(s[0]) * 255 + int(s[1]);       int u = int(s[1] * 255) + int(s[2]);       start = v;       g[v].insert(u);       out[v]++;       in[u]++;     }     for (int i = 1; i <= 65025; i++) {       int d = out[i] - in[i];       if (d == 1) {         kol1++;         start = i;       } else {         if (d == -1)           kol2++;         else if (d != 0) {           cout <<  NO ;           return 0;         }       }       if (kol1 > 1 || kol2 > 1) {         cout <<  NO ;         return 0;       }     }     dfs(start);     ans += char(start / 255);     reverse(ans.begin(), ans.end());     if (ans.length() == n + 2) {       cout <<  YES             <<   n ;       cout << ans;     } else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     string s, a =   , b;     cin >> s;     unordered_map<string, int> m;     m[s]++;     for (int i = s.length() - 1; i >= 0; i--) {       a += s[i];       b = a;       reverse(b.begin(), b.end());       m[b + s.substr(0, i)]++;     }     cout << m.size() << endl;   }   int main() {     { solve(); }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     cin >> n;     int k = 0;     if (n == 1 || n == 2)       cout << -1;     else if (n == 3)       cout << 210;     else       for (int i = 4; i <= n; i++) {         k++;         if (k == 7) k = 1;       }     if (k == 1) {       cout << 1;       for (int i = 2; i <= n - 2; i++) cout << 0;       cout << 50;     } else if (k == 2) {       cout << 1;       for (int i = 2; i <= n - 2; i++) cout << 0;       cout << 80;     } else if (k == 3) {       cout << 1;       for (int i = 2; i <= n - 3; i++) cout << 0;       cout << 170;     } else if (k == 4) {       cout << 1;       for (int i = 2; i <= n - 2; i++) cout << 0;       cout << 20;     } else if (k == 5) {       cout << 1;       for (int i = 2; i <= n - 3; i++) cout << 0;       cout << 200;     } else if (k == 6) {       cout << 1;       for (int i = 2; i <= n - 3; i++) cout << 0;       cout << 110;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 5;   int minn[MAX], maxx[MAX], h[MAX];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) cin >> h[i];     int block = 1;     minn[1] = h[1];     maxx[1] = h[1];     for (int i = 2; i <= n; ++i) {       if (maxx[block] <= h[i]) {         block++;         minn[block] = h[i];         maxx[block] = h[i];       } else {         minn[block] = min(minn[block], h[i]);         maxx[block] = max(maxx[block], h[i]);         while (block > 1 && maxx[block - 1] > minn[block]) {           --block;           minn[block] = min(minn[block], minn[block + 1]);           maxx[block] = max(maxx[block], maxx[block + 1]);         }       }     }     cout << block;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005, SQRT = 550, Q = 100005;   struct Query {     int l, r, id;     Query() {}     Query(int l, int r, int id) : l(l), r(r), id(id) {}     bool operator<(const Query& q) const {       if (l / SQRT != q.l / SQRT) return l / SQRT < q.l / SQRT;       return r > q.r;     }   } queries[Q];   int arr[N], n, k;   int freq[2000005], answer[Q], mo_distinct, mo_has_special;   deque<int> nums[N];   map<int, int> freq_ratio[N];   inline void add_elem(int x) {     if (++freq[x] == 1) ++mo_distinct;   }   inline void rem_elem(int x) {     if (freq[x]-- == 1) --mo_distinct;   }   inline void add_freq_ratio(int x, int diff) {     bool before = freq_ratio[x].size() <= 1;     ++freq_ratio[x][diff];     bool after = freq_ratio[x].size() == 1;     if (!before && after) ++mo_has_special;     if (before && !after) --mo_has_special;   }   inline void rem_freq_ratio(int x, int diff) {     bool before = freq_ratio[x].size() == 1;     --freq_ratio[x][diff];     if (freq_ratio[x][diff] == 0) freq_ratio[x].erase(diff);     bool after = freq_ratio[x].size() <= 1;     if (!before && after) ++mo_has_special;     if (before && !after) --mo_has_special;   }   void add_begin(int idx, int x) {     add_elem(x);     if (!nums[x].empty()) {       int diff = nums[x].front() - idx;       add_freq_ratio(x, diff);     } else {       ++mo_has_special;     }     nums[x].push_front(idx);   }   void add_end(int idx, int x) {     add_elem(x);     if (!nums[x].empty()) {       int diff = idx - nums[x].back();       add_freq_ratio(x, diff);     } else {       ++mo_has_special;     }     nums[x].push_back(idx);   }   void rem_begin(int x) {     rem_elem(x);     int front = nums[x].front();     nums[x].pop_front();     if (!nums[x].empty()) {       int diff = nums[x].front() - front;       rem_freq_ratio(x, diff);     } else {       --mo_has_special;     }   }   void rem_end(int x) {     rem_elem(x);     int back = nums[x].back();     nums[x].pop_back();     if (!nums[x].empty()) {       int diff = back - nums[x].back();       rem_freq_ratio(x, diff);     } else {       --mo_has_special;     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d , &arr[i]);     int q;     scanf( %d , &q);     for (int i = 0; i < q; ++i) {       int l, r;       scanf( %d %d , &l, &r);       --l;       --r;       queries[i] = Query(l, r, i);     }     sort(queries, queries + q);     mo_has_special = mo_distinct = 0;     int l = 0, r = 0;     add_end(0, arr[0]);     for (int i = 0; i < q; ++i) {       while (r < queries[i].r) {         ++r;         add_end(r, arr[r]);       }       while (l > queries[i].l) {         --l;         add_begin(l, arr[l]);       }       while (l < queries[i].l) rem_begin(arr[l++]);       while (r > queries[i].r) rem_end(arr[r--]);       if (mo_has_special != 0) {         answer[queries[i].id] = mo_distinct;       } else {         answer[queries[i].id] = mo_distinct + 1;       }     }     for (int i = 0; i < q; ++i) printf( %d n , answer[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string n;     cin >> n;     reverse(n.begin(), n.end());     int k = n[0] + 10 * n[1];     if (k % 4 == 0) {       cout << 4 << endl;     } else {       cout << 0 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     ;     vector<int> v(n);     long long x = 0;     for (int i = 0; i < n; i++) {       char x;       cin >> x;       if ((x -  0 ) % 2 == 0) {         v.push_back(i + 1);       }     }     for (int i = 0; i < v.size(); i++) {       x += v[i];     }     cout << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 10;   int P[N], ar[N];   long long c[N];   vector<int> v[N];   bool used[N];   void init() {     P[1] = 1;     int i, j;     for (i = 1; i < N; ++i)       for (j = (i * 2); j < N; j += i) P[j] -= P[i];   }   int main() {     int tt, q, x, n, i, j;     scanf( %d%d , &n, &q);     long long ret = 0;     init();     for (i = 1; i <= n; ++i) {       scanf( %d , &ar[i]);       x = ar[i];       for (j = 1; j * j <= x; ++j)         if (x % j == 0) {           v[i].push_back(j);           if ((x / j) != j) v[i].push_back(x / j);         }     }     for (i = 0; i < q; ++i) {       scanf( %d , &x);       for (j = 0; j < v[x].size(); ++j) {         tt = v[x][j];         ret -= c[tt] * (c[tt] - 1) / 2 * P[tt];         if (used[x])           --c[tt];         else           ++c[tt];         ret += c[tt] * (c[tt] - 1) / 2 * P[tt];       }       used[x] ^= true;       cout << ret << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   void debug() {}   int const N = 1e3 + 9, OO = 1e9;   int main() {     int n, q, type, a;     char b;     bool prd = 0;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         scanf(  %c , &b);         if (j == i && b ==  1 ) prd = !prd;       }     }     scanf( %d , &q);     for (int i = 0; i < q; ++i) {       scanf( %d , &type);       if (type != 3) scanf( %d , &a);       if (type == 1 || type == 2)         prd = !prd;       else         printf((prd) ?  1  :  0 );     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = (int)1e6 + 10;   int mod = (int)1e9 + 7;   int sum(int a, int b) {     int s = a + b;     if (s >= mod) s -= mod;     return s;   }   int n;   int mult(int a, int b) { return (1LL * a * b) % mod; }   int a[maxN];   int inv[maxN];   int fact;   void calc() {     inv[1] = 1;     for (int i = 2; i < maxN; i++) {       inv[i] = mult((mod - (mod / i)), inv[mod % i]);     }     fact = 1;     for (int i = 2; i <= n; i++) fact = mult(fact, i);   }   int main() {     ios_base::sync_with_stdio(false);     srand(time(0));     srand(time(0));     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     calc();     sort(a + 1, a + n + 1);     int mx = a[n];     int ans = 0;     int x = 0;     int prv = 0;     int sz = 1;     for (int i = 1; i <= n; i++) {       if (a[i] == mx) break;       if (i > 1) {         if (a[i] == a[i - 1])           sz++;         else {           prv = prv + sz;           sz = 1;         }       }       ans = sum(ans, mult(mult(a[i], fact), inv[n - prv]));     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   clock_t timeStart, timeFinish;   void timeBegin() { timeStart = clock(); }   void timeEnd() { timeFinish = clock(); }   void timeDuration() {     timeEnd();     double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC);     cout <<  Time taken by program is :   << fixed << time_taken          << setprecision(5);     cout <<   sec   << endl;   }   class InputReader {    public:     InputReader() {       input_file = stdin;       cursor = 0;       fread(buffer, SIZE, 1, input_file);     }     InputReader(const char *file_name) {       input_file = fopen(file_name,  r );       cursor = 0;       fread(buffer, SIZE, 1, input_file);     }     inline InputReader &operator>>(int &n) {       while ((buffer[cursor] <  0  || buffer[cursor] >  9 ) &&              buffer[cursor] !=  - ) {         advance();       }       int sign = 1;       if (buffer[cursor] ==  - ) {         sign = -1;         advance();       }       n = 0;       while ( 0  <= buffer[cursor] && buffer[cursor] <=  9 ) {         n = n * 10 + buffer[cursor] -  0 ;         advance();       }       n *= sign;       return *this;     }       private:     FILE *input_file;     static const int SIZE = 1 << 17;     int cursor;     char buffer[SIZE];     inline void advance() {       ++cursor;       if (cursor == SIZE) {         cursor = 0;         fread(buffer, SIZE, 1, input_file);       }     }   };   const int MAXN = 200000;   const int MAXLOG = 18;   const int INF = 1000000000;   vector<int> g[1 + MAXN];   vector<pair<int, int> > t[1 + MAXN];   int dad[1 + MAXLOG][1 + MAXN], answer[1 + MAXN], virusId[1 + MAXN];   int counter = 0, start[1 + MAXN], finish[1 + MAXN], depth[1 + MAXN];   int speed[1 + MAXN], id[1 + MAXN], virus[1 + MAXN], city[1 + MAXN],       st[1 + MAXN];   void DFS(int node, int father) {     dad[0][node] = father;     depth[node] = depth[father] + 1;     for (int i = 1; (1 << i) < depth[node]; i++) {       dad[i][node] = dad[i - 1][dad[i - 1][node]];     }     start[node] = ++counter;     for (auto son : g[node]) {       if (son != father) {         DFS(son, node);       }     }     finish[node] = counter;   }   int raise(int node, int delta) {     for (int i = 0; i < MAXLOG; i++) {       if ((delta >> i) & 1) {         node = dad[i][node];       }     }     return node;   }   bool isParent(int a, int b) {     return start[a] <= start[b] && finish[b] <= finish[a];   }   int lca(int a, int b) {     if (depth[a] < depth[b]) {       swap(a, b);     }     a = raise(a, depth[a] - depth[b]);     for (int i = MAXLOG - 1; i >= 0; i--) {       if (dad[i][a] != dad[i][b]) {         a = dad[i][a];         b = dad[i][b];       }     }     if (a != b) {       a = dad[0][a];       b = dad[0][b];     }     return a;   }   inline bool compareStart(const int &a, const int &b) {     return start[a] < start[b];   }   void sortUnique(vector<int> &v) {     sort(v.begin(), v.end(), compareStart);     v.resize(unique(v.begin(), v.end()) - v.begin());   }   pair<int, int> d[1 + MAXN];   bool isSmaller(int c1, int s1, int c2, int s2) {     int t1 = (c1 == 0 ? 0 : (c1 - 1) / speed[s1] + 1);     int t2 = (c2 == 0 ? 0 : (c2 - 1) / speed[s2] + 1);     return make_pair(t1, virusId[s1]) < make_pair(t2, virusId[s2]);   }   bool seen[1 + MAXN];   void dijkstra(vector<int> &v) {     priority_queue<pair<pair<int, int>, pair<int, int> >,                    vector<pair<pair<int, int>, pair<int, int> > >,                    greater<pair<pair<int, int>, pair<int, int> > > >         h;     for (auto node : v) {       if (virusId[node] != 0) {         d[node] = {0, virusId[node]};         h.push({{0, virusId[node]}, {node, 0}});       } else {         d[node] = {INF, 0};       }     }     while (!h.empty()) {       int r = h.top().first.first, s = h.top().first.second,           node = h.top().second.first, c = h.top().second.second;       h.pop();       if (seen[node]) {         continue;       }       seen[node] = true;       for (auto it : t[node]) {         int other = it.first, l = it.second;         int r0 = (c + l - 1) / speed[s] + 1, s0 = s;         if (make_pair(r0, s0) < d[other]) {           d[other] = {r0, s0};           h.push({{r0, s0}, {other, c + l}});         }       }     }   }   void solve(int k, int m) {     vector<int> v;     for (int i = 1; i <= k; i++) {       v.push_back(virus[i]);     }     for (int i = 1; i <= m; i++) {       v.push_back(city[i]);     }     sortUnique(v);     int x = v.size();     for (int i = 0; i + 1 < x; i++) {       v.push_back(lca(v[i], v[i + 1]));     }     sortUnique(v);     int top = 1;     st[top] = v[0];     for (int i = 1; i < v.size(); i++) {       int node = v[i];       while (!isParent(st[top], node)) {         top--;       }       int l = depth[node] - depth[st[top]];       t[st[top]].push_back({node, l});       t[node].push_back({st[top], l});       top++;       st[top] = node;     }     dijkstra(v);     for (auto node : v) {       seen[node] = false;       t[node].clear();       if (id[node] != 0) {         answer[id[node]] = d[node].second;       }     }   }   int main() {     timeBegin();     ios_base::sync_with_stdio(false);     cin.tie(0);     speed[0] = 1;     int n;     cin >> n;     for (int i = 1; i < n; i++) {       int a, b;       cin >> a >> b;       g[a].push_back(b);       g[b].push_back(a);     }     DFS(1, 0);     int q;     cin >> q;     for (int i = 1; i <= q; i++) {       int k, m;       cin >> k >> m;       for (int i = 1; i <= k; i++) {         cin >> virus[i] >> speed[i];         virusId[virus[i]] = i;       }       for (int i = 1; i <= m; i++) {         cin >> city[i];         id[city[i]] = i;       }       solve(k, m);       for (int i = 1; i <= m; i++) {         cout << answer[i] <<    ;       }       cout <<   n ;       for (int i = 1; i <= k; i++) {         virusId[virus[i]] = 0;       }       for (int i = 1; i <= m; i++) {         id[city[i]] = 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using lint = long long;   int main(void) {     lint Q, N;     cin >> Q;     vector<lint> ans(Q);     for (int i = 0; i < Q; i++) {       cin >> N;       lint sum = 0;       for (int k = 0; k < N; k++) {         int x;         cin >> x;         sum += x;       }       ans[i] = ceil(sum / (double)N);     }     for (int i = 0; i < Q; i++) cout << ans[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int MAX_N = 1e3 + 9;   int par[MAX_N], rankv[MAX_N];   vector<pair<int, int>> cycles;   vector<array<int, 4>> ans;   void init(int n) {     for (int i = 1; i <= n; i++) par[i] = i, rankv[i] = 1;   }   int find_set(int u) {     if (par[u] == u) return u;     return par[u] = find_set(par[u]);   }   void link(int x, int y) {     if (rankv[x] > rankv[y]) swap(x, y);     par[x] = y;     rankv[y] += (rankv[y] == rankv[x]);   }   bool union_set(int x, int y) {     x = find_set(x), y = find_set(y);     if (x != y) link(x, y);     return x != y;   }   int32_t main() {     ios_base::sync_with_stdio(0), cin.tie(0);     int n, u, v;     cin >> n;     init(n);     for (int i = 0; i < n - 1; i++) {       cin >> u >> v;       if (!union_set(u, v)) cycles.push_back({u, v});     }     for (int i = 0; i < cycles.size(); i++) {       auto cur = cycles[i];       for (int j = 1; j <= n; j++) {         if (union_set(cur.first, j)) {           ans.push_back({cur.first, cur.second, cur.first, j});           break;         }       }     }     if (ans.empty()) cout << 0, exit(0);     cout << ans.size() <<   n ;     for (auto i : ans) {       for (int j = 0; j < 4; j++) cout << i[j] <<    [j == 3];       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int N;   long long arr[100020];   long long prefix[100020];   multiset<long long> s;   set<int> ded;   long long range(int i, int j) {     if (i == 0) {       return prefix[j];     } else {       return prefix[j] - prefix[i - 1];     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     if (fopen( cf722c.in ,  r )) {       freopen( cf722c.in ,  r , stdin);       freopen( cf722c.out ,  w , stdout);     }     cin >> N;     arr[0] = 0;     for (int i = 1; i <= N; i++) {       cin >> arr[i];     }     prefix[0] = arr[0];     for (int i = 1; i <= N + 1; i++) {       prefix[i] = prefix[i - 1] + arr[i];     }     s.insert(0);     s.insert(prefix[N + 1]);     ded.insert(0);     ded.insert(N + 1);     for (int i = 0; i < N; i++) {       int x;       cin >> x;       ded.insert(x);       int l = 0, r = 0;       auto lt = ded.lower_bound(x);       if (lt == ded.begin()) {         l = 0;       } else {         lt--;         l = *lt;         l++;       }       auto rt = ded.upper_bound(x);       if (rt == ded.end()) {         r = N + 1;       } else {         r = *rt;         r--;       }       s.erase(s.find(range(l, r)));       s.insert(range(l, x - 1));       s.insert(range(x + 1, r));       cout << *(s.rbegin()) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   string s;   int main() {     cin >> n >> m >> s;     if (m - 1 < n - m) {       while (m > 1) {         cout <<  LEFT n ;         --m;       }       for (int i = 0; i < (int)(n); i++) {         cout <<  PRINT   << s[i] <<   n ;         if (i != n - 1) cout <<  RIGHT n ;       }     } else {       while (m < n) {         cout <<  RIGHT n ;         ++m;       }       for (int i = 0; i < (int)(n); i++) {         cout <<  PRINT   << s[n - 1 - i] <<   n ;         if (i != n - 1) cout <<  LEFT n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int cnt = 0;     bool flag = true;     int low = -2000000000, high = 2000000000;     bool lset = false;     bool hset = false;     cin >> cnt;     for (int i = 1; i <= cnt; i++) {       string str1, str2, str3;       cin >> str1 >> str2 >> str3;       stringstream stream(str2);       int num;       stream >> num;       if (((str1 ==  > ) && (str3 ==  Y )) || ((str1 ==  <= ) && (str3 ==  N ))) {         if (low <= num) {           low = num + 1;           lset = true;         }         if (lset && hset && (high < low || low > 2000000000)) {           flag = false;           break;         }       }       if (((str1 ==  >= ) && (str3 ==  Y )) || ((str1 ==  < ) && (str3 ==  N ))) {         if (low < num) {           low = num;           lset = true;         }         if (lset && hset && (high < low || low > 2000000000)) {           flag = false;           break;         }       }       if (((str1 ==  <= ) && (str3 ==  Y )) || ((str1 ==  > ) && (str3 ==  N ))) {         if (high > num) {           high = num;           hset = true;         }         if (lset && hset && (high < low || high < -2000000000)) {           flag = false;           break;         }       }       if (((str1 ==  < ) && (str3 ==  Y )) || ((str1 ==  >= ) && (str3 ==  N ))) {         if (high >= num) {           high = num - 1;           hset = true;         }         if (lset && hset && (high < low || high < -2000000000)) {           flag = false;           break;         }       }     }     if (flag && (!hset) && lset) cout << low << endl;     if (flag && (!lset) && hset) cout << high << endl;     if (flag && !lset && !hset) cout <<  0  << endl;     if (flag && lset && hset) cout << low << endl;     if (!flag) cout <<  Impossible  << endl;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   long long n, x, y, nr;   long long cx, cy;   int ans[2000002];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> x >> y;     int xx = 0;     while (xx <= x + y) {       long long pa = (cx + 1) * y;       long long pb = (cy + 1) * x;       if (pa == pb)         ++cx, ++cy, xx += 2;       else if (pa < pb)         ans[xx] = 1, ++cx, ++xx;       else         ans[xx] = 2, ++cy, ++xx;     }     for (int i = 1; i <= n; ++i) {       cin >> nr;       --nr;       nr %= (x + y);       if (ans[nr] == 1) cout <<  Vanya  <<   n ;       if (ans[nr] == 2) cout <<  Vova  <<   n ;       if (ans[nr] == 0) cout <<  Both  <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const int MAX = 1e9 + 5;   int i, j, w1, w2, k, n, m, a[N], b[N], calca[N], calcb[N], temp;   vector<pair<int, int> > ans, adasd;   bool ctr(int s) {     int set = 0;     int p1 = 0;     int p2 = 0;     int last1 = 0;     int last2 = 0;     int lastt = 0;     while (1) {       if (w1 == last1 && w2 == last2)         if (((p1 > p2 && lastt == 1) || (p2 > p1 && lastt == 2)) && p1 != p2) {           temp = max(p1, p2);           return true;         } else           return false;       if (last2 + s <= w2 && b[last2 + s] < a[last1 + s]) {         p2++;         lastt = 2;         last2 += s;         last1 = lower_bound(a + 1, a + w1 + 1, b[last2]) - a - 1;       } else if (last1 + s <= w1 && a[last1 + s] < b[last2 + s]) {         p1++;         lastt = 1;         last1 += s;         last2 = lower_bound(b + 1, b + w2 + 1, a[last1]) - b - 1;       } else         return false;     }   }   int main() {     cin >> n;     int asdasdasda;     for (int i = 1; i <= 200001; i++) a[i] = b[i] = 100000000;     int x;     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       if (x == 1)         a[++w1] = i;       else         b[++w2] = i;     }     for (int i = 1; i <= n; i++)       if (ctr(i)) ans.push_back(make_pair(temp, i));     sort(ans.begin(), ans.end());     cout << ans.size() <<   n ;     for (int i = 0; i <= (int)ans.size() - 1; i++)       cout << ans[i].first <<     << ans[i].second <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i, j, k, l, maxx, minn, c = 1, c2 = 0, cnt = 0, a1 = 0, a2 = 0;     cin >> n;     long long a[n], b[n];     cnt = n;     for (i = 0; i < n; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(b, b + n);     maxx = b[n - 1];     minn = b[0];     for (i = 0; i < n; i++) {       if (a[i] == a[i + 1])         ++c;       else {         a1 = a1 + ((c - 1) * c) / 2;         c = 1;       }     }     a1 = a1 + ((c - 1) * c) / 2;     cnt = cnt + a1;     cout << cnt << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int q[N];   int n;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cin >> n;     for (int i = 1; i <= n; i++) cin >> q[i];     if (n & 1) {       cout <<  YES  <<   n ;       cout << n - 1 <<   n ;       for (int i = 1; i < n; i += 2) {         cout << i <<     << i + 1 <<     << i + 2 <<   n ;       }       for (int i = 1; i < n; i += 2) {         cout << i <<     << i + 1 <<     << n <<   n ;       }     } else {       int tmp = 0;       for (int i = 1; i <= n; i++) tmp ^= q[i];       if (tmp != 0) {         cout <<  NO  <<   n ;       } else {         cout <<  YES  <<   n ;         cout << n - 2 <<   n ;         for (int i = 1; i < n - 1; i += 2) {           cout << i <<     << i + 1 <<     << i + 2 <<   n ;         }         for (int i = 1; i < n - 1; i += 2) {           cout << i <<     << i + 1 <<     << n - 1 <<   n ;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   bool mayorigual(const string &s, int i, int ifin, int j, int jfin) {     if (ifin - i > jfin - j) return true;     if (ifin - i < jfin - j) return false;     int len = ifin - i;     for (int k = 0; k < len; k++) {       if (s[k + i] > s[k + j])         return true;       else if (s[k + i] < s[k + j])         return false;     }     return true;   }   int main() {     int ret = 0;     string s;     cin >> s;     int j = s.size();     int i = j - 1;     while (i >= 0 && j > 0) {       if (s[i] ==  0 )         i--;       else if (mayorigual(s, 0, i, i, j)) {         ret++;         j = i;         i = j - 1;       } else {         ret++;         break;       }     }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 131072, P = 998244353, inv2 = (P + 1) / 2;   int n, m, ans, a[maxn], b[maxn], fact[maxn], finv[maxn];   int lim, l, r[maxn], w[maxn];   vector<int> V;   vector<int> f[maxn << 2][2][2];   int qp(int x, int y) {     int z = 1;     for (; y; y >>= 1, x = 1LL * x * x % P) {       if (y & 1) z = 1LL * z * x % P;     }     return z;   }   void init() {     for (int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));     w[lim >> 1] = 1;     int t = qp(3, (P - 1) / lim);     for (int i = (lim >> 1) + 1; i < lim; i++) w[i] = 1LL * t * w[i - 1] % P;     for (int i = (lim >> 1) - 1; i > 0; i--) w[i] = w[i << 1];   }   void DFT(int *a, int type) {     static unsigned long long c[maxn];     copy(a, a + lim, c);     for (int i = 0; i < lim; i++)       if (i < r[i]) swap(c[i], c[r[i]]);     for (int i = 1; i < lim; i <<= 1) {       for (int j = 0; j < lim; j += i << 1) {         for (int k = 0; k < i; k++) {           unsigned long long t = c[i + j + k] * w[i + k] % P;           c[i + j + k] = c[j + k] + P - t, c[j + k] += t;         }       }     }     if (!~type) {       reverse(c + 1, c + lim);       for (int i = 0, t = qp(lim, P - 2); i < lim; i++) c[i] = c[i] % P * t % P;     }     for (int i = 0; i < lim; i++) a[i] = c[i] % P;   }   vector<int> operator*(vector<int> A, vector<int> B) {     int len = A.size() + B.size() - 1;     for (lim = 1, l = 0; lim < len; lim <<= 1) l++;     A.resize(lim), B.resize(lim);     init(), DFT(A.data(), 1), DFT(B.data(), 1);     for (int i = 0; i < lim; i++) A[i] = 1LL * A[i] * B[i] % P;     DFT(A.data(), -1), A.resize(len);     return A;   }   int main() {     scanf( %d , &n);     for (int i = fact[0] = finv[0] = 1; i <= n; i++) {       scanf( %d , &a[i]);       fact[i] = 1LL * i * fact[i - 1] % P;       finv[i] = qp(fact[i], P - 2);     }     for (int i = 1; i <= n; i += a[i]) {       b[++m] = a[i];       for (int j = i; j < i + a[i]; j++) {         if (a[i] ^ a[j]) puts( 0 ), exit(0);       }     }     function<void(int, int, int)> solve = [&](int k, int l, int r) {       if (l == r) {         f[k][0][0] = {0, b[l] == 1 ? inv2 : 1};         f[k][0][1] = f[k][1][0] = f[k][1][1] = {0, 1};         return;       }       int mid = (l + r) / 2, ls = k << 1, rs = k << 1 | 1;       solve(ls, l, mid), solve(rs, mid + 1, r);       for (int p : {0, 1})         for (int q : {0, 1}) {           f[k][p][q].resize(r - l + 2);         }       for (int a : {0, 1})         for (int b : {0, 1})           for (int c : {0, 1}) {             vector<int> g = f[ls][a][b] * f[rs][b][c];             for (int i = b; i < g.size(); i++) (f[k][a][c][i - b] += g[i]) %= P;           }     };     solve(1, 1, m);     for (int i = 1; i <= m; i++) {       ans = (ans + 1LL * fact[i] * f[1][0][0][i] % P * qp(2, i) % P *                        ((m - i) & 1 ? P - 1 : 1)) %             P;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   int arr[200005];   int brr[200005];   priority_queue<int, vector<int>, greater<int> > q;   stack<int> s;   int vis[200005];   int main() {     while (cin >> n >> k) {       while (!q.empty()) q.pop();       while (!s.empty()) s.pop();       memset(vis, 0, sizeof(vis));       int kmax = 0;       for (int i = 0; i < k; i++) {         scanf( %d , &arr[i]);         vis[arr[i]] = 1;         kmax = max(kmax, arr[i]);       }       for (int i = 1; i <= kmax; i++) {         if (vis[i] == 0) {           q.push(i);         }       }       int indexb = 0;       for (int i = 0; i < k; i++) {         if (s.empty()) {           s.push(arr[i]);         } else if (arr[i] > s.top()) {           while (!s.empty() && arr[i] > s.top()) {             brr[indexb++] = s.top();             s.pop();           }           s.push(arr[i]);         } else {           s.push(arr[i]);         }       }       bool flag = true;       for (int i = 0; i < indexb; i++) {         if (brr[i] != i + 1) flag = false;       }       if (flag == false) {         cout << -1 << endl;         continue;       }       stack<int> choosing;       for (int i = 0; i < k; i++) printf( %d%c , arr[i], i == k - 1 ?     :    );       int cnt = 0;       while (!s.empty()) {         while (!q.empty() && q.top() < s.top()) {           choosing.push(q.top());           q.pop();         }         s.pop();         while (!choosing.empty()) {           printf( %d , choosing.top());           cnt++;           if (cnt == kmax - k && kmax == n)             printf(  n );           else             printf(   );           choosing.pop();         }       }       for (int i = n; i >= kmax + 1; i--) {         printf( %d%c , i, i == kmax + 1 ?   n  :    );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long f[50];   int main() {     int n;     cin >> f[0] >> f[1] >> n;     for (int i = 2; i <= n; i++) f[i] = f[i - 1] + f[i - 2];     cout << f[n];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   char zh[125], ch, sw;   int n, m;   int main() {     cin >> n >> m >> s;     for (int i = 97; i <= 122; i += 1) {       zh[i] = i;     }     for (int i = 1; i <= m; i += 1) {       cin >> ch >> sw;       for (int j = 97; j <= 122; j += 1) {         if (zh[j] == ch) {           zh[j] = sw;         } else {           if (zh[j] == sw) {             zh[j] = ch;           }         }       }     }     for (int i = 0; i <= n - 1; i += 1) {       cout << zh[s[i]];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<string, long long> > Name(1010);   long long n;   vector<vector<pair<string, long long> > > a(1010);   map<string, long long> already;   map<string, long long>::iterator ii;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     long long i, j;     for (i = 1; i <= n; i++) {       string w;       long long version;       cin >> w >> version;       if (i == 1) {         already[w] = version;       }       Name[i] = make_pair(w, version);       long long m;       cin >> m;       for (j = 0; j < m; j++) {         cin >> w >> version;         a[i].push_back(make_pair(w, version));       }     }     vector<pair<string, long long> > q;     map<string, long long> pretendent;     q.push_back(Name[1]);     while (q.size() > 0) {       long long qs = 0, qf = q.size();       vector<pair<string, long long> > temp;       temp.clear();       pretendent.clear();       while (qs < qf) {         long long cur;         for (i = 1; i <= n; i++) {           if (Name[i].first == q[qs].first && Name[i].second == q[qs].second)             cur = i;         }         for (i = 0; i < a[cur].size(); i++) {           string task = a[cur][i].first;           long long version = a[cur][i].second;           if (already[task] == 0)             pretendent[task] = max(pretendent[task], version);         }         qs++;       }       q.clear();       for (ii = pretendent.begin(); ii != pretendent.end(); ii++) {         string task = ii->first;         long long version = ii->second;         already[task] = version;         q.push_back(make_pair(task, version));       }     }     if (already[Name[1].first] == Name[1].second) already.erase(Name[1].first);     cout << already.size() << endl;     for (ii = already.begin(); ii != already.end(); ii++) {       cout << ii->first <<     << ii->second << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int dat[100000 + 10];   int vis[100000 + 10];   int dfs(int x) {     vis[x] = 1;     if (vis[dat[x]] == 0) return dfs(dat[x]) + 1;     return 1;   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> dat[i + 1];     }     vector<long long> g;     for (int i = 1; i <= n; i++) {       if (vis[i] == 0) {         g.push_back(dfs(i));       }     }     sort(g.begin(), g.end());     reverse(g.begin(), g.end());     if (g.size() == 0) {       cout <<  0  << endl;     } else if (g.size() == 1) {       cout << g[0] * g[0] << endl;     } else {       long long total = (g[0] + g[1]) * (g[0] + g[1]);       for (int i = 2; i < g.size(); i++) {         total += g[i] * g[i];       }       cout << total << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxx = (int)1e7;   long long SOD(long long n) {     long long ans = 0;     while (n) {       ans += (n % 10);       n /= 10;     }     return ans;   }   int main() {     int T;     cin >> T;     while (T--) {       vector<int> num;       long long n, s, nnn;       cin >> n >> s;       nnn = n;       if (SOD(n) <= s) {         cout << 0 << endl;         continue;       }       while (n) {         num.push_back(n % 10);         n /= 10;       }       reverse(num.begin(), num.end());       int sum = 0;       int start = 0;       for (int i = 0; i < num.size(); i++) {         sum += num[i];         if (sum >= s) {           if (i != 0 && num[i - 1] != 9) {             num[i - 1]++;             start = i;             break;           }           if (i == 0) {             num.insert(num.begin(), 1);             start = 1;             break;           } else if (num[i - 1] == 9) {             while (i != 0 && num[i - 1] == 9) {               i--;             }             i--;           }         }       }       for (int i = start; i < num.size(); i++) {         num[i] = 0;       }       int p = num.size() - 1;       long long nn = 0;       for (int i = 0; i < num.size(); i++) {         nn += num[i] * round(pow(10, p));         p--;       }       cout << (nn - nnn) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 100100;   int n, m, k;   int a[mx];   int pos[mx];   int main() {     ios::sync_with_stdio(0);     cin >> n >> m >> k;     long long ans = 0;     for (int i = 1; i <= k; i++) {       cin >> a[i];       pos[a[i]] = i;     }     while (n--) {       for (int i = 0, x; i < m; i++) {         cin >> x;         ans += pos[x];         for (int j = pos[x]; j > 1; j--) {           swap(a[j], a[j - 1]);           pos[a[j]]++;         }         pos[x] = 1;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, t;     cin >> n >> t;     long long int i, a, c, ans, k = 0, f = 0;     for (i = 0; i < n; i++) {       cin >> a;       c = 86400 - a;       if (a == 0) {         c = 86400;       }       k += c;       if (k >= t && f == 0) {         ans = i + 1;         f = 100;       }     }     if (n == 1) {       cout << 1 << endl;       return 0;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 1e6;   int st[MX * 4], la[MX * 4], tin[MX], tout[MX], t, n;   vector<int> chd[MX];   void dfs(int u) {     tin[u] = ++t;     for (auto c : chd[u]) {       dfs(c);     }     tout[u] = t;   }   void push(int v) {     st[v * 2] += la[v];     la[v * 2] += la[v];     st[v * 2 + 1] += la[v];     la[v * 2 + 1] += la[v];     la[v] = 0;   }   void upd(int a, int b, int val, int v = 1, int l = 1, int r = n) {     if (b < l || r < a) return;     if (a <= l && r <= b) {       st[v] += val;       la[v] += val;     } else {       push(v);       int m = (l + r) / 2;       upd(a, b, val, v * 2, l, m);       upd(a, b, val, v * 2 + 1, m + 1, r);       st[v] = max(st[v * 2], st[v * 2 + 1]);     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int k;     cin >> n >> k;     vector<pair<int, int>> stk;     for (int i = 0; i < n; ++i) {       int a;       cin >> a;       while (!stk.empty() && stk.back().first < a) {         chd[i].push_back(stk.back().second);         stk.pop_back();       }       stk.emplace_back(a, i);     }     for (auto [a, i] : stk) {       dfs(i);     }     for (int i = 0; i < n; ++i) {       upd(tin[i], tout[i], 1);       if (i >= k - 1) {         if (i >= k) {           upd(tin[i - k], tout[i - k], -1);         }         cout << st[1] <<    ;       }     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int maxPrime[10000005];   long long cnt_prime[10000005];   int primes[10000005];   int cnt_p;   void init() {     for (int i = 2; i < 10000005; i += 2) maxPrime[i] = 2;     for (int i = 3; i * i < 10000005; i += 2) {       if (maxPrime[i] == i || maxPrime[i] == 0) {         maxPrime[i] = i;         for (int j = i * 2; j < 10000005; j += i) {           maxPrime[j] = i;         }       }     }     for (int i = 2; i < 10000005; i++) {       if (maxPrime[i] == 0) maxPrime[i] = i;       if (maxPrime[i] == i) {         primes[cnt_p++] = i;       }     }   }   bool good(long long x) {     for (int i = 0; i < cnt_p; i++) {       long long temp = x;       long long res = 0;       while (temp) {         temp /= primes[i];         res += temp;       }       if (res < cnt_prime[primes[i]]) return false;     }     return true;   }   int main() {     long long sum = 0;     int n;     int maxx = 0;     init();     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int a;       scanf( %d , &a);       maxx = max(maxx, a);       sum += a;       cnt_prime[a]++;     }     for (int i = maxx; i >= 2; i--) {       cnt_prime[i] += cnt_prime[i + 1];     }     for (int i = maxx; i >= 2; i--) {       cnt_prime[i / maxPrime[i]] += cnt_prime[i];       if (maxPrime[i] != i) {         cnt_prime[maxPrime[i]] += cnt_prime[i];       }     }     long long left = maxx, right = sum;     while (left <= right) {       long long mid = (left + right) / 2;       if (good(mid)) {         right = mid - 1;       } else {         left = mid + 1;       }     }     cout << left << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long DP[1000000];   long long counter[1000000];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       counter[x]++;     }     DP[0] = 0;     DP[1] = counter[1];     for (int i = 0; i < 1000000; i++) {       DP[i] = max(DP[i - 1], DP[i - 2] + counter[i] * i);     }     cout << DP[1000000 - 1];   }


#include <bits/stdc++.h>   using namespace std;   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);   }   const long long maxn = 100;   long long mas[maxn], mas2[maxn];   vector<pair<long long, long long> > cur;   vector<pair<long long, long long> > ans[maxn];   void split(long long n) {     cur.clear();     long long a, b;     a = 1, b = 1;     for (long long i = 2; i <= n; i++) {       if (mas[i] == mas[i - 1] + 1) {         b = i;       } else {         cur.push_back({a, b});         a = i, b = i;       }     }     cur.push_back({a, b});   }   signed main() {     fast();     long long n, i, j, p, k, a, b;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> mas[i];     }     long long x;     p = 0;     while (true) {       split(n);       long long sz = cur.size();       if (sz == 1) {         break;       }       p++;       x = mas[n];       if (x == n) {         x = mas[cur[sz - 2].second];         for (i = 1; i <= n; i++) {           if (mas[i] == x + 1) {             if (i != 1) {               ans[p].push_back({1, i - 1});             }             ans[p].push_back({i, cur[sz - 2].first - 1});             ans[p].push_back(cur[sz - 2]);             ans[p].push_back(cur[sz - 1]);             k = 1;             for (j = cur[sz - 1].first; j <= cur[sz - 1].second; j++) {               mas2[k] = mas[j];               k++;             }             for (j = cur[sz - 2].first; j <= cur[sz - 2].second; j++) {               mas2[k] = mas[j];               k++;             }             for (j = i; j <= cur[sz - 2].first - 1; j++) {               mas2[k] = mas[j];               k++;             }             if (i != 1) {               for (j = 1; j <= i - 1; j++) {                 mas2[k] = mas[j];                 k++;               }             }             for (k = 1; k <= n; k++) {               mas[k] = mas2[k];             }             break;           }         }       } else {         for (i = 1; i <= n; i++) {           if (mas[i] == x + 1) {             if (i != 1) {               ans[p].push_back({1, i - 1});             }             ans[p].push_back({i, cur[sz - 1].first - 1});             ans[p].push_back(cur[sz - 1]);             k = 1;             for (j = cur[sz - 1].first; j <= cur[sz - 1].second; j++) {               mas2[k] = mas[j];               k++;             }             for (j = i; j <= cur[sz - 1].first - 1; j++) {               mas2[k] = mas[j];               k++;             }             if (i != 1) {               for (j = 1; j <= i - 1; j++) {                 mas2[k] = mas[j];                 k++;               }             }             for (k = 1; k <= n; k++) {               mas[k] = mas2[k];             }             break;           }         }       }     }     cout << p << endl;     for (i = 1; i <= p; i++) {       cout << ans[i].size() <<    ;       for (j = 0; j < ans[i].size(); j++) {         cout << ans[i][j].second - ans[i][j].first + 1 <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void _fill_int(int* p, int val, int rep) {     int i;     for (i = 0; i < rep; i++) p[i] = val;   }   signed long long GETi() {     signed long long i;     scanf( %lld , &i);     return i;   }   int N, A[100001];   void solve() {     int f, i, j, k, l, x, y;     cin >> N;     for (i = 0; i < N; i++) cin >> A[i];     sort(A, A + N);     if (N == 1) return (void)printf( -1 n );     if (A[0] == A[N - 1]) return (void)printf( 1 n%d n , A[0]);     if (N == 2 && (A[1] - A[0]) % 2 == 0)       return (void)printf( 3 n%d %d %d n , A[0] - (A[1] - A[0]),                           A[0] + (A[1] - A[0]) / 2, A[1] + (A[1] - A[0]));     int mi = A[1] - A[0];     for (i = 0; i < N - 1; i++) mi = min(mi, A[i + 1] - A[i]);     x = -1;     for (i = 0; i < N - 1; i++) {       if (A[i + 1] - A[i] == mi)         continue;       else if (A[i + 1] - A[i] == 2 * mi && x == -1)         x = i;       else         return (void)printf( 0 n );     }     if (x == -1)       (void)printf( 2 n%d %d n , A[0] - mi, A[N - 1] + mi);     else       (void)printf( 1 n%d n , A[x] + mi);   }   int main(int argc, char** argv) {     string s;     if (argc == 1) ios::sync_with_stdio(false);     for (int i = 1; i < argc; i++) s += argv[i], s +=   n ;     for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int>> g;   vector<int> vis, d;   void dfs(int v, int c) {     d[v] = c;     vis[v] = true;     for (int u : g[v]) {       if (!vis[u]) dfs(u, c + 1);     }   }   void solve() {     int n, m;     cin >> n >> m;     g.resize(n + 1, vector<int>());     vis.resize(n + 1, false);     d.resize(n + 1, -1);     while (m--) {       int a, b;       cin >> a >> b;       g[a].push_back(b);       g[b].push_back(a);     }     dfs(1, 0);     int u = max_element(d.begin(), d.end()) - d.begin();     vis.clear();     vis.resize(n + 1, false);     dfs(u, 0);     cout << (*max_element(d.begin(), d.end())) <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int T = 1;     while (T--) {       solve();     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using namespace std;   int n, m;   pair<int, pair<int, int> > a[100010];   pair<int, int> g[100010];   int r[100010];   void solve() {     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> a[i].first >> a[i].second.first;       a[i].second.first *= -1;       a[i].second.second = i;     }     for (int i = 0; i < n; i++) r[i] = i + 1;     sort(a, a + m);     int cur = 0;     int lst = 0;     for (int i = 0; i < m; i++) {       if (a[i].second.first == -1) {         cur = 1;         lst++;         g[a[i].second.second] = pair<int, int>(0, lst);       } else {         while (cur < lst && r[cur] > lst) cur++;         if (cur >= lst) {           cout << -1;           return;         }         g[a[i].second.second] = pair<int, int>(cur, r[cur]);         r[cur]++;       }     }     for (int i = 0; i < m; i++) {       cout << g[i].first + 1 <<     << g[i].second + 1 << endl;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     srand(88);     solve();     return 0;   }


#include <bits/stdc++.h>      #define endl   n    #define fi first   #define se second   #define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))   #define forn(i,n) for (int i = 0; i < int(n); i++)   #define forr(i,a,b) for (int i = a; i <= b; i++)   #define all(v) v.begin(), v.end()   #define pb push_back      using namespace std;      typedef long long ll;   typedef long double ld;   typedef pair<int, int> ii;   typedef vector<int> vi;   typedef vector<vi> vvi;   typedef vector<ii> vii;      const int MX = 405, mod = 998244353;   int n, c[26];   ll dp[2][MX][MX][2][2][2][2], res;      int main () {    ios_base::sync_with_stdio(0); cin.tie(0);        cin >> n;    forn (i, 26)     cin >> c[i];        int x = 0, y = 1 - x;        forn (a, 2) forn (b, 2) forn (c, 2) forn (d, 2)     dp[x][n + 1][n + 1][a][b][c][d] = 1;          for (int t = n - 1; t >= 0; t--) {     forn (i, n + 2) forn (j, n + 2) forn (a, 2) forn (b, 2) forn (c, 2) forn (d, 2) {      ll &res = dp[y][i][j][a][b][c][d];      res = (24 - (!a && !c && t >= 2)) * dp[x][i][j][b][0][d][0];            if (!a)       res += dp[x][i == n + 1 ? i : i + 1][j][b][1][d][0];            if (!c)       res += dp[x][i][j == n + 1 ? j : j + 1][b][0][d][1];            res %= mod;     }          swap(x, y);    }        res = dp[x][n + 1][n + 1][0][0][0][0];        forn (i, 26)     res -= dp[x][n - c[i]][n + 1][0][0][0][0];        forn (i, 26) forn (j, i)     res += dp[x][n - c[i]][n - c[j]][0][0][0][0];        cout << MOD(res, mod) << endl;         return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long l;   long long calc(long long a, long long b, long long c) {     long long sum = b + c;     long long t = max(a, sum);     long long ret = 0;     while (t - a + sum - (b + c) <= l) {       long long rem = l - (t - a + sum - (b + c));       ret += (rem + 1) * (sum - (b + c) + 1);       sum++;       t = max(a, sum);     }     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long a, b, c;     cin >> a >> b >> c >> l;     long long ans = 0;     for (int i = 0; i <= l; i++) ans += (i + 2) * 1ll * (i + 1) / 2;     ans -= calc(a, b, c);     ans -= calc(b, a, c);     ans -= calc(c, a, b);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string s;     string s1;     cin >> s >> s1;     map<char, int> m;     m.insert(make_pair( a , 1));     m.insert(make_pair( b , 2));     m.insert(make_pair( c , 3));     m.insert(make_pair( d , 4));     m.insert(make_pair( e , 5));     m.insert(make_pair( f , 6));     m.insert(make_pair( g , 7));     m.insert(make_pair( h , 8));     m.insert(make_pair( i , 9));     m.insert(make_pair( j , 10));     m.insert(make_pair( k , 11));     m.insert(make_pair( l , 12));     m.insert(make_pair( m , 13));     m.insert(make_pair( n , 14));     m.insert(make_pair( o , 15));     m.insert(make_pair( p , 16));     m.insert(make_pair( q , 17));     m.insert(make_pair( r , 18));     m.insert(make_pair( s , 19));     m.insert(make_pair( t , 20));     m.insert(make_pair( u , 21));     m.insert(make_pair( v , 22));     m.insert(make_pair( w , 23));     m.insert(make_pair( x , 24));     m.insert(make_pair( y , 25));     m.insert(make_pair( z , 26));     int cnt1[27];     memset(cnt1, 0, sizeof(cnt1));     for (int i = 0; i < s.length(); i++) {       cnt1[m.find(s[i])->second]++;     }     int count1 = 0;     int cnt2[27];     memset(cnt2, 0, sizeof(cnt2));     for (int i = 0; i < s1.length(); i++) {       if (cnt1[m.find(s1[i])->second] == 0) {         cout << -1 << endl;         return 0;       }       cnt2[m.find(s1[i])->second]++;     }     for (int i = 1; i <= 26; i++) {       if (cnt2[i] > cnt1[i])         count1 += cnt1[i];       else if (cnt2[i] <= cnt1[i])         count1 += cnt2[i];     }     cout << count1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const long double EPS = 1e-11;   const long long INFL = 1e16;   const int INF = 2e9 + 7;   mt19937 rnd((unsigned)chrono::steady_clock::now().time_since_epoch().count());   void add(long long& a, long long b) {     a += b;     while (a >= MOD) {       a -= MOD;     }   }   void sub(long long& a, long long b) { add(a, MOD - b); }   void mul(long long& a, long long b) { a = (a * b) % MOD; }   long long todeg(long long x, long long d) {     if (d < 0) {       d += MOD - 1;     }     if (d == 0) {       return 1;     }     long long ans = todeg(x, d / 2);     mul(ans, ans);     if (d % 2 == 1) {       mul(ans, x);     }     return ans;   }   vector<long long> fact, rfact;   void prep(long long high) {     fact.assign(high + 1, 1);     rfact.resize(high + 1);     for (int i = 1; i <= high; ++i) {       fact[i] = i;       mul(fact[i], fact[i - 1]);     }     rfact[high] = todeg(fact[high], -1);     for (int i = high; i > 0; --i) {       rfact[i - 1] = rfact[i];       mul(rfact[i - 1], i);     }   }   long long C(int n, int k) {     long long ans = fact[n];     mul(ans, rfact[k]);     mul(ans, rfact[n - k]);     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     long long n, k;     cin >> n >> k;     prep(n + 5);     vector<long long> sposs(n, 1);     for (int i = 1; i < n; ++i) {       if (i <= k) {         sposs[i] = fact[i];         continue;       }       sposs[i] = sposs[i - 1];       mul(sposs[i], i);       long long del = sposs[i - k - 1];       mul(del, fact[i - 1]);       mul(del, rfact[i - k - 1]);       sub(sposs[i], del);     }     long long ans = fact[n];     for (int i = 0; i < n; ++i) {       long long cv = C(n - 1, i);       mul(cv, sposs[i]);       mul(cv, fact[n - i - 1]);       sub(ans, cv);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long rev_6 = 166666668ll;   const long long rev_2 = 500000004ll;   const long long MOD = 1000000007ll;   long long pow(long long A, long long n) {     long long r = 1ll;     while (n) {       if (n & 1ll) r = r * A % MOD;       A = A * A % MOD;       n >>= 1;     }     return r;   }   int main() {     bool odd = true, one = true;     int k;     long long ans = 2, temp;     cin >> k;     while (k--) {       cin >> temp;       if (temp > 1ll) one = false;       if (temp % 2ll == 0ll) odd = false;       ans = pow(ans, temp);     }     if (one)       cout <<  0/1 n ;     else {       long long q = ans * rev_2 % MOD;       if (odd)         ans -= 2;       else         ans += 2;       if (ans < 0) ans += MOD;       ans = ans * rev_6 % MOD;       cout << ans <<  /  << q << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int mod = 998244353;   const int M = 1e6 + 10;   const int N = 3e5 + 10;   inline long long read() {     long long b = 1, sum = 0;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) b = -1;       c = getchar();     }     while (isdigit(c)) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     return b * sum;   }   long long T, n, m;   int main() {     int i, j, k;     T = read();     while (T--) {       bool f = 0;       n = read();       long long x = 1, sum = 1;       vector<long long> ans;       while (1) {         if (n - sum >= 4 * x) {           ans.push_back(x);           x *= 2;           sum += x;         } else if (n - sum >= 3 * x) {           int x1 = n - sum - 2 * x;           x1 = x1 / 2;           int x2 = n - sum - 2 * x - 2 * x1;           ans.push_back(x1);           ans.push_back(x2);           break;         } else if (n - sum > 2 * x) {           ans.push_back(0);           ans.push_back(n - sum - 2 * x);           break;         } else if (n - sum >= x) {           ans.push_back(n - sum - x);           break;         } else {           f = 1;           break;         }       }       if (f) {         printf( -1 n );         continue;       }       printf( %d n , ans.size());       for (i = 0; i < ans.size(); i++) {         printf( %lld  , ans[i]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 5;   const int MAXT = 2e5 + 10;   long long a[MAXN];   long long cnt[MAXN];   long long ans[MAXT];   int L, R;   long long res;   struct node {     int l, r, bid, id;     bool operator<(const node &other) const {       if (other.bid == bid) return r < other.r;       return bid < other.bid;     }   } q[MAXT];   void query(int l, int r, int is) {     if (is) {       for (int i = l; i < L; i++) {         res += ((cnt[a[i]] << 1) + 1) * a[i];         cnt[a[i]]++;       }       for (int i = L; i < l; i++) {         cnt[a[i]]--;         res -= ((cnt[a[i]] << 1) + 1) * a[i];       }       for (int i = R + 1; i <= r; i++) {         res += ((cnt[a[i]] << 1) + 1) * a[i];         cnt[a[i]]++;       }       for (int i = r + 1; i <= R; i++) {         cnt[a[i]]--;         res -= ((cnt[a[i]] << 1) + 1) * a[i];       }     } else {       for (int i = l; i <= r; i++) {         res += ((cnt[a[i]] << 1) + 1) * a[i];         cnt[a[i]]++;       }     }     L = l;     R = r;     ans[q[is].id] = res;   }   int main() {     int n, t;     scanf( %d%d , &n, &t);     int bsize = sqrt(n + 1);     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     for (int i = 0; i < t; i++) {       scanf( %I64d%I64d , &q[i].l, &q[i].r);       q[i].bid = q[i].l / bsize;       q[i].id = i;     }     sort(q, q + t);     for (int i = 0; i < t; i++) query(q[i].l, q[i].r, i);     for (int i = 0; i < t; i++) printf( %I64d n , ans[i]);     return 0;   }