#include <bits/stdc++.h>   using namespace std;   int AR[1000000 + 7];   int main() {     int cnt = 0, k;     string s;     cin >> k >> s;     AR[0] = 1;     int64_t r = 0;     for (int i = 0; s[i] !=   0 ; i++) {       if (s[i] ==  1 ) cnt++;       if (cnt >= k) r += AR[cnt - k];       AR[cnt]++;     }     printf( %I64d n , r);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int solve() {     long long int n;     cin >> n;     vector<bool> dp(2 * n + 1, 0);     vector<long long int> p(2 * n);     vector<long long int> nums;     long long int cover = 0;     vector<long long int> inv(2 * n + 1);     for (long long int j = 0; j < 2 * n; j++) {       cin >> p[j];       inv[p[j]] = j;       if (p[j] == 2 * n) {         cover = j;       }     }     nums.push_back(2 * n - cover);     for (long long int j = 2 * n - 1; j >= 1; j--) {       if (inv[j] > cover) {         continue;       } else {         nums.push_back(cover - inv[j]);         cover = inv[j];       }     }     sort(nums.begin(), nums.end());     dp[nums[0]] = 1;     dp[0] = 1;     long long int k = nums.size();     for (long long int j = 1; j < k; j++) {       vector<long long int> f(2 * n + 1, 0);       for (long long int i = 0; i <= 2 * n; i++) {         f[i] = dp[i];         if (i - nums[j] >= 0) {           f[i] = dp[i] || dp[i - nums[j]];         }       }       for (long long int i = 0; i <= 2 * n; i++) {         dp[i] = f[i];       }     }     if (!dp[n]) {       cout <<  NO n ;     } else {       cout <<  YES n ;     }     return 0;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int s[220];   int d[220];   int sol[220];   vector<pair<int, int> > v;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     cin >> n;     for (int(i) = (0); (i) < (n); (i)++) cin >> s[i] >> d[i];     for (int(i) = (0); (i) < (n); (i)++) {       int last = 1;       int ok = 0;       for (typeof(v.begin()) it = (v.begin()); it != v.end(); it++) {         if (last <= s[i] && it->first >= s[i] + d[i]) {           v.insert(it, make_pair((s[i]), (d[i])));           ok = 1;           sol[i] = s[i];           break;         }         last = it->first + it->second;       }       if (v.empty() ||           v[((int)v.size()) - 1].first + v[((int)v.size()) - 1].second <= s[i]) {         v.push_back(make_pair((s[i]), (d[i])));         ok = 1;         sol[i] = s[i];       }       if (!ok) {         last = 1;         for (typeof(v.begin()) it = (v.begin()); it != v.end(); it++) {           if (it->first >= last + d[i]) {             v.insert(it, make_pair((last), (d[i])));             sol[i] = last;             ok = 1;             break;           }           last = it->first + it->second;         }       }       if (!ok) {         v.push_back(make_pair((last), (d[i])));         sol[i] = last;       }       cout << sol[i] <<     << sol[i] + d[i] - 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, seq[100005];   char s[2][55];   string a[100005], b[100005], cur;   void solve() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %s %s , s[0], s[1]);       a[i] = s[0];       b[i] = s[1];     }     for (int i = 0; i < n; i++) {       scanf( %d , &seq[i]);       seq[i]--;     }     if (a[seq[0]] < b[seq[0]])       cur = a[seq[0]];     else       cur = b[seq[0]];     for (int i = 1; i < n; i++) {       if (cur < a[seq[i]] && cur < b[seq[i]]) {         if (a[seq[i]] < b[seq[i]])           cur = a[seq[i]];         else           cur = b[seq[i]];       } else if (cur < a[seq[i]])         cur = a[seq[i]];       else if (cur < b[seq[i]])         cur = b[seq[i]];       else {         puts( NO );         return;       }     }     puts( YES );   }   int main(void) {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<string> s;   string v;   map<string, bool> m;   void rot(string x) {     if (m[x] == 1) return;     m[x] = 1;     s.erase(x);     string y = x;     y[0] = x[1];     y[1] = x[2];     y[2] = x[3];     y[3] = x[0];     rot(y);     y = x;     y[1] = x[4];     y[4] = x[3];     y[3] = x[5];     y[5] = x[1];     rot(y);     return;   }   int main() {     cin >> v;     for (int i = 0; i < 1000; i++) {       next_permutation(v.begin(), v.end());       s.insert(v);     }     int ans = 0;     while (s.size() > 0) {       ans++;       rot(*s.begin());     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int l = 1;     int r = n;     bool trn = 0;     while (l <= r) {       if (!trn)         cout << l++;       else         cout << r--;       cout <<    ;       trn = !trn;     }   }


#include <bits/stdc++.h>   using namespace std;   const int M = 105;   bool visit[M];   string second[M];   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) cin >> second[i];     vector<string> v;     for (int i = 0; i < m; i++) {       int x;       scanf( %d , &x);       visit[x] = 1;       v.push_back(second[x]);     }     for (int i = 1; i < m; i++) {       if (v[i].size() != v[0].size()) {         cout <<  No ;         return 0;       }     }     string pattern =   ;     for (int i = 0; i < v[0].size(); i++) {       set<char> c;       for (int j = 0; j < m; j++) c.insert(v[j][i]);       if (c.size() == 1)         pattern += *c.begin();       else         pattern +=  ? ;     }     for (int i = 1; i <= n; i++) {       if (second[i].size() != pattern.size()) continue;       bool match = 1;       for (int j = 0; j < second[i].size(); j++) {         if (pattern[j] != second[i][j] && pattern[j] !=  ? ) {           match = 0;           break;         }       }       if (match && !visit[i]) {         cout <<  No ;         return 0;       }     }     cout <<  Yes n  << pattern;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long mmul(long long a, long long b) { return (a * b) % 998244353; }   inline long long madd(long long a, long long b) { return (a + b) % 998244353; }   long long mpow(long long a, long long p) {     if (p == 0) return 1;     long long halfPow = mpow(a, p / 2);     return mmul(halfPow, p % 2 ? mmul(a, halfPow) : halfPow);   }   long long minv(long long a) { return mpow(a, 998244353 - 2); }   long long mdiv(long long a, long long b) { return mmul(a, minv(b)); }   vector<long long> _mfactMem(1, 1);   long long mfact(long long a) {     if (a >= 998244353) return 0;     while (a >= _mfactMem.size())       _mfactMem.push_back(mmul(_mfactMem.size(), _mfactMem.back()));     return _mfactMem[a];   }   long long mcomb(long long n, long long k) {     if (n == 0 && k == 0) return 1;     long long ni = n % 998244353, ki = k % 998244353;     if (ni < ki) return 0;     long long comb = mmul(mfact(ni), mmul(minv(mfact(ki)), minv(mfact(ni - ki))));     return mcomb(n / 998244353, k / 998244353) * comb;   }   long long calc(int na, int nb) {     long long cnt = 0;     for (int i = 0; i <= min(na, nb); i++) {       cnt = madd(cnt, mmul(mfact(i), mmul(mcomb(na, i), mcomb(nb, i))));     }     return cnt;   }   int main() {     int a, b, c;     scanf( %d %d %d n , &a, &b, &c);     int res = (int)mmul(calc(a, b), mmul(calc(b, c), calc(c, a)));     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[1009];   int cmp(int a, int b) { return a > b; }   int main() {     long long T;     scanf( %lld , &T);     while (T--) {       long long n, sum = 0;       scanf( %lld , &n);       for (int i = 1; i <= n; i++) {         scanf( %lld , &a[i]);         sum = sum + a[i];       }       sort(a + 1, a + n + 1, cmp);       if (a[1] > sum / 2) {         printf( T n );       } else if (a[1] <= sum / 2) {         if (sum % 2 == 1) {           printf( T n );         } else if (sum % 2 == 0) {           printf( HL n );         }       }     }     return 0;   }


#include <bits/stdc++.h>      using namespace std;   #define FAST      ios_base::sync_with_stdio(false); cin.tie(NULL);   #define prs(n)      cout << fixed << setprecision(n)   #define ll       long long   #define db       double   #define fo(i,m,n)       for(int i = m; i <= n; i++)   #define fob(i,m,n)      for(int i = n; i >= m; i--)   typedef                 pair<int, int> ii;   typedef                 vector<ii> vii;   typedef                 vector<vii> vvii;   const                   int maxi=INT_MAX;   const                   int _x[]={0,0,1,-1};   const                   int _y[]={1,-1,0,0};      ll n, ans, l[200007], r[200007], dp[200007][2];   vector <ll> a[200007];      void input()   {       int u, v;       cin >> n;       for (int i = 1; i <= n; i++)       {           cin >> l[i] >> r[i], a[i].resize(0);           dp[i][1] = dp[i][0] = 0;       }       for (int i = 1; i < n ; i++)       {           cin >> u >> v;           a[u].push_back(v);           a[v].push_back(u);       }   }      void dfs(int u, int f)   {       for (int i:a[u])       {           if (i != f)           {               dfs(i,u);               dp[u][0] += max(dp[i][0] + abs(l[u] - l[i]), dp[i][1] + abs(l[u] - r[i]));               dp[u][1] += max(dp[i][0] + abs(r[u] - l[i]), dp[i][1] + abs(r[u] - r[i]));           }       }   }      void solve()   {       dfs(1,0);       ans = max(dp[1][0], dp[1][1]);   }      int main()   {       FAST       //freopen( a.INP ,  r , stdin);       //freopen( a.OUT ,  w , stdout);       int t;       cin >> t;       while (t--)       {           input();           solve();           cout << ans <<   n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   char str[1000000];   int len;   bool yes(char s[3], int ls, int le) {     while (true) {     start:       int mid = le - ls - 1, i;       if (le != len - 1) {         if (s[0] != s[1]) return 0;       }       int c[3] = {0}, sd[2];       c[(s[1] -  0 ) ^ 1] += len - le - 1;       for (i = ls + 1; i < le; i++) {         if (str[i] == s[0]) return 0;       }       c[(s[0] -  0 ) ^ 1] += mid;       for (i = 0; i < ls; i++) {         if (str[i] ==  ? )           c[2]++;         else           c[str[i] -  0 ]++;       }       if (len & 1)         sd[0] = len / 2 - 1, sd[1] = sd[0] + 1;       else         sd[0] = len / 2 - 1, sd[1] = sd[0];       if (s[0] != s[1]) {         if (c[s[1] -  0 ] > sd[s[1] -  0 ]) return 0;         if (c[s[0] -  0 ] > sd[s[0] -  0 ]) {           if (str[ls] ==  ? ) {             for (i = ls - 1; i >= 0; i--) {               if (str[i] ==  ?  || str[i] == s[0]) break;             }             if (i >= 0) {               ls = i;               goto start;             }           }           return 0;         }       } else {         if (s[0] ==  0 ) {           if (c[0] + c[2] < sd[0]) return 0;         } else {           if (c[1] + c[2] < sd[1]) return 0;         }       }       return 1;     }   }   bool ok(char s[3]) {     int i, ls, le;     for (le = len - 1; le >= 0; le--) {       if (str[le] == s[1] || str[le] ==  ? ) break;     }     if (le <= 0) return 0;     for (ls = le - 1; ls >= 0; ls--) {       if (str[ls] == s[0] || str[ls] ==  ? ) break;     }     if (ls < 0) return 0;     return yes(s, ls, le);   }   void run() {     len = strlen(str);     if (ok( 00 )) printf( 00 n );     if (ok( 01 )) printf( 01 n );     if (ok( 10 )) printf( 10 n );     if (ok( 11 )) printf( 11 n );   }   int main() {     scanf( %s , str);     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100100, INFTY = 0x3f3f3f3f;   int A[MAXN], B[MAXN];   vector<int> p[MAXN];   int posb[330];   int resp[330];   int main() {     int N, M, S, E;     posb[0] = 0;     resp[0] = 0;     for (int i = 1; i <= 300; i++) {       posb[i] = MAXN;       resp[i] = INFTY;     }     for (int i = 1; i <= MAXN - 100; i++) p[i].clear();     scanf( %d %d %d %d , &N, &M, &S, &E);     for (int i = 1; i <= N; i++) scanf( %d , &A[i]);     for (int i = 1; i <= M; i++) {       scanf( %d , &B[i]);       p[B[i]].push_back(i);     }     for (int i = 1; i <= 10000; i++) {       int tam = p[i].size();       if (tam == 0) continue;     }     for (int k = 1; k <= N; k++) {       int x = A[k];       for (int i = 299; i >= 0; i--) {         if (posb[i] == MAXN) continue;         int ini = posb[i] + 1;         int tam = p[x].size();         int prox = (lower_bound(&p[x][0], &p[x][tam], ini) - &p[x][0]);         if (prox == tam) continue;         posb[i + 1] = min(posb[i + 1], p[x][prox]);         resp[i + 1] = min(resp[i + 1], p[x][prox] + k);       }     }     int ans = 0;     for (int i = 0; i <= 300; i++) {       long long int qq = E, rr = S;       qq *= i;       qq += resp[i];       if (qq > rr) break;       ans = i;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int T;   signed long long S[101010], E[101010];   int win[101010], lose[101010];   int canwin(signed long long s, signed long long e) {     if (s == e) return 0;     if (s >= e) return 1;     if (e % 2) {       return !(s % 2);     } else {       if (s * 2 > e) return (e - s) % 2;       if (4 * s > e) return 1;       return canwin(s, e / 4);     }   }   int canlose(signed long long s, signed long long e) {     if (e < 2 * s) return 1;     return canwin(s, e / 2);   }   void solve() {     int i, j, k, r, x, y;     string s;     cin >> T;     for (i = 0; i < (T); i++) {       cin >> S[i] >> E[i];       win[i] = canwin(S[i], E[i]);       lose[i] = canlose(S[i], E[i]);     }     int w = 1, l = 0;     for (i = 0; i < (T); i++) {       if (w == l) break;       if (w) {         w = lose[i];         l = win[i];       } else {         w = !lose[i];         l = !win[i];       }     }     cout << l <<     << w << endl;   }   int main(int argc, char** argv) {     string s;     int i;     if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);     for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ;     for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     map<int, bool> hash;     int p, n;     bool has = false;     cin >> p >> n;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       x %= p;       if (hash.count(x) && !has) {         cout << i + 1 << endl;         has = true;       } else {         hash[x] = true;       }     }     if (!has) printf( -1 );   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-10;   const double PI = acos(-1.0);   int dp[51][51][51];   int dfs(int n, int m, int k) {     if (dp[n][m][k] >= 0) return dp[n][m][k];     if (k == 0 || n * m == k) return dp[n][m][k] = 0;     dp[n][m][k] = 1e9;     for (int j = 0; j <= k; ++j) {       for (int i = 1; i < n; ++i)         dp[n][m][k] =             min(dp[n][m][k], dfs(n - i, m, k - j) + dfs(i, m, j) + m * m);       for (int i = 1; i < m; ++i)         dp[n][m][k] =             min(dp[n][m][k], dfs(n, m - i, k - j) + dfs(n, i, j) + n * n);     }     return dp[n][m][k];   }   int main() {     cin.tie(0);     ios_base::sync_with_stdio(false);     fill((int*)dp, (int*)dp + 51 * 51 * 51, -1);     int T;     cin >> T;     while (T--) {       int n, m, k;       cin >> n >> m >> k;       cout << dfs(n, m, k) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     long long int d;     cin >> n >> d;     vector<int> check_days;     int a[n];     long long int prefix_sum[n];     cin >> a[0];     if (a[0] == 0) check_days.push_back(0);     prefix_sum[0] = a[0];     for (int i = 1; i < n; i++) {       cin >> a[i];       if (a[i] == 0) check_days.push_back(i);       prefix_sum[i] = prefix_sum[i - 1] + a[i];     }     int days = 0;     long long int mi = LLONG_MIN;     long long int max_balance_after[n];     for (int i = n - 1; i >= 0; i--) {       if (prefix_sum[i] > d) {         cout << -1;         return 0;       }       mi = max(mi, prefix_sum[i]);       max_balance_after[i] = mi;     }     int deposit_val = 0;     for (int i = 0; i < check_days.size(); i++) {       int j = check_days[i];       if (prefix_sum[j] + deposit_val < 0) {         int new_bal = 0, val_to_deposit = 0;         new_bal = max_balance_after[j] + deposit_val;         val_to_deposit = d - new_bal;         days++;         deposit_val += val_to_deposit;         if (prefix_sum[j] + deposit_val < 0) {           cout << -1;           return 0;         }       }     }     cout << days;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5000;   map<int, int> cntmap;   long long ans;   int arr[MAXN];   char s[MAXN];   int a, ln;   void fillmap(int x) { cntmap[x]++; }   void getans(int x) {     if (x == 0) {       if (a == 0) ans += (ln * (ln + 1)) / 2;     } else if (a % x == 0)       if (cntmap.count(a / x)) ans += cntmap[a / x];   }   void loop(void (*fun)(int)) {     int sm = 0;     for (int i = 0; i < ln; i++) {       sm = arr[i];       for (int j = i; j < ln; j++) {         if (j != i) sm += arr[j];         fun(sm);       }     }     return;   }   int main() {     scanf( %d %s , &a, s);     ans = 0;     ln = strlen(s);     for (int i = 0; i < ln; i++) arr[i] = s[i] -  0 ;     loop(fillmap);     loop(getans);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = acos(-1.0);   const long double EPS = 1e-12;   const int MAXN = (int)1e5;   const int INF = (int)2e9;   int main() {     int n, a[100];     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d , &a[i]);     int ans = INF;     for (int i = 0; i < n; ++i) {       int sum = 0;       for (int j = 0; j < a[i]; ++j) {         int num;         scanf( %d , &num);         sum += num * 5 + 15;       }       ans = min(ans, sum);     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 20, Mod = 1e9 + 7;   int pl, pr, vl, vr, k;   long double p, q;   vector<int> v;   void count(int t) {     int L, R;     L = min(v[t], pr) - max(v[t - 1] + 1, pl);     R = min(v[t + k] - 1, vr) - max(v[t + k - 1], vl);     p += max(L + 1, 0) * 1ll * max(R + 1, 0);     L = min(v[t], vr) - max(v[t - 1] + 1, vl);     R = min(v[t + k] - 1, pr) - max(v[t + k - 1], pl);     p += max(L + 1, 0) * 1ll * max(R + 1, 0);     if (k == 1) {       if (vl <= v[t] && v[t] <= vr && pl <= v[t] && v[t] <= pr) p--;     }   }   void add(int length) {     for (int i = 0; i < (1 << length); i++) {       int num = 0;       for (int j = 0; j < length; j++) {         num *= 10;         if (i & (1 << j))           num += 4;         else           num += 7;       }       v.push_back(num);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> pl >> pr >> vl >> vr >> k;     q = (pr - pl + 1) * 1ll * (vr - vl + 1);     for (int i = 1; i <= 9; i++) add(i);     v.push_back(0);     v.push_back(Mod);     v.push_back(Mod);     sort(v.begin(), v.end());     int n = v.size();     for (int i = 1; i < n - k - 1; i++) {       count(i);     }     cout << fixed << setprecision(10) << p / q;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   const int M = 5.1e6 + 5;   const int B = 10;   int n, q;   int h[M];   int go[B + 1][N];   vector<int> dv[N];   int best[8][M];   int main() {     for (int i = 2; i * i < M; i++) {       if (!h[i]) {         for (int j = i; j < M; j += i) h[j] = i;       }     }     scanf( %d %d , &n, &q);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       while (h[x]) {         int y = h[x], c = 0;         while (x % y == 0) {           x /= y;           c ^= 1;         }         if (c) dv[i].push_back(y);       }       if (x > 1) dv[i].push_back(x);       assert(dv[i].size() <= 7);     }     for (int need = 0; need <= B; need++) {       go[need][n + 1] = n + 1;       memset(best, 0, sizeof(best));       for (int i = n; i >= 1; i--) {         go[need][i] = go[need][i + 1];         for (int mask = 0; mask < (1 << dv[i].size()); mask++) {           int prod = 1, cnt = dv[i].size() - __builtin_popcount(mask);           for (int j = 0; j < dv[i].size(); j++)             if (mask & (1 << j)) prod *= dv[i][j];           if (cnt <= need and need - cnt <= 7 and best[need - cnt][prod]) {             go[need][i] = min(go[need][i], best[need - cnt][prod]);           }           best[cnt][prod] = i;         }       }     }     for (int i = 1; i <= q; i++) {       int l, r;       scanf( %d %d , &l, &r);       for (int i = 0; i <= B + 1; i++) {         if (i > B or go[i][l] <= r) {           printf( %d n , i);           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int pr[2 * 100 * 1000 + 10];   int a[2 * 100 * 1000 + 10];   int main() {     int max_n = 0;     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] > max_n) max_n = a[i];     }     for (int i = 0; i <= max_n; i++) pr[i] = 1;     for (int i = 2; i <= max_n; i++)       if (pr[i]) {         int k = 2;         while (i * k <= max_n) {           pr[i * k] = 0;           k++;         }       }     long long ans = 1;     for (long long i = 2; i <= max_n; i++) {       if (!pr[i]) continue;       int bad_index = -1;       long long cur = i;       while (cur <= max_n) {         int cnt = 0;         int new_bad = -1;         for (int j = 0; j < n; j++)           if (j != bad_index && a[j] % cur) {             cnt++;             new_bad = j;             if (cnt > 1) break;           }         if (cnt > 1) break;         if (bad_index != -1 && new_bad != -1) break;         if (bad_index == -1) bad_index = new_bad;         ans *= i;         cur *= i;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<double, int> Freq;   int main() {     double N;     double W;     scanf( %lf%lf , &N, &W);     vector<double> vec;     for (int i = 0; i < N * 2; i++) {       double a;       scanf( %lf , &a);       vec.push_back(a);     }     sort((vec).begin(), (vec).end());     double ans = min(vec[0] * 1.0, vec[(int)(N + 1e-9)] / 2.0);     ans = min(ans, W * 1.0 / (N * 3.0));     printf( %.9lf n , 3 * N * ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> edge[200002], col;   int vis[200002];   queue<int> q;   void bfs(int s) {     vis[s] = 1;     q.push(s);     while (!q.empty()) {       int x = q.front();       q.pop();       set<int> st;       for (auto y : edge[x]) {         if (vis[y]) {           st.insert(col[y - 1]);         }       }       if (col[x - 1] == -1) {         int flag = 1;         while (1) {           if (st.find(flag) != st.end()) {             flag++;           } else {             col[x - 1] = flag;             break;           }         }       }       int start = col[x - 1] - 1, end = col[x - 1] + 1;       for (auto y : edge[x]) {         if (!vis[y]) {           vis[y] = 1;           q.push(y);           int flag = 0;           while (!flag) {             if (start > 0) {               if (st.find(start) != st.end()) {                 start--;               } else {                 col[y - 1] = start;                 st.insert(start);                 start--;                 flag = 1;               }             } else {               if (st.find(end) != st.end()) {                 end++;               } else {                 col[y - 1] = end;                 st.insert(end);                 end++;                 flag = 1;               }             }           }         }       }     }   }   void solve() {     int n;     cin >> n;     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       edge[x].push_back(y);       edge[y].push_back(x);     }     col.assign(n, -1);     for (int i = 1; i <= n; i++) {       if (edge[i].size() > 1) {         bfs(i);         break;       }     }     int mx = 1;     for (auto x : col) {       mx = max(x, mx);     }     cout << mx <<   n ;     for (auto x : col) {       cout << x <<    ;     }     cout <<   n ;   }   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int val, id;     friend bool operator<(node a, node b) { return a.id > b.id; }   };   int main() {     int t, m, n;     struct node p;     while (~scanf( %d %d , &m, &n)) {       priority_queue<node> a;       priority_queue<node> b;       int id = 0;       for (int i = 0; i < m; i++) {         scanf( %d , &t);         p.id = id++;         p.val = t;         a.push(p);       }       id = 0;       ;       for (int i = 0; i < n; i++) {         scanf( %d , &t);         p.id = id++;         p.val = t;         b.push(p);       }       while (!b.empty() && !a.empty()) {         if (a.top().val <= b.top().val) {           a.pop();           b.pop();         } else if (a.top().val > b.top().val) {           a.pop();         }       }       printf( %d n , n - b.size());     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5;   const int MAXM = 2e5;   int NV, NH;   int V[MAXN];   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> NV >> NH;     for (int i = 0; i < NV; i++) {       cin >> V[i];     }     priority_queue<int, vector<int>, greater<int>> H;     for (int i = 0; i < NH; i++) {       int x1, x2, y;       cin >> x1 >> x2 >> y;       if (x1 == 1) {         H.push(x2);       } else {         continue;       }     }     int res = NV + NH;     sort(V, V + NV);     V[NV] = int(1e9);     for (int i = 0; i <= NV; i++) {       while (!H.empty() && H.top() < V[i]) {         H.pop();       }       res = min(res, i + int(H.size()));     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double EPS = 1e-3;   const int INF = (long double)(1e9);   const long long mod = (int)(1e+9 + 7);   const int N = (int)(2e3);   int days_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   long long ekn;   long long inverse;   long long expo(long long b, long long e) {     long long prod = 1;     while (e != 0) {       if (e & 1) prod = (1LL * prod * b) % mod;       b = (1LL * b * b) % mod;       e >>= 1;     }     return prod % mod;   }   int f(int i) {     long long prod = (1LL * expo(2, i) * (ekn - 1)) % mod;     prod = (prod * inverse) % mod;     return prod;   }   int main() {     string s;     cin >> s;     int k;     cin >> k;     int n = int(s.size());     ekn = expo(2, 1LL * k * n);     inverse = expo(expo(2, n) - 1, mod - 2);     int ans = 0;     for (int i = 0; i < n; i++) {       if (s[i] !=  0  && s[i] !=  5 ) continue;       ans += f(i);       if (ans >= mod) ans -= mod;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2 * (int)1e5 + 100;   int n, k;   char a[N];   int main() {     scanf( %d %d , &n, &k);     scanf( %s , a);     int tmp = 0, result = 0, ans = 0;     for (int i = 0; i < 26; i++) {       char ch =  a  + i;       tmp = 0;       result = 0;       for (int j = 0; j <= n; j++) {         if (a[j] == ch) {           tmp++;         } else {           result += tmp / k;           tmp = 0;         }       }       ans = max(ans, result);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 110;   const int INF = 1 << 28;   const double EPS = 1e-9;   const double PI = 3.14159265358979;   int n, d;   int a[MAX_N];   int x[MAX_N], y[MAX_N];   int g[MAX_N][MAX_N];   int main() {     cin >> n >> d;     for (int i = (1); i < (n - 1); i++) cin >> a[i];     for (int i = (0); i < (n); i++) cin >> x[i] >> y[i];     for (int i = (0); i < (n); i++)       for (int j = (0); j < (n); j++) {         g[i][j] = d * (abs(x[i] - x[j]) + abs(y[i] - y[j]));         if (i != j) g[i][j] -= a[j];       }     for (int k = (0); k < (n); k++)       for (int i = (0); i < (n); i++)         for (int j = (0); j < (n); j++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]);     cout << g[0][n - 1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, long long> Map;   void modify(long long x, long long y) {     map<long long, long long>::iterator it;     long long c;     scanf( %I64d , &c);     while (x != y) {       if (x < y) swap(x, y);       it = Map.find(x);       if (it != Map.end())         it->second += c;       else         Map[x] = c;       x >>= 1;     }   }   long long query(long long x, long long y) {     map<long long, long long>::iterator it;     long long ans = 0;     while (x != y) {       if (x < y) swap(x, y);       it = Map.find(x);       if (it != Map.end()) ans += it->second;       x >>= 1;     }     return ans;   }   int main(int argc, char* argv[]) {     int q = 0;     for (; scanf( %d , &q) != EOF;) {       Map.clear();       while (q--) {         int od;         long long x, y, c;         scanf( %d%I64d%I64d , &od, &x, &y);         if (od == 1)           modify(x, y);         else           printf( %I64d n , query(x, y));       }     }     return 0;   }


#include <bits/stdc++.h>   int a[1000005];   double tt;   int main() {     int n, i, t;     double vb, vs, x, y, d;     while (scanf( %d %lf %lf , &n, &vb, &vs) != EOF) {       t = 2;       for (i = 1; i <= n; i++) {         scanf( %d , &a[i]);       }       scanf( %lf %lf , &x, &y);       d = a[2] / vb + sqrt((x - a[2]) * (x - a[2]) + y * y) / vs;       for (i = 2; i <= n; i++) {         tt = a[i] / vb + sqrt((x - a[i]) * (x - a[i]) + y * y) / vs;         if (tt <= d) {           d = tt;           t = i;         }       }       printf( %d n , t);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   int sumOfDigits(int n) {     int ans = 0;     if (n <= 9) return n;     while (n) {       ans += n % 10;       n /= 10;     }     return ans;   }   int main() {     cin.tie(NULL);     ios_base::sync_with_stdio(false);     int a;     cin >> a;     while (sumOfDigits(a) % 4 != 0) {       a++;     }     cout << a << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 4e5 + 10;   const long long mod = 998244353;   const long long inf = 1e18 + 10;   const long long Log = 21;   long long n, m, k;   long long pw(long long a, long long b) {     if (b == 0) return 1;     long long x = pw(a, b / 2);     x = (x * x) % mod;     if (b & 1) x = (x * a) % mod;     return x;   }   long long cnt(long long x, long long p) {     long long X = 0;     while (x % p == 0) {       x /= p;       X++;     }     return X;   }   long long D;   vector<long long> pr;   long long fac[maxn], repo[maxn];   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     fac[0] = 1;     repo[0] = 1;     for (long long i = 1; i < 200; i++) {       fac[i] = (fac[i - 1] * i) % mod;       repo[i] = pw(fac[i], mod - 2);     }     cin >> D;     for (long long i = 2; i <= sqrt(D); i++)       if (D % i == 0) {         while (D % i == 0) D /= i;         pr.push_back(i);       }     if (D > 1) pr.push_back(D);     long long q;     cin >> q;     while (q--) {       long long u, v;       cin >> u >> v;       if (u > v) swap(u, v);       long long ans = 1, sm2 = 0, sm1 = 0;       for (auto p : pr) {         long long x = cnt(u, p);         long long y = cnt(v, p);         (ans *= repo[abs(x - y)]) %= mod;         if (x < y) sm1 += y - x;         if (x > y) sm2 += x - y;       }       (ans *= (fac[sm1] * fac[sm2]) % mod) %= mod;       cout << ans <<   n ;     }   }


#pragma GCC optimize( -Ofast , -funroll-all-loops )   #pragma GCC optimize(2)   #pragma GCC optimize(3, Ofast , inline )   #include<bits/stdc++.h>   using namespace std;      typedef long long ll;   int _;   int n;   int a[30010];      void solve(){       cin >> _;       while(_--){           cin >> n;           int cnt0 = 0, cnt1 = 0, cnt2 = 0;           for(int i = 1; i <= n; i++){               cin >> a[i];               if(a[i] % 3 == 0){                   cnt0++;               }               else if(a[i] % 3 == 1){                   cnt1++;               }               else{                   cnt2++;               }           }           int need = n/3;           if(cnt0 == need && cnt1 == need && cnt2 == need){               cout <<  0  <<   n ;           }           else if(cnt0 > need && cnt1 > need && cnt2 < need){               cout << (cnt0-need)*2 + (cnt1-need) <<   n ;           }           else if(cnt0 < need && cnt1 > need && cnt2 > need){               cout << (cnt1-need)*2+(cnt2-need) <<   n ;           }           else if(cnt0 > need && cnt1 < need && cnt2 > need){               cout << (cnt0-need) + (cnt2-need)*2 <<   n ;           }           else if(cnt0 > need && cnt1 < need && cnt2 < need){               cout << (need-cnt1) + (need-cnt2)*2 <<   n ;           }           else if(cnt0 < need && cnt1 > need && cnt2 < need){               cout << (need-cnt2) + 2*(need-cnt0) <<   n ;           }           else if(cnt0 < need && cnt1 < need && cnt2 > need){               cout << (need-cnt0) + 2*(need-cnt1) <<   n ;           }           else if(cnt0 == need){               if(cnt1 > need && cnt2 < need){                   cout << (cnt1-need) <<   n ;               }               else{                   cout << 2*(cnt2-need) <<  n ;               }           }           else if(cnt1 == need){               if(cnt0 > need && cnt2 < need){                   cout << 2*(cnt0-need) <<   n ;               }               else{                   cout << (cnt2-need) <<   n ;               }           }           else if(cnt2 == need){               if(cnt0 > need && cnt1 < need){                   cout << (cnt0 - need) <<   n ;               }               else{                   cout << 2*(cnt1 - need) <<   n ;               }           }       }       return ;   }      int main(){       ios::sync_with_stdio(false);       cin.tie(0);       cout.tie(0);              solve();       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200005;   int n, a[maxn], total;   void solve() {     vector<int> ans;     for (int i = 1; i <= n; i++) {       if (a[i] * n == total) ans.push_back(i);     }     cout << (int)ans.size() << endl;     for (int i = 0; i < ans.size(); i++) {       if (i) printf(   );       cout << ans[i];     }     cout << endl;   }   int main() {     cin >> n;     total = 0;     for (int i = 1; i <= n; i++) {       cin >> a[i];       total += a[i];     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[1000000], arr1[1000000], cnt2[1000000];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, k, i, j, x, y, z, ans, a, b, c;     cin >> n >> k;     for (i = 1; i <= n; i++) {       cin >> arr[i];     }     for (i = 1; i <= n; i++) {       cin >> arr1[i];     }     x = arr[k] + arr1[1];     y = 1;     z = n;     for (i = 1; i <= k; i++) {       if (arr[i] + arr1[z] <= x) {         z--;       } else if (arr[i] + arr1[z] > x) {         y++;       }     }     cout << y << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500010;   struct edge {     int nxt, t;   } e[N * 3];   int head[N], edge_cnt;   void add_edge(int x, int y) {     e[edge_cnt] = (edge){head[x], y};     head[x] = edge_cnt++;   }   int dfn[N], low[N], Tar_cnt, stk[N], stk_top, loop_cnt, ID[N], P[N], tot, st[N],       ed[N];   void Tarjan(int x, int f) {     dfn[x] = low[x] = ++Tar_cnt;     stk[++stk_top] = x;     int i;     for (i = head[x]; ~i; i = e[i].nxt) {       int to = e[i].t;       if (to == f) {         continue;       }       if (dfn[to] == 0) {         Tarjan(to, x);         low[x] = min(low[x], low[to]);       } else {         low[x] = min(low[x], dfn[to]);       }     }     if (low[x] == dfn[x]) {       int y;       loop_cnt++;       st[x] = tot + 1;       do {         y = stk[stk_top--];         ID[y] = loop_cnt;         P[++tot] = y;       } while (x != y);       ed[x] = tot;     }   }   int D[N], Mx[N][2], dis[N], Fa[N];   bool vis[N];   void cmp(int x, int y) {     if (dis[Mx[x][0]] < dis[y]) {       Mx[x][1] = Mx[x][0];       Mx[x][0] = y;     } else if (dis[Mx[x][1]] < dis[y]) {       Mx[x][1] = y;     }   }   void BFS(int n) {     int L = 1, R = 1, i;     D[1] = 1;     vis[1] = 1;     while (L <= R) {       int x = D[L++];       for (i = head[x]; ~i; i = e[i].nxt) {         int to = e[i].t;         if (vis[to]) {           continue;         }         vis[to] = 1;         Fa[to] = x;         D[++R] = to;       }     }     dis[0] = -1;     for (i = n; i > 1; i--) {       int x = D[i], y = Fa[x];       if (ID[x] != ID[y]) {         cmp(y, x);       }       dis[y] = max(dis[y], dis[x] + 1);     }   }   struct node {     int id, w;   } Q[N << 1];   int G[N], val[N], Ans[N], dp[N];   void Push(int &L, int &R, node x) {     while (L <= R && Q[R].w <= x.w) {       R--;     }     Q[++R] = x;   }   void Pop1(int &L, int x) {     if (Q[L].id < x) {       L++;     }   }   void Pop2(int &L, int x) {     if (Q[L].id > x) {       L++;     }   }   void Solve(int S, int f, int Fr) {     int i, j, d = ed[S] - st[S] + 1, k, L, R;     dp[S] = Fr;     if (d > 1) {       for (i = st[S]; i <= ed[S]; i++) {         int x = P[i], t = dis[Mx[x][0]] + 1;         G[i - st[S] + 1] = x;         Ans[x] = t;         if (x == S) {           val[i - st[S] + 1] = max(Fr, t);         } else {           val[i - st[S] + 1] = t;         }       }       L = 1, R = 0, k = d / 2;       for (i = d - k + 1; i <= d; i++) {         Push(L, R, (node){i - d, val[i] - (i - d)});       }       for (i = 1; i <= d; i++) {         Pop1(L, i - k);         dp[G[i]] = max(dp[G[i]], Q[L].w + i);         Push(L, R, (node){i, val[i] - i});       }       L = 1, R = 0, k = (d - 1) / 2;       for (i = k; i >= 1; i--) {         Push(L, R, (node){i + d, val[i] + (i + d)});       }       for (i = d; i >= 1; i--) {         Pop2(L, i + k);         dp[G[i]] = max(dp[G[i]], Q[L].w - i);         Push(L, R, (node){i, val[i] + i});       }     } else {       Ans[S] = max(Fr, dis[Mx[S][0]] + 1);     }     for (i = st[S]; i <= ed[S]; i++) {       int x = P[i];       for (j = head[x]; ~j; j = e[j].nxt) {         int to = e[j].t;         if (ID[x] == ID[to] || to == f) {           continue;         }         if (Mx[x][0] == to) {           Solve(to, x, max(dp[x] + 1, dis[Mx[x][1]] + 2));         } else {           Solve(to, x, max(dp[x] + 1, dis[Mx[x][0]] + 2));         }       }     }   }   int main() {     int n, m, i;     scanf( %d%d , &n, &m);     memset(head + 1, -1, sizeof(int) * 1 * n);     for (i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       add_edge(x, y);       add_edge(y, x);     }     memset(dfn + 1, 0, sizeof(int) * 1 * n);     Tarjan(1, 0);     BFS(n);     Solve(1, 0, 0);     for (i = 1; i <= n; i++) {       printf( %d n , max(Ans[i], dp[i]));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   int n, q, a[300500], dp[300500][30], best[300500][30], last[30];   int f(int x, int bt) {     if (a[x] & (1 << bt)) return x;     if (dp[x][bt] != -1) return dp[x][bt];     int mn = 1e9;     for (int i = 0; i <= 27; i++) {       if (a[x] & (1 << i) && best[x][i] != 1e9) mn = min(mn, f(best[x][i], bt));     }     return dp[x][bt] = mn;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     memset(dp, -1, sizeof dp);     for (int i = 0; i <= 27; i++) last[i] = 1e9;     cin >> n >> q;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = n - 1; i >= 0; i--) {       for (int j = 0; j <= 27; j++) best[i][j] = 1e9;       for (int j = 0; j <= 27; j++) {         best[i][j] = last[j];         if ((a[i] & (1 << j))) last[j] = i;       }     }     while (q--) {       int x, y, ans = 0;       cin >> x >> y;       x--, y--;       for (int i = 0; i <= 27; i++) {         if ((a[y] & (1 << i)) && f(x, i) <= y) ans = 1;       }       if (ans)         cout <<  Shi  << endl;       else         cout <<  Fou  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int OO = (int)2e9;   const double PI = 2 * acos(0.0);   const double EPS = 1e-9;   int dcmp(double a, double b) { return fabs(a - b) <= EPS ? 0 : a > b ? 1 : 2; }   int DI[] = {-1, 0, 1, 0, 1, -1, -1, 1};   int DJ[] = {0, 1, 0, -1, 1, -1, 1, -1};   map<int, long long> m;   int main() {     int n;     cin >> n;     for (int i = 0; i < (int)n; i++) {       int x;       cin >> x;       m[x]++;     }     long long ans = 0;     for (int i = -10; i < 0; i++)       if (m.count(i) && m.count(abs(i))) ans += (m[i] * m[abs(i)]);     if (m.count(0)) {       for (long long t = m[0] - 1; t > 0; t--) ans += t;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   priority_queue<pair<int, int>, vector<pair<int, int> >,                  greater<pair<int, int> > >       que;   stringstream ssin;   const long long LINF = 0x7fffffffffffffffll;   const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;   int n, idx, sum;   int e[M], ne[M], h[N], ans[N];   inline long long read() {     char c = getchar();     long long x = 0, f = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = x * 10 + c -  0 ;       c = getchar();     }     return x * f;   }   void add(int a, int b) {     e[idx] = b;     ne[idx] = h[a];     h[a] = idx++;   }   void dfs(int u, int fa) {     for (int i = h[u]; ~i; i = ne[i]) {       int v = e[i];       if (v == fa) continue;       dfs(v, u);     }     if (fa != -1) {       if (ans[u] == u) {         swap(ans[u], ans[fa]);         sum += 2;       }     }   }   int main() {     n = read();     memset(h, -1, sizeof h);     ;     for (int i = 1; i < n; ++i) {       ans[i] = i;       int a, b;       a = read();       b = read();       add(a, b);       add(b, a);     }     ans[n] = n;     dfs(1, -1);     if (ans[1] == 1) {       sum += 2;       for (int i = h[1]; ~i; i = ne[i]) {         int v = e[i];         swap(ans[1], ans[v]);         break;       }     }     cout << sum <<   n ;     for (int i = 1; i <= n; ++i) cout << ans[i] <<    ;     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   long long inv(long long a, long long mod) {     long long b = mod, p = 1, q = 0;     while (b) {       long long c = a / b, d = a;       a = b;       b = d % b;       d = p;       p = q;       q = d - c * q;     }     return (p + mod) % mod;   }   int fs = 505050;   vector<long long> fact(fs), ifact(fs);   long long binom(int a, int b, long long mod) {     if (a < b) return 0;     return fact[a] * ifact[b] % mod * ifact[a - b] % mod;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     long long k;     cin >> k;     vector<long long> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     long long mod = 998244353;     fact[0] = 1;     for (int i = 1; i < fs; i++) fact[i] = fact[i - 1] * i % mod;     ifact[fs - 1] = inv(fact[fs - 1], mod);     for (int i = fs - 1; i > 0; i--) ifact[i - 1] = ifact[i] * i % mod;     long long res = 0;     int same = 0;     for (int i = 0; i < n; i++) {       if (a[i] == a[(i + n - 1) % n]) same++;     }     int x = n - same;     if (x & 1) {       long long coef = k - 2;       for (int i = 1; i <= x; i += 2) {         res +=             binom(x, i, mod) * coef % mod * binom(x - i, (x - i) / 2, mod) % mod;         res %= mod;         coef = coef * (k - 2) % mod * (k - 2) % mod;       }     } else {       long long coef = 1;       for (int i = 0; i <= x; i += 2) {         res +=             binom(x, i, mod) * coef % mod * binom(x - i, (x - i) / 2, mod) % mod;         res %= mod;         coef = coef * (k - 2) % mod * (k - 2) % mod;       }     }     for (int i = 0; i < same; i++) {       res = res * k % mod;     }     long long ent = 1;     for (int i = 0; i < n; i++) {       ent = ent * k % mod;     }     res = (ent - res + mod) % mod;     res = res * ((mod + 1) / 2) % mod;     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 20, MAX2 = 1e7 + 10;   int an[MAX], n, x;   bool primes[MAX2];   void seive(long long n) {     for (long long i = 2; i <= n; i++) primes[i] = 1;     for (long long i = 2; i <= n; i++) {       if (primes[i] == 1) {         for (long long j = i * i; j <= n; j += i) primes[j] = 0;       }     }   }   set<int> mm;   int main() {     scanf( %d%d , &n, &x);     for (int i = 0; i < n; i++) scanf( %d , &an[i]), mm.insert(an[i]);     int k = 1299721;     seive(k);     if (x == 2)       printf( %d n , 0);     else if (*mm.begin() == 1)       printf( %d n , 1);     else {       if (x - 1 > 1299721)         printf( %d n , -1);       else {         vector<int> pp;         for (int i = 2; i <= x - 1; i++) {           if (primes[i] == 1) pp.push_back(i);         }         bool ok = 1;         for (int i = 0; i < pp.size(); i++) {           auto ite = mm.find(pp[i]);           if (ite == mm.end()) ok = 0;         }         if (!ok)           printf( %d n , -1);         else           printf( %d n , pp.size());       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   long long read_ll() {     char c = getchar();     while ((c <  0  || c >  9 ) && c !=  - ) c = getchar();     long long ret = 0;     int neg = 0;     if (c ==  - ) neg = 1, c = getchar();     while (c >=  0  && c <=  9 ) {       ret = 10 * ret + c - 48;       c = getchar();     }     return neg ? -ret : ret;   }   int main() {     long long n, i;     n = read_ll();     long long a[n + 2], mini = INT_MAX, maxi = INT_MIN;     a[0] = INT_MIN;     a[n + 1] = INT_MIN;     for (i = 1; i <= n; i++) {       a[i] = read_ll();       mini = min(a[i], mini);       maxi = max(a[i], maxi);     }     long long leftmin[n + 1], rightmin[n + 1], leftmax[n + 1], rightmax[n + 1];     memset(leftmax, 0, sizeof(leftmax));     memset(rightmin, 0, sizeof(rightmin));     memset(rightmax, 0, sizeof(rightmax));     memset(leftmin, 0, sizeof(leftmin));     stack<long long> st;     st.push(0);     for (i = 1; i <= n; i++) {       while (a[st.top()] >= a[i]) st.pop();       leftmin[i] = st.top();       st.push(i);     }     while (!st.empty()) st.pop();     st.push(n + 1);     for (i = n; i >= 1; i--) {       while (a[st.top()] > a[i]) st.pop();       rightmin[i] = st.top();       st.push(i);     }     while (!st.empty()) st.pop();     a[0] = INT_MAX, a[n + 1] = INT_MAX;     st.push(0);     for (i = 1; i <= n; i++) {       while (a[st.top()] <= a[i]) st.pop();       leftmax[i] = st.top();       st.push(i);     }     while (!st.empty()) st.pop();     st.push(n + 1);     for (i = n; i >= 1; i--) {       while (a[st.top()] < a[i]) st.pop();       rightmax[i] = st.top();       st.push(i);     }     long long minsum = mini, maxsum = maxi, res = 0;     for (i = 1; i <= n; i++) {       long long cnt = (rightmax[i] - i) * (i - leftmax[i]) -                       (rightmin[i] - i) * (i - leftmin[i]);       res += cnt * a[i];     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2010;   int n, a[N], p[N], pos[N];   vector<int> c[26], ans;   string s, t;   void add(int x) {     if (0 < x) {       ans.push_back(x);     }     vector<int> nw;     for (int i = n - 1; i >= n - x; --i) {       nw.push_back(p[i]);     }     for (int i = n - 1; i >= x; --i) {       p[i] = p[i - x];     }     for (int i = 0; i < x; ++i) {       p[i] = nw[i];     }     for (int i = 0; i < n; ++i) {       pos[p[i]] = i;     }   }   int main() {     cin >> n >> s >> t;     for (int i = 0; i < n; ++i) {       c[t[i] -  a ].push_back(i);     }     for (int i = 0; i < n; ++i) {       int ind = s[i] -  a ;       if (c[ind].size() == 0) {         puts( -1 );         return 0;       }       p[i] = c[ind].back();       c[ind].pop_back();       pos[p[i]] = i;     }     for (int i = 0; i < n; ++i) {       add(n - pos[i] - 1);       add(1);       add(n);     }     cout << ans.size() <<   n ;     for (int i : ans) cout << i <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   int main() {     int n, m;     cin >> n >> m;     vector<int> session(n);     vector<vector<int> > s2d(m);     for (int i = 0; i < n; i++) {       cin >> session[i];       if (session[i]) s2d[session[i] - 1].push_back(i);     }     vector<int> price(m);     for (int i = 0; i < m; i++) {       cin >> price[i];     }     int l = 0, r = n, mid, cnt, doned;     vector<int> pos(m);     bool res, ares = false;     while (r - l > 1) {       mid = (l + r) / 2;       for (int i = 0; i < m; i++) {         pos[i] = (int)(upper_bound(s2d[i].begin(), s2d[i].end(), mid) -                        s2d[i].begin()) -                  1;         pos[i] = (pos[i] >= 0 ? s2d[i][pos[i]] : -1);       }       cnt = 0, doned = 0;       res = true;       for (int i = 0; i <= mid && res; i++) {         if (session[i] && pos[session[i] - 1] == i) {           cnt -= price[session[i] - 1];           doned++;         } else           cnt++;         if (cnt < 0) res = false;       }       if (!res || doned < m) {         l = mid;       } else {         r = mid;         ares = true;       }     }     if (ares)       cout << r + 1;     else       cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int f[500000], g[500000], a[500000];   map<long long, int> mp;   struct bit1 {     int s[500000];     int num;     void add(int x, int z) {       for (int i = x; i <= num; i += (i & -i)) s[i] = max(s[i], z);     }     int ask(int x) {       int tmp = 0;       for (int i = x; i >= 1; i -= (i & -i)) tmp = max(tmp, s[i]);       return tmp;     }     void clear() { memset(s, 0, sizeof(s)); }   } s;   struct bit2 {     int s[500000];     int num;     void add(int x, int z) {       for (int i = x; i >= 1; i -= (i & -i)) s[i] = max(s[i], z);     }     int ask(int x) {       int tmp = 0;       for (int i = x; i <= num; i += (i & -i)) tmp = max(tmp, s[i]);       return tmp;     }     void clear() { memset(s, 0, sizeof(s)); }   } t;   int main() {     scanf( %d , &n);     int mx = 0, lis = 0;     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), mx = max(mx, a[i]);     s.clear();     s.num = mx + 1;     for (int i = 1; i <= n; i++) {       f[i] = s.ask(a[i] - 1) + 1;       s.add(a[i], f[i]);       lis = max(lis, f[i]);     }     t.clear();     t.num = mx + 1;     for (int i = n; i >= 1; i--) {       g[i] = t.ask(a[i] + 1) + 1;       t.add(a[i], g[i]);     }     for (int i = 1; i <= n; i++) {       long long tmp = f[i] * (n + 1) + g[i];       if (mp.find(tmp) == mp.end())         mp[tmp] = 1;       else         mp[tmp]++;     }     for (int i = 1; i <= n; i++) {       long long tmp = f[i] * (n + 1) + g[i];       if (f[i] + g[i] - 1 < lis)         printf( 1 );       else if (mp[tmp] > 1)         printf( 2 );       else         printf( 3 );     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 256;   const char nxt =   n ;   inline void in(string Q) {     Q +=  .in ;     freopen(Q.c_str(),  r , stdin);   }   inline void out(string Q) {     Q +=  .out ;     freopen(Q.c_str(),  w , stdout);   }   inline void open_file(string Q) {     in(Q);     out(Q);   }   int n, a[111][111];   int main() {     if (0) open_file(  );     scanf( %d , &n);     while (n--) {       int x, y, X, Y;       scanf( %d , &x);       scanf( %d , &y);       scanf( %d , &X);       scanf( %d , &Y);       for (int i = x; i <= X; ++i) {         for (int j = y; j <= Y; ++j) {           a[i][j]++;         }       }     }     int ans = 0;     for (int i = 1; i <= 100; ++i) {       for (int j = 1; j <= 100; ++j) {         ans += a[i][j];       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char BUFFER[100000 + 5];   bool readn(int &n) { return scanf( %d , &n) == 1; }   bool readl(long long &n) { return scanf( %I64d , &n) == 1; }   bool readd(double &n) { return scanf( %lf , &n) == 1; }   bool reads(string &s) {     s =   ;     int n = scanf( %s , BUFFER);     if (n == 1) s = BUFFER;     return n == 1;   }   bool readln(string &s) {     char *valid = gets(BUFFER);     if (valid) s = BUFFER;     return ((bool)valid);   }   int Print(long long a, long long b) {     cout << a <<     << b;     return 0;   }   void update(long long &su, long long n, long long mod) {     su *= n;     su %= mod;   }   int doit(long long n, long long mod) {     long long su = 1;     int i;     for (i = 1; i < n; i++) {       su *= 10;       su %= mod;     }     update(su, 3, mod);     update(su, 3, mod);     update(su, 5, mod);     update(su, 2, mod);     update(su, 3, mod);     update(su, 3, mod);     su++;     su %= mod;     long long a = 1, b = 1;     for (i = 0; i < n; i++) b *= 10;     if (0 == su) return Print(a, b);     su = mod - su;     return Print(a + su, b + su);   }   int main() {     long long a;     cin >> a;     doit(18, a);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long n;     cin >> n;     vector<long long> a(n), b(n);     for (long long i = 0; i < n; ++i) cin >> a[i];     for (long long i = 0; i < n; ++i) cin >> b[i];     if (n == 1) {       if (a[0] == b[0])         cout <<  SMALL n  << 0;       else         cout <<  IMPOSSIBLE ;       return 0;     }     if (n == 2) {       vector<pair<long long, bool>> res;       long long kol = 0;       while (true) {         if (b[0] == 0 || b[1] == 0) {           cout <<  IMPOSSIBLE ;           return 0;         }         if (a == b) break;         if (a[0] == b[1] && a[1] == b[0]) {           res.emplace_back(1, false);           break;         }         if (b[0] == b[1]) {           cout <<  IMPOSSIBLE ;           return 0;         }         if (b[0] > b[1]) {           res.emplace_back(1, false);           swap(b[0], b[1]);         }         if (a[0] == b[0] && a[1] < b[1] && a[1] % b[0] == b[1] % b[0]) {           res.emplace_back((b[1] - a[1]) / b[0], true);           kol += res.back().first;           break;         }         if (a[1] == b[0] && a[0] < b[1] && a[0] % b[0] == b[1] % b[0]) {           res.emplace_back((b[1] - a[0]) / b[0], true);           kol += res.back().first;           res.emplace_back(1, false);           break;         }         kol += b[1] / b[0];         res.emplace_back(b[1] / b[0], true);         b[1] %= b[0];       }       if (kol > 2e5) {         cout <<  BIG n ;         cout << kol;       } else {         cout <<  SMALL n ;         long long flex = 0;         for (auto i : res) flex += i.first;         cout << flex <<   n ;         for (long long i = res.size() - 1; i >= 0; --i) {           for (long long j = 0; j < res[i].first; ++j) {             if (res[i].second)               cout <<  P ;             else               cout <<  R ;           }         }       }       return 0;     }     vector<bool> ans;     long long kol = 0;     while (true) {       if (a == b) break;       reverse(b.begin(), b.end());       if (a == b) {         ans.push_back(false);         break;       }       reverse(b.begin(), b.end());       bool vozr = false, ub = false, r = false;       for (long long i = 1; i < n; ++i) {         if (b[i] > b[i - 1])           vozr = true;         else if (b[i] < b[i - 1])           ub = true;         else           r = true;       }       if (r || (vozr && ub)) {         cout <<  IMPOSSIBLE ;         return 0;       }       vector<long long> c(n);       if (ub) {         ans.push_back(false);         reverse(b.begin(), b.end());       }       c[0] = b[0];       for (long long i = 1; i < n; ++i) {         c[i] = b[i] - b[i - 1];       }       ans.push_back(true);       b = c;       ++kol;     }     if (kol > 2e5) {       cout <<  BIG n  << kol;     } else {       cout <<  SMALL n  << ans.size() <<   n ;       for (long long i = ans.size() - 1; i >= 0; --i) {         if (ans[i])           cout <<  P ;         else           cout <<  R ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n, p[200009], b[200009], ans, u[200009], sum;   void dfs(int v) {     if (u[v] == 1) {       ans++;       return;     }     u[v] = 1;     dfs(p[v]);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> p[i];     for (int i = 1; i <= n; i++) cin >> b[i];     for (int i = 1; i <= n; i++) {       if (!u[p[i]]) {         dfs(p[i]);       }     }     if (ans == 1) ans = 0;     for (int i = 1; i <= n; i++) sum += b[i];     if (sum % 2 == 0) ans++;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int h1, a1, c1;     int h2, a2;     cin >> h1 >> a1 >> c1;     cin >> h2 >> a2;     int th1 = h1;     int th2 = h2;     int count = 0;     while (th2 > 0) {       if (th1 > a2 || th2 - a1 <= 0) {         count++;         th2 = th2 - a1;         th1 = th1 - a2;       } else {         count++;         th1 = th1 + c1;         th1 = th1 - a2;       }     }     cout << count <<   n ;     while (h2 > 0) {       if (h1 > a2 || h2 - a1 <= 0) {         h2 = h2 - a1;         h1 = h1 - a2;         cout <<  STRIKE n ;       } else {         h1 = h1 + c1;         h1 = h1 - a2;         cout <<  HEAL n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, i, j, curr, n;     cin >> curr >> n;     int arr[curr + 1];     int temp;     arr[0] = 1;     int count = curr - 1;     for (i = 1; i <= curr; i++) arr[i] = 0;     for (i = 0; i < n; i++) {       cin >> temp;       if (temp == 1) {         int a, b;         cin >> a >> b;         arr[a] = 1;         arr[b] = 1;         count = count - 2;       } else {         int a;         cin >> a;         arr[a] = 1;         count = count - 1;       }     }     arr[curr] = 1;     int min = 0;     for (i = 1; i < curr; i++) {       if (arr[i] == 0 && arr[i + 1] == 0) {         min += 1;         i++;         continue;       } else if (arr[i] == 0) {         min += 1;       }     }     cout << min <<     << count << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[10001];   int best = 100000000;   int main() {     int n, m, d;     scanf( %d %d %d , &n, &m, &d);     scanf( %d , &a[0]);     int c = a[0] % d;     int lll = n * m;     for (int i = 1; i < lll; i++) {       scanf( %d , &a[i]);       if (a[i] % d != c) {         cout << -1 << endl;         return 0;       }     }     for (int i = 0; i < lll; i++) {       int ans = 0;       int lo = a[i];       for (int j = 0; j < lll; j++) {         ans += abs(a[j] - lo) / d;       }       best = min(ans, best);     }     cout << best << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     pair<int, int> s[n + 1];     for (int a = 0; a < n; a++) {       int x, y;       cin >> x >> y;       s[a] = make_pair(max(x - y, 1), min(x + y, m));     }     sort(s, s + n);     int dp[m + 1];     dp[m] = 0;     for (int a = m - 1; a >= 0; a--) {       bool cover = 0;       int minima = m - a;       for (int b = 0; b < n; b++) {         if (s[b].first <= (a + 1) && s[b].second >= (a + 1)) {           cover = 1;           break;         } else if (s[b].first > (a + 1)) {           minima = min(minima, dp[min(m, s[b].second + s[b].first - (a + 1))] +                                    s[b].first - (a + 1));         }       }       if (cover) {         dp[a] = dp[a + 1];       } else         dp[a] = minima;     }     cout << dp[0];   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());   const int N = 3e5;   int n, a[N + 1], b[N + 1], c[N + 1];   int dp[N + 1];   vector<int> g1[N + 1], g2[N + 1];   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];     }     vector<pair<int, int> > st;     for (int i = 1; i <= n; ++i) {       while (!st.empty() && st.back().first < a[i]) {         st.pop_back();       }       if (!st.empty()) {         b[i] = st.back().second;       }       st.emplace_back(a[i], i);     }     st.clear();     for (int i = 1; i <= n; ++i) {       while (!st.empty() && st.back().first > a[i]) {         st.pop_back();       }       if (!st.empty()) {         c[i] = st.back().second;       }       st.emplace_back(a[i], i);     }     st.clear();     for (int i = n; i >= 1; --i) {       while (!st.empty() && st.back().first < a[i]) {         st.pop_back();       }       if (!st.empty()) {         g1[st.back().second].emplace_back(i);       }       st.emplace_back(a[i], i);     }     st.clear();     for (int i = n; i >= 1; --i) {       while (!st.empty() && st.back().first > a[i]) {         st.pop_back();       }       if (!st.empty()) {         g2[st.back().second].emplace_back(i);       }       st.emplace_back(a[i], i);     }     dp[1] = 0;     for (int i = 2; i <= n; ++i) {       dp[i] = dp[i - 1] + 1;       if (b[i]) dp[i] = min(dp[i], dp[b[i]] + 1);       if (c[i]) dp[i] = min(dp[i], dp[c[i]] + 1);       for (int x : g1[i]) {         if (x >= b[i]) {           dp[i] = min(dp[i], dp[x] + 1);         }       }       for (int x : g2[i]) {         if (x >= c[i]) {           dp[i] = min(dp[i], dp[x] + 1);         }       }     }     cout << dp[n] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {-1, 0, 0, 1};   int dy[] = {0, -1, 1, 0};   int dx1[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};   int dy1[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};   const double eps = 1e-9;   const int inf = 2000000000;   const long long int infLL = 9000000000000000000;   const long long int MOD = 1e+7;   const double PI = 3.141592653589793238462643383279;   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     int ans = 2 * n - m + 1;     for (long long int i = 0; i < m; i++) {       int k;       cin >> k;       for (long long int j = 0; j < k; j++) {         int a;         cin >> a;         if (a == j + 1) ans -= 2;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void __print(int x) { cout << x; }   void __print(long x) { cout << x; }   void __print(long long x) { cout << x; }   void __print(unsigned x) { cout << x; }   void __print(unsigned long x) { cout << x; }   void __print(unsigned long long x) { cout << x; }   void __print(float x) { cout << x; }   void __print(double x) { cout << x; }   void __print(long double x) { cout << x; }   void __print(char x) { cout <<      << x <<     ; }   void __print(const char *x) { cout <<     << x <<    ; }   void __print(const string &x) { cout <<     << x <<    ; }   void __print(bool x) { cout << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     __print(x.first);     cout <<  , ;     __print(x.second);   }   template <typename T>   void __print(const T &x) {     int f = 0;     for (auto &i : x) cout << (f++ ?  ,  :   ), __print(i);   }   void _print() { cout <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cout <<  ,  ;     _print(v...);   }   const int N = 2e5 + 7;   const int MOD = 1e9 + 7;   long long fastpow(long long b, long long p) {     long long temp;     if (p == 0) return 1;     if (p == 1) return b;     temp = fastpow(b, p / 2);     temp = temp * temp % MOD;     if (p & 1) {       temp = temp * b % MOD;     }     return temp;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n, k;     cin >> n >> k;     string s;     cin >> s;     vector<char> v(k);     for (int i = 0; i < k; i++) cin >> v[i];     vector<int> temp(n);     for (int i = 0; i < n; i++) {       if (count(v.begin(), v.end(), s[i]))         temp[i] = 1;       else         temp[i] = 0;     }     long long ans = 0;     for (int i = 0; i < n; i++) {       if (temp[i] == 1) {         long long j = i;         while (j < n && temp[j] == 1) j++;         ans += ((j - i) * (j - i + 1)) / 2;         i = j;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[101];   int main() {     bool f = false;     int n, counter = 1;     long long ans = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] == 0 && f) counter++;       if (a[i] == 1 && !f)         f = true, ans = 1;       else if (a[i] == 1 && f)         ans *= counter, counter = 1;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int tt;     cin >> tt;     while (tt--) {       int n;       cin >> n;       vector<int> a(n + 1);       for (int i = 1; i <= n; i++) {         cin >> a[i];       }       int lo = 1, hi = n;       while (a[lo] == lo && lo <= n) lo++;       while (a[hi] == hi && hi >= 0) hi--;       if (lo == n + 1 || hi == -1) {         cout << 0 <<   n ;         continue;       }       int ans = 1;       for (int i = lo; i <= hi; i++) {         if (a[i] == i) {           ans++;           break;         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 9;   vector<int> ans[N];   vector<char> arr;   int k, n, q, a, hash[N][50], ar[N], s;   char c;   string str;   bool flag;   int main() {     cin >> k;     cin >> str;     int n = str.size();     for (int i = 1; i <= k; i++)       for (int j = 0; j < n; j++) arr.push_back(str[j]);     for (int i = 0; i < arr.size(); i++) ans[arr[i]].push_back(i);     cin >> q;     for (int i = 1; i <= q; i++) {       cin >> a >> c;       arr[ans[c][a - 1]] =  0 ;       ans[c].erase(ans[c].begin() + a - 1);     }     for (int i = 0; i < arr.size(); i++)       if (arr[i] !=  0 ) cout << arr[i];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e5 + 7;   const long long INF = (long long)1e18;   int n;   int m;   vector<pair<int, int>> g[N];   long long dist[N];   int par[N];   int t[N];   int root(int x) {     if (t[x] == x) {       return x;     } else {       return t[x] = root(t[x]);     }   }   void unite(int x, int y) { t[root(x)] = root(y); }   struct T {     int x;     long long d;   };   bool operator<(T a, T b) { return a.d > b.d; }   struct edge {     int x;     int y;     long long d;   };   bool operator<(edge a, edge b) { return a.d < b.d; }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int x, y, z;       cin >> x >> y >> z;       g[x].push_back({y, z});       g[y].push_back({x, z});     }     for (int i = 1; i <= n; i++) {       t[i] = i;       dist[i] = INF;     }     priority_queue<T> pq;     int cnt;     cin >> cnt;     for (int i = 1; i <= cnt; i++) {       int portal;       cin >> portal;       dist[portal] = 0;       par[portal] = portal;       pq.push({portal, 0});     }     while (!pq.empty()) {       int x = pq.top().x;       long long d = pq.top().d;       pq.pop();       if (d != dist[x]) {         continue;       }       for (auto &it : g[x]) {         int y = it.first;         long long dy = dist[x] + it.second;         if (dy < dist[y]) {           dist[y] = dy;           par[y] = par[x];           pq.push({y, dy});         }       }     }     vector<edge> e;     for (int i = 1; i <= n; i++) {       for (auto &it : g[i]) {         int j = it.first;         int c = it.second;         if (par[i] != par[j]) {           e.push_back({par[i], par[j], c + dist[i] + dist[j]});         }       }     }     long long sol = dist[1];     sort(e.begin(), e.end());     for (auto &it : e) {       if (root(it.x) != root(it.y)) {         unite(it.x, it.y);         sol += it.d;       }     }     cout << sol <<   n ;     return 0;   }


#include <bits/stdc++.h>   const long long mod = 1e9 + 9;   using namespace std;   const long long N = 105;   long long vis[N], l[N], who[N], n, m;   void fail() {     cout <<  -1 n ;     exit(0);   }   int32_t main() {     cin >> n >> m;     for (long long i = 1; i <= m; i++) cin >> l[i];     long long cur = l[1];     for (long long i = 2; i <= m; i++) {       long long dif = (l[i] - cur);       if (dif <= 0) dif += n;       if (vis[dif] && vis[dif] != cur) fail();       who[cur] = dif;       vis[dif] = cur;       cur = l[i];     }     long long cnt = 0;     set<long long> st;     for (long long i = 1; i <= n; i++) {       if (!vis[i]) {         st.insert(i);       }       if (!who[i]) cnt++;     }     auto it = st.begin();     if (cnt > st.size()) fail();     for (long long i = 1; i <= n; i++) {       if (who[i])         cout << who[i];       else {         cout << *it;         it++;       }       cout <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100086;   int z[maxn], n, m, num, order[maxn][2], last[maxn], ans[maxn], len, in[maxn];   bool flag, ok[maxn];   char s[2];   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %s , s);       scanf( %d , &num);       if (s[0] ==  - ) {         if (z[num] == 0) z[num] = -1;         order[i][0] = -1;         order[i][1] = num;       } else {         if (z[num] == 0) z[num] = 1;         order[i][0] = 1;         order[i][1] = num;       }     }     num = 0;     for (int i = 1; i <= n; i++) {       if (z[i] == 0)         ans[++len] = i;       else {         if (z[i] == -1) {           in[i] = 1;           num++;         }         ok[i] = 1;       }     }     for (int i = 1; i <= m; i++) {       if (order[i][0] == -1) {         num--;         in[order[i][1]] = 0;         if (num) ok[order[i][1]] = 0;         last[order[i][1]] = i;         if ((i != m) && (order[i + 1][1] != order[i][1])) ok[order[i][1]] = 0;       } else {         in[order[i][1]] = 1;         if (num) ok[order[i][1]] = 0;         if (last[order[i][1]] != i - 1) ok[order[i][1]] = 0;         num++;         last[order[i][1]] = i;       }     }     for (int i = 1; i <= n; i++)       if (ok[i]) ans[++len] = i;     sort(ans + 1, ans + 1 + len);     printf( %d n , len);     if (len) {       for (int i = 1; i < len; i++) printf( %d  , ans[i]);       printf( %d , ans[len]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g[1002];   bool vis[1002];   bool my[1002], chen[1002];   int dfs(int n) {     if (my[n] == 1) return n;     vis[n] = 1;     for (int i = 0; i < g[n].size(); i++) {       int v = g[n][i];       if (!vis[v]) {         int ans = dfs(v);         if (ans != -1) return ans;       }     }     return -1;   }   void make() {     memset((vis), 0, sizeof(vis));     memset((my), 0, sizeof(my));     memset((chen), 0, sizeof(chen));     for (int i = 0; i < 1002; i++) g[i].clear();   }   int main() {     int t, i, j;     cin >> t;     while (t--) {       make();       int n, k1, k2;       cin >> n;       for (i = 0; i < n - 1; i++) {         int u, v;         cin >> u >> v;         g[u].push_back(v);         g[v].push_back(u);       }       cin >> k1;       for (i = 0; i < k1; i++) {         int u;         cin >> u;         my[u] = 1;       }       cin >> k2;       for (i = 0; i < k2; i++) {         int u;         cin >> u;         chen[u] = 1;       }       int q;       for (i = 1; i <= n; i++)         if (chen[i] == 1) break;       cout <<  B   << i << endl;       cout.flush();       cin >> q;       if (my[q] == 1) {         cout <<  C   << q << endl;         cout.flush();         continue;       }       int ans = dfs(q);       cout <<  A   << ans << endl;       cout.flush();       cin >> q;       if (chen[q] == 1) {         cout <<  C   << ans << endl;         cout.flush();       } else {         cout <<  C   << -1 << endl;         cout.flush();       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int sasha_chiz, masha_chiz, coconut_price, sasha_buy_co,         masha_buy_co, sum_buy_co, given_chiz = 0;     cin >> sasha_chiz >> masha_chiz >> coconut_price;     sasha_buy_co = sasha_chiz / coconut_price;     masha_buy_co = masha_chiz / coconut_price;     if (sasha_chiz % coconut_price != 0 && masha_chiz % coconut_price != 0) {       sasha_chiz = sasha_chiz % coconut_price;       masha_chiz = masha_chiz % coconut_price;       if (sasha_chiz + masha_chiz >= coconut_price) {         if (sasha_chiz >= masha_chiz) {           given_chiz = coconut_price - sasha_chiz;           sasha_buy_co = sasha_buy_co + 1;         } else {           given_chiz = coconut_price - masha_chiz;           masha_buy_co = masha_buy_co + 1;         }       }     }     sum_buy_co = sasha_buy_co + masha_buy_co;     cout << sum_buy_co <<     << given_chiz;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   string s;   long long nb, nc, ns, push_back, pc, ps, money, cntb, cntc, cnts, l, r, mid, mb,       ms, mc;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     cin >> s >> nb >> ns >> nc >> push_back >> ps >> pc >> money;     for (size_t i = 0; i < s.length(); i++) {       if (s[i] ==  B )         cntb++;       else if (s[i] ==  S )         cnts++;       else         cntc++;     }     l = 0;     r = 1e14;     while (l <= r) {       mid = (l + r) / 2;       mb = max(0ll, (cntb * mid - nb) * push_back);       ms = max(0ll, (cnts * mid - ns) * ps);       mc = max(0ll, (cntc * mid - nc) * pc);       if (mc + mb + ms <= money)         l = mid + 1;       else         r = mid - 1;     }     cout << r;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = int(1e6) + 50, P = int(1e9) + 7;   long long fac_NESC[N], rev[N], pow_NESC[N], revfac[N], w;   int k;   inline void inc(long long &x, long long y) { x = (x + y) % P; }   inline long long M(long long x, long long y) { return x * y % P; }   inline long long S(long long x) { return x * x % P; }   inline long long fpow(long long x, long long y) {     long long r(1);     for (; y; y >>= 1, x = M(x, x))       if (y & 1) r = M(r, x);     return r;   }   int main() {     ios::sync_with_stdio(false);     cin >> k >> w;     rev[1] = 1;     for (int i = 2; i <= k; ++i) rev[i] = ((-rev[P % i] * (P / i)) % P + P) % P;     fac_NESC[0] = 1;     for (int i = 1; i <= k; ++i) fac_NESC[i] = M(fac_NESC[i - 1], i);     pow_NESC[0] = fpow(k, w);     for (int i = 1; i <= k + 20; ++i) pow_NESC[i] = M(pow_NESC[i - 1], rev[k]);     revfac[0] = 1;     for (int i = 1; i <= k; ++i) revfac[i] = M(revfac[i - 1], rev[i]);     long long res(0);     for (int t = 1; t <= k; ++t) {       if (t <= w - 1)         inc(res, M(M(M(t, S(fac_NESC[k])), pow_NESC[t + 1]), S(revfac[k - t])));       if (t > w - 1)         inc(res,             M(M(M(fac_NESC[k], w), fac_NESC[k - t + w - 1]), S(revfac[k - t])));       if (t + 1 <= w - 1)         inc(res, M(M(M(t, S(fac_NESC[k])), pow_NESC[t + 2]),                    M(revfac[k - t], revfac[k - t - 1])));       if (t + 1 > w - 1)         inc(res, M(M(M(fac_NESC[k], w - 1), fac_NESC[k - t + w - 2]),                    M(revfac[k - t], revfac[k - t - 1])));     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void read(long long &x) {     x = 0;     char c = getchar();     long long f = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -f;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + c -  0 ;       c = getchar();     }     x *= f;     return;   }   void print(long long x) {     if (x < 10) {       putchar(x +  0 );       return;     }     print(x / 10);     putchar(x % 10 +  0 );   }   void write(long long x) {     if (x < 0) x = -x, putchar( - );     print(x);   }   long long maxt(long long a, long long b) { return a < b ? b : a; }   long long mint(long long a, long long b) { return a < b ? a : b; }   struct node {     long long v, id;     node(long long V, long long Id) { v = V, id = Id; }   };   stack<long long> q, r;   vector<node> R[1000005], G[1000005];   vector<long long> E[1000005];   long long n, m, dfn[1000005], low[1000005], cnt, w[1000005], s[1000005],       bel[1000005], f[1000005], e[1000005], dep[1000005], col;   void tarjan(long long u, long long fa) {     q.push(u);     dfn[u] = low[u] = ++cnt;     for (int i = 0; i < R[u].size(); i++) {       long long v = R[u][i].v, id = R[u][i].id;       if (v == fa) continue;       r.push(id);       if (!dfn[v]) {         tarjan(v, u);         low[u] = mint(low[u], low[v]);         if (low[v] > dfn[u]) {           ++col;           while (r.top() != id) {             if (w[r.top()]) s[col] = 1;             r.pop();           }           r.pop();           while (q.top() != v) {             bel[q.top()] = col;             E[col].push_back(q.top());             q.pop();           }           bel[v] = col;           E[col].push_back(v);           q.pop();         }       } else         low[u] = mint(low[u], dfn[v]);     }     if (fa == -1) {       ++col;       while (!r.empty()) {         if (w[r.top()]) s[col] = 1;         r.pop();       }       while (!q.empty()) {         bel[q.top()] = col;         E[col].push_back(q.top());         q.pop();       }     }   }   void dfs(long long u, long long fa) {     f[u] = fa;     dep[u] = dep[fa] + 1;     for (int i = 0; i < G[u].size(); i++) {       long long v = G[u][i].v, id = G[u][i].id;       if (v == fa) continue;       e[v] = id;       dfs(v, u);     }   }   int main() {     read(n), read(m);     for (int i = 1; i <= m; i++) {       long long u, v;       read(u), read(v), read(w[i]);       R[u].push_back(node(v, i));       R[v].push_back(node(u, i));     }     for (int i = 1; i <= n; i++)       if (!dfn[i]) tarjan(i, -1);     for (int i = 1; i <= col; i++)       for (int j = 0; j < E[i].size(); j++) {         long long u = E[i][j];         for (int k = 0; k < R[u].size(); k++) {           long long v = R[u][k].v, id = R[u][k].id;           if (bel[u] != bel[v]) G[bel[u]].push_back(node(bel[v], id));         }       }     dfs(1, 1);     long long a, b;     bool fl = 0;     read(a), read(b);     a = bel[a], b = bel[b];     if (dep[a] < dep[b]) swap(a, b);     while (dep[a] > dep[b]) {       if (w[e[a]] || s[a]) fl = 1;       a = f[a];     }     while (a != b) {       if (w[e[a]] || w[e[b]] || s[a] || s[b]) fl = 1;       a = f[a];       b = f[b];     }     if (s[a]) fl = 1;     if (fl)       puts( YES );     else       puts( NO );   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int x = 0;     bool flg = false;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) flg = true;     for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);     return flg ? -x : x;   }   int n;   struct P {     long long x, y;   } p[100010];   P operator-(const P &x, const P &y) { return P{x.x - y.x, x.y - y.y}; }   long long cross(const P &x, const P &y) { return x.x * y.y - x.y * y.x; }   long long dot(const P &x, const P &y) { return x.x * y.x + x.y * y.y; }   long long dot(const P &x) { return x.x * x.x + x.y * x.y; }   bool solve() {     n = read();     for (int i(1), _i(n); i <= _i; i++) {       int x = read(), y = read();       p[i] = P{x, y};     }     p[n + 1] = p[1];     if (n & 1) return 0;     int m = n >> 1;     for (int i(1), _i(m); i <= _i; i++) {       if (cross(p[i + m + 1] - p[i + m], p[i + 1] - p[i])) return 0;       if (dot(p[i + m + 1] - p[i + m], p[i + 1] - p[i]) >= 0) return 0;       if (dot(p[i + m + 1] - p[i + m]) != dot(p[i + 1] - p[i])) return 0;     }     return 1;   }   int main() {     for (int T = 1; T--;) puts(solve() ?  yes  :  no );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   string s;   int pre[N], nxt0[N], now = 100002;   int ans[N];   int n;   void init() {     vector<int> v;     for (int i = n - 1; i >= 1; i--) {       int p = s[i] -  0 ;       int pos = upper_bound(v.begin(), v.end(), -p) - v.begin();       if (pos == v.size()) {         v.push_back(-p);       } else {         v[pos] = -p;       }       ans[i] = v.size();     }   }   int main() {     ios::sync_with_stdio(false);     cin >> s;     s =  *  + s;     n = s.length();     init();     for (int i = 1; i < n; i++) pre[i] = pre[i - 1] + (s[i] ==  1 );     for (int i = n - 1; i >= 1; i--) {       nxt0[i] = now;       if (s[i] ==  0 ) now = i;     }     for (int i = 1; i <= n; i++) {       if (s[i] ==  0 ) continue;       if (s[i - 1] ==  1 ) continue;       int zuo1 = pre[i - 1], you1 = pre[n - 1] - pre[i],           you0 = (n - 1 - i) - you1;       int youmx = ans[nxt0[i]];       if (youmx <= you1) {         s[i] =  0 ;       }     }     for (int i = 1; i < n; i++) cout << s[i];   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> a(n);       for (int &elem : a) cin >> elem;       vector<vector<int>> dp(n, vector<int>(2, -1));       function<int(int, int)> solve = [&](int idx, int turn) -> int {         if (idx >= n) {           return 0;         }         if (dp[idx][turn] != -1) return dp[idx][turn];         int ans = INT_MAX;         if (turn == 1) {           if (a[idx] == 1) {             ans = min(ans, 1 + solve(idx + 1, 0));             if (idx + 1 < n && a[idx + 1] == 1) {               ans = min(ans, 2 + solve(idx + 2, 0));             } else {               ans = min(ans, 1 + solve(idx + 2, 0));             }           } else {             ans = min(ans, solve(idx + 1, 0));             if (idx + 1 < n)               ans = min(ans, (a[idx + 1] == 1) + solve(idx + 2, 0));           }         } else {           ans = min(ans, solve(idx + 1, 1));           ans = min(ans, solve(idx + 2, 1));         }         return dp[idx][turn] = ans;       };       cout << solve(0, 1) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, cnt11 = 0, cnt10 = 0, cnt01 = 0;     string s, t;     cin >> n >> s >> t;     for (int i = 0; i < 2 * n; ++i) {       if (s[i] ==  1  && t[i] ==  1 )         ++cnt11;       else if (s[i] ==  1 )         ++cnt10;       else if (t[i] ==  1 )         ++cnt01;     }     if (cnt11 & 1) {       if (cnt10 >= cnt01)         puts( First );       else if (cnt10 + 1 == cnt01 || cnt10 + 2 == cnt01)         puts( Draw );       else         puts( Second );     } else {       if (cnt10 == cnt01 || cnt10 + 1 == cnt01)         puts( Draw );       else if (cnt01 < cnt10)         puts( First );       else         puts( Second );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   int n1, n2, n3;   int c[510];   int sc[510];   int pai[100010];   int type[100010];   int peso[100010];   int dp[510][510];   vector<pair<pair<int, int>, int> > edges;   int find(int i) { return pai[i] = (pai[i] == i) ? i : find(pai[i]); }   void join(int i, int j) {     i = find(i);     j = find(j);     if (i == j) return;     if (peso[i] < peso[j]) swap(i, j);     pai[j] = i;     if (peso[i] == peso[j]) peso[i]++;   }   void init() {     for (int g = 1; g <= n; g++) {       pai[g] = g;       peso[g] = 0;     }   }   int main() {     scanf( %d %d %d , &n, &m, &k);     init();     for (int g = 1; g <= k; g++) {       scanf( %d , &c[g]);       sc[g] = sc[g - 1] + c[g];     }     for (int g = 0; g < m; g++) {       scanf( %d %d %d , &n1, &n2, &n3);       edges.push_back(make_pair(make_pair(n1, n2), n3));       if (n3 == 0) join(n1, n2);     }     for (int g = 1; g <= k; g++)       for (int h = sc[g - 1] + 1; h <= sc[g]; h++) type[h] = g;     bool error = false;     for (int g = 1; g < n; g++)       if (type[g] == type[g + 1] && find(g) != find(g + 1)) error = true;     if (error) {       printf( No n );       return 0;     }     printf( Yes n );     for (int g = 1; g <= k; g++)       for (int h = 1; h <= k; h++) dp[g][h] = 1000000010;     for (int g = 1; g <= k; g++) dp[g][g] = 0;     for (int g = 0; g < m; g++) {       int i = edges[g].first.first;       int j = edges[g].first.second;       int w = edges[g].second;       i = type[i];       j = type[j];       if (i != j) dp[i][j] = dp[j][i] = min(dp[i][j], w);     }     for (int p = 1; p <= k; p++)       for (int q = 1; q <= k; q++)         for (int r = 1; r <= k; r++)           dp[q][r] = min(dp[q][r], dp[q][p] + dp[p][r]);     for (int q = 1; q <= k; q++) {       for (int p = 1; p <= k; p++)         printf( %d  , (dp[p][q] == 1000000010) ? -1 : dp[p][q]);       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   int sum, i, j, a[5][5], n, b[20];   int check() {     int res = 0;     int i, j, nowsum;     for (i = 0; i < n; i++)       for (j = 0; j < n; j++) {         a[i][j] = b[i * n + j];       }     for (i = 0; i < n; i++) {       nowsum = 0;       for (j = 0; j < n; j++) nowsum += a[i][j];       res += abs(sum - nowsum);     }     for (j = 0; j < n; j++) {       nowsum = 0;       for (i = 0; i < n; i++) nowsum += a[i][j];       res += abs(sum - nowsum);     }     nowsum = 0;     for (i = 0; i < n; i++) nowsum += a[i][i];     res += abs(nowsum - sum);     nowsum = 0;     for (i = 0; i < n; i++) nowsum += a[i][n - i - 1];     res += abs(nowsum - sum);     return res;   }   int main() {     srand((unsigned)time(NULL));     cin >> n;     for (i = 0; i < n * n; i++) {       cin >> b[i];       sum += b[i];     }     sum /= n;     int q, p, i, j;     q = check();     while (q) {       random_shuffle(b, b + n * n);       q = check();       for (int iter = 0; iter < 1000; iter++) {         i = (rand() * 257) % (n * n);         j = (rand() * 37) % (n * n);         while (j == i) {           j = (rand() * 37) % (n * n);         }         if (i > j) swap(i, j);         swap(b[i], b[j]);         p = check();         if (p < q) {           q = p;         } else {           swap(b[i], b[j]);         }       }       q = check();     }     cout << sum << endl;     for (i = 0; i < n; i++) {       for (j = 0; j < n; j++) cout << a[i][j] <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = int(3002), mod = int(1e9) + 7;   char second[N][N];   int n, m;   long long dp1[N][N], dp2[N][N], a, b, c, d;   bool used[N][N];   inline long long dp(int x, int y) {     if (x <= 0 || y <= 0) return 0;     if (second[x][y] ==  # ) return 0;     if (x == 1 && y == 2) return 1;     if (used[x][y]) return dp1[x][y];     used[x][y] = 1;     return dp1[x][y] = (dp(x - 1, y) + dp(x, y - 1)) % mod;   }   inline long long rec(int x, int y) {     if (x <= 0 || y <= 0) return 0;     if (second[x][y] ==  # ) return 0;     if (x == 2 && y == 1) return 1;     if (used[x][y]) return dp2[x][y];     used[x][y] = 1;     return dp2[x][y] = (rec(x - 1, y) + rec(x, y - 1)) % mod;   }   int main() {     scanf( %d%d n , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %s n , second[i] + 1);     }     a = rec(n, m - 1), c = rec(n - 1, m);     memset(used, 0, sizeof(used));     b = dp(n - 1, m), d = dp(n, m - 1);     a = a * b % mod;     c = c * d % mod;     a = (a - c) % mod;     if (a < 0) a += mod;     printf( %d , a);     return 0;   }


#include <bits/stdc++.h>   using namespace std;      const int M = 1000000007;   struct mint {        int val = 0;        mint() {}        mint(int a) : val(a) {       while (val < 0) {         val += M;       }       while (val >= M) {         val -= M;       }     }        mint(long long a) : val(a % M) {       if (val < 0) {         val += M;       }     }        mint operator+=(mint oth) {       val += oth.val;       val -= (val >= M) ? M : 0;       return *this;     }        mint operator-=(mint oth) {       val -= oth.val;       val += (val < 0) ? M : 0;       return *this;     }        mint operator*=(mint oth) {       val = 1LL * val * oth.val % M;       return *this;     }        void operator++() {       ++val;       if (val == M) {         val = 0;       }     }        void operator--() {       --val;       if (val == -1) {         val = M - 1;       }     }        mint operator-() {       mint res;       res.val = (val == 0) ? 0 : M - val;       return res;     }        friend mint operator+(mint a, mint b) {       return mint(a) += b;     }        friend mint operator-(mint a, mint b) {       return mint(a) -= b;     }        friend mint operator*(mint a, mint b) {       return mint(a) *= b;     }        friend bool operator==(mint a, mint b) {       return a.val == b.val;     }        friend bool operator!=(mint a, mint b) {       return a.val != b.val;     }        friend ostream& operator<<(ostream &out, mint a) {       out << a.val;       return out;     }        friend mint pow(mint b, long long e = M - 2) {       mint res = 1;       while (e > 0) {         if (e % 2 == 1) {           res *= b;         }         b *= b;         e /= 2;       }       return res;     }      };      const int N = 40 + 1;      using state = tuple<int, int, int, int, map<int, int>>;      mint choose[N][N], fact[N];   bool visited[N];   int perm[N], n;      mint calc(int k) {     return choose[n][k + 1] * fact[k] * fact[n - k - 1];   }      int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);        for (int i = 0; i < N; ++i) {       choose[i][0] = choose[i][i] = 1;       fact[i] = (i == 0) ? 1 : fact[i - 1] * i;       for (int j = 1; j < i; ++j) {         choose[i][j] = choose[i - 1][j - 1] + choose[i - 1][j];       }     }        cin >> n;        for (int i = 0; i < n; ++i) {       cin >> perm[i];       --perm[i];     }        map<int, int> cycles;     for (int i = 0; i < n; ++i) {       if (!visited[i]) {         int j = i, l = 0;         while (!visited[j]) {           visited[j] = true;           j = perm[j];           ++l;         }         ++cycles[l];       }     }        mint ans = 0;     cycles[n + 1] = 0;     map<state, mint> ways;     ways.insert({{0, cycles.begin()->first, 0, 0, cycles}, 1});        while (!ways.empty()) {       auto [s, x] = *ways.begin();       auto [i, j, p, c, count] = s;       ways.erase(ways.begin());          if (j > n) {         if (c > 0) {           if (i + c == n) {             ans += x;           } else {             state t = {i + c, count.begin()->first, c, 0, count};             ways[t] += x;           }         }       } else {         mint y = pow(calc(n - i - 1), j);         if (p > 0) {           y -= pow(calc(n - i - 1 + p), j);         }            state t = {i, count.upper_bound(j)->first, p, c, count};         for (int k = 0; k <= count[j]; ++k) {           ways[t] += x * pow(y, k) * choose[count[j]][k];           get<3>(t) += j;           --get<4>(t)[j];         }       }     }        cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1200005;   const int OO = 0x3f3f3f3;   int vis[maxn];   vector<int> p;   int N = 1200000;   void find_prime() {     p.clear();     memset(vis, 0, sizeof(vis));     for (int i = 2; i <= N; i++)       if (!vis[i])         for (int j = i * 2; j <= N; j += i) vis[j] = 1;     for (int i = 2; i <= N; i++)       if (!vis[i]) p.push_back(i);   }   vector<int> palin;   void find_palin() {     palin.clear();     for (int i = 1; i <= 9; i++) {       palin.push_back(i);       palin.push_back(i * 11);     }     for (int i = 1; i <= 9; i++)       for (int j = 0; j <= 9; j++) {         palin.push_back(i * 101 + j * 10);         palin.push_back(i * 1001 + j * 110);       }     for (int i = 1; i <= 9; i++)       for (int j = 0; j <= 9; j++)         for (int k = 0; k <= 9; k++) {           palin.push_back(i * 10001 + j * 1010 + k * 100);           palin.push_back(i * 100001 + j * 10010 + k * 1100);         }     for (int i = 0; i <= 1; i++)       for (int j = 0; j <= 9; j++)         for (int k = 0; k <= 9; k++) {           palin.push_back(1000001 + i * 100010 + j * 10100 + k * 1000);         }     sort(palin.begin(), palin.end());   }   int cnt_prime[maxn], cnt_palin[maxn];   int main() {     find_prime();     find_palin();     memset(cnt_palin, 0, sizeof(cnt_palin));     memset(cnt_prime, 0, sizeof(cnt_prime));     for (int i = 2; i <= N; i++) cnt_prime[i] = cnt_prime[i - 1] + 1 - vis[i];     memset(vis, 0, sizeof(vis));     for (int i = 0; i < palin.size(); i++) vis[palin[i]] = 1;     for (int i = 1; i <= N; i++) cnt_palin[i] = cnt_palin[i - 1] + vis[i];     int sz = palin.size();     int ans = -1;     int x, y;     scanf( %d%d , &x, &y);     for (int i = sz - 1; i >= 0; i--)       if (cnt_prime[palin[i]] * y <= cnt_palin[palin[i]] * x) {         ans = palin[i];         break;       }     sz = p.size();     for (int i = sz - 1; i >= 0; i--)       if (cnt_prime[p[i] - 1] * y <= cnt_palin[p[i] - 1] * x) {         if (p[i] - 1 > ans) ans = p[i] - 1;         break;       }     if (ans == -1)       printf( Palindromic tree is better than splay tree n );     else       printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, i, j;     while (~scanf( %d%d , &n, &k)) {       if (n <= 2 * k) {         printf( -1 n );         continue;       } else {         printf( %d n , n * k);         for (i = 1; i <= n; i++) {           for (j = 1; j <= k; j++) {             if (i == n)               printf( %d %d n , i, j);             else if (i + j > n)               printf( %d %d n , i, i + j - n);             else               printf( %d %d n , i, i + j);           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, flag = 0;     vector<int> A, B;     map<int, int> store;     store.clear();     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a);       if (store[a] < 2) {         if (store[a] == 0) {           A.push_back(a);           store[a] = 1;         } else {           flag = 1;           B.push_back(a);           store[a] = 2;         }       }     }     sort(A.begin(), A.end());     sort(B.begin(), B.end());     reverse(B.begin(), B.end());     int ans = A.size() + B.size();     if (flag)       if (A[A.size() - 1] == B[0]) ans--;     cout << ans << endl;     for (int i = 0; i < A.size(); i++) {       cout << A[i] <<    ;     }     for (int i = 0; i < B.size(); i++) {       if (i == 0 && B[i] == A[A.size() - 1]) continue;       cout << B[i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 2;   int n, a[N];   long long pref[N];   int get(int pos) {     int lo = 0, hi = min(pos - 1, n - pos);     while (hi - lo > 3) {       int m1 = (lo + lo + hi) / 3;       int m2 = (lo + hi + hi) / 3;       long double r1 = (pref[pos] - pref[pos - m1 - 1] + pref[n] - pref[n - m1]) /                        double(m1 * 2 + 1);       long double r2 = (pref[pos] - pref[pos - m2 - 1] + pref[n] - pref[n - m2]) /                        double(m2 * 2 + 1);       r1 -= a[pos];       r2 -= a[pos];       if (r1 < r2)         lo = m1;       else         hi = m2;     }     int ret = lo;     for (int i = lo; i <= hi; i++) {       long double r1 =           (pref[pos] - pref[pos - ret - 1] + (pref[n] - pref[n - ret])) /           double(ret * 2 + 1);       long double r2 = (pref[pos] - pref[pos - i - 1] + (pref[n] - pref[n - i])) /                        double(i * 2 + 1);       r1 -= a[pos];       r2 -= a[pos];       if (r1 < r2) ret = i;     }     return ret;   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n; i++) {       pref[i] = pref[i - 1] + a[i];     }     long double mx = 0;     pair<int, int> p = {1, 0};     for (int i = 1; i <= n; i++) {       int best = get(i);       long double ret =           (pref[i] - pref[i - best - 1] + (pref[n] - pref[n - best])) /           double(best * 2 + 1);       ret -= a[i];       if (mx <= ret) {         mx = ret;         p = {i, best};       }     }     vector<int> ans;     for (int i = p.first; i >= (p.first - p.second); i--) ans.push_back(a[i]);     for (int i = n; i > (n - p.second); i--) ans.push_back(a[i]);     sort(ans.begin(), ans.end());     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<pair<long long, long long>>> adj;   vector<long long> d;   long long n;   vector<bool> mark;   vector<long long> trains;   void dijktras(long long s) {     d[s] = 0;     priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,                    greater<pair<long long, long long>>>         pq;     pq.push({0, s});     for (long long i = 2; i <= n; i++) {       if (trains[i] != INT_MAX) pq.push({trains[i], i});     }     while (!pq.empty()) {       long long v = pq.top().second;       long long d_v = pq.top().first;       pq.pop();       if (d_v != d[v]) continue;       for (auto edge : adj[v]) {         long long to = edge.first;         long long len = edge.second;         if (d[to] > d[v] + len) {           d[to] = d[v] + len;           pq.push({d[to], to});         } else if (d[to] == d[v] + len) {           if (d[to] == trains[to]) mark[to] = true;         }       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long m, k;     cin >> n >> m >> k;     adj.resize(n + 1);     d.assign(n + 1, INT_MAX);     mark.assign(n + 1, false);     while (m--) {       long long u, v, w;       cin >> u >> v >> w;       adj[u].push_back({v, w});       adj[v].push_back({u, w});     }     long long ans = 0;     trains.assign(n + 1, INT_MAX);     while (k--) {       long long s, y;       cin >> s >> y;       if (d[s] == INT_MAX) {         d[s] = y;         trains[s] = y;       } else {         ans++;         trains[s] = min(trains[s], y);         d[s] = min(d[s], y);       }     }     dijktras(1);     for (int i = 2; i <= n; i++)       if (d[i] < trains[i] && trains[i] != INT_MAX) {         mark[i] = true;       }     for (int i = 2; i <= n; i++)       if (mark[i]) ans++;     cout << ans <<   n ;     cout <<   n ;     return 0;   }