#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200005;   struct Edge {     int to;     int nxt;   } edge[MAXN << 1];   int n, k, id;   int first[MAXN];   int a[MAXN];   int fa[MAXN];   int f[MAXN];   int g[MAXN];   int sz[MAXN];   void addE(int u, int v) {     edge[++id] = (Edge){v, first[u]};     first[u] = id;   }   void dfsinfo(int u) {     sz[u] = 1;     for (int i = first[u]; i; i = edge[i].nxt)       if (edge[i].to != fa[u]) {         fa[edge[i].to] = u;         dfsinfo(edge[i].to);         sz[u] += sz[edge[i].to];       }   }   void dfsf(int u, int lim) {     int MX = 0, sum = 1;     for (int i = first[u]; i; i = edge[i].nxt)       if (edge[i].to != fa[u] && a[edge[i].to] >= lim) {         dfsf(edge[i].to, lim);         if (sz[edge[i].to] == f[edge[i].to])           sum += f[edge[i].to];         else           MX = max(MX, f[edge[i].to]);       }     f[u] = MX + sum;   }   void dfsg(int u, int lim) {     int MX1 = 0, MX2 = 0, sum = 1;     for (int i = first[u]; i; i = edge[i].nxt)       if (edge[i].to != fa[u] && a[edge[i].to] >= lim) {         if (sz[edge[i].to] == f[edge[i].to])           sum += f[edge[i].to];         else if (f[edge[i].to] >= MX1) {           MX2 = MX1;           MX1 = f[edge[i].to];         } else if (f[edge[i].to] > MX2)           MX2 = f[edge[i].to];       }     if (n - sz[u] == g[u])       sum += g[u];     else if (g[u] >= MX1) {       MX2 = MX1;       MX1 = g[u];     } else if (g[u] > MX2)       MX2 = g[u];     for (int i = first[u]; i; i = edge[i].nxt)       if (edge[i].to != fa[u] && a[edge[i].to] >= lim) {         if (sz[edge[i].to] == f[edge[i].to])           g[edge[i].to] = sum - f[edge[i].to] + MX1;         else if (f[edge[i].to] == MX1)           g[edge[i].to] = sum + MX2;         else           g[edge[i].to] = sum + MX1;         dfsg(edge[i].to, lim);       }   }   int getans(int lim) {     memset(f, 0, sizeof(f));     memset(g, 0, sizeof(g));     if (a[1] >= lim) {       dfsf(1, lim);       dfsg(1, lim);     }     for (int i = 1; i <= n; i++)       if (a[i] < lim)         for (int j = first[i]; j; j = edge[j].nxt)           if (edge[j].to != fa[i] && a[edge[j].to] >= lim) {             dfsf(edge[j].to, lim);             dfsg(edge[j].to, lim);           }     int res = 0;     for (int i = 1; i <= n; i++) {       if (a[i] < lim) continue;       int MX = 0, sum = 1;       for (int j = first[i]; j; j = edge[j].nxt)         if (a[edge[j].to] >= lim) {           int SZ = 0, FG = 0;           if (edge[j].to == fa[i]) {             SZ = n - sz[i];             FG = g[i];           } else {             SZ = sz[edge[j].to];             FG = f[edge[j].to];           }           if (FG == SZ)             sum += FG;           else             MX = max(MX, FG);         }       res = max(res, MX + sum);     }     return res;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int u, v, i = 1; i < n; i++) {       scanf( %d%d , &u, &v);       addE(u, v);       addE(v, u);     }     dfsinfo(1);     int low = 1, high = 1000000;     while (low < high) {       int mid = low + high + 1 >> 1;       if (getans(mid) >= k)         low = mid;       else         high = mid - 1;     }     printf( %d n , low);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 510;   const int inf = 0x3f3f3f3f;   int n;   long long d[maxn][maxn];   int w(int i, int j, int k) { return i * j * k; }   int main() {     int n;     scanf( %d , &n);     memset(d, 0x3f, sizeof(d));     for (int i = 1; i <= n; i++) {       d[i][i + 2] = w(i, i + 1, i + 2);       d[i][i] = 0;       d[i][i + 1] = 0;     }     for (int i = n - 2; i >= 1; i--) {       for (int j = i + 2; j <= n; j++) {         for (int k = i + 1; k < j; k++) {           d[i][j] = min(d[i][j], d[i][k] + d[k][j] + w(i, j, k));         }       }     }     printf( %I64d , d[1][n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k;   int l, MOD;   map<long long, int> mp;   int fib(long long n) {     if (mp.count(n)) return mp[n];     if (n & 1)       return mp[n] = 1LL * fib(n / 2) * (fib(n / 2 - 1) + fib(n / 2 + 1)) % MOD;     return mp[n] = (1LL * fib(n / 2) * fib(n / 2) +                     1LL * fib(n / 2 - 1) * fib(n / 2 - 1)) %                    MOD;   }   int bpow(int a, long long b) {     int ans = 1;     while (b) {       if (b & 1) ans = 1LL * ans * a % MOD;       b >>= 1;       a = 1LL * a * a % MOD;     }     return ans;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> k >> l >> MOD;     if (k >= (1LL << min(l, 62))) {       cout <<  0 n ;       return 0;     }     mp[0] = 1 % MOD;     mp[1] = 1 % MOD;     int zero = fib(n + 1);     int one = (bpow(2, n) - zero + MOD) % MOD;     int ans = 1 % MOD;     for (int i = 0; i < min(l, 62); i++)       if ((1LL << i) & k)         ans = 1LL * ans * one % MOD;       else         ans = 1LL * ans * zero % MOD;     for (int i = 62; i < l; i++) ans = 1LL * ans * zero % MOD;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void sol() {     long long int n;     cin >> n;     vector<int> ar[6];     long long int a;     vector<int> v = {4, 8, 15, 16, 23, 42};     for (long long int i = 0; i < n; i++) {       cin >> a;       ar[lower_bound(v.begin(), v.end(), a) - v.begin()].push_back(i);     }     int p[6] = {0, 0, 0, 0, 0, 0};     int sit = 0;     int cnt = 0;     while (1) {       int prev = -1;       for (long long int i = 0; i < 6; i++) {         while (p[i] < ar[i].size() && ar[i][p[i]] < prev) {           p[i]++;         }         if (p[i] >= ar[i].size()) {           sit = 1;           break;         }         prev = ar[i][p[i]];         p[i]++;       }       if (sit == 1)         break;       else         cnt++;     }     cout << n - 6 * cnt <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int TESTS = 1;     while (TESTS--) {       sol();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   const int bound = 400;   struct qq {     int id, vl;     bool operator<(const qq &temp) const { return vl < temp.vl; }   };   qq bk[210000 / bound][bound], ns;   int nk[210000 / bound];   struct pp {     int vl, id, min_value, value;     int flag;     bool operator<(const pp &temp) const { return vl < temp.vl; }   };   pp bl[210000 / bound][2 * bound], now;   int num[210000 / bound], num_bl, cnt_list, np[2 * bound];   int m, n, a[220000], num_q, ans, nw;   int lower_bound(int left, int right, qq bl[], int x) {     int mid;     while (left <= right) {       mid = (left + right) >> 1;       if (bl[mid].vl < x)         left = mid + 1;       else         right = mid - 1;     }     return right;   }   int lower_bound(int left, int right, pp bl[], int x) {     int mid;     while (left <= right) {       mid = (left + right) >> 1;       if (bl[mid].vl < x)         left = mid + 1;       else         right = mid - 1;     }     return right;   }   int main() {     int i, j, s, p, q, ip, id, l, r, x, fir;     scanf( %d%d , &n, &m);     for (i = 0; i < n; i++) scanf( %d , &a[i]);     memset(num, 0, sizeof(num));     memset(nk, 0, sizeof(nk));     num_bl = 0;     for (i = 0; i < n; i++) {       if (i % bound == 0) {         for (j = i; j < min(n, i + bound); j++) {           if (j + m < n) {             now.id = j;             now.vl = a[j];             now.flag = -1;             bl[num_bl][num[num_bl]++] = now;             now.id = j + m;             now.vl = a[j + m];             now.flag = 1;             bl[num_bl][num[num_bl]++] = now;           }           ns.id = j;           ns.vl = a[j];           bk[num_bl][nk[num_bl]++] = ns;         }         num_bl++;       }     }     for (i = 0; i < num_bl; i++) {       sort(bl[i], bl[i] + num[i]);       sort(bk[i], bk[i] + nk[i]);       for (j = i * bound; j < min((i + 1) * bound, n); j++) np[j - i * bound] = 0;       for (j = 0; j < num[i]; j++) {         bl[i][j].value = bl[i][j].flag;         ip = bl[i][j].id;         if (bl[i][j].flag > 0) ip -= m;         np[ip - i * bound] += bl[i][j].flag;         if (bl[i][j].flag < 0) {           if (bl[i][j].id + m >= n) {             bl[i][j].value = 0;             np[ip - i * bound]++;           }         }         if (j) {           bl[i][j].value += bl[i][j - 1].value;         }         bl[i][j].min_value = 0;         nw = 0;         for (s = i * bound; s < min((i + 1) * bound, n); s++) {           nw += np[s - i * bound];           if (bl[i][j].min_value > nw) bl[i][j].min_value = nw;         }       }     }     scanf( %d , &num_q);     ans = 0;     while (num_q--) {       scanf( %d%d%d , &l, &r, &x);       l--;       r--;       x ^= ans;       ans = 0;       id = l / bound;       nw = fir = 0;       for (i = l; i <= min(l + m - 1, (l / bound + 1) * bound - 1); i++) {         if (a[i] < x) fir++;       }       if (l + m - 1 >= (l / bound + 1) * bound) {         for (i = (l + m - 1) / bound * bound; i <= l + m - 1; i++) {           if (a[i] < x) fir++;         }         for (i = l / bound + 1; i < (l + m - 1) / bound; i++) {           id = lower_bound(0, nk[i] - 1, bk[i], x);           fir += (id + 1);         }       }       for (i = l; i <= min(r - 1, (l / bound + 1) * bound - 1); i++) {         if (i + m < n) {           if (a[i] < x) nw--;           if (a[i + m] < x) nw++;           if (ans > nw) ans = nw;         }       }       if (r - 1 >= (l / bound + 1) * bound) {         for (i = l / bound + 1; i < (r - 1) / bound; i++) {           id = lower_bound(0, num[i] - 1, bl[i], x);           if (id >= 0) {             if (nw + bl[i][id].min_value < ans) ans = nw + bl[i][id].min_value;             nw += bl[i][id].value;           }         }         for (i = ((r - 1) / bound) * bound; i <= r - 1; i++) {           if (i + m < n) {             if (a[i] < x) nw--;             if (a[i + m] < x) nw++;             if (ans > nw) ans = nw;           }         }       }       ans += fir;       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void readi(int &x);   const int maxn = 300005;   int n, b[maxn], id[maxn], rk[maxn];   double fl[maxn], fr[maxn];   bool cmpb(const int &i, const int &j) { return b[i] < b[j]; }   struct node {     double div, dta;     node *lc, *rc;     inline void downdate() {       if (div != 1)         lc->div *= div, lc->dta *= div, rc->div *= div, rc->dta *= div, div = 1;       if (dta != 0) lc->dta += dta, rc->dta += dta, dta = 0;     }     inline void modify(int l, int r, const int &a, const int &b, const double &c,                        const double &d) {       if (l >= a && r <= b)         div *= c, dta *= c, dta += d;       else {         int mid = l + r >> 1;         downdate();         if (a <= mid) lc->modify(l, mid, a, b, c, d);         if (b > mid) rc->modify(mid + 1, r, a, b, c, d);       }     }     inline double get(int l, int r, const int &p) {       if (l == r) return dta;       int mid = l + r >> 1;       downdate();       if (p <= mid)         return lc->get(l, mid, p);       else         return rc->get(mid + 1, r, p);     }   } ndl[maxn * 3], *root;   int ns;   node *build(int l, int r) {     node *c = ndl + ns++;     c->dta = 0;     c->div = 1;     if (l == r)       c->lc = c->rc = NULL;     else {       int mid = l + r >> 1;       c->lc = build(l, mid);       c->rc = build(mid + 1, r);     }     return c;   }   void build() { ns = 1, root = build(1, n); }   void init() {     readi(n);     for (int i = 1; i <= n; i++) readi(b[i]), id[i] = i;     sort(id + 1, id + n + 1, cmpb);     for (int i = 1; i <= n; i++) rk[id[i]] = i;   }   void work() {     build();     for (int i = 1; i <= n; i++) {       root->modify(1, n, 1, n, 1, 1);       if (rk[i] > 1) root->modify(1, n, 1, rk[i] - 1, .5, 0);       fl[i] = root->get(1, n, rk[i]) / 2;     }     build();     for (int i = n; i >= 1; i--) {       root->modify(1, n, 1, n, 1, 1);       if (rk[i] > 1) root->modify(1, n, 1, rk[i] - 1, .5, 0);       fr[i] = root->get(1, n, rk[i]) / 2;     }     double ans = 0;     for (int i = 1; i <= n; i++) ans += fl[i] * fr[i] * b[i];     ans /= (double)n * n;     printf( %lf , (double)ans * 2);   }   int main() {     init();     work();     return 0;   }   inline void readi(int &x) {     char c;     for (c = getchar(); c >  9  || c <  0 ; c = getchar())       ;     x = c ^  0 ;     for (c = getchar(); c >=  0  && c <=  9 ; c = getchar())       x = x * 10 + (c ^  0 );   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > seg[2];   int n, m, k, x, y, dX, dY;   bool flag = 0;   long long ans;   void add(int tx, int ty) {     seg[0].push_back(pair<int, int>(tx - ty, tx));     seg[1].push_back(pair<int, int>(tx + ty, tx));   }   void reflect() {     int fc;     if (dX == dY)       fc = 0;     else       fc = 1;     pair<int, int> p = (fc) ? pair<int, int>(x + y, x) : pair<int, int>(x - y, x);     vector<pair<int, int> >::iterator it =         upper_bound(seg[fc].begin(), seg[fc].end(), p);     while (it->first != p.first) it--;     if (dX < 0)       while (it->second >= x) it--;     ans += abs(x - it->second) - 1;     x = it->second;     y = (fc) ? (it->first - x) : (x - it->first);     bool A = binary_search(seg[0].begin(), seg[0].end(),                            pair<int, int>(x - y - dX, x - dX));     bool B = binary_search(seg[0].begin(), seg[0].end(),                            pair<int, int>(x - y + dY, x));     if (A == B)       flag = 1, dX = -dX, dY = -dY;     else {       if (A)         x -= dX, dY = -dY;       else if (B)         y -= dY, dX = -dX;     }   }   int main() {     scanf( %d %d %d , &n, &m, &k);     for (int i = 1; i <= m; i++) add(0, i), add(n + 1, i);     for (int i = 0; i <= n + 1; i++) add(i, 0), add(i, m + 1);     for (int i = 1; i <= k; i++) {       scanf( %d %d , &x, &y);       add(x, y);     }     scanf( %d %d , &x, &y);     char c = getchar();     while (c !=  N  && c !=  S  && c !=  W  && c !=  E ) c = getchar();     if (c ==  N )       dX = -1;     else       dX = 1;     c = getchar();     if (c ==  W )       dY = -1;     else       dY = 1;     sort(seg[0].begin(), seg[0].end());     sort(seg[1].begin(), seg[1].end());     reflect();     int sx = x, sy = y, sdx = dX, sdy = dY;     ans = 0;     do reflect();     while (!(x == sx && y == sy && dX == sdx && dY == sdy));     printf( %lld n , (flag) ? (ans >> 1) : ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int OO = 0x3f3f3f3f, N = 1e5 + 5, mod = 1e9 + 7;   const double pi = acos(-1), eps = 1e-9;   int n, x, c = 1;   vector<int> v;   long long ans;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , &x);       if (x >= c) {         while (c < x) {           ans += i - v.back();           ++c;           v.pop_back();         }         ++c;       } else {         v.push_back(i);         ans += c - x - 1;       }     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int a[N];   long long dp[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       int x;       scanf( %d , &x);       a[x]++;     }     long long ans = 0;     dp[N - 1] = dp[N - 2] = 0;     for (int i = N - 3; i >= 1; --i) {       dp[i] = max(dp[i + 1], dp[i + 2] + 1LL * a[i] * i);       ans = max(ans, dp[i]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 10, MAX = 2e5 + 1e4, MOD = 1e9 + 7, MAXL = 25;   void OUT(long double o, int x) {     cout << fixed << setprecision(x) << o;     return;   }   long long a[MAX], t = 1;   vector<int> adj[MAX];   void dfs(int v, int p) {     long long c = 0;     for (int u : adj[v]) {       if (u != p) {         dfs(u, v);         if (adj[u].size() == 1) c++;       }     }     if (adj[v].size() > 1 && c < 3) t = 0;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     for (int i = 2; i <= n; i++) {       long long x;       cin >> x;       adj[x].push_back(i);       adj[i].push_back(x);     }     dfs(1, 1);     if (t)       cout <<  Yes ;     else       cout <<  No ;     return 0;   }


#include <bits/stdc++.h>   const int maxn = 1e6 + 1, mod = 1e9 + 7;   int n, m, k, cnt[maxn], pow2[maxn], ans;   int main() {     scanf( %d%d%d , &n, &m, &k);     ++k;     while (m--) {       int u, v;       scanf( %d%d , &u, &v);       if (u + 1 != v && u + k != v) {         puts( 0 );         return 0;       }       if (u + k == v) cnt[u] = 1;     }     for (int i = 1; i <= n; ++i) cnt[i] += cnt[i - 1];     for (int i = k + 1; i <= n; ++i)       if (cnt[i] - cnt[i - 1] && cnt[i - k]) {         puts( 0 );         return 0;       }     pow2[0] = 1;     for (int i = 1; i <= n; ++i) {       pow2[i] = pow2[i - 1] << 1;       if (pow2[i] >= mod) pow2[i] -= mod;     }     ans = !cnt[n];     for (int i = 1; i <= n - k; ++i)       if (cnt[i] == cnt[n]) {         if (i <= k)           ans += pow2[i - cnt[i - 1] - 1];         else if (!cnt[i - k])           ans += pow2[k - cnt[i - 1] - 1];         if (ans >= mod) ans -= mod;       }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   using v2d = vector<vector<T> >;   template <class T>   bool uin(T &a, T b) {     return a > b ? (a = b, true) : false;   }   template <class T>   bool uax(T &a, T b) {     return a < b ? (a = b, true) : false;   }   mt19937 rng(chrono::system_clock::now().time_since_epoch().count());   const int maxN = 2e3 + 10;   int n, k, a[maxN], dp[maxN][maxN];   void solve() {     cin >> n >> k;     for (int i = 1; i <= (int)(n); ++i) {       cin >> a[i];     }     for (int i = (int)(0); i <= (int)(n); ++i) {       for (int j = (int)(0); j <= (int)(n); ++j) {         dp[i][j] = -1e9;       }     }     dp[0][0] = 0;     for (int i = 1; i <= (int)(n); ++i) {       for (int j = (int)(0); j <= (int)(i); ++j) {         if (j > 0) {           uax(dp[i][j], dp[i - 1][j - 1]);         }         uax(dp[i][j], dp[i - 1][j] + (a[i] == i - j));       }     }     for (int i = 0; i < (int)(n); ++i) {       if (dp[n][i] >= k) {         cout << i <<   n ;         return;       }     }     cout << -1 <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int T = 1;     cin >> T;     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool adj[200][200];   int bfs(int source, int dest) {     bool vis[200];     for (int i = 0; i < 200; i++) vis[i] = 0;     queue<pair<int, int> > qe;     qe.push({source, 0});     while (!qe.empty()) {       int node = qe.front().first;       int d = qe.front().second;       qe.pop();       if (node == dest) return d;       vis[node] = 1;       for (int i = 0; i < 200; i++) {         if (adj[node][i] and !vis[i]) qe.push({i, d + 1});       }     }     return INT_MAX;   }   int main() {     int n;     cin >> n;     vector<long long> arr;     while (n--) {       long long x;       cin >> x;       if (x != 0) arr.push_back(x);     }     n = arr.size();     if (n > 128) {       cout << 3 << endl;       return 0;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (arr[i] & arr[j]) adj[i][j] = 1;       }     }     int min_cycle = INT_MAX;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         if (adj[i][j]) {           adj[i][j] = 0;           adj[j][i] = 0;           int m = bfs(i, j);           min_cycle = min(m, min_cycle);           adj[i][j] = 1;           adj[j][i] = 1;         }       }     }     if (min_cycle > 10000000)       cout << -1 << endl;     else       cout << min_cycle + 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int is[1010];   int main() {     int n;     scanf( %d , &n);     int x = 0;     for (int i = (0); i < (n); ++i)       for (int j = (0); j < (n); ++j) {         int v;         scanf( %d , &v);         if ((i == j) && (v == 1)) x ^= 1;       }     int q;     scanf( %d , &q);     for (int i = (0); i < (q); ++i) {       int t;       scanf( %d , &t);       if (t < 3) {         x ^= 1;         int v;         scanf( %d , &v);       } else         printf( %d , x);     }     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 840;   long long arr[9], W, ans;   long long dp[7890];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> W;     for (int i = 1; i < 9; ++i) {       cin >> arr[i];     }     if (W > 840) {       for (int i = 1; i < 9; ++i) {         long long lef = max(arr[i] - 840 / i, (long long)0);         long long g = (W - 840 - ans) / i;         g = min(g, lef);         if (g < 0) continue;         ans += g * i;         arr[i] -= g;       }     }     dp[0] = 1;     for (int i = 1; i < 9; ++i) {       for (int j = 0; j < min(arr[i], (long long)840 / i); ++j) {         for (int k = 6789; k >= i; --k) {           if (dp[k - i]) dp[k] = 1;         }       }     }     for (int k = 6789;; --k) {       if (dp[k] && ans + k <= W) {         cout << ans + k <<   n ;         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int a[n * n];     for (int i = 1; i <= n * n; ++i) a[i - 1] = i;     if (k == 1) {       long long sum = 0;       for (int i = 0; i < n; ++i) {         sum += n * i + 1;       }       cout << sum << endl;       for (int i = 0; i < n; ++i) {         for (int j = 0; j < n - 1; ++j) {           printf( %d  , a[i * n + j]);         }         printf( %d n , a[i * n + n - 1]);       }       return 0;     }     vector<int> v[n];     k--;     for (int i = 0; i < k * n; ++i) {       v[i % k].push_back(i + 1);     }     for (int i = k * n; i < n * n; ++i) {       v[(i - k * n) % (n - k) + k].push_back(i + 1);     }     long long sum = 0;     for (int i = 0; i < n; ++i) {       sum += v[k][i];     }     cout << sum << endl;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n - 1; ++j) {         printf( %d  , v[j][i]);       }       printf( %d n , v[n - 1][i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1003][1003];   int cost[43][1003][1003];   vector<vector<pair<int, int>>> COL(43);   int n, m, k;   int di1[4] = {0, 1, 0, -1};   int di2[4] = {1, 0, -1, 0};   void bfs(int col) {     vector<bool> vis(43, 0);     queue<pair<int, int>> q;     for (int i = 0; i < COL[col].size(); i++) {       cost[col][COL[col][i].first][COL[col][i].second] = 0;       q.push({COL[col][i].first, COL[col][i].second});     }     while (!q.empty()) {       int x = q.front().first;       int y = q.front().second;       q.pop();       if (!vis[a[x][y]]) {         vis[a[x][y]] = 1;         for (int i = 0; i < COL[a[x][y]].size(); i++) {           int nx = COL[a[x][y]][i].first;           int ny = COL[a[x][y]][i].second;           if (cost[col][nx][ny] == -1) {             cost[col][nx][ny] = 1 + cost[col][x][y];             q.push({nx, ny});           }         }       }       for (int i = 0; i < 4; i++) {         int nx = x + di1[i];         int ny = y + di2[i];         if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && cost[col][nx][ny] == -1) {           cost[col][nx][ny] = 1 + cost[col][x][y];           q.push({nx, ny});         }       }     }   }   int main() {     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> a[i][j];         COL[a[i][j]].push_back({i, j});       }     }     memset(cost, -1, sizeof(cost));     for (int i = 1; i <= k; i++) {       bfs(i);     }     int q;     cin >> q;     while (q--) {       int sx, sy, ex, ey;       cin >> sx >> sy >> ex >> ey;       int ans = abs(ex - sx) + abs(ey - sy);       for (int i = 1; i <= k; i++) {         ans = min(ans, 1 + cost[i][sx][sy] + cost[i][ex][ey]);       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long i, i1, j, k, k1, t, n, m, res, flag[10], a, b, mt[110][110], s;   char c;   vector<array<long long, 6>> sl;   vector<array<long long, 3>> cl;   void mv(long long a, long long b, long long c, long long d, long long e,           long long f) {     sl.push_back({a, b, c, d, e, f});     mt[a][b] ^= 1;     mt[c][d] ^= 1;     mt[e][f] ^= 1;   }   void cl4(vector<long long> st) {     long long i, l, x, s = 0;     for (i = 1; i <= 4; i++) s += st[i];     if (s == 0) return;     if (s == 3) {       l = 0;       cl.push_back({0, 0, 0});       x = cl.size();       x--;       for (i = 1; i <= 4; i++) {         if (st[i] == 1) {           st[i] ^= 1;           cl[x][l] = i;           l++;         }       }     }     if (s == 2) {       flag[1] = 0;       l = 0;       cl.push_back({0, 0, 0});       x = cl.size();       x--;       for (i = 1; i <= 4; i++) {         if (st[i] == 0 || (st[i] == 1 && flag[1] == 0)) {           st[i] ^= 1;           cl[x][l] = i;           l++;           if (st[i] == 0) flag[1] = 1;         }       }       cl4(st);     }     if (s == 1) {       flag[0] = 0;       l = 0;       cl.push_back({0, 0, 0});       x = cl.size();       x--;       for (i = 1; i <= 4; i++) {         if (st[i] == 1 || (st[i] == 0 && flag[0] < 2)) {           st[i] ^= 1;           cl[x][l] = i;           l++;           if (st[i] == 1) flag[0]++;         }       }       cl4(st);     }     if (s == 4) {       flag[1] = 0;       l = 0;       cl.push_back({0, 0, 0});       x = cl.size();       x--;       for (i = 1; i <= 4; i++) {         if (st[i] == 0 || (st[i] == 1 && flag[1] < 3)) {           st[i] ^= 1;           cl[x][l] = i;           l++;           if (st[i] == 0) flag[1]++;         }       }       cl4(st);     }   }   void init(vector<long long> st) {     cl.clear();     cl4(st);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> t;     while (t--) {       cin >> n >> m;       sl.clear();       for (i = 1; i <= n; i++) {         for (j = 1; j <= m; j++) {           cin >> c;           if (c ==  0 )             mt[i][j] = 0;           else             mt[i][j] = 1;         }       }       for (i = 1; i <= n - 2; i++) {         for (j = 1; j <= m - 1; j++) {           if (mt[i][j] == 1 && mt[i][j + 1] == 1) {             mv(i, j, i, j + 1, i + 1, j);           } else if (mt[i][j] == 1 && mt[i][j + 1] == 0) {             mv(i, j, i + 1, j, i + 1, j + 1);           } else if (mt[i][j] == 0 && mt[i][j + 1] == 1) {             mv(i, j + 1, i + 1, j, i + 1, j + 1);           }         }       }       if (m % 2) {         if (mt[n - 1][m] == 1 && mt[n][m] == 1) {           mv(n - 1, m, n, m - 1, n, m);         } else if (mt[n - 1][m] == 1 && mt[n][m] == 0) {           mv(n - 1, m - 1, n - 1, m, n, m - 1);         } else if (mt[n - 1][m] == 0 && mt[n][m] == 1) {           mv(n - 1, m - 1, n, m - 1, n, m);         }       }       for (j = 1; j <= m - 1; j += 2) {         vector<long long> st;         vector<array<long long, 2>> fn;         st.push_back(0);         st.push_back(mt[n - 1][j]);         st.push_back(mt[n - 1][j + 1]);         st.push_back(mt[n][j]);         st.push_back(mt[n][j + 1]);         init(st);         for (auto u : cl) {           fn.clear();           for (i = 0; i < 3; i++) {             if (u[i] == 1)               fn.push_back({n - 1, j});             else if (u[i] == 2)               fn.push_back({n - 1, j + 1});             else if (u[i] == 3)               fn.push_back({n, j});             else if (u[i] == 4)               fn.push_back({n, j + 1});           }           sl.push_back({0, 0, 0, 0, 0, 0});           for (i = 0; i < 3; i++) {             s = sl.size();             s--;             sl[s][2 * i] = fn[i][0];             sl[s][2 * i + 1] = fn[i][1];           }         }       }       s = sl.size();       cout << s <<   n ;       for (auto u : sl) {         for (i = 0; i < 6; i++) cout << u[i] <<    ;         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int sz0[400000];   vector<int> g[400000];   vector<int> c0[400000];   int centroid;   int sz[400000];   vector<int> cs[400000];   int ans[400000];   void dfs0(int x) {     sz0[x] = 1;     for (int y : g[x]) {       if (sz0[y] == 0) {         c0[x].push_back(y);         dfs0(y);         sz0[x] += sz0[y];       }     }   }   int findc(int x) {     for (int y : c0[x]) {       if (sz0[y] > n / 2) {         return findc(y);       }     }     return x;   }   void dfs1(int x) {     sz[x] = 1;     for (int y : g[x]) {       if (sz[y] == 0) {         cs[x].push_back(y);         dfs1(y);         sz[x] += sz[y];       }     }   }   bool cmp(int a, int b) { return sz[a] > sz[b]; }   void dfs2(int x, int s, int m) {     if (s - m <= n / 2) ans[x] = 1;     for (int y : cs[x]) {       int ss = s + sz[x] - sz[y];       dfs2(y, ss, m);     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       g[a].push_back(b);       g[b].push_back(a);     }     dfs0(0);     centroid = findc(0);     dfs1(centroid);     ans[centroid] = 1;     if (cs[centroid].size() == 1) {       assert(n <= 2);       ans[0] = ans[1] = 1;     } else {       vector<int>& c = cs[centroid];       sort(c.begin(), c.end(), cmp);       dfs2(c[0], sz[centroid] - sz[c[0]], sz[c[1]]);       for (int i = 1; i < c.size(); i++)         dfs2(c[i], sz[centroid] - sz[c[i]], sz[c[0]]);       if (sz[c[0]] * 2 == n)         for (int i = 0; i < n; i++) ans[i] = 1;     }     for (int i = 0; i < n; i++) printf( %d  , ans[i]);     putchar(  n );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int a = min(k, 26) + 97;     vector<char> b;     for (int i = 97; i < a; i++) {       b.push_back((char)i);     }     string key =   ;     for (int i = 0; i < b.size(); i++) key += b[i];     int c = n - k, j = 0;     while (true) {       key += b[j];       j++;       if (j > b.size() - 1) j = 0;       c--;       if (c <= 0) break;     }     if (key.size() > n) {       key.erase(key.size() - 1, 1);     }     cout << key;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string itoa(int x) {     string s =   ;     char c;     if (x < 0) x *= -1, s +=  - ;     while (x) {       c = x % 10 + 48;       s += c;       x /= 10;     }     if (s[0] ==  - )       reverse(s.begin() + 1, s.end());     else       reverse(s.begin(), s.end());     if (s ==   ) s +=  0 ;     return s;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     priority_queue<int, vector<int>, greater<int> > pq;     int n;     cin >> n;     string s, t, c;     int x;     vector<string> ans;     while (n--) {       cin >> s;       if (s !=  removeMin ) {         cin >> x;         if (s ==  insert ) {           pq.push(x);           t =  insert  , c = itoa(x), t += c;           ans.push_back(t);         } else {           while (!pq.empty() && pq.top() < x) {             t =  removeMin ;             ans.push_back(t);             pq.pop();           }           if (pq.empty() || pq.top() > x)             t =  insert  , c = itoa(x), t += c, ans.push_back(t), pq.push(x);           t =  getMin  , c = itoa(x), t += c;           ans.push_back(t);         }       } else {         if (pq.empty()) t =  insert 0 , ans.push_back(t), pq.push(0);         t =  removeMin , ans.push_back(t), pq.pop();       }     }     cout << ans.size() <<   n ;     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int getValue(vector<int> &ai, int n, int sum) {     int val = 0;     int mval = ai[ai.size() - 1];     for (int i = 1; i <= mval; i++) {       if (n * i >= sum) {         val = i;         break;       }     }     return val;   }   int main() {     int q;     scanf( %d , &q);     for (int i = 0; i < q; i++) {       int n;       vector<int> ai;       scanf( %d , &n);       int sum = 0;       for (int j = 0; j < n; j++) {         int temp;         scanf( %d , &temp);         ai.push_back(temp);         sum += temp;       }       sort(ai.begin(), ai.end());       int ret = getValue(ai, n, sum);       printf( %d n , ret);     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000 * 100 + 3;   int n, a[N];   void input() {     cin >> n;     for (int i = 0; i + 1 < n; i++) cin >> a[i];   }   void solve() {     sort(a, a + n - 1);     for (int i = 0; i + 1 < n; i++)       if (a[i] != i + 1) cout << i + 1 << endl, exit(0);     cout << n << endl;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     input(), solve();     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   inline long long input() {     long long n;     cin >> n;     return n;   }   string base2(long long n) {     string a =   ;     while (n >= 2) {       a += (char)(n % 2 +  0 );       n /= 2;     }     a += (char)(n +  0 );     reverse((a).begin(), (a).end());     return a;   }   const int MAXN = 1e6 + 10;   const int MOD = 1e9 + 7;   const int MOD2 = 998244353;   const long long INF = 8e18;   long long pw(long long a, long long b, long long mod) {     return (!b ? 1                : (b & 1 ? (a * pw(a * a % mod, b / 2, mod)) % mod                         : pw(a * a % mod, b / 2, mod)));   }   int n, m, mark[MAXN], king[MAXN], par[MAXN], sz[MAXN];   vector<pair<int, int> > G[MAXN];   vector<int> M;   long long dis[MAXN];   pair<long long, pair<long long, long long> > E[MAXN];   vector<pair<long long, pair<long long, long long> > > E2;   int GET(int x) { return ((x == par[x]) ? x : par[x] = GET(par[x])); }   bool unify(int x, int y) {     int v = GET(x), u = GET(y);     if (v == u) return 0;     if (sz[u] > sz[v]) {       swap(u, v);     }     par[u] = v;     sz[v] += sz[u];     return 1;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> m;     for (int i = 1; i <= n; i++) dis[i] = 1e18, sz[i] = 1, par[i] = i;     for (int i = 1; i <= m; i++) {       int a, b, c;       cin >> a >> b >> c;       G[a].push_back(make_pair(b, c));       G[b].push_back(make_pair(a, c));       E[i] = make_pair(c, make_pair(a, b));     }     set<pair<long long, long long> > st;     int k;     cin >> k;     for (int i = 1; i <= k; i++) {       int a;       cin >> a;       M.push_back(a);       king[a] = a;       st.insert(make_pair(0, a));       dis[a] = 0;     }     while (!st.empty()) {       pair<long long, long long> cu = *st.begin();       st.erase(st.begin());       int v = cu.second;       if (mark[v]) {         continue;       }       mark[v] = 1;       for (auto y : G[v]) {         long long u = y.first, cost = y.second;         if (dis[u] > dis[v] + cost) {           king[u] = king[v];           dis[u] = dis[v] + cost;           st.insert(make_pair(dis[u], u));         }       }     }     for (int i = 1; i <= m; i++) {       long long v = E[i].second.first, u = E[i].second.second, cost = E[i].first;       if (king[u] != king[v]) {         E2.push_back(             make_pair(dis[v] + dis[u] + cost, make_pair(king[u], king[v])));       }     }     sort((E2).begin(), (E2).end());     long long MST = 0;     for (auto x : E2) {       long long u = x.second.first, v = x.second.second, cost = x.first;       if (unify(v, u)) {         MST += cost;       }     }     set<long long> second;     for (auto x : M) {       second.insert(GET(x));     }     if ((int)second.size() > 1) {       return cout << 1e18, 0;     }     cout << MST + dis[1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, p[50];   int ask(int second) {     cout << second <<   n ;     fflush(stdout);     cin >> second;     return second;   }   int main() {     cin >> m >> n;     for (int i = 0; i < n; i++) {       p[i] = ask(1);       if (p[i] == 0) {         cout << 1 <<   n ;         exit(0);       }     }     int l = 1, r = m, t = 0;     while (l <= r) {       int m = (l + r) / 2;       int tmp = ask(m) * p[t];       if (tmp == 0) {         cout << tmp <<   n ;         exit(0);       }       if (tmp == -1)         r = m - 1;       else         l = m + 1;       t = (t + 1) % n;     }   }


#include <bits/stdc++.h>   using namespace std;   string s;   long long int dp[510][501];   long long int solve(long long int l, long long int r) {     if (dp[l][r] != -1) {       return dp[l][r];     }     if (l > r) return 0;     long long int a = 1 + solve(l + 1, r);     for (long long int i = l + 1; i <= r; i++) {       if (s[i] != s[l]) continue;       a = min(a, solve(l + 1, i - 1) + solve(i, r));     }     dp[l][r] = a;     return a;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, i;     cin >> n;     cin >> s;     memset(dp, -1, sizeof dp);     cout << solve(0, s.size() - 1);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     t = 1;     while (t--) {       long long n, a[1001];       cin >> n;       for (int i = 0; i <= n - 1; i++) {         cin >> a[i];       }       sort(a, a + n);       if (a[0] == 1)         cout << -1;       else         cout << 1;     }     cerr <<  Time taken :   << (float)clock() / CLOCKS_PER_SEC <<   secs  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = 3.141592653589793;   const int INF = 2000000000;   const int mod = 1000000007;   const int N = 100000000;   const int base = 10;   vector<int> read(string s) {     vector<int> a;     for (int i = (int)s.length(); i > 0; i -= 1)       if (i < 1)         a.push_back(atoi(s.substr(0, i).c_str()));       else         a.push_back(atoi(s.substr(i - 1, 1).c_str()));     return a;   }   vector<int> add(vector<int> a, vector<int> b) {     int carry = 0;     for (size_t i = 0; i < max(a.size(), b.size()) || carry; ++i) {       if (i == a.size()) a.push_back(0);       a[i] += carry + (i < b.size() ? b[i] : 0);       carry = a[i] >= base;       if (carry) a[i] -= base;     }     return a;   }   vector<int> mul(vector<int> a, int b) {     int carry = 0;     for (size_t i = 0; i < a.size() || carry; ++i) {       if (i == a.size()) a.push_back(0);       long long cur = carry + a[i] * 1ll * b;       a[i] = int(cur % base);       carry = int(cur / base);     }     while (a.size() > 1 && a.back() == 0) a.pop_back();     return a;   }   vector<int> c;   vector<int> a[100005], b[100005];   vector<bool> f;   vector<bool> f1;   int main() {     vector<int> queue;     int n, x, y, m;     cin >> n >> m;     for (int(i) = (0); (i) < (n); (i)++) {       cin >> x;       c.push_back(x);     }     for (int(i) = (0); (i) < (m); (i)++) {       cin >> x >> y;       x--;       y--;       a[x].push_back(y);       b[y].push_back(x);     }     for (int(i) = (0); (i) < (c.size()); (i)++)       if (c[i] == 1) {         queue.push_back(i);         f.push_back(true);       } else {         f.push_back(false);       }     for (int(i) = (0); (i) < (queue.size()); (i)++) {       for (int(j) = (0); (j) < (a[queue[i]].size()); (j)++) {         if (f[a[queue[i]][j]]) continue;         queue.push_back(a[queue[i]][j]);         f[a[queue[i]][j]] = true;       }     }     queue.clear();     for (int(i) = (0); (i) < (c.size()); (i)++)       if (c[i] == 2) {         queue.push_back(i);         f1.push_back(true);       } else {         f1.push_back(false);       }     for (int(i) = (0); (i) < (queue.size()); (i)++) {       for (int(j) = (0); (j) < (b[queue[i]].size()); (j)++) {         if (f1[b[queue[i]][j]]) continue;         f1[b[queue[i]][j]] = true;         if (c[b[queue[i]][j]] == 1) continue;         queue.push_back(b[queue[i]][j]);       }     }     for (int(i) = (0); (i) < (n); (i)++)       if (f[i] && f1[i]) {         cout << 1 << endl;       } else {         cout << 0 << endl;       }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const int MOD = 1000000007;   const int UNDEF = -1;   const int INF = 1 << 30;   template <typename T>   inline bool chkmax(T &aa, T bb) {     return aa < bb ? aa = bb, true : false;   }   template <typename T>   inline bool chkmin(T &aa, T bb) {     return aa > bb ? aa = bb, true : false;   }   int rint() {     int x;     scanf( %d , &x);     return x;   }   long long rlong() {     long long x;     scanf( %lld , &x);     return x;   }   const int mn = (5e5) + 4;   long long c[mn];   map<long long, vector<pair<int, int> > > sig2e;   map<int, vector<int> > h;   set<int> seen;   int cnt = 0;   void dfs(int x) {     seen.insert(x);     for (auto &y : h[x]) {       if (seen.find(y) == seen.end()) {         --cnt;         dfs(y);       }     }   }   int ptwo[mn];   int main() {     int n = rint(), m = rint(), k = rint();     ptwo[0] = 1;     for (int i = 1; i < mn; i++) {       ptwo[i] = ptwo[i - 1] * 2;       if (ptwo[i] >= MOD) ptwo[i] -= MOD;     }     for (int x = 1; x <= n; x++) c[x] = rlong();     for (int i = 0; i < m; i++) {       int x = rint(), y = rint();       long long sig = c[x] ^ c[y];       sig2e[sig].push_back(make_pair(x, y));     }     int rem = (ptwo[k] - (int)sig2e.size() + MOD) % MOD;     int ans = (rem * (long long)ptwo[n]) % MOD;     for (auto &sigve : sig2e) {       auto &ve = sigve.second;       h.clear();       seen.clear();       for (auto &e : ve) {         int x = e.first, y = e.second;         h[x].push_back(y);         h[y].push_back(x);       }       cnt = n;       for (auto &e : ve) {         int x = e.first, y = e.second;         if (seen.find(x) == seen.end()) dfs(x);         if (seen.find(y) == seen.end()) dfs(y);       }       ans += ptwo[cnt];       if (ans >= MOD) ans -= MOD;     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   const long long mod = 1000000007;   const long long Inf = 1e9;   using namespace std;   long long a, b, d, k, t;   double f(long long x) {     double res = 0;     res = (double)x * (double)a +           (double)(max(x / k - (x % k == 0 ? 1 : 0), (long long)0)) * (double)t +           (double)(d - x) * (double)b;     return res;   }   int main() {     scanf( %I64d%I64d%I64d%I64d%I64d , &d, &k, &a, &b, &t);     long long l = 1, r = d;     double ans = 1e18;     while (r - l > 2000000) {       long long x1 = l + (r - l) / 3;       long long x2 = r - (r - l) / 3;       if (f(x1) > f(x2))         l = x1;       else if (f(x1) < f(x2))         r = x2;       else {         l = x1;         r = x2;       }     }     for (long long i = l; i <= r; ++i) {       ans = min(ans, f(i));     }     printf( %0.lf , ans);     return 0;   }


#include <bits/stdc++.h>   static int solve();   using ll = long long;   int main(int argc, char* argv[]) {     int tc = 1;     scanf( %d , &tc);     while (tc--) solve();     return 0;   }   static constexpr int MOD = 998244353;   static int solve() {     int n, x, y;     scanf( %d %d %d , &n, &x, &y);     int sum = x + y;     int min_pos, max_pos;     if (sum == n + n) {       min_pos = n;       max_pos = n;     } else if (sum <= n) {       min_pos = 1;       max_pos = sum - 1;     } else {       min_pos = sum - n + 1;       max_pos = n;     }     printf( %d %d n , min_pos, max_pos);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, l, r, p, span, t, lwr, upr;   long pr[200000];   vector<long> vec[200000];   long calc[200000];   long long res, nmoves[200000];   long long check[200000];   long long temp;   long long answ;   long long gcd(long long a, long long b) {     while (a && b) a > b ? a %= b : b %= a;     return a + b;   }   long long solve1(long long x, long long lim) {     long long res = 0;     for (int mask = 0; mask < (1 << (vec[x].size())); mask++) {       temp = 1;       for (int j = 0; j < vec[x].size(); j++)         if (mask & (1 << j)) temp *= vec[x][j];       if (calc[mask] % 2)         res -= lim / temp;       else         res += lim / temp;     }     return res;   }   long long solve(long long x, long long lim) {     long long res = 0;     long long ts = 0;     long long q, qsum;     for (int mask = 0; mask < (1 << (vec[x].size())); mask++) {       temp = 1;       for (int j = 0; j < vec[x].size(); j++)         if (mask & (1 << j)) temp *= vec[x][j];       q = lim / temp;       qsum = q * (q + 1) / 2;       qsum *= temp;       if (calc[mask] % 2)         res -= qsum;       else         res += qsum;     }     return res;   }   long long gett(long long g, long long x, long long lim) {     if (lim <= 0) return 0;     long long tsum = (g + 1) * solve1(x, lim);     tsum -= solve(x, lim);     return tsum;   }   int main() {     ios_base::sync_with_stdio(0);     pr[1] = 1;     for (int i = 2; i <= 100000; i++)       if (pr[i] == 0)         for (int j = i * 2; j <= 100000; j += i) {           pr[j] = 1;           vec[j].push_back(i);         }     for (int i = 2; i <= 100000; i++)       if (pr[i] == 0) vec[i].push_back(i);     calc[0] = 0;     for (int i = 1; i <= (1 << 15); i++) {       t = i;       for (int j = 0; j < 15; j++)         if (t & (1 << j)) ++calc[i];     }     long long a, b;     cin >> n >> m;     cin >> l >> r >> p;     l *= l;     r *= r;     for (long long span = 2; span <= 100000 && span <= n; span++) {       if (span * span > r) continue;       t = l - span * span;       if (t <= 0)         lwr = 0;       else         lwr = sqrt(t - 0.5) + 1;       t = r - span * span;       upr = sqrt(t + 0.5);       if (upr >= span) upr = span - 1;       if (upr > m) upr = m;       if (upr < lwr) continue;       res = (gett(m, span, upr) - gett(m, span, lwr - 1)) * (n - span + 1) * 2;       answ = answ + res;       answ %= p;     }     swap(n, m);     for (long long span = 2; span <= 100000 && span <= n; span++) {       if (span * span > r) continue;       t = l - span * span;       if (t <= 0)         lwr = 0;       else         lwr = sqrt(t - 0.5) + 1;       t = r - span * span;       upr = sqrt(t + 0.5);       if (upr >= span) upr = span - 1;       if (upr > m) upr = m;       if (upr < lwr) continue;       res = (gett(m, span, upr) - gett(m, span, lwr - 1)) * (n - span + 1) * 2;       answ = answ + res;       answ %= p;     }     if (1 >= l && 1 <= r) answ += n * (m + 1), answ += m * (n + 1);     if (2 >= l && 2 <= r) answ += 2 * n * m;     cout << answ % p << endl;     cin.get();     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string p( / ), t;     while (n--) {       string foo;       cin >> foo;       if (foo ==  pwd ) {         cout << p << endl;       } else {         string dir;         cin >> dir;         dir +=  / ;         for (int i = 0; i < dir.length(); i++) {           t += dir[i];           if (dir[i] ==  / ) {             if (t ==  / )               p = t;             else if (t ==  ../ ) {               for (int j = p.length() - 1; j > 0 && p[j - 1] !=  / ; j--) {                 p.resize(j - 1);               }             } else               p += t;             t =   ;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());   mt19937 rnf(2106);   const int N = 300005;   int n;   int a[N];   vector<int> v[N];   int ans[N];   void solv() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) v[i].clear();     for (int i = 1; i <= n; ++i) {       v[a[i]].push_back(i);     }     for (int i = 1; i <= n; ++i) ans[i] = N;     for (int i = 1; i <= n; ++i) {       if (v[i].empty()) continue;       int maxu = v[i][0] - 1;       for (int j = 0; j < ((int)(v[i]).size()) - 1; ++j)         maxu = max(maxu, v[i][j + 1] - v[i][j] - 1);       maxu = max(maxu, n - v[i].back());       ans[maxu + 1] = min(ans[maxu + 1], i);     }     for (int i = 2; i <= n; ++i) ans[i] = min(ans[i], ans[i - 1]);     for (int i = 1; i <= n; ++i) {       if (ans[i] == N)         printf( -1  );       else         printf( %d  , ans[i]);     }     printf(  n );   }   int main() {     int tt;     scanf( %d , &tt);     while (tt--) solv();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1000000000000000000LL;   const double EPS = 1e-9;   const long long mod = 1000000007ll;   const int maxn = 500500;   int longestP[1000500];   char pat[1000500];   void KMP() {     for (int i = 1, k = 0; pat[i]; i++) {       while (k > 0 && pat[i] != pat[k]) k = longestP[k - 1];       if (pat[i] == pat[k])         longestP[i] = ++k;       else         longestP[i] = k;     }   }   int main() {     scanf( %s , pat);     int n = strlen(pat);     KMP();     if (longestP[n - 1] == 0) {       puts( Just a legend );       return 0;     }     for (int i = 0; i + 1 < n; i++)       if (longestP[n - 1] == longestP[i]) {         for (int j = 0; j < longestP[i]; j++) putchar(pat[j]);         return 0;       }     if (longestP[longestP[n - 1] - 1] == 0) {       puts( Just a legend );       return 0;     }     for (int j = 0; j < longestP[longestP[n - 1] - 1]; j++) putchar(pat[j]);   }


#include <bits/stdc++.h>   using namespace std;   int n, ans = 0;   int a[1000 + 10], pre[9][1000 + 10], kr[9][1000 + 10], cnt[10];   int f[1000 + 10][1 << 8];   int g[9] = {0, 1, 2, 4, 8, 16, 32, 64, 128};   int read() {     int x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return x * f;   }   int find(int t, int p) {     int l = 1, r = n, tmp;     while (l <= r) {       int mid = l + r >> 1;       pre[t][mid] > p || pre[t][mid] == 0 ? r = mid - 1                                           : (l = mid + 1, tmp = mid);     }     return tmp;   }   bool check(int st) {     memset(f, -1, sizeof(f));     int tmp = -1;     for (int i = 1; i <= n; i++) {       f[i][0] = 0;       for (int j = 0; j < (1 << 8); j++) {         if (j != (1 << 8) - 1) {           for (int k = 1; k <= 8; k++)             if (!(j & g[k]) && f[i][j] != -1) {               int p = pre[k][i - (a[i] == k)];               if (st == 0) tmp = max(tmp, f[i][j]);               if (kr[k][p + st] != 0)                 f[kr[k][p + st]][j | g[k]] =                     max(f[kr[k][p + st]][j | g[k]], f[i][j] + st);               if (kr[k][p + st + 1] != 0)                 f[kr[k][p + st + 1]][j | g[k]] =                     max(f[kr[k][p + st + 1]][j | g[k]], f[i][j] + st + 1);             }         } else if (f[i][j] != -1)           tmp = max(tmp, f[i][j]);       }     }     if (tmp != -1) {       ans = max(ans, tmp);       return true;     } else       return false;   }   int main() {     n = read();     for (int i = 1; i <= n; i++) {       a[i] = read();       for (int j = 1; j <= 8; j++) pre[j][i] = pre[j][i - 1] + (a[i] == j);       kr[a[i]][++cnt[a[i]]] = i;     }     int l = 0, r = n;     while (l <= r) {       int mid = l + r >> 1;       check(mid) ? l = mid + 1 : r = mid - 1;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[25] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29,                31, 37, 41, 43, 47, 4,  9,  25, 49};   int main() {     string s;     int num = 0;     for (int i = 0; i < 19; i++) {       printf( %d n , a[i]);       fflush(stdout);       cin >> s;       if (s ==  yes ) num++;     }     if (num <= 1)       printf( prime n );     else       printf( composite n );     fflush(stdout);   }


#include <bits/stdc++.h>   using namespace std;   const char IO_MODE = 3;   inline long long ReadInt() {     long long x = 0, s = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) s = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = x * 10 + c -  0 ;       c = getchar();     }     return s * x;   }   inline void WriteInt(long long x) {     char c[20];     if (!x) {       putchar( 0 );       return;     }     if (x < 0) putchar( - ), x = -x;     int i = 0;     while (x > 0) c[++i] = x % 10, x /= 10;     while (i) putchar(c[i--] + 48);   }   template <typename T>   inline void out(T x) {     if (IO_MODE & 1)       WriteInt(x);     else if (typeid(x) == typeid(int))       printf( %i , x);     else       printf( %lld , (long long)x);   }   template <typename T, typename... Args>   inline void out(T x, Args... args) {     out(x);     putc(   , stdout);     out(args...);   }   template <typename T>   inline void in(T &x) {     if (IO_MODE & 2)       x = ReadInt();     else if (typeid(x) == typeid(int))       scanf( %i , &x);     else if (typeid(x) == typeid(long long))       scanf( %lld , &x);   }   template <typename T, typename... Args>   inline void in(T &x, Args &...args) {     in(x);     in(args...);   }   int tc;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c *x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug &operator<<(const c &) {       return *this;     }   };   vector<char *> tokenizer(const char *args) {     char *token = new char[111];     strcpy(token, args);     token = strtok(token,  ,  );     vector<char *> v({token});     while (token = strtok(NULL,  ,  )) v.push_back(token);     return reverse(v.begin(), v.end()), v;   }   void debugg(vector<char *> args) { cerr <<   b b  ; }   template <typename Head, typename... Tail>   void debugg(vector<char *> args, Head H, Tail... T) {     debug() <<   [  << args.back() <<  :   << H <<  ]  ;     args.pop_back();     debugg(args, T...);   }   long long n, A[100200];   long long check(long long x) {     if (!x) return 0;     for (long long i = 0; i < n; i++)       if (A[i] % x > A[i] / x) return 0;     return 1;   }   int32_t main() {     in(n);     for (long long i = 0; i < n; i++) in(A[i]);     sort(A, A + n);     long long x = A[0];     long long mx = 1;     for (long long i = 1; i * i <= x; i++) {       long long m = x / i;       if (x % i == 0) {         if (check(i - 1)) mx = max(mx, i - 1);         if (check(m - 1)) mx = max(mx, m - 1);         if (check(i)) mx = max(mx, i);         if (check(m)) mx = max(mx, m);       } else {         if (check(i)) mx = max(mx, i);         if (check(m)) mx = max(mx, m);       }     }     long long ans = 0;     for (long long i = 0; i < n; i++) ans += (A[i] + mx) / (mx + 1);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int i, t;   double a, b;   int main() {     cin >> t;     for (i = 0; i < t; i++) {       cin >> a >> b;       if (b == 0) {         cout << 1 << endl;         continue;       }       b *= 4;       if (a > b) {         cout << 1.0 - b / (4.0 * a) << endl;         continue;       }       cout << (a / 2.0 + b) / (2.0 * b) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t, n, k;     cin >> t;     while (t--) {       cin >> n;       int arr[n];       int c1 = 0;       for (int i = 0; i < n; i++) {         cin >> arr[i];         if (arr[i] == 1) c1++;       }       if (n - c1 >= c1) {         cout << n - c1 << endl;         for (int i = 0; i < n - c1; i++) {           cout <<  0  ;         }         cout << endl;       } else {         if (c1 % 2 == 0) {           cout << c1 << endl;           for (int i = 0; i < c1; i++) {             cout <<  1  ;           }           cout << endl;         } else {           cout << c1 - 1 << endl;           for (int i = 0; i < c1 - 1; i++) {             cout <<  1  ;           }           cout << endl;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   long long a[N], n, seg[20][N];   void build(int xl = 0, int xr = n, int ind = 0) {     if (xr - xl == 1) {       seg[ind][xl] = a[xr] - a[xl];       return;     }     int xm = (xl + xr) / 2;     build(xl, xm, ind + 1);     build(xm, xr, ind + 1);     for (int i = xl; i < xm; i++) seg[ind][i] = a[xm] - a[i];     for (int i = xm; i < xr; i++) seg[ind][i] = a[i + 1] - a[xm];     sort(&seg[ind][xl], &seg[ind][xm]);     sort(&seg[ind][xm], &seg[ind][xr]);   }   inline long long GreaterThan(long long x, int xl = 0, int xr = n, int k = 1,                                int ind = 0) {     if (xr - xl == 1) return (seg[ind][xl] > x);     int xm = (xl + xr) / 2;     long long GreaterChild = GreaterThan(x, xl, xm, 2 * k, ind + 1) +                              GreaterThan(x, xm, xr, 2 * k + 1, ind + 1);     long long GreaterMid = 0;     int j = xr;     for (int i = xl; i < xm; i++) {       while (j > xm && seg[ind][j - 1] + seg[ind][i] > x) j--;       GreaterMid += xr - j;     }     return GreaterChild + GreaterMid;   }   int main() {     ios::sync_with_stdio(0);     long long k;     scanf( %lld %lld , &n, &k);     for (int i = 0; i < n; i++) scanf( %lld , &a[i + 1]);     for (int i = 1; i < n; i++) a[i + 1] += a[i];     build();     long long l = -1e14 - 7, r = 1e14 + 7;     while (r - l > 1) {       long long m = (l + r) / 2;       long long ans = GreaterThan(m);       if (ans >= k)         l = m;       else         r = m;     }     printf( %lld n , r);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 500 + 10;   int R, C;   char pic[maxn][maxn];   bool vis[maxn][maxn];   int sr, sc, er, ec;   const int dr[] = {-1, 0, 1, 0};   const int dc[] = {0, 1, 0, -1};   bool ok(int r, int c) { return r >= 1 && r <= R && c >= 1 && c <= C; }   struct Node {     int r, c;     Node(int r = 0, int c = 0) : r(r), c(c) {}   };   bool bfs() {     memset(vis, 0, sizeof(vis));     queue<Node> Q;     Q.push(Node(sr, sc));     while (!Q.empty()) {       Node now = Q.front();       Q.pop();       int r = now.r, c = now.c;       pic[r][c] =  X ;       for (int i = 0; i < 4; ++i) {         int tr = r + dr[i];         int tc = c + dc[i];         if (tr == er && tc == ec) {           if (pic[tr][tc] ==  X ) return true;           for (int i = 0; i < 4; ++i) {             int ttr = tr + dr[i], ttc = tc + dc[i];             if (ok(ttr, ttc) && pic[ttr][ttc] ==  . ) return true;           }         }         if (ok(tr, tc) && !vis[tr][tc] && pic[tr][tc] ==  . ) {           vis[tr][tc] = 1;           Q.push(Node(tr, tc));         }       }     }     return false;   }   int main() {     scanf( %d %d , &R, &C);     for (int i = 1; i <= R; ++i) scanf( %s , pic[i] + 1);     scanf( %d %d %d %d , &sr, &sc, &er, &ec);     if (bfs())       puts( YES );     else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double majk;   double dp[8005][1005];   int visdp[8005][1005];   double solve(int n, int k) {     if (n == 0 && k == 0) return 1;     if (n == 0) return 0;     double sol = 0;     if (visdp[n][k]) return dp[n][k];     if (k) {       sol += solve(n - 1, k - 1) * ((double)k / majk);     }     sol += solve(n - 1, k) * ((double)(majk - k) / majk);     visdp[n][k] = 1;     return dp[n][k] = sol;   }   int main() {     int k, q;     cin >> k >> q;     majk = k;     solve(8000, k);     while (q--) {       double x;       double e = 1e-7;       cin >> x;       double lhs = (x - e) / 2000.0;       for (int i = k; i <= 8000; i++) {         if (solve(i, k) > lhs) {           cout << i <<   n ;           break;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long maxm = 0;   long long nmbr = INT_MAX;   map<long long, bool> vstd;   class graph {    public:     map<long long, list<pair<long long, long long>>> adj;     void input(long long n) {       long long x, y, z;       for (long long i = 1; i < n; i++) {         cin >> x >> y >> z;         adj[x].push_back(make_pair(y, z));         adj[y].push_back(make_pair(x, z));       }     }     void dfs(long long a, long long sum) {       vstd[a] = true;       for (auto j : adj[a]) {         long long nghbr = j.first;         long long wt = j.second;         if (!vstd[nghbr]) {           dfs(nghbr, sum + wt);         }       }       maxm = max(maxm, sum);     }   };   int main() {     graph g;     long long n;     cin >> n;     g.input(n);     g.dfs(0, 0);     cout << maxm;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     cin.sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long t;     cin >> t;     while (t--) {       long long n, s;       cin >> n >> s;       long long half = n / 2;       half++;       cout << s / half << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(double(-1));   const double INF = 1e10;   const double EPS = 1e-8;   inline double sqr(double x) { return x * x; }   struct PT {     double x, y;     PT() {}     PT(double x, double y) : x(x), y(y) {}     void in() {       double _x, _y;       scanf( %lf%lf , &_x, &_y);       x = _x, y = _y;     }   };   bool operator<(const PT &p1, const PT &p2) {     if (fabs(p1.x - p2.x) > EPS) return p1.x < p2.x;     return p1.y + EPS < p2.y;   }   bool operator==(const PT &p1, const PT &p2) {     return fabs(p1.x - p2.x) < EPS && fabs(p1.y - p2.y) < EPS;   }   PT operator+(PT p1, PT p2) { return PT(p1.x + p2.x, p1.y + p2.y); }   PT operator-(PT p1, PT p2) { return PT(p1.x - p2.x, p1.y - p2.y); }   PT operator*(PT p, double c) { return PT(p.x * c, p.y * c); }   PT operator/(PT p, double c) { return PT(p.x / c, p.y / c); }   double dis(PT p) { return sqrt(sqr(p.x) + sqr(p.y)); }   double dis2(PT p) { return sqr(p.x) + sqr(p.y); }   PT rot(PT p, double t) {     return PT(p.x * cos(t) - p.y * sin(t), p.x * sin(t) + p.y * cos(t));   }   PT polar(PT p, double r, double t) {     return PT(p.x + r * cos(t), p.y + r * sin(t));   }   double dis2(PT p1, PT p2) { return sqr(p1.x - p2.x) + sqr(p1.y - p2.y); }   double dis(PT p1, PT p2) { return sqrt(dis2(p1, p2)); }   double vect(PT p1, PT p2) { return p1.x * p2.y - p2.x * p1.y; }   double scal(PT p1, PT p2) { return p1.x * p2.x + p1.y * p2.y; }   double getAngle(PT p1, PT p2) { return atan2(p2.y - p1.y, p2.x - p1.x); }   double vect(PT p, PT p1, PT p2) { return vect(p1 - p, p2 - p); }   double scal(PT p, PT p1, PT p2) { return scal(p1 - p, p2 - p); }   double getAngle(PT p, PT p1, PT p2) {     return getAngle(p, p2) - getAngle(p, p1);   }   double getAngle180(PT p, PT p1, PT p2) {     return acos(scal(p, p1, p2) / dis(p, p1) / dis(p, p2));   }   double disToLine(PT p, PT p1, PT p2) {     return fabs(vect(p, p1, p2)) / dis(p1, p2);   }   double disToSeg(PT p, PT p1, PT p2) {     if (scal(p1, p, p2) < 0) return dis(p, p1);     if (scal(p2, p, p1) < 0) return dis(p, p2);     return disToLine(p, p1, p2);   }   bool onLine(PT p, PT p1, PT p2) { return fabs(vect(p1 - p, p2 - p)) < EPS; }   bool onSeg(PT p, PT p1, PT p2) {     if (!onLine(p, p1, p2)) return 0;     return (p1.x - p.x) * (p2.x - p.x) < EPS && (p1.y - p.y) * (p2.y - p.y) < EPS;   }   bool cross(PT p1, PT p2, PT p3, PT p4) {     return vect(p1, p2, p3) * vect(p1, p2, p4) < -EPS &&            vect(p3, p4, p1) * vect(p3, p4, p2) < -EPS;   }   bool common(PT p1, PT p2, PT p3, PT p4) {     if (max(p1.x, p2.x) + EPS < min(p3.x, p4.x) ||         max(p3.x, p4.x) + EPS < min(p1.x, p2.x))       return 0;     if (max(p1.y, p2.y) + EPS < min(p3.y, p4.y) ||         max(p3.y, p4.y) + EPS < min(p1.y, p2.y))       return 0;     return vect(p1, p2, p3) * vect(p1, p2, p4) < EPS &&            vect(p3, p4, p1) * vect(p3, p4, p2) < EPS;   }   PT projection(PT a, PT b, PT p) {     return a + (a - b) * (scal(p - a, a - b) / dis2(a - b));   }   int posLineLine(PT p1, PT p2, PT p3, PT p4, PT &p) {     double s1 = vect(p1, p2, p3), s2 = vect(p1, p2, p4);     if (fabs(s1 - s2) < EPS) {       if (fabs(s1) < EPS) return 2;       return 0;     }     p = p3 + (p4 - p3) * s1 / (s1 - s2);     return 1;   }   int posCirLine(PT p, double r, PT p1, PT p2, PT *q) {     double a, b, c, d2, d;     a = dis2(p1, p2);     b = scal(p1, p, p2);     c = dis2(p, p1) - sqr(r);     d2 = sqr(b) - a * c;     if (d2 < -EPS) return 0;     d = sqrt(fabs(d2));     q[0] = p1 + (p2 - p1) * (b + d) / a;     if (d2 < EPS) return 1;     q[1] = p1 + (p2 - p1) * (b - d) / a;     return 2;   }   int posCirCir(PT p1, double r1, PT p2, double r2, PT *q) {     double dx, dy, d, e, f2, f;     dx = p2.x - p1.x, dy = p2.y - p1.y;     d = dis(p1, p2);     e = (sqr(r1) - sqr(r2) + sqr(d)) / 2 / d;     f2 = sqr(r1) - sqr(e);     if (f2 < -EPS) return 0;     f = sqrt(fabs(f2));     q[0] = PT(p1.x + (e * dx - f * dy) / d, p1.y + (e * dy + f * dx) / d);     if (f2 < EPS) return 1;     q[1] = PT(p1.x + (e * dx + f * dy) / d, p1.y + (e * dy - f * dx) / d);     return 2;   }   double calcArea(PT *p, int n) {     double rlt = 0;     for (int i = 0; i < n; i++) rlt += vect(p[i], p[(i + 1) % n]);     return rlt / 2;   }   int inPolygon(PT p, PT *q, int m) {     PT p1, p2;     int k = 0;     for (int i = 0; i < m; i++) {       p1 = q[i], p2 = q[(i + 1) % m];       if (onSeg(p, p1, p2)) return 2;       if (p1.y > p2.y) swap(p1, p2);       if (p1.y < p.y + EPS && p.y + EPS < p2.y && vect(p, p1, p2) > 0) k++;     }     return k % 2;   }   inline int sgn(double a) { return a > EPS ? 1 : a < -EPS ? -1 : 0; }   bool inCPolygon(PT *p, int n, PT q) {     bool ccw = vect(p[1] - p[0], p[2] - p[1]) > EPS;     int below = sgn(vect(q - p[0], p[n - 1] - p[0]));     if (ccw && below < 0) return 0;     if (!ccw && below > 0) return 0;     int lo = 0, hi = n - 1;     while (hi - lo > 1) {       int mid = (hi + lo) >> 1;       int s1 = sgn(vect(p[mid] - p[lo], q - p[lo]));       int s2 = sgn(vect(q - p[hi], p[mid] - p[hi]));       bool f1 = ccw ? (s1 >= 0) : (s1 <= 0);       bool f2 = ccw ? (s2 >= 0) : (s2 <= 0);       if (f1 && f2) return 1;       if (!f1 && !f2) return 0;       if (f1)         lo = mid;       else         hi = mid;     }     return 0;   }   bool contact_convex(PT *p, int n, PT P, int &L, int &R) {     if (inCPolygon(p, n, P)) return 0;     for (int T = 0; T < 2; T++) {       auto check = [&](int fir, int sec) {         return vect(P, p[sec], p[fir]) > EPS;       };       bool up = false;       double cr = 0;       if (n >= 2) {         cr = vect(P, p[0], p[1]);       }       if (fabs(cr) < EPS && n >= 3) {         cr = vect(P, p[0], p[2]);       }       up = (cr > EPS);       int bd[] = {1, n - 1};       int ans = 0;       while (bd[0] + 6 <= bd[1]) {         int h[2];         for (int hh = 0; hh < 2; hh++) {           h[hh] = (bd[0] + bd[1] + bd[hh]) / 3;         }         if (!check(h[up ^ T], 0) ^ T) {           bd[up ^ T] = h[up ^ T];         } else {           int gr = check(h[0], h[1]);           bd[gr ^ T] = h[gr ^ T];         }       }       for (int i = bd[0]; i <= bd[1]; i++) {         if (check(i, ans) ^ T) ans = i;       }       T ? R = ans : L = ans;     }     return 1;   }   int posLineSegEx(PT p1, PT p2, PT p3, PT p4, PT &p) {     double s1 = vect(p1, p2, p3), s2 = vect(p1, p2, p4);     p = p3 + (p4 - p3) * s1 / (s1 - s2);     if (s1 * s2 > -EPS) return 0;     return 1;   }   int cutPolygon(PT *p, int n, PT p1, PT p2, PT *q) {     int m = 0;     for (int i = 0; i < n; i++) {       if (vect(p[i], p1, p2) > -EPS) q[m++] = p[i];       if (posLineSegEx(p1, p2, p[i], p[(i + 1) % n], q[m])) m++;     }     return m;   }   int cutConvex(PT *p, int n, PT p1, PT p2, PT *q) {     assert(dis(p1, p2) > EPS);     p1 = p1 + (p1 - p2) * 1e8;     int L, R;     assert(contact_convex(p, n, p1, L, R));     if (vect(p[R], p1, p2) > -EPS) {       for (int i = 0; i < n; i++) q[i] = p[i];       return n;     }     if (vect(p[L], p1, p2) < -EPS) return 0;     int st = L, en = R;     if (en < st) en += n;     while (st < en) {       int md = (st + en) / 2;       if (vect(p[md % n], p1, p2) > EPS)         st = md + 1;       else         en = md;     }     int T = st % n;     st = R, en = L;     if (en < st) en += n;     while (st < en) {       int md = (st + en) / 2;       if (vect(p[md % n], p1, p2) < EPS)         st = md + 1;       else         en = md;     }     int F = st % n;     int qn = 0;     for (int i = F; i != T; i = (i + 1) % n) q[qn++] = p[i];     posLineSegEx(p1, p2, p[(T - 1 + n) % n], p[T], q[qn]), qn++;     posLineSegEx(p1, p2, p[(F - 1 + n) % n], p[F], q[qn]);     if (dis(q[qn], q[qn - 1]) > EPS) qn++;     return qn;   }   double disToPolygon(PT p, PT *q, int m) {     if (inPolygon(p, q, m)) return 0;     double rlt = INF;     for (int i = 0; i < m; i++) rlt = min(rlt, disToSeg(p, q[i], q[(i + 1) % m]));     return rlt;   }   double dis(PT *p, int n, PT *q, int m) {     double rlt;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (common(p[i], p[(i + 1) % n], q[j], q[(j + 1) % m])) return 0;       }     }     if (inPolygon(p[0], q, m) || inPolygon(q[0], p, n)) return 0;     rlt = INF;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         rlt = min(rlt, disToSeg(p[i], q[j], q[(j + 1) % m]));         rlt = min(rlt, disToSeg(q[j], p[i], p[(i + 1) % n]));       }     }     return rlt;   }   int convex_hull(PT *p, int n, PT *q) {     sort(p, p + n);     n = unique(p, p + n) - p;     if (n == 1) {       q[0] = p[0];       return 1;     }     int m = 0;     for (int i = 0; i < n; i++) {       while (m > 1 && vect(q[m - 2], q[m - 1], p[i]) < EPS) m--;       q[m++] = p[i];     }     int k = m;     for (int i = n - 2; i >= 0; i--) {       while (m > k && vect(q[m - 2], q[m - 1], p[i]) < EPS) m--;       q[m++] = p[i];     }     return --m;   }   PT innercenter(PT a, PT b, PT c) {     double A = dis(b - c);     double B = dis(c - a);     double C = dis(a - b);     return PT((A * a.x + B * b.x + C * c.x) / (A + B + C),               (A * a.y + B * b.y + C * c.y) / (A + B + C));   }   PT outercenter(PT a, PT b, PT c) {     double c1 = (scal(a, a) - scal(b, b)) / 2, c2 = (scal(a, a) - scal(c, c)) / 2;     double x0 = (c1 * (a.y - c.y) - c2 * (a.y - b.y)) / vect(a - b, a - c);     double y0 = (c1 * (a.x - c.x) - c2 * (a.x - b.x)) / vect(a - c, a - b);     return PT(x0, y0);   }   const int N = 1100000;   double v[N];   PT P[N], Q[N], R[] = {PT(0, 0), PT(1e5, 0), PT(1e5, 1e5), PT(0, 1e5)}, T[22],                  S[22];   int dx[] = {0, 1, 0, -1};   int dy[] = {-1, 0, 1, 0};   void print(PT pt) { printf( %d %d n , (int)round(pt.x), (int)round(pt.y)); }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) P[i].in(), scanf( %lf , &v[i]);     int qn = 0;     for (int i = 0; i < n; i++) {       if (v[i] < EPS) {         Q[qn++] = P[i];         continue;       }       int m = 4;       for (int j = 0; j < m; j++) T[j] = P[i] + PT(v[i] * dx[j], v[i] * dy[j]);       for (int j = 0; j < 4; j++) {         m = cutPolygon(T, m, R[j], R[(j + 1) % 4], S);         for (int k = 0; k < m; k++) T[k] = S[k];       }       for (int j = 0; j < m; j++) Q[qn++] = T[j];     }     qn = convex_hull(Q, qn, P);     P[qn] = P[0], P[qn + 1] = P[1];     double ans = 0;     int cur;     for (int i = 0; i < qn; i++) {       double ang = getAngle180(P[i], P[i + 1], P[i + 2]);       double r = dis(P[i + 1], P[i + 2]) / sin(ang);       if (ans < r) {         ans = r;         cur = i;       }     }     for (int i = cur; i < cur + 3; i++) print(P[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100001;   const long long mod = 1e9 + 7;   int main() {     int N, M;     scanf( %d %d , &N, &M);     int arr[M][2], nums[N];     for (int i = 0; i < N; i++) {       scanf( %d , &nums[i]);     }     for (int i = 0; i < M; i++) {       scanf( %d %d , &arr[i][0], &arr[i][1]);       arr[i][0]--;       arr[i][1]--;     }     int a = 0, b = 0;     int res, ans = 0;     for (int i = 0; i < N; i++) {       for (int j = 0; j < N; j++) {         res = 0;         for (int k = 0; k < M; k++) {           if (arr[k][0] <= i && arr[k][1] >= i &&               !(arr[k][0] <= j && arr[k][1] >= j)) {             res++;           }         }         if (res + nums[j] - nums[i] > ans) {           a = i;           b = j;         }         ans = max(ans, res + nums[j] - nums[i]);       }     }     printf( %d n , ans);     vector<int> ans2;     for (int k = 0; k < M; k++) {       if (arr[k][0] <= a && arr[k][1] >= a &&           !(arr[k][0] <= b && arr[k][1] >= b)) {         ans2.push_back(k + 1);       }     }     printf( %d n , ans2.size());     for (int i : ans2) {       printf( %d  , i);     }   }


#include <bits/stdc++.h>   using namespace std;   set<int> s;   map<long long, int> m1, m2;   int main() {     int n, n2, k;     cin >> n >> n2 >> k;     for (int i = 0; i < n; i++) {       int q;       cin >> q;       m1[q]++;       s.insert(q);     }     for (int i = 0; i < n2; i++) {       int q;       cin >> q;       m2[q]++;       s.insert(q);     }     vector<int> difs;     while (!s.empty()) {       int q = *s.begin();       difs.push_back(m1[q] - m2[q]);       s.erase(s.begin());     }     int sum = 0;     for (int i = difs.size() - 1; i >= 0; i--) {       sum += difs[i];       if (sum > 0) return 0 * bool(cout <<  YES  << endl);     }     cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 2e5 + 10;   long long n, T, fac[MAXN], a[MAXN], inv[MAXN], b[MAXN], ans, mod = 1e9 + 7;   long long read() {     long long sss = 0, fff = 1;     char ccc;     ccc = getchar();     while (ccc <  0  || ccc >  9 ) {       if (ccc ==  - ) fff = -1;       ccc = getchar();     }     while (ccc >=  0  && ccc <=  9 )       sss = (sss << 1) + (sss << 3) + (ccc ^  0 ), ccc = getchar();     return sss * fff;   }   long long ksm(long long base, long long k) {     long long sum = 1;     while (k) {       if (k & 1) sum = sum * base % mod;       base = base * base % mod;       k >>= 1;     }     return sum;   }   long long C(long long n, long long m) {     if (n < m) return 0;     return fac[n] * inv[n - m] % mod * inv[m] % mod;   }   int main() {     n = read();     T = read();     for (long long i = 1; i <= n; i++) a[i] = read();     b[n] = ksm(2, n);     b[n] = ksm(b[n], mod - 2);     for (long long i = n - 1; i >= 0; i--) b[i] = b[i + 1] * 2 % mod;     fac[0] = 1;     for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod;     inv[n] = ksm(fac[n], mod - 2);     for (int i = n - 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod;     long long now = 0;     bool flag = true;     long long TT, sum = 0, CCC;     while (now <= n && T >= a[now + 1]) {       now++, T -= a[now];       if (now <= T)         ans = (ans + 1) % mod;       else {         if (flag) {           TT = T;           CCC = now;           for (long long i = 0; i <= TT; i++) sum = (sum + C(now, i)) % mod;         } else           sum = (sum * 2 % mod - C(now - 1, TT) + mod) % mod;         flag = false;         while (TT > T) {           sum = (sum - C(now, TT) + mod) % mod;           TT--;         }         ans = (ans + sum * b[now] % mod) % mod;       }     }     printf( %lld n , ans);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n, k;     cin >> n >> k;     string s;     cin >> s;     string ss = s;     int iter = 0;     int total = 0;     while (true) {       int cnt = 0;       for (int i = 1; i < n; i++)         if (s[i - 1] ==  R  && s[i] ==  L ) {           s[i - 1] =  L ;           s[i] =  R ;           cnt++;           i++;         }       if (cnt == 0) break;       iter++;       total += cnt;     }     if (k < iter || k > total) {       cout << -1 << endl;       return 0;     }     s = ss;     int slow = k - iter;     while (true) {       int cnt = 0;       vector<int> res;       for (int i = 1; i < n; i++)         if (s[i - 1] ==  R  && s[i] ==  L ) {           s[i - 1] =  L ;           s[i] =  R ;           cnt++;           if (slow > 0) {             slow--;             cout <<  1   << i <<   n ;           } else {             res.push_back(i);           }           i++;         }       if (cnt == 0) break;       if (res.size() > 0) {         cout << res.size();         for (int i = 0; i < res.size(); i++) cout <<     << res[i];         cout <<   n ;       } else {         slow++;       }     }     cout.flush();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N;     cin >> N;     int v[N];     for (int i = 0; i < N; i++) {       cin >> v[i];     }     vector<int> quadr;     for (int i = 0; 1; i++) {       if (i > sqrt(INT_MAX)) {         break;       }       quadr.push_back(i * i);     }     int dist[N];     vector<int> minDiffs;     int contQuadr = 0;     for (int i = 0; i < N; i++) {       int curr = v[i];       int index = upper_bound(quadr.begin(), quadr.end(), curr) - quadr.begin();       int l = abs(curr - quadr[index]);       int r = l;       if (index - 1 >= 0) r = abs(curr - quadr[index - 1]);       int minDiff = min(l, r);       if (minDiff == 0) contQuadr++;       minDiffs.push_back(minDiff);     }     if (contQuadr == N / 2) {       cout << 0;       return 0;     }     if (contQuadr < N / 2) {       int diff = N / 2 - contQuadr;       sort(minDiffs.begin(), minDiffs.end());       long long int res = 0;       int i = 0;       int cont = 0;       while (diff > 0 && i < N) {         if (minDiffs[i] == 0) {           i++;           continue;         }         res += minDiffs[i];         i++;         diff--;       }       cout << res;       return 0;     }     if (contQuadr > N / 2) {       long long int diff = contQuadr - N / 2;       long long int res = 0;       int costs[N];       memset(costs, 0, N * sizeof(int));       for (int i = 0; i < N; i++) {         if (v[i] == 0) {           costs[i] = 2;           continue;         }         if (minDiffs[i] == 0) {           costs[i] = 1;         }       }       sort(costs, costs + N);       for (int i = 0; i < N && diff > 0; i++) {         if (costs[i] == 0) continue;         res += costs[i];         diff--;       }       cout << res;       return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 5;   char a[N], b[N];   int main() {     int n, k;     cin >> n >> k;     scanf( %s %s , a + 1, b + 1);     long long ans = 0, t = 1;     for (int i = 1; i <= n; ++i) {       t = t * 2;       if (b[i] ==  a ) t--;       if (a[i] ==  b ) t--;       if (t > k) t = k + 1;       ans += (t > k ? k : t);     }     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   class FenwickTree {    private:     vector<T> bit;     int M;       public:     FenwickTree(int M) : bit(vector<T>(M + 1, 0)), M(M) {}     int sum(int i) {       if (!i) return 0;       return bit[i] + sum(i - (i & -i));     }     void add(int i, T x) {       if (i > M) return;       bit[i] += x;       add(i + (i & -i), x);     }   };   int main() {     ios::sync_with_stdio(false);     int N;     cin >> N;     vector<int> X1(N), X2(N), Y1(N), Y2(N);     map<int, int> Yc;     set<int> Ys;     vector<int> YI;     map<int, vector<int>> Ha, He;     map<int, vector<pair<int, int>>> V, H;     set<int> cand;     YI.push_back(-1 << 30);     for (int i = 0; i < N; ++i) {       int x1, x2, y1, y2;       cin >> x1 >> y1 >> x2 >> y2;       if (x2 < x1) swap(x1, x2);       if (y2 < y1) swap(y1, y2);       YI.push_back(y1 - 1);       YI.push_back(y1);       YI.push_back(y1 + 1);       YI.push_back(y2 - 1);       YI.push_back(y2);       YI.push_back(y2 + 1);       X1[i] = x1, X2[i] = x2, Y1[i] = y1, Y2[i] = y2;     }     sort((YI.begin()), (YI.end()));     YI.erase(unique(YI.begin(), YI.end()), YI.end());     for (int i = 0; i < N; ++i) {       Y1[i] = lower_bound((YI.begin()), (YI.end()), Y1[i]) - YI.begin();       Y2[i] = lower_bound((YI.begin()), (YI.end()), Y2[i]) - YI.begin();       if (X1[i] == X2[i]) {         V[X1[i]].push_back({Y1[i], Y2[i]});         cand.insert(X1[i]);       } else {         Ha[X1[i]].push_back(Y1[i]);         He[X2[i]].push_back(Y1[i]);         H[Y1[i]].push_back({X1[i], X2[i]});         cand.insert(X1[i]);         cand.insert(X2[i]);       }     }     FenwickTree<signed long long> bt(YI.size());     signed long long ret = 0;     for (auto cx : cand) {       for (auto y : Ha[cx])         if (Yc[y]++ == 0) bt.add(y, 1);       if (V.count(cx)) {         vector<pair<int, int>>& T = V[cx];         sort((T.begin()), (T.end()));         int ma = -1 << 30;         for (auto r : T) {           r.first = max(r.first, ma + 1);           if (r.first > r.second) continue;           ret += YI[r.second] - YI[r.first] + 1;           ret -= bt.sum(r.second) - bt.sum(r.first - 1);           ma = r.second;         }       }       for (auto y : He[cx])         if (--Yc[y] == 0) bt.add(y, -1);     }     for (auto hy : H) {       vector<pair<int, int>>& T = hy.second;       sort((T.begin()), (T.end()));       int ma = -1 << 30;       for (auto r : T) {         r.first = max(r.first, ma + 1);         if (r.first > r.second) continue;         ret += r.second - r.first + 1;         ma = r.second;       }     }     cout << ret << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 777;   vector<int> G[maxn];   map<int, int> savesiz[maxn];   int siz[maxn];   vector<int> ans;   int n;   void dfs(int s, int fa) {     siz[s] = 1;     int cnt = 0;     for (int d : G[s]) {       if (d == fa) continue;       dfs(d, s);       savesiz[s][siz[d]]++;       cnt++;       siz[s] += siz[d];     }     if (n - siz[s] > 0) {       savesiz[s][n - siz[s]]++;       cnt++;     }     if (savesiz[s].size() == 1 && cnt >= 2) ans.push_back(s);   }   vector<int> aans;   map<int, int> have[maxn];   void func(int s) {     int nowsiz = 1;     int nowid = G[s][0];     int fa = s;     while (G[nowid].size() == 2) {       nowsiz++;       if (G[nowid][0] == fa) {         fa = nowid;         nowid = G[nowid][1];       } else {         fa = nowid;         nowid = G[nowid][0];       }     }     if (have[nowid].count(nowsiz)) return;     if (have[nowid].size() >= 2) return;     have[nowid][nowsiz] = 1;     if ((savesiz[nowid][nowsiz] == 1 && savesiz[nowid].size() == 2) ||         savesiz[nowid].size() == 1) {       aans.push_back(s);     }   }   set<int> se[maxn];   int vis[maxn];   bool ck(int s) {     for (int i = 0; i <= n; i++) {       se[i].clear();       vis[i] = 0;     }     queue<pair<int, int> > q;     vis[s] = 1;     q.push(make_pair(s, 0));     while (!q.empty()) {       int nowid = q.front().first;       int nowdis = q.front().second;       q.pop();       se[nowdis].insert(G[nowid].size());       if (se[nowdis].size() >= 2) return false;       for (int d : G[nowid]) {         if (vis[d]) continue;         vis[d] = 1;         q.push(make_pair(d, nowdis + 1));       }     }     return true;   }   void solve() {     scanf( %d , &n);     if (n == 1) {       cout << 1 << endl;       return;     }     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d %d , &a, &b);       G[a].push_back(b);       G[b].push_back(a);     }     dfs(1, 1);     for (int iid : ans) {       if (ck(iid)) {         cout << iid << endl;         return;       }     }     for (int i = 1; i <= n; i++) {       if (G[i].size() == 1) {         func(i);       }     }     for (int iid : aans) {       if (ck(iid)) {         cout << iid << endl;         return;       }     }     cout << -1 << endl;   }   signed main() { solve(); }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   const double eps = 1e-9;   const double pi = acos(-1.0);   const int maxn = (int)1e4 + 10;   int a[maxn], b[maxn];   int main() {     int n, c;     cin >> n >> c;     if (c < n) {       cout << 0 << endl;       return 0;     }     c -= n;     for (int i = 0; i < n; i++) {       cin >> a[i] >> b[i];     }     int m = 0;     for (int i = 0; i < n; i++)       if (a[i] != 0) {         a[m] = a[i];         b[m] = b[i];         m++;       }     if (m == 0) {       if (c == 0) {         cout << -1 << endl;       } else {         cout << 0 << endl;       }       return 0;     }     long long least = 0;     for (int i = 0; i < m; i++) {       least += a[i] / b[i];     }     if (least > c) {       cout << 0 << endl;       return 0;     }     long long lf = 0;     long long rg = (long long)1e18;     while (lf < rg - 1) {       long long mm = (lf + rg) >> 1;       double sum = 0.0;       for (int i = 0; i < m; i++) {         sum += floor(a[i] * 1.0 / b[i] * mm + eps);         if (sum >= c) break;       }       if (sum >= c) {         rg = mm;       } else {         lf = mm;       }     }     long long sum = 0;     for (int i = 0; i < m; i++) {       sum += (long long)(a[i] * 1.0 / b[i] * rg + eps);     }     if (sum > c) {       cout << 0 << endl;       return 0;     }     int res = (int)1e9 + 10;     for (int i = 0; i < m; i++) {       int ost = a[i] % b[i] * 1LL * (rg % b[i]) % b[i];       res = min(res, 1 + (b[i] - ost) / a[i] - ((b[i] - ost) % a[i] == 0));     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)15e4 + 10, M = 310;   int n, m, d;   long long a[N], b[N], t[N], tmp[N], dp[2][N];   inline void f(int j) {     long long k = 1ll * (t[j] - t[j - 1]) * d;     int r = j % 2;     deque<int> deq;     for (int i = 0; i < n; i++) {       while (deq.size() && dp[!r][deq.back()] <= dp[!r][i]) deq.pop_back();       deq.push_back(i);       while (deq.front() < i - k) deq.pop_front();       tmp[i] = dp[!r][deq.front()];     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m >> d;     for (int i = 0; i < m; i++) {       cin >> a[i] >> b[i] >> t[i];       a[i]--;     }     for (int i = 0; i < n; i++) dp[0][i] = b[0] - abs(a[0] - i);     for (int j = 1; j < m; j++) {       int r = j % 2;       f(j);       for (int i = 0; i < n; i++) dp[r][i] = tmp[i];       reverse(dp[!r], dp[!r] + n);       f(j);       for (int i = 0; i < n; i++) dp[r][i] = max(dp[r][i], tmp[n - 1 - i]);       for (int i = 0; i < n; i++) dp[r][i] += b[j] - abs(a[j] - i);     }     m--;     cout << *max_element(dp[m % 2], dp[m % 2] + n);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast ,  unroll-loops ,  omit-frame-pointer ,  inline )   #pragma GCC optimize(3)   using namespace std;   const long long INF = 1e17;   const long long maxn = 1e5 + 700;   const int mod = 998244353;   const int up = 1e9;   template <typename T>   inline void read(T &a) {     char c = getchar();     T x = 0, f = 1;     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + c -  0 ;       c = getchar();     }     a = f * x;   }   long long n, m, p;   int num[maxn];   struct node {     int tag, w, l, r;   } t[maxn * 40];   int root[maxn * 40];   int cnt = 0;   void Modify(int &now, int pre, int l, int r, int x, int y, int w) {     t[now = ++cnt] = t[pre];     if (x <= l && y >= r) {       t[now].tag += w;       t[now].w += w;       return;     }     int mid = (l + r) / 2;     if (x <= mid) Modify(t[now].l, t[pre].l, l, mid, x, y, w);     if (y > mid) Modify(t[now].r, t[pre].r, mid + 1, r, x, y, w);     int ls = t[now].l, rs = t[now].r;     t[now].w = min(t[ls].w, t[rs].w) + t[now].tag;   }   int Query(int now, int l, int r, int id, int tag, int k) {     if (l == r) return l;     int mid = (l + r) / 2;     if (mid < id) return Query(t[now].r, mid + 1, r, id, tag + t[now].tag, k);     int ls = t[now].l, rs = t[now].r;     return t[rs].w + tag + t[now].tag > k                ? Query(ls, l, mid, id, tag + t[now].tag, k)                : Query(rs, mid + 1, r, id, tag + t[now].tag, k);   }   int work(int x) {     int s = 1, ans = 0;     while (s <= n) {       s = Query(root[s], 1, n, s, 0, x) + 1;       ans++;     }     return ans;   }   int last[maxn];   int main() {     read(n);     for (int i = 1; i <= n; i++) read(num[i]);     for (int i = n; i >= 1; i--) {       int up = last[num[i]] ? last[num[i]] - 1 : n;       Modify(root[i], root[i + 1], 1, n, i, up, 1);       last[num[i]] = i;     }     for (int i = 1; i <= n; i++) printf( %d  , work(i));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000000], ans[1000000];   int n, k, p, x, y, sum = 0;   int main() {     cin >> n >> k >> p >> x >> y;     for (int i = 1; i <= k; i++) {       cin >> a[i];       sum += a[i];     }     if (sum + n - k > x) {       cout << -1;       return 0;     }     for (int i = k + 1; i <= n; i++) {       a[i] = y;       sum += y;     }     int id = k + 1;     while (sum > x) {       if (a[id] == 1) {         id++;         continue;       }       a[id]--;       sum--;     }     int c = 0;     for (int i = k + 1; i <= n; i++) ans[c++] = a[i];     sort(a + 1, a + n + 1);     if (a[(n + 1) / 2] < y)       cout << -1;     else {       for (int i = 0; i < c; i++) cout << ans[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   inline bool check(int x) {     long long t = (long long)x * (long long)(x - 1) / 2;     if (x & 1)       t++;     else       t += x / 2;     return t <= N;   }   int main() {     int M;     scanf( %d%d , &N, &M);     vector<int> A;     for (int i = 0; i < M; i++) {       int x;       scanf( %*d%d , &x);       A.push_back(x);     }     sort(A.begin(), A.end(), greater<int>());     int lb = 1, rb = M;     while (lb < rb) {       int mid = (lb + rb + 1) >> 1;       if (check(mid))         lb = mid;       else         rb = mid - 1;     }     long long ans = 0;     for (int i = 0; i < lb; i++) ans += A[i];     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x;     cin >> x;     string names[x * 2];     for (int i = 0; i < x * 2; i++) cin >> names[i];     for (int i = 0; i < x * 2; i++)       if (names[i] ==  rat ) {         cout << names[i - 1];         cout << endl;       }     for (int i = 0; i < x * 2; i++)       if (names[i] ==  woman  || names[i] ==  child ) {         cout << names[i - 1];         cout << endl;       }     for (int i = 0; i < x * 2; i++)       if (names[i] ==  man ) {         cout << names[i - 1];         cout << endl;       }     for (int i = 0; i < x * 2; i++)       if (names[i] ==  captain ) {         cout << names[i - 1];         cout << endl;       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   struct EDGE {     int u, v, w;   } p[100000 + 5];   struct Edge {     int v, w, next;   } edge[100000 + 5];   int head[100000 + 5], cnt = 0;   void init() {     cnt = 0;     memset(head, -1, sizeof head);   }   void add_edge(int u, int v, int w) {     edge[cnt].v = v;     edge[cnt].w = w;     edge[cnt].next = head[u];     head[u] = cnt++;   }   int vis[100000 + 5];   int rd[100000 + 5];   bool check(int up) {     memset(rd, 0, sizeof rd);     memset(vis, 0, sizeof vis);     queue<int> que;     for (int i = 1; i <= m; i++)       if (p[i].w > up) rd[p[i].v]++;     int tot = 0;     while (1) {       for (int u = 1; u <= n; u++)         if (!vis[u] && rd[u] == 0) que.push(u);       if (que.empty()) break;       while (!que.empty()) {         int u = que.front();         que.pop();         vis[u] = ++tot;         for (int i = head[u]; i != -1; i = edge[i].next)           if (!vis[edge[i].v] && edge[i].w > up) rd[edge[i].v]--;       }     }     for (int u = 1; u <= n; u++)       if (!vis[u]) return 0;     return 1;   }   int main() {     if (1 && fopen( in.txt ,  r )) {       freopen( in.txt ,  r , stdin);       freopen( out.txt ,  w , stdout);     }     init();     scanf( %d%d , &n, &m);     int l = 0, r = 0, mid;     for (int i = 1; i <= m; i++) {       scanf( %d%d%d , &p[i].u, &p[i].v, &p[i].w);       add_edge(p[i].u, p[i].v, p[i].w);       r = max(r, p[i].w);     }     while (l < r) {       mid = (l + r) >> 1;       if (check(mid))         r = mid;       else         l = mid + 1;     }     int ans = l;     vector<int> ansque;     check(ans);     for (int i = 1; i <= m; i++) {       int u = p[i].u, v = p[i].v;       if (p[i].w <= ans && vis[u] > vis[v]) ansque.push_back(i);     }     sort(ansque.begin(), ansque.end());     printf( %d %d n , ans, ansque.size());     for (int i = 0; i < ansque.size(); i++)       printf( %d%c , ansque[i], i == ansque.size() - 1 ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, a[150];     scanf( %d , &n);     int sum = 0;     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);       sum += a[i];     }     int ans = 0;     for (i = 0; i < n; i++) (sum % 2) ^ ((a[i] + 1) % 2) ? ans++ : ans = ans;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     int mini = INT_MAX;     cin >> a >> b;     int az = a.size();     for (int i = -2000; i <= az; i++) {       int ans = b.size();       for (int j = 0; j < b.size(); j++) {         ans -= (i + j >= 0 && i + j < a.size() && a[i + j] == b[j]);       }       mini = min(mini, ans);     }     cout << mini;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef struct line {     int k;     int b;     long double x;   } line;   bool com(line l1, line l2) {     if (l1.x < l2.x)       return true;     else       return false;   }   int main() {     long long k, b, n, t;     cin >> k >> b >> n >> t;     long long x = 1;     int t1 = 0;     while (x < t) {       x = k * x + b;       t1++;     }     if (x != t) t1--;     if (t1 < n) {       cout << n - t1;     } else       cout << 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 10;   struct Edge {     int to, nxt;   } e[N << 1];   int h[N], idx;   void Ins(int a, int b) {     e[++idx].to = b;     e[idx].nxt = h[a];     h[a] = idx;   }   int bas, f[N], g[N], to[N], n, siz[N], Mx[N], mk[N], sec[N];   void dfs(int u, int fa) {     siz[u] = 1;     for (int i = h[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (v == fa) continue;       dfs(v, u);       siz[u] += siz[v];       if (Mx[u] < siz[v]) Mx[u] = siz[v], to[u] = v;       if (siz[v] <= bas) {         if (g[u] < siz[v]) {           sec[u] = g[u];           g[u] = siz[v];           mk[u] = v;         } else if (sec[u] < siz[v]) {           sec[u] = siz[v];         }       } else {         if (g[v] > g[u]) {           sec[u] = g[u];           g[u] = g[v];           mk[u] = v;         } else if (g[v] > sec[u])           sec[u] = g[v];       }     }     if (Mx[u] < n - siz[u]) {       Mx[u] = n - siz[u];       to[u] = fa;     }   }   void dfs2(int u, int fa) {     if (Mx[u] <= bas)       f[u] = 1;     else if (to[u] != fa)       f[u] = (Mx[u] - g[to[u]]) <= bas;     else {       if (mk[fa] == u) {         f[u] = (Mx[u] - sec[fa]) <= bas;       } else         f[u] = (Mx[u] - g[fa]) <= bas;     }     if (mk[fa] == u) {       if (sec[fa] > g[u]) {         sec[u] = g[u];         g[u] = sec[fa];         mk[u] = fa;       } else if (sec[fa] > sec[u]) {         sec[u] = sec[fa];       }       int w = n - siz[u];       if (w <= bas) {         if (w > g[u]) {           sec[u] = g[u];           g[u] = w;           mk[u] = fa;         } else if (w > sec[u]) {           sec[u] = w;         }       }     } else {       if (g[fa] > g[u]) {         sec[u] = g[u];         g[u] = g[fa];         mk[u] = fa;       } else if (g[fa] > sec[u])         sec[u] = g[fa];       int w = n - siz[u];       if (w <= bas) {         if (w > g[u]) {           sec[u] = g[u];           g[u] = w;           mk[u] = fa;         } else if (w > sec[u]) {           sec[u] = w;         }       }     }     for (int i = h[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (v == fa) continue;       dfs2(v, u);     }   }   int main() {     scanf( %d , &n);     bas = n >> 1;     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d%d , &a, &b);       Ins(a, b);       Ins(b, a);     }     dfs(1, 0);     dfs2(1, 0);     for (int i = 1; i <= n; i++) printf( %d  , f[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   int main() {     int n;     cin >> n >> ws;     char s[1001];     scanf( %s , &s);     int h = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  H ) h++;     }     int _min = INF;     for (int i = 0; i < n; i++) {       int ans = 0;       for (int j = 0; j < h; j++) {         if (s[(i + j) % n] ==  T ) ans++;       }       _min = min(ans, _min);     }     cout << _min;     return 0;   }


#include <bits/stdc++.h>   using ll = long long;   using ld = long double;   using ull = unsigned long long;   using namespace std;   const int N = 10;   int T, n, a[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> T;     while (T--) {       cin >> n;       for (int i = (0); i <= (n - 1); ++i) cin >> a[i];       sort(a, a + n);       bool ok = 0;       for (int i = (0); i <= (n - 1); ++i) {         if (a[i] == 0 || (i > 0 && a[i] == a[i - 1])) ok = 1;       }       for (int i = (0); i <= (n - 1); ++i) {         for (int j = (0); j <= (n - 1); ++j)           if (a[j] == -a[i]) {             ok = 1;             break;           }         for (int x = (0); x <= ((1 << n) - 1); ++x)           if (!(x >> i & 1)) {             for (int mask = x;; mask = (mask - 1) & x) {               int sum = 0;               for (int j = (0); j <= (n - 1); ++j)                 if (x >> j & 1) {                   if (mask >> j & 1)                     sum += a[j];                   else                     sum -= a[j];                 }               if (sum == a[i]) {                 ok = 1;                 break;               }               if (mask == 0) break;             }           }       }       cout << (ok ?  YES n  :  NO n );     }     return 0;   }