#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int a, b, c, d, m, n, p, q, r, x, y, z, aa, bb, cc, dd;     double pp, qq, rr, xx, yy, zz;     char ch1, ch2, ch3, ch4, ch5;     long long int i, j, k, l;     long long int cnt = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0, sum = 0, sum2 = 0,                   mx = LLONG_MIN, mn = LLONG_MAX;     long long int flag = 0, flag2 = 0;     vector<long long int> vec1, vec2, vec3;     string s1, s2, s3;     cin >> n;     for (long long int i = (long long int)(0); i < (long long int)(n); ++i) {       cin >> x;       vec1.push_back(x);     }     for (long long int i = (long long int)(0); i < (long long int)(n); ++i) {       cin >> x;       vec2.push_back(x);     }     if (n == 1) {       cout <<  0 n ;       return;     }     for (long long int i = (long long int)(n - 1); i >= (long long int)(0); --i) {       sum += vec1[i];       vec1[i] = sum;     }     sum = 0;     for (long long int i = (long long int)(0); i < (long long int)(n); ++i) {       sum += vec2[i];       vec2[i] = sum;     }     for (long long int i = (long long int)(0); i < (long long int)(n - 2); ++i) {       mx = max(vec2[i], vec1[i + 2]);       mn = min(mn, mx);     }     mn = min(mn, vec1[1]);     mn = min(mn, vec2[n - 2]);     cout << mn << endl;   }   int main() {     ios::sync_with_stdio(false);     long long int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   const long long int mx = 1e9 + 7;   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     int n, t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       vector<pair<int, int>> vp, vpAns;       map<int, int> mp, mpAns, mpCnt;       int A[n + 2], x = n / k;       for (int i = 0; i < n; i++) {         cin >> A[i];         vp.push_back(make_pair(A[i], i));       }       sort(vp.begin(), vp.end());       int cnt = 0, cn = 0;       for (int i = 0; i < n; i++) {         int a = vp[i].first, b = vp[i].second;         mp[a]++;         if (mp[a] > k) {           vpAns.push_back(make_pair(b, 0));           continue;         }         cnt++;         vpAns.push_back(make_pair(b, cnt));         mpCnt[cnt]++;         if (cnt == k) cnt = 0;       }       int mn = 10000000;       for (auto u : mpCnt) {         mn = min(mn, u.second);       }       sort(vpAns.begin(), vpAns.end());       for (int i = 0; i < vpAns.size(); i++) {         int b = vpAns[i].second;         mpAns[b]++;         if (mpAns[b] > mn)           cout << 0 <<    ;         else           cout << vpAns[i].second <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 100;   const int mod = 1e9 + 7;   int t;   int n;   int m;   long long p[maxn];   int size[maxn];   vector<int> g[maxn];   void dfs(int u, int pre) {     size[u] = 1;     for (int v : g[u]) {       if (v == pre) continue;       dfs(v, u);       size[u] += size[v];     }   }   struct node {     long long x, y;     bool operator<(const node &r) const {       return (long long)x * y > (long long)r.x * r.y;     }   } Node[maxn];   bool cmp(int x, int y) { return x > y; }   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       for (int i = 1; i <= n; i++) g[i].clear();       for (int i = 1; i < n; i++) {         int x, y;         scanf( %d%d , &x, &y);         g[x].push_back(y);         g[y].push_back(x);       }       scanf( %d , &m);       for (int i = 1; i <= m; i++) scanf( %lld , p + i);       sort(p + 1, p + m + 1, cmp);       dfs(1, 0);       int tot = 0;       for (int i = 2; i <= n; i++)         Node[++tot] = {(long long)n - size[i], (long long)size[i]};       sort(Node + 1, Node + tot + 1);       long long ans = 0;       if (n - 1 >= m) {         for (int i = 1; i <= m; i++) {           ans += (long long)p[i] * Node[i].x * Node[i].y;           ans %= mod;         }         for (int i = m + 1; i <= tot; i++) {           ans += (long long)Node[i].x * Node[i].y;           ans %= mod;         }       } else {         long long tt = 1;         for (int i = 1; i <= (m - n + 2); i++) {           tt *= p[i];           tt %= mod;         }         ans += (long long)tt * Node[1].x % mod * Node[1].y;         ans %= mod;         for (int i = m - n + 3; i <= m; i++) {           ans +=               (long long)p[i] * Node[i - (m - n + 1)].x * Node[i - (m - n + 1)].y;           ans %= mod;         }       }       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   int S[100009], cnt[1 << 7], num[1 << 7];   char s[100009];   int len, n, nu;   inline long long rd() {     long long x = 0;     char c = getchar();     bool f = 0;     while (!isdigit(c)) {       if (c ==  - ) f = 1;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + (c ^ 48);       c = getchar();     }     return f ? -x : x;   }   int main() {     scanf( %s , s + 1);     len = strlen(s + 1);     int x;     int maxn = (1 << 6) - 1;     for (int i = 1; i <= len; ++i) {       int x = s[i] -  a ;       for (int j = 1; j <= maxn; ++j)         if (j & (1 << x)) cnt[j]++;     }     n = rd();     for (int i = 1; i <= n; ++i) {       x = rd();       scanf( %s , s + 1);       int xx = strlen(s + 1);       for (int j = 1; j <= xx; ++j) S[x] |= (1 << (s[j] -  a ));       for (int j = 1; j <= maxn; ++j)         if ((j & S[x]) == S[x]) num[j]++;     }     for (int i = 1; i <= len; ++i)       if (!S[i]) {         S[i] = maxn;         num[S[i]]++;       }     for (int i = 1; i <= len; ++i) {       bool tag = 0;       for (int j = 0; j < 6; ++j)         if ((1 << j) & S[i]) {           bool tg = 0;           for (int k = 1; k <= maxn; ++k) {             int x = (k & (1 << j)) ? cnt[k] - 1 : cnt[k],                 y = ((k & S[i]) == S[i]) ? num[k] - 1 : num[k];             if (x < y) {               tg = 1;               break;             }           }           if (!tg) {             tag = 1;             s[++nu] = j +  a ;             for (int k = 1; k <= maxn; ++k) {               if ((1 << j) & k) cnt[k]--;               if ((S[i] & k) == S[i]) num[k]--;             }             break;           }         }       if (!tag) {         puts( Impossible );         return 0;       }     }     for (int i = 1; i <= nu; ++i) printf( %c , s[i]);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     int a[n];     vector<int> ans;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     ans.push_back(a[0]);     for (int i = 1; i < n; i++) {       if (a[i] != ans.back()) ans.push_back(a[i]);     }     bool got = false;     int len = ans.size();     for (int i = 0; i < len - 2; i++) {       if ((ans[i + 1] - ans[i] < 3 && ans[i + 2] - ans[i] < 3 &&            ans[i + 2] - ans[i + 1] < 3)) {         got = true;         break;       }     }     cout << (got ?  YES n  :  NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     int cnt = 0, count = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  8 ) cnt++;     }     while (cnt) {       n = n - 1;       cnt = cnt - 1;       if (n / 10 > 0) {         count++;         n = n - 10;       }     }     cout << count;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   long long m, x, p[22], a[22], b[22], len, res = 1;   map<long long, int> Map[22];   inline void init(long long n) {     for (register long long i = 2; i * i <= n; i++)       if (n % i == 0) {         len++;         p[len] = i;         while (n % i == 0) n /= i, a[len]++;       }     if (n > 1) len++, p[len] = n, a[len] = 1;   }   inline void init2(long long n, int tp) {     for (register long long i = 2; i * i <= n; i++) {       while (n % i == 0) Map[tp][i]++, n /= i;     }     if (n > 1) Map[tp][n]++;   }   inline long long mul(long long a, long long n, long long mo) {     long long res = 0;     n %= mo;     a %= mo;     if (mo <= 2000000000) return a * n % mo;     while (n) {       if (n & 1) res = res + a < mo ? res + a : res + a;       a = a + a < mo ? a + a : a + a - mo;       n >>= 1;     }     return res % mo;   }   inline long long power(long long a, long long n, long long mo) {     long long res = 1;     if (mo == 1) return 0;     n %= mo - 1;     while (n) {       if (n & 1) res = mul(res, a, mo);       a = mul(a, a, mo);       n >>= 1;     }     return res;   }   inline void dfs(int dep, long long d) {     if (dep > len) {       if (d == 1) return;       map<long long, int> fac;       for (register int i = 1; i <= (len); i++)         if (b[i]) {           if (b[i] - 1) fac[p[i]] += b[i] - 1;           for (map<long long, int>::iterator it = Map[i].begin();                it != Map[i].end(); it++)             fac[(*it).first] += (*it).second;         }       static long long c[22];       int tot = 0;       long long phi = 1;       for (map<long long, int>::iterator it = fac.begin(); it != fac.end();            it++) {         tot++, c[tot] = (*it).first;         for (register int i = 1; i <= ((*it).second); i++) phi *= c[tot];       }       long long k = phi;       for (register int i = 1; i <= (tot); i++) {         while (k % c[i] == 0 && power(x, k / c[i], d) == 1) k /= c[i];       }       res += phi / k;       return;     }     for (register int i = 0; i <= (a[dep]); i++)       b[dep] = i, dfs(dep + 1, d), d *= p[dep], b[dep] = 0;   }   int main() {     scanf( %lld%lld , &m, &x);     init(m);     for (register int i = 1; i <= (len); i++) init2(p[i] - 1, i);     dfs(1, 1);     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   const int M = 1000010;   const int MAX = M;   struct BIT {     int bit[MAX + 1], N;     BIT() {}     BIT(int N) : N(N) { memset(bit, 0, sizeof(bit)); }     int sum(int i) {       i++;       int ret = 0;       while (i) {         ret += bit[i];         i -= i & -i;       }       return ret;     }     int sum(int i, int j) { return sum(j - 1) - sum(i - 1); }     void add(int i, int x) {       i++;       while (i <= N) {         bit[i] += x;         i += i & -i;       }     }     int find(int i) {       int lb = -1, ub = N, mid;       while (ub - lb > 1) {         mid = (lb + ub) / 2;         if (sum(mid) >= i)           ub = mid;         else           lb = mid;       }       return ub;     }   };   int n, m, a[M], seq[M], k[M];   int len, ns, ms;   BIT b;   void solve() {     if (!len) {       puts( Poor stack! );       return;     }     b = BIT(ns);     for (int i = (int)(0); i < (int)(ns); i++) b.add(i, 1);     for (int i = (int)(0); i < (int)(ms); i++) {       for (int j = k[i] - 1; j >= 0; --j) {         int pos = b.find(a[j]);         b.add(pos, -1);       }     }     for (int i = (int)(0); i < (int)(ns); i++)       if (b.sum(i, i + 1)) printf( %d , seq[i]);     puts(  );   }   void input() {     scanf( %d%d , &n, &m);     for (int i = (int)(0); i < (int)(m); i++) scanf( %d , a + i);     for (int i = (int)(0); i < (int)(n); i++) {       scanf( %d , seq + ns);       if (seq[ns] < 0) {         if (a[0] > len) continue;         k[ms] = lower_bound(a, a + m, len + 1) - a;         len -= k[ms++];       } else         ++len, ++ns;     }   }   int main() {     input();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> newvec;     int n;     int x;     int ans;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x;       newvec.push_back(x);     }     sort(newvec.begin(), newvec.end());     if (newvec.size() % 2 == 1) {       ans = newvec[newvec.size() / 2];     } else {       ans = newvec[newvec.size() / 2 - 1];     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = pow(10, 9) + 7;   long long fac[1001];   template <typename T, typename U>   static inline void amin(T &x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   static inline void amax(T &x, U y) {     if (x < y) x = y;   }   void SieveOfEratosthenes(long long n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (long long p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (long long i = p * p; i <= n; i += p) prime[i] = false;       }     }     for (long long p = 2; p <= n; p++)       if (prime[p])         ;   }   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long modInverse(long long n, long long p) { return power(n, p - 2, p); }   long long nCrModPFermat(long long n, long long r, long long p) {     if (n < r) return 0;     if (r == 0) return 1;     return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %            p;   }   void dfs(long long v, vector<vector<long long>> &adj, vector<long long> &used,            vector<long long> &maxD) {     for (long long u : adj[v]) {       if (!used[u]) {         used[u] = 1;         if (adj[u].size() == 1 && u != 1) {           maxD[u] = 0;           amax(maxD[v], 1 + maxD[u]);         } else {           dfs(u, adj, used, maxD);           amax(maxD[v], 1 + maxD[u]);         }       }     }   }   void solve() {     long long n, x, i;     cin >> n >> x;     vector<vector<long long>> adj(n + 1);     long long u, v;     for (i = 2; i <= n; i++) {       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     vector<long long> d(n + 1);     d[1] = 0;     vector<long long> visited(n + 1);     vector<long long> parent(n + 1);     visited[1] = 1;     parent[1] = 0;     queue<long long> q;     q.push(1);     while (!q.empty()) {       long long p = q.front();       q.pop();       for (auto el : adj[p]) {         if (!visited[el]) {           visited[el] = 1;           q.push(el);           d[el] = d[p] + 1;           parent[el] = p;         }       }     }     vector<long long> maxD(n + 1), used(n + 1);     used[1] = 1;     dfs(1, adj, used, maxD);     long long f = 1, ans = max(maxD[x] * 2, (abs(d[1] - d[x]) + maxD[x]) * 2),               start = x;     while (f) {       long long el = parent[x];       if (abs(d[el] - d[1]) > abs(d[el] - d[start])) {         long long z = max(maxD[el] * 2, (abs(d[1] - d[el]) + maxD[el]) * 2);         amax(ans, z);         x = el;       } else {         f = 0;         break;       }     }     cout << ans << endl;     return;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long test = 1;     switch (test) {       case 1:         solve();         break;       case 2: {         long long t;         cin >> t;         while (t--) {           solve();         }       }; break;     }   }


#include <bits/stdc++.h>   using namespace std;   int T;   int n;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> T;     while (T--) {       cin >> n;       float FloatN = n;       int Num8 = ceil(FloatN / 4);       int Num9 = n - Num8;       for (int i = 0; i < Num9; ++i) {         cout << 9;       }       for (int i = 0; i < Num8; ++i) {         cout << 8;       }       cout << endl;     }   }


#include <bits/stdc++.h>   #pragma GCC optimize(3)   #pragma GCC optimize(2)   #pragma GCC optimize( Ofast )   using namespace std;   vector<int> g[2097312];   int a[2097312][2];   char st[2097312];   int f[2097312];   int find(int x) {     if (f[x] == x) return x;     return f[x] = find(f[x]);   }   int main() {     int n, i, j, cnt;     scanf( %d , &n);     scanf( %s , st + 1);     for (i = n; i >= 1; --i) {       if (st[i] !=  1 ) {         a[i][0] = a[i + 1][0] + 1;       }       if (st[i] !=  0 ) {         a[i][1] = a[i + 1][1] + 1;       }       g[max(a[i][1], a[i][0])].push_back(i);       f[i] = i;     }     f[n + 1] = n + 1;     for (i = 1; i <= n; ++i) {       j = 1, cnt = 0;       while (j <= n) {         j = find(j);         if (j > n) {           break;         }         cnt++;         j += i;       }       printf( %d  , cnt);       for (auto v : g[i]) {         f[v] = v + 1;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long a[n], b[n];       for (int i = 0; i < n; i++) cin >> a[i];       for (int i = 0; i < n; i++) cin >> b[i];       long long flag = 0, p = 0, ne = 0;       if (a[0] != b[0]) {         flag = 1;         cout <<  NO  << endl;         continue;       }       if (a[0] == 1) p++;       if (a[0] == -1) ne++;       for (int i = 1; i < n; i++) {         if ((a[i] > b[i] && ne == 0) || (a[i] < b[i] && p == 0)) {           flag = 1;           cout <<  NO  << endl;           break;         }         if (a[i] == 1) p++;         if (a[i] == -1) ne++;       }       if (flag == 0) cout <<  YES  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int sum = 0, p = 1;     char ch = getchar();     while (!(( 0  <= ch && ch <=  9 ) || ch ==  - )) ch = getchar();     if (ch ==  - ) p = -1, ch = getchar();     while ( 0  <= ch && ch <=  9 ) sum = sum * 10 + ch - 48, ch = getchar();     return sum * p;   }   const int maxn = 52;   const int mod = 1e9 + 7;   int n, f[maxn][maxn][maxn], g[maxn][maxn], d[maxn], dp[maxn][maxn][maxn],       jc[maxn], jcn[maxn], inv[maxn];   inline int C(int n, int m) {     return (long long)jc[n] * jcn[m] % mod * jcn[n - m] % mod;   }   inline int power(int a, int b) {     int ans = 1;     while (b) {       if (b & 1) ans = (long long)ans * a % mod;       b >>= 1;       a = (long long)a * a % mod;     }     return ans;   }   inline void init() {     n = read();     for (int i = (1), _end_ = (n); i <= _end_; i++) d[i] = read();     jc[0] = jc[1] = jcn[0] = jcn[1] = inv[1] = 1;     for (int i = (2), _end_ = (50); i <= _end_; i++)       jc[i] = (long long)i * jc[i - 1] % mod,       inv[i] = (long long)(mod - mod / i) * inv[mod % i] % mod,       jcn[i] = (long long)inv[i] * jcn[i - 1] % mod;     f[0][0][0] = 1;     for (int k = (2), _end_ = (50); k <= _end_; k++)       if (~k & 1) f[0][0][k] = (long long)f[0][0][k - 2] * (k - 1) % mod;     for (int j = (1), _end_ = (50); j <= _end_; j++)       for (int k = (0), _end_ = (50); k <= _end_; k++)         if (~k & 1) {           if (k >= 2)             f[0][j][k] =                 (f[0][j][k] + (long long)f[0][j - 1][k - 2] * C(k, 2)) % mod;           if (j >= 2)             f[0][j][k] =                 (f[0][j][k] + (long long)f[0][j - 2][k] * (j - 1) * k) % mod;           if (j >= 3)             f[0][j][k] =                 (f[0][j][k] + (long long)f[0][j - 3][k + 2] * C(j - 1, 2)) % mod;         }     for (int i = (1), _end_ = (50); i <= _end_; i++)       for (int j = (0), _end_ = (50); j <= _end_; j++)         for (int k = (0), _end_ = (50); k <= _end_; k++)           f[i][j][k] = ((j ? (long long)f[i - 1][j - 1][k + 1] * j : 0) +                         (k ? (long long)f[i - 1][j][k - 1] * k : 0)) %                        mod;   }   inline void doing() {     dp[1][1][1] = 1;     if (d[1] == 2) {       dp[2][3][2] = 1;     } else {       dp[2][4][3] = 1;     }     for (int i = (3), _end_ = (n); i <= _end_; i++) {       for (int j = (i), _end_ = (n); j <= _end_; j++) {         for (int k = (1), _end_ = (j); k <= _end_; k++) {           int A = 0, B = 0;           for (int l = (1), _end_ = (j - k); l <= _end_; l++) {             (d[j - k - l + 1] == 2 ? B : A)++;             dp[i][j][k] =                 (dp[i][j][k] + (long long)dp[i - 1][j - k][l] * f[k][A][B]) % mod;           }         }       }     }     int ans = 0;     for (int i = (2), _end_ = (n); i <= _end_; i++) {       int A = 0, B = 0;       for (int k = (1), _end_ = (n); k <= _end_; k++) {         (d[n - k + 1] == 2 ? B : A)++;         ans = (ans + (long long)dp[i][n][k] * f[0][A][B]) % mod;       }     }     printf( %d n , ans);   }   int main() {     init();     doing();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   bool go(int x, multiset<int> &hand, deque<int> &dq) {     if (hand.find(x) == hand.end()) return false;     hand.erase(hand.find(x));     hand.insert(dq.front());     dq.pop_front();     dq.push_back(x);     return true;   }   multiset<int> hand;   deque<int> dq;   int Try() {     multiset<int> hand1 = hand;     deque<int> dq1 = dq;     int pos = -1;     for (int i = 0; i < n; i++) {       if (dq[i] == 1) pos = i;     }     if (pos == -1) return -1;     for (int i = pos; i < n; i++)       if (dq[i] != i - pos + 1) return -1;     int done = n - pos;     int ans = 0;     while (done < n) {       ++done;       ++ans;       if (!go(done, hand1, dq1)) return -1;     }     return ans;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       hand.insert(x);     }     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       dq.push_back(x);     }     int ans = Try();     if (ans != -1) {       cout << ans << endl;       return 0;     }     ans = 0;     while (hand.find(1) == hand.end()) {       assert(go(0, hand, dq));       ans++;     }     int mx = n;     for (int i = 0; i < n; i++) {       if (dq[i] == 0) continue;       int cur = i + 1 + (n + 1 - dq[i]);       mx = max(mx, cur);     }     cout << ans + mx << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300005;   pair<long long, long long> dif[maxn];   long long x[maxn], y[maxn], pref[maxn], suff[maxn], ans[maxn];   vector<int> adj[maxn];   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %lld %lld , &x[i], &y[i]);       dif[i] = {y[i] - x[i], i};     }     for (int i = 1; i <= m; i++) {       int u, v;       scanf( %d %d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);     }     sort(dif + 1, dif + n + 1);     for (int i = 1; i <= n; i++) {       pref[i] = pref[i - 1] + y[dif[i].second];     }     for (int i = n; i >= 1; i--) {       suff[i] = suff[i + 1] + x[dif[i].second];     }     for (int i = 1; i <= n; i++) {       int u = dif[i].second;       long long res =           pref[i - 1] + suff[i + 1] + 1ll * (i - 1) * x[u] + 1ll * (n - i) * y[u];       for (auto v : adj[u]) {         res -= min(x[u] + y[v], x[v] + y[u]);       }       ans[u] = res;     }     for (int i = 1; i <= n; i++) {       printf( %lld  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     int ans = -1;     int sum = 7;     int i = 1;     while (sum <= n) {       int y = n - sum;       if (y % 4 == 0) ans = i;       sum += 7;       i++;     }     if (ans != -1) {       int s = 7 * ans;       int y = n - s;       int x = y / 4;       while (x--) {         cout <<  4 ;       }       while (ans--) {         cout <<  7 ;       }       return 0;     }     if (n % 7 == 0) {       int x = n / 7;       while (x--) cout <<  7 ;       return 0;     }     if (n % 4 == 0) {       int x = n / 4;       while (x--) cout <<  4 ;       return 0;     }     cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string vow =  aeiou ;   const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};   const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};   const int dx[] = {-1, 0, 0, 1};   const int dy[] = {0, -1, 1, 0};   const long double pie = 3.14159265358979;   const long long mod = 1e9 + 7;   void solve(int test_case) {     int k;     cin >> k;     string s;     cin >> s;     int n = s.size();     vector<int> pref(n + 1);     for (int i = 1; i < n + 1; i++) pref[i] = pref[i - 1] + s[i - 1] -  0 ;     long long ans = 0;     for (int i = 0; i < n; i++) {       int x = pref[i + 1];       int val = x - k;       if (val < 0) continue;       ;       int ans_indx = -1;       for (int lo = 0, hi = i; lo <= hi;) {         int mid = lo + hi >> 1;         if (pref[mid] > val)           hi = mid - 1;         else if (pref[mid] == val)           ans_indx = mid, hi = mid - 1;         else           lo = mid + 1;       }       if (ans_indx == -1) continue;       ;       long long L = ans_indx;       ans_indx = -1;       for (int lo = 0, hi = i; lo <= hi;) {         int mid = lo + hi >> 1;         if (pref[mid] == val)           ans_indx = mid, lo = mid + 1;         else if (pref[mid] < val)           lo = mid + 1;         else           hi = mid - 1;       }       if (ans_indx == -1) continue;       ;       long long R = ans_indx;       ans += R - L + 1;     }     cout << ans;     cout <<   n ;   }   int main() {     int t = 1;     for (int i = 0; i < t; i++) solve(i);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, flag = 1;     char ch = getchar();     for (; ch >  9  || ch <  0 ; ch = getchar())       if (ch ==  - ) flag = -1;     for (; ch >=  0  && ch <=  9 ; ch = getchar())       x = (x << 3) + (x << 1) + ch -  0 ;     return x * flag;   }   const int MAXN = 2e5 + 50, B = 317, INF = 998244353;   int n, m, Ans[MAXN], rt, cnt = 0;   int XX[MAXN], YY[MAXN], DY[MAXN], Cl = 0;   struct Point {     int x, y, id;   } P[MAXN];   struct Ques {     int X1, Y1, X2, Y2, id, h;   } Q[MAXN];   struct Tree {     int X, Y, Min, ls, rs, dat, fa;     int mx[2], my[2];     void clean() {       X = Y = ls = rs = mx[1] = my[1] = 0;       mx[0] = my[0] = dat = Min = INF;     }   } T[MAXN];   int MIN(int a, int b) { return a < b ? a : b; }   bool cmp3(Point a, Point b) { return a.y < b.y; }   bool cmp2(Point a, Point b) { return a.x < b.x; }   bool cmp(Ques a, Ques b) { return a.h < b.h; }   void update(int cur) {     T[cur].Min = MIN(T[T[cur].rs].Min, T[T[cur].ls].Min);     T[cur].Min = MIN(T[cur].Min, T[cur].dat);     T[cur].mx[0] = MIN(MIN(T[T[cur].rs].mx[0], T[T[cur].ls].mx[0]), T[cur].mx[0]);     T[cur].my[0] = MIN(MIN(T[T[cur].rs].my[0], T[T[cur].ls].my[0]), T[cur].my[0]);     T[cur].mx[1] = max(max(T[T[cur].rs].mx[1], T[T[cur].ls].mx[1]), T[cur].mx[1]);     T[cur].my[1] = max(max(T[T[cur].rs].my[1], T[T[cur].ls].my[1]), T[cur].my[1]);   }   void build(int &cur, int L, int R, int D, int fa) {     if (L > R) return;     if (!cur) cur = ++cnt;     int mid = (L + R) >> 1;     T[cur].fa = fa;     if (L == R) {       int x = P[L].x, y = P[L].y;       T[cur].mx[0] = T[cur].mx[1] = T[cur].X = x;       T[cur].my[0] = T[cur].my[1] = T[cur].Y = y;       T[cur].dat = T[cur].Min = P[L].id;       DY[P[L].id] = cur;       return;     }     if (D == 0)       nth_element(P + L, P + mid, P + 1 + R, cmp2);     else       nth_element(P + L, P + mid, P + 1 + R, cmp3);     T[cur].dat = P[mid].id, T[cur].X = P[mid].x, T[cur].Y = P[mid].y;     T[cur].mx[0] = T[cur].mx[1] = T[cur].X;     T[cur].my[0] = T[cur].my[1] = T[cur].Y;     DY[P[mid].id] = cur;     build(T[cur].ls, L, mid - 1, D ^ 1, cur);     build(T[cur].rs, mid + 1, R, D ^ 1, cur);     update(cur);     return;   }   bool out(int x, int X1, int Y1, int X2, int Y2) {     return (T[x].mx[0] > X2 || T[x].mx[1] < X1 || T[x].my[0] > Y2 ||             T[x].my[1] < Y1);   }   bool in(int x, int X1, int Y1, int X2, int Y2) {     return (T[x].mx[0] >= X1 && T[x].mx[1] <= X2 && T[x].my[0] >= Y1 &&             T[x].my[1] <= Y2);   }   int query(int now, int X1, int Y1, int X2, int Y2) {     int Min = INF;     if (T[now].X >= X1 && T[now].Y >= Y1 && T[now].X <= X2 && T[now].Y <= Y2)       Min = T[now].dat;     if (out(now, X1, Y1, X2, Y2) || !now) return Min;     if (in(now, X1, Y1, X2, Y2)) return min(T[now].Min, T[now].dat);     if (T[now].ls) Min = MIN(Min, query(T[now].ls, X1, Y1, X2, Y2));     if (T[now].rs) Min = MIN(Min, query(T[now].rs, X1, Y1, X2, Y2));     return Min;   }   void Go(int x, int t) {     if (!x) return;     if (T[x].dat == t) T[x].dat = INF;     update(x);     Go(T[x].fa, t);     return;   }   void clean() {     for (int i = 1; i <= cnt; i++) T[i].clean();     cnt = 0;   }   int main() {     n = read();     T[0].clean();     for (int i = 1; i <= n; i++) Ans[i] = -1;     for (int i = 1; i <= n; i++) {       Q[i].X1 = read(), Q[i].X2 = read();       Q[i].Y1 = read(), Q[i].Y2 = read();       Q[i].h = read(), Q[i].id = i;     }     rt = 0;     m = read();     sort(Q + 1, Q + 1 + n, cmp);     for (int i = 1; i <= m; i++) {       XX[i] = P[i].x = read(), YY[i] = P[i].y = read();       P[i].id = i;     }     build(rt, 1, m, 0, 0);     for (int i = 1; i <= n; i++) {       int t = query(rt, Q[i].X1, Q[i].Y1, Q[i].X2, Q[i].Y2);       if (t == 998244353 || !t) continue;       Ans[t] = Q[i].id, Go(DY[t], t);     }     for (int i = 1; i <= m; i++) printf( %d n , max(Ans[i], 0));     return 0;   }


#include <bits/stdc++.h>   const int MAXN = 100010;   int head[MAXN], nxt[MAXN << 1], to[MAXN << 1], val[MAXN << 1], tot;   void addedge(int b, int e, int v) {     nxt[++tot] = head[b];     to[head[b] = tot] = e;     val[tot] = v;     nxt[++tot] = head[e];     to[head[e] = tot] = b;     val[tot] = v;   }   int n, Q;   std::pair<int, int> diameter(int u, int fa = 0) {     std::pair<int, int> res(0, u);     for (int i = head[u]; i; i = nxt[i])       if (to[i] != fa) {         std::pair<int, int> t = diameter(to[i], u);         t.first += val[i];         res = std::max(res, t);       }     return res;   }   struct TTT {     int fa[MAXN], son[MAXN], top[MAXN], dep[MAXN], sonmax[MAXN];     int dis[MAXN];     void dfs1(int u) {       for (int i = head[u]; i; i = nxt[i])         if (to[i] != fa[u]) {           fa[to[i]] = u;           dep[to[i]] = dep[u] + 1;           dis[to[i]] = dis[u] + val[i];           dfs1(to[i]);           sonmax[to[i]] += val[i];           if (sonmax[to[i]] > sonmax[son[u]]) son[u] = to[i];         }       sonmax[u] = sonmax[son[u]];     }     int down[MAXN];     void dfs2(int u) {       down[top[u]] = u;       if (son[u]) top[son[u]] = top[u], dfs2(son[u]);       for (int i = head[u]; i; i = nxt[i])         if (to[i] != fa[u] && to[i] != son[u]) top[to[i]] = to[i], dfs2(to[i]);     }     int chv[MAXN], li[MAXN], rk[MAXN], bak, pre[MAXN];     void init(int rt) {       dep[rt] = 1, top[rt] = rt;       dfs1(rt);       dfs2(rt);       for (int i = 1; i <= n; ++i)         if (i == top[i]) li[++bak] = i, chv[i] = dis[down[i]] - dis[fa[i]];       std::sort(li + 1, li + 1 + bak,                 [this](int a, int b) { return chv[a] > chv[b]; });       for (int i = 1; i <= bak; ++i)         rk[li[i]] = i, pre[i] = pre[i - 1] + chv[li[i]];     }     int solve(int u, int v) {       int lans = 0;       v = std::min(bak, v * 2 - 1);       if (rk[top[u]] <= v)         lans = pre[v];       else {         lans = pre[v - 1] + chv[top[u]];         int now = u;         while (rk[top[now]] > v) now = fa[top[now]];         int tv = pre[v] - dis[down[top[now]]] + dis[down[top[u]]];         tv = std::max(tv, pre[v - 1] - dis[now] + dis[down[top[u]]]);         lans = std::max(lans, tv);       }       return lans;     }   } t1, t2;   int main() {     std::ios_base::sync_with_stdio(false), std::cin.tie(0);     std::cin >> n >> Q;     for (int i = 1, t1, t2, t3; i < n; ++i) {       std::cin >> t1 >> t2 >> t3;       addedge(t1, t2, t3);     }     int rt1 = diameter(1).second, rt2 = diameter(rt1).second;     t1.init(rt1), t2.init(rt2);     int lans = 0;     while (Q-- > 0) {       int u, v;       std::cin >> u >> v;       u = (u + lans - 1) % n + 1;       v = (v + lans - 1) % n + 1;       lans = std::max(t1.solve(u, v), t2.solve(u, v));       std::cout << lans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   void read(int &x) {     x = 0;     int f = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) f = -f;     for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     x *= f;   }   void print(int x) {     if (x < 0) putchar( - ), x = -x;     if (!x) return;     print(x / 10), putchar(x % 10 + 48);   }   void write(int x) {     if (!x)       putchar( 0 );     else       print(x);     putchar(  n );   }   const int maxn = 1e6 + 10;   const int inf = 1e9;   const double eps = 1e-8;   const int mod = 1e9 + 7;   const int B = 400;   int a[maxn], b[maxn], n, k, q;   int c[maxn], s[maxn], ans, res;   namespace pps {   void modify(int p, int x) {     c[p] = x;     for (int i = p; i <= n; i += k) {       ans -= !(!s[i]);       if (i + k > n) res -= !s[i];       s[i] = c[i] ^ (i >= k ? s[i - k] : 0);       if (i + k > n) res += !s[i];       ans += !(!s[i]);     }   }   void solve() {     ans = n - ans;     while (q--) {       char op;       int p, v;       scanf( %c , &op), read(p), read(v);       if (op ==  a )         a[p] = v;       else         b[p] = v;       modify(p, (a[p] ^ b[p]) ^ (a[p - 1] ^ b[p - 1]));       p++;       modify(p, (a[p] ^ b[p]) ^ (a[p - 1] ^ b[p - 1]));       write(res == k ? ans : -1);     }   }   }  // namespace pps   namespace modpps {   struct block {     vector<int> r, bel, val, w;     vector<vector<int> > t;     int tag[600], m;     void init(int x) {       if (!x) x = k;       m = 0;       for (int i = x; i <= n; i += k) m++;       r.resize(m + 2), bel.resize(m + 2);       val.resize(m + 2), w.resize(m + 2);       for (int i = 1; i <= m; i++) bel[i] = (i - 1) / B + 1;       for (int i = m; i; i--) r[i] = bel[i] == bel[i + 1] ? r[i + 1] : i;       for (int i = 1; i <= m; i++) {         w[i] = s[(i - 1) * k + x];         val[i] = c[(i - 1) * k + x];       }       vector<int> et(1 << 14, 0);       for (int i = 0; i <= bel[m]; i++) t.push_back(et);       for (int i = 1; i <= m; i++) t[bel[i]][w[i]]++;     }     void modify(int x, int v) {       x = x / k + (x % k != 0);       int _ = v ^ val[x];       val[x] = v;       v = _;       res -= tag[bel[m]] == w[m];       for (int i = x; i <= r[x]; i++) {         t[bel[i]][w[i]]--;         if (w[i] == tag[bel[i]]) ans--;         w[i] ^= v;         t[bel[i]][w[i]]++;         if (w[i] == tag[bel[i]]) ans++;       }       for (int i = bel[x] + 1; i <= bel[m]; i++) {         ans -= t[i][tag[i]];         tag[i] ^= v;         ans += t[i][tag[i]];       }       res += tag[bel[m]] == w[m];     }   } s[402];   void solve() {     for (int i = 0; i < k; i++) s[i].init(i);     cerr <<  OK  << endl;     while (q--) {       char op;       int p, v;       scanf( %c , &op), read(p), read(v);       if (op ==  a )         a[p] = v;       else         b[p] = v;       s[p % k].modify(p, (a[p] ^ b[p]) ^ (a[p - 1] ^ b[p - 1]));       p++;       s[p % k].modify(p, (a[p] ^ b[p]) ^ (a[p - 1] ^ b[p - 1]));       write(res == k ? n - ans : -1);       if (q % 10000 == 0) cerr << q << endl;     }   }   }  // namespace modpps   int main() {     int st = clock();     read(n), read(k), read(q);     for (int i = 1; i <= n; i++) read(a[i]);     for (int i = 1; i <= n; i++) read(b[i]);     n++;     for (int i = 1; i <= n; i++) c[i] = (a[i] ^ b[i]) ^ (a[i - 1] ^ b[i - 1]);     for (int i = 1; i <= n; i++)       s[i] = c[i] ^ (i >= k ? s[i - k] : 0), ans += !s[i];     for (int i = n - k + 1; i <= n; i++) res += !s[i];     write(res == k ? n - ans : -1);     if (k > B)       pps ::solve();     else       modpps ::solve();     cerr << (double)(clock() - st) / 1e3 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline char gc() {     static char buf[100000], *p1 = buf, *p2 = buf;     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)                ? EOF                : *p1++;   }   inline long long read() {     long long x = 0;     char ch = getchar();     bool positive = 1;     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) positive = 0;     for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     return positive ? x : -x;   }   inline void write(long long a) {     if (a < 0) {       a = -a;       putchar( - );     }     if (a >= 10) write(a / 10);     putchar( 0  + a % 10);   }   inline void writeln(long long a) {     write(a);     puts(  );   }   inline void wri(long long a) {     write(a);     putchar(   );   }   long long t, n, nl[100010], ml[100010], nll[100010], mll[100010];   int main() {     n = read();     for (int i = 1; i <= n; i++) {       nl[i] = read();     }     for (int i = 1; i <= n; i++) {       ml[i] = read();     }     for (int i = 1; i < n; i++) {       nll[i] = nl[i + 1] - nl[i];       mll[i] = ml[i + 1] - ml[i];     }     sort(nll + 1, nll + n);     sort(mll + 1, mll + n);     int pd = 0;     for (int i = 1; i < n; i++) {       if (nll[i] != mll[i]) pd = 1;     }     if (pd == 0 && nl[1] == ml[1] && nl[n] == ml[n]) {       puts( Yes );     } else {       puts( No );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3e5 + 5;   const int mod = 1e9 + 7;   int n;   int num[MAXN];   bool vis[MAXN];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &num[i]);     int e = n;     printf( 1 );     int sum = 0;     for (int i = 1; i <= n; ++i) {       vis[num[i]] = 1;       while (vis[e] && e >= 1) {         sum++;         e--;       }       printf(  %d , i - sum + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:102400000,102400000 )   int Map[50002][7];   int main() {     int m, n;     scanf( %d%d , &m, &n);     for (int i = 1; i <= m; i++)       for (int j = 1; j <= n; j++) scanf( %d , &Map[i][j]);     for (int i = 1; i <= m; i++) Map[i][1] += Map[i - 1][1];     for (int j = 1; j <= n; j++) Map[1][j] += Map[1][j - 1];     for (int i = 2; i <= m; i++)       for (int j = 2; j <= n; j++) Map[i][j] += max(Map[i - 1][j], Map[i][j - 1]);     for (int i = 1; i <= m; i++) {       if (i > 1) putchar(   );       printf( %d , Map[i][n]);     }     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   bool cmpDouble(const double& a, const double& b) {     return std::fabs(a - b) < std::numeric_limits<double>::epsilon();   }   int main() {     int n, k;     cin >> n >> k;     int arr[5001];     int ans[5001];     for (int i = 0; i < n; ++i) {       cin >> arr[i];       ans[i] = 0;     }     map<int, set<int>> seen;     for (int i = 0; i < k; ++i) {       ans[i] = i + 1;       seen[arr[i]].insert(i + 1);     }     for (int i = k; i < n; ++i) {       if (seen[arr[i]].size() >= k) {         cout <<  NO  << endl;         return 0;       }       for (int j = 1; j <= k; ++j) {         if (seen[arr[i]].find(j) == seen[arr[i]].end()) {           ans[i] = j;           seen[arr[i]].insert(j);           break;         }       }     }     cout <<  YES  << endl;     for (int i = 0; i < n; ++i) {       cout << ans[i] <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct data {     int info;     int l, r;     data() {       info = 0;       l = 0;       r = 0;     }   };   data tr[100000 * 40];   int avail = 0;   int update(int no, int s, int e, int in, int val) {     int idx = no;     if (!no) {       idx = ++avail;       tr[idx] = data();     }     if (s == e) {       tr[idx].info += val;       return idx;     }     int mid = (s + e) / 2;     if (mid >= in) {       tr[idx].l = update(tr[idx].l, s, mid, in, val);     } else {       tr[idx].r = update(tr[idx].r, mid + 1, e, in, val);     }     tr[idx].info = (tr[tr[idx].l].info + tr[tr[idx].r].info);     return idx;   }   int query(int idx, int s, int e, int st, int en) {     if (idx == 0) return 0;     if (s >= st && e <= en) return tr[idx].info;     int mid = (s + e) / 2;     if (mid >= en)       return query(tr[idx].l, s, mid, st, en);     else if (mid < st)       return query(tr[idx].r, mid + 1, e, st, en);     else       return query(tr[idx].l, s, mid, st, mid) +              query(tr[idx].r, mid + 1, e, mid + 1, en);   }   int main() {     int n;     cin >> n;     map<int, int> mp;     while (n--) {       int x, y, z;       cin >> x >> y >> z;       if (x == 1) {         mp[z] = update(mp[z], 1, 1e9 + 7, y, 1);       } else if (x == 2) {         mp[z] = update(mp[z], 1, 1e9 + 7, y, -1);       } else {         printf( %d n , query(mp[z], 1, 1e9 + 7, 1, y));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     vector<int> a;     map<int, int> mp;     for (int i = 0; i < n; i++) {       int val;       cin >> val;       if (mp.find(val) == mp.end()) {         mp[val]++;         a.push_back(val);       }     }     sort(a.begin(), a.end());     int i = 0, sum = 0;     int g = a.size();     while (i < min(g, k)) {       int x = a[i] - sum;       cout << x << endl;       sum += x;       i++;     }     while (i < k) {       cout <<  0  << endl;       i++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int xpow(long long int a, long long int b) {     if (b == 0) return 1;     if (b % 2 == 0) {       long long int k = xpow(a, b / 2);       return k * k;     }     if (b % 2 != 0) return a * xpow(a, b - 1);   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int n, sx, sy;     cin >> n >> sx >> sy;     int l = 0, r = 0, d = 0, u = 0;     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       if (x > sx) r++;       if (x < sx) l++;       if (y > sy) u++;       if (y < sy) d++;     }     int maxi = max({l, r, d, u});     cout << maxi << endl;     if (l == maxi)       sx--;     else if (r == maxi)       sx++;     else if (u == maxi)       sy++;     else if (d == maxi)       sy--;     cout << sx <<     << sy << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     double a;     int ans = 0;     vector<int> v;     int n, m;     cin >> n;     int ara1[n];     for (int i = 0; i < n; i++) cin >> ara1[i];     cin >> m;     int ara2[m];     for (int i = 0; i < m; i++) cin >> ara2[i];     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         a = (double)ara2[i] / ara1[j];         if (a == (int)a) {           int b = a;           v.push_back(b);         }       }     }     sort(v.rbegin(), v.rend());     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         a = (double)ara2[i] / ara1[j];         if (a == v[0]) ans++;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   int main() {     int t;     scanf( %d , &t);     while (t--) {       long long int n, i, j, k, f = 0;       scanf( %lld , &n);       long long int a[n];       for (i = 0; i < n; i++) scanf( %lld , &a[i]);       for (i = 2; i < n; i++) {         if (a[i] >= a[0] + a[1]) {           f = 1;           break;         }       }       if (f == 1)         printf( 1 2 %lld n , i + 1);       else         printf( -1 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     int v = 0;     int ans = 0, ans1 = 0;     for (int i = 0; i < n; i++) {       int a;       cin >> a;       if (i < k - 1 && a != 0) ans1++;       if (i == k - 1) ans = a;       if (i >= k - 1 && a == ans && ans > 0) v++;     }     cout << v + ans1 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void pv(vector<T> &vec) {     for (int i = 0; i < (int)(vec).size(); i++)       cout << vec[i] << (i == (int)(vec).size() - 1 ?   n  :    );   }   bool ok(int x) { return (x % 10 == 7); }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int a, b, x, ans = 0;     cin >> x >> a >> b;     while (1) {       if (ok(a) || ok(b)) break;       if (b >= x)         b -= x;       else {         b = 60 - abs(b - x);         a = (a ? a - 1 : 23);       }       ans++;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sN = 1 << 19;   long long v[sN], d[sN], l[sN], r[sN], s[sN], w[sN];   int n, m;   int t, Left, Right;   long long x;   void down(int a, int c1, int c2) {     d[c1] += d[a];     d[c2] += d[a];     s[c1] += d[a] * w[c1];     s[c2] += d[a] * w[c2];     d[a] = 0;     if (v[a] > 0) v[c1] = v[c2] = v[a];   }   long long get(int a) {     if (Right < l[a] or r[a] < Left) return 0ll;     if (Left <= l[a] and r[a] <= Right) return s[a];     int c1 = a << 1, c2 = c1 + 1;     down(a, c1, c2);     return get(c1) + get(c2);   }   void update(int a) {     if (Right < l[a] or r[a] < Left) return;     if (Left <= l[a] and r[a] <= Right and v[a] > 0) {       long long inc = x - v[a];       if (inc < 0) inc = -inc;       d[a] += inc;       s[a] += inc * w[a];       v[a] = x;       return;     }     int c1 = a << 1, c2 = c1 + 1;     down(a, c1, c2);     update(c1);     update(c2);     s[a] = s[c1] + s[c2];     if (v[c1] == v[c2])       v[a] = v[c1];     else       v[a] = 0;   }   int main() {     scanf( %d %d , &n, &m);     int nn = 1;     for (; nn < n; nn <<= 1)       ;     n = nn--;     for (int i = 1; i <= n; i++)       v[nn + i] = l[nn + i] = r[nn + i] = i, w[nn + i] = 1;     for (int i = nn, j = i << 1; i > 0; i--, j = i << 1)       l[i] = l[j], r[i] = r[j + 1], w[i] = w[j] << 1;     for (; m--;) {       scanf( %d , &t);       if (t == 1) {         scanf( %d %d , &Left, &Right);         scanf( %I64d , &x);         update(1);       } else {         scanf( %d %d , &Left, &Right);         printf( %I64d n , get(1));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   const int inf = 0x3f3f3f3f;   const int mod = 998244353;   int a[N], n, L;   bool check(double x, int id) {     double s1 = 0, s2 = 0;     double t = x;     int s = 1;     for (int i = 1; i <= n + 1; i++) {       double cur = 1.0 * (a[i] - a[i - 1]) / s;       if (cur <= t) {         s1 = a[i];         t -= cur;       } else {         s1 += t * s;         break;       }       s++;     }     s = 1;     t = x;     for (int i = n; i >= 0; i--) {       double cur = 1.0 * (a[i + 1] - a[i]) / s;       if (cur <= t) {         s2 = L - a[i];         t -= cur;       } else {         s2 += t * s;         break;       }       s++;     }     return s1 + s2 >= 1.0 * L;   }   void solve() {     scanf( %d%d , &n, &L);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     a[n + 1] = L;     double l = 0, r = L;     double res = 1e9;     for (int i = 1; i <= 100; i++) {       double mid = 1.0 * (l + r) / 2;       if (check(mid, i)) {         res = mid;         r = mid;       } else         l = mid;     }     printf( %.6f n , res);   }   int main() {     int t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, i, j, v[1005];   char s[1005];   int main() {     scanf( %d n , &n);     scanf( %s , s + 1);     s[0] =  L ;     s[n] =  R ;     for (i = 1; i <= n; ++i) {       j = i - 1;       while (s[j] !=  L ) {         if (s[j] ==  R ) ++v[i];         --j;       }       int aux = 0;       j = i;       while (s[j] !=  R ) {         if (s[j] ==  L ) ++aux;         ++j;       }       v[i] = max(v[i], aux);       ++v[i];     }     for (i = 1; i <= n; ++i) printf( %d  , v[i]);   }


#include <bits/stdc++.h>   using namespace std;   long long int max(long long int a, long long int b) {     if (a < b) swap(a, b);     return a;   }   long long int min(long long int a, long long int b) {     if (a >= b) swap(a, b);     return a;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string st, c;     long long int len, k, y[29] = {0}, i;     cin >> len >> k;     cin >> st;     c = st;     sort(c.begin(), c.end());     for (i = 0; i < k; i++) {       y[c[i] - 96] += 1;     }     for (i = 0; i < len; i++) {       if (y[st[i] - 96])         y[st[i] - 96] -= 1;       else         cout << st[i];     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( -O3 )   using namespace std;   void solve() {     long long int n, k;     cin >> n >> k;     vector<long long int> a(n);     for (long long int i = 0; i < n; i++) cin >> a[i];     sort(a.begin(), a.end());     long long int ones = 0;     vector<long long int> b;     for (long long int i = 0; i < k; i++) {       long long int temp;       cin >> temp;       if (temp == 1)         ones++;       else         b.push_back(temp);     }     sort(b.begin(), b.end(), greater<long long int>());     k = b.size();     long long int ans = accumulate(a.end() - 1 - ones + 1, a.end(), 0ll) * 2;     long long int end = n - 1 - ones, beg = 0;     for (long long int i = 0; i < k; i++) {       long long int cur = b[i];       ans += (a[end]);       cur--;       end--;       if (cur == 0)         ans += (a[end + 1]);       else {         ans += a[beg];         beg += cur;       }     }     cout << ans;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int __t;     cin >> __t;     while (__t--) {       solve();       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   ostream& operator<<(ostream& os, vector<T> V) {     os <<  [  ;     for (auto v : V) os << v <<    ;     os <<  ] ;     return os;   }   template <class T>   ostream& operator<<(ostream& os, set<T> second) {     os <<  {  ;     for (auto s : second) os << s <<    ;     return os <<  } ;   }   template <class T>   ostream& operator<<(ostream& os, multiset<T> second) {     os <<  {  ;     for (auto s : second) os << s <<    ;     return os <<  } ;   }   template <class L, class R>   ostream& operator<<(ostream& os, pair<L, R> P) {     return os <<  (  << P.first <<  ,  << P.second <<  ) ;   }   template <class L, class R>   ostream& operator<<(ostream& os, map<L, R> M) {     os <<  {  ;     for (auto m : M) os <<  (  << m.first <<  :  << m.second <<  )  ;     return os <<  } ;   }   template <class L, class R>   ostream& operator<<(ostream& os, unordered_map<L, R> M) {     os <<  {  ;     for (auto m : M) os <<  (  << m.first <<  :  << m.second <<  )  ;     return os <<  } ;   }   const long long N = 2010, M = 1e9 + 7;   long long n, a[N][N], dp1[N][N], dp2[N][N], dp3[N][N];   void solve() {     cin >> n;     for (long long i = (0); i <= n - 1; i++)       for (long long j = (0); j <= n - 1; j++) cin >> a[i][j];     for (long long i = (0); i <= n - 1; i++)       dp1[i][0] = a[i][0], dp1[0][i] = a[0][i], dp2[i][n - 1] = a[i][n - 1],       dp2[0][n - i - 1] = a[0][n - i - 1];     for (long long i = (1); i <= n - 1; i++)       for (long long j = (1); j <= n - 1; j++)         dp1[i][j] = a[i][j] + dp1[i - 1][j - 1];     for (long long i = (1); i <= n - 1; i++)       for (long long j = (0); j <= n - 2; j++)         dp2[i][j] = a[i][j] + dp2[i - 1][j + 1];     for (long long i = (0); i <= n - 1; i++)       for (long long j = (0); j <= n - 1; j++) {         long long i1 = i + min(n - i - 1, n - j - 1);         long long j1 = j + min(n - i - 1, n - j - 1);         long long i2 = i + min(n - i - 1, j);         long long j2 = j - min(n - i - 1, j);         dp3[i][j] = dp1[i1][j1] + dp2[i2][j2] - a[i][j];       }     long long ans1 = LLONG_MIN, ans2 = LLONG_MIN, posx1, posy1, posx2, posy2;     for (long long i = (0); i <= n - 1; i++)       for (long long j = (0); j <= n - 1; j++) {         if ((i + j) % 2 == 0 && dp3[i][j] > ans1) {           ans1 = max(ans1, dp3[i][j]);           posx1 = i + 1;           posy1 = j + 1;         } else if ((i + j) % 2 == 1 && dp3[i][j] > ans2) {           ans2 = max(ans2, dp3[i][j]);           posx2 = i + 1;           posy2 = j + 1;         }       }     cout << ans1 + ans2 <<   n ;     cout << posx1 <<     << posy1 <<     << posx2 <<     << posy2 <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int a[2][2];     node() { a[0][0] = a[0][1] = a[1][0] = a[1][1] = 0; }     int *operator[](const size_t &s) { return a[s]; }   };   node segTree[400008], L[400008];   node id, M, zero;   int a[100002];   int lazy[400008];   inline node operator+(node &a, node &b) {     node res;     for (int i = 0; i < 2; i++)       for (int j = 0; j < 2; j++) res[i][j] = (a[i][j] + b[i][j]) % 1000000007;     return res;   }   inline node operator*(node &a, node &b) {     node res;     for (int i = 0; i < 2; i++)       for (int j = 0; j < 2; j++)         for (int k = 0; k < 2; k++)           res[i][j] =               (res[i][j] + (1ll * a[i][k] * b[k][j]) % 1000000007) % 1000000007;     return res;   }   inline node matPower(int n) {     node res = id;     node m = M;     while (n > 0) {       if (n & 1) res = res * m;       m = m * m;       n >>= 1;     }     return res;   }   inline void buildTree(int v, int s, int e) {     L[v] = id;     lazy[v] = 0;     if (s == e) {       segTree[v] = matPower(a[s]);       return;     }     int m = (s + e) / 2;     buildTree(2 * v, s, m);     buildTree(2 * v + 1, m + 1, e);     segTree[v] = segTree[2 * v] + segTree[2 * v + 1];   }   inline void propogate(int v, int l, int r) {     if (lazy[v] > 0) {       segTree[v] = segTree[v] * L[v];       if (l != r) {         lazy[2 * v] = lazy[2 * v + 1] = 1;         L[2 * v] = L[2 * v] * L[v];         L[2 * v + 1] = L[2 * v + 1] * L[v];       }       lazy[v] = 0;       L[v] = id;     }     return;   }   inline void update(int v, int tl, int tr, int l, int r, node x) {     propogate(v, tl, tr);     if (l > r) return;     if (tl == l && tr == r) {       lazy[v] = 1;       L[v] = x;       propogate(v, tl, tr);       return;     }     int tm = (tl + tr) / 2;     update(2 * v, tl, tm, l, min(tm, r), x);     update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, x);     segTree[v] = segTree[2 * v] + segTree[2 * v + 1];   }   inline node query(int v, int tl, int tr, int l, int r) {     propogate(v, tl, tr);     if (l > r) return zero;     if (tl == l && tr == r) return segTree[v];     int tm = (tl + tr) / 2;     node left = query(2 * v, tl, tm, l, min(tm, r));     node right = query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);     return (left + right);   }   int main() {     id[0][0] = id[1][1] = 1;     M[0][0] = M[0][1] = M[1][0] = 1;     ios::sync_with_stdio(false);     int n, m, q, l, r, x;     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a[i];     buildTree(1, 0, n - 1);     while (m--) {       cin >> q;       if (q == 1) {         cin >> l >> r >> x;         node val = matPower(x);         update(1, 0, n - 1, l - 1, r - 1, val);       } else {         cin >> l >> r;         cout << query(1, 0, n - 1, l - 1, r - 1)[0][1] << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     long long s = 1;     for (long long i = 1; i <= n; i++) {       s += (i - 1) * 4;     }     cout << s;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 100;   int a[N];   int main() {     ios_base::sync_with_stdio(false);     int n, U;     cin >> n >> U;     for (int i = 1; i <= n; ++i) cin >> a[i];     double ans = -1;     for (int i = 1, j = 1; i + 1 <= n; ++i) {       while (j + 1 <= n and a[j + 1] - a[i] <= U) ++j;       if (j - i + 1 < 3) continue;       ans = max(ans, (double)(a[j] - a[i + 1]) / (a[j] - a[i]));     }     cout << fixed << setprecision(9) << ans <<   n ;   }


#include <bits/stdc++.h>   int main() {     int q;     std::cin >> q;     while (q--) {       int n, ans(0);       std::cin >> n;       std::set<std::string> check, p;       std::string s;       std::vector<std::string> res, a(n);       for (int i = 0; i < n; i++) {         std::cin >> a[i];         check.insert(a[i]);       }       for (int i = 0; i < n; i++) {         if (p.find(a[i]) != p.end()) {           std::string t = a[i];           for (int x = 0; x < 10; x++) {             if (check.find(t) == check.end()) {               check.insert(t);               p.insert(t);               break;             }             t[0] = char( 0  + x);           }           ++ans;           res.push_back(t);         } else {           res.push_back(a[i]);           p.insert(a[i]);         }       }       std::cout << ans << std::endl;       for (int i = 0; i < res.size(); i++) {         std::cout << res[i] << std::endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef long long int ll;   typedef unsigned long long int ull;   inline void smax(int &x, int y) { x = max(x, y); }   inline void smin(int &x, int y) { x = min(x, y); }   ll gcd(ll a, ll b) { return ((b == 0) ? a : gcd(b, a % b)); }   ll n, m, a, b;   vector<vector<int> > gr;   vector<int> vis;   ll con = 0;   int dfs(int s, int prt) {     vis[s] = 1;     con++;     int f = 0;     if (prt == s) f = 1;     for (__typeof(gr[s].size()) i = (0) - ((0) > (gr[s].size()));          i != (gr[s].size()) - ((0) > (gr[s].size()));          i += 1 - 2 * ((0) > (gr[s].size()))) {       int x = gr[s][i];       if (!vis[x]) {         if (dfs(x, prt) == 1) f = 1;       }     }     return f;   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     int tcase;     cin >> tcase;     for (__typeof(tcase + 1) tc = (1) - ((1) > (tcase + 1));          tc != (tcase + 1) - ((1) > (tcase + 1));          tc += 1 - 2 * ((1) > (tcase + 1))) {       cin >> n >> m >> a >> b;       gr.resize(0);       vis.resize(0);       gr.resize(n + 1);       vis.resize(n + 1);       for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));            i += 1 - 2 * ((0) > (m))) {         int x, y;         cin >> x >> y;         gr[x].push_back(y);         gr[y].push_back(x);       }       ll con1 = 0;       vis[a] = 1;       for (__typeof(gr[a].size()) i = (0) - ((0) > (gr[a].size()));            i != (gr[a].size()) - ((0) > (gr[a].size()));            i += 1 - 2 * ((0) > (gr[a].size()))) {         if (vis[gr[a][i]]) continue;         con = 0;         int tch = dfs(gr[a][i], b);         if (tch == 0) {           con1 += con;         }       }       vis.resize(0);       vis.resize(n + 1);       vis[b] = 1;       ll con2 = 0;       for (__typeof(gr[b].size()) i = (0) - ((0) > (gr[b].size()));            i != (gr[b].size()) - ((0) > (gr[b].size()));            i += 1 - 2 * ((0) > (gr[b].size()))) {         con = 0;         if (vis[gr[b][i]]) continue;         int tch = dfs(gr[b][i], a);         if (tch == 0) {           con2 += con;         }       }       cout << con2 * con1 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> SEC;   int main() {     long long n, x, y;     cin >> n >> x >> y;     long long tedX = 0, tedY = 0;     while (tedX + tedY < x + y) {       if ((tedX + 1) * y > (tedY + 1) * x) {         SEC.push_back(2);         tedY++;       } else if ((tedX + 1) * y < (tedY + 1) * x) {         SEC.push_back(1);         tedX++;       } else {         SEC.push_back(-1);         SEC.push_back(-1);         tedX++;         tedY++;       }     }     for (int i = 0; i < n; i++) {       long long tmp;       cin >> tmp;       long long ans = SEC[(tmp - 1) % (x + y)];       if (ans == 1) {         cout <<  Vanya  << endl;       } else if (ans == 2) {         cout <<  Vova  << endl;       } else         cout <<  Both  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 400010;   struct Event {     int x, type, ind;     Event() {}     Event(int x, int type, int ind) : x(x), type(type), ind(ind) {}     bool operator<(const Event &other) const {       return make_pair(x, type) < make_pair(other.x, other.type);     }   };   int n, k;   int l[N], r[N];   Event a[N];   bool del[N];   set<pair<int, int> > q;   vector<int> res;   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &l[i], &r[i]);       a[2 * i - 1] = Event(l[i], 0, i);       a[2 * i] = Event(r[i], 1, i);     }     sort(a + 1, a + 2 * n + 1);     int cov = 0;     int ans = 0;     for (int i = 1; i <= n; i++) del[i] = false;     for (int i = 1; i <= 2 * n; i++) {       int j = a[i].ind;       if (a[i].type == 0) {         q.insert(make_pair(-r[j], j));         cov++;         if (cov > k) {           cov--;           ans++;           int ind = (*q.begin()).second;           del[ind] = true;           res.push_back(ind);           q.erase(q.begin());         }       } else if (!del[j]) {         cov--;         q.erase(make_pair(-r[j], j));       }     }     printf( %d n , ans);     for (int x : res) printf( %d  , x);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using str = string;   using pi = pair<int, int>;   using pl = pair<ll, ll>;   using vi = vector<int>;   using vl = vector<ll>;   using vpi = vector<pair<int, int>>;   using vvi = vector<vi>;   int Bit(int mask, int b) { return (mask >> b) & 1; }   template <class T>   bool ckmin(T &a, const T &b) {     if (b < a) {       a = b;       return true;     }     return false;   }   template <class T>   bool ckmax(T &a, const T &b) {     if (b > a) {       a = b;       return true;     }     return false;   }   const int INFi = 2e9;   const ll INF = 2e18;   const int maxN = 605;   const int LG = 20;   struct blossom {     int n, m;     vector<int> mate;     vector<vector<int>> b;     vector<int> p, d, bl;     vector<vector<int>> g;     blossom(int n) : n(n) {       m = n + n / 2;       mate.assign(n, -1);       b.resize(m);       p.resize(m);       d.resize(m);       bl.resize(m);       g.assign(m, vector<int>(m, -1));     }     void add_edge(int u, int v) {       g[u][v] = u;       g[v][u] = v;     }     void match(int u, int v) {       g[u][v] = g[v][u] = -1;       mate[u] = v;       mate[v] = u;     }     vector<int> trace(int x) {       vector<int> vx;       while (true) {         while (bl[x] != x) x = bl[x];         if (!vx.empty() && vx.back() == x) break;         vx.push_back(x);         x = p[x];       }       return vx;     }     void contract(int c, int x, int y, vector<int> &vx, vector<int> &vy) {       b[c].clear();       int r = vx.back();       while (!vx.empty() && !vy.empty() && vx.back() == vy.back()) {         r = vx.back();         vx.pop_back();         vy.pop_back();       }       b[c].push_back(r);       b[c].insert(b[c].end(), vx.rbegin(), vx.rend());       b[c].insert(b[c].end(), vy.begin(), vy.end());       for (int i = 0; i <= c; i++) {         g[c][i] = g[i][c] = -1;       }       for (int z : b[c]) {         bl[z] = c;         for (int i = 0; i < c; i++) {           if (g[z][i] != -1) {             g[c][i] = z;             g[i][c] = g[i][z];           }         }       }     }     vector<int> lift(vector<int> &vx) {       vector<int> A;       while (vx.size() >= 2) {         int z = vx.back();         vx.pop_back();         if (z < n) {           A.push_back(z);           continue;         }         int w = vx.back();         int i = (A.size() % 2 == 0                      ? find(b[z].begin(), b[z].end(), g[z][w]) - b[z].begin()                      : 0);         int j =             (A.size() % 2 == 1                  ? find(b[z].begin(), b[z].end(), g[z][A.back()]) - b[z].begin()                  : 0);         int k = b[z].size();         int dif = (A.size() % 2 == 0 ? i % 2 == 1 : j % 2 == 0) ? 1 : k - 1;         while (i != j) {           vx.push_back(b[z][i]);           i = (i + dif) % k;         }         vx.push_back(b[z][i]);       }       return A;     }     int solve() {       for (int ans = 0;; ans++) {         fill(d.begin(), d.end(), 0);         queue<int> Q;         for (int i = 0; i < m; i++) bl[i] = i;         for (int i = 0; i < n; i++) {           if (mate[i] == -1) {             Q.push(i);             p[i] = i;             d[i] = 1;           }         }         int c = n;         bool aug = false;         while (!Q.empty() && !aug) {           int x = Q.front();           Q.pop();           if (bl[x] != x) continue;           for (int y = 0; y < c; y++) {             if (bl[y] == y && g[x][y] != -1) {               if (d[y] == 0) {                 p[y] = x;                 d[y] = 2;                 p[mate[y]] = y;                 d[mate[y]] = 1;                 Q.push(mate[y]);               } else if (d[y] == 1) {                 vector<int> vx = trace(x);                 vector<int> vy = trace(y);                 if (vx.back() == vy.back()) {                   contract(c, x, y, vx, vy);                   Q.push(c);                   p[c] = p[b[c][0]];                   d[c] = 1;                   c++;                 } else {                   aug = true;                   vx.insert(vx.begin(), y);                   vy.insert(vy.begin(), x);                   vector<int> A = lift(vx);                   vector<int> B = lift(vy);                   A.insert(A.end(), B.rbegin(), B.rend());                   for (int i = 0; i < (int)A.size(); i += 2) {                     match(A[i], A[i + 1]);                     if (i + 2 < (int)A.size()) add_edge(A[i + 1], A[i + 2]);                   }                 }                 break;               }             }           }         }         if (!aug) return ans;       }     }   };   vector<int> g1[maxN];   bool used[maxN];   bool was[maxN];   int comp[maxN];   bool ok = false;   int ee = 0;   int vv = 0;   void dfs(int v, int p) {     ee += (int)g1[v].size();     vv++;     was[v] = true;     for (auto &u : g1[v]) {       if (u == p) continue;       if (was[u]) {         ok = true;         continue;       }       dfs(u, v);     }   }   void dfs2(int v) {     used[v] = true;     for (auto &u : g1[v]) {       if (used[u]) continue;       dfs2(u);     }   }   void dfs3(int v) {     for (auto &u : g1[v]) {       if (comp[u]) continue;       comp[u] = comp[v];       dfs3(u);     }   }   vector<pair<int, int>> gg[maxN];   bool U[maxN];   void my_assert(bool x) {     if (!x) exit(0);   }   void find_answ(vector<int> a) {     int n = a.size();     vector<array<int, 4>> kek1;     for (int i = 0, j = 0; i < n; i = j) {       if (a[i] != 0) {         j++;         continue;       }       while (j < n && a[j] == 0) j++;       int len = j - i;       if (len % 2 == 0) continue;       my_assert(i > 0 && j < n);       if (a[i - 1] == a[j]) {         a[i] = a[i - 1];         continue;       }       gg[a[i - 1]].emplace_back(a[j], kek1.size());       gg[a[j]].emplace_back(a[i - 1], kek1.size());       kek1.push_back({a[i - 1], a[j], i, j - 1});     }     for (int i = 1; i < n; ++i) {       if (a[i] == a[i - 1] && a[i] != 0) {         U[a[i]] = true;       }     }     queue<int> q;     vi h(kek1.size(), 0);     auto make = [&](int i, int c) {       h[i] = 1;       auto [x, y, l, r] = kek1[i];       if (x == c) {         a[l] = c;       } else {         my_assert(y == c);         a[r] = c;       }     };     function<void(int)> dfss = [&](int v) {       for (auto &[u, i] : gg[v]) {         if (U[u] || h[i]) continue;         U[u] = true;         make(i, u);         dfss(u);       }     };     for (int i = 1; i < maxN; ++i) {       if (U[i]) {         dfss(i);         continue;       }     }     function<void(int)> go = [&](int v) {       for (auto &[u, i] : gg[v]) {         if (U[u] || h[i]) continue;         U[u] = true;         make(i, u);         go(u);       }     };     vi deg(maxN, 0);     for (int i = 1; i < maxN; ++i) {       if (U[i]) continue;       deg[i] = gg[i].size();       if (gg[i].size() == 1) {         q.push(i);       }     }     while (!q.empty()) {       int v = q.front();       q.pop();       vector<pair<int, int>> tt;       for (auto &[u, i] : gg[v]) {         if (U[u]) continue;         tt.emplace_back(u, i);       }       my_assert(tt.size() <= 1);       if (tt.empty()) continue;       auto [u, i] = tt.back();       U[v] = true;       make(i, v);       deg[u]--;       if (deg[u] == 1) {         q.push(u);       }     }     for (int i = 1; i < maxN; ++i) {       if (deg[i] == 0 || U[i]) continue;       go(i);     }     int k = 1;     for (int i = 1; i < n; ++i) {       if (a[i] == 0 && a[i - 1] == 0) {         while (k < maxN && U[k]) k++;         a[i] = a[i - 1] = k++;         if (k < maxN) U[k] = true;       } else if (a[i - 1] == 0) {         a[i - 1] = 1;       }     }     for (int i = 0; i < (n); ++i) cout << a[i] <<    ;     cout <<   n ;   }   void solve() {     int n;     cin >> n;     vi a(n);     for (int i = 0; i < (n); ++i) cin >> a[i];     int L, R;     int g = 0;     {       int k = 0;       while (k < n && a[k] == 0) k++;       if (k == n) {         for (int i = 1; i < n; i += 2) {           a[i] = a[i - 1] = (i / 2) + 1;         }         a[n - 1] = a[n - 2];         for (int i = 0; i < (n); ++i) cout << a[i] <<    ;         return;       }       L = k;       g += k / 2;       if (k % 2) {         used[a[k]] = true;         a[k - 1] = a[k];       }     }     {       int k = n - 1;       while (k >= 0 && a[k] == 0) k--;       R = k;       g += (n - 1 - k) / 2;       if ((n - 1 - k) % 2) {         used[a[k]] = true;         a[k + 1] = a[k];       }     }     for (int i = L + 1; i <= R; ++i) {       if (a[i] == 0) continue;       if (a[i] != a[i - 1]) continue;       used[a[i]] = true;     }     vector<array<int, 4>> kek;     for (int i = L, j = L; i <= R; i = j) {       j = i + 1;       if (a[i] != 0) continue;       while (j <= R && a[j] == 0) j++;       int len = j - i;       int t1 = a[i - 1];       int t2 = a[j];       if (len % 2) {         g += len / 2;         g1[t1].push_back(t2);         g1[t2].push_back(t1);         continue;       }       g += (len) / 2 - 1;       kek.push_back({t1, t2, i, j - 1});     }     for (int i = 1; i < maxN; ++i) {       if (was[i]) continue;       ee = vv = 0;       ok = false;       dfs(i, -1);       if ((vv - 1) * 2 != ee) {         ok = true;       }       if (ok) {         used[i] = true;       }     }     for (int i = 1; i < maxN; ++i) {       if (used[i]) {         dfs2(i);       }     }     int C = 0;     int t = 0;     for (int i = 1; i < maxN; ++i) {       if (used[i]) continue;       t++;       if (comp[i]) continue;       C++;       t--;       comp[i] = C;       dfs3(i);     }     blossom B(C + 1);     set<pair<int, int>> wasss;     for (auto &[a, b, i, j] : kek) {       if (used[a] || used[b] || comp[a] == comp[b]) {         g++;         continue;       }       pair<int, int> tmp = {comp[a], comp[b]};       if (tmp.first > tmp.second) swap(tmp.first, tmp.second);       if (wasss.count(tmp)) continue;       wasss.insert(tmp);       B.add_edge(comp[a], comp[b]);     }     B.solve();     set<pair<int, int>> have;     for (int i = 1; i <= C; ++i) {       if (i < B.mate[i]) {         have.insert({i, B.mate[i]});       }     }     auto ans = a;     for (auto &[a, b, i, j] : kek) {       if (used[a] || used[b] || comp[a] == comp[b]) {         continue;       }       pair<int, int> cur = {comp[a], comp[b]};       if (cur.first > cur.second) swap(cur.first, cur.second);       if (!have.count(cur)) {         g++;         continue;       }       have.erase(cur);       ans[i] = ans[i - 1];       ans[j] = ans[j + 1];     }     find_answ(ans);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t = 1;     for (int i = 0; i < (t); ++i) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using namespace std;   const int MAXN = -1;   const int MOD = 1;   const int INF = (int)(1e9);   int sign(long long x) {     if (x > 0) return 1;     if (x < 0) return -1;     return 0;   }   vector<int> getB(vector<int> a, int n, int &s) {     vector<int> b(a.size() + 40);     for (int i = 0; i < (int)a.size(); i++) {       int x = a[i];       int j = i;       while (x) {         b[j] += x % 2;         x /= 2;         j++;       }     }     for (int j = 0; j < (int)b.size(); j++) {       if (b[j]) {         b[j + 1] += b[j] / 2;         b[j] %= 2;       }     }     int mx = -1;     for (int j = 0; j < (int)b.size(); j++)       if (b[j] != 0) mx = j;     if (mx == -1) {       s = 0;       return vector<int>(a.size(), 0);     }     s = 1;     if (b[mx] == -1) {       s = -1;       for (int i = 0; i < (int)b.size(); i++) b[i] *= -1;     }     for (int i = 0; i < (int)b.size(); i++) {       b[i + 1] += b[i] / 2;       b[i] %= 2;       if (b[i] == -1) {         b[i] = 1;         b[i + 1] += -1;       }     }     return b;   }   int fastsolve(vector<int> a, int n, int k) {     for (int i = 0; i < 40; i++) a.push_back(0);     long long cur = 0;     int mn = INF, mx = -INF;     int s;     vector<int> b = getB(a, n, s);     for (int i = 0; i < (int)b.size(); i++) {       if (b[i] == 1) {         mn = min(mn, i);         mx = max(mx, i);       }     }     if (mx - mn > 35) {       return 0;     }     cur = 0;     for (int i = mx; i >= mn; i--) {       cur *= 2;       if (i != mn) cur += b[i];     }     cur *= s;     int ans = 0;     for (int i = mn; i >= 0; i--) {       cur += b[i] * s;       if (abs(cur) > 2LL * k) {         break;       }       ans += i <= n && abs(cur - a[i]) <= k && (i != n || abs(cur - a[i]) != 0);       cur *= 2;     }     return ans;   }   int slowsolve(vector<int> a, int n, int k) {     for (int i = 0; i < 40; i++) a.push_back(0);     int ans = 0;     for (int i = 0; i <= n; i++) {       vector<int> na = a;       for (int j = -k; j <= k; j++) {         na[i] = j;         if (i == n && j == 0) continue;         int s;         getB(na, n, s);         if (s == 0) {           ans++;           getB(na, n, s);         }       }     }     return ans;   }   int main() {     int n, k;     while (scanf( %d%d , &n, &k) == 2) {       vector<int> a(n + 1);       for (int i = 0; i <= n; i++) scanf( %d , &a[i]);       printf( %d n , fastsolve(a, n, k));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long T = 0;     int f = 0;     char c = getchar();     for (; c <  0  || c >  9 ; c = getchar()) f |= c ==  - ;     for (; c >=  0  && c <=  9 ; c = getchar()) T = T * 10 + c -  0 ;     return f ? -T : T;   }   const double eps = 1e-7;   const int maxn = 1e5 + 233;   const double pi = acos(-1.0);   struct Vector {     double x, y, r;   } A[maxn];   int n, a;   double ans;   struct WZP_AK_SZB {     double l, r;     bool operator<(const WZP_AK_SZB &n) const {       if (l == n.l) return r > n.r;       return l < n.l;     }   } B[maxn];   int numB;   set<double> S;   set<double>::iterator it;   bool check(double x) {     numB = 0;     for (register int i = (1); i <= (n); ++i) {       double dis = sqrt(((A[i].x - a) * (A[i].x - a)) + ((A[i].y) * (A[i].y)));       if (dis > A[i].r + x || A[i].r > dis + x || x > dis + A[i].r) continue;       double u = atan2(A[i].y, A[i].x - a);       double t = acos((((dis) * (dis)) + ((x) * (x)) - ((A[i].r) * (A[i].r))) /                       2.0 / dis / x);       double l = u - t, r = u + t;       if (l < -pi) l += 2 * pi;       if (l > pi) l -= 2 * pi;       if (r < -pi) r += 2 * pi;       if (r > pi) r -= 2 * pi;       if (l > r) swap(l, r);       B[++numB].l = l;       B[numB].r = r;     }     sort(B + 1, B + 1 + numB);     S.clear();     for (register int i = (1); i <= (numB); ++i) {       it = S.upper_bound(B[i].l);       if (it != S.end() && *it < B[i].r) return true;       S.insert(B[i].r);     }     return false;   }   int main() {     n = read(), a = read();     for (register int i = (1); i <= (n); ++i) {       A[i].x = read(), A[i].y = read();       A[i].r = sqrt(((A[i].x + a) * (A[i].x + a)) + ((A[i].y) * (A[i].y)));     }     double l = 0.0;     double r = 2.0 * a;     while (r - l > 1e-7) {       double mid = (l + r) / 2;       if (check(mid)) {         r = mid;       } else {         l = mid;       }     }     printf( %.10lf n , l);   }


#include <bits/stdc++.h>   using namespace std;   struct child {     long long v, p, d;   };   child c[4123];   bool gone[4123];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < (int)(n); i++) {       scanf( %lld%lld%lld , &c[i].v, &c[i].d, &c[i].p);     }     int ans = 0;     vector<int> all;     for (int i = 0; i < (int)(n); i++) {       if (gone[i]) continue;       ans++;       all.push_back(i + 1);       for (int j = i + 1; j < n && c[i].v; j++) {         if (gone[j]) continue;         c[j].p -= c[i].v;         c[i].v--;       }       long long dec = 0;       for (int j = i + 1; j < n; j++) {         if (gone[j]) continue;         c[j].p -= dec;         if (c[j].p < 0) {           gone[j] = 1;           dec += c[j].d;         }       }     }     printf( %d n , ans);     for (int i = 0; i < (int)(all.size()); i++) {       printf( %d%c , all[i], (i == all.size() - 1 ?   n  :    ));     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int n, mnPre[(100005)], mxPre[(100005)], mnSuf[(100005)], mxSuf[(100005)];   pair<int, int> a[2][(100005)];   long long solve(int ax) {     sort(a[ax] + 1, a[ax] + 1 + n);     for (int i = 1; i <= n; ++i) {       mnPre[i] = min(mnPre[i - 1], a[ax][i].second);       mxPre[i] = max(mxPre[i - 1], a[ax][i].second);     }     for (int i = n; i >= 1; --i) {       mnSuf[i] = min(mnSuf[i + 1], a[ax][i].second);       mxSuf[i] = max(mxSuf[i + 1], a[ax][i].second);     }     long long r = (1LL << 60) - 1;     long long m = 0;     int mn, mx;     for (int i = 59; i >= 0; --i) {       bool allowed = 0;       m = r - (1LL << i);       allowed = (1LL * (a[ax][n].first - a[ax][1].first) *                  (a[ax][n].first - a[ax][1].first)) <= m;       for (int en = 1, st = 1; en <= n && !allowed; ++en) {         while ((1LL * (a[ax][en].first - a[ax][st].first) *                 (a[ax][en].first - a[ax][st].first)) > m)           ++st;         mn = min(mnPre[st - 1], mnSuf[en + 1]);         mx = max(mxPre[st - 1], mxSuf[en + 1]);         allowed = (1LL * (mx - mn) * (mx - mn)) <= m &&                   max((1LL * (mn) * (mn)), (1LL * (mx) * (mx))) +                           max((1LL * (a[ax][en].first) * (a[ax][en].first)),                               (1LL * (a[ax][st].first) * (a[ax][st].first))) <=                       m;       }       if (allowed) r -= (1LL << i);     }     return r;   }   int main() {     scanf( %d , &n);     for (int i = 1, x, y; i <= n; ++i) {       scanf( %d%d , &x, &y);       a[0][i] = {x, y}, a[1][i] = {y, x};     }     printf( %I64d , min(solve(0), solve(1)));   }


#include <bits/stdc++.h>   using namespace std;   long long int M = 1000000007;   long long int min(long long int a, long long int b, long long int c,                     long long int d) {     long long int arr[4] = {a, b, c, d};     sort(arr, arr + 4);     return arr[0];   }   long long int max(long long int a, long long int b, long long int c,                     long long int d) {     long long int arr[4] = {a, b, c, d};     sort(arr, arr + 4);     return arr[3];   }   long long int gcd(long long int a, long long int b) {     if (b < a) {       int temp = a;       a = b;       b = temp;     }     if (a == 0) {       return b;     }     return gcd(b % a, a);   }   long long int lcm(long long int a, long long int b) {     return (a * b / gcd(a, b));   }   vector<long long int> getprimes() {     vector<long long int> pl(101, 1);     for (int i = 2; i <= 100; i++) {       if (pl[i] == 1) {         for (int j = 2 * i; j <= 100; j += i) {           pl[j] = 0;         }       }     }     return pl;   }   double distsqrd(double x1, double y1, double x2, double y2) {     return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);   }   long long int fact(long long int first) {     long long int ans = 1;     if (first == 0) {       return 1;     }     while (first > 0) {       ans = (ans * first) % 1000000007;       first--;     }     return ans % 1000000007;   }   long long int power(long long int a, long long int b) {     long long int ans = 1;     while (b > 0) {       if (b % 2 == 1) {         ans *= a;         ans = ans;       }       b /= 2;       a *= a;       a = a;     }     return ans;   }   vector<int> getbinary(int first) {     vector<int> bin(10, 0);     int iter = 0;     while (first > 0) {       if (first % 2 == 1) {         bin[iter] = 1;       } else {         bin[iter] = 0;       }       iter++;       first /= 2;     }     return bin;   }   int getnum(vector<int> bin) {     int t = 1;     int n = bin.size();     int ret = 0;     for (int i = 0; i < n; i++) {       ret += t * bin[i];       t *= 2;     }     return ret;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     map<pair<int, int>, int> m1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         int temp;         cin >> temp;         m1[make_pair(i, temp)] = j;       }     }     int b[n + 1];     map<int, int> m2;     for (int i = 1; i <= n; i++) {       cin >> b[i];       m2[b[i]] = i;     }     int ans[n + 1][n + 1];     set<int> s;     for (int i = 1; i <= n; i++) {       s.insert(i);       int no1 = -1, no2 = -1, min1 = n + 1, min2 = n + 1;       for (auto e : s) {         if (m2[e] < min1) {           min2 = min1;           min1 = m2[e];           no2 = no1;           no1 = e;         } else if (m2[e] < min2) {           min2 = m2[e];           no2 = e;         }       }       if (no2 == -1) {         no2 = no1;       }       for (int j = 1; j <= n; j++) {         if (j == no1 && j == no2) {           ans[i][j] = -1;         } else if (j != no1) {           ans[i][j] = no1;         } else if (j == no1 && j != no2) {           ans[i][j] = no2;         }       }     }     for (int j = 1; j <= n; j++) {       int min = 1e9, out;       for (int i = 1; i <= n; i++) {         if (ans[i][j] != -1) {           if (m1[make_pair(j, ans[i][j])] < min) {             min = m1[make_pair(j, ans[i][j])];             out = i;           }         }       }       cout << out <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   int solver(string s) {     if (s.length() == 1) {       if (s ==  0 ) return 1;       if (s ==  _ )         return 1;       else         return 0;     }     if (s[0] ==  0 ) {       return 0;     }     if (s.back() ==  _ ) {       int ans = 0;       for (char c =  0 ; c <=  9 ; c++) {         s[s.length() - 1] = c;         ans += solver(s);       }       return ans;     }     int _ = 0;     for (char c : s) {       _ += (c ==  _ );     }     if (s.back() >=  0  && s.back() <=  9  && s[s.length() - 2] >=  0  &&         s[s.length() - 2] <=  9 ) {       if (s.back() !=  0  && s.back() !=  5 ) {         return 0;       }       if (s.back() ==  0 ) {         if (s[s.length() - 2] !=  5  && s[s.length() - 2] !=  0 ) {           return 0;         }       }       if (s.back() ==  5 ) {         if (s[s.length() - 2] !=  2  && s[s.length() - 2] !=  7 ) {           return 0;         }       }       return pow(10, _);     }     if (s.back() >=  0  && s.back() <=  9  && s[s.length() - 2] ==  _ ) {       if (s.back() ==  0  || s.back() ==  5 )         return 2 * pow(10, _ - 1);       else         return 0;     }   }   vector<string> generate(string orig) {     vector<string> v;     string dum = orig;     int x = 0;     for (char c : orig) x += (c ==  X );     if (!x) {       return {orig};     }     for (char i =  0 ; i <=  9 ; i++) {       for (int j = 0; j < dum.length(); j++) {         if (dum[j] ==  X ) {           dum[j] = i;         }       }       v.push_back(dum);       dum = orig;     }     return v;   }   int main() {     string orig;     cin >> orig;     if (orig.length() == 1) {       if (orig ==  X )         cout << 1;       else         cout << solver(orig);       return 0;     }     vector<string> v = generate(orig);     int ans = 0;     for (string s : v) {       if (s[0] ==  0 ) {         ans += 0;       } else if (s[0] ==  _ ) {         for (char j =  1 ; j <=  9 ; j++) {           s[0] = j;           ans += solver(s);         }       } else {         ans += solver(s);       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500010;   int ans[N], arr[N], MAX[N];   long long power_mod(long long x, long long y) {     x %= 1000000007;     long long res = 1;     while (y) {       if (y & 1) {         res = (res * x) % 1000000007;       }       y /= 2;       x = (x * x) % 1000000007;     }     return res;   }   void solve() {     int n;     cin >> n;     vector<pair<int, int>> v(N);     for (int i = 1; i <= n; i++) {       cin >> v[i].first >> v[i].second;       ans[i] = MAX[i] = 1;     }     sort(v.begin() + 1, v.begin() + n + 1);     arr[n + 1] = v[n].second + 1;     for (int i = 1; i <= n; i++) {       arr[i] = v[i].first;     }     int ma = 0;     for (int i = n; i >= 1; i--) {       int up = upper_bound(arr + 1, arr + 2 + n, v[i].second) - arr;       int p = ans[i];       ans[i] = max(ans[i], ans[i + 1]);       ans[i] = max(ans[i], p + ans[up]);     }     int res = *max_element(ans, ans + n + 1);     cout << res <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int tc = 1;     while (tc--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, p[11111], c[11111];   vector<pair<int, int> > ve;   int main() {     int a;     cin >> n;     for (int i = 2; i <= n; i++) {       cin >> a;       c[i] = a;     }     int s = 1;     cin >> p[1];     for (int i = 2; i <= n; i++) {       cin >> p[i];       if (p[i] != p[c[i]]) {         s++;       }     }     cout << s;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     set<long long> s;     for (long long a = 1; a <= 2 * 1e9; a *= 2) {       for (long long b = 1; b <= 2 * 1e9; b *= 3) {         s.insert(a * b);       }     }     int l, r, ans = 0;     cin >> l >> r;     for (set<long long>::iterator it = s.begin(); it != s.end(); ++it)       ans += *it <= r && *it >= l;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const bool debug = 0;   int main() {     if (!debug) {       cin.tie(0);       cin.sync_with_stdio(0);       cout.tie(0);       cout.sync_with_stdio(0);     }     int n, m;     cin >> n >> m;     vector<vector<int>> d(m, vector<int>(n));     for (int i = 0; i < m; i++)       for (int j = 0; j < n; j++) cin >> d[i][j];     vector<int> out(m);     for (int i = 0; i < m; i++) out[i] = i;     for (int v = 0; v < n - 1; v++) {       vector<pair<int, int>> k(m);       int r = 0;       for (int i = 0; i < m; i++) {         k[i] = {d[i][n - 1] - d[i][v], i};         r += k[i].first;       }       sort(k.begin(), k.end());       reverse(k.begin(), k.end());       if (debug)         for (int i = 0; i < m; i++)           cout << k[i].first <<  :  << k[i].second <<    ;       if (debug) cout <<   n  << r <<   n ;       int i = 0;       for (; i < m; i++) {         if (r > 0)           r -= k[i].first;         else           break;       }       if (r <= 0 && out.size() > i) {         out.resize(i);         for (int j = 0; j < i; j++) out[j] = k[j].second;       }     }     cout << out.size() <<   n ;     for (int i = 0; i < out.size(); i++) cout << out[i] + 1 <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace IO {   inline char gc() {     static char buf[1000000], *p1 = buf, *p2 = buf;     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)                ? EOF                : *p1++;   }   template <typename T>   inline void read(T &x) {     x = (T)0;     T f = 1;     char ssd = gc();     while (ssd <  0  || ssd >  9 ) f = ssd ==  -  ? -1 : 1, ssd = gc();     while (ssd >=  0  && ssd <=  9 )       x = (x << 3) + (x << 1) + (ssd -  0 ), ssd = gc();     x = x * f;   }   char OUT[1000000], *S = OUT, *T = OUT + 1000000;   char Num[1000000];   int toP;   inline void out() {     fwrite(OUT, sizeof(char), S - OUT, stdout);     S = OUT;   }   inline void pc(char ssd) {     *S++ = ssd;     if (S == T) out();   }   template <typename T>   inline void write(T x) {     if (x == 0) pc( 0 );     if (x < 0) x = -x, pc( - );     toP = 0;     while (x) Num[++toP] = x % 10 +  0 , x /= 10;     while (toP) pc(Num[toP--]);   }   };  // namespace IO   const int maxn = 4e5 + 10;   namespace SGT {   int sum[maxn << 5];   int ls[maxn << 5], rs[maxn << 5], Node;   inline void pushup(int NODE) { sum[NODE] = sum[ls[NODE]] + sum[rs[NODE]]; }   void change(int &NODE, int l, int r, int id) {     if (!NODE) NODE = ++Node;     if (l == r) {       sum[NODE] += 1;       return;     }     int mid = (l + r) >> 1;     if (mid >= id)       change(ls[NODE], l, mid, id);     else       change(rs[NODE], mid + 1, r, id);     pushup(NODE);   }   int query(int NODE, int l, int r, int L, int R) {     if (L > R) return 0;     if (!NODE || (L <= l && r <= R)) return sum[NODE];     int re = 0;     int mid = (l + r) >> 1;     if (mid >= L) re += query(ls[NODE], l, mid, L, R);     if (mid < R) re += query(rs[NODE], mid + 1, r, L, R);     return re;   }   int merge(int root1, int root2, int l, int r) {     if (!root1 || !root2) return root1 + root2;     int New = ++Node;     if (l == r) {       sum[New] = sum[root1] + sum[root2];       return New;     } else {       int mid = (l + r) >> 1;       ls[New] = merge(ls[root1], ls[root2], l, mid);       rs[New] = merge(rs[root1], rs[root2], mid + 1, r);       pushup(New);       return New;     }   }   }  // namespace SGT   namespace Tham {   int root[maxn];   int len[maxn], fa[maxn], son[maxn][26], end = 1, tnt = 1;   inline int insert(int C, int last) {     end = ++tnt;     len[end] = len[last] + 1;     while (last && son[last][C] == 0) son[last][C] = end, last = fa[last];     if (!last) {       fa[end] = 1;       return end;     }     register int c = son[last][C];     if (len[last] + 1 == len[c]) {       fa[end] = c;       return end;     }     len[++tnt] = len[last] + 1;     for (register int i = 0; i < 26; i += 1) son[tnt][i] = son[c][i];     fa[tnt] = fa[c], fa[c] = tnt;     fa[end] = tnt;     while (son[last][C] == c) son[last][C] = tnt, last = fa[last];     return end;   }   }  // namespace Tham   int Len;   char ssd[maxn], S[maxn];   int head[maxn], to[maxn], nxt[maxn], tot;   inline void add(int x, int y) {     nxt[++tot] = head[x];     head[x] = tot;     to[tot] = y;   }   void dfs(int now) {     for (register int i = head[now]; i; i = nxt[i]) {       dfs(to[i]);       Tham::root[now] = SGT::merge(Tham::root[now], Tham::root[to[i]], 1, Len);     }   }   int pos[maxn], id[maxn];   int main() {     scanf( %s , S + 1);     Len = strlen(S + 1);     id[0] = pos[0] = 1;     for (register int i = 1; i <= Len; i += 1)       pos[i] = Tham::insert(S[i] -  a , pos[i - 1]),       SGT::change(Tham::root[pos[i]], 1, Len, i);     for (register int i = 2; i <= Tham::tnt; i += 1) add(Tham::fa[i], i);     dfs(1);     int T;     scanf( %d , &T);     while (T--) {       int l, r;       scanf( %d %d %s , &l, &r, ssd + 1);       int LL = strlen(ssd + 1), match = 0;       for (register int i = 1; i <= LL; i += 1) {         if (Tham::son[id[i - 1]][ssd[i] -  a ])           id[i] = Tham::son[id[i - 1]][ssd[i] -  a ], match += 1;         else           break;       }       bool ccf = false;       for (register int i = match; i >= 0; i -= 1) {         for (register int color = (i < LL ? ssd[i + 1] -  a  + 1 : 0); color < 26;              color += 1)           if (Tham::son[id[i]][color]) {             int nxt = Tham::son[id[i]][color];             if (SGT::query(Tham::root[nxt], 1, Len, l + i, r)) {               for (register int j = 1; j <= i; j += 1) printf( %c , ssd[j]);               printf( %c n , char(color + 97));               ccf = true;               break;             }           }         if (ccf) break;       }       if (!ccf) puts( -1 );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long int t;     cin >> t;     while (t--) {       long long n;       cin >> n;       unordered_map<int, long long> mmap;       while (n % 3 == 0) {         mmap[3]++;         n /= 3;       }       while (n % 2 == 0) {         mmap[2]++;         n /= 2;       }       if ((n > 1) || (mmap[2] > mmap[3]))         cout << -1 << endl;       else {         long long count = mmap[3];         count += (mmap[3] - mmap[2]);         cout << count << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 58;   vector<int> adj[maxn], V;   bitset<maxn> mark;   void dfs(int v) {     mark[v] = 1;     V.push_back(v);     for (auto u : adj[v]) {       if (!mark[u]) {         dfs(u);         V.push_back(v);       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, m, k;     cin >> n >> m >> k;     for (int i = 0; i < m; i++) {       int v, u;       cin >> v >> u;       v--;       u--;       adj[v].push_back(u);       adj[u].push_back(v);     }     dfs(0);     int ptr = 0;     while (k) {       cout << (V.size() - ptr) / k <<    ;       for (int i = 0; i < (V.size() - ptr) / k; i++) {         cout << V[ptr + i] + 1 <<    ;       }       ptr += (V.size() - ptr) / k;       cout <<   n ;       k--;     }   }


#include <bits/stdc++.h>   const int N = 123456;   int arr[N], i, j;   using namespace std;   int main() {     long long n, k, a, b, ans1 = 0, ans2 = 0, ans = 0;     scanf( %lld %lld , &n, &k);     scanf( %lld %lld , &a, &b);     ans1 = (n - 1) * a;     if (k == 1) return cout << ans1 << endl, 0;     while (n != 0 && n != 1) {       if (n % k == 0) {         if (b < a * (n - n / k))           ans += b;         else           ans += a * (n - n / k);         n = n / k;       } else {         ans += ((n % k) * a);         n = n - (n % k);       }     }     if (n == 0) ans -= a;     printf( %lld n , ans);     return 0;   }