#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   #pragma GCC optimize( -ffloat-store )   using namespace std;   clock_t time_p = clock();   void aryanc403() {}   const long long int INF = 0xFFFFFFFFFFFFFFFL;   long long int seed;   mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());   inline long long int rnd(long long int l = 0, long long int r = INF) {     return uniform_int_distribution<long long int>(l, r)(rng);   }   class CMP {    public:     bool operator()(pair<long long int, long long int> a,                     pair<long long int, long long int> b) {       return !(a.first < b.first || (a.first == b.first && a.second <= b.second));     }   };   void add(map<long long int, long long int> &m, long long int x,            long long int cnt = 1) {     auto jt = m.find(x);     if (jt == m.end())       m.insert({x, cnt});     else       jt->second += cnt;   }   void del(map<long long int, long long int> &m, long long int x,            long long int cnt = 1) {     auto jt = m.find(x);     if (jt->second <= cnt)       m.erase(jt);     else       jt->second -= cnt;   }   bool cmp(const pair<long long int, long long int> &a,            const pair<long long int, long long int> &b) {     return a.first < b.first || (a.first == b.first && a.second < b.second);   }   const long long int mod = 1000000007L;   long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y;   long long int m;   string s, t, a, b;   bool fl;   vector<vector<long long int> > dp, bb;   long long int solve(long long int n, long long int m) {     long long int &ans = dp[n + 1][m + 1];     if (ans != -1) return ans;     ans = INF;     if (n != -1) {       long long int cur = solve(n - 1, m) + 1;       if (cur < INF) ans = min(ans, bb[cur][a[n] -  a ]);     }     if (m != -1) {       long long int cur = solve(n, m - 1) + 1;       if (cur < INF) ans = min(ans, bb[cur][b[m] -  a ]);     }     return ans;   }   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> T;     while (T--) {       cin >> s >> t;       s =  #  + s;       fl = false;       bb.clear();       bb.resize((int)(s).size() + 5, vector<long long int>(26, INF));       for (i = ((int)(s).size() - 1); i >= (1); --i) {         assert(i + 1 < (int)(bb).size());         bb[i] = bb[i + 1];         bb[i][s[i] -  a ] = i;       }       for (long long int l = 1; l <= (int)(t).size(); ++l) {         a = t.substr(0, l);         b = t.substr(l);         n = (int)(a).size();         m = (int)(b).size();         dp.clear();         dp.resize(n + 2, vector<long long int>(m + 2, -1));         dp[0][0] = 0;         if (solve(n - 1, m - 1) < (int)(s).size()) {           fl = true;           break;         }       }       cout << (fl ?  YES  :  NO ) <<   n ;     }     aryanc403();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline char get(void) {     static char buf[100000], *S = buf, *T = buf;     if (S == T) {       T = (S = buf) + fread(buf, 1, 100000, stdin);       if (T == S) return EOF;     }     return *S++;   }   inline void read(int& x) {     static char c;     x = 0;     for (c = get(); c <  0  || c >  9 ; c = get())       ;     for (; c >=  0  && c <=  9 ; c = get()) x = x * 10 + c -  0 ;   }   const int M = 500 * 1000 + 10;   const int up = 300000 + 10;   const int mod = 998244353;   int n;   int cnt[M] = {0};   bool vs[M] = {0};   int dp[M] = {0};   int fac[100];   int tp;   int num[100];   int C[8][M] = {0};   void init() {     C[0][0] = 1;     for (int i = 1; i < up; ++i) {       C[0][i] = 1;       for (int j = 1; j < 8; ++j) C[j][i] = (C[j - 1][i - 1] + C[j][i - 1]) % mod;     }   }   void dfs(int c, int al) {     if (c == tp) {       ++cnt[al];       return;     }     for (int i = 0; i < num[c] + 1; ++i) {       dfs(c + 1, al);       al *= fac[c];     }   }   void add(int a) {     tp = 0;     for (int i = 2; i * i <= a; ++i) {       if (0 == a % i) {         fac[tp] = i;         num[tp] = 0;         while (a % i == 0) ++num[tp], a /= i;         ++tp;       }     }     if (1 != a) {       fac[tp] = a;       num[tp++] = 1;     }     dfs(0, 1);   }   bool solve(int x) {     for (int t = up; t; --t) {       dp[t] = C[x][cnt[t]];       for (int i = 2; i * t < up; ++i) dp[t] = (dp[t] - dp[i * t] + mod) % mod;     }     return dp[1] > 0;   }   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     init();     cin >> n;     int a;     for (int i = 0; i < n; ++i) {       cin >> a;       if (vs[a]) continue;       vs[a] = 1;       add(a);     }     bool fg = 1;     if (vs[1]) {       fg = 0;       cout << 1 << endl;     }     for (int i = 2; i < 8; ++i) {       if (!fg) break;       if (solve(i)) {         fg = 0;         cout << i << endl;         break;       }     }     if (fg) cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = (long long)2e18;   const int N = 1005;   char s[N];   int lcp[N][N];   vector<pair<int, int>> segs;   bool cmp(pair<int, int> a, pair<int, int> b) {     int alen = a.second - a.first;     int blen = b.second - b.first;     int clen = min(alen, blen);     int com = lcp[a.first][b.first];     if (com < clen) return s[a.first + com] < s[b.first + com];     return alen < blen;   }   int n, m;   int mn[N];   long long dp[N][N];   void sadd(long long &a, long long b) {     a += b;     if (a >= INF) a = INF;   }   long long getCntGEQ(int id) {     for (int i = 0; i < n; i++) mn[i] = n + 1;     for (int i = id; i < (int)segs.size(); i++)       mn[segs[i].first] = min(mn[segs[i].first], segs[i].second);     memset(dp, 0, sizeof dp);     dp[0][0] = 1;     for (int i = 0; i < n; i++)       for (int j = 0; j <= m; j++) {         long long cur = dp[i][j];         if (i != 0) sadd(dp[i + 1][j], cur);         sadd(dp[mn[i]][j + 1], cur);       }     return dp[n][m];   }   void printStr(pair<int, int> p) {     int l = p.first, r = p.second;     for (int i = l; i < r; i++) printf( %c , s[i]);     printf(  n );   }   int main(int, char **) {     long long k;     scanf( %d%d%lld , &n, &m, &k);     scanf( %s , s);     for (int i = n - 1; i >= 0; i--)       for (int j = n - 1; j >= 0; j--)         if (s[i] == s[j]) lcp[i][j] = 1 + lcp[i + 1][j + 1];     for (int i = 0; i < n; i++)       for (int j = i + 1; j <= n; j++) segs.emplace_back(i, j);     sort(segs.begin(), segs.end(), cmp);     int lb = -1, rb = (int)segs.size();     while (rb - lb > 1) {       int mb = (lb + rb) / 2;       long long cur = getCntGEQ(mb);       if (cur >= k)         lb = mb;       else         rb = mb;     }     printStr(segs[lb]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   using V = vector<T>;   const long long int mod = 1000000007;   struct sb {     size_t c, ind;     unsigned long long int a, b;   };   bool cmp(sb a, sb b) { return a.c < b.c; }   struct s {     unsigned long long int sub;     size_t lastsub;     int before;     bool can;   } DP[51][101][51];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     size_t N, M;     unsigned long long int K;     cin >> N >> M >> K;     V<sb> subs(M);     for (__typeof(M) i = (0) - ((0) > (M)); i != (M) - ((0) > (M));          i += ((0) > (M) ? -1 : 1))       cin >> subs[i].a >> subs[i].b >> subs[i].c, subs[i].ind = i;     sort((subs).begin(), (subs).end(), cmp);     pair<size_t, unsigned long long int> ans;     for (__typeof(M) i = (0) - ((0) > (M)); i != (M) - ((0) > (M));          i += ((0) > (M) ? -1 : 1)) {       for (__typeof(subs[i].b + 1) j =                (subs[i].a) - ((subs[i].a) > (subs[i].b + 1));            j != (subs[i].b + 1) - ((subs[i].a) > (subs[i].b + 1));            j += ((subs[i].a) > (subs[i].b + 1) ? -1 : 1)) {         size_t l = j - subs[i].a;         DP[i][l][1].sub = j;         DP[i][l][1].lastsub = 0;         DP[i][l][1].before = -1;         DP[i][l][1].can = true;         for (__typeof(N + 1) k = (2) - ((2) > (N + 1));              k != (N + 1) - ((2) > (N + 1)); k += ((2) > (N + 1) ? -1 : 1)) {           DP[i][l][k].sub = 0;           DP[i][l][k].lastsub = 0;           DP[i][l][k].before = -1;           DP[i][l][k].can = false;           for (__typeof(0) z = (i) - ((i) > (0)); z != (0) - ((i) > (0));                z += ((i) > (0) ? -1 : 1)) {             if (subs[z].c == subs[i].c) continue;             if (j % K == 0 && j / K >= subs[z].a &&                 (j / K - subs[z].a) <= subs[z].b - subs[z].a) {               if (DP[z][j / K - subs[z].a][k - 1].can &&                   DP[z][j / K - subs[z].a][k - 1].sub + j >= DP[i][l][k].sub) {                 DP[i][l][k].sub = DP[z][j / K - subs[z].a][k - 1].sub + j;                 DP[i][l][k].lastsub = j / K - subs[z].a;                 DP[i][l][k].can = true;                 DP[i][l][k].before = z;               }             }             if (j >= K + subs[z].a &&                 (j - K - subs[z].a) <= subs[z].b - subs[z].a) {               if (DP[z][j - K - subs[z].a][k - 1].can &&                   DP[z][j - K - subs[z].a][k - 1].sub + j >= DP[i][l][k].sub) {                 DP[i][l][k].sub = DP[z][j - K - subs[z].a][k - 1].sub + j;                 DP[i][l][k].lastsub = j - K - subs[z].a;                 DP[i][l][k].can = true;                 DP[i][l][k].before = z;               }             }           }         }         if (DP[i][l][N].can &&             DP[i][l][N].sub >= DP[ans.first][ans.second][N].sub)           ans = {i, l};       }     }     if (!DP[ans.first][ans.second][N].can) {       cout <<  NO  << endl;       return 0;     }     cout <<  YES  << endl;     V<pair<size_t, unsigned long long int> > res;     for (__typeof(1) i = (N + 1) - ((N + 1) > (1)); i != (1) - ((N + 1) > (1));          i += ((N + 1) > (1) ? -1 : 1)) {       res.push_back({subs[ans.first].ind + 1, subs[ans.first].a + ans.second});       ans = {DP[ans.first][ans.second][i].before,              DP[ans.first][ans.second][i].lastsub};     }     for (__typeof(0) i = (N) - ((N) > (0)); i != (0) - ((N) > (0));          i += ((N) > (0) ? -1 : 1))       cout << res[i].first <<     << res[i].second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int32_t INFint = 1e9;   const long long INFll = 1e18;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long arr[n];       for (long long i = 0; i < n; i++) cin >> arr[i];       ;       sort(arr, arr + n);       long long ans = arr[0] * arr[n - 1];       long long fact[100000];       long long index = 0;       for (long long i = 2; i <= sqrt(ans); i++) {         if (ans % i == 0) {           fact[index] = i;           index++;           if (i != ans / i) {             fact[index] = ans / i;             index++;           }         }       }       sort(fact, fact + index);       if (index != n) {         cout << -1 <<   n ;       } else {         long long flag = 0;         for (long long i = 0; i < n; i++) {           if (fact[i] != arr[i]) {             flag = 1;             break;           }         }         ans = flag ? -1 : ans;         cout << ans <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     map<pair<string, string>, int> map_count;     vector<int> cost_list;     int n, a, b, k, f;     string s, t, prv =   ;     cin >> n >> a >> b >> k >> f;     for (int cost, i = 0; i < n; i++) {       cin >> s >> t;       if (s == prv) {         cost = b;       } else {         cost = a;       }       prv = t;       if (s > t) {         swap(s, t);       }       map_count[make_pair(s, t)] += cost;     }     for (auto& pkv : map_count) {       cost_list.push_back(pkv.second);     }     sort(cost_list.begin(), cost_list.end(), greater<int>());     int res = 0;     for (int i = 0; i < cost_list.size(); i++) {       if (i < k && cost_list[i] > f) {         res += f;       } else {         res += cost_list[i];       }     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int>> compos;   int complement_component(vector<vector<int>> &g) {     int n = g.size(), ans = 0;     set<int> S;     for (int i = 0; i < n; ++i) S.insert(i);     while (S.size()) {       ++ans;       int theCompo = 0;       queue<int> q;       q.push(*S.begin());       S.erase(S.begin());       compos.push_back(vector<int>{});       while (q.size()) {         int u = q.front();         q.pop();         compos.back().push_back(u);         if (g[u].size()) {           while (S.size() and *S.begin() < g[u][0]) {             q.push(*S.begin());             S.erase(S.begin());           }           int it = 0;           while (S.size() and it < g[u].size()) {             auto cur = S.upper_bound(g[u][it]);             while (cur != S.end() and                    (it + 1 == g[u].size() or *cur < g[u][it + 1])) {               q.push(*cur), S.erase(cur);               cur = S.upper_bound(g[u][it]);             }             ++it;           }         } else {           compos.back() = vector<int>(n);           iota(compos.back().begin(), compos.back().end(), 0);           return 1;         }       }     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int N, M, u, v;     cin >> N >> M;     vector<vector<int>> g(N);     while (M--) {       cin >> u >> v, --u, --v;       g[u].push_back(v), g[v].push_back(u);     }     for (int i = 0; i < N; ++i) sort(g[i].begin(), g[i].end());     cout << complement_component(g) <<   n ;     sort(compos.begin(), compos.end());     for (auto &compo : compos) {       cout << compo.size() <<    ;       for (int i = 0; i < compo.size(); ++i)         cout << compo[i] + 1 <<    n [i == compo.size() - 1];     }   }


#include <bits/stdc++.h>   using namespace std;   long long int n;   vector<long long int> a, b;   struct segmentTree {     vector<long long int> t;     segmentTree() { t.resize(4 * n); }     void add(long long int ind, long long int x, long long int v = 0,              long long int tl = 0, long long int tr = n) {       if (tr - tl == 1)         t[v] += x;       else {         long long int mid = (tl + tr) / 2;         if (ind >= mid)           add(ind, x, 2 * v + 2, mid, tr);         else           add(ind, x, 2 * v + 1, tl, mid);         t[v] = t[2 * v + 1] + t[2 * v + 2];       }     }     long long int getK(long long int k, long long int v = 0, long long int tl = 0,                        long long int tr = n) {       if (tr - tl == 1)         return tl;       else {         long long int mid = (tl + tr) / 2;         if (t[2 * v + 1] >= k)           return getK(k, 2 * v + 1, tl, mid);         else           return getK(k - t[2 * v + 1], 2 * v + 2, mid, tr);       }     }   };   struct query {     long long int k, p, ind;     query(long long int k = 0, long long int p = 0, long long int ind = 0)         : k(k), p(p), ind(ind) {}   };   signed main() {     cin >> n;     a.resize(n);     b.resize(n);     iota(b.begin(), b.end(), 0);     for (long long int i = 0; i < n; ++i) cin >> a[i];     sort(b.begin(), b.end(), [](long long int i, long long int j) {       return a[i] > a[j] || (a[i] == a[j] && i < j);     });     long long int q;     cin >> q;     vector<long long int> ans(q);     vector<query> queries;     for (long long int i = 0; i < q; ++i) {       long long int k, p;       cin >> k >> p;       queries.emplace_back(k, p, i);     }     sort(queries.begin(), queries.end(),          [](const query& a, const query& b) { return a.k < b.k; });     long long int last = 0;     segmentTree t;     for (auto ev : queries) {       while (last < ev.k) t.add(b[last++], 1);       long long int ind = t.getK(ev.p);       ans[ev.ind] = a[ind];     }     for (auto i : ans) cout << i <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 1e18;   long long k, n, c;   string s;   long long p[26][26];   long long f[105][105][26];   char x, y;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> s >> k >> n;     for (long long i = 1; i <= n; i++) {       cin >> x >> y >> c;       p[x -  a ][y -  a ] = c;     }     s =  *  + s;     for (long long i = 0; i < s.size(); i++)       for (long long j = 0; j <= k; j++)         for (long long v = 0; v < 26; v++) f[i][j][v] = -MAX;     for (long long i = 0; i < 26; i++) {       if (i == s[1] -  a )         f[1][0][i] = 0;       else         f[1][1][i] = 0;     }     for (long long i = 2; i < s.size(); i++) {       for (long long j = 0; j <= min(i - 1, k); j++) {         for (long long v = 0; v < 26; v++) {           for (long long u = 0; u < 26; u++) {             long long ok = (s[i] -  a  != v);             f[i][j + ok][v] = max(f[i][j + ok][v], f[i - 1][j][u] + p[u][v]);           }         }       }     }     long long ans = -MAX;     for (long long i = 0; i <= k; i++)       for (long long j = 0; j < 26; j++) ans = max(f[s.size() - 1][i][j], ans);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k, mx, a[100][100], dp[100][100], me[100], inf = -1e15;   int sub(int a, int b) { return (((a - b) % k) + k); }   signed main(void) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m >> k;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) cin >> a[i][j];     memset(dp, -1, sizeof dp);     dp[0][0] = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         for (int u = min(m / 2, j); u >= 1; u--) {           for (int t = 0; t < k; t++) {             int x = sub(t, a[i][j]) % k;             if (dp[u - 1][x] != -1)               dp[u][t] = max(dp[u][t], dp[u - 1][x] + a[i][j]);           }         }       }       for (int u = 1; u <= m / 2; u++) {         for (int t = 0; t < k; t++) {           dp[0][t] = max(dp[0][t], dp[u][t]);           if (n != i) dp[u][t] = -1;         }       }     }     int mx = 0;     for (int i = 0; i <= m / 2; i++) mx = max(mx, dp[i][0]);     cout << mx <<   n ;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   #pragma GCC optimize(3)   #pragma GCC optimize( Ofast )   #pragma GCC optimize( inline )   #pragma GCC optimize( -fgcse )   #pragma GCC optimize( -fgcse-lm )   #pragma GCC optimize( -fipa-sra )   #pragma GCC optimize( -ftree-pre )   #pragma GCC optimize( -ftree-vrp )   #pragma GCC optimize( -fpeephole2 )   #pragma GCC optimize( -ffast-math )   #pragma GCC optimize( -fsched-spec )   #pragma GCC optimize( unroll-loops )   #pragma GCC optimize( -falign-jumps )   #pragma GCC optimize( -falign-loops )   #pragma GCC optimize( -falign-labels )   #pragma GCC optimize( -fdevirtualize )   #pragma GCC optimize( -fcaller-saves )   #pragma GCC optimize( -fcrossjumping )   #pragma GCC optimize( -fthread-jumps )   #pragma GCC optimize( -funroll-loops )   #pragma GCC optimize( -fwhole-program )   #pragma GCC optimize( -freorder-blocks )   #pragma GCC optimize( -fschedule-insns )   #pragma GCC optimize( inline-functions )   #pragma GCC optimize( -ftree-tail-merge )   #pragma GCC optimize( -fschedule-insns2 )   #pragma GCC optimize( -fstrict-aliasing )   #pragma GCC optimize( -fstrict-overflow )   #pragma GCC optimize( -falign-functions )   #pragma GCC optimize( -fcse-skip-blocks )   #pragma GCC optimize( -fcse-follow-jumps )   #pragma GCC optimize( -fsched-interblock )   #pragma GCC optimize( -fpartial-inlining )   #pragma GCC optimize( no-stack-protector )   #pragma GCC optimize( -freorder-functions )   #pragma GCC optimize( -findirect-inlining )   #pragma GCC optimize( -frerun-cse-after-loop )   #pragma GCC optimize( inline-small-functions )   #pragma GCC optimize( -finline-small-functions )   #pragma GCC optimize( -ftree-switch-conversion )   #pragma GCC optimize( -foptimize-sibling-calls )   #pragma GCC optimize( -fexpensive-optimizations )   #pragma GCC optimize( -funsafe-loop-optimizations )   #pragma GCC optimize( inline-functions-called-once )   #pragma GCC optimize( -fdelete-null-pointer-checks )   using namespace std;   inline long long read() {     long long x = 0, y = 0, c = getchar();     while (!isdigit(c)) y = c, c = getchar();     while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^  0 ), c = getchar();     return y ==  -  ? -x : x;   }   inline void print(long long q) {     if (q < 0) putchar( - ), q = -q;     if (q >= 10) print(q / 10);     putchar(q % 10 +  0 );   }   long long s1 = 19260817, s2 = 23333333, s3 = 998244353, srd;   inline long long rd() { return srd = (srd * s1 + s2 + rand()) % s3; }   void file() {     freopen( lis.in ,  r , stdin);     freopen( lis.out ,  w , stdout);   }   int n, m;   set<int> s;   int t[2][800010];   void ins(int op, int q, int l, int r, int ax, int x) {     if (l == r) {       t[op][q] = x;       return;     }     int mid = l + r >> 1;     if (mid >= ax)       ins(op, q << 1, l, mid, ax, x);     else       ins(op, q << 1 | 1, mid + 1, r, ax, x);     t[op][q] = max(t[op][q << 1], t[op][q << 1 | 1]);   }   int qy(int op, int q, int l, int r, int al, int ar) {     if (l >= al && r <= ar) return t[op][q];     int mid = l + r >> 1;     int as = 0;     if (mid >= al) as = qy(op, q << 1, l, mid, al, ar);     if (mid < ar) as = max(as, qy(op, q << 1 | 1, mid + 1, r, al, ar));     return as;   }   int gx(int op, int q) {     int tmp = qy(op, 1, 0, (!op ? n : m + 10), 0, q - 1) + 1;     ins(op, 1, 0, (!op ? n : m + 10), q, tmp);     return tmp;   }   int gd[200010], wz[200010];   int main() {     srand(time(0));     rd();     int i, j, op, x, y;     n = read();     m = read();     for (i = 1; i <= m; ++i) {       op = read();       x = read();       if (op == 1) {         y = read();         x = n - x + 1;         y = i - y + 10;         gd[x] = y;         wz[y] = x;         s.insert(x);         for (j = y + 1; j <= y + 9; ++j)           if (wz[j]) ins(0, 1, 0, n, wz[j], 0);         for (j = y; j <= y + 9; ++j)           if (wz[j]) {             int nxt = gx(0, wz[j]);             ins(1, 1, 0, m + 10, j, nxt);           }       } else {         set<int>::iterator it = s.end(), tp;         while (x--) {           --it;           ins(1, 1, 0, m + 10, gd[*it], 0);         }         tp = it;         ins(0, 1, 0, n, *it, 0);         wz[gd[*it]] = 0;         gd[*it] = 0;         for (++it; it != s.end(); ++it) {           int nxt = gx(1, gd[*it]);           ins(0, 1, 0, n, *it, nxt);         }         s.erase(tp);       }       print(t[0][1]);       putchar(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, f[10] = {0}, x, i;     long long ans, z;     scanf( %lld , &n);     scanf( %lld , &m);     for (i = 0; i < n; i++) {       scanf( %lld , &x);       f[x - 1]++;     }     ans = (n * (n - 1)) / 2;     for (i = 0; i < m; i++) {       z = (f[i] * (f[i] - 1)) / 2;       ans -= z;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   bool IsPrime(int n) {     if (n == 1) return false;     for (int d = 2; d * d <= n; d++) {       if (n % d == 0) return false;     }     return true;   }   int main() {     int n;     cin >> n;     int count = 0;     while (n > 0) {       int j = 0;       int k = n;       int amx = INT_MIN;       while (k > 0) {         j = k % 10;         k = k / 10;         amx = max(amx, j);       }       n -= amx;       count++;     }     cout << count << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 998244353, N = 1e6 + 5;   long long fact[N], d[N];   long long f(int x) {     if (x == 0) return 1;     if (fact[x] != 0) return fact[x];     return fact[x] = (x * f(x - 1)) % MOD;   }   int main() {     d[1] = 1;     for (int i = 2; i < N; i++) {       d[i] = (1ll * i * (f(i - 1) - 1 + d[i - 1])) % MOD;     }     int n;     cin >> n;     cout << d[n];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void chkmin(T &a, T b) {     a = min(a, b);   }   template <typename T>   void chkmax(T &a, T b) {     a = max(a, b);   }   inline int fread(void) {     int ret = 0, op = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) op = -1;     for (; isdigit(ch); ch = getchar()) ret = (ret << 1) + (ret << 3) -  0  + ch;     return ret * op;   }   const int MAXN = 2e5 + 10;   const int MAXM = +10;   int n, m, a[MAXN], c[MAXN];   struct rec {     int t, r;     bool operator<(const rec &b) const { return r < b.r; }   } s[MAXN];   int top;   inline void init(void) {     n = fread(), m = fread();     for (int i = 1; i <= n; ++i) a[i] = fread(), c[i] = a[i];   }   int lt, rt;   inline void work(void) {     rec now;     top = 0;     for (int i = 1; i <= m; ++i) {       now.t = fread(), now.r = fread();       while (top && s[top] < now) --top;       s[++top] = now;     }     sort(c + 1, c + s[1].r + 1);     lt = 1;     rt = s[1].r;     ++top;     s[top].r = 0;     for (int i = 2; i <= top; ++i) {       if (s[i - 1].t == 1) {         for (int j = s[i - 1].r; j > s[i].r; --j) a[j] = c[rt--];       } else {         for (int j = s[i - 1].r; j > s[i].r; --j) a[j] = c[lt++];       }     }     for (int i = 1; i <= n; ++i) printf( %d  , a[i]);   }   int main() {     init();     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool cmp(const pair<long long int, long long int> &a,            const pair<long long int, long long int> &b) {     return a.second < b.second;   }   int main() {     long long int n, k, ai, bi, total = 0, ans = 0;     vector<long long int> a;     vector<pair<long long int, long long int> > ab;     map<long long int, long long int> jobs;     scanf( %lld %lld , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %lld , &ai), a.push_back(ai), jobs[ai]++;       if (jobs[ai] == 1) total++;     }     for (int i = 0; i < n; i++)       scanf( %lld , &bi), ab.push_back(make_pair(a[i], bi));     sort(ab.begin(), ab.end(), cmp);     total = k - total;     for (int i = 0; i < n; i++) {       if (jobs[ab[i].first] > 1 && total > 0) {         ans += ab[i].second;         total--;         jobs[ab[i].first]--;       }     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     scanf( %d n , &n);     string a, b;     getline(cin, a);     getline(cin, b);     int q(0);     map<pair<char, char>, int> SET;     map<char, int> have;     for (int i(0), _l((int)(n)-1); i <= _l; ++i) {       if (a[i] != b[i]) {         ++q;         SET[make_pair(a[i], b[i])] = i;         have[a[i]] = i;       }     }     int delta(0);     pair<int, int> ans(-2, -2);     for (int i(0), _l((int)(n)-1); i <= _l; ++i) {       if (a[i] != b[i]) {         if (have.count(b[i]) && delta == 0)           delta = 1, ans = make_pair(i, have[b[i]]);         if (SET.count(make_pair(b[i], a[i])))           delta = 2, ans = make_pair(i, SET[make_pair(b[i], a[i])]);       }     }     cout << q - delta << endl;     cout << ans.first + 1 <<     << ans.second + 1 << endl;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[200005], b[200005];   int nx[500005];   int n, m;   void getnext() {     nx[0] = -1;     int j = 0, k = -1;     while (j < m) {       if (k == -1 || b[j] == b[k])         nx[++j] = ++k;       else         k = nx[k];     }   }   int KMP() {     int ret = 0;     int i = 0, j = 0;     for (i = 0; i < n; i++) {       while (j > 0 && a[i] != b[j]) j = nx[j];       if (a[i] == b[j]) j++;       if (j == m) {         ret++;         j = nx[j];       }     }     return ret;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (i != 0) a[i - 1] = a[i] - a[i - 1];     }     for (int i = 0; i < m; i++) {       cin >> b[i];       if (i != 0) b[i - 1] = b[i] - b[i - 1];     }     if (m == 1) {       cout << n << endl;       return 0;     }     n--;     m--;     getnext();     cout << KMP() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1010;   int N;   long long c[4][maxn][maxn];   void upd(int p, int i, int j, long long x) {     int jj = j;     while (i <= N) {       j = jj;       while (j <= N) c[p][i][j] ^= x, j += j & (-j);       i += i & (-i);     }   }   long long get(int p, int i, int j) {     int jj = j;     long long ans = 0;     while (i) {       j = jj;       while (j) ans ^= c[p][i][j], j -= j & (-j);       i -= i & (-i);     }     return ans;   }   void upd(int i, int j, long long x) {     upd(0, i, j, x);     upd(1, i, j, x * (i % 2));     upd(2, i, j, x * (j % 2));     upd(3, i, j, x * (i % 2) * (j % 2));   }   long long get(int i, int j) {     long long s0 = get(0, i, j);     long long s1 = get(1, i, j);     long long s2 = get(2, i, j);     long long s3 = get(3, i, j);     int x = i, y = j;     x++, y++;     x %= 2, y %= 2;     return (x * y * s0) ^ (y * s1) ^ (x * s2) ^ (s3);   }   int main() {     int n, m, i, j, k;     int x0, x1, y0, y1;     scanf( %d%d , &n, &m);     N = n;     while (m--) {       scanf( %d , &i);       if (i == 1) {         scanf( %d%d%d%d , &x0, &y0, &x1, &y1);         printf( %I64d n , get(x1, y1) ^ get(x0 - 1, y1) ^ get(x1, y0 - 1) ^                               get(x0 - 1, y0 - 1));       } else {         scanf( %d%d%d%d , &x0, &y0, &x1, &y1);         long long v;         scanf( %I64d , &v);         upd(x0, y0, v);         upd(x0, y1 + 1, v);         upd(x1 + 1, y0, v);         upd(x1 + 1, y1 + 1, v);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<long long int> v(n);     for (int i = 0; i < n; i++) {       cin >> v[i];     }     int i;     for (int i = 0; i < n; i++) {       while (v[i] % 2 == 0) {         v[i] /= 2;       }       while (v[i] % 3 == 0) {         v[i] /= 3;       }     }     cout << endl;     vector<long long int>::iterator it = min_element(v.begin(), v.end());     vector<long long int>::iterator it2 = max_element(v.begin(), v.end());     if (*it == *it2)       cout <<  Yes ;     else       cout <<  No ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long max(long long a, long long b) {     if (a < b) return b;     return a;   }   long long min(long long a, long long b) {     if (a < b) return a;     return b;   }   bool okay(vector<long long> &v, long long d, long long k) {     long long sum = 0;     priority_queue<long long, vector<long long>, greater<long long>> pq;     for (int i = 0; i < d; i++) pq.push(0);     for (auto x : v) {       long long ind = pq.top();       pq.pop();       sum += max(0, x - ind);       pq.push(ind + 1);     }     return sum >= k;   }   void solve() {     long long n, m;     cin >> n >> m;     vector<long long> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     sort(v.rbegin(), v.rend());     long long low = 1, high = n, ans = -1;     while (low <= high) {       long long mid = low + (high - low) / 2;       if (okay(v, mid, m)) {         ans = mid;         high = mid - 1;       } else         low = mid + 1;     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace GR1 {   vector<int> solve(int N, int num_exact_matches, int num_common,                     const vector<int>& B) {     vector<queue<int> > pos(N + 2);     for (int i = 0, _n = (N); i < _n; ++i) {       int b = B[i];       pos[b].push(i);     }     int missing = 0;     set<pair<int, int>, greater<pair<int, int> > > S;     for (int v = (1), _b = (N + 1); v <= _b; ++v)       if (!pos[v].empty())         S.emplace(int((pos[v]).size()), v);       else         missing = v;     assert(missing > 0);     vector<int> res(N);     vector<bool> exact(N);     for (int t = 0, _n = (num_exact_matches); t < _n; ++t) {       auto it = S.begin();       int v = it->second;       S.erase(it);       queue<int>& q = pos[v];       int i = q.front();       q.pop();       res[i] = v;       exact[i] = true;       if (!q.empty()) S.emplace(int((q).size()), v);     }     int num_mismatches = N - num_common;     int max_freq = S.begin()->first;     if (max_freq * 2 > N - num_exact_matches + num_mismatches) return {};     vector<int> block;     for (auto it : S) {       int v = it.second;       while (!pos[v].empty()) {         int j = pos[v].front();         pos[v].pop();         block.push_back(j);       }     }     for (int t = 0, _n = ((N - num_exact_matches) / 2); t < _n; ++t)       block.push_back(block[t]);     for (int k = 0, _n = (N - num_exact_matches); k < _n; ++k) {       int i = block[k];       int j = block[k + (N - num_exact_matches) / 2];       res[i] = B[j];     };     ;     for (int i = 0, _n = (N); i < _n; ++i) {       if (exact[i]) continue;       if (res[i] == B[i]) {         assert(num_mismatches > 0);         --num_mismatches;         res[i] = missing;       }     };     ;     for (int i = 0; i < N && num_mismatches > 0; ++i) {       if (exact[i] || res[i] == missing) continue;       --num_mismatches;       res[i] = missing;     }     assert(num_mismatches == 0);     return res;   }   }  // namespace GR1   namespace GR2 {   vector<int> solve(int N, int num_exact_matches, int num_common,                     const vector<int>& B) {     vector<queue<int> > pos(N + 2);     for (int i = 0, _n = (N); i < _n; ++i) {       int b = B[i];       pos[b].push(i);     }     int missing = 0;     set<pair<int, int>, greater<pair<int, int> > > S;     for (int v = (1), _b = (N + 1); v <= _b; ++v)       if (!pos[v].empty())         S.emplace(int((pos[v]).size()), v);       else         missing = v;     assert(missing > 0);     vector<int> res(N);     for (int t = 0, _n = (num_exact_matches); t < _n; ++t) {       auto it = S.begin();       int v = it->second;       S.erase(it);       queue<int>& q = pos[v];       int i = q.front();       q.pop();       res[i] = v;       if (!q.empty()) S.emplace(int((q).size()), v);     }     int num_mismatches = N - num_common;     num_common -= num_exact_matches;     while (num_common > 0) {       assert(!S.empty());       auto max_it = S.begin();       int v = max_it->second;       S.erase(max_it);       assert(!pos[v].empty());       int i = pos[v].front();       pos[v].pop();       if (B[i] == v) {         if (int((S).size()) > 0) {           auto max2_it = S.begin();           int w = max2_it->second;           S.erase(max2_it);           assert(!pos[w].empty());           int j = pos[w].front();           pos[w].pop();           if (!pos[w].empty()) S.emplace(int((pos[w]).size()), w);           res[i] = w;           --num_common;           if (num_common > 0) {             res[j] = v;             --num_common;           }         } else if (num_mismatches > 0) {           res[i] = missing;           --num_mismatches;         } else           return {};       } else {         res[i] = v;         --num_common;       }       if (!pos[v].empty()) S.emplace(int((pos[v]).size()), v);     }     for (int i = 0, _n = (N); i < _n; ++i) {       if (res[i] == 0) {         assert(num_mismatches > 0);         --num_mismatches;         res[i] = missing;       }     }     return res;   }   }  // namespace GR2   int main(int argc, char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int TC;     cin >> TC;     for (int tc = (1), _b = (TC); tc <= _b; ++tc) {       int N, x, y;       cin >> N >> x >> y;       vector<int> B(N);       for (int i = 0, _n = (N); i < _n; ++i) cin >> B[i];       vector<int> res = GR1::solve(N, x, y, B);       if (res.empty())         cout <<  NO n ;       else {         cout <<  YES n ;         for (int a : res) cout << a <<    ;         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   bool v[2001];   vector<int> G[2001];   int cnt = 0;   int a[2001][2001];   int Dfn[2001], Low[2001];   int Dindex = 0;   int Bcnt;   bool instack[2001];   int Sta[2001];   int top = -1;   void add(int x, int y) { G[x].push_back(y); }   void read() {     scanf( %d , &N);     for (int i = 0; i < N; ++i)       for (int j = 0; j < N; ++j) {         scanf( %d , &a[i][j]);       }     for (int i = 0; i < N; ++i)       for (int j = 0; j < N; ++j)         if (a[i][j] && i != j) add(i, j);   }   void tarjan(int v) {     Dfn[v] = Low[v] = ++Dindex;     instack[v] = 1;     Sta[++top] = v;     for (int i = 0; i < G[v].size(); ++i) {       int w = G[v][i];       if (!Dfn[w]) {         tarjan(w);         Low[v] = min(Low[v], Low[w]);       } else if (instack[w])         Low[v] = min(Low[v], Dfn[w]);     }     int j;     if (Dfn[v] == Low[v]) {       Bcnt++;       do {         j = Sta[top--];         instack[j] = 0;       } while (j != v);     }   }   int main() {     read();     Bcnt = 0;     for (int i = 0; i < N; ++i)       if (!Dfn[i]) tarjan(i);     if (Bcnt > 1)       puts( NO );     else       puts( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e6 + 10;   int n, m;   int a[N];   int find(int x) {     for (int i = 1; i <= n; i++) {       if (a[i] == x) return i;     }   }   vector<int> v;   signed main() {     int x, y, z, T;     cin >> T;     while (T--) {       scanf( %d , &n);       for (int i = 1; i <= n; i++) scanf( %d , &a[i]);       bool f = 0;       for (int i = 1; i <= n; i += 2)         if (a[i] % 2 == 0) f = 1;       if (f) {         puts( -1 );         continue;       }       v.clear();       for (int i = n; i >= 1; i -= 2) {         if (i == 1) continue;         int l = find(i), r = find(i - 1);         if (l == i && r == i - 1) continue;         if (l == 1 && r == 2) {           v.push_back(i);           reverse(a + 1, a + 1 + i);           continue;         }         v.push_back(l);         reverse(a + 1, a + 1 + l);         l = 1, r = find(i - 1);         if (l == 1 && r == 2) {           v.push_back(i);           reverse(a + 1, a + 1 + i);           continue;         }         v.push_back(r - 1);         reverse(a + 1, a + 1 + r - 1);         v.push_back(r + 1);         reverse(a + 1, a + 1 + r + 1);         v.push_back(3);         reverse(a + 1, a + 1 + 3);         v.push_back(i);         reverse(a + 1, a + 1 + i);       }       cout << v.size() << endl;       for (int i : v) cout << i <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, t, c, d[555555], res = 0, ll = -1;   bool try_ans(int k) {     if (d[n - 1] > k) return false;     int last = 0, now = 0, res, l2 = -1;     vector<pair<int, int> > aa;     for (int i = 0; i < n; i++) {       if (d[i] > k) {         l2 = i;         if (!last) aa.push_back(make_pair(i, now));         last = 1;         now += 2;       } else if (last == 1)         last = 2, l2 = i;       else         last = 0;     }     if (ll > l2) l2 = ll;     res = now + l2 + 1;     for (int i = aa.size() - 1; i >= 0; i--)       if (2 * l2 + 1 - aa[i].first + aa[i].second < res)         res = 2 * l2 + 1 - aa[i].first + aa[i].second;     return res <= t;   }   int ans(int max, int min) {     if (max == min) return min;     int mid = (max + min) / 2;     bool a = try_ans(mid);     if (a)       return ans(mid, min);     else       return ans(max, mid + 1);   }   int main() {     cin >> n >> t;     string str;     cin >> str;     int now = 0;     for (int i = 0; i < n; i++) {       c = str[i];       if (c ==  H ) now++, ll = i;       if (c ==  S ) now--;       d[i] = now;       if (now > res) res = now;     }     if (ll + 1 > t)       cout << -1;     else {       cout << ans(res, 0);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T& x) {     x = 0;     int fl = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) fl = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + ch -  0 ;       ch = getchar();     }     x *= fl;   }   template <typename T, typename... Args>   inline void read(T& t, Args&... args) {     read(t);     read(args...);   }   const int N = 3005;   int T, n, a[N], b[N];   int main() {     for (read(T); T; T--) {       read(n);       for (int i = 1; i <= n; i++) read(a[i]), b[i] = a[i];       sort(b + 1, b + n + 1);       int len = unique(b + 1, b + n + 1) - (b + 1);       for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;       int now = 1, mx = 0;       while (now <= n) {         int len = 0;         for (int i = 1; i <= n; i++)           if (a[i] == now) len++, now++;         mx = max(mx, len);       }       printf( %d n , n - mx);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int MN = 200010;   int exp(int x, int n) {     int ret = 1;     while (n) {       if (n & 1) ret = 1LL * ret * x % mod;       x = 1LL * x * x % mod;       n >>= 1;     }     return ret;   }   int inv(int x) { return exp(x, mod - 2); }   int fact[MN], invf[MN];   int comb(int n, int k) {     if (n < k) return 0;     return 1LL * fact[n] * invf[k] % mod * invf[n - k] % mod;   }   int S(int n, int k) {     int ret = 0;     for (int i = 0; i < k; i++) {       ret += 1LL * comb(k, i) * (i % 2 ? mod - 1 : 1) % mod * exp(k - i, n) % mod;       ret %= mod;     }     ret = 1LL * ret * invf[k] % mod;     return ret;   }   int N, K;   int W[MN];   int main() {     fact[0] = 1;     for (int i = 1; i < MN; i++) {       fact[i] = 1LL * fact[i - 1] * i % mod;     }     for (int i = 0; i < MN; i++) {       invf[i] = inv(fact[i]);     }     scanf( %d %d , &N, &K);     int sum = 0;     for (int i = 0; i < N; i++) {       scanf( %d , &W[i]);       sum += W[i];       sum %= mod;     }     int ans =         1LL * ((S(N, K) + 1LL * (N - 1) * S(N - 1, K) % mod) % mod) * sum % mod;     printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int MAXN = 1e5 + 10;   bool bd[MAXN];   int n;   int a[MAXN];   vector<pair<int, int>> sol;   pair<int, int> coord(pair<int, int> p) { return {(n - 1 - p.second), p.first}; }   int main(void) {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d , &a[i]);     for (int i = 0; i < n; ++i) bd[i] = false;     int lst = -1;     queue<int> dva;     for (int i = n - 1; i >= 0; --i) {       if (a[i] == 0) continue;       while (!dva.empty() && bd[dva.front()]) dva.pop();       if ((a[i] == 2 && dva.empty()) || (a[i] == 3 && lst == -1)) {         printf( -1 n );         return 0;       }       if (a[i] == 1) {         sol.emplace_back(i, (n - 1) - i);         lst = i;         dva.push(i);         continue;       }       if (a[i] == 2) {         int xx = dva.front();         dva.pop();         bd[i] = bd[xx] = true;         sol.emplace_back(i, (n - 1) - xx);         lst = i;       }       if (a[i] == 3) {         sol.emplace_back(i, (n - 1) - i);         sol.emplace_back(lst, (n - 1) - i);         bd[i] = bd[lst] = true;         lst = i;       }     }     printf( %d n , (int)sol.size());     for (auto &p : sol) {       auto c = coord(p);       printf( %d %d n , c.first + 1, c.second + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string moves[] = { MW ,  LW ,  LM };   int sequence[25];   void listify(int k, int n) {     for (int i = 0; i != n; ++i) {       sequence[i] = k % 3;       k /= 3;     }   }   void solve(int n, vector<vector<int> >& V) {     int n1 = n / 2;     int n2 = n - n1;     int k1 = pow(3, n1), k2 = pow(3, n2);     vector<int> state(3);     map<pair<int, int>, pair<int, int> > sums;     for (int k = 0; k != k1; ++k) {       listify(k, n1);       state[0] = state[1] = state[2] = 0;       for (int i = 0; i != n1; ++i) {         state[(sequence[i] + 1) % 3] += V[i][(sequence[i] + 1) % 3];         state[(sequence[i] + 2) % 3] += V[i][(sequence[i] + 2) % 3];       }       pair<int, int> p(state[1] - state[0], state[2] - state[0]);       if (sums.find(p) == sums.end() || sums[p].first < state[0]) {         sums[p] = make_pair(state[0], k);       }     }     int max_equal_rel = -1000000000;     pair<int, int> argmax;     for (int k = 0; k != k2; ++k) {       listify(k, n2);       state[0] = state[1] = state[2] = 0;       for (int i = 0; i != n2; ++i) {         state[(sequence[i] + 1) % 3] += V[i + n1][(sequence[i] + 1) % 3];         state[(sequence[i] + 2) % 3] += V[i + n1][(sequence[i] + 2) % 3];       }       pair<int, int> p(state[0] - state[1], state[0] - state[2]);       if (sums.find(p) != sums.end() &&           sums[p].first + state[0] > max_equal_rel) {         max_equal_rel = sums[p].first + state[0];         argmax = make_pair(sums[p].second, k);       }     }     if (max_equal_rel == -1000000000) {       cout <<  Impossible  << endl;     } else {       listify(argmax.first, n1);       for (int i = 0; i != n1; ++i) {         cout << moves[sequence[i]] << endl;       }       listify(argmax.second, n2);       for (int i = 0; i != n2; ++i) {         cout << moves[sequence[i]] << endl;       }     }   }   int main() {     int n;     cin >> n;     vector<vector<int> > V(n, vector<int>(3));     for (int i = 0; i != n; ++i) {       cin >> V[i][0] >> V[i][1] >> V[i][2];     }     solve(n, V);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long powmod(long long a, long long b) {     long long res = 1;     a %= 1000000007;     for (; b; b >>= 1) {       if (b & 1) res = res * a % 1000000007;       a = a * a % 1000000007;     }     return res;   }   template <typename T>   inline bool chkmin(T &a, const T &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T>   inline bool chkmax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   const int P = 10000007;   unsigned int p[500100], l[500100], r[500100];   int m;   char ans[500100], s[500100];   int n;   bool ok(int i, int x) {     if (++i < x) return 0;     return ((r[i] - r[i - x] * p[x]) * p[i - x]) == (l[i] - l[i - x]);   }   bool dfs(int i, int x) {     if (i == n) {       puts(ans);       return 1;     }     ans[i] = x ? s[i] :  a ;     for (; ans[i] <=  z ; ans[i]++) {       l[i + 1] = l[i] + ans[i] * p[i];       r[i + 1] = r[i] * P + ans[i];       if (!ok(i, m) && !ok(i, m + 1) && dfs(i + 1, x && (ans[i] == s[i])))         return 1;     }     return 0;   }   int main() {     m = read();     scanf( %s , s);     n = strlen(s);     int i;     for (i = n - 1; i >= 0 && s[i] ==  z ; i--) s[i] =  a ;     if (i == -1) {       puts( Impossible );       return 0;     }     s[i]++;     p[0] = 1;     for (int i = 1; i < 500100; i++) p[i] = p[i - 1] * P;     if (!dfs(0, 1)) puts( Impossible );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXS = 200005;   const int MAXN = 100005;   const int B = 75;   template <typename T>   inline void read(T &AKNOI) {     T x = 0, flag = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) flag = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     AKNOI = flag * x;   }   template <typename T>   inline void cmin(T &x, T y) {     if (x > y) x = y;   }   template <typename T>   inline void cmax(T &x, T y) {     if (x < y) x = y;   }   char ss[MAXS], tt[MAXS];   int lg2[MAXS];   struct SuffixArray {     int sa[MAXS], x[MAXS], y[MAXS], cnt[MAXS];     int rnk[MAXS], hei[MAXS], st[MAXS][20];     inline bool cmp(int i, int j, int k) {       return (y[i] == y[j] && y[i + k] == y[j + k]);     }     void build(int n) {       int m = 256;       for (int i = 1; i <= n; ++i) {         ++cnt[x[i] = ss[i]];       }       for (int i = 1; i <= m; ++i) {         cnt[i] += cnt[i - 1];       }       for (int i = n; i >= 1; --i) {         sa[cnt[x[i]]--] = i;       }       for (int k = 1; k <= n; k <<= 1) {         int p = 0;         for (int i = n - k + 1; i <= n; ++i) {           y[++p] = i;         }         for (int i = 1; i <= n; ++i) {           if (sa[i] > k) {             y[++p] = sa[i] - k;           }         }         for (int i = 0; i <= m; ++i) {           cnt[i] = 0;         }         for (int i = 1; i <= n; ++i) {           ++cnt[x[y[i]]];         }         for (int i = 1; i <= m; ++i) {           cnt[i] += cnt[i - 1];         }         for (int i = n; i >= 1; --i) {           sa[cnt[x[y[i]]]--] = y[i];         }         swap(x, y);         x[sa[1]] = p = 1;         for (int i = 2; i <= n; ++i) {           x[sa[i]] = (cmp(sa[i], sa[i - 1], k) ? p : ++p);         }         if (p >= n) break;         m = p;       }       for (int i = 1; i <= n; ++i) {         rnk[sa[i]] = i;       }       for (int i = 1, j = 0; i <= n; ++i) {         if (j) --j;         while (ss[i + j] == ss[sa[rnk[i] - 1] + j]) ++j;         hei[rnk[i]] = j;       }       lg2[1] = 0;       for (int i = 2; i <= n; ++i) {         lg2[i] = lg2[i >> 1] + 1;       }       for (int i = n; i >= 1; --i) {         st[i][0] = hei[i];         for (int j = 1; i + (1 << j) - 1 <= n; ++j) {           st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);         }       }     }     inline int LCP(int x, int y) {       if (x == y) return MAXS;       int l = rnk[x], r = rnk[y];       if (l > r) swap(l, r);       int k = lg2[r - l];       return min(st[l + 1][k], st[r - (1 << k) + 1][k]);     }     inline int sgn(int x, int y, int len) {       if (LCP(x, y) >= len) return 0;       return (rnk[x] < rnk[y]) ? -1 : 1;     }   } SA;   int n, m, Q, sor[MAXN], rnk[MAXN], ans[MAXN];   inline bool cmp(int x, int y) {     static int t;     bool swp = (x > y);     if (swp) swap(x, y);     if (x + m <= y) {       t = SA.sgn(n + 2, x + 1, m);       if (t) return (t < 0) ^ swp;       t = SA.sgn(x + 1, x + m + 1, y - x - m);       if (t) return (t < 0) ^ swp;       t = SA.sgn(y - m + 1, n + 2, m);       if (t) return (t < 0) ^ swp;     } else {       t = SA.sgn(n + 2, x + 1, y - x);       if (t) return (t < 0) ^ swp;       t = SA.sgn(n + 2 + y - x, n + 2, x + m - y);       if (t) return (t < 0) ^ swp;       t = SA.sgn(x + 1, n + 2 + x + m - y, y - x);       if (t) return (t < 0) ^ swp;     }     return swp ^ 1;   }   struct Query {     int l, r, x, y, id;   } tmp;   vector<Query> q[80];   struct SparseTable {     int st[MAXN][20];     void build(int n) {       for (int i = n; i >= 0; --i) {         for (int j = 1; i + (1 << j) - 1 <= n; ++j) {           st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);         }       }     }     int query(int l, int r) {       int k = lg2[r - l + 1];       return min(st[l][k], st[r - (1 << k) + 1][k]);     }   } ST;   void Work1(Query p, int k) {     for (int x = p.x, y = p.y; x <= n; x += k, y += k) {       int l = max(p.l, x), r = min(p.r, y);       if (l > r) continue;       cmin(ans[p.id], ST.query(l, r));     }   }   void Work2(int k) {     for (int x = 0; x < k; ++x) {       int cnt = 0;       for (int i = x; i <= n; i += k) {         ST.st[cnt++][0] = rnk[i];       }       ST.build(cnt - 1);       for (auto p : q[k]) {         if (x < p.x || x > p.y) continue;         int l = p.l / k + (p.l % k > x);         int r = p.r / k - (p.r % k < x);         if (l > r) continue;         cmin(ans[p.id], ST.query(l, r));       }     }   }   void init() {     scanf( %s , ss + 1);     n = strlen(ss + 1);     scanf( %s , tt + 1);     m = strlen(tt + 1);     ss[n + 1] =  # ;     for (int i = 1; i <= m; ++i) {       ss[n + 1 + i] = tt[i];     }     SA.build(n + 1 + m);     for (int i = 0; i <= n; ++i) {       sor[i] = i;     }     sort(sor, sor + n + 1, cmp);     for (int i = 0; i <= n; ++i) {       rnk[sor[i]] = i;     }   }   void solve() {     for (int i = 0; i <= n; ++i) {       ST.st[i][0] = rnk[i];     }     ST.build(n);     read(Q);     memset(ans, 0x3f, sizeof(ans));     for (int i = 1, k; i <= Q; ++i) {       read(tmp.l);       read(tmp.r);       read(k);       read(tmp.x);       read(tmp.y);       tmp.id = i;       if (k > B) {         Work1(tmp, k);       } else {         q[k].push_back(tmp);       }     }     for (int i = 1; i <= B; ++i) {       Work2(i);     }     for (int i = 1; i <= Q; ++i) {       printf( %d  , (ans[i] <= n) ? sor[ans[i]] : -1);     }   }   int main() {     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int morh[100001], parent[100001], pos[100001], temp = 0, ans = 0;   int calculate(int i) {     int posi = i, ans = 0;     while (posi != 0) {       if (pos[posi] > 1)         break;       else {         posi = parent[posi];         ans++;       }     }     return ans;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &morh[i]);     }     for (int i = 1; i <= n; i++) {       scanf( %d , &parent[i]);       pos[parent[i]]++;     }     int temp2;     for (int i = 1; i <= n; i++) {       if (morh[i] == 1) {         temp2 = calculate(i);         if (temp2 > ans) {           ans = temp2;           temp = i;         }       }     }     temp2 = ans;     int give_ans[ans], i = 0;     for (int j = 0; j < temp2; j++) {       give_ans[i] = temp;       i++;       temp = parent[temp];     }     printf( %d n , ans);     for (int i = ans - 1; i >= 0; i--) printf( %d  , give_ans[i]);     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   struct three {     int f, s, w;   };   map<pair<int, int>, bool> mp;   int parent[200005], pat[200005];   three A[200005];   int time_in[200005], time_out[200005];   vector<vector<int> > dp(200005);   vector<vector<int> > dp_mx(200005);   vector<vector<pair<int, int> > > g;   vector<vector<pair<int, int> > > tree;   set<pair<int, pair<int, int> > > st;   set<pair<int, pair<int, int> > >::iterator st_it;   void make_set(int);   int find_set(int);   void union_sets(int, int);   bool subtree(int, int);   int lca(int, int);   void DFS(int, int, int);   int get_max(int, int);   int n, m;   int a, b;   int timer;   int lg;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     g.resize(n + 1);     tree.resize(n + 1);     for (int k = 1; k <= n; k++) make_set(k);     for (int k = 1; k <= m; k++) {       cin >> A[k].f >> A[k].s >> A[k].w;       g[A[k].f].push_back(make_pair(A[k].s, A[k].w));       g[A[k].s].push_back(make_pair(A[k].f, A[k].w));       st.insert(make_pair(A[k].w, make_pair(A[k].f, A[k].s)));     }     long long ans = 0;     while (!st.empty()) {       st_it = st.begin();       a = st_it->second.first;       b = st_it->second.second;       while (find_set(a) == find_set(b)) {         st.erase(st_it);         if (st.empty()) break;         st_it = st.begin();         a = st_it->second.first;         b = st_it->second.second;       }       if (st.empty()) break;       union_sets(a, b);       mp[{a, b}] = 1;       mp[{b, a}] = 1;       tree[a].push_back(make_pair(b, st_it->first));       tree[b].push_back(make_pair(a, st_it->first));       ans += st_it->first;       st.erase(st_it);     }     lg = log2(n + 1);     for (int k = 0; k <= n; k++) {       dp[k].resize(lg + 1);       dp_mx[k].resize(lg + 1);     }     DFS(1, 0, 0);     for (int k = 1; k <= m; k++) {       int pq = lca(A[k].f, A[k].s);       int q = max(get_max(A[k].f, pq), get_max(A[k].s, pq));       long long cnt = ans - q + A[k].w;       cout << cnt <<   n ;     }     return 0;   }   void make_set(int v) { parent[v] = v; }   int find_set(int v) {     if (v == parent[v]) return v;     return parent[v] = find_set(parent[v]);   }   void union_sets(int a, int b) {     a = find_set(a);     b = find_set(b);     if (a != b) parent[b] = a;   }   void DFS(int v, int p, int d) {     pat[v] = p;     dp[v][0] = p;     dp_mx[v][0] = d;     for (int k = 1; k <= lg; k++) {       dp[v][k] = dp[dp[v][k - 1]][k - 1];       if (dp[v][k] != 0)         dp_mx[v][k] = max(dp_mx[v][k - 1], dp_mx[dp[v][k - 1]][k - 1]);     }     time_in[v] = timer++;     for (int k = 0; k < tree[v].size(); k++) {       int to = tree[v][k].first;       if (to == p) continue;       DFS(to, v, tree[v][k].second);     }     time_out[v] = timer++;   }   bool subtree(int a, int b) {     if (time_in[a] <= time_in[b] && time_out[b] <= time_out[a]) return 1;     return 0;   }   int lca(int a, int b) {     if (subtree(a, b)) return a;     if (subtree(b, a)) return b;     for (int k = lg; k >= 0; k--) {       if (dp[a][k] == 0) continue;       if (subtree(dp[a][k], b)) continue;       a = dp[a][k];     }     return pat[a];   }   int get_max(int a, int lca) {     int counter = -1;     for (int k = lg; k >= 0; k--) {       if (dp[a][k] == 0) continue;       if (subtree(dp[a][k], lca) && dp[a][k] != lca) continue;       counter = max(counter, dp_mx[a][k]);       a = dp[a][k];     }     return counter;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int maxn = 2e5 + 50;   const long long inf = 1e17;   int t;   void get() {     fflush(stdout);     scanf( %d , &t);     for (int i = 1; i <= t; i++) scanf( %*s );   }   int main() {     do {       puts( next 0 1 ), get();       puts( next 0 ), get();     } while (t == 3);     do {       puts( next 0 1 2 3 4 5 6 7 8 9 ), get();     } while (t == 2);     puts( done );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 2e5 + 5;   long long ans[max_n];   struct no {     long long p, val;   };   vector<no> v;   bool operator<(const no &x, const no &y) { return x.p < y.p; }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       long long x, y;       scanf( %lld%lld , &x, &y);       v.push_back(no{x, 1});       v.push_back(no{y + 1, -1});     }     sort(v.begin(), v.end());     long long add = v[0].val, now = v[0].p;     for (int i = 1; i < v.size();) {       for (int j = i; j < v.size(); j++) {         if (v[j].p == now) {           add = add + v[j].val;           i++;         } else {           ans[add] += v[j].p - now;           add += v[j].val;           now = v[j].p;           i = j + 1;           break;         }       }     }     for (int i = 1; i <= n; i++) printf( %lld  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T& x) {     x = 0;     char ch = getchar();     long long f = 1;     while (!isdigit(ch)) {       if (ch ==  - ) f *= -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch - 48;       ch = getchar();     }     x *= f;   }   template <typename T, typename... Args>   void read(T& first, Args&... args) {     read(first);     read(args...);   }   template <typename T>   void write(T arg) {     T x = arg;     if (x < 0) {       putchar( - );       x = -x;     }     if (x > 9) {       write(x / 10);     }     putchar(x % 10 +  0 );   }   template <typename T, typename... Ts>   void write(T arg, Ts... args) {     write(arg);     if (sizeof...(args) != 0) {       putchar(   );       write(args...);     }   }   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   const int N = 1e5 + 5;   int n, m;   int a[N], b[N], fa[N << 1];   int cnt = 0;   long long res = 0;   struct node {     int u, v, w;     bool operator<(const node& no) const { return w > no.w; }   } edge[N << 1];   int findroot(int x) {     if (fa[x] == x) return x;     return fa[x] = findroot(fa[x]);   }   bool merge(int x, int y) {     int fax = findroot(x);     int fay = findroot(y);     if (fax != fay) {       fa[fax] = fay;       return true;     }     return false;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> m >> n;     int tot = n;     for (int i = 1; i <= m; i++) cin >> a[i];     for (int i = 1; i <= n; i++) cin >> b[i];     for (int i = 1; i <= m; i++) {       ++tot;       int s;       cin >> s;       for (int j = 1; j <= s; j++) {         int v;         cin >> v;         edge[++cnt].u = tot, edge[cnt].v = v, edge[cnt].w = a[i] + b[v];         res += edge[cnt].w;       }     }     sort(edge + 1, edge + 1 + cnt);     for (int i = 1; i <= tot + 5; i++) fa[i] = i;     for (int i = 1; i <= cnt; i++) {       int u = edge[i].u, v = edge[i].v, w = edge[i].w;       if (merge(u, v)) res -= w;     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long a[100005];   long long c[100005];   int main() {     int n, k;     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       a[i] -= k;     }     long long A;     scanf( %lld , &A);     priority_queue<long long, vector<long long>, greater<long long> > P;     long long cnt = 0;     long long ans = 0;     long long now = 0;     for (int i = 1; i <= n; i++) {       scanf( %lld , &c[i]);     }     for (int i = 1; i <= n; i++) {       P.push(c[i]);       if (a[i] > cnt) {         long long t;         if (a[i] < now)           t = 0;         else           t = ((a[i] - now) % A == 0 ? (a[i] - now) / A : (a[i] - now) / A + 1);         for (int j = 1; j <= t; j++) {           if (P.empty()) {             ans = -1;             break;           }           ans += P.top();           now += A;           P.pop();         }         cnt = a[i];         if (ans == -1) break;       }     }     printf( %lld , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t, n, k;     cin >> t;     while (t--) {       cin >> n >> k;       long long int a[n];       set<long long int> s;       for (long long int i = 0; i < n; i++) {         cin >> a[i];         s.insert(a[i]);       }       sort(a, a + n);       if (k == 1 && s.size() == 1) {         cout <<  1 n ;         continue;       }       if (k == 1 && s.size() != 1) {         cout <<  -1 n ;         continue;       }       long long int c = 0;       for (long long int i = 0; i < n - 1; i++) {         if (a[i + 1] - a[i] != 0) {           c++;         }       }       long long int o = 1;       cout << max(o, ((c + k - 2) / (k - 1))) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> ways[400], a[400], num[400];   vector<int> ans;   struct edge {     int a, b;   };   vector<edge> gr;   bool dfs(int v, int f, int pr) {     if (v == f) return true;     for (int i = 0; i < a[v].size(); i++) {       if (a[v][i] != pr)         if (dfs(a[v][i], f, v)) return true;     }     return false;   }   void findway(int v, int f, int j) {     if (v == f) {       ways[j].push_back(v);       return;     }     for (int i = 0; i < a[v].size(); i++) {       if (dfs(a[v][i], f, v)) {         ways[j].push_back(v);         findway(a[v][i], f, j);         return;       }     }   }   int main() {     cin >> n;     for (int i = 0; i < n - 1; i++) {       int z, l;       cin >> z >> l;       a[l].push_back(z);       a[z].push_back(l);       num[l].push_back(i + 1);       num[z].push_back(i + 1);     }     int num = 0;     int k;     while (cin >> k) {       findway(1, k, num);       num++;     }     for (int i = 0; i < ways[0].size(); i++) {       ans.push_back(ways[0][i]);     }     for (int i = 1; i < num; i++) {       int j = 0;       while (ways[i - 1][j] == ways[i][j]) j++;       j--;       for (int z = ways[i - 1].size() - 2; z >= j; z--) {         ans.push_back(ways[i - 1][z]);       }       for (int z = j + 1; z < ways[i].size(); z++) {         ans.push_back(ways[i][z]);       }     }     for (int i = ways[num - 1].size() - 2; i >= 0; i--) {       ans.push_back(ways[num - 1][i]);     }     if (ans.size() != 2 * n - 1)       cout << -1;     else {       for (int i = 0; i < ans.size(); i++) {         cout << ans[i] <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)4e18;   int n, q;   long long c[110000];   long long v[110000];   long long d[110000];   int u[110000];   int main() {     ios::sync_with_stdio(0);     cin >> n >> q;     for (int i = 0; i < n; ++i) cin >> v[i];     for (int i = 0; i < n; ++i) cin >> c[i];     while (q--) {       long long a, b;       cin >> a >> b;       for (int i = 0; i <= n; ++i) d[i] = -inf;       long long et = 0;       pair<long long, int> m1 = make_pair(-inf, -1);       pair<long long, int> m2 = make_pair(-inf, -1);       for (int i = 0; i < n; ++i) {         long long ans = max(v[i] * b, d[c[i]]);         ans = max(ans, d[c[i]] + v[i] * a);         if (m1.second != c[i])           ans = max(ans, m1.first + v[i] * b);         else           ans = max(ans, m2.first + v[i] * b);         if (ans > m1.first)           if (m1.second != c[i])             m2 = m1, m1 = make_pair(ans, c[i]);           else             m1.first = ans;         else if (ans > m2.first && c[i] != m1.second)           m2 = make_pair(ans, c[i]);         d[c[i]] = max(d[c[i]], ans);         et = max(et, ans);       }       cout << et <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   const int inf = 0x3c3c3c3c;   const long long infl = 0x3c3c3c3c3c3c3c3c;   long long dp[5003][5003];   int A, B, C;   int main() {     ios::sync_with_stdio(0), cin.tie(0);     ;     cin >> A >> B >> C;     vector<int> v({A, B, C});     sort((v).begin(), (v).end());     for (int i = 0; i <= 5000; i++) dp[i][0] = 1;     for (int i = 1; i <= 5000; i++) {       for (int j = 1; j <= 5000; j++) {         dp[i][j] = (dp[i - 1][j - 1] * i + dp[i][j - 1]) % mod;       }     }     long long ans = 1;     ans = (ans * dp[v[2]][v[1]]) % mod;     ans = (ans * dp[v[2]][v[0]]) % mod;     ans = (ans * dp[v[1]][v[0]]) % mod;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     scanf( %d , &n);     int block = (int)sqrt(n), total = 0;     for (int t = 1;; ++t) {       for (int j = 1, k = max(1, n - t * block + 1); j <= block; ++j, ++k) {         printf( %d  , k);         if (++total == n) {           return 0;         }       }     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long long a, b, c, d, e, f, n, ans, cn1, cn2, cn3, t;   string s, s1, s2;   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> a;     cin >> b;     cin >> c;     cin >> d;     cin >> e;     cin >> f;     ans = 0;     if (e >= f) {       ans = min(a, d);       d = d - ans;       ans = ans * e;       ans += min(b, min(c, d)) * f;       cout << ans;     } else if (e < f) {       ans = 0;       ans = min(b, min(c, d));       d = d - ans;       ans = ans * f;       ans += min(a, d) * e;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a, b;     cin >> n >> m;     if (m == n) {       vector<bool> rev(n, false);       vector<vector<int> > mat(n + 1);       for (int i = 0; i < m; i++) {         cin >> a >> b;         mat[a].push_back(b);         mat[b].push_back(a);       }       int cont = 0;       for (int i = 1; i <= m; i++) {         if (!rev[i]) {           cont++;           queue<int> COLA;           COLA.push(i);           rev[i] = true;           while (!COLA.empty()) {             int j = COLA.front();             COLA.pop();             for (int k = 0; k < mat[j].size(); k++) {               if (!rev[mat[j][k]]) {                 rev[mat[j][k]] = 1;                 COLA.push(mat[j][k]);               }             }           }         }       }       if (cont == 1)         cout <<  FHTAGN! n ;       else         cout <<  NO n ;     } else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,no-stack-protector )   #pragma GCC target( avx )   int V[100000];   float x;   int main() {     int n, m, l, r, res, type;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &V[i]);     while (m--) {       scanf( %d%d%d%f , &type, &l, &r, &x);       if (type == 1)         for (int i = l; i <= r; ++i) V[i] -= V[i] > x ? x : 0;       else {         res = 0;         for (int i = l; i <= r; ++i) V[i] == x ? ++res : 0;         printf( %d n , res);       }     }   }


#include <bits/stdc++.h>   using namespace std;   #define TC 1   #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);   #define deb(...)   [  << #__VA_ARGS__  :   << (__VA_ARGS__) <<  ]     using ll = long long;      template <typename T>   vector<T> readArr(int n) {       vector <T> v(n);       for (auto &it : v)           cin >> it;       return v;   }      void init() {       ;   }      const int N = 1e5+ 5;      void solve() {       int n, m; cin >> n >> m;              vector <int> v = readArr<int>(n);              for (int &x: v)           x = x % m;              vector <int> ct(N);       for (int x: v)           ct[x]++;                  int res = (ct[0] > 0 ? 1 : 0);     //  cout << deb(res) <<   n ;       for (int i=1; i<=m/2; i++) {           int j = m - i;           if (i == j) {               res += (ct[j] > 0 ? 1 : 0);              // cout << deb(i) deb(res) <<   n ;               continue;           }           int a = ct[i], b = ct[j];           if (a > 0 && b > 0) {               res++;               if (a <= b) {                   ct[i] = 0;                   ct[j] -= (a+1);                   ct[j] = max(ct[j], 0);               } else {                   ct[j] = 0;                   ct[i] -= (b+1);                   ct[i] = max(ct[i], 0);               }           }                                 res += ct[i];           res += ct[j];                     // cout << deb(i) deb(j) deb(res) <<   n ;       }       cout << res <<   n ;   }      int main() {       IOS       init();       int t = 1;       if (TC)           cin >> t;          while (t--) {           solve();       }   }


#include <bits/stdc++.h>   template <class T>   T gcd(T a, T b) {     T r;     while (b != 0) {       r = a % b;       a = b;       b = r;     }     return a;   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   using namespace std;   const long long MOD = 1e9 + 7;   vector<int> p, q;   long long res[200005];   void act() {     long long a, b, c, d, cnt = 0;     cin >> a >> b >> c >> d;     for (int i = 1; i <= c; i++) p.push_back(2);     for (int i = 1; i <= a; i++) p.push_back(0);     for (int i = 1; i <= d; i++) q.push_back(3);     for (int i = 1; i <= b; i++) q.push_back(1);     if (p.size() <= q.size()) swap(p, q);     for (int i = 0; i < p.size(); i++) {       res[++cnt] = p[i];       if (i < q.size()) res[++cnt] = q[i];     }     for (int i = 1; i < cnt; i++)       if (abs(res[i] - res[i + 1]) != 1) {         cout <<  NO               <<   n ;         return;       }     cout <<  YES           <<   n ;     for (int i = 1; i <= cnt; i++) cout << res[i] <<    ;   }   int main() {     int t = 1;     while (t--) act();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n;   int a[N], b[N], c[N];   map<pair<int, int>, pair<int, int> > mtp;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d %d %d , &a[i], &b[i], &c[i]);     }     double ans = 0;     int idx = -1;     for (int i = 1; i <= n; i++) {       double curr = min(min(a[i], b[i]), c[i]);       curr /= 2.0;       if (curr > ans) {         ans = curr;         idx = i;       }     }     int idx1 = -1, idx2 = -1;     for (int i = 1; i <= n; ++i) {       int aa = a[i];       int bb = b[i];       int cc = c[i];       if (mtp.find({min(aa, bb), max(aa, bb)}) != mtp.end()) {         int key = mtp[{min(aa, bb), max(aa, bb)}].first;         int backtrack = mtp[{min(aa, bb), max(aa, bb)}].second;         double curr = min(min(aa, bb), key + cc);         curr /= 2.0;         if (curr > ans) {           ans = curr;           idx1 = i;           idx2 = backtrack;         }       }       if (mtp.find({min(bb, cc), max(bb, cc)}) != mtp.end()) {         int key = mtp[{min(bb, cc), max(bb, cc)}].first;         int backtrack = mtp[{min(bb, cc), max(bb, cc)}].second;         double curr = min(min(bb, cc), key + aa);         curr /= 2.0;         if (curr > ans) {           ans = curr;           idx1 = i;           idx2 = backtrack;         }       }       if (mtp.find({min(aa, cc), max(aa, cc)}) != mtp.end()) {         int key = mtp[{min(aa, cc), max(aa, cc)}].first;         int backtrack = mtp[{min(aa, cc), max(aa, cc)}].second;         double curr = min(min(aa, cc), key + bb);         curr /= 2.0;         if (curr > ans) {           ans = curr;           idx1 = i;           idx2 = backtrack;         }       }       int x = min(aa, bb);       int y = max(aa, bb);       if (mtp.find({x, y}) == mtp.end()) {         mtp[{x, y}] = {cc, i};       } else {         if (cc > mtp[{x, y}].first) {           mtp[{x, y}] = {cc, i};         }       }       x = min(cc, bb);       y = max(cc, bb);       if (mtp.find({x, y}) == mtp.end()) {         mtp[{x, y}] = {aa, i};       } else {         if (aa > mtp[{x, y}].first) {           mtp[{x, y}] = {aa, i};         }       }       x = min(aa, cc);       y = max(aa, cc);       if (mtp.find({x, y}) == mtp.end()) {         mtp[{x, y}] = {bb, i};       } else {         if (bb > mtp[{x, y}].first) {           mtp[{x, y}] = {bb, i};         }       }     }     if (idx2 == -1) {       printf( 1 n%d n , idx);     } else {       printf( 2 n%d %d n , idx1, idx2);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, a, b, x1, y1, x2, y2, d1, d2, r1 = 10000000000000000,                                                r2 = 10000000000000000, ans;     ans = r2;     r1 *= -1;     r2 = r1;     vector<pair<long long, pair<long long, long long> > > v1, v2;     cin >> n >> x1 >> y1 >> x2 >> y2;     for (int i = 0; i < n; i++) {       cin >> a >> b;       d1 = (y1 - b) * (y1 - b) + (x1 - a) * (x1 - a);       d2 = (y2 - b) * (y2 - b) + (x2 - a) * (x2 - a);       v1.push_back(make_pair(d1, make_pair(a, b)));       v2.push_back(make_pair(d2, make_pair(a, b)));     }     v1.push_back(make_pair(0, make_pair(x1, y1)));     sort(v1.begin(), v1.end());     for (int i = 0; i <= n; i++) {       long long rad1 = v1[i].first, rad2 = 0, itr = 1;       while (rad1 == v1[i + itr].first) itr++;       for (int j = i + itr; j <= n; j++) {         a = v1[j].second.first;         b = v1[j].second.second;         d2 = (y2 - b) * (y2 - b) + (x2 - a) * (x2 - a);         rad2 = max(rad2, d2);       }       ans = min(rad1 + rad2, ans);     }     cout << ans << endl;     return 0;   }


#include<bits/stdc++.h>   #define ll long long   using namespace std;      int k;   int dfs(int v, int p, vector<vector<int>> &viz){    int t=-1;    vector < int > pos;    for(auto u: viz[v]){     if(u==p) continue;     int w=dfs(u,v,viz);     pos.push_back(w);     if(t==-1) t=w+1;     t = min(t,w+1);    }    sort(pos.begin(), pos.end());    int n = pos.size();    if(v==1){     if(n>=1) k = max(k, pos[n-1]+1);     if(n>=2) k = max(k, pos[n-2]+2);    }    else{     if(n>=2) k = max(k, pos[n-1]+2);    }    if(t==-1) t=0;    return t;   }      int main(){    ios::sync_with_stdio(false);      cin.tie(0);      int t;      cin >> t;      while(t--){       int n;       cin >> n;       k=0;       vector < vector <int> > viz(n+1);       for(int i=0; i<n-1; i++){        int a, b;        cin >> a >> b;        viz[a].push_back(b);        viz[b].push_back(a);     }     dfs(1,0,viz);     cout << k << endl;    }   }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const long double pi = acos(-1.0);   const long long inf = 1e+9;   #pragma comment(linker,  /STACK:36777216 )   template <typename T>   istream &operator>>(istream &, vector<T> &);   template <typename T>   ostream &operator<<(ostream &, const vector<T> &);   long long s, x;   int bit(long long x, int pos) { return (x & (1LL << pos)) > 0; }   void solve() {     cin >> s >> x;     int l = 45;     while (bit(s, l) == 0) l--;     vector<vector<long long> > dp(l + 2, vector<long long>(2, 0));     dp[0][0] = 1;     for (int i = 1; i <= l + 1; i++) {       int bs = bit(s, i - 1);       int bx = bit(x, i - 1);       if (bs == 0) {         if (bx == 0) {           dp[i][0] = dp[i - 1][0];           dp[i][1] = dp[i - 1][0];         } else {           dp[i][1] += 2 * dp[i - 1][1];         }       } else {         if (bx == 0) {           dp[i][0] = dp[i - 1][1];           dp[i][1] = dp[i - 1][1];         } else {           dp[i][0] += 2 * dp[i - 1][0];         }       }     }     long long ans = dp[l + 1][0];     if (x > s) ans = 0;     if (x == 0) {       if (s % 2 == 0)         ans = 1;       else         ans = 0;     }     if (x == s) ans -= 2;     cout << ans << endl;   }   int main() {     solve();     return 0;   }   template <typename T>   istream &operator>>(istream &is, vector<T> &v) {     for (int i = 0; i < v.size(); ++i) is >> v[i];     return is;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     for (int i = 0; i < v.size(); ++i) os << v[i];     return os;   }


#include <bits/stdc++.h>   using namespace std;   long long mc[1 << 20];   list<int> th;   struct f {     long long x, k, m, ms[20];   } fs[100];   bool kcomp(f a, f b) { return a.k < b.k; }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n, m, b;     cin >> n >> m >> b;     for (int i = 0; i < n; i++) {       cin >> fs[i].x >> fs[i].k >> fs[i].m;       for (int j = 0; j < fs[i].m; j++) {         cin >> fs[i].ms[j];         fs[i].ms[j]--;       }     }     sort(fs, fs + n, kcomp);     long long bst = -1;     fill_n(mc, 1 << 20, -1);     for (int i = 0; i < n; i++) {       int tbt = 0;       for (int mi = 0; mi < fs[i].m; mi++) {         tbt |= 1 << fs[i].ms[mi];       }       int old = mc[tbt];       mc[tbt] = mc[tbt] == -1 ? fs[i].x : min(mc[tbt], fs[i].x);       if (old == mc[tbt]) continue;       th.push_front(tbt);       auto it = th.begin();       auto pe = th.end();       {         auto it2 = it;         ++it2;         for (; it2 != pe; ++it2) {           int bt = *it | *it2;           if (mc[bt] == -1) th.push_back(bt);           mc[bt] =               mc[bt] == -1 ? mc[*it] + mc[*it2] : min(mc[bt], mc[*it] + mc[*it2]);         }       }       for (int cpos = 0; it != th.end(); ++it, ++cpos) {         auto it2 = pe;         auto ce = th.end();         for (; it2 != ce; ++it2) {           int bt = *it | *it2;           if (mc[bt] == -1) th.push_back(bt);           mc[bt] =               mc[bt] == -1 ? mc[*it] + mc[*it2] : min(mc[bt], mc[*it] + mc[*it2]);         }       }       if (mc[(1 << m) - 1] != -1) {         bst = bst == -1 ? mc[(1 << m) - 1] + b * fs[i].k                         : min(bst, mc[(1 << m) - 1] + b * fs[i].k);       }     }     cout << bst <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   char s[10];   int v[10][10];   int dx[4] = {-2, -2, 2, 2}, dy[4] = {2, -2, 2, -2};   int sx, sy, ex, ey;   int mark[10][10];   int ans;   void dfs(int curx, int cury, int curnum) {     for (int i = 0; i < 4; i++) {       int tempx = curx + dx[i];       int tempy = cury + dy[i];       if (tempx >= 1 && tempx <= 8 && tempy >= 1 && tempy <= 8 &&           !mark[tempx][tempy]) {         mark[tempx][tempy] = curnum + 1;         dfs(tempx, tempy, curnum + 1);       }     }   }   int const nMax = 1;   int main() {     int t;     scanf( %d , &t);     while (t--) {       sx = -1;       memset(mark, 0, sizeof(mark));       memset(v, 0, sizeof(v));       getchar();       for (int i = 1; i <= 8; i++) {         gets(s);         for (int j = 0; j < 8; j++) {           if (s[j] ==  # )             v[i][j + 1] = -1;           else if (s[j] ==  . ) {             v[i][j + 1] = 0;           } else {             v[i][j + 1] = 0;             if (sx != -1) {               ex = i;               ey = j + 1;             } else {               sx = i;               sy = j + 1;             }           }         }       }       if (abs(sx - ex) % 2 != 0 || abs(sy - ey) % 2 != 0) {         puts( NO );         continue;       }       dfs(sx, sy, 0);       if (mark[ex][ey] % 2 == 0 && mark[ex][ey] != 0)         puts( YES );       else         puts( NO );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int report[105], folder[105];   int main() {     int n;     cin >> n;     int k = 1, am = 0;     for (int i = 1; i <= n; i++) cin >> report[i];     for (int i = 1; i <= n; i++) {       if (report[i] < 0 && am == 2) {         k++;         folder[k]++;         am = 1;       } else {         folder[k]++;         if (report[i] < 0) am++;       }     }     cout << k << endl;     for (int i = 1; i <= k; i++) cout << folder[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   string a;   vector<pair<int, int> > v;   void input() {     cin >> a;     n = a.size();   }   void solve() {     int i;     for (i = 0; i < n; i++) {       if (a[i] !=  a ) {         v.push_back(make_pair((a[i] -  a ), i));       }     }     int sz = v.size();     if (sz == 0) {       cout << a <<   n ;       return;     }     if ((sz % 2) != 0) {       cout <<  :( n ;       return;     }     int half = (sz / 2);     for (i = 0; i < half; i++) {       if (v[i].first != v[i + half].first) {         cout <<  :( n ;         return;       }     }     int st_id = v[half].second;     for (i = st_id; i < n; i++) {       if (a[i] ==  a ) {         cout <<  :( n ;         return;       }     }     for (i = 0; i < st_id; i++) {       cout << a[i];     }     cout <<   n ;   }   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     input();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> vec[10];   vector<vector<int> > vvv(183);   queue<int> qu;   map<string, int> mp;   map<string, int>::iterator iter;   bool ch[10] = {false};   int main() {     string s1, s2;     cin >> s1 >> s2;     for (int i = 0; i < s1.length(); i++) {       if (s1[i] >= s2[i])         s1[i] = s2[i];       else {         cout << -1 << endl;         return 0;       }     }     cout << s1 << endl;     return 0;   }


#include <bits/stdc++.h>   int freq[1005] = {0};   int find_int(int a, int b, int c, int d) {     int i;     if (b < c) return 0;     if (a > d) return 0;     if (a <= c && b >= d) {       for (i = c; i <= d; i++) freq[i] = 1;       return d - c + 1;     } else if (a >= c && b <= d) {       for (i = a; i <= b; i++) freq[i] = 1;       return b - a + 1;     } else if (a >= c && b >= d) {       for (i = a; i <= d; i++) freq[i] = 1;       return d - a + 1;     } else if (a <= c && b <= d) {       for (i = c; i <= b; i++) freq[i] = 1;       return b - c + 1;     }   }   int main() {     int i, j, k, l, r, p, q, a[55], b[55], c[55], d[55];     scanf( %d %d %d %d , &p, &q, &l, &r);     for (i = 0; i < p; i++) scanf( %d %d , &a[i], &b[i]);     for (i = 0; i < q; i++) scanf( %d %d , &c[i], &d[i]);     int count = 0;     for (i = 0; i < q; i++) {       for (j = 0; j < p; j++) {         int l1, r1;         if (c[i] >= a[j] && c[i] <= b[j]) l1 = 0;         r1 = b[j] - c[i];         if (c[i] < a[j]) {           l1 = a[j] - d[i];           r1 = b[j] - c[i];         }         count = count + find_int(l1, r1, l, r);       }     }     count = 0;     for (i = l; i <= r; i++) {       if (freq[i] == 1) count++;     }     printf( %d n , count);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int GLL(long long& x) { return scanf( %lld , &x); }   int GI(int& x) { return scanf( %d , &x); }   int m, n;   long long add(long long x, long long y) {     x += y;     if (x >= m) x -= m;     return x;   }   long long sub(long long x, long long y) {     x -= y;     if (x < 0) x += m;     return x;   }   long long mul(long long x, long long y) { return (x * y) % m; }   long long powmod(long long a, long long b) {     long long res = 1;     a %= m;     for (; b; b >>= 1) {       if (b & 1) res = res * a % m;       a = a * a % m;     }     return res;   }   bool euler_criterion(long long a, long long p) {     return powmod(a, (p - 1) / 2) == 1;   }   long long tonelli_shanks(long long a, long long p) {     long long q = p - 1;     long long s = 0;     while (q % 2 == 0) {       s++;       q /= 2;     }     long long z = rand() % p;     while (euler_criterion(z, p)) {       z = rand() % p;     }     long long c = powmod(z, q);     long long r = powmod(a, (q + 1) / 2);     long long t = powmod(a, q);     long long m = s;     while (t != 1) {       long long lo = 1, hi = m - 1;       long long i = -1;       while (lo <= hi) {         long long mid = (lo + hi) / 2;         if (powmod(t, 1LL << mid) == 1) {           i = mid;           hi = mid - 1;         } else {           lo = mid + 1;         }       }       long long b = powmod(c, 1LL << (m - i - 1));       r = mul(b, r);       t = mul(t, mul(b, b));       c = mul(b, b);       m = i;     }     return r;   }   const int MAXN = 100005;   long long a[MAXN], d[MAXN], sqrd[MAXN], sumd[MAXN], sumsqrd[MAXN];   long long sumsqr[MAXN];   unordered_set<long long> sa, stest;   int main() {     GI(m);     GI(n);     for (int i = 1; i <= (int)(n); i++) {       GLL(a[i]);       sa.insert(a[i]);     }     if (n == 0) {       printf( %lld 0 n , a[0]);       return 0;     }     memset(d, 0, sizeof d);     memset(sqrd, 0, sizeof sqrd);     memset(sumd, 0, sizeof sumd);     memset(sumsqrd, 0, sizeof sumsqrd);     sort(a + 1, a + n + 1);     long long inv2 = powmod(2, m - 2);     for (int i = 2; i <= n; i++) {       d[i] = add(d[i - 1], mul(a[i] - a[i - 1], i - 1));       sqrd[i] = add(add(sqrd[i - 1], mul(2, mul(a[i] - a[i - 1], d[i - 1]))),                     mul(mul(a[i] - a[i - 1], a[i] - a[i - 1]), i - 1));       sumd[i] = add(sumd[i - 1], d[i]);       sumsqrd[i] = add(sumsqrd[i - 1], sqrd[i]);     }     memset(sumsqr, 0, sizeof sumsqr);     for (int i = 1; i < MAXN; i++) sumsqr[i] = add(sumsqr[i - 1], mul(i, i));     long long c = 0;     for (int i = 1; i <= (int)(n - 1); i++) c = add(c, sumsqr[i]);     long long test2 = mul(sumsqrd[n], powmod(c, m - 2));     if (test2 == 0 || euler_criterion(test2, m)) {       long long test = test2 == 0 ? 1 : tonelli_shanks(test2, m);       int fwd = 0;       long long curr = add(a[1], test);       while (curr != a[1] && sa.find(curr) != sa.end()) {         fwd++;         curr = add(curr, test);       }       int bwd = 0;       curr = sub(a[1], test);       while (curr != a[1] && sa.find(curr) != sa.end()) {         bwd++;         curr = sub(curr, test);       }       if (fwd + bwd + 1 >= n) {         curr = add(curr, test);         printf( %lld %lld n , curr, test);       } else {         printf( -1 n );       }     } else {       printf( -1 n );       return 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void Boost() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   const long double eps = 1e-10;   int main() {     Boost();     int n;     cin >> n;     vector<long double> A(n), B(n);     for (auto &x : A) cin >> x;     for (auto &x : B) cin >> x;     long double lo = 0;     long double hi = 1000000000;     long double best = 0;     while (hi - lo > eps) {       long double mid = (lo + hi) / 2.0;       pair<long double, long double> inter = {A[0] - mid * B[0],                                               A[0] + mid * B[0]};       for (int i = 1; i < n; ++i) {         pair<long double, long double> temp = {A[i] - mid * B[i],                                                A[i] + mid * B[i]};         inter.first = max(inter.first, temp.first);         inter.second = min(inter.second, temp.second);       }       if (inter.first <= inter.second) {         best = mid;         hi = mid;       } else {         lo = mid;       }     }     cout << fixed << setprecision(10) << best;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x;     long long score, sum = 0;     cin >> n;     vector<int> ara(n);     for (int i = 0; i < n; i++) {       cin >> ara[i];       sum += ara[i];     }     sort(ara.begin(), ara.end());     reverse(ara.begin(), ara.end());     score = sum;     for (int i = n - 1; i >= 0; i--) {       sum -= ara[i];       score += (ara[i] + sum);     }     score -= ara[0];     cout << score << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x;     cin >> x;     x++;     cout << x / 36 <<     << (((x % 36)) / 3);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n = 0, m = 0, mn = 0, mx = 0, l = 1, r = 0, k = 0, j = 1, i = 0,               ans = 0;     cin >> n;     vector<int> a(n);     for (auto &x : a) cin >> x;     bool got = true;     while (got) {       while (i < l) {         if (a[i] > l) {           l = a[i];         }         i++;       }       l = i + 1;       m++;       if (l == n + 1) {         break;       }     }     cout << m << endl;   }


#include <bits/stdc++.h>   using namespace std;   set<int> st;   set<int> en;   vector<int> out;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int t;       cin >> t;       if (st.find(t) == st.end())         st.insert(t);       else if (en.find(t) == en.end())         en.insert(t);     }     int ma = 0;     for (set<int>::iterator it = st.begin(); it != st.end(); it++) {       out.push_back(*it);       ma = max(ma, (*it));     }     for (set<int>::reverse_iterator it = en.rbegin(); it != en.rend(); it++)       if ((*it) != ma) out.push_back(*it);     cout << out.size() << endl;     for (int i = 0; i < out.size(); i++) cout << out[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int const MAXN = 2e5 + 10;   int n, m, T;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m;     int t = 0;     while (m) {       t = 10 * t + m % 10;       m /= 10;     }     cout << n + t;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   const int32_t M = 1e9 + 7;   const int32_t MM = 998244353;   void solve() {     long long x;     cin >> x;     long long n = 2;     long long tot = (n * (n - 1)) / 2;     long long ans = 0;     while (tot <= x) {       ans++;       n *= 2;       tot += (n * (n - 1)) / 2;     }     cout << ans << endl;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000010, P = 2000010;   int f[P];   int n, t, a[N];   int d[N], g[N], r[P];   vector<int> q[P], v[N];   void init(int c, int p) {     vector<int> z;     int cv = a[c];     d[c] = d[p] + 1;     g[c] = -1;     if (c == 1) fill(r + 1, r + P, -1);     for (int i = 0; i < q[cv].size(); i++) {       int t = q[cv][i];       if (g[c] == -1 || d[r[t]] > d[g[c]]) g[c] = r[t];       z.push_back(r[t]);       r[t] = c;     }     for (int i = 0; i < v[c].size(); i++)       if (v[c][i] != p) init(v[c][i], c);     for (int i = 0; i < q[cv].size(); i++) r[q[cv][i]] = z[i];   }   int main() {     scanf( %d %d , &n, &t);     for (int i = 2; i < P; i++)       if (!f[i])         for (int j = 1; i * j < P; j++) f[i * j] = i;     for (int i = 2; i < P; i++) {       int c = i / f[i];       if (f[c] != f[i]) q[i].push_back(f[i]);       for (int j = 0; j < q[c].size(); j++) q[i].push_back(q[c][j]);     }     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d %d , &x, &y);       v[x].push_back(y);       v[y].push_back(x);     }     init(1, 0);     while (t--) {       int k;       scanf( %d , &k);       if (k == 1) {         int c;         scanf( %d , &c);         printf( %d n , g[c]);       } else {         int x, y;         scanf( %d %d , &x, &y);         a[x] = y;         init(1, 0);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MXN = 2e5 + 10;   const long long MXS = MXN * 4;   const long long INF = 1e18;   struct Typ {     long long Sum, ValL, ValR, dpL, dpR, Maxdp, sz, Mindis, Val2L, Val2R;     pair<long long, long long> Valley;     void init() {       Mindis = INF;       Val2L = Val2R = Valley.first = Valley.second = 0;       Sum = dpL = dpR = Maxdp = ValL = ValR = sz = 0;     }   };   long long n, q, k;   long long A[MXN], Lazy[MXS];   bool Ok;   Typ seg[MXS], M;   Typ Merge(Typ a, Typ b) {     Typ now;     now.Mindis = INF;     now.Sum = a.Sum + b.Sum + abs(a.ValR - b.ValL);     now.ValL = a.ValL, now.ValR = b.ValR;     now.dpL = (a.sz > 1 ? a.dpL : (a.ValR >= b.ValL));     now.dpR = (b.sz > 1 ? b.dpR : (b.ValL >= a.ValR));     long long x = a.dpR + (a.ValR >= b.ValL);     long long y = b.dpL + (b.ValL >= a.ValR);     now.Maxdp = max(a.Maxdp, b.Maxdp);     if (a.sz > 1) now.Maxdp = max(now.Maxdp, x);     if (b.sz > 1) now.Maxdp = max(now.Maxdp, y);     if (a.sz > 1 && x == 1) {       now.Mindis =           min(now.Mindis, (b.ValL > a.ValR ? b.ValL - a.ValR : a.Val2R - a.ValR));     }     if (a.sz > 1 && x == 0) {       now.Valley = {a.Val2R - a.ValR, b.ValL - a.ValR};     }     if (b.sz > 1 && y == 1) {       now.Mindis =           min(now.Mindis, (a.ValR > b.ValL ? a.ValR - b.ValL : b.Val2L - b.ValL));     }     if (b.sz > 1 && y == 0) {       now.Valley = {a.ValR - b.ValL, b.Val2L - b.ValL};     }     if (a.Valley.first != 0) {       now.Valley = a.Valley;     }     if (b.Valley.first != 0) {       now.Valley = b.Valley;     }     now.Mindis = min(now.Mindis, a.Mindis);     now.Mindis = min(now.Mindis, b.Mindis);     if (a.sz == 1)       now.Val2L = b.ValL;     else       now.Val2L = a.Val2L;     if (b.sz == 1)       now.Val2R = a.ValR;     else       now.Val2R = b.Val2R;     now.sz = a.sz + b.sz;     return now;   }   void Build(long long id = 1, long long s = 1, long long e = n) {     seg[id].init();     seg[id].sz = (e - s + 1);     if ((e - s + 1) == 1) {       seg[id].ValL = seg[id].ValR = A[s];       return;     }     Build((id * 2), s, ((s + e) / 2)), Build((id * 2 + 1), ((s + e) / 2 + 1), e);     seg[id] = Merge(seg[(id * 2)], seg[(id * 2 + 1)]);   }   void Shift(long long id, long long s, long long e) {     if (!Lazy[id]) return;     seg[id].ValL += Lazy[id], seg[id].ValR += Lazy[id];     if ((e - s + 1) > 1) {       seg[id].Val2L += Lazy[id], seg[id].Val2R += Lazy[id];       Lazy[(id * 2)] += Lazy[id];       Lazy[(id * 2 + 1)] += Lazy[id];     }     Lazy[id] = 0;   }   void Upd(long long l, long long r, long long x, long long id = 1,            long long s = 1, long long e = n) {     Shift(id, s, e);     if (e < l || s > r) return;     if (l <= s && e <= r) {       Lazy[id] += x;       Shift(id, s, e);       return;     }     Upd(l, r, x, (id * 2), s, ((s + e) / 2)),         Upd(l, r, x, (id * 2 + 1), ((s + e) / 2 + 1), e);     seg[id] = Merge(seg[(id * 2)], seg[(id * 2 + 1)]);   }   void Calc(long long l, long long r, long long id = 1, long long s = 1,             long long e = n) {     Shift(id, s, e);     if (e < l || s > r) return;     if (l <= s && e <= r) {       if (!Ok)         M = seg[id], Ok = true;       else {         M = Merge(M, seg[id]);       }       return;     }     Calc(l, r, (id * 2), s, ((s + e) / 2)),         Calc(l, r, (id * 2 + 1), ((s + e) / 2 + 1), e);     seg[id] = Merge(seg[(id * 2)], seg[(id * 2 + 1)]);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> A[i];     Build();     cin >> q;     while (q--) {       long long t, l, r, x, ans;       cin >> t >> l >> r >> x;       if (t == 2)         Upd(l, r, x);       else {         Ok = false;         Calc(max(l - 1, 1ll), min(r + 1, n));         if (M.Maxdp == 2) ans = 2 * x + seg[1].Sum;         if (M.Maxdp == 0) {           ans = seg[1].Sum;           ans += (M.Valley.first >= x ? -x : x - M.Valley.first * 2);           ans += (M.Valley.second >= x ? -x : x - M.Valley.second * 2);         }         if (M.Maxdp == 1) {           ans = x + (M.Mindis >= x ? -x : x - M.Mindis * 2);           if (M.Valley.first != 0) {             ans = max(ans,                       (M.Valley.first >= x ? -x : x - M.Valley.first * 2) +                           (M.Valley.second >= x ? -x : x - M.Valley.second * 2));           }           ans += seg[1].Sum;         }         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   template <typename T, typename U>   inline void amin(T &x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   inline void amax(T &x, U y) {     if (x < y) x = y;   }   using namespace std;   int main() {     int n, m;     cin >> n >> m;     string grid[n];     for (int i = 0; i < n; ++i) {       cin >> grid[i];     }     int left[n][m], right[n][m], top[n][m], bottom[n][m];     memset(left, (0), sizeof(left));     memset(right, (0), sizeof(right));     memset(top, (0), sizeof(top));     memset(bottom, (0), sizeof(bottom));     for (int i = 1; i < n; ++i) {       for (int j = 1; j < m; ++j) {         if (grid[i][j] !=  * ) continue;         left[i][j] = left[i][j - 1];         top[i][j] = top[i - 1][j];         if (grid[i][j - 1] ==  * ) left[i][j] = left[i][j - 1] + 1;         if (grid[i - 1][j] ==  * ) top[i][j] = top[i - 1][j] + 1;       }     }     for (int i = n - 2; i >= 0; --i) {       for (int j = m - 2; j >= 0; --j) {         if (grid[i][j] !=  * ) continue;         if (grid[i][j + 1] ==  * ) right[i][j] = right[i][j + 1] + 1;         if (grid[i + 1][j] ==  * ) bottom[i][j] = bottom[i + 1][j] + 1;       }     }     bool marked[n][m];     memset(marked, (false), sizeof(marked));     vector<int> row, col, sz;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         if (grid[i][j] !=  * ) continue;         int cnt1 = min(left[i][j], right[i][j]);         int cnt2 = min(top[i][j], bottom[i][j]);         int cnt = min(cnt1, cnt2);         if (cnt != 0) {           row.push_back(i + 1);           col.push_back(j + 1);           sz.push_back(cnt);           marked[i][j] = true;           for (int k = 1; k <= cnt; ++k) {             marked[i][j - k] = true;             marked[i][j + k] = true;             marked[i - k][j] = true;             marked[i + k][j] = true;           }         }       }     }     bool pos = true;     for (int l = 0; l < n; ++l) {       for (int j = 0; j < m; ++j) {         if (grid[l][j] ==  *  && !marked[l][j]) {           pos = false;           break;         }       }     }     if (pos) {       cout << row.size() <<   n ;       for (int i = 0; i < row.size(); ++i) {         cout << row[i] <<     << col[i] <<     << sz[i] <<   n ;       }     } else {       cout <<  -1 n ;     }     return 0;   }