#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n, max_len = 0, tmpl, tmpr;     pair<int, int> fmaxl, fminr, smaxl(-1, -1), sminr(-1, -1);     cin >> n >> tmpl >> tmpr;     fmaxl = make_pair(tmpl, tmpr);     fminr = make_pair(tmpl, tmpr);     for (int i = 1; i < n; i++) {       cin >> tmpl >> tmpr;       if (tmpl >= fmaxl.first) {         smaxl = fmaxl;         fmaxl = make_pair(tmpl, tmpr);       }       if (tmpr <= fminr.second) {         sminr = fminr;         fminr = make_pair(tmpl, tmpr);       }       if (smaxl.first == -1 || (tmpl < fmaxl.first && tmpl > smaxl.first))         smaxl = make_pair(tmpl, tmpr);       if (sminr.first == -1 || (tmpr > fminr.second && tmpr < sminr.second))         sminr = make_pair(tmpl, tmpr);     }     if (fmaxl == fminr && smaxl == sminr)       cout << sminr.second - sminr.first;     else if (fmaxl == fminr)       cout << max((sminr.second - smaxl.first), (sminr.second - smaxl.first));     else {       max_len = max((fminr.second - smaxl.first), (sminr.second - fmaxl.first));       max_len < 0 ? cout << 0 : cout << max_len;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000005];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int i, j, k, l, m, n, t, r, cnt;     int flag = 0;     long long ans = 0, an = 0;     t = 1;     while (t--) {       cin >> n >> m;       cout << min(n, m) + 1 << endl;       int x, y;       x = 0;       y = m;       while (x <= n && y >= 0) {         cout << x <<     << y << endl;         x++;         y--;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, diff = 0;     bool flag = 1;     scanf( %d , &n);     int a[n];     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       if (i == 1) diff = a[i] - a[i - 1];     }     for (int i = 1; i < n; i++) {       if (a[i] - a[i - 1] != diff) {         flag = 0;         break;       }     }     if (flag)       printf( %d n , a[0] + n * diff);     else       printf( %d n , a[n - 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long sqr(long long x) { return x * x; }   mt19937 Random((int)time(0));   inline long long rnd(long long x) { return Random() % x; }   const long long INF = (long long)1e9 + 2018;   const long long MOD = (long long)1e9 + 7;   const long double PI = acos(-1.0);   pair<int, int> cnt(int n) {     if (n & 1) {       return {sqr(n / 2 + 1) + sqr(n / 2), n * n - sqr(n / 2 + 1) - sqr(n / 2)};     } else {       return {n * n / 2, n * n / 2};     }   }   void $main() {     int n;     cin >> n;     int a = 0, b = 0;     for (int i = 0; i < n; i++) {       string s;       cin >> s >> s;       a += s ==  hard ;       b += s ==  soft ;     }     for (int i = 1;; i++) {       pair<int, int> res = cnt(i);       if ((res.first >= a && res.second >= b) ||           (res.second >= a && res.first >= b)) {         cout << i;         return;       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout.setf(ios_base::fixed, ios_base::floatfield);     cout.precision(10);     $main();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 7;   int T, n, vis[N], can[N];   map<char, int> mp, ha = {{ ? , 0}, { V , 1}, { K , 2}};   char s[N];   bool check(int l) {     static int vis[N];     for (int i = 0; i < l; ++i) {       int who = ha[s[i]], now = i + l;       while (now < n) {         if (who && s[now] !=  ?  && who != ha[s[now]]) return false;         if (s[now] !=  ? ) who = ha[s[now]];         now += l;       }     }     return true;   }   int main() {     cin >> T;     while (T--) {       scanf( %d , &n);       scanf( %s , s);       memset(vis, 0, sizeof(int) * (n + 1));       memset(can, 0, sizeof(int) * (n + 1));       mp.clear();       for (int i = 0; i < n; ++i) mp[s[i]] = 1;       if (mp[ V ] + mp[ K ] <= 1) {         printf( %d n , n);         for (int i = 1; i <= n; ++i) printf( %d%c , i, i == n ?   n  :    );         continue;       }       for (int i = 2; i <= n; ++i) {         if (!vis[i]) {           if (check(i)) {             for (int j = i; j <= n; j += i) can[j] = true, vis[j] = 0;             can[i] = true;           }         }       }       int ans = 0, first = 1;       for (int i = 2; i <= n; ++i) ans += can[i];       printf( %d n , ans);       for (int i = 2; i <= n; ++i) {         if (can[i]) {           if (first)             first = false;           else             putchar(   );           printf( %d , i);         }       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   int getbit(T s, int i) {     return (s >> i) & 1;   }   template <class T>   T onbit(T s, int i) {     return s | (T(1) << i);   }   template <class T>   T offbit(T s, int i) {     return s & (~(T(1) << i));   }   template <class T>   int cntbit(T s) {     return __builtin_popcount(s);   }   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;   }   int test;   int n;   long long RR;   vector<int> KK[105];   long long HH(long long x) { return x * x * x * x * 3 + 425689 * x - 23156667; }   long long go(int par, int u) {     long long res = 32131;     for (int i = 0; i < (((int)(KK[u]).size())); ++i) {       int v = KK[u][i];       if (v == par) continue;       res += HH(go(u, v));     }     return res;   }   long long hash_tree(vector<pair<int, int> > V) {     int d[105];     memset(d, 0, sizeof(d));     for (int i = (1); i <= (n); ++i)       if (((int)(KK[i]).size())) KK[i].clear();     for (int i = 0; i < (((int)(V).size())); ++i) {       int u = V[i].first, v = V[i].second;       KK[u].push_back(v);       KK[v].push_back(u);       d[u]++;       d[v]++;     }     int number = 0;     int que[105], size = 0, que1[105], size1 = 0;     for (int i = (1); i <= (n); ++i) {       if (d[i] >= 1) number++;       if (d[i] == 1) que[size++] = i;     }     while (true) {       if (size <= 2) break;       size1 = 0;       for (int t = 0; t < (size); ++t) {         que1[size1++] = que[t];       }       size = 0;       for (int t = 0; t < (size1); ++t) {         int u = que1[t];         for (int j = 0; j < (((int)(KK[u]).size())); ++j) {           int v = KK[u][j];           d[v]--;           if (d[v] == 1) {             que[size++] = v;           }         }       }     }     long long res = 325843;     vector<long long> Vll;     for (int i = 0; i < (size); ++i) {       Vll.push_back(go(0, que[i]));     }     sort((Vll).begin(), (Vll).end());     for (int i = 0; i < (size); ++i) {       res = res * 1000000007 + Vll[i];     }     return res;   }   long long hash_forest(vector<pair<int, int> > V) {     bool flag[105];     vector<int> VV[105];     memset(flag, 0, sizeof(flag));     for (int i = 0; i < (((int)(V).size())); ++i) {       int u = V[i].first, v = V[i].second;       VV[u].push_back(v);       VV[v].push_back(u);     }     vector<long long> Vll;     for (int i = 1; i <= n; i++)       if (!flag[i]) {         flag[i] = 1;         int que[105], size = 0;         que[size++] = i;         vector<pair<int, int> > temp;         for (int t = 0; t < (size); ++t) {           int u = que[t];           for (int j = 0; j < (((int)(VV[u]).size())); ++j) {             int v = VV[u][j];             if (!flag[v]) {               flag[v] = 1;               que[size++] = v;               temp.push_back(make_pair(u, v));             }           }         }         Vll.push_back(hash_tree(temp));       }     sort((Vll).begin(), (Vll).end());     long long res = 132435;     for (int i = 0; i < ((int)(Vll).size()); i++) res = res * 1000000007 + Vll[i];     return res;   }   long long hash_alot(vector<vector<pair<int, int> > > V) {     vector<long long> Vll;     for (int i = 0; i < ((int)(V).size()); i++) {       Vll.push_back(hash_forest(V[i]));     }     sort((Vll).begin(), (Vll).end());     long long res = 132435;     for (int i = 0; i < ((int)(Vll).size()); i++) res = res * 1000000007 + Vll[i];     return res;   }   bool calcu(vector<pair<int, int> > V) {     vector<vector<pair<int, int> > > VV;     for (int i = (1); i <= (n); ++i) {       vector<pair<int, int> > temp;       for (int j = 0; j < (((int)(V).size())); ++j) {         if (V[j].first != i && V[j].second != i) {           temp.push_back(V[j]);         }       }       VV.push_back(temp);     }     long long res = hash_alot(VV);     return res == RR;   }   void cal(vector<pair<int, int> > V) {     bool have[105];     memset(have, 0, sizeof(have));     for (int i = 0; i < (((int)(V).size())); ++i) {       int u = V[i].first, v = V[i].second;       have[u] = 1;       have[v] = 1;     }     for (int i = (1); i <= (n); ++i)       if (!have[i]) {         for (int j = (1); j <= (n); ++j)           if (j != i) {             vector<pair<int, int> > VV;             VV = V;             VV.push_back(make_pair(i, j));             if (calcu(VV)) {               cout <<  YES n ;               for (int t = 0; t < (((int)(VV).size())); ++t) {                 cout << VV[t].first <<     << VV[t].second <<   n ;               }               return;             }           }       }     cout <<  NO n ;   }   void solve() {     cin >> n >> n;     vector<vector<pair<int, int> > > V;     for (int i = 0; i < (n); ++i) {       int num;       cin >> num;       vector<pair<int, int> > temp;       for (int j = 0; j < (num); ++j) {         int u, v;         cin >> u >> v;         temp.push_back(make_pair(u, v));       }       V.push_back(temp);     }     RR = hash_alot(V);     for (int i = 0; i < n; i++) {       if (((int)(V[i]).size()) == n - 2) {         cal(V[i]);         break;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> test;     for (int itest = 0; itest < (test); ++itest) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, res;   int mat[405][405];   int cnt[405][405][4];   bool valid(int x1, int y1, int x2, int y2) {     int num = 2;     if (x1 == x2) num = 1;     if (y1 == y2) num = 0;     if (x1 + y1 == x2 + y2) num = 3;     return cnt[x1][y1][num] == cnt[x2][y2][num];   }   void check(int x1, int y1, int x2, int y2, int x3, int y3) {     if (x1 < 1 || y1 < 1 || x2 < 1 || y2 < 1 || x3 < 1 || y3 < 1) return;     if (x1 > n || y1 > m || x2 > n || y2 > m || x3 > n || y3 > m) return;     if (mat[x1][y1] || mat[x2][y2] || mat[x3][y3]) return;     if (!valid(x1, y1, x2, y2)) return;     if (!valid(x2, y2, x3, y3)) return;     if (!valid(x1, y1, x3, y3)) return;     res++;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         char c;         cin >> c;         mat[i][j] = (c -  0 );         cnt[i][j][0] = cnt[i - 1][j][0] + mat[i][j];         cnt[i][j][1] = cnt[i][j - 1][1] + mat[i][j];         cnt[i][j][2] = cnt[i - 1][j - 1][2] + mat[i][j];       }     }     for (int i = 1; i <= n; i++) {       for (int j = m; j >= 1; j--) {         cnt[i][j][3] = cnt[i - 1][j + 1][3] + mat[i][j];       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         for (int k = 1; k <= min(n, m); k++) {           check(i, j, i + k, j, i, j + k);           check(i, j, i - k, j, i, j + k);           check(i, j, i + k, j, i, j - k);           check(i, j, i - k, j, i, j - k);           check(i - k, j, i, j + k, i, j - k);           check(i + k, j, i, j + k, i, j - k);           check(i + k, j, i - k, j, i, j - k);           check(i + k, j, i - k, j, i, j + k);         }       }     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool upmin(T &x, T y) {     return y < x ? x = y, 1 : 0;   }   template <typename T>   inline bool upmax(T &x, T y) {     return x < y ? x = y, 1 : 0;   }   const long double eps = 1e-11;   const long double pi = acos(-1);   const int oo = 1 << 30;   const long long loo = 1ll << 62;   const int MAXN = 5005;   const int mods = 998244353;   const int INF = 0x3f3f3f3f;   inline int read() {     int f = 1, x = 0;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + (c ^ 48);       c = getchar();     }     return x * f;   }   int f[MAXN][MAXN][2], g[MAXN][2], cnt[MAXN], num = 0, n;   vector<int> e[MAXN];   void tree_dp(int x, int father) {     int flag = 0;     for (auto v : e[x])       if (v != father) flag = 1, tree_dp(v, x);     if (!flag)       f[x][0][0] = f[x][1][1] = 0, cnt[x] = 1;     else       f[x][0][0] = f[x][0][1] = cnt[x] = 0;     for (auto v : e[x])       if (v != father) {         cnt[x] += cnt[v];         for (int i = cnt[x]; i >= 0; i--) {           g[i][0] = INF, g[i][1] = INF;           for (int j = min(cnt[v], i); j >= 0; j--) {             upmin(g[i][0], min(f[x][i - j][0] + f[v][j][0],                                f[x][i - j][0] + f[v][j][1] + 1));             upmin(g[i][1], min(f[x][i - j][1] + f[v][j][0] + 1,                                f[x][i - j][1] + f[v][j][1]));           }           f[x][i][0] = g[i][0], f[x][i][1] = g[i][1];         }       }   }   signed main() {     n = read();     for (int i = 1, u, v; i < n; i++)       u = read(), v = read(), e[u].push_back(v), e[v].push_back(u);     for (int i = 0; i <= n; i++)       for (int j = 0; j <= n; j++) f[i][j][0] = f[i][j][1] = INF;     for (int i = 1; i <= n; i++)       if (e[i].size() > 1) {         tree_dp(i, 0);         printf( %d n , min(f[i][cnt[i] / 2][0], f[i][cnt[i] / 2][1]));         return 0;       }     puts( 1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n + 1];     int i, m, k, l, x, y;     for (i = 1; i <= n; i++) cin >> a[i];     cin >> m;     for (i = 0; i < m; i++) {       cin >> x >> y;       a[x - 1] += y - 1;       a[x + 1] += (a[x] - y);       a[x] = 0;     }     for (i = 1; i <= n; i++) cout << a[i] << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long t;     cin >> t;     while (t--) {       long long n, m, x, y;       cin >> n >> m >> x >> y;       if (2 * x < y) y = 2 * x;       vector<vector<long long>> grid(n, vector<long long>(m));       for (long long i = 0; i < n; i++) {         for (long long j = 0; j < m; j++) {           char c;           cin >> c;           if (c ==  . )             grid[i][j] = 0;           else             grid[i][j] = 1;         }       }       long long sum = 0;       for (long long i = 0; i < n; i++) {         vector<pair<long long, long long>> all;         all.push_back({grid[i][0], 1});         for (long long j = 1; j < m; j++) {           if (grid[i][j] == all.back().first)             all.back().second++;           else             all.push_back({grid[i][j], 1});         }         for (auto it : all) {           if (it.first == 0) {             long long a = it.second / 2, b = it.second % 2;             sum += a * y;             sum += b * x;           }         }       }       cout << sum <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int jou[n + 1];     vector<bool> u(n + 1, false);     int k = 1;     for (int i = 1; i < n + 1; i++) {       cin >> jou[i];       if (u[jou[i]] == false) {         u[jou[i]] = true;       } else         k++;     }     cout << k;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long rdtsc() {     long long tmp;     asm( rdtsc  :  =A (tmp));     return tmp;   }   inline int myrand() { return abs((rand() << 15) ^ rand()); }   inline int rnd(int x) { return myrand() % x; }   const int INF = (int)1.01e9;   const long double EPS = 1e-9;   void precalc() {}   long long x;   bool read() {     if (scanf( %lld , &x) < 1) {       return 0;     }     return 1;   }   void solve() {     vector<pair<long long, long long> > ans;     for (long long t = 1;; ++t) {       long long a = t * (t + 1) / 2;       long long b = a * (2 * t + 1) / 3;       if (b > x) {         break;       }       long long left = x - b;       if (left % a) {         continue;       }       left /= a;       ans.push_back(make_pair(t, t + left));       if (left) {         ans.push_back(make_pair(t + left, t));       }     }     printf( %d n , ((int)(ans).size()));     sort(ans.begin(), ans.end());     for (int i = 0; i < ((int)(ans).size()); ++i) {       printf(            %lld                           %lld              n ,           ans[i].first, ans[i].second);     }   }   int main() {     srand(rdtsc());     precalc();     while (1) {       if (!read()) {         break;       }       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Mod = 1e9 + 7;   int n, inv;   long long m;   long long A[30];   inline int Qpow(int a, int b) {     int ans = 1;     while (b) {       if (b & 1) ans = (long long)ans * a % Mod;       b >>= 1;       a = (long long)a * a % Mod;     }     return ans;   }   inline int C(long long a) {     if (a < (n - 1)) return 0;     long long res = 1;     for (int i = 0; i < n - 1; i++) res = res * ((a - i) % Mod) % Mod;     return res * inv % Mod;   }   inline void Init() {     cin >> n >> m;     inv = 1;     for (int i = 2; i < n; i++) inv = (long long)inv * i % Mod;     inv = Qpow(inv, Mod - 2);     for (int i = 1; i <= n; i++) cin >> A[i];   }   inline void Work() {     int res = 0;     for (int i = 0; i < (1 << n); i++) {       long long a = n + m - 1;       int flag = 1;       for (int j = 0; j < n; j++) {         if ((i >> j) & 1) {           flag *= -1;           a -= A[j + 1] + 1;         }       }       res = ((long long)res + (long long)flag * C(a) % Mod) % Mod;     }     cout << (res + Mod) % Mod << endl;   }   int main() {     Init();     Work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, K, bs[500005], hs[500005], ht[500005], ht2[500005], lpos[500005],       rpos[500005], ansl, ansr;   bool flag;   set<int> S;   set<int>::iterator it;   struct node {     int len, id;   } a[500005];   char s[500005], t[500005];   int Hashs(int fr, int to) {     return (-(long long)hs[fr - 1] * bs[to - fr + 1] % 1000000007 + hs[to] +             1000000007) %            1000000007;   }   bool check(int fr, int l) { return ht[l] == Hashs(fr, fr + l - 1); }   bool check2(int to, int l) { return ht2[m - l + 1] == Hashs(to - l + 1, to); }   bool cmp1(const node &lhs, const node &rhs) { return lhs.len > rhs.len; }   int main() {     scanf( %d%d%d , &n, &m, &K);     scanf( %s , s + 1);     scanf( %s , t + 1);     bs[0] = 1;     for (int i = 1; i <= n; ++i) bs[i] = (long long)bs[i - 1] * 8741 % 1000000007;     for (int i = 1; i <= n; ++i)       hs[i] = ((long long)hs[i - 1] * 8741 + s[i]) % 1000000007;     for (int i = 1; i <= m; ++i)       ht[i] = ((long long)ht[i - 1] * 8741 + t[i]) % 1000000007;     for (int i = 1; i <= n; ++i) {       int l = 0, r = min(m, n - i + 1), mid;       while (l < r) {         mid = (l + r + 1) >> 1;         if (check(i, mid))           l = mid;         else           r = mid - 1;       }       a[i] = (node){l, i};     }     sort(a + 1, a + n + 1, cmp1);     S.clear();     int now = 1;     for (int i = m; i >= 1; --i) {       for (; a[now].len == i && now <= n; now++) {         S.insert(a[now].id);       }       if (i == m) {         it = S.lower_bound(1);         if (it != S.end())           lpos[i] = *it;         else           lpos[i] = 1000000000;       } else {         it = S.lower_bound(K - i + 1);         if (it != S.end())           lpos[i] = *it;         else           lpos[i] = 1000000000;       }     }     for (int i = m; i >= 1; --i) {       ht2[i] = (-(long long)ht[i - 1] * bs[m - i + 1] % 1000000007 + ht[m] +                 1000000007) %                1000000007;     }     S.clear();     for (int i = 1; i <= n; ++i) {       int l = 0, r = i, mid;       while (l < r) {         mid = (l + r + 1) >> 1;         if (check2(i, mid))           l = mid;         else           r = mid - 1;       }       a[i] = (node){l, i};     }     sort(a + 1, a + n + 1, cmp1);     now = 1;     for (int i = m; i >= 1; --i) {       for (; a[now].len == i && now <= n; now++) {         S.insert(a[now].id);       }       if (i == m) {         it = S.lower_bound(K);         if (it != S.end())           rpos[i] = *it;         else           rpos[i] = -1000000000;       } else {         it = S.upper_bound(n + i - K);         if (it != S.begin()) {           it--;           rpos[i] = *it;         } else           rpos[i] = -1000000000;       }     }     flag = 0;     for (int i = 1; i < m; ++i) {       int x = lpos[i], y = rpos[m - i];       if (i > K || m - i > K) continue;       if (x + i - 1 <= y - m + i) {         flag = 1;         ansl = x + i - K, ansr = y - m + i + 1;         break;       }     }     if (!flag && m <= K) {       if (max(K, lpos[m] + m - 1) + K <= n) {         flag = 1;         ansl = max(lpos[m] + m - 1, K) - K + 1, ansr = n - K + 1;       }       if (min(n - K + 1, rpos[m] - m + 1) - K >= 1) {         flag = 1;         ansl = 1, ansr = min(n - K + 1, rpos[m] - m + 1);       }     }     if (flag)       printf( Yes n%d %d n , ansl, ansr);     else       printf( No n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100 + 10;   const int inf = 1e9;   vector<int> getArray(int *a, int n, int k) {     vector<int> ret;     for (int i = 0; i < n * k; i++) {       if (i < n)         ret.push_back(a[i]);       else         ret.push_back(a[i % n]);     }     return ret;   }   vector<int> myLIS(vector<int> a, int n) {     vector<int> dp, ret;     dp.resize(n + 1);     dp[0] = -inf;     for (int i = 1; i <= n; i++) dp[i] = 1000;     for (int i = 0; i < n; i++) {       int tmp = upper_bound(dp.begin(), dp.end(), a[i]) - dp.begin();       ret.push_back(tmp);       dp[tmp] = a[i];     }     return ret;   }   int main() {     int n, T, a[maxn];     scanf( %d%d , &n, &T);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     int ret = 0;     if (T <= 1000) {       vector<int> prefix = myLIS(getArray(a, n, T), n * T);       for (int i = 0; i < n * T; i++) {         ret = max(ret, prefix[i]);       }     } else {       vector<int> newA = getArray(a, n, n);       vector<int> prefix = myLIS(newA, n * n);       for (int i = 0; i < newA.size(); i++) {         newA[i] = -newA[i];       }       reverse(newA.begin(), newA.end());       vector<int> suffix = myLIS(newA, n * n);       for (int i = 0; i < n; i++) {         int cnt = 0;         for (int j = 0; j < n; j++)           if (a[i] == a[j]) cnt++;         for (int j = 0; j < n; j++) {           if (a[j] < a[i]) continue;           ret = max(ret, prefix[(n - 1) * n + i] + cnt * (T - 2 * n) +                              suffix[n * n - j - 1]);         }       }     }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     set<int> st;     for (int i = 0; i < n; i++) {       cin >> a[i];       st.insert(a[i]);     }     set<int>::iterator it = st.begin();     it++;     if (it == st.end())       cout <<  NO ;     else       cout << (*it);     return 0;   }


#include <bits/stdc++.h>   int main() {     long a, b, c;     scanf( %ld%ld%ld , &a, &b, &c);     if (b == a) {       printf( YES n );     } else if (c > 0) {       if (b < a) {         printf( NO n );       } else if (((b - a) % c) == 0) {         printf( YES n );       } else {         printf( NO n );       }     } else if (c < 0) {       if (b > a) {         printf( NO n );       } else if (((a - b) % c) == 0) {         printf( YES n );       } else {         printf( NO n );       }     } else {       printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e4 + 100;   const long long inf = 0x3f3f3f3f3f;   int n;   int a[maxn];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       if (i != n) {         int q;         cout <<  ?               <<   1               <<     << i + 1 << endl;         cin >> q;         a[i + 1] = q;       } else {         int q;         cout <<  ?               <<                  <<  2               <<     << 3 << endl;         cin >> q;         a[1] = (a[2] + a[3] - q) / 2;       }     }     cout <<  !  ;     for (int i = 1; i <= n; i++) {       if (i == 1)         cout << a[1] <<    ;       else         cout << a[i] - a[1] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long int n, q;   long long int gcd(long long int a, long long int b) {     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   long long int powe(long long int a, long long int b) {     long long int res = 1;     while (b > 0) {       if (b % 2 == 1) {         res = (res * a);       }       a = (a * a);       b /= 2;     }     return res;   }   long long int power(long long int a, long long int b, long long int M) {     a %= M;     long long int res = 1;     while (b > 0) {       if (b % 2 == 1) {         res = (res * a) % M;       }       a = (a * a) % M;       b /= 2;     }     return res;   }   long long int extendedEuclid(long long int A, long long int B, long long int &x,                                long long int &y) {     if (B == 0) {       x = 1;       y = 0;       return A;     } else {       long long int x1, y1;       long long int gcd = extendedEuclid(B, A % B, x1, y1);       y = x1 - (A / B) * y1;       x = y1;       return gcd;     }   }   long long int mi(long long int A, long long int M) {     long long int x, y;     extendedEuclid(A, M, x, y);     if (x < 0) {       x += (long long int)1000000007;     }     return x;   }   template <typename T>   void read(T &x) {     cin >> x;   }   template <typename T, typename T0>   void read(T &x, T0 &y) {     cin >> x >> y;   }   template <typename T, typename T0, typename T1>   void read(T &x, T0 &y, T1 &z) {     cin >> x >> y >> z;   }   template <typename T, typename T0, typename T1, typename T2>   void read(T &x, T0 &y, T1 &z, T2 &w) {     cin >> x >> y >> z >> w;   }   template <typename T, typename T0>   void read(pair<T, T0> &p) {     cin >> p.first >> p.second;   }   template <typename T, typename T0>   void write(pair<T, T0> &p) {     write(p.first);     write(p.second);   }   template <typename T>   void read(vector<T> &oneD, long long int n) {     for (long long int i = 0; i < n; i++) {       long long int x;       read(x);       oneD.push_back(x);     }   }   template <typename T>   void read(T oneD[], long long int n) {     for (long long int i = 0; i < n; i++) {       read(oneD[i]);     }   }   template <typename T>   void write(T oneD[], int n) {     for (long long int i = 0; i < n; i++) {       write(oneD[i]);     }     cout << endl;   }   vector<bool> sieve(1000000, true);   void Sieve() {     sieve[0] = false;     sieve[1] = false;     for (long long int i = 2; i * i <= 1000000; i++) {       if (sieve[i] == true) {         for (long long int j = i * i; j < 1000000; j += i) sieve[j] = false;       }     }   }   vector<long long int> sieve_spf;   void Sieve_spf() {     const long long int n = 1e6 + 5;     sieve_spf.resize(n);     for (long long int i = 0; i < n; i++) sieve_spf[i] = i;     sieve_spf[0] = -1;     sieve_spf[1] = 1;     for (long long int i = 2; i <= n; i += 2) sieve_spf[i] = 2;     for (long long int i = 3; i <= n; i += 2)       if (sieve_spf[i] == i)         for (long long int j = i * i; j <= n; j += i)           if (sieve_spf[j] == j) sieve_spf[j] = i;   }   bool oppositeSigns(long long int x, long long int y) { return ((x ^ y) < 0); }   int mpt(long long int n) {     long long int c = 0;     long long int n1 = pow(2, c);     while (n1 < n) {       c++;       n1 = pow(2, c);     }     return c;   }   void the_happiest_place_on_earth() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;   }   int main() {     the_happiest_place_on_earth();     long long int t;     read(t);     while (t--) {       read(n);       cout << n - 1 <<     << 1;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mob[2000005], vis[2000005], p[1000005];   long long cnt[2000005], cnt2[2000005], pnum;   bool prime[1000005];   long long mod = 1e9 + 7;   int read() {     char c;     for (c = getchar(); c <  0  || c >  9 ; c = getchar())       ;     int x = c - 48;     for (c = getchar(); c >=  0  && c <=  9 ; c = getchar()) x = x * 10 + c - 48;     return x;   }   void Mobius() {     int i, j;     pnum = 0;     memset(prime, true, sizeof(prime));     mob[1] = 1;     for (i = 2; i < 1000005; i++) {       if (prime[i]) {         p[pnum++] = i;         mob[i] = -1;       }       for (j = 0; j < pnum && (long long)i * p[j] < 1000005; j++) {         prime[i * p[j]] = false;         if (i % p[j] == 0) {           mob[i * p[j]] = 0;           break;         }         mob[i * p[j]] = -mob[i];       }     }   }   int main(void) {     int n, m, x, T, MAX, ca = 1;     long long ans, temp, k, kk, nnn, res, i, j;     Mobius();     n = read();     k = read();     kk = read();     nnn = k / kk;     memset(cnt, 0, sizeof(cnt));     memset(cnt2, 0, sizeof(cnt2));     MAX = 1;     for (i = 1; i <= n; i++) {       x = read();       cnt[x]++;       cnt2[x] += x;       MAX = max(MAX, x);     }     for (int i = 1; i <= 2000000; i++) {       cnt[i] += cnt[i - 1];       cnt2[i] += cnt2[i - 1];     }     ans = -1;     T = 0;     for (i = p[T]; T < pnum; i = p[T]) {       T++;       res = 0;       for (j = i; j <= 1000000 + i; j += i) {         if (nnn >= i) {           res += (((cnt[j - 1] - cnt[j - i]) * j) - (cnt2[j - 1] - cnt2[j - i])) *                  kk;         } else {           res += (cnt[j - nnn - 1] - cnt[j - i]) * k;           res += (((cnt[j - 1] - cnt[j - nnn - 1]) * j) -                   (cnt2[j - 1] - cnt2[j - nnn - 1])) *                  kk;         }       }       if (ans == -1) ans = res;       ans = min(ans, res);     }     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int n;   string s;   set<vector<int> > DP[10001];   bool R[4];   int main() {     cin >> n;     cin >> s;     for (int i = 0; i < s.size(); i++)       if (s[i] ==  G )         s[i] =  0 ;       else if (s[i] ==  S )         s[i] =  1 ;       else if (s[i] ==  H )         s[i] =  2 ;       else if (s[i] ==  R )         s[i] =  3 ;     vector<int> E(4, 0);     DP[0].insert(E);     for (int i = 0; i < n; i++) {       set<vector<int> >::iterator it;       for (it = DP[i].begin(); it != DP[i].end(); it++) {         vector<int> T;         T = *it;         int q = min(T[0], min(T[1], min(T[2], T[3])));         for (int j = 0; j < 4; j++) {           if ((s[i] !=  ?  && (j == (s[i] -  0 )) ||                (s[i] ==  ?  && T[j] == q))) {             vector<int> N;             N = T;             N[j]++;             int p = min(N[0], min(N[1], min(N[2], N[3])));             for (int l = 0; l < 4; l++) N[l] -= p;             DP[i + 1].insert(N);           }         }       }     }     set<vector<int> >::iterator it;     for (it = DP[n].begin(); it != DP[n].end(); it++) {       vector<int> T;       T = *it;       int q = min(T[0], min(T[1], min(T[2], T[3])));       for (int j = 0; j < 4; j++)         if (T[j] == q) R[j] = true;     }     if (R[0]) cout <<  Gryffindor  << endl;     if (R[2]) cout <<  Hufflepuff  << endl;     if (R[3]) cout <<  Ravenclaw  << endl;     if (R[1]) cout <<  Slytherin  << endl;     cin >> n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, k, d;     scanf( %d%d%d , &n, &k, &d);     vector<int> v(n);     for (int &x : v) {       scanf( %d , &x);     }     map<int, int> m_map;     int mn = 0, cnt = 0;     for (int i = 0; i < d; i++) {       if (m_map[v[i]] == 0) {         cnt++;       }       m_map[v[i]]++;     }     mn = cnt;     for (int i = d; i < n; i++) {       m_map[v[i - d]]--;       if (m_map[v[i - d]] == 0) cnt--;       if (m_map[v[i]] == 0) cnt++;       m_map[v[i]]++;       mn = min(mn, cnt);     }     printf( %d n , mn);   }   int main() {     int testcase;     scanf( %d , &testcase);     for (int i = 1; i <= testcase; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>      #define ll long long      using namespace std;      int t;      int main() {       ios_base::sync_with_stdio(false);       cin.tie(0);       cout.tie(0);       cin >> t;       while (t--) {           int n;           cin >> n;           vector<pair<int, int> > a(n + 1);           for (int i = 1; i <= n; i++) {               cin >> a[i].first;               a[i].second = i;           }           sort(a.begin() + 1, a.end());           if (a[1].first == a[2].first) {               cout << a[n].second << endl;           } else {               cout << a[1].second << endl;           }       }          return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k;     string s;     map<char, int> m1;     cin >> k >> s;     int n = s.length();     for (int i = 0; i < n; i++) {       m1[s[i]]++;     }     map<char, int>::iterator it;     int flag = 0;     for (it = m1.begin(); it != m1.end(); it++) {       if ((it->second) % k) {         flag = 1;         break;       }     }     if (flag) {       cout <<  -1 ;     } else {       it = m1.begin();       int val = k;       string s1 =   ;       for (it = m1.begin(); it != m1.end(); it++) {         int num = (it->second) / k;         while (num--) s1 += it->first;       }       while (k--) cout << s1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string dp[5001];   vector<int> win[2001];   int n;   string pow2[2001];   void multi2(string &ress, const string &p) {     int flag = 0;     ress =   ;     for (int i = 0; i < p.size(); i++) {       ress += ((p[i] -  0 ) * 2 + flag) % 10 +  0 ;       flag = ((p[i] -  0 ) * 2 + flag) / 10;     }     if (flag) ress +=  1 ;     int pos = ress.size() - 1;     for (; pos >= 0 && ress[pos] ==  0 ; pos--)       ;     ress = ress.substr(0, pos + 1);     if (ress ==   ) ress =  0 ;     return;   }   void initPow2(void) {     pow2[0] =  1 ;     for (int i = 1; i <= 2000; i++) multi2(pow2[i], pow2[i - 1]);     return;   }   void add(string &ress, string num) {     while (ress.size() < num.size()) ress +=  0 ;     while (num.size() < ress.size()) num +=  0 ;     int flag = 0;     for (int i = 0; i < ress.size(); i++) {       char tRes = (ress[i] -  0  + num[i] -  0  + flag) % 10 +  0 ;       flag = (ress[i] -  0  + num[i] -  0  + flag) / 10;       ress[i] = tRes;     }     if (flag) ress +=  1 ;     int pos = ress.size() - 1;     for (; pos >= 0 && ress[pos] ==  0 ; pos--)       ;     ress = ress.substr(0, pos + 1);     if (ress ==   ) ress =  0 ;     return;   }   bool larger(const string &str1, const string &str2) {     if (str1.size() > str2.size()) return true;     if (str1.size() < str2.size()) return false;     for (int i = str1.size() - 1; i >= 0; i--) {       if (str1[i] > str2[i]) return true;       if (str1[i] < str2[i]) return false;     }     return false;   }   int main(void) {     char enter;     char input[10];     int t;     dp[0] =  0 ;     initPow2();     for (int i = 0; i <= 2000; i++) win[i].clear();     scanf( %d%c , &n, &enter);     for (int i = 1; i <= n; i++) {       scanf( %s%c%d%c , input, &enter, &t, &enter);       if (input[0] ==  w ) {         win[t].push_back(i);         dp[i] = dp[i - 1];       } else {         dp[i] = dp[i - 1];         if (win[t].size() != 0) {           for (int j = 0; j < win[t].size(); j++) {             string temp = pow2[t];             add(temp, dp[win[t][j] - 1]);             if (larger(temp, dp[i])) dp[i] = temp;           }         }       }     }     reverse(dp[n].begin(), dp[n].end());     printf( %s n , dp[n].c_str());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, s, ans;   int ar[5005];   int cnt[5005];   int ar2[5005];   vector<pair<pair<int, int>, pair<int, int> > > v;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) ar[i] = 1000000000;     for (int i = 0; i < m; i++) {       int t, l, r, x;       scanf( %d%d%d%d , &t, &l, &r, &x);       if (t == 1)         for (int j = l; j <= r; j++) cnt[j] += x;       else {         for (int j = l; j <= r; j++) ar[j] = min(ar[j], x - cnt[j]);       }       v.push_back(pair<pair<int, int>, pair<int, int> >(pair<int, int>(t, x),                                                         pair<int, int>(l, r)));     }     for (int i = 1; i <= n; i++)       if (max(ar[i], -ar[i]) > 1000000000) {         printf( NO n );         exit(0);       }     for (int i = 1; i <= n; i++) ar2[i] = ar[i];     for (int i = 0; i < m; i++) {       int t = v[i].first.first;       int x = v[i].first.second;       int l = v[i].second.first;       int r = v[i].second.second;       if (t == 1) {         for (int j = l; j <= r; j++) ar2[j] += x;       } else {         int mx = -1000000000;         for (int j = l; j <= r; j++) mx = max(mx, ar2[j]);         if (mx != x) {           printf( NO n );           exit(0);         }       }     }     printf( YES n );     for (int i = 1; i <= n; i++) printf( %d  , ar[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int pot(int n) {     if (n == (n & -n)) return n;     return (n - (n & -n));   }   int main() {     int n, b, p;     cin >> n >> b >> p;     int ans1 = 0, ans2 = p * n;     while (n != 1) {       int d = pot(n);       int e = n - d;       n -= (d / 2);       ans1 += (d * b + (d / 2));     }     cout << ans1 <<     << ans2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     char name[21];     int initials[26];     cin >> n;     memset(initials, 0, 26 * sizeof(int));     for (int i = 0; i < n; ++i) {       cin >> name;       initials[name[0] -  a ]++;     }     int total = 0;     for (int i = 0; i < 26; ++i) {       int k = initials[i];       if (k % 2)         total += ((k - 1) / 2) * ((k - 1) / 2);       else         total += k / 2 * (k / 2 - 1);     }     cout << total << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   long long ans, dp[N];   int ed, n, m;   struct node {     int w, c;     double val;     node(int a = 0, int b = 0) : w(a), c(b) {}     bool operator<(const node &b) const { return val > b.val; }   } a[N], tmp[N];   long long DP() {     for (int i = 1; i <= ed; i++) {       for (int j = m; j >= tmp[i].w; j--) {         dp[j] = max(dp[j], dp[j - tmp[i].w] + tmp[i].c);       }     }     return dp[m];   }   int main() {     scanf( %d%d , &n, &m);     ans = 0;     for (int i = 1; i <= n; i++)       scanf( %d%d , &a[i].w, &a[i].c), a[i].val = 1.0 * a[i].c / a[i].w;     sort(a + 1, a + 1 + n);     int tp[4];     memset(tp, -1, sizeof(tp));     for (int i = 1; i <= n; i++) {       if (m < 30) {         ed = 0;         while (i <= n) tmp[++ed] = a[i], i++;         for (int ii = 1; ii <= 3; ii++)           if (~tp[ii]) tmp[++ed] = node(ii, tp[ii]), m += ii, ans -= tp[ii];         ans += DP();         break;       }       ans += a[i].c;       m -= a[i].w;       tp[a[i].w] = a[i].c;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};   int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};   template <class T>   inline T biton(T n, T pos) {     return n | ((T)1 << pos);   }   template <class T>   inline T bitoff(T n, T pos) {     return n & ~((T)1 << pos);   }   template <class T>   inline T ison(T n, T pos) {     return (bool)(n & ((T)1 << pos));   }   template <class T>   inline T gcd(T a, T b) {     while (b) {       a = a % b;       swap(a, b);     }     return a;   }   template <class T>   inline T bigmod(T p, T e, T m) {     T ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p) % m;       p = (p * p) % m;     }     return (T)ret;   };   void solve() {     string s, c;     cin >> s >> c;     if (s < c) {       cout << s << endl;       return;     }     int n = (int)s.size();     map<char, vector<int>> mn;     map<char, set<char>> st;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (s[i] >= s[j]) {           if (st[s[i]].count(s[j]) == 0) {             mn[s[i]].push_back({j});             st[s[i]].insert(s[j]);           }         }       }     }     for (int i = 0; i < n; i++) {       for (auto m : mn) {         for (auto j : m.second) {           swap(s[i], s[j]);           if (s < c) {             cout << s << endl;             return;           }           swap(s[i], s[j]);         }       }     }     cout <<  ---  << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cout.setf(ios::fixed);     cout.precision(10);     cout.tie(nullptr);     cin.tie(nullptr);     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, t;     cin >> s;     for (int i = s.length() - 1; i >= 0; i--) {       if (t ==   ) {         t.push_back(s[i]);       } else if (t.back() <= s[i]) {         t.push_back(s[i]);       }     }     reverse(t.begin(), t.end());     cout << t;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   #pragma GCC optimize( unroll-loops )   using namespace std;   const long double eps = 1e-7;   const int inf = 1000000010;   const long long INF = 10000000000000010LL;   const int mod = 1000000007;   const int MAXN = 100010, LOG = 20;   int n, m, k, q, u, v, x, y, t, a, b, ans;   int stt[MAXN], fnt[MAXN], ras[MAXN], timer = 1;   int sz[MAXN], par[MAXN], head[MAXN], h[MAXN];   pair<long long, int> seg[MAXN << 2];   long long lazy[MAXN << 2];   set<int> st[MAXN];   vector<int> G[MAXN];   int dfs1(int node) {     h[node] = h[par[node]] + 1;     for (int v : G[node])       if (v != par[node]) {         par[v] = node;         sz[node] += dfs1(v);       }     return ++sz[node];   }   void dfs2(int node, int hd) {     head[node] = hd;     stt[node] = timer++;     ras[stt[node]] = node;     int big = 0;     for (int v : G[node])       if (v != par[node] && sz[v] > sz[big]) big = v;     if (big) dfs2(big, hd);     for (int v : G[node])       if (v != par[node] && v != big) dfs2(v, v);     fnt[node] = timer;   }   pair<long long, int> Build(int id, int tl, int tr) {     if (tr - tl == 1) return seg[id] = {INF, ras[tl]};     int mid = (tl + tr) >> 1;     return seg[id] = min(Build(id << 1, tl, mid), Build(id << 1 | 1, mid, tr));   }   void add_lazy(int id, long long val) {     seg[id].first += val;     lazy[id] += val;   }   void shift(int id) {     if (!lazy[id]) return;     add_lazy(id << 1, lazy[id]);     add_lazy(id << 1 | 1, lazy[id]);     lazy[id] = 0;   }   void Add(int id, int tl, int tr, int l, int r, long long val) {     if (r <= tl || tr <= l) return;     if (l <= tl && tr <= r) {       add_lazy(id, val);       return;     }     shift(id);     int mid = (tl + tr) >> 1;     Add(id << 1, tl, mid, l, r, val);     Add(id << 1 | 1, mid, tr, l, r, val);     seg[id] = min(seg[id << 1], seg[id << 1 | 1]);   }   pair<long long, int> Get(int id, int tl, int tr, int l, int r) {     if (r <= tl || tr <= l) return seg[0];     if (l <= tl && tr <= r) return seg[id];     shift(id);     int mid = (tl + tr) >> 1;     return min(Get(id << 1, tl, mid, l, r), Get(id << 1 | 1, mid, tr, l, r));   }   void Set(int id, int tl, int tr, int pos, long long val) {     if (pos < tl || tr <= pos) return;     if (tr - tl == 1) {       seg[id].first = val;       return;     }     shift(id);     int mid = (tl + tr) >> 1;     Set(id << 1, tl, mid, pos, val);     Set(id << 1 | 1, mid, tr, pos, val);     seg[id] = min(seg[id << 1], seg[id << 1 | 1]);   }   pair<long long, int> GetPath(int u, int v) {     pair<long long, int> res = {INF, -1};     while (head[u] != head[v]) {       if (h[head[u]] > h[head[v]]) swap(u, v);       res = min(res, Get(1, 1, n + 1, stt[head[v]], stt[v] + 1));       v = par[head[v]];     }     if (h[u] > h[v]) swap(u, v);     res = min(res, Get(1, 1, n + 1, stt[u], stt[v] + 1));     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> q;     for (int i = 1; i < n; i++) {       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     for (int i = 1; i <= m; i++) cin >> x, st[x].insert(i);     dfs1(1);     dfs2(1, 1);     Build(1, 1, n + 1);     seg[0] = {INF, 0};     for (int i = 1; i <= n; i++)       if (st[i].size()) Set(1, 1, n + 1, stt[i], *st[i].begin());     while (q--) {       cin >> t;       if (t == 2) {         cin >> v >> x;         Add(1, 1, n + 1, stt[v], fnt[v], x);         continue;       }       vector<int> out;       cin >> u >> v >> k;       while (k--) {         pair<long long, int> p = GetPath(u, v);         if (p.first >= INF) break;         int v = p.second, id = *st[v].begin();         out.push_back(id);         st[v].erase(id);         if (st[v].empty())           Set(1, 1, n + 1, stt[v], INF);         else           Add(1, 1, n + 1, stt[v], stt[v] + 1, *st[v].begin() - id);       }       cout << out.size() <<    ;       for (int i : out) cout << i <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int h = 17, N = 1 << h;   long long t[2 * N];   long long k;   long long product(int a, int b) {     if (a > b) return 1;     if (a % 2 == 1) return (t[a] * product(a + 1, b)) % k;     if (b % 2 == 0) return (t[b] * product(a, b - 1)) % k;     return product(a / 2, b / 2);   }   int main() {     int n;     scanf( %d%lld , &n, &k);     for (int i = 1; i < 2 * N; ++i) t[i] = 1;     for (int i = 0; i < n; ++i) {       scanf( %lld , &t[N + i]);       t[N + i] = t[N + i] % k;     }     for (int i = N - 1; i >= 1; --i) t[i] = (t[2 * i] * t[2 * i + 1]) % k;     long long m = 0;     for (int i = 0; i < n; ++i) {       int lo = i, hi = n - 1;       while (lo < hi) {         int mid = lo + (hi - lo) / 2;         if (product(N + i, N + mid) == 0)           hi = mid;         else           lo = mid + 1;       }       if (lo >= n || product(N + i, N + lo) != 0) break;       m += static_cast<long long>(n) - lo;     }     printf( %lld n , m);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10, mod = 1e9 + 7;   int head[N], nxt[N << 1], to[N << 1], num = 0, n, b[N], fa[N], q[N], DFN = 0,                                         k[N], in[N];   inline void link(int x, int y) {     nxt[++num] = head[x];     to[num] = y;     head[x] = num;   }   inline int qm(int x, int k) {     int sum = 1;     while (k) {       if (k & 1) sum = 1ll * sum * x % mod;       x = 1ll * x * x % mod;       k >>= 1;     }     return sum;   }   inline int inv(int x) { return qm(x, mod - 2); }   inline void dfs(int x, int last) {     q[++DFN] = x;     for (int i = head[x]; i; i = nxt[i])       if (to[i] != last) fa[to[i]] = x, dfs(to[i], x);   }   int main() {     int x, y, z;     scanf( %d , &n);     for (int i = 1; i < n; i++) {       scanf( %d%d%d , &x, &y, &z);       x++;       y++;       link(x, y);       link(y, x);       k[x]++;       k[y]++;       b[x] += z;       b[y] += z;     }     for (int i = 1; i <= n; i++) in[i] = k[i];     dfs(1, 1);     for (int i = n; i >= 1; i--) {       int x = q[i];       if (in[x] == 1) continue;       k[fa[x]] = (k[fa[x]] - inv(k[x])) % mod;       b[fa[x]] = (b[fa[x]] + 1ll * inv(k[x]) * b[x]) % mod;     }     b[1] = 1ll * b[1] * inv(k[1]) % mod;     if (b[1] < 0) b[1] += mod;     printf( %d n , b[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y;     cin >> n >> x >> y;     int a[n + 1];     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= n; i++) {       int flag = 1;       for (int j = i - 1; j >= 1 && j >= i - x; j--) {         if (a[i] > a[j]) {           flag = 0;           break;         }       }       for (int j = i + 1; j <= n && j <= i + y; j++) {         if (a[i] > a[j]) {           flag = 0;           break;         }       }       if (flag == 1) {         cout << i << endl;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using pr = pair<int, int>;   using prll = pair<long, long>;   using V = vector<int>;   using Vll = vector<long long>;   template <typename T, typename TT>   pr operator+(pair<T, TT> a, pair<T, TT> b) {     return {a.first + b.first, a.second + b.second};   }   template <typename T, typename TT>   ostream& operator<<(ostream& s, pair<T, TT> t) {     return s <<  (  << t.first <<  ,  << t.second <<  ) ;   }   template <typename T>   ostream& operator<<(ostream& s, vector<T> t) {     for (auto itr : t) s << itr <<    ;     return s;   }   int n, m;   vector<pair<int, int>> a(200000);   vector<long long> c(100000);   int binSearch(int x) {     int beg = 0;     int last = m - 1;     while (beg < last) {       int mid = beg + (last - beg + 1) / 2;       if (a[mid].first < x)         last = mid - 1;       else         beg = mid;     }     if (a[beg].first < x) return -1;     return beg;   }   bool cmp(pr a, pr b) {     if (a.first == b.first) return a.second > b.second;     return a.first > b.first;   }   int main() {     ios_base::sync_with_stdio(NULL);     cin.tie(0);     cout.tie(0);     int T;     cin >> T;     while (T--) {       cin >> n >> m;       for (int i = 0; i < int(m); i++) {         int x, y;         cin >> x >> y;         a[i] = {x, y};       }       sort(a.begin(), a.begin() + m, cmp);       c[0] = a[0].first;       for (int i = 0; i < int(m - 1); i++) c[i + 1] = c[i] + a[i + 1].first;       ll ans = INT_MIN;       ans = c[min(n, m) - 1];       for (int i = 0; i < int(m); i++) {         int beg = binSearch(a[i].second);         int t = min(beg, n - 3);         if (beg >= i) t = min(beg, n - 2);         if (t < 0)           ans = max(ans, a[i].first + (n - 1) * 1LL * a[i].second);         else if (t >= i)           ans = max(ans, c[t] + (n - t - 1) * 1LL * a[i].second);         else           ans = max(ans, c[t] + (n - t - 2) * 1LL * a[i].second + a[i].first);       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, Q, ans[100005], t[265000];   vector<int> v[100005];   vector<pair<int, int> > vq[100005];   void build(int k, int l, int r) {     t[k] = 1e9;     if (l == r) return;     int mid = (l + r) / 2;     build(k * 2, l, mid);     build(k * 2 + 1, mid + 1, r);   }   void pushdown(int k) {     t[k * 2] = min(t[k * 2], t[k]);     t[k * 2 + 1] = min(t[k * 2 + 1], t[k]);   }   void change(int k, int l, int r, int x, int y, int v) {     if (t[k] <= v) return;     if (l == x && r == y) {       t[k] = v;       return;     }     int mid = (l + r) / 2;     pushdown(k);     if (y <= mid)       change(k * 2, l, mid, x, y, v);     else if (x > mid)       change(k * 2 + 1, mid + 1, r, x, y, v);     else       change(k * 2, l, mid, x, mid, v),           change(k * 2 + 1, mid + 1, r, mid + 1, y, v);     t[k] = max(t[k * 2], t[k * 2 + 1]);   }   int ask(int k, int l, int r, int p, int v) {     if (t[k] <= v) return -1;     if (p <= l) {       for (; l < r;) {         int mid = (l + r) / 2;         pushdown(k);         k *= 2;         if (t[k] > v)           r = mid;         else           l = mid + 1, k++;       }       return l;     }     int mid = (l + r) / 2;     int tmp = -1;     if (p <= mid) tmp = ask(k * 2, l, mid, p, v);     if (tmp != -1) return tmp;     return ask(k * 2 + 1, mid + 1, r, p, v);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       if (x < y) v[x].push_back(y);     }     scanf( %d , &Q);     for (int i = 1; i <= Q; i++) {       int x, y;       scanf( %d%d , &x, &y);       vq[x].push_back(make_pair(y, i));     }     build(1, 1, n);     for (int i = n; i; i--) {       for (int j = 0; j < v[i].size(); j++)         change(1, 1, n, i, v[i][j] - 1, v[i][j]);       for (int j = 0; j < vq[i].size(); j++)         ans[vq[i][j].second] = ask(1, 1, n, i, vq[i][j].first);     }     for (int i = 1; i <= Q; i++) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m;   vector<long long int> ar[100001];   vector<long long int> res[2];   long long int vis[100001];   long long int col[100001];   bool dfs(long long int node, long long int c) {     vis[node] = 1;     res[c].push_back(node);     col[node] = c;     for (long long int child : ar[node]) {       if (vis[child] == 0) {         if (dfs(child, c ^ 1) == false) return false;       } else if (col[node] == col[child])         return false;     }     return true;   }   void solve() {     long long int test = 1;     while (test--) {       cin >> n >> m;       while (m--) {         long long int a, b;         cin >> a >> b;         ar[a].push_back(b);         ar[b].push_back(a);       }       long long int count = 1;       vector<long long int> von;       memset(vis, 0, sizeof(vis));       for (long long int i = 1; i <= n; i++) {         if (vis[i] == 0) {           if (ar[i].empty()) continue;           if (dfs(i, 0) == false) {             cout << -1 << endl;             return;           }         }       }       for (long long int i = 0; i < 2; i++) {         cout << res[i].size() << endl;         for (auto x : res[i]) cout << x <<    ;         cout << endl;       }     }     return;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (a == 0 || b == 0) return a + b;     if (a < b) return gcd(b % a, a);     return gcd(a % b, b);   }   int a[111];   int main() {     int n;     cin >> n;     int g = 0, max = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       g = gcd(g, a[i]);       if (max < a[i]) max = a[i];     }     for (int i = 0; i < n; i++) {       a[i] /= g;     }     max /= g;     int moves = max - n;     if (moves % 2)       cout <<  Alice ;     else       cout <<  Bob ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   vector<long long> p;   int main() {     int n, q;     cin >> n >> q;     string s;     cin >> s;     p.resize(1e5 + 1);     p[0] = 1;     for (int i = 1; i <= 1e5; i++) {       p[i] = (p[i - 1] * 2) % MOD;     }     vector<long long> kum(n + 1);     kum[0] = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  1 ) {         kum[i + 1] = kum[i] + 1;       } else {         kum[i + 1] = kum[i];       }     }     for (int i = 0; i < q; i++) {       int l, r;       cin >> l >> r;       long long kol_1 = kum[r] - kum[l - 1];       long long kol_0 = r - l + 1 - kol_1;       long long ans_1 = p[kol_1] - 1;       long long ans_0 = ans_1 * (p[kol_0] - 1);       cout << (ans_1 + ans_0) % MOD <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   priority_queue<int> a, b;   int c = 0;   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       a.push(x);     }     cin >> n;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       b.push(x);     }     while (!b.empty() && !a.empty()) {       if (abs(a.top() - b.top()) < 2) {         c++;         a.pop();         b.pop();         continue;       }       if (a.top() > b.top())         a.pop();       else         b.pop();     }     cout << c;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> v;   int n, k;   bool check(long long m) {     long long sum = 0;     for (int i = n / 2; i < n; i++) {       sum += max(m - v[i], 0ll);     }     if (sum <= k) {       return true;     }     return false;   }   int main() {     cin >> n >> k;     v.resize(n);     for (int i = 0; i < n; i++) {       cin >> v[i];     }     sort(v.begin(), v.end());     long long l = v[n / 2];     long long r = 1e10;     while (l + 1 < r) {       long long m = (l + r) / 2;       if (check(m)) {         l = m;       } else {         r = m;       }     }     cout << l;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void reverseStr(string& str);   void reverseQueue(queue<int>& Queue);   void print_queue(queue<int>& q);   int n, a[250], sum, num;   int main() {     cin >> n;     for (int i = 0; i < n * 2; i++) cin >> a[i];     for (int i = 0; i < n * 2; i++) {       for (int j = i + 2; j < n * 2; j++) {         if (a[i] == a[j]) {           sum += j - i - 1;           for (int l = j; l > i; l--) {             swap(a[l], a[l - 1]);           }         }       }     }     cout << sum;   }   void reverseStr(string& str) {     int n = str.length();     for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]);   }   void reverseQueue(queue<int>& Queue) {     stack<int> Stack;     while (!Queue.empty()) {       Stack.push(Queue.front());       Queue.pop();     }     while (!Stack.empty()) {       Queue.push(Stack.top());       Stack.pop();     }   }   void print_queue(queue<int>& q) {     while (!q.empty()) {       cout << q.front() <<    ;       q.pop();     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 1e5 + 7;   vector<long long> v[MX];   bool vis[MX];   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long n;     cin >> n;     pair<long long, long long> p[n];     for (long long i = 0; i < n; i++) cin >> p[i].first >> p[i].second;     long double l = 0, r = 1e10;     for (long long i = 0; i < 100; i++) {       long long cnt;       long double mid = (l + r) / 2, d = -1e20;       for (cnt = 0; cnt < n; cnt++) {         if (p[cnt].second > 0)           d = max(d, p[cnt].first + mid * p[cnt].second);         else if (p[cnt].first + mid * p[cnt].second <= d)           break;       }       if (cnt < n)         r = mid;       else         l = mid;     }     if (r == 1e10)       cout << -1;     else       cout << fixed << setprecision(18) << l;   }


#include <bits/stdc++.h>   using namespace std;   int t, ans, tot, pus, a, b, c, d;   int main() {     register int i;     scanf( %d , &t);     while (t--) {       scanf( %d%d%d%d , &a, &b, &c, &d);       ans = a * (b - d - 1);       ans = ((ans) > (a * d) ? (ans) : (a * d));       ans = ((ans) > (b * (a - c - 1)) ? (ans) : (b * (a - c - 1)));       ans = ((ans) > (b * c) ? (ans) : (b * c));       printf( %d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> factor[100001];   void precal() {     for (int i = (1); i < (100001); ++i)       for (int j = (i); j < (100001); j += (i)) factor[j].push_back(i);   }   int expo(int a, int b) {     if (b == 0) return 1;     if (b == 1) return a % 1000000007;     int c = expo(a, b >> 1);     c = ((long long)c * c) % 1000000007;     if (b & 1) c = ((long long)c * a) % 1000000007;     return c;   }   int main(int argc, char **argv) {     int N;     scanf( %d , &N);     vector<int> A(N);     for (int i = (0); i < (N); ++i) scanf( %d , &A[i]);     sort(A.begin(), A.end());     precal();     int res = 0;     for (int f = (1); f < (100001); ++f) {       int numFactor = factor[f].size();       int bound[numFactor];       int cur = 0;       for (int i = (0); i < (numFactor); ++i) {         int lo = cur, hi = N - 1;         int nextFactor = (i + 1 == numFactor) ? (1 << 29) : factor[f][i + 1];         while (lo <= hi) {           int mi = (lo + hi) / 2;           if (A[mi] >= nextFactor)             hi = mi - 1;           else             lo = mi + 1;         }         bound[i] = lo - cur;         cur = lo;       }       if (!bound[numFactor - 1]) continue;       int countWays = 1;       for (int i = (0); i < (numFactor - 1); ++i)         countWays = ((long long)countWays * expo(i + 1, bound[i])) % 1000000007;       countWays = ((long long)countWays *                    (((long long)expo(numFactor, bound[numFactor - 1]) +                      1000000007 - expo(numFactor - 1, bound[numFactor - 1])) %                     1000000007)) %                   1000000007;       res = (res + countWays) % 1000000007;     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct inf {     long long t, w;   };   struct djk {     long long n, v;     bool operator>(const djk &i) const { return v < i.v; }     bool operator<(const djk &i) const { return v > i.v; }   } n;   struct edge {     long long n1, n2, t;     bool operator<(const edge &i) const { return t < i.t; }   };   long long nodes, edges, n1, n2, weight, nn, totes, p1, p2, ports;   long long isp[100005], cp[100005], pd[100005], rnk[100005], head[100005];   edge elist[100005];   vector<inf> nlist[100005];   priority_queue<djk> viser;   long long ufind(long long n) {     if (head[n] != n) head[n] = ufind(head[n]);     return head[n];   }   void uni(long long n1, long long n2) {     p1 = ufind(n1);     p2 = ufind(n2);     if (rnk[p1] > rnk[p2])       head[p1] = p2;     else if (rnk[p2] > rnk[p1])       head[p2] = p1;     else       head[p1] = p2, rnk[p1]++;   }   int main() {     scanf( %I64d%I64d , &nodes, &edges);     for (int i = 1; i <= nodes; i++) pd[i] = -1, head[i] = i, rnk[i] = 1;     for (int i = 1; i <= edges; i++) {       scanf( %I64d%I64d%I64d , &n1, &n2, &weight);       nlist[n1].push_back({n2, weight});       nlist[n2].push_back({n1, weight});       elist[i] = {n1, n2, weight};     }     scanf( %I64d , &ports);     for (int i = 1; i <= ports; i++) {       scanf( %I64d , &n1);       isp[n1] = 1;       viser.push({n1, 0});       pd[n1] = 0;       cp[n1] = n1;     }     while (!viser.empty()) {       n = viser.top();       viser.pop();       if (pd[n.n] > n.v) continue;       for (int i = 0; i < nlist[n.n].size(); i++) {         nn = nlist[n.n][i].t;         if (pd[nn] == -1 || pd[nn] > nlist[n.n][i].w + pd[n.n]) {           pd[nn] = nlist[n.n][i].w + pd[n.n];           cp[nn] = cp[n.n];           viser.push({nn, pd[nn]});         }       }     }     for (int i = 1; i <= edges; i++)       elist[i].t += pd[elist[i].n1] + pd[elist[i].n2];     sort(elist + 1, elist + 1 + edges);     for (int i = 1; i <= edges; i++) {       if (ufind(cp[elist[i].n1]) != ufind(cp[elist[i].n2])) {         uni(cp[elist[i].n1], cp[elist[i].n2]);         totes += elist[i].t;       }     }     if (!isp[1] && pd[1] != -1) totes += pd[1];     printf( %I64d n , totes);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 55;   const int INF = 1 << 20;   bool dp1[MAXN][MAXN][26] = {false};   bool dp2[MAXN][MAXN][26] = {false};   int dp[MAXN][MAXN];   char s1[MAXN], s2[MAXN];   string tr[MAXN];   int n;   void cal(char* s, bool f[MAXN][MAXN][26]) {     int len = strlen(s);     for (int i = 0; i < len; i++) {       f[i][i][s[i] -  a ] = true;     }     for (int L = 1; L <= len; L++) {       for (int i = 0; i + L - 1 < len; i++) {         int j = i + L - 1;         for (int k = i; k < j; k++) {           for (int c = 0; c < n; c++) {             if (f[i][k][tr[c][3] -  a ] && f[k + 1][j][tr[c][4] -  a ]) {               f[i][j][tr[c][0] -  a ] = true;             }           }         }       }     }   }   int main() {     cin >> s1 >> s2;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> tr[i];     }     cal(s1, dp1);     cal(s2, dp2);     int len1 = strlen(s1);     int len2 = strlen(s2);     for (int i = 0; i <= len1; i++)       for (int j = 0; j <= len2; j++) dp[i][j] = INF;     dp[0][0] = 0;     for (int i = 0; i < len1; i++)       for (int ii = i; ii < len1; ii++)         for (int c = 0; c < 26; c++)           if (dp1[i][ii][c])             for (int j = 0; j < len2; j++)               for (int jj = j; jj < len2; jj++)                 if (dp2[j][jj][c])                   dp[ii + 1][jj + 1] = min(dp[ii + 1][jj + 1], dp[i][j] + 1);     int ans = (dp[len1][len2] == INF ? -1 : dp[len1][len2]);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   void c_p_c() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   long long me(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   int main() {     c_p_c();     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long a[n];       long long b[n];       for (long long i = 0; i < n; i++) cin >> a[i];       for (long long i = 0; i < n; i++) cin >> b[i];       long long c[n];       multiset<long long> s;       long long l = 0;       vector<long long> v;       long long g = 0;       long long temp = 0;       long long g1 = 0;       for (long long i = 0; i < n; i++) {         c[i] = b[i] - a[i];         if (c[i] == 0 && g1 == 1) {           break;         }         if (c[i] < 0) {           cout <<  NO n ;           l = 1;           break;         }         if (c[i] != 0 && (temp == c[i] || g == 0)) {           a[i] = a[i] + c[i];           temp = c[i];           g = 1;           g1 = 1;         }       }       if (l == 1) {         continue;       }       long long flg = 0;       for (long long i = 0; i < n; i++) {         if (a[i] != b[i]) {           flg = 1;           break;         }       }       if (flg == 1) {         cout <<  NO n ;       } else {         cout <<  YES n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t, n, k, c = 0, d = 0;     cin >> n >> k;     map<long long, long long> m;     for (int i = 0; i < n; ++i) {       cin >> t;       m[t]++;     }     auto it2 = m.begin();     for (auto it = m.begin(); it != m.end(); ++it) {       it2 = it;       it2++;       t = it->first;       if (it2 == m.end()) {         c += it->second;         break;       }       d = it2->first;       if (t + k < d) c += it->second;     }     cout << c;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T sqr(T a) {     return a * a;   }   int n;   int a[21000];   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     int best = -1000000000;     for (int i = 3; i <= n; i++)       if (n % i == 0) {         int g = n / i;         for (int j = 0; j < g; j++) {           int cur = 0;           for (int k = j; k < n; k += g) cur += a[k];           best = max(cur, best);         }       }     cout << best;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long INFL = 0x3f3f3f3f3f3f3f3fLL;   struct Edge {     int src, dst;     int weight;     Edge(int src_, int dst_, int weight_)         : src(src_), dst(dst_), weight(weight_) {}     Edge(int dst_, int weight_) : src(-2), dst(dst_), weight(weight_) {}   };   bool operator<(const Edge &e, const Edge &f) {     return e.weight != f.weight ? e.weight > f.weight : e.dst < f.dst;   }   void dijkstra(const vector<vector<Edge> > &g, int start, vector<int> &dist) {     int n = g.size();     dist.assign(n, INF);     dist[start] = 0;     vector<int> prev(n, -1);     priority_queue<Edge> q;     for (q.push(Edge(-2, start, 0)); !q.empty();) {       Edge e = q.top();       q.pop();       if (prev[e.dst] != -1) continue;       prev[e.dst] = e.src;       for (typeof((g[e.dst]).begin()) f = ((g[e.dst]).begin());            f != (g[e.dst]).end(); ++f) {         if (dist[f->dst] > e.weight + f->weight) {           dist[f->dst] = e.weight + f->weight;           q.push(Edge(f->src, f->dst, e.weight + f->weight));         }       }     }   }   int main() {     int n;     int x0, y0, x1, y1;     vector<pair<int, pair<int, int> > > v;     cin >> y0 >> x0 >> y1 >> x1;     cin >> n;     for (int(i) = 0; (i) < (int)(n); ++(i)) {       int o, a, b;       cin >> o >> a >> b;       v.push_back(make_pair((o), (make_pair((a), (b)))));     }     sort((v).begin(), (v).end());     map<pair<int, int>, int> m;     int t = 0;     static const int dy[4] = {0, -1, -1, -1}, dx[4] = {-1, 0, -1, 1};     vector<vector<Edge> > g;     for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) {       for (int(j) = (int)(i->second.first); (j) <= (int)(i->second.second);            ++(j)) {         if (m.count(make_pair((i->first), (j)))) continue;         int q = m[make_pair((i->first), (j))] = t++;         g.push_back(vector<Edge>());         for (int(d) = 0; (d) < (int)(4); ++(d))           if (m.count(make_pair((i->first + dy[d]), (j + dx[d])))) {             g[q].push_back(                 Edge(q, m[make_pair((i->first + dy[d]), (j + dx[d]))], 1));           }       }     }     vector<vector<Edge> > gg(t);     for (int(i) = 0; (i) < (int)(t); ++(i))       for (typeof((g[i]).begin()) j = ((g[i]).begin()); j != (g[i]).end(); ++j) {         gg[j->src].push_back(Edge(j->src, j->dst, 1));         gg[j->dst].push_back(Edge(j->dst, j->src, 1));       }     int r = -1;     if (!m.count(make_pair((y0), (x0))))       r = -999;     else if (!m.count(make_pair((y1), (x1))))       r = -999;     else {       vector<int> d;       dijkstra(gg, m[make_pair((y0), (x0))], d);       r = d[m[make_pair((y1), (x1))]];       if (r == INF) r = -1;     }     cout << r << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr <<  [   << *it <<   =   << a <<   ]  ;     err(++it, args...);   }   template <typename Tk, typename Tv>   ostream& operator<<(ostream& os, const pair<Tk, Tv>& p) {     os <<  {  << p.first <<  ,  << p.second <<  } ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const vector<T>& p) {     os <<  [  ;     for (T x : p) os << x <<    ;     os <<  ]  <<   n ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const set<T>& p) {     os <<  {  ;     for (T x : p) os << x <<    ;     os <<  }  <<   n ;     return os;   }   template <typename Tk, typename Tv>   ostream& operator<<(ostream& os, const map<Tk, Tv>& p) {     os <<  {  ;     for (pair<Tk, Tv> x : p) os << x <<    ;     os <<  }  <<   n ;     return os;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t = 1;     cin >> t;     while (t--) {       long long int n, k, l;       cin >> n >> k >> l;       vector<long long int> depth(n), time(2 * k);       for (long long int i = 0; i <= k; i++) {         time[i] = i;       }       long long int ct = 1;       for (long long int i = 0; i < n; i++) {         cin >> depth[i];       }       for (long long int i = k - 1; i > 0; i--) {         time[k + (ct++)] = i;       }       bool dp[2 * k][n];       memset(dp, false, sizeof(dp));       for (long long int i = 0; i < 2 * k; i++) {         if (depth[0] + time[i] <= l) {           dp[i][0] = true;         }       }       for (long long int i = 0; i < n - 1; i++) {         if (depth[i] + k <= l) {           if (i == 0) {             for (long long int j = 0; j < 2 * k; j++) {               dp[j][i] = true;             }           } else {             bool find_true = false;             for (long long int j = 0; j < 2 * k; j++) {               if (dp[j][i]) {                 find_true = true;               }             }             if (find_true) {               for (long long int j = 0; j < 2 * k; j++) {                 dp[j][i] = true;               }             }           }         }         for (long long int j = 0; j < 2 * k; j++) {           if (dp[j][i] &&               (depth[i + 1] + (long long int)time[(j + 1) % (2 * k)] <= l)) {             dp[(j + 1) % (2 * k)][i + 1] = true;           }         }       }       bool pass = false;       for (long long int i = 0; i < 2 * k; i++) {         if (dp[i][n - 1]) {           pass = true;           break;         }       }       cout << (pass ?  YES n  :  NO n );     }     return 0;   }


#include <bits/stdc++.h>   int max(int a, int b) {     if (a > b) return a;     return b;   }   int min(int a, int b) {     if (a < b)       return a;     else       return b;   }   char s1[600000], s2[600000];   int z[600000], y[600000];   struct student {     int l;     int r;   } a[600000];   int main() {     int i;     int n, m, p;     int j;     scanf( %d%d%d , &n, &m, &p);     for (i = 0; i <= n + 1; i++) {       a[i].l = i - 1;       a[i].r = i + 1;     }     getchar();     gets(s1 + 1);     gets(s2 + 1);     int num = 0;     int len = strlen(s1 + 1);     for (i = 1; i <= len; i++) {       if (s1[i] ==  ( )         z[num++] = i;       else {         y[z[--num]] = i;         y[i] = z[num];       }     }     len = strlen(s2 + 1);     for (i = 1; i <= len; i++) {       if (s2[i] ==  R )         p = a[p].r;       else if (s2[i] ==  L )         p = a[p].l;       else {         a[a[max(p, y[p])].r].l = a[min(p, y[p])].l;         a[a[min(p, y[p])].l].r = a[max(p, y[p])].r;         a[min(p, y[p])].r = a[max(p, y[p])].r;         p = a[p].r;       }       if (p < 1) p = a[p].r;       if (p > n) p = a[p].l;     }     for (i = a[0].r; i <= n; i = a[i].r) {       printf( %c , s1[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   const int sieve_size = 5e6;   void solve() {     int n;     cin >> n;     vector<int> a(n);     for (int &x : a) cin >> x;     vector<int> primes;     vector<bool> sieve(sieve_size);     for (int i = 2; i < int((sieve).size()); i++) {       if (sieve[i]) continue;       primes.push_back(i);       for (int j = i + i; j < int((sieve).size()); j += i) {         sieve[j] = true;       }     }     auto get_decomp = [](int x) {       vector<int> decomp;       for (int j = 2; j * j <= x; j++) {         if (x % j == 0) {           decomp.push_back(j);           x /= j;           while (x % j == 0) x /= j;         }       }       if (x != 1) decomp.push_back(x);       return decomp;     };     set<int> occupied_primes;     vector<int> b(n);     bool a_coprime = true;     for (int i = 0, primes_idx = 0; i < n; i++) {       if (a_coprime) {         vector<int> decomp = get_decomp(a[i]);         for (int x : decomp) {           if (occupied_primes.count(x)) {             a_coprime = false;             break;           }         }         if (!a_coprime) {           for (a[i]++;; a[i]++) {             decomp = get_decomp(a[i]);             bool ok = true;             for (int x : decomp) {               if (occupied_primes.count(x)) {                 ok = false;                 break;               }             }             if (ok) break;           }         }         for (int x : decomp) occupied_primes.insert(x);         b[i] = a[i];       } else {         while (occupied_primes.count(primes[primes_idx])) primes_idx++;         b[i] = primes[primes_idx++];       }     }     for (int x : b) cout << x <<    ;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0);     int tcs = 1;     for (int tc = 1; tc <= tcs; tc++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, h;   long long l[200010], r[200010];   long long dsum[200010], rsum[200010];   int main() {     scanf( %lld%lld , &n, &h);     for (long long i = (long long)(0); i < (long long)(n); i++)       scanf( %lld%lld , l + i, r + i);     for (long long i = (long long)(0); i < (long long)(n - 1); i++)       dsum[i + 1] = dsum[i] + l[i + 1] - r[i];     for (long long i = (long long)(0); i < (long long)(n); i++)       rsum[i + 1] = rsum[i] + r[i] - l[i];     long long res = h;     for (long long i = (long long)(0); i < (long long)(n - 1); i++) {       long long lb = 0, ub = n;       while (ub - lb > 1) {         long long mid = (lb + ub) / 2;         if (dsum[mid] - dsum[i] < h)           lb = mid;         else           ub = mid;       }       res = (res > h + rsum[ub] - rsum[i] ? res : h + rsum[ub] - rsum[i]);     }     res = (res > h + r[n - 1] - l[n - 1] ? res : h + r[n - 1] - l[n - 1]);     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Act {     int l, r;     int mx;     double pb;     vector<Act *> son;     static int dp_size;     double dp[5005];     bool operator<(const Act &act) const {       if (l < act.l) {         return true;       } else if (l > act.l) {         return false;       } else if (r > act.r) {         return true;       }       return false;     }     bool contains(Act &act) { return act.l >= l && act.r <= r; }     double p(int profit) {       if (profit < 0) return 0.0;       if (profit > dp_size) return 1.0;       return dp[profit];     }   };   int Act::dp_size = 0;   int n, q;   int *a;   int **m;   int *lg;   Act act[5005];   void dfs(Act &act) {     for (int i = 0; i < act.son.size(); i++) {       dfs(*act.son[i]);     }     for (int i = 0; i <= Act::dp_size; i++) {       act.dp[i] = 0.0;     }     for (int i = 0; i <= Act::dp_size; i++) {       double p = (i == 0 ? 0.0 : act.pb);       double np = 1.0 - act.pb;       for (int j = 0; j < act.son.size(); j++) {         Act *son = act.son[j];         p *= son->p(act.mx - son->mx + i - 1);         np *= son->p(act.mx - son->mx + i);       }       act.dp[i] = p + np;     }   }   int main() {     cin >> n >> q;     a = new int[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     lg = new int[n + 1];     lg[1] = 0;     for (int i = 2; i <= n; i++) {       lg[i] = lg[i / 2] + 1;     }     m = new int *[lg[n] + 1];     for (int i = 0; i <= lg[n]; i++) {       m[i] = new int[n];     }     for (int i = 0; i < n; i++) {       m[0][i] = a[i];     }     for (int i = 1; i <= lg[n]; i++) {       int psz = 1 << (i - 1);       int nsz = 1 << i;       for (int j = 0; j + nsz <= n; j++) {         m[i][j] = max(m[i - 1][j], m[i - 1][j + psz]);       }     }     q++;     for (int i = 0; i < q; i++) {       if (i == q - 1) {         act[i].l = 1;         act[i].r = n;         act[i].pb = 0.0;       } else {         cin >> act[i].l >> act[i].r >> act[i].pb;       }       act[i].l--, act[i].r--;       int tlg = lg[act[i].r - act[i].l + 1];       int tsz = 1 << tlg;       act[i].mx = max(m[tlg][act[i].l], m[tlg][act[i].r - tsz + 1]);     }     sort(act, act + q);     Act::dp_size = q;     for (int i = 1; i < q; i++) {       int j = i - 1;       while (!act[j].contains(act[i])) j--;       act[j].son.push_back(act + i);     }     dfs(act[0]);     double ans = 0.0;     for (int i = 0; i <= Act::dp_size; i++) {       ans += (act[0].mx + i) * (act[0].p(i) - act[0].p(i - 1));     }     printf( %.8lf , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long i, j, k, l, n, a, b, ans = 0;   int gcd(int a, int b) {     if (a < b) swap(a, b);     return (b == 0) ? a : gcd(b, a % b);   }   struct st {     int first;     long double second;   };   int main() {     std::ios_base::sync_with_stdio(0);     string s;     cin >> s;     n = s.size();     list<int> left, right;     j = 0;     for (auto i : s) {       if (i ==  l ) {         left.push_front(++j);       } else {         right.push_front(++j);       }     }     reverse(right.begin(), right.end());     for (auto i : right) cout << i <<   n ;     for (auto i : left) cout << i <<   n ;     fflush(stdin);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class _T>   inline _T sqr(const _T &x) {     return x * x;   }   template <class _T>   inline string tostr(const _T &a) {     ostringstream os(  );     os << a;     return os.str();   }   const long double PI = 3.1415926535897932384626433832795;   const long double EPS = 1e-11;   const int dx[4] = {1, 0, -1, 0};   const int dy[4] = {0, -1, 0, 1};   int n, m;   int a[2024][2024];   bool u[2024][2024];   int u2[2024][2024];   bool u3[2024][2024];   int d[2024][2024];   int q[2024 * 2024][2];   int e[1024000];   void bfs(int x, int y, signed long long &sx, signed long long &sy, int &s) {     int qb = 0, qe = 0;     s = 1;     sx = x;     sy = y;     u[x][y] = true;     q[qe][0] = x;     q[qe][1] = y;     qe++;     while (qb != qe) {       x = q[qb][0];       y = q[qb][1];       qb++;       for (int l = 0; l < (int)(4); l++) {         int xn = x + dx[l];         int yn = y + dy[l];         if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] || u[xn][yn])           continue;         u[xn][yn] = true;         q[qe][0] = xn;         q[qe][1] = yn;         sx += xn;         sy += yn;         s++;         qe++;       }     }   }   int bfs2(int x, int y, int col) {     if (!a[x][y])       for (;;)         ;     int qb = 0, qe = 0;     u2[x][y] = col;     d[x][y] = 0;     q[qe][0] = x;     q[qe][1] = y;     qe++;     while (qb != qe) {       x = q[qb][0];       y = q[qb][1];       qb++;       for (int l = 0; l < (int)(4); l++) {         int xn = x + dx[l];         int yn = y + dy[l];         if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] || u2[xn][yn])           continue;         d[xn][yn] = d[x][y] + 1;         u2[xn][yn] = col;         q[qe][0] = xn;         q[qe][1] = yn;         qe++;       }     }     return qe;   }   int main() {     cout << setiosflags(ios::fixed) << setprecision(10);     scanf( %d%d , &n, &m);     for (int i = 0; i < (int)(n); i++) {       for (int j = 0; j < (int)(m); j++) {         scanf( %d , &a[i][j]);       }     }     int cnt = 0;     memset((u), 0, sizeof(u));     memset((u2), 0, sizeof(u2));     memset((u3), 0, sizeof(u3));     for (int i = 0; i < (int)(n); i++) {       for (int j = 0; j < (int)(m); j++) {         if (a[i][j] && !u[i][j]) {           signed long long xs, ys;           int s;           bfs(i, j, xs, ys, s);           int t = bfs2(xs / s, ys / s, cnt + 1);           int z = 0;           for (int k = 0; k < (int)(t); k++) {             int x = q[k][0];             int y = q[k][1];             bool bb = false;             for (int l = 0; l < (int)(4); l++) {               int xn = x + dx[l];               int yn = y + dy[l];               if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn]) continue;               if (d[xn][yn] > d[x][y]) bb = true;             }             if (!u3[x][y] && bb) {               int w = 0;               for (int di = (-4); di <= (int)(4); di++) {                 for (int dj = (-4); dj <= (int)(4); dj++) {                   int xn = x + di;                   int yn = y + dj;                   if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] ||                       u2[xn][yn] != cnt + 1)                     continue;                   w++;                 }               }               if (w < 27) {                 z++;                 for (int di = (-4); di <= (int)(4); di++) {                   for (int dj = (-4); dj <= (int)(4); dj++) {                     int xn = x + di;                     int yn = y + dj;                     if (xn < 0 || xn >= n || yn < 0 || yn >= m || !a[xn][yn] ||                         u3[xn][yn])                       continue;                     u3[xn][yn] = true;                   }                 }               }             }           }           e[cnt] = z;           cnt++;         }       }     }     printf( %d n , cnt);     sort(e, e + cnt);     for (int i = 0; i < (int)(cnt); i++) {       if (i) putchar(   );       printf( %d , e[i]);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[105], ans, n;   int main() {     scanf( %lld , &n);     for (long long i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       ans += a[i] * i;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, q;     std::cin >> n >> q;     int ans[q], servers[n], server;     for (server = 0; server < n; ++server) {       servers[server] = 0;     }     int index, t, k, d, planner[n];     for (int i = 0; i < q; ++i) {       server = 0;       index = 0;       std::cin >> t >> k >> d;       while (index < k && server < n) {         if (servers[server] <= t) {           planner[index++] = server;           servers[server] = t;         }         ++server;       }       if (index == k) {         ans[i] = k;         for (index = 0; index < k; ++index) {           ans[i] += planner[index];           servers[planner[index]] += d;         }       } else {         ans[i] = -1;       }     }     for (int a : ans) {       std::cout << a << std::endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       vector<int> sml, big;       for (int i = 1, j = n; i < j; i++, j--) {         printf( ? %d %d n , i, j);         fflush(stdout);         char c[5];         scanf( %s , c);         if (c[0] ==  < ) {           sml.push_back(i);           big.push_back(j);         }         if (c[0] ==  > ) {           sml.push_back(j);           big.push_back(i);         }         if (c[0] ==  = ) {           sml.push_back(i);           big.push_back(j);         }       }       if (n % 2) {         big.push_back(ceil(n / 2.0));         sml.push_back(ceil(n / 2.0));       }       int mn = sml[0];       for (int i = 1; i < sml.size(); i++) {         printf( ? %d %d n , mn, sml[i]);         fflush(stdout);         char c[5];         scanf( %s , c);         if (c[0] ==  > ) {           mn = sml[i];         }       }       int mx = big[0];       for (int i = 1; i < big.size(); i++) {         printf( ? %d %d n , mx, big[i]);         fflush(stdout);         char c[5];         scanf( %s , c);         if (c[0] ==  < ) {           mx = big[i];         }       }       printf( ! %d %d n , mn, mx);       fflush(stdout);     }   }


#include <bits/stdc++.h>   using namespace std;   int mpow(int base, int exp);   void ipgraph(int m);   void dfs(int u, int par);   const long long mod = 1000000007 * 1LL * 1000000007;   const int N = 1e6 + 3, M = N;   vector<int> g[N];   int a[N];   long long h[N], val[N];   vector<pair<int, int> > E;   long long r() { return rand() * 32000 + rand(); }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int i, n, k, j, m;     cin >> n >> m;     srand(1234);     for (i = 1; 1 < n + 1 ? i < n + 1 : i > n + 1; 1 < n + 1 ? i += 1 : i -= 1)       h[i] = (r() * r() + r() + r()) % mod;     ipgraph(m);     long long ans = 0;     for (pair<int, int> e : E) {       int u = e.first, v = e.second;       val[u] = (val[u] + mod - h[v]) % mod;       val[v] = (val[v] + mod - h[u]) % mod;       ans += val[u] == val[v];       val[u] = (val[u] + h[v]) % mod;       val[v] = (val[v] + h[u]) % mod;     }     vector<long long> fin;     for (i = 1; 1 < n + 1 ? i < n + 1 : i > n + 1; 1 < n + 1 ? i += 1 : i -= 1)       fin.push_back(val[i]);     sort(fin.begin(), fin.end());     long long c = 1;     for (i = 1; 1 < fin.size() ? i < fin.size() : i > fin.size();          1 < fin.size() ? i += 1 : i -= 1) {       if (fin[i] == fin[i - 1])         c++;       else         ans += c * (c - 1) / 2, c = 1;     }     ans += c * (c - 1) / 2;     cout << ans << endl;     return 0;   }   int mpow(int base, int exp) {     base %= mod;     int result = 1;     while (exp > 0) {       if (exp & 1) result = ((long long)result * base) % mod;       base = ((long long)base * base) % mod;       exp >>= 1;     }     return result;   }   void ipgraph(int m) {     int i, u, v;     for (i = 0; i < m; i++) {       cin >> u >> v;       val[u] = (val[u] + h[v]) % mod;       val[v] = (h[u] + val[v]) % mod;       E.push_back({u, v});       u++, v++;       g[u - 1].push_back(v - 1);       g[v - 1].push_back(u - 1);     }   }   void dfs(int u, int par) {     for (int v : g[u]) {       if (v == par) continue;       dfs(v, u);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, m, k;       cin >> n >> m >> k;       int card_to_person = n / k;       if (card_to_person >= m) {         cout << m <<   n ;         continue;       }       int tmp = (m - card_to_person) / (k - 1);       if ((m - card_to_person) % (k - 1) != 0) {         tmp++;       }       cout << card_to_person - tmp <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 1000005;   int N;   vector<int> G[MAX_N];   int cyid[MAX_N], Q[MAX_N];   vector<vector<int>> cycles, newcycles;   void find_cycle(int s, int curid) {     cyid[s] = curid;     cycles.back().push_back(s);     for (int i = 0; i < G[s].size(); i++) {       if (cyid[G[s][i]] == -1) {         find_cycle(G[s][i], curid);       }     }   }   void get_cycles() {     fill(cyid + 1, cyid + N + 1, -1);     int nCycles = 0;     for (int i = 1; i <= N; i++) {       if (cyid[i] == -1) {         cycles.push_back(vector<int>());         find_cycle(i, ++nCycles);       }     }   }   void construct_root_permutation() {     int numEven = 0, numOdd = 0;     for (vector<int> p : cycles) {       if (p.size() % 2 == 0)         numEven++;       else         numOdd++;     }     if (numEven % 2 == 1) {       printf( -1 n );       return;     }     sort(cycles.begin(), cycles.end(),          [](vector<int> a, vector<int> b) { return a.size() < b.size(); });     for (int i = 0; i < cycles.size();) {       if (cycles[i].size() % 2 == 1) {         if (cycles[i].size() == 1)           newcycles.push_back({cycles[i][0]});         else {           vector<int> newcyk;           int ptr1 = 0, ptr2 = (cycles[i].size() - 1) / 2 + 1;           for (int j = 0; j < cycles[i].size() / 2; j++) {             newcyk.push_back(cycles[i][ptr1++]);             newcyk.push_back(cycles[i][ptr2++]);           }           newcyk.push_back(cycles[i][ptr1]);           newcycles.emplace_back(newcyk);         }         i++;       } else {         if (i + 1 < cycles.size()) {           if (cycles[i].size() != cycles[i + 1].size()) {             printf( -1 n );             return;           } else {             vector<int> merged;             for (int j = 0; j < cycles[i].size(); j++) {               merged.push_back(cycles[i][j]);               merged.push_back(cycles[i + 1][j]);             }             newcycles.emplace_back(merged);             i += 2;           }         } else {           printf( -1 n );           return;         }       }     }     for (int i = 0; i < newcycles.size(); i++) {       if (newcycles[i].size() == 1)         Q[newcycles[i][0]] = newcycles[i][0];       else {         newcycles[i].push_back(newcycles[i][0]);         for (int j = 0; j < newcycles[i].size() - 1; j++) {           int next = newcycles[i][j + 1];           Q[newcycles[i][j]] = next;         }       }     }     for (int i = 1; i <= N; i++) printf( %d  , Q[i]);     printf(  n );   }   int main() {     scanf( %d , &N);     for (int i = 1; i <= N; i++) {       int pi;       scanf( %d , &pi);       G[i].push_back(pi);     }     get_cycles();     construct_root_permutation();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3fLL;   const double EPS = 1e-8;   const int MOD = 998244353;   const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};   int mod = MOD;   struct ModInt {     unsigned val;     ModInt() : val(0) {}     ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {}     ModInt pow(long long exponent) {       ModInt tmp = *this, res = 1;       while (exponent > 0) {         if (exponent & 1) res *= tmp;         tmp *= tmp;         exponent >>= 1;       }       return res;     }     ModInt &operator+=(const ModInt &rhs) {       if ((val += rhs.val) >= mod) val -= mod;       return *this;     }     ModInt &operator-=(const ModInt &rhs) {       if ((val += mod - rhs.val) >= mod) val -= mod;       return *this;     }     ModInt &operator*=(const ModInt &rhs) {       val = (unsigned long long)val * rhs.val % mod;       return *this;     }     ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); }     bool operator==(const ModInt &rhs) const { return val == rhs.val; }     bool operator!=(const ModInt &rhs) const { return val != rhs.val; }     bool operator<(const ModInt &rhs) const { return val < rhs.val; }     bool operator<=(const ModInt &rhs) const { return val <= rhs.val; }     bool operator>(const ModInt &rhs) const { return val > rhs.val; }     bool operator>=(const ModInt &rhs) const { return val >= rhs.val; }     ModInt operator-() const { return ModInt(-val); }     ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; }     ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; }     ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; }     ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; }     friend ostream &operator<<(ostream &os, const ModInt &rhs) {       return os << rhs.val;     }     friend istream &operator>>(istream &is, ModInt &rhs) {       long long x;       is >> x;       rhs = ModInt(x);       return is;     }       private:     ModInt inv() const {       unsigned a = val, b = mod;       int x = 1, y = 0;       while (b) {         unsigned tmp = a / b;         a -= tmp * b;         swap(a, b);         x -= tmp * y;         swap(x, y);       }       return ModInt(x);     }   };   ModInt abs(const ModInt &x) { return x.val; }   struct Combinatorics {     Combinatorics(int MAX = 5000000) {       MAX <<= 1;       fact.resize(MAX + 1);       fact_inv.resize(MAX + 1);       fact[0] = 1;       for (int i = (1); i < (MAX + 1); ++i) fact[i] = fact[i - 1] * i;       fact_inv[MAX] = ModInt(1) / fact[MAX];       for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i;     }     ModInt nCk(int n, int k) {       if (n < 0 || n < k || k < 0) return ModInt(0);       return fact[n] * fact_inv[k] * fact_inv[n - k];     }     ModInt nPk(int n, int k) {       if (n < 0 || n < k || k < 0) return ModInt(0);       return fact[n] * fact_inv[n - k];     }     ModInt nHk(int n, int k) {       if (n < 0 || k < 0) return ModInt(0);       return (k == 0 ? ModInt(1) : nCk(n + k - 1, k));     }       private:     vector<ModInt> fact, fact_inv;   };   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int n, m;     cin >> n >> m;     vector<int> a(n);     for (int i = (0); i < (n); ++i) cin >> a[i];     vector<int> w(n);     for (int i = (0); i < (n); ++i) cin >> w[i];     int dl = 0, l = 0;     for (int i = (0); i < (n); ++i) (a[i] == 0 ? dl : l) += w[i];     vector<vector<ModInt> > dp(m + 1, vector<ModInt>(m + 1, 0));     dp[0][0] = 1;     for (int i = (0); i < (m); ++i)       for (int j = (0); j < (m); ++j) {         int sum = l + dl - j + (i - j);         if (j > dl || j > i || sum <= 0) continue;         dp[i + 1][j + 1] += dp[i][j] * (dl - j) / sum;         dp[i + 1][j] += dp[i][j] * (l + (i - j)) / sum;       }     vector<ModInt> ans(2, 0);     ModInt tmp = 1;     for (int i = (0); i < (dl + 1); ++i) {       if (i > m) break;       ans[0] += tmp * dp[m][i];       if (i < dl) tmp *= (ModInt(1) - ModInt(1) / (dl - i));     }     tmp = 1;     for (int i = (0); i < (m + 1); ++i) {       ans[1] += tmp * dp[m][m - i];       tmp *= (ModInt(1) + ModInt(1) / (l + i));     }     for (int i = (0); i < (n); ++i) cout << ans[a[i]] * w[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   using vv = vector<vector<T>>;   template <class T>   inline bool MX(T &l, const T &r) {     return l < r ? l = r, 1 : 0;   }   template <class T>   inline bool MN(T &l, const T &r) {     return l > r ? l = r, 1 : 0;   }   const long long MOD = 1e9 + 7;   int N;   long long C[114514];   long long T[114514];   vector<long long> ds, es;   int main() {     scanf( %d , &N);     for (int(i) = 0; (i) < (N); ++(i)) {       scanf( %lld , &C[i]);       if (i > 0) ds.emplace_back(C[i] - C[i - 1]);     }     for (int(i) = 0; (i) < (N); ++(i)) {       scanf( %lld , &T[i]);       if (i > 0) es.emplace_back(T[i] - T[i - 1]);     }     sort((ds).begin(), (ds).end());     sort((es).begin(), (es).end());     if (C[0] != T[0]) {       puts( No );       return 0;     }     assert((int)ds.size() == N - 1);     assert((int)es.size() == N - 1);     for (int(i) = 0; (i) < (N - 1); ++(i)) {       if (ds[i] != es[i]) {         puts( No );         return 0;       }     }     puts( Yes );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N, M, x, y;     cin >> N >> M >> x >> y;     x--;     y--;     cout << x + 1 <<     << y + 1 << endl;     for (int i = 1; i < x; i++) cout << i + 1 <<     << y + 1 << endl;     for (int i = x + 1; i < N; i++) cout << i + 1 <<     << y + 1 << endl;     cout << 1 <<     << y + 1 << endl;     int uneven = 0;     for (int j = 0; j < M; j++) {       if (j == y) {         uneven = 1;         continue;       }       if (j % 2 == uneven) {         for (int i = 0; i < N; i++) {           cout << i + 1 <<     << j + 1 << endl;         }       }       if (j % 2 != uneven) {         for (int i = N - 1; i > -1; i--) {           cout << i + 1 <<     << j + 1 << endl;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   int a[10];   int main() {     int T, N, ans;     scanf( %d , &T);     while (T--) {       scanf( %d , &N);       ans = 1;       for (int i = 0; i <= 30; i++)         if (N & (1 << i)) ans *= 2;       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n, m;       scanf( %d%d , &n, &m);       int x, ans = 0;       for (int i = 0; i < n; i++) {         scanf( %d , &x);         ans += x;       }       if (ans == m)         printf( YES n );       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 2000 * 100 + 10;   map<int, int> val;   int a[maxN];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       val[a[i]]++;     }     sort(a, a + n);     n = unique(a, a + n) - a;     cout << a[n - 1] - a[0] <<    ;     if (n == 1)       cout << 1LL * val[a[0]] * (val[a[0]] - 1) / 2 << endl;     else       cout << 1LL * val[a[0]] * val[a[n - 1]] << endl;   }