#include <bits/stdc++.h>   using namespace std;   int n, m, nex[2000000], hea[2000000], wen[2000000], val[2000000], aid[2000000],       root2, root1, len, maxx;   struct segment_tree {     int a[2000000][2], lazy[2000000], fa[1000000], dep[1000000], in[1000000],         out[1000000], m;     void pushdown(int k) {       if (!lazy[k]) return;       swap(a[k << 1][0], a[k << 1][1]);       swap(a[(k << 1) | 1][0], a[(k << 1) | 1][1]);       lazy[k << 1] ^= 1;       lazy[(k << 1) | 1] ^= 1;       lazy[k] = 0;     }     void update1(int l, int r, int k, int x, int y, int z) {       if (l == r) {         a[k][z] = y;         return;       }       int mid = (l + r) >> 1;       if (x <= mid) update1(l, mid, k << 1, x, y, z);       if (x > mid) update1(mid + 1, r, (k << 1) | 1, x, y, z);       a[k][0] = max(a[k << 1][0], a[(k << 1) | 1][0]);       a[k][1] = max(a[k << 1][1], a[(k << 1) | 1][1]);     }     void update2(int l, int r, int k, int x, int y) {       if (l >= x && r <= y) {         lazy[k] ^= 1;         swap(a[k][0], a[k][1]);         return;       }       pushdown(k);       int mid = (l + r) >> 1;       if (x <= mid) update2(l, mid, k << 1, x, y);       if (y > mid) update2(mid + 1, r, (k << 1) | 1, x, y);       a[k][0] = max(a[k << 1][0], a[(k << 1) | 1][0]);       a[k][1] = max(a[k << 1][1], a[(k << 1) | 1][1]);     }     void build(int x, int y, int z) {       int a = 0;       in[x] = ++m;       dep[x] = dep[y] + 1;       for (int i = hea[x]; i; i = nex[i])         if (wen[i] != y) {           ++a;           fa[aid[i]] = wen[i];           build(wen[i], x, z ^ val[i]);         }       update1(1, n, 1, in[x], dep[x], z);       out[x] = m;     }     void revers(int x) { update2(1, n, 1, in[fa[x]], out[fa[x]]); }   } st1, st2;   void add(int x, int y, int z, int p) {     ++len;     nex[len] = hea[x];     wen[len] = y;     val[len] = z;     aid[len] = p;     hea[x] = len;   }   void dfs(int x, int y, int z) {     if (z >= maxx) maxx = z, root1 = x;     for (int i = hea[x]; i; i = nex[i])       if (wen[i] != y) dfs(wen[i], x, z + 1);   }   void dfs1(int x, int y, int z) {     if (z >= maxx) maxx = z, root2 = x;     for (int i = hea[x]; i; i = nex[i])       if (wen[i] != y) dfs1(wen[i], x, z + 1);   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int x, y, z;       scanf( %d%d%d , &x, &y, &z);       add(x, y, z, i);       add(y, x, z, i);     }     dfs(1, 0, 0);     dfs1(root1, 0, 0);     st1.build(root1, 0, 0);     st2.build(root2, 0, 0);     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       int x;       scanf( %d , &x);       st1.revers(x);       st2.revers(x);       printf(  %d n , max(st1.a[1][0], st2.a[1][0]) - 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int a = 0, b = 1023, c, n;     char op;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> op >> c;       switch (op) {         case  | :           a |= c;           b |= c;           break;         case  & :           a &= c;           b &= c;           break;         case  ^ :           a ^= c;           b ^= c;           break;       }     }     int r = 0, nd = 1023, xr = 0;     for (int i = 0; i < 10; ++i) {       int p = (a >> i) & 1;       int q = (b >> i) & 1;       if (!p && !q) {         nd ^= (1 << i);       } else if (p && !q) {         xr |= (1 << i);       } else if (p && q) {         r |= (1 << i);       }     }     int s = 3;     if (nd == 1023) s--;     if (xr == 0) s--;     if (r == 0) s--;     cout << s << endl;     if (nd != 1023) cout <<  &   << nd << endl;     if (xr != 0) cout <<  ^   << xr << endl;     if (r != 0) cout <<  |   << r << endl;     return 0;   }


#include <bits/stdc++.h>   int main() {     std::ios_base::sync_with_stdio(false);     std::string s;     std::cin >> s;     int n = s.length();     std::vector<int> prefix_fun(n, 0);     for (int i = 1; i < n; i++) {       int j = prefix_fun[i - 1];       while (j > 0 && s[i] != s[j]) {         j = prefix_fun[j - 1];       }       if (s[i] == s[j]) {         j++;       }       prefix_fun[i] = j;     }     if (prefix_fun[n - 1] == 0) {       std::cout <<  Just a legend ;       return 0;     }     for (int i = 1; i < n - 1; i++) {       if (prefix_fun[i] == prefix_fun[n - 1]) {         std::cout << s.substr(0, prefix_fun[i]);         return 0;       }     }     int j = prefix_fun[prefix_fun[n - 1] - 1];     if (j == 0) {       std::cout <<  Just a legend ;       return 0;     }     std::cout << s.substr(0, j);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 7, M = 1e6 + 7;   int row[N], col[N];   long long dpc[M], dpr[M];   int main() {     priority_queue<int> pqc, pqr;     int n, m, k, p;     scanf( %d %d %d %d , &n, &m, &k, &p);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int a;         scanf( %d , &a);         row[i] += a;         col[j] += a;         if (i == n - 1) {           pqc.push(col[j]);         }       }       pqr.push(row[i]);     }     for (int i = 1; i <= k; i++) {       dpr[i] = pqr.top() + dpr[i - 1];       pqr.push(pqr.top() - p * m);       pqr.pop();       dpc[i] = pqc.top() + dpc[i - 1];       pqc.push(pqc.top() - p * n);       pqc.pop();     }     long long ans = -1e18;     for (int i = 0; i <= k; i++) {       ans = max(ans, 1ll * dpr[i] + dpc[k - i] - (1ll * i * (k - i) * p));     }     printf( %lld , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int n, x;       cin >> n >> x;       long long int arr[n];       for (int i = 0; i < n; i++) cin >> arr[i];       sort(arr, arr + n);       long double rich_sum = 0;       long long int poor_idx = -1;       for (int i = 0; i < n; i++) {         if (arr[i] < x) {           poor_idx = i;         } else           rich_sum += arr[i];       }       int i;       for (i = poor_idx; i >= 0; i--) {         rich_sum += arr[i];         if (rich_sum / (n - i) < x) break;       }       int poor_cnt = i + 1;       cout << (n - poor_cnt) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   const int INF = (int)2e9 + 10;   typedef struct vocationn {     int min_cost[N];     vector<pair<int, int> > left[N];     vector<pair<int, int> > right[N];   } vocation;   int n, x, cost, l, r, ans = INF;   vocation voc;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     for (int i = 0; i < N; i++) voc.min_cost[i] = INF;     cin >> n >> x;     for (size_t i = 0; i < n; i++) {       cin >> l >> r >> cost;       voc.left[l].push_back(pair<int, int>(r - l + 1, cost));       voc.right[r].push_back(pair<int, int>(r - l + 1, cost));     }     for (int i = 0; i < N; i++) {       for (pair<int, int> t : voc.left[i]) {         if (t.first >= x) continue;         if (voc.min_cost[x - t.first] < INF)           ans = min(ans, voc.min_cost[x - t.first] + t.second);       }       for (pair<int, int> t : voc.right[i])         voc.min_cost[t.first] = min(voc.min_cost[t.first], t.second);     }     if (ans == INF)       cout <<  -1  << endl;     else       cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void readi(T &x) {     T input = 0;     bool negative = false;     char c =    ;     while (c <  - ) {       c = getchar();     }     if (c ==  - ) {       negative = true;       c = getchar();     }     while (c >=  0 ) {       input = input * 10 + (c -  0 );       c = getchar();     }     if (negative) {       input = -input;     }     x = input;   }   template <class T>   void printi(T output) {     if (output == 0) {       putchar( 0 );       return;     }     if (output < 0) {       putchar( - );       output = -output;     }     int aout[20];     int ilen = 0;     while (output) {       aout[ilen] = ((output % 10));       output /= 10;       ilen++;     }     for (int i = ilen - 1; i >= 0; i--) {       putchar(aout[i] +  0 );     }     return;   }   template <class T>   void ckmin(T &a, T b) {     a = min(a, b);   }   template <class T>   void ckmax(T &a, T b) {     a = max(a, b);   }   long long randomize(long long mod) {     return ((1ll << 30) * rand() + (1ll << 15) * rand() + rand()) % mod;   }   const long double PI = 4.0 * atan(1.0);   const long double EPS = 1e-10;   long long normalize(long long x, long long mod = 1000000007) {     return (((x % mod) + mod) % mod);   }   int Q;   int X[3];   vector<int> factors[100013];   int A, B, C;   vector<int> guy[8];   int need[8][8][8];   int sz[8], pref[8];   int p[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},                  {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};   bool works(int a, int b, int c) {     if ((a & 1) && (b & 2) && (c & 4)) {       return true;     }     if ((a & 1) && (b & 4) && (c & 2)) {       return true;     }     if ((a & 2) && (b & 1) && (c & 4)) {       return true;     }     if ((a & 2) && (b & 4) && (c & 1)) {       return true;     }     if ((a & 4) && (b & 1) && (c & 2)) {       return true;     }     if ((a & 4) && (b & 2) && (c & 1)) {       return true;     }     return false;   }   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   long long cntleq(int idx, long long x) {     return upper_bound(guy[idx].begin(), guy[idx].end(), x) - guy[idx].begin();   }   long long choose2(long long a) { return a * (a - 1) / 2; }   long long choose3(long long a) { return a * (a - 1) * (a - 2) / 6; }   int32_t main() {     ios_base::sync_with_stdio(0);     srand(time(0));     if (fopen( cf1007b.in ,  r )) {       freopen( cf1007b.in ,  r , stdin);     }     for (int i = 1; i < 100013; i++) {       for (int j = i; j < 100013; j += i) {         factors[j].push_back(i);       }     }     cin >> Q;     for (int q = 0; q < Q; q++) {       cin >> X[0] >> X[1] >> X[2];       sort(X, X + 3);       long long ans = 0;       guy[1] = factors[X[0]];       guy[2] = factors[X[1]];       guy[3] = factors[gcd(X[0], X[1])];       guy[4] = factors[X[2]];       guy[5] = factors[gcd(X[0], X[2])];       guy[6] = factors[gcd(X[1], X[2])];       guy[7] = factors[gcd(X[0], gcd(X[1], X[2]))];       sz[7] = guy[7].size();       sz[6] = guy[6].size() - guy[7].size();       sz[5] = guy[5].size() - guy[7].size();       sz[3] = guy[3].size() - guy[7].size();       sz[4] = guy[4].size() - guy[6].size() - guy[5].size() + guy[7].size();       sz[2] = guy[2].size() - guy[6].size() - guy[3].size() + guy[7].size();       sz[1] = guy[1].size() - guy[3].size() - guy[5].size() + guy[7].size();       for (int i = 1; i < 8; i++) {         pref[i] = guy[i].size();       }       long long one = 0, two = 0, three = 0;       one = sz[7];       for (int i = 1; i < 8; i++) {         for (int j = 1; j < 8; j++) {           if (!works(i, i, j)) {             continue;           }           if (i == j) continue;           two += sz[i] * sz[j];         }       }       two += choose2(sz[7]) * 2;       for (int i = 1; i < 8; i++) {         for (int j = i + 1; j < 8; j++) {           for (int k = j + 1; k < 8; k++) {             if (!works(i, j, k)) continue;             three += sz[i] * sz[j] * sz[k];           }         }       }       for (int i = 1; i < 8; i++) {         for (int j = 1; j < 8; j++) {           if (!works(i, i, j)) continue;           if (i == j) continue;           three += choose2(sz[i]) * sz[j];         }       }       three += choose3(sz[7]);       ans = one + two + three;       cout << ans <<   n ;     }     cerr <<  time elapsed =   << (clock() / (CLOCKS_PER_SEC / 1000)) <<   ms           << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200000 + 10;   vector<int> G[maxn];   vector<int> inv[maxn];   int n, m;   int vis[2][maxn];   int in[maxn];   inline int topo_sort(vector<int>* G, int* in) {     stack<int> S;     for (auto i = (0); i < (n); ++i) {       if (in[i] == 0) S.push(i);     }     int cnt = 0;     for (; !S.empty();) {       int u = S.top();       S.pop();       ++cnt;       for (auto v : G[u]) {         if (--in[v] == 0) S.push(v);       }     }     return cnt == n;   }   inline void dfs(int u, vector<int>* g, int* vis) {     if (vis[u]) return;     vis[u] = 1;     for (auto v : g[u]) {       if (vis[v]) continue;       dfs(v, g, vis);     }   }   inline void solve() {     int ans = 0;     string o;     for (auto i = (0); i < (n); ++i) {       if (!vis[0][i] && !vis[1][i]) {         o.push_back( A );         ans++;       } else         o.push_back( E );       dfs(i, G, vis[0]);       dfs(i, inv, vis[1]);     }     printf( %d n%s n , ans, o.c_str());   }   int main(void) {     scanf( %d , &(n)), scanf( %d , &(m));     for (auto i = (0); i < (m); ++i) {       int u, v;       scanf( %d , &(u)), scanf( %d , &(v));       u--, v--;       G[u].push_back(v);       inv[v].push_back(u);       in[v]++;     }     if (!topo_sort(G, in)) return puts( -1 ), 0;     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   template <typename T>   T getint() {     T val = 0;     char c;     while ((c = getchar()) && !(c >=  0  && c <=  9 ))       ;     do {       val = (val * 10) + c -  0 ;     } while ((c = getchar()) && (c >=  0  && c <=  9 ));     return val;   }   const int maxn = 100001, hgt = 17;   int n, m;   vector<int> adj[maxn];   int par[maxn], b[maxn], lvl[maxn];   int dp[maxn][hgt];   void dfs_lca(int x, int p = -1, int lev = 0) {     b[x] = 1;     par[x] = p;     lvl[x] = lev;     for (auto i : adj[x]) {       if (!b[i]) {         dfs_lca(i, x, lev + 1);       }     }     b[x] = 2;   }   void init() {     memset(dp, -1, sizeof dp);     for (int i = 1; i <= n; ++i) {       dp[i][0] = par[i];     }     for (int j = 1; j < hgt; ++j) {       for (int i = 1; i <= n; ++i) {         if (dp[i][j - 1] != -1) {           dp[i][j] = dp[dp[i][j - 1]][j - 1];         }       }     }   }   int lca(int p, int q) {     if (lvl[p] > lvl[q]) swap(p, q);     for (int i = hgt - 1; i >= 0; i--) {       if (dp[q][i] != -1 && lvl[p] <= lvl[dp[q][i]]) q = dp[q][i];     }     if (p == q) return p;     for (int i = hgt - 1; i >= 0; i--) {       if (dp[q][i] != -1 && dp[q][i] != dp[p][i]) {         p = dp[p][i];         q = dp[q][i];       }     }     return dp[p][0];   }   int kth(int p, int k) {     for (int i = 0; i < hgt; ++i) {       if (k & (1 << i)) {         p = dp[p][i];       }     }     return p;   }   int dist(int a, int b) {     int l = lca(a, b);     return lvl[a] + lvl[b] - 2 * lvl[l];   }   int banned[maxn];   int b0[maxn], sz[maxn];   void dfs0(int x) {     b0[x] = 1;     sz[x] = 1;     for (auto i : adj[x]) {       if (b0[i] || banned[i]) continue;       dfs0(i);       sz[x] += sz[i];     }     b0[x] = 2;   }   int dfs1(int x, int p, int nn) {     int ind = -1;     for (auto i : adj[x]) {       if (banned[i] || i == p) continue;       if (sz[i] > nn / 2) ind = i;     }     if (ind != -1) return dfs1(ind, x, nn);     return x;   }   vector<int> cadj[maxn];   int cpar[maxn];   int decompose(int x = 1) {     memset(b0, 0, sizeof b0);     dfs0(x);     int centroid = dfs1(x, -1, sz[x]);     banned[centroid] = 1;     for (auto i : adj[centroid]) {       if (!banned[i]) {         int child = decompose(i);         cadj[centroid].push_back(child);         cpar[child] = centroid;       }     }     return centroid;   }   int main() {     n = getint<int>();     m = getint<int>();     for (int i = 1; i < n; ++i) {       int a, b;       a = getint<int>();       b = getint<int>();       adj[a].push_back(b);       adj[b].push_back(a);     }     dfs_lca(1);     init();     int root = decompose();     vector<int> ans(n + 1, 1e9);     for (int i = -1; i < m; ++i) {       int typ, v;       if (i >= 0) {         typ = getint<int>();         v = getint<int>();       } else {         typ = 1;         v = 1;       }       if (typ == 1) {         int akt = v;         while (akt) {           ans[akt] = min(ans[akt], dist(akt, v));           akt = cpar[akt];         }       } else {         int res = 1e9;         int akt = v;         while (akt) {           res = min(res, ans[akt] + dist(akt, v));           akt = cpar[akt];         }         printf( %d n , res);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> volc[100000];   int main() {     int n, m;     scanf( %d , &n);     scanf( %d , &m);     for (int i = 0; i < m; i++) {       scanf( %d , &volc[i].first);       scanf( %d , &volc[i].second);     }     sort(volc, volc + m);     int x = 0;     vector<pair<int, int> > ints;     ints.push_back(make_pair(1, 1));     int i = 0;     while (i != m) {       if (ints.empty()) break;       if (volc[i].first > x + 1) {         int f = ints[0].first;         ints.clear();         ints.push_back(make_pair(f, n));       }       x = volc[i].first;       vector<pair<int, int> > ys;       int last = 0;       while (x == volc[i].first) {         if ((volc[i].second - last - 1) > 0)           ys.push_back(make_pair(last + 1, volc[i].second - 1));         last = volc[i].second;         i++;       }       if ((n + 1 - last - 1) > 0) ys.push_back(make_pair(last + 1, n));       vector<pair<int, int> > nints;       int k = 0;       for (int j = 0; j < (int)(ints.size()); j++) {         while (k < (int)(ys.size()) && ys[k].second < ints[j].first) k++;         while (k < (int)(ys.size()) && ys[k].first <= ints[j].second) {           nints.push_back(               make_pair(max(ys[k].first, ints[j].first), ys[k].second));           k++;         }       }       ints = nints;     }     if (ints.empty()) {       cout << -1 << endl;       return 0;     }     if (x != n || ints[(int)(ints.size()) - 1].second == n)       cout << 2 * (n - 1) << endl;     else       cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = acos(-1);   vector<complex<double> > fft(const vector<complex<double> >& as) {     int n = (int)as.size();     int k = 0;     while ((1 << k) < n) k++;     vector<int> r(n);     r[0] = 0;     int h = -1;     for (int i = 1; i < n; i++) {       if ((i & (i - 1)) == 0) h++;       r[i] = r[i ^ (1 << h)];       r[i] |= (1 << (k - h - 1));     }     vector<complex<double> > root(n);     for (int i = 0; i < n; i++) {       double ang = 2.0 * PI * i / n;       root[i] = complex<double>(cos(ang), sin(ang));     }     vector<complex<double> > cur(n);     for (int i = 0; i < n; i++) cur[i] = as[r[i]];     for (int len = 1; len < n; len <<= 1) {       vector<complex<double> > ncur(n);       int step = n / (len << 1);       for (int pdest = 0; pdest < n;) {         for (int i = 0; i < len; i++) {           complex<double> val = root[i * step] * cur[pdest + len];           ncur[pdest] = cur[pdest] + val;           ncur[pdest + len] = cur[pdest] - val;           pdest++;         }         pdest += len;       }       cur.swap(ncur);     }     return cur;   }   vector<complex<double> > inv_fft(const vector<complex<double> >& fa) {     vector<complex<double> > res = fft(fa);     int nn = res.size();     for (int i = 0; i < nn; i++) {       res[i] /= nn;     }     reverse(res.begin() + 1, res.end());     return res;   }   vector<complex<double> > multiply(vector<complex<double> >& a,                                     vector<complex<double> >& b) {     int len = a.size() + b.size();     while (len & (len - 1)) ++len;     a.resize(len);     b.resize(len);     a = fft(a);     b = fft(b);     for (int i = 0; i < len; ++i) a[i] *= b[i];     a = inv_fft(a);     return a;   }   void create(string& s, int size, char x, int k, vector<complex<double> >& v) {     complex<double> zero(0, 0);     for (int i = 0, b = 1; i < size; i += b, b = 1) {       if (s[i] == x) {         b = 0;         v[i] = 1;         for (int j = i - 1; j >= 0 && s[j] != x && v[j] == zero && j >= i - k;              j--)           v[j] = 1;         int tmp = i;         for (++i; i < size && s[i] != x && i <= tmp + k && v[i] == zero; i++)           v[i] = 1;       }     }   }   int main() {     ios::sync_with_stdio(false), cin.tie(nullptr);     int k, a = 0, g = 0, c = 0, t = 0, n, m;     string s, s2;     cin >> n >> m >> k >> s >> s2;     for (int i = 0; i < m / 2; i++) {       swap(s2[i], s2[m - 1 - i]);     }     int size = 1;     while (size < m + n - 1) size *= 2;     vector<complex<double> > av(n), av2(m);     create(s, n,  A , k, av);     create(s2, m,  A , 0, av2);     av = multiply(av, av2);     vector<complex<double> > tv(n), tv2(m);     create(s, n,  T , k, tv);     create(s2, m,  T , 0, tv2);     tv = multiply(tv, tv2);     vector<complex<double> > gv(n), gv2(m);     create(s, n,  G , k, gv);     create(s2, m,  G , 0, gv2);     gv = multiply(gv, gv2);     vector<complex<double> > cv(n), cv2(m);     create(s, n,  C , k, cv);     create(s2, m,  C , 0, cv2);     cv = multiply(cv, cv2);     int sum = 0;     for (int i = 0; i < cv.size(); i++)       sum += ((int)(cv[i].real() + gv[i].real() + av[i].real() + tv[i].real() +                     0.3) == m);     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char grid[512][512];   bool used[512][512];   int n, m;   struct op {     char type;     int x, y;     op() {}     op(char c, int a, int b) {       type = c;       x = a;       y = b;     }   };   vector<op> res;   int dx[4] = {0, -1, 0, 1};   int dy[4] = {1, 0, -1, 0};   void go(int x, int y, int level) {     if (x < 1 || x > n || y > m || y < 1) return;     if (used[x][y]) return;     used[x][y] = 1;     for (int i = 0; i < 4; i++) {       go(x + dx[i], y + dy[i], level + 1);     }     if (level) {       res.push_back(op( D , x, y));       res.push_back(op( R , x, y));     }   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) cin >> grid[i][j];     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         if (grid[i][j] ==  . )           res.push_back(op( B , i, j));         else           used[i][j] = 1;       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         if (!used[i][j]) {           go(i, j, 0);         }       }     int k = res.size();     cout << k << endl;     for (int i = 0; i < k; i++) {       cout << res[i].type <<     << res[i].x <<     << res[i].y << endl;     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     bool f = false;     for (int x = 0; x < n; x++) {       cin >> a[x];       if (a[x] == 1) f = true;     }     if (f) {       cout << -1;     } else       cout << 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 50 + 10;   long long dp[maxN];   int used[maxN];   int main() {     int n;     long long l;     cin >> n >> l;     dp[0] = 1;     dp[1] = 1;     for (int i = 2; i < maxN; i++) dp[i] = dp[i - 1] + dp[i - 2];     l--;     for (int i = 0; i < n; i++) {       int k = n - i;       if (dp[k - 1] > l)         cout << i + 1 <<    ;       else {         l -= dp[k - 1];         cout << i + 2 <<     << i + 1 <<    ;         i++;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void Emsawy() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   const double PI = acos(-1.0);   const double eps = 1e-10;   const long long mod = 1e9 + 7, oo = 1e9;   int dx[]{1, -1, 0, 0, 1, -1, 1, -1};   int dy[]{0, 0, 1, -1, 1, -1, -1, 1};   vector<vector<pair<int, int> > > adj;   int n, m, k, t;   const int N = 4001;   int L[N], R[N];   long dp[89][2002][89 * 2 + 10];   long go(int l, int r, int k) {     long &ret = dp[k][l][n - r - l + 89];     if (ret != -oo) return ret;     if (l <= (n - r)) {       ret = -oo;       if (r - l > k) ret = max(ret, go(l + k, r, k) - L[l] + L[l + k]);       if (r - l > k + 1)         ret = max(ret, go(l + k + 1, r, k + 1) - L[l] + L[l + k + 1]);       return (ret == -oo ? ret = 0 : ret);     }     ret = oo;     if (r - l > k) ret = min(ret, go(l, r - k, k) - (-R[r] + R[r - k]));     if (r - l > k + 1)       ret = min(ret, go(l, r - k - 1, k + 1) - (-R[r] + R[r - k - 1]));     return (ret == oo ? ret = 0 : ret);   }   int main() {     Emsawy();     while (cin >> n) {       for (int i = 1; i <= n; i++) {         cin >> L[i];         R[i] = L[i];         L[i] += L[i - 1];       }       for (int i = n - 1; i >= 1; i--) R[i] += R[i + 1];       for (int i = 0; i < 89; i++) {         for (int j = 0; j < 2002; j++) {           for (int g = 0; g < 89 * 2 + 10; g++) {             dp[i][j][g] = -oo;           }         }       }       n += 1;       cout << go(0, n, 1) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int nod, edge;   int a[401][401];   int dist[401];   int vis[401];   int bfs(int node) {     queue<int> q;     q.push(node);     vis[node] = 1;     while (q.size()) {       int cur = q.front();       q.pop();       if (cur == nod) return dist[cur];       for (int i = nod; i >= 1; i--) {         if (!vis[i] and (a[cur][i] + a[i][cur])) {           dist[i] += dist[cur] + 1, q.push(i);           vis[i] = 1;         }       }     }     return 0;   }   int bfs1(int node) {     memset(dist, 0, sizeof dist);     memset(vis, 0, sizeof vis);     queue<int> q;     q.push(node);     vis[node] = 1;     while (q.size()) {       int cur = q.front();       q.pop();       if (cur == nod) return dist[cur];       for (int i = nod; i >= 1; i--) {         if (!vis[i] and !(a[cur][i] + a[i][cur])) {           dist[i] += dist[cur] + 1, q.push(i);           vis[i] = 1;         }       }     }     return 0;   }   int main() {     cin >> nod >> edge;     int x, y;     while (edge--) {       cin >> x >> y;       a[x][y] = 1;       a[y][x] = 1;     }     int ans = bfs(1);     int ans2 = bfs1(1);     if (ans != 0 and ans2 != 0)       cout << max(ans, ans2);     else       cout << -1;   }


#include <bits/stdc++.h>   using namespace std;   struct point {     int x, y;     int det(point p) { return x * p.y - y * p.x; }   };   point a[10], b[10];   int GetCross(point p1, point p2, point p) {     return (p2.x - p1.x) * (p.y - p1.y) - (p.x - p1.x) * (p2.y - p1.y);   }   bool check(point p) {     return GetCross(b[1], b[2], p) * GetCross(b[3], b[4], p) >= 0 &&            GetCross(b[2], b[3], p) * GetCross(b[4], b[1], p) >= 0;   }   int main() {     for (int i = 1; i <= 4; i++) {       scanf( %d%d , &a[i].x, &a[i].y);     }     for (int i = 1; i <= 4; i++) {       scanf( %d%d , &b[i].x, &b[i].y);     }     int mix = min(min(min(a[1].x, a[2].x), a[3].x), a[4].x);     int mxx = max(max(max(a[1].x, a[2].x), a[3].x), a[4].x);     int miy = min(min(min(a[1].y, a[2].y), a[3].y), a[4].y);     int mxy = max(max(max(a[1].y, a[2].y), a[3].y), a[4].y);     for (int i = mix; i <= mxx; i++) {       for (int j = miy; j <= mxy; j++) {         point pi;         pi.x = i;         pi.y = j;         if (check(pi)) {           printf( YES n );           return 0;         }       }     }     printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   const int sqrtn = 505;   int l = 1, r, sum, cant;   int n, m, p, s[maxn], w[maxn], cnt[maxn], last[maxn], pre[maxn], nxt[maxn],       qwq[maxn][sqrtn], num[maxn];   vector<int> v;   struct node {     int l, r, id;   } a[maxn];   inline bool cmp(node a, node b) {     return a.l / p == b.l / p ? ((a.l / p) & 1 ? a.r < b.r : a.r > b.r)                               : a.l < b.l;   }   inline void add(int x) {     cnt[w[x]]++;     sum += cnt[w[x]] == 1;   }   inline void add_right(int x) {     add(x);     if (cnt[w[x]] != 1 && pre[x]) {       qwq[w[x]][pre[x]]++;       if (qwq[w[x]][pre[x]] == 1) {         num[w[x]]++;         if (num[w[x]] == 2) {           cant++;         }       }     }   }   void add_left(int x) {     add(x);     if (cnt[w[x]] != 1 && nxt[x]) {       qwq[w[x]][nxt[x]]++;       if (qwq[w[x]][nxt[x]] == 1) {         num[w[x]]++;         if (num[w[x]] == 2) {           cant++;         }       }     }   }   inline void del(int x) {     cnt[w[x]]--;     sum -= cnt[w[x]] == 0;   }   inline void del_right(int x) {     del(x);     if (qwq[w[x]][pre[x]] && pre[x]) {       qwq[w[x]][pre[x]]--;       if (qwq[w[x]][pre[x]] == 0) {         num[w[x]]--;         if (num[w[x]] == 1) {           cant--;         }       }     }   }   inline void del_left(int x) {     del(x);     if (qwq[w[x]][nxt[x]] && nxt[x]) {       qwq[w[x]][nxt[x]]--;       if (qwq[w[x]][nxt[x]] == 0) {         num[w[x]]--;         if (num[w[x]] == 1) {           cant--;         }       }     }   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> w[i];       if (last[w[i]]) {         pre[i] = i - last[w[i]];         nxt[last[w[i]]] = i - last[w[i]];         v.push_back(pre[i]);         v.push_back(nxt[last[w[i]]]);       }       last[w[i]] = i;     }     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     for (int i = 1; i <= n; i++) {     }     p = sqrt(n);     cin >> m;     for (int i = 1; i <= m; i++) {       cin >> a[i].l >> a[i].r;       a[i].id = i;     }     sort(a + 1, a + m + 1, cmp);     for (int i = 1; i <= m; i++) {       while (l > a[i].l) {         add_left(--l);       }       while (r < a[i].r) {         add_right(++r);       }       while (l < a[i].l) {         del_left(l++);       }       while (r > a[i].r) {         del_right(r--);       }       s[a[i].id] = sum + (cant == sum);     }     for (int i = 1; i <= m; i++) {       cout << s[i] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   bool isprime(int n) {     for (int i = 2; i < n; i++) {       if (n % i == 0) {         return false;       }     }     return true;   }   int main() {     int n;     cin >> n;     string s;     cin >> s;     int diff = 1;     int i = 0;     while (i < n) {       cout << s[i];       i = i + diff;       diff++;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<char, pair<int, int> > > closest(10,                                                make_pair( * , make_pair(0, 0)));   int n, X, Y;   int check_dir(int x, int y) {     if (x == X && y > Y) return 1;     if (x == X && y < Y) return 5;     if (Y == y && x > X) return 3;     if (Y == y && x < X) return 7;     if (X < x && (x - X) == (y - Y)) return 2;     if (X > x && (x - X) == (y - Y)) return 6;     if (y > Y && (X - x) == (y - Y)) return 4;     if (y < Y && (X - x) == (y - Y)) return 8;     return 0;   }   int dist(int x, int y, int xx, int yy) { return max(abs(xx - x), abs(yy - y)); }   void get_closest(char z, int x, int y, int dir) {     if (closest[dir].first ==  *  ||         dist(X, Y, x, y) <             dist(X, Y, closest[dir].second.first, closest[dir].second.second)) {       closest[dir].first = z;       closest[dir].second = make_pair(x, y);     }   }   int main() {     scanf( %d%d%d , &n, &X, &Y);     for (int i = 0; i < n; ++i) {       char z;       int x, y;       cin >> z >> x >> y;       int dir = check_dir(x, y);       if (dir) {         get_closest(z, x, y, dir);       }     }     bool flag = false;     for (int i = 1; i <= 7; i += 2) {       if (closest[i].first ==  R  || closest[i].first ==  Q ) flag = true;     }     for (int i = 2; i <= 8; i += 2) {       if (closest[i].first ==  B  || closest[i].first ==  Q ) flag = true;     }     if (flag)       printf( YES );     else       printf( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void c_p_c() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   bool isPrime(long long n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long i = 5; i * i <= n; i += 6) {       if (n % i == 0 || n % (i + 2) == 0) return false;     }     return true;   }   vector<long long> Sieve(long long n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (long long i = 2; i * i <= n; i++) {       if (prime[i]) {         for (long long j = i * i; j <= n; j += i) {           prime[j] = false;         }       }     }     vector<long long> primes;     for (long long i = 2; i <= n; i++) {       if (prime[i]) primes.push_back(i);     }     return primes;   }   map<long long, long long> primeFactors(long long n) {     map<long long, long long> factors;     while (n % 2 == 0) {       n /= 2;       factors[2]++;     }     for (long long i = 3; i <= sqrt(n); i += 2) {       while (n % i == 0) {         factors[i]++;         n /= i;       }     }     if (n > 2) factors[n]++;     return factors;   }   int32_t main() {     c_p_c();     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       vector<long long> arr(n + 1, 0);       long long num;       for (long long i = 0; i < n; i++) {         cin >> num;         arr[num]++;       }       sort(arr.begin(), arr.end(), greater<long long>());       long long ans = arr[0], prev = arr[0];       for (long long i = 1; i <= n; i++) {         if (prev == 0) break;         if (arr[i] >= prev) {           ans += prev - 1;           prev--;         } else {           ans += arr[i];           prev = arr[i];         }       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e4 + 5;   int n, a[N], m, k;   long long ans;   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     if (n % 2 == 0) {       puts( 0 );       return 0;     }     int mn = N * 100;     for (int i = 1; i <= n; i += 2) mn = min(mn, a[i]);     long long ans = min((long long)mn, 1ll * (m / ((n + 1) / 2)) * k);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string ITS(long long x) {     string s =   ;     while (x > 0) {       s += (char)(x % 10 +  0 );       x /= 10;     }     string t =   ;     for (int i = s.size() - 1; i > -1; i--) t += s[i];     return t;   }   long long inf = 1e9;   long long mod = 1e9 + 7;   const long long M = 2e3;   int main() {     std::ios::sync_with_stdio(false);     long long n, m;     cin >> n >> m;     double a = n * pow(1.000000011, m);     cout << fixed << setprecision(15) << a;   }


#include <bits/stdc++.h>   using namespace std;   long long n = 0, m = 0, q = 0;   long long ans = 0;   vector<vector<int>> G = vector<std::vector<int>>(100001);   vector<long long> f(112345), fa(100001), dep(100001), val(100001), sz(100001),       s(100001);   long long F(long long x) { return x == f[x] ? x : (f[x] = F(f[x])); }   void dfs(long long u, long long ff) {     sz[u] = 1;     dep[u] = dep[ff] + 1;     fa[u] = ff;     for (int i = 0; i < G[u].size(); ++i) {       long long v = G[u][i];       if (v == ff) {         continue;       }       dfs(v, u);       sz[u] += sz[v];       val[u] += sz[v] * sz[v];     }     val[u] += (n - sz[u]) * (n - sz[u]);     ans -= val[u];   }   void merge(int x, int y) {     ans -= s[x] * (s[x] - 1) * (s[x] - 2);     ans -= s[y] * (s[y] - 1) * (s[y] - 2);     ans -= s[x] * (s[x] - 1) * (n - s[x]) * 2;     ans -= s[y] * (s[y] - 1) * (n - s[y]) * 2;     ans -= (n - s[x]) * (n - s[x]) * s[x];     ans -= (n - s[y]) * (n - s[y]) * s[y];     ans += val[x] * s[x];     ans += val[y] * s[y];     val[x] += val[y] - sz[y] * sz[y] - (n - sz[y]) * (n - sz[y]);     s[x] += s[y];     f[y] = x;     ans += s[x] * (s[x] - 1) * (s[x] - 2);     ans += s[x] * (s[x] - 1) * (n - s[x]) * 2;     ans += (n - s[x]) * (n - s[x]) * s[x];     ans -= val[x] * s[x];   }   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       G[i] = std::vector<int>();       s[i] = 1;       f[i] = i;     }     for (int i = 0; i < n - 1; ++i) {       int u, v;       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     ans = 1LL * n * (n - 1) * (n - 1);     dfs(1, 0);     cout << ans << endl;     cin >> q;     while (q-- > 0) {       int u, v;       cin >> u >> v;       u = F(u);       v = F(v);       while (u != v) {         if (dep[u] < dep[v]) {           int o = v;           v = u;           u = o;         }         merge(F(fa[u]), u);         u = F(u);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   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;   }   const long long maxn = 1000001, mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     long double a[n], b[n];     for (long long i = 0; i < (long long)(n); ++i) cin >> a[i];     for (long long i = 0; i < (long long)(n); ++i) cin >> b[i];     int ex = 0;     map<long double, int> freq;     for (long long i = 0; i < (long long)(n); ++i) {       if (a[i] == 0 && b[i] == 0) ex++;       if (a[i] == 0) continue;       freq[b[i] / a[i]]++;     }     int ans = 0;     for (auto it : freq) {       ans = max(ans, it.second);     }     cout << ans + ex <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const int N = 3e5 + 5, M = 1e8 + 5, OO = 1000000;   int T, n, m;   int u, v;   int deg[N];   int main() {     scanf( %d , &n);     for (int i = 1; i < n; ++i) {       scanf( %d %d , &u, &v);       ++deg[u], ++deg[v];     }     for (int i = 1; i <= n; ++i) {       if (deg[i] == 2) {         printf( NO n );         return 0;       }     }     printf( YES n );   }


#include <bits/stdc++.h>   #pragma GCC target( sse )   const int maxn = 2e5 + 5;   struct IO {     char c;     inline char gc() {       static char buf[maxn], *p1 = buf, *p2 = buf;       return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, maxn, stdin), p1 == p2)                  ? EOF                  : *p1++;     }     template <typename T>     inline IO &operator>>(T &_) {       _ = 0;       bool f = 1;       c = gc();       while (c <  0  || c >  9 ) {         if (c ==  - ) f = 0;         c = gc();       }       while (c >=  0  && c <=  9 ) {         _ = _ * 10 + c - 48;         c = gc();       }       if (!f) _ = -_;       return *this;     }     char buf[maxn];     int p = 0;     ~IO() { fwrite(buf, 1, p, stdout); }     inline void pc(const char &c) {       buf[p++] = c;       if (p == maxn) fwrite(buf, 1, maxn, stdout), p = 0;     }     template <typename T>     inline IO &operator<<(T x) {       if (!x) {         pc(48);         pc(  n );         return *this;       }       static int wt[41], len;       len = 0;       if (x < 0) {         pc( - );         x = -x;       }       for (; x; x /= 10) {         wt[++len] = x % 10;       }       while (len) {         pc(wt[len--] + 48);       }       pc(  n );       return *this;     }     inline IO &operator>>(char s[]) {       c = gc();       int len = -1;       while (c ==     || c ==   n  || c ==   r ) c = gc();       while (c !=     && c !=   n  && c !=   r  && c !=   0  && c != EOF) {         s[++len] = c;         c = gc();       }       s[++len] =   0 ;       return *this;     }   } io;   int n, m, q;   char s1[maxn], s2[maxn];   unsigned long long b1[64][(maxn >> 6) + 64], b2[64][(maxn >> 6) + 64];   void build() {     for (int i = 0; i < 64; i++) {       for (int j = i; j < n; j += 64) {         for (int k = 0; k < 64; k++)           b1[i][j >> 6] += (1ll << k) * (s1[j + k] - 48);       }     }     for (int i = 0; i < 64; i++) {       for (int j = i; j < m; j += 64) {         for (int k = 0; k < 64; k++)           b2[i][j >> 6] += (1ll << k) * (s2[j + k] - 48);       }     }   }   int main() {     io >> s1 >> s2;     n = strlen(s1), m = strlen(s2);     build();     io >> q;     while (q--) {       int p1, p2, len;       io >> p1 >> p2 >> len;       int now = 0, a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0;       unsigned long long *f1 = b1[p1 & 63] + (p1 >> 6),                          *f2 = b2[p2 & 63] + (p2 >> 6);       for (; now + 511 < len; now += 512, f1 += 8, f2 += 8) {         a1 += __builtin_popcountll(f1[0] ^ f2[0]);         a2 += __builtin_popcountll(f1[1] ^ f2[1]);         a3 += __builtin_popcountll(f1[2] ^ f2[2]);         a4 += __builtin_popcountll(f1[3] ^ f2[3]);         a5 += __builtin_popcountll(f1[4] ^ f2[4]);         a6 += __builtin_popcountll(f1[5] ^ f2[5]);         a7 += __builtin_popcountll(f1[6] ^ f2[6]);         a8 += __builtin_popcountll(f1[7] ^ f2[7]);       }       for (; now < len; now++) a1 += (s1[p1 + now] - 48) ^ (s2[p2 + now] - 48);       io << a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int n, i, j, k, sum = 0, ev = 0, ze = 0;       string s;       cin >> s;       n = s.length();       for (i = 0; i < n; i++) {         sum = sum + s[i] -  0 ;         if ((s[i] -  0 ) % 2 == 0) ev++;         if (s[i] -  0  == 0) ze++;       }       if (ze == 0)         cout <<  cyan  << endl;       else {         if (ev < 2)           cout <<  cyan  << endl;         else {           if (sum % 3 == 0)             cout <<  red  << endl;           else             cout <<  cyan  << endl;         }       }     }   }


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


#include <bits/stdc++.h>   using namespace std;   int MOD = 1000000007;   int tree[20000][2];   int main() {     ios::sync_with_stdio(0);     int n, a, b, k;     cin >> n >> a >> b >> k;     for (int i = 1; i <= n; i++) {       if (i != b) tree[i][0]++;     }     int h = 1;     for (int j = 1; j <= k; j++) {       for (int i = 2; i <= n; i++) {         tree[i][1 - h] += tree[i - 1][1 - h];         tree[i][1 - h] %= MOD;       }       for (int i = 1; i <= n; i++) {         if (i == b) continue;         int lim = abs(i - b);         lim--;         tree[i][h] =             tree[min(n, i + lim)][1 - h] - tree[max(1, i - lim) - 1][1 - h];         tree[i][h] += MOD;         tree[i][h] %= MOD;         tree[i][h] -= (tree[i][1 - h] - tree[i - 1][1 - h] + MOD) % MOD;         tree[i][h] += MOD;         tree[i][h] %= MOD;       }       h ^= 1;     }     for (int i = 2; i <= n; i++) {       tree[i][1 - h] += tree[i - 1][1 - h];       tree[i][1 - h] %= MOD;     }     cout << (tree[a][1 - h] - tree[a - 1][1 - h] + MOD) % MOD << endl;     return 0;   }


#include <bits/stdc++.h>   template <typename T>   bool Chkmax(T &x, T y) {     return x < y ? x = y, true : false;   }   template <typename T>   bool Chkmin(T &x, T y) {     return y < x ? x = y, true : false;   }   const int MAXN = 1e5 + 1e4, MAXK = 5 + 5;   const double eps = 1e-4;   int n;   int k;   std::pair<int, int> ans[MAXK];   inline int Dcmp(const double &x) {     if (fabs(x) < eps) return 0;     return x > 0 ? 1 : -1;   }   struct Point {     double x, y;     Point() {}     Point(double x, double y) : x(x), y(y) {}   };   struct Line {     int A, B, C, id;     Line() {}     Line(int A, int B, int C, int id) : A(A), B(B), C(C), id(id) {}     inline bool OnLine(const Point &p) {       return Dcmp(A * p.x + B * p.y + C) == 0;     }   } s[MAXN];   Point GetInter(Line l1, Line l2) {     if (l2.A == 0) std::swap(l1, l2);     double x, y;     x = (double)(-l2.C + (double)l2.B * l1.C / l1.B) /         (l2.A - (double)l1.A * l2.B / l1.B);     y = (double)(-l1.C - l1.A * x) / l1.B;     return Point(x, y);   }   bool Work(int N, int K) {     if (N <= K) {       printf( YES n%d n , k - K + N);       for (int i = 1, _ = N; i <= _; i++) printf( %d %d n , s[i].id, -1);       return true;     }     if (K == 0) return false;     Point p[MAXK * MAXK];     std::pair<int, int> fr[MAXK * MAXK];     int tot = 0;     for (int i = 1, _ = K + 1; i <= _; i++)       for (int j = i + 1, _ = K + 1; j <= _; j++) {         ++tot;         p[tot] = GetInter(s[i], s[j]);         fr[tot] = std::make_pair(s[i].id, s[j].id);       }     for (int i = 1, _ = tot; i <= _; i++) {       int newN = N;       for (int j = 1; j <= newN; j++) {         if (s[j].OnLine(p[i])) {           std::swap(s[j], s[newN--]);           j--;         }       }       if (Work(newN, K - 1)) {         printf( %d %d n , fr[i].first, fr[i].second);         return true;       }     }     return false;   }   void Init() {     int A, B, C;     scanf( %d%d , &n, &k);     for (int i = 1, _ = n; i <= _; i++) {       scanf( %d%d%d , &A, &B, &C);       s[i] = Line(A, B, C, i);     }   }   int main() {     if (fopen( D.in ,  r ) != NULL) {       freopen( D.in ,  r , stdin);       freopen( D.out ,  w , stdout);     }     Init();     if (!Work(n, k)) printf( NO n );     return 0;   }


#include <bits/stdc++.h>   int mod = 1000000007;   using namespace std;   using namespace std;   int n, k;   int a[400005];   int pos[400005];   map<int, int> m;   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     ;     cin >> n >> k;     for (int i = 1; i <= n; i++) pos[i] = 1e7;     m[a[1]] = 1e7;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= n; i++) {       pos[m[a[i]]] = i;       m[a[i]] = i;     }     int ans = 0;     set<int> s;     set<pair<int, int> > ss;     for (int i = 1; i <= n; i++) {       if (!s.count(a[i])) {         if (s.size() < k) {           ans++;           s.insert(a[i]);           ss.insert(make_pair(pos[i], a[i]));         } else {           ans++;           set<pair<int, int> >::iterator it = ss.end();           it--;           int val = (*it).second;           ss.erase(it);           s.erase((val));           s.insert(a[i]);           ss.insert(make_pair(pos[i], a[i]));         }       } else {         ss.erase(ss.begin());         ss.insert(make_pair(pos[i], a[i]));       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n = ({       int x;       scanf( %d , &x);       x;     });     string s;     cin >> s;     string ans =  no ;     for (int d = (1); d < (30); d += (1)) {       for (int i = (0); i < (n); i += (1)) {         int g = 1;         for (int j = (0); j < (5); j += (1)) {           if (i + j * d >= n || s[i + j * d] ==  . ) {             g = 0;           }         }         if (g) {           ans =  yes ;           break;         }       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using ull = unsigned long long;   using pii = pair<int, int>;   using C = complex<double>;   const double PI = 3.14159265;   const double eps = 1e-8;   const int N = 1 << 17;   const int M = 1e9 + 7;   const int inf = 1e9 + 7;   const ll INF = 1e18;   ll pw(ll a, ll b) {     ll t = 1;     a %= M;     for (; b; b >>= 1) {       if (b & 1) {         t = t * a % M;       }       a = a * a % M;     }     return t;   }   ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }   int n;   int num[N];   int mx[N << 1];   int seg[N << 1];   int find() {     int u = 1, w = 0;     while (u < N) {       if (mx[u << 1 | 1] + w > 0) {         u = u << 1 | 1;       } else {         w += seg[u << 1 | 1];         u <<= 1;       }     }     return u - N;   }   void add(int p, int v) {     p += N;     seg[p] += v;     mx[p] = seg[p] == 1;     for (p >>= 1; p; p >>= 1) {       seg[p] = seg[p << 1] + seg[p << 1 | 1];       mx[p] = max(mx[p << 1 | 1], mx[p << 1] + seg[p << 1 | 1]);     }   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n;     int p, t, x;     for (int i = 0; i < n; i++) {       cin >> p >> t;       if (t == 1) {         cin >> num[p];         add(p, 1);       } else {         add(p, -1);       }       int ps = find();       if (mx[1] <= 0) {         puts( -1 );       } else {         printf( %d n , num[ps]);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long x, y;       cin >> x >> y;       long long l = x - y;       l = l < 0 ? -l : l;       int r = l % 10;       long long a = l / 10.0;       if (r) a++;       cout << a;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long double INF = 1e9;   const long double EPS = 1e-9;   long double dist(long double x1, long double y1, long double x2,                    long double y2) {     return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));   }   long double func(long double X, long double Y, long double L, long double R) {     return min(dist(L, 0, X, Y), dist(R, 0, X, Y)) + R - L;   }   int main() {     cout.setf(ios::fixed);     cout.precision(15);     int n, k;     cin >> n >> k;     k--;     vector<int> x(n);     for (int i = 0; i < n; i++) cin >> x[i];     int X, Y;     cin >> X >> Y;     int xStart;     if (k < n) xStart = x[k];     sort(x.begin(), x.end());     if (k == n) {       cout << func(X, Y, x[0], x[n - 1]);       return 0;     }     k = lower_bound(x.begin(), x.end(), xStart) - x.begin();     long double ans = INF;     for (int i = 0; i < n; i++) {       long double cur1 =           min(x[k] - x[0] + x[i] - x[0] + dist(x[i], 0, X, Y),               abs(x[i] - x[k]) + x[i] - x[0] + dist(x[0], 0, X, Y));       if (i + 1 < n) cur1 += func(X, Y, x[i + 1], x[n - 1]);       long double cur2 =           min(x[n - 1] - x[k] + x[n - 1] - x[i] + dist(x[i], 0, X, Y),               abs(x[k] - x[i]) + x[n - 1] - x[i] + dist(x[n - 1], 0, X, Y));       if (i - 1 >= 0) cur2 += func(X, Y, x[0], x[i - 1]);       ans = min(ans, min(cur1, cur2));     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int v, e;   vector<int> out[30005];   vector<int> ans;   bool vis[30005];   void dfstree(int n) {     for (auto x : out[n]) {       if (!vis[x]) {         vis[x] = true;         dfstree(x);       }     }     ans.push_back(n);   }   int main() {     cin >> v >> e;     for (int i = 0; i < e; i++) {       int x, y;       cin >> x >> y;       out[x].push_back(y);     }     for (int i = 1; i < v + 1; i++) {       if (!vis[i]) {         vis[i] = true;         dfstree(i);       }     }     for (auto x : ans) cout << x <<    ;     cout <<   n ;     ;   }


#include <bits/stdc++.h>   using namespace std;   long long n, a[300005], ans;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n / 2; i++) {       ans += (a[i] + a[n - i + 1]) * (a[i] + a[n - i + 1]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100;   int n, m, ans;   int num[maxn];   set<int> st[2];   set<int>::iterator it, itt;   void get_st(int begin, int end, set<int> &st) {     st.clear();     st.insert(0);     for (int i = 0; i < (1 << (end - begin)); ++i) {       int tmp = 0;       for (int j = 0; j < end - begin; ++j) {         if (((i >> j) & 1) == 1) {           tmp = (tmp + num[j + begin]) % m;         }       }       st.insert(tmp);     }   }   int main() {     ios::sync_with_stdio(false);     while (scanf( %d%d , &n, &m) != EOF) {       ans = 0;       for (int i = 0; i < n; ++i) {         scanf( %d , &num[i]);       }       get_st(0, (n + 1) / 2, st[0]);       get_st((n + 1) / 2, n, st[1]);       for (it = st[0].begin(); it != st[0].end(); ++it) {         for (int i = 1; i <= 2; ++i) {           itt = st[1].lower_bound(m * i - *it);           if (itt != st[1].begin()) {             --itt;             ans = max(ans, (*it + *itt) % m);           }         }       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a[200005], head[200005];   struct edge {     int to, nxt;     edge(int _t = -1, int _n = -1) {       to = _t;       nxt = _n;     }   } e[400005];   void addedge(int u, int v) {     e[m] = edge(v, head[u]);     head[u] = m++;     e[m] = edge(u, head[v]);     head[v] = m++;   }   vector<int> g;   int dp[200005], s;   void dfs1(int fa, int u) {     if (a[u] == -1)       dp[u] = 1;     else       dp[u] = 0;     for (int p = head[u]; p != -1; p = e[p].nxt) {       int v = e[p].to;       if (v == fa) continue;       dfs1(u, v);       dp[u] += dp[v];     }   }   void dfs2(int fa, int u) {     if (s == 0 || dp[u] == 0) return;     int x;     for (int p = head[u]; p != -1; p = e[p].nxt) {       int v = e[p].to;       x = v;       if (v == fa || dp[v] == 0) continue;       g.push_back(v + 1);       if (a[v] == -1) {         s--;         dp[v]--;       } else {         s++;         dp[v]++;       }       a[v] = -a[v];       dfs2(u, v);       if (s == 0) return;       g.push_back(u + 1);       if (a[u] == -1) {         s--;         dp[u]--;       } else {         s++;         dp[u]++;       }       a[u] = -a[u];     }     if (a[u] == -1) {       a[u] = 1;       if (fa == -1) {         g.push_back(x + 1);         g.push_back(1);         g.push_back(x + 1);         return;       } else {         g.push_back(fa + 1);         g.push_back(u + 1);         s--;         dp[u]--;         if (a[fa] == -1) {           s--;           dp[fa]--;         } else {           s++;           dp[fa]++;         }         a[fa] = -a[fa];       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);       if (a[i] == -1) s++;     }     memset(head, -1, sizeof(head));     m = 0;     for (int i = 1; i < n; ++i) {       int u, v;       scanf( %d%d , &u, &v);       addedge(u - 1, v - 1);     }     if (s == 0) {       printf( 1 n );       return 0;     }     g.clear();     dfs1(-1, 0);     dfs2(-1, 0);     printf( 1 );     for (int i = 0; i < g.size(); ++i) printf(  %d , g[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = int(3e5), mod = int(1e9) + 7, block = 316;   int n, m, q;   vector<int> g[N], comp[N], heavy, di[N];   int root[N], cur, sz[N], d[N], dr[N], d1[N], d2[N];   bool used[N];   bool h[N];   long double ans[N], fans[N];   vector<pair<int, int> > qu[N];   void dfs(int v) {     used[v] = 1;     comp[cur].push_back(v);     sz[cur]++;     root[v] = cur;     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i];       if (!used[to]) dfs(to);     }   }   void dfs1(int v, int p = -1) {     used[v] = 1;     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i];       if (to == p) continue;       dfs1(to, v);       d1[v] = max(d1[v], d1[to] + 1);     }   }   void dfs2(int v, int p = -1) {     int mx1 = d2[v], mx2 = 0;     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i];       if (to == p) continue;       if (d1[to] + 1 > mx1) {         mx2 = mx1;         mx1 = d1[to] + 1;       } else if (d1[to] + 1 > mx2) {         mx2 = d1[to] + 1;       }     }     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i];       if (to == p) continue;       if (mx1 == d1[to] + 1) {         d2[to] = mx2 + 1;       } else         d2[to] = mx1 + 1;       dfs2(to, v);     }   }   int a[N], b[N], an, bn;   long long pref[N];   void cons(int v) {     an = 0;     for (int i = 0; i < di[v].size(); i++) {       a[++an] = di[v][i];     }     for (int i = 1; i <= an; i++) {       pref[i] = pref[i - 1] + a[i];     }   }   long double get(int v, int curd) {     bn = 0;     long long sum = 0;     for (int i = 0; i < di[v].size(); i++) {       b[++bn] = di[v][i];     }     int j = an;     for (int i = 1; i <= bn; i++) {       while (j > 0 && b[i] + a[j] + 1 > curd) j--;       sum += 1ll * j * curd + 1ll * (an - j) * b[i] + pref[an] - pref[j] + an - j;     }     return (sum + 0.0) / (1ll * an * bn);   }   int main() {     scanf( %d%d%d , &n, &m, &q);     for (int i = 1, u, v; i <= m; i++) {       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     for (int i = 1; i <= n; i++) {       if (!used[i]) {         cur = i;         dfs(i);       }     }     for (int i = 1; i <= n; i++) {       if (root[i] == i) {         if (sz[i] > block) {           heavy.push_back(i);           h[i] = 1;         }       }     }     memset(used, 0, sizeof(used));     for (int i = 1; i <= n; i++) {       if (!used[i]) {         dfs1(i);         dfs2(i);         int v = root[i];         for (int j = 0; j < comp[v].size(); j++) {           int u = comp[v][j];           dr[u] = max(d1[u], d2[u]);           di[v].push_back(dr[u]);           d[v] = max(d[v], dr[u]);         }         sort(di[v].begin(), di[v].end());       }     }     for (int i = 1, u, v; i <= q; i++) {       scanf( %d%d , &u, &v);       u = root[u];       v = root[v];       if (u == v) {         ans[i] = -1;       } else {         if (h[u]) {           qu[u].push_back(make_pair(v, i));         } else if (h[v]) {           qu[v].push_back(make_pair(u, i));         } else {           int curd = max(d[u], d[v]);           cons(u);           ans[i] = get(v, curd);         }       }     }     for (int i = 1; i <= n; i++) {       if (int(qu[i].size()) > 0) {         cons(i);         for (int j = 1; j <= n; j++)           if (root[j] == j) {             fans[j] = get(j, max(d[i], d[j]));           }         for (int j = 0; j < qu[i].size(); j++) {           ans[qu[i][j].second] = fans[qu[i][j].first];         }       }     }     for (int i = 1; i <= q; i++) {       printf( %.12lf n , double(ans[i]));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 10;   int n, k;   int ver[maxn], t[maxn], dp[maxn], tin[maxn],       cnt = 0, tout[maxn], h[maxn], p[maxn][30], important[maxn], ans = 0;   vector<int> e[maxn];   void dfs(int u, int par = 0) {     cnt++;     tin[u] = cnt;     h[u] = h[par] + 1;     for (int i = 0; i < e[u].size(); i++) {       int v = e[u][i];       if (v == par) continue;       p[v][0] = u;       for (int j = 1; j < 22; j++) p[v][j] = p[p[v][j - 1]][j - 1];       dfs(v, u);     }     tout[u] = cnt;     return;   }   int get(int u, int len) {     for (int i = 22; i >= 0; i--)       if (len & (1 << i)) u = p[u][i];     return u;   }   int lca(int u, int v) {     if (h[u] > h[v]) swap(u, v);     int len = h[v] - h[u];     v = get(v, len);     if (u == v) return u;     for (int i = 22; i >= 0; i--)       if (p[u][i] != p[v][i]) {         u = p[u][i];         v = p[v][i];       }     return p[u][0];   }   bool cmp(int u, int v) { return tin[u] < tin[v]; }   bool compare(int u, int v) { return tin[u] < tin[v] && tout[u] >= tout[v]; }   bool buildGraph() {     for (int i = 1; i < k; i++) ver[i + k] = lca(ver[i], ver[i + 1]);     sort(ver + 1, ver + 2 * k, cmp);     int cnter = unique(ver + 1, ver + 2 * k) - ver - 1;     for (int i = 1; i <= cnter; i++) e[ver[i]].clear();     stack<int> s;     s.push(ver[1]);     for (int i = 2; i <= cnter; i++) {       int v = ver[i];       while (!compare(s.top(), v)) s.pop();       int u = s.top();       if (important[u] && important[v] && u == p[v][0]) return 0;       s.push(v);       e[u].push_back(v);     }     return 1;   }   void dfsSolve(int u) {     int cnter = 0;     for (int i = 0; i < e[u].size(); i++) {       int v = e[u][i];       dfsSolve(v);       cnter += dp[v];     }     if (important[u]) {       ans += cnter;       dp[u] = 1;     } else {       if (cnter >= 2)         ans++, dp[u] = 0;       else if (cnter == 1)         dp[u] = 1;       else         dp[u] = 0;     }   }   int main() {     memset(h, 0, sizeof(h));     memset(tin, 0, sizeof(tin));     memset(tout, 0, sizeof(tout));     memset(dp, 0, sizeof(dp));     memset(p, 0, sizeof(p));     memset(important, 0, sizeof(important));     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       e[u].push_back(v);       e[v].push_back(u);     }     dfs(1);     int q;     cin >> q;     while (q--) {       cin >> k;       for (int i = 1; i <= k; i++) {         cin >> ver[i];         important[ver[i]] = 1;         t[i] = ver[i];       }       sort(ver + 1, ver + k + 1, cmp);       if (!buildGraph()) {         cout << -1 << endl;       } else {         ans = 0;         dfsSolve(ver[1]);         cout << ans << endl;       }       for (int i = 1; i <= k; i++) important[t[i]] = 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string ar;   string br;   int n = 0, lst = 0, lstmin = 0, lp = 0, lpc = 0;   int gzg = 0, rzg = 0, gzy2 = 0, rzy2 = 0;   int gzf = 0, gzc = 0, rzf = 0, rzc = 0;   int gzcc = 0, rzff = 0;   int main() {     int f = 0;     cin >> n;     ar.reserve(n + 1);     br.reserve(n + 1);     cin >> ar;     for (f = 0; f < n; f++) {       if (lst == lstmin) lpc++;       if (ar[f] ==  ( )         lst++;       else         lst--;       if (lst < lstmin) {         lstmin = lst;         lp = f + 1;         lpc = 0;       }     }     if (lst != 0) {       cout <<  0 n1 1 n ;       return 0;     }     rzg = lpc;     gzcc = lp;     rzff = lp;     for (f = lp; f < lp + n * 2 + 2; f++) {       if (ar[f % n] ==  ( )         lst++;       else         lst--;       switch (lst) {         case 0:           if (gzy2 <= gzg) {             gzy2 = gzg;             gzc = (gzcc + 1) % n + 1;             gzf = f % n + 1;           }           gzg = 0;           gzcc = f % n;           break;         case 1:           gzg++;           if (rzy2 <= rzg) {             rzy2 = rzg;             rzf = rzff % n + 1;             rzc = f % n + 1;           }           rzff = f % n + 1;           rzg = lpc;           break;         case 2:           rzg++;           break;       }     }     if (rzy2 >= gzy2 && rzy2 > lpc) {       cout << rzy2 <<   n  << rzf <<     << rzc <<   n ;     } else if (gzy2 > rzy2 && gzy2 > lpc) {       cout << gzy2 <<   n  << gzf <<     << gzc <<   n ;     } else {       cout << lpc <<   n1 1 n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ToInt(string& s, int len) {     int r = 0;     for (int i = 0, max_ = (len); i < max_; ++i) {       r *= 10;       r += s[i] -  0 ;     }     return r;   }   int GCD(int a, int b) { return b != 0 ? GCD(b, a % b) : a; }   int LCM(int a, int b) { return a * (b / GCD(a, b)); }   long long Pow(long long n, long long e) {     if (e == 0) return 1;     if (e == 1)       return n;     else if (e & 1) {       long long t = Pow(n, e / 2);       return n * t * t;     } else {       long long t = Pow(n, e / 2);       return t * t;     }   }   int main() {     string s;     int N;     scanf( %d , &N);     getline(cin, s);     getline(cin, s);     int h = 0;     for (int i = 0, max_ = ((int)(s).size()); i < max_; ++i)       if (s[i] ==  H ) ++h;     int min = 10000;     for (int start = 0, max_ = ((int)(s).size()); start < max_; ++start) {       int swaps = 0;       for (int i = 0, max_ = (h); i < max_; ++i)         if (s[(start + i) % (int)(s).size()] ==  T ) ++swaps;       min = std::min(min, swaps);     }     printf( %d n , min);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cnt[(100000 + 10)];   int f[(100000 + 10)];   int main() {     int n;     cin >> n;     int ans = 0;     int mx = 0;     for (int i = 1; i <= n; i++) {       int v;       cin >> v;       cnt[f[v]]--;       f[v]++;       cnt[f[v]]++;       int ok = 0;       mx = max(mx, f[v]);       if (cnt[1] == i) {         ok = 1;       } else if (cnt[mx] == i) {         ok = 1;       } else if (cnt[mx] * mx == i - 1 && cnt[1] == 1) {         ok = 1;       } else if (cnt[mx - 1] * (mx - 1) == i - mx && cnt[mx] == 1) {         ok = 1;       }       if (ok) {         ans = i;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void amin(T &x, const T &y) {     if (y < x) x = y;   }   template <class T>   inline void amax(T &x, const T &y) {     if (x < y) x = y;   }   template <class Iter>   void rprintf(const char *fmt, Iter begin, Iter end) {     for (bool sp = 0; begin != end; ++begin) {       if (sp)         putchar(   );       else         sp = true;       printf(fmt, *begin);     }     putchar(  n );   }   int A, B, C;   int N;   void MAIN() {     scanf( %d%d%d%d , &A, &B, &C, &N);     int ans = 0;     for (int i = 0, i_len = (N); i < i_len; ++i) {       int x;       scanf( %d , &x);       if (B < x && x < C) ans++;     }     printf( %d n , ans);   }   int main() {     int TC = 1;     for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct art {     int t, d, p, id;   } a[101];   pair<int, int> go[101][2001];   bool cmp(art xx, art yy) { return xx.d < yy.d; }   int f[101][2001], num[101][2001], ans[1000], tt;   int main() {     int n;     cin >> n;     for (register int i = 1; i <= n; i++)       cin >> a[i].t >> a[i].d >> a[i].p, a[i].id = i;     sort(a + 1, a + n + 1, cmp);     for (register int i = 1; i <= n; i++)       for (register int k = 1; k <= 2000; k++) {         if (k <= a[i].d && k - a[i].t > 0) {           f[i][k] = f[i - 1][k - a[i].t] + a[i].p,           go[i][k] = make_pair(i - 1, k - a[i].t), num[i][k] = i;         }         if (f[i][k - 1] > f[i][k]) {           f[i][k] = f[i][k - 1];           go[i][k] = make_pair(i, k - 1), num[i][k] = -1;         }         if (f[i - 1][k] > f[i][k]) {           f[i][k] = f[i - 1][k];           go[i][k] = make_pair(i - 1, k), num[i][k] = -1;           ;         }       }     cout << f[n][2000] << endl;     int n1 = n, n2 = 2000;     while (go[n1][n2].first) {       if (num[n1][n2] != -1) ans[++tt] = a[num[n1][n2]].id;       pair<int, int> cur = go[n1][n2];       n1 = cur.first;       n2 = cur.second;     }     if (num[n1][n2] != -1 && num[n1][n2] != 0) ans[++tt] = a[num[n1][n2]].id;     cout << tt << endl;     for (register int i = tt; i; i--) cout << ans[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   int main() {     int i = 0, z, flag = 0, even = 0;     char str[100002], temp;     scanf( %s , str);     int size = strlen(str);     char x = str[size - 1];     for (i = 0; i < size; i++) {       if (str[i] % 2 == 0) {         even = 1;         if (str[i] < x) {           flag = 1;           str[size - 1] = str[i];           str[i] = x;           break;         } else {           z = i;         }       }     }     if (i >= size && even == 1) {       flag = 1;       str[size - 1] = str[z];       str[z] = x;     }     if (flag == 1) {       printf( %s , str);     } else       printf( -1 );     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   long long n;   bool valid(long long x) {     long long a = 0, b = 0;     long long r = n;     while (r) {       a += min(r, x);       r -= min(r, x);       b += r / 10;       r -= r / 10;     }     return a >= b;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long seed;     asm( rdtsc  :  =A (seed));     srand(seed);     cin >> n;     long long lo = 0, hi = n;     while (lo + 1 < hi) {       long long mid = (lo + hi) / 2;       if (valid(mid))         hi = mid;       else         lo = mid;     }     cout << lo + 1 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   string to_string(pair<A, B> p);   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p);   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p);   string to_string(const string& s) { return     + s +    ; }   string to_string(const char* s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   string to_string(vector<bool> v) {     bool first = true;     string res =  { ;     for (int i = 0; i < static_cast<int>(v.size()); i++) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(v[i]);     }     res +=  } ;     return res;   }   template <size_t N>   string to_string(bitset<N> v) {     string res =   ;     for (size_t i = 0; i < N; i++) {       res += static_cast<char>( 0  + v[i]);     }     return res;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto& x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ) ;   }   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   template <typename T>   T inverse(T a, T m) {     T u = 0, v = 1;     while (a != 0) {       T t = m / a;       m -= t * a;       swap(a, m);       u -= t * v;       swap(u, v);     }     assert(m == 1);     return u;   }   template <typename T>   class Modular {    public:     using Type = typename decay<decltype(T::value)>::type;     constexpr Modular() : value() {}     template <typename U>     Modular(const U& x) {       value = normalize(x);     }     template <typename U>     static Type normalize(const U& x) {       Type v;       if (-mod() <= x && x < mod())         v = static_cast<Type>(x);       else         v = static_cast<Type>(x % mod());       if (v < 0) v += mod();       return v;     }     const Type& operator()() const { return value; }     template <typename U>     explicit operator U() const {       return static_cast<U>(value);     }     constexpr static Type mod() { return T::value; }     Modular& operator+=(const Modular& other) {       if ((value += other.value) >= mod()) value -= mod();       return *this;     }     Modular& operator-=(const Modular& other) {       if ((value -= other.value) < 0) value += mod();       return *this;     }     template <typename U>     Modular& operator+=(const U& other) {       return *this += Modular(other);     }     template <typename U>     Modular& operator-=(const U& other) {       return *this -= Modular(other);     }     Modular& operator++() { return *this += 1; }     Modular& operator--() { return *this -= 1; }     Modular operator++(int) {       Modular result(*this);       *this += 1;       return result;     }     Modular operator--(int) {       Modular result(*this);       *this -= 1;       return result;     }     Modular operator-() const { return Modular(-value); }     template <typename U = T>     typename enable_if<is_same<typename Modular<U>::Type, int>::value,                        Modular>::type&     operator*=(const Modular& rhs) {       value = normalize(static_cast<int64_t>(value) *                         static_cast<int64_t>(rhs.value));       return *this;     }     template <typename U = T>     typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value,                        Modular>::type&     operator*=(const Modular& rhs) {       int64_t q = static_cast<int64_t>(static_cast<long double>(value) *                                        rhs.value / mod());       value = normalize(value * rhs.value - q * mod());       return *this;     }     template <typename U = T>     typename enable_if<!is_integral<typename Modular<U>::Type>::value,                        Modular>::type&     operator*=(const Modular& rhs) {       value = normalize(value * rhs.value);       return *this;     }     Modular& operator/=(const Modular& other) {       return *this *= Modular(inverse(other.value, mod()));     }     template <typename U>     friend const Modular<U>& abs(const Modular<U>& v) {       return v;     }     template <typename U>     friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);     template <typename U>     friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);     template <typename U>     friend std::istream& operator>>(std::istream& stream, Modular<U>& number);       private:     Type value;   };   template <typename T>   bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) {     return lhs.value == rhs.value;   }   template <typename T, typename U>   bool operator==(const Modular<T>& lhs, U rhs) {     return lhs == Modular<T>(rhs);   }   template <typename T, typename U>   bool operator==(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) == rhs;   }   template <typename T>   bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) {     return !(lhs == rhs);   }   template <typename T, typename U>   bool operator!=(const Modular<T>& lhs, U rhs) {     return !(lhs == rhs);   }   template <typename T, typename U>   bool operator!=(U lhs, const Modular<T>& rhs) {     return !(lhs == rhs);   }   template <typename T>   bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) {     return lhs.value < rhs.value;   }   template <typename T>   Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) += rhs;   }   template <typename T, typename U>   Modular<T> operator+(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) += rhs;   }   template <typename T, typename U>   Modular<T> operator+(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) += rhs;   }   template <typename T>   Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) -= rhs;   }   template <typename T, typename U>   Modular<T> operator-(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) -= rhs;   }   template <typename T, typename U>   Modular<T> operator-(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) -= rhs;   }   template <typename T>   Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) *= rhs;   }   template <typename T, typename U>   Modular<T> operator*(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) *= rhs;   }   template <typename T, typename U>   Modular<T> operator*(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) *= rhs;   }   template <typename T>   Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) /= rhs;   }   template <typename T, typename U>   Modular<T> operator/(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) /= rhs;   }   template <typename T, typename U>   Modular<T> operator/(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) /= rhs;   }   template <typename T, typename U>   Modular<T> power(const Modular<T>& a, const U& b) {     assert(b >= 0);     Modular<T> x = a, res = 1;     U p = b;     while (p > 0) {       if (p & 1) res *= x;       x *= x;       p >>= 1;     }     return res;   }   template <typename T>   bool IsZero(const Modular<T>& number) {     return number() == 0;   }   template <typename T>   string to_string(const Modular<T>& number) {     return to_string(number());   }   template <typename T>   std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {     return stream << number();   }   template <typename T>   std::istream& operator>>(std::istream& stream, Modular<T>& number) {     typename common_type<typename Modular<T>::Type, int64_t>::type x;     stream >> x;     number.value = Modular<T>::normalize(x);     return stream;   }   constexpr int md = 998244353;   using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;   vector<Mint> fact(1, 1);   vector<Mint> inv_fact(1, 1);   Mint C(int n, int k) {     if (k < 0 || k > n) {       return 0;     }     while ((int)fact.size() < n + 1) {       fact.push_back(fact.back() * (int)fact.size());       inv_fact.push_back(1 / fact.back());     }     return fact[n] * inv_fact[k] * inv_fact[n - k];   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     C(n + m, 0);     auto Prob = [&](int i, int p) {       Mint ret = fact[i] * inv_fact[p] * m * inv_fact[i + m] * fact[m - 1 + p];       return ret;     };     vector<Mint> dp(n + 1);     dp[0] = 0;     Mint c1 = 0;     Mint ci = 0;     for (int i = 0; i <= n; i++) {       Mint q = Mint(1) / (i + m + 1);       if (i > 0) {         dp[i] = c1 + ci * i;         dp[i] *= m * fact[i] * inv_fact[i + m] * q;         Mint rest = Prob(i, i);         dp[i] = (dp[i] + rest * (1 + q * (n - i))) / (1 - rest);       }       int p = i;       c1 += (dp[p] * (m + 1) + Mint(1 - p) * (n + m + 1)) * inv_fact[p] *             fact[m - 1 + p];       ci += (dp[p] + (n + m + 1)) * inv_fact[p] * fact[m - 1 + p];     }     cout << dp[n] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 7;   int T, n, t, a[N];   map<long long, long long> mp;   int main() {     scanf( %d , &T);     while (T--) {       mp.clear();       scanf( %d%d , &n, &t);       for (int i = 1; i <= n; ++i) {         scanf( %d , &a[i]);       }       if (t % 2 == 1) {         for (int i = 1; i <= n; ++i) {           mp[a[i]] = 1;           mp[t - a[i]] = 0;         }         for (int i = 1; i <= n; ++i) {           printf( %d  , mp[a[i]]);         }         printf(  n );       } else {         for (int i = 1; i <= n; ++i) {           mp[a[i]] = 1;           mp[t - a[i]] = 0;         }         int flag = 0;         for (int i = 1; i <= n; ++i) {           if (a[i] * 2 == t) {             printf( %d  , flag);             flag = 1 - flag;           } else {             printf( %d  , mp[a[i]]);           }         }         printf(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000010;   int dp[maxn][2];   int n, cnt = 1, head[maxn], to[maxn], nxt[maxn], val[maxn];   inline void add(int x, int y, int z) {     nxt[++cnt] = head[x];     head[x] = cnt;     to[cnt] = y;     val[cnt] = z;   }   void dfs(int x, int fa) {     for (int i = head[x]; i; i = nxt[i]) {       if (to[i] == fa) continue;       dfs(to[i], x);       dp[x][0] += dp[to[i]][0] + val[i];     }   }   void dfs1(int x, int fa) {     for (int i = head[x]; i; i = nxt[i]) {       if (to[i] == fa) continue;       dp[to[i]][1] = dp[x][0] - dp[to[i]][0] - val[i] + dp[x][1] + val[i ^ 1];       dfs1(to[i], x);     }   }   int st[maxn], top;   int main() {     cin >> n;     for (int i = 1, x, y; i < n; ++i) {       cin >> x >> y;       add(x, y, 0);       add(y, x, 1);     }     dfs(1, 1);     dfs1(1, 1);     int tot = 1e9;     for (int i = 1; i <= n; ++i) {       tot = min(tot, dp[i][0] + dp[i][1]);     }     for (int i = 1; i <= n; ++i) {       if (dp[i][0] + dp[i][1] == tot) {         st[++top] = i;       }     }     cout << tot << endl;     for (int i = 1; i <= top; ++i) {       cout << st[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int T;     cin >> T;     while (T--) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       int temp = 0;       if (a == b && b == c) {         printf( 0 n );       } else {         if (b > a) {           temp = b;           b = a;           a = temp;         }         if (c > a) {           temp = c;           c = a;           a = temp;         }         if (b >= c) {           printf( %d n , max(2 * (a - c) - 4, 0));         } else {           printf( %d n , max(2 * (a - b) - 4, 0));         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int fireball1, fireball2, health[100], attack[100], attack1[100];   int n, sum = 2e+8;   void dfs(int x, int y) {     if (y >= sum) return;     if (x == n) {       sum = y;       for (int i = 1; i <= n; i++) attack[i] = attack1[i];       return;     }     for (int i = 0; i <= 100; i++) {       if (y + i > sum) break;       if (health[x - 1] - fireball2 * i >= 0) continue;       if (x == n - 1)         if (health[n] - fireball2 * i >= 0) continue;       attack1[x] = i;       health[x - 1] -= fireball2 * i;       health[x] -= fireball1 * i;       health[x + 1] -= fireball2 * i;       dfs(x + 1, y + i);       health[x - 1] += fireball2 * i;       health[x] += fireball1 * i;       health[x + 1] += fireball2 * i;       if (health[x] - fireball1 * i < 0 && health[x + 1] - fireball2 * i < 0)         break;     }   }   int main() {     scanf( %d%d%d , &n, &fireball1, &fireball2);     for (int i = 1; i <= n; i++) scanf( %d , &health[i]);     dfs(2, 0);     printf( %d n , sum);     for (int i = 2; i < n; i++)       for (int j = 1; j <= attack[i]; j++) printf( %d  , i);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300000 + 3000;   int n, m;   int a[maxn], l[maxn], r[maxn], pos[maxn];   struct Segtree {     int l, r;     int val;   } node[maxn << 3];   inline int read() {     int x;     scanf( %d , &x);     return x;   }   inline void build(int p, int l, int r) {     node[p].l = l, node[p].r = r;     node[p].val = 0;     if (l == r) return;     int mid = (l + r) >> 1;     build(p << 1, l, mid);     build(p << 1 | 1, mid + 1, r);   }   inline void update(int p) {     node[p].val = node[p << 1].val + node[p << 1 | 1].val;   }   inline void change(int p, int vl, int vr, int c) {     if (node[p].l == vl && node[p].r == vr) {       node[p].val += c;       return;     }     int mid = (node[p].l + node[p].r) >> 1;     if (vr <= mid)       change(p << 1, vl, vr, c);     else       change(p << 1 | 1, vl, vr, c);     update(p);   }   inline int query(int p, int vl, int vr) {     if (node[p].l == vl && node[p].r == vr) return node[p].val;     int mid = (node[p].l + node[p].r) >> 1;     if (vr <= mid)       return query(p << 1, vl, vr);     else if (vl > mid)       return query(p << 1 | 1, vl, vr);     else       return query(p << 1, vl, mid) + query(p << 1 | 1, mid + 1, vr);   }   int main() {     n = read(), m = read();     build(1, 1, n + m);     for (int i = 1; i <= n; ++i)       change(1, m + i, m + i, 1), pos[i] = m + i, l[i] = r[i] = i;     for (int i = 1; i <= m; ++i) {       scanf( %d , &a[i]);       l[a[i]] = min(1, l[a[i]]);       r[a[i]] = max(r[a[i]], query(1, 1, pos[a[i]]));       change(1, pos[a[i]], pos[a[i]], -1);       pos[a[i]] = m - i + 1;       change(1, pos[a[i]], pos[a[i]], 1);     }     for (int i = 1; i <= n; ++i) {       r[i] = max(r[i], query(1, 1, pos[i]));       printf( %d %d n , l[i], r[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     char c = getchar();     int x = 0;     bool f = 0;     for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);     for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);     if (f) x = -x;     return x;   }   int n, m, k, s;   vector<int> e[200005];   vector<int> dfn, res[200005];   bool vis[200005];   void dfs(int u) {     vis[u] = 1;     dfn.push_back(u);     for (auto v : e[u])       if (!vis[v]) dfs(v), dfn.push_back(u);   }   signed main() {     n = read(), m = read(), k = read(), s = ceil(2.0 * n / k);     for (register int i = (1); i <= (m); ++i) {       int u = read(), v = read();       e[u].push_back(v), e[v].push_back(u);     }     dfs(1);     for (register int i = (0); i <= ((int)dfn.size() - 1); ++i)       res[i / s + 1].push_back(dfn[i]);     for (register int i = (1); i <= (k); ++i) {       if (!res[i].size()) res[i].push_back(1);       cout << res[i].size() <<    ;       for (auto x : res[i]) cout << x <<    ;       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct sb {     long long x;     long long y;     bool operator<(const sb &wakaka) const { return x < wakaka.x; }   } test;   set<sb> node;   int main() {     long long x;     cin >> x;     long long len = 2 * pow((double)x, (double)1 / (double)3);     long long cnt = 0;     long long n = 0;     while (1) {       n++;       if (n > len) break;       long long en = x * 6;       long long i2 = (n - 1) * n * (2 * n - 1);       en = en - i2;       long long he = (n - 1) * n * 3;       en = en + he * n;       long long la = 6 * n * n - he;       if (en % la == 0) {         long long ans = en / la;         test.x = n;         test.y = ans;         if (ans < n) break;         node.insert(test);         test.y = n;         test.x = ans;         node.insert(test);         cnt = cnt + 2;       }     }     cout << node.size() << endl;     for (set<sb>::iterator it = node.begin(); it != node.end(); it++) {       test = *it;       cout << test.x <<     << test.y << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace std;   int n, a, b, ans;   int main() {     cin >> n;     while (n--) {       cin >> a >> b;       a++, b--;       while (b /= 4) a++;       ans = max(ans, a);     }     cout << ans;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   using namespace std;   const int N = 30 + 7;   const int mod = 1e9 + 7;   const int B = 317;   int n;   int u, r;   int a[N][N];   int b[N];   int c[N];   int p[N];   long long foo(int x, bool f) {     long long s = -1e18;     if (x % 2 == 0) {       s = 0;       for (int i = 1; i <= n; i++) s += a[x][i] * 1LL * c[i];     }     if (x == 0) return s;     if (f) {       for (int i = 1; i <= n; i++) {         a[x - 1][i] = a[x][i] ^ b[i];       }       s = max(s, foo(x - 1, false));     }     for (int i = 1; i <= n; i++) {       a[x - 1][i] = a[x][p[i]] + r;     }     s = max(s, foo(x - 1, true));     return s;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> u >> r;     for (int i = 1; i <= n; i++) cin >> a[u][i];     for (int i = 1; i <= n; i++) cin >> b[i];     for (int i = 1; i <= n; i++) cin >> c[i];     for (int i = 1; i <= n; i++) cin >> p[i];     cout << foo(u, true);   }


#include <bits/stdc++.h>   using namespace std;   long long ans;   int n, l, w, x, pn;   vector<int> pos, neg;   int cal(long long a, int b) {     if (b == 0) return (a > 0 ? INT_MAX : -INT_MAX);     if (a < 0) {       if (a % b != 0) return (a / b - 1);     }     return a / b;   }   int main() {     ios::sync_with_stdio(false);     scanf( %d%d%d , &n, &l, &w);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &x, &pn);       if (pn == -1)         neg.push_back(x);       else         pos.push_back(x);     }     sort(neg.begin(), neg.end());     sort(pos.begin(), pos.end());     for (int i = 0; i < neg.size(); i++) {       int v = neg[i];       auto now = lower_bound(pos.begin(), pos.end(), -v - l);       int max1 = cal((long long)(v + l) * (w + 1) - 1, w - 1);       int max2 = cal((long long)(v + l) * (w - 1) - 1, w + 1);       ans += (upper_bound(pos.begin(), now, max1) - pos.begin()) +              (upper_bound(now, pos.end(), min(max2, v)) - now);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<pair<int, int>, int> mp;   set<int> s;   int m;   pair<int, int> p[100001];   long long ans;   const long long md = 1e9 + 9;   int up[3][2] = {-1, 1, 0, 1, 1, 1};   int dwn[3][2] = {-1, -1, 0, -1, 1, -1};   int cnt(int ind) {     int res = 0;     int x = p[ind].first, y = p[ind].second;     for (int i = 0; i < 3; i++) {       int xx = x + dwn[i][0], yy = y + dwn[i][1];       if (mp.find(make_pair(xx, yy)) != mp.end()) res++;     }     return res;   }   bool chk(int ind) {     int x = p[ind].first, y = p[ind].second;     for (int i = 0; i < 3; i++) {       int xx = x + up[i][0], yy = y + up[i][1];       if (mp.find(make_pair(xx, yy)) != mp.end()) {         int d = mp[make_pair(xx, yy)];         if (cnt(d) <= 1) {           return false;         }       }     }     return true;   }   void work(int ind) {     ans = (ans * (long long)m % md + ind) % md;     int x = p[ind].first, y = p[ind].second;     mp.erase(make_pair(x, y));     for (int i = 0; i < 3; i++) {       int xx = x + dwn[i][0], yy = y + dwn[i][1];       if (mp.find(make_pair(xx, yy)) != mp.end()) {         int d = mp[make_pair(xx, yy)];         if (chk(d)) {           s.insert(d);         }       }     }   }   void mx() {     set<int>::iterator it;     int x;     while (!s.empty()) {       it = s.end();       it--;       if (chk(*it)) {         x = *it;         s.erase(it);         break;       }       s.erase(it);     }     work(x);   }   void mn() {     set<int>::iterator it;     int x;     while (!s.empty()) {       it = s.begin();       if (chk(*it)) {         x = *it;         s.erase(it);         break;       }       s.erase(it);     }     work(x);   }   int main() {     scanf( %d , &m);     for (int i = 0; i < m; i++) {       scanf( %d%d , &p[i].first, &p[i].second);       mp[make_pair(p[i].first, p[i].second)] = i;       s.insert(i);     }     for (int i = 0; i < m; i++) {       if (i & 1)         mn();       else         mx();     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 50050;   const long long bign = 55000;   long long n, m;   long long tmp[bign], judge[bign];   long long ans, head[maxn], sz[maxn], maxp[maxn];   long long dis[bign], size;   long long q[bign], targetnum;   long long rt, tot, vis[maxn];   struct edge {     long long next, to, dis;   } e[maxn * 2];   inline void addedge(long long next, long long to, long long dis) {     e[++size].to = to;     e[size].dis = dis;     e[size].next = head[next];     head[next] = size;   }   void getzx(long long t, long long fat) {     long long i, j;     maxp[t] = sz[t] = 1;     for (i = head[t]; i; i = e[i].next) {       j = e[i].to;       if (j == fat || vis[j]) continue;       getzx(j, t);       sz[t] += sz[j];       maxp[t] = max(maxp[t], sz[j]);     }     maxp[t] = max(maxp[t], tot - sz[t]);     if (maxp[t] < maxp[rt]) rt = t;   }   inline void getdis(long long t, long long fat) {     long long i, j, k;     tmp[++tmp[0]] = dis[t];     for (i = head[t]; i; i = e[i].next) {       j = e[i].to;       k = e[i].dis;       if (j == fat || vis[j]) continue;       dis[j] = dis[t] + k;       getdis(j, t);     }   }   inline void calc(long long t) {     long long i, j, k, l, p = 0;     for (i = head[t]; i; i = e[i].next) {       j = e[i].to;       k = e[i].dis;       if (vis[j]) continue;       dis[j] = k;       tmp[0] = 0;       getdis(j, t);       for (k = tmp[0]; k; k--)         if (targetnum >= tmp[k]) ans += judge[targetnum - tmp[k]];       for (k = tmp[0]; k; k--) {         q[++p] = tmp[k];         judge[tmp[k]]++;       }     }     for (k = p; k; k--) judge[k] = 0;   }   inline void solve(long long t) {     long long i, j;     vis[t] = judge[0] = 1;     calc(t);     for (i = head[t]; i; i = e[i].next) {       j = e[i].to;       if (vis[j]) continue;       tot = sz[j];       maxp[rt = 0] = 0x3f3f3f3f;       getzx(j, 0);       solve(rt);     }   }   signed main() {     ios::sync_with_stdio(false);     register long long i, j;     cin >> n >> targetnum;     long long t1, t2;     for (i = 1; i < n; i++) {       cin >> t1 >> t2;       addedge(t1, t2, 1);       addedge(t2, t1, 1);     }     maxp[rt = 0] = 0x3f3f3f3f;     tot = n;     getzx(1, 0);     solve(rt);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 12, mod = 1e9 + 7;   long long dp[maxn][2][maxn];   int num[maxn];   long long dfs(int pos, bool bnd, int ned) {     if (ned < 0) return 0;     if (pos < 0) return ned == 0;     long long& ndp = dp[pos][bnd][ned];     if (~ndp) return ndp;     ndp = 0;     int bound = bnd ? num[pos] : 9;     for (int i = 0; i <= bound; i++)       ndp += dfs(pos - 1, bnd && i == bound, ned - (i == 4 || i == 7));     return ndp;   }   long long cnt[maxn];   int init(long long x) {     int len = 0;     while (x) {       num[len++] = x % 10;       x /= 10;     }     memset(dp, -1, sizeof(dp));     for (int i = len; i >= 0; i--) {       cnt[i] = dfs(len - 1, true, i);     }     cnt[0]--;     return len;   }   long long ans;   void dfs(int st, long long v, int len, int adder) {     if (st == 7) {       for (int i = adder + 1; i <= len; i++) {         (ans += v * cnt[i] % mod) %= mod;       }       return;     }     if (adder >= len) return;     for (int i = 0; i <= len; i++) {       if (cnt[i] == 0) continue;       long long mid = v * cnt[i] % mod;       cnt[i]--;       dfs(st + 1, mid, len, adder + i);       cnt[i]++;     }   }   int main() {     long long n;     scanf( %I64d , &n);     int len = init(n);     ans = 0;     dfs(1, 1, len, 0);     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[100][100];   bool dp[100][100][1000];   bool W[100][100][1000];   int main() {     int N;     int M;     int K;     scanf( %d %d %d , &N, &M, &K);     K++;     char ch[101];     for (int n = 0; n < N; n++) {       scanf( %s , ch);       for (int m = 0; m < M; m++) {         arr[n][m] = ch[m] -  0 ;       }     }     memset(dp, false, sizeof(dp));     for (int m = 0; m < M; m++) {       dp[N - 1][m][arr[N - 1][m]] = true;     }     for (int n = N - 2; n >= 0; n--) {       for (int m = 0; m < M; m++) {         int y[] = {n + 1, n + 1};         int x[] = {m - 1, m + 1};         for (int pos = 0; pos < 2; pos++) {           int Y = y[pos];           int X = x[pos];           if (Y >= 0 && Y < N && X >= 0 && X < M) {             for (int k = 0; k < 1000; k++) {               if (dp[Y][X][k]) {                 dp[n][m][k + arr[n][m]] = true;                 W[n][m][k + arr[n][m]] = pos;               }             }           }         }       }     }     int sol = -1;     int pos = -1;     for (int k = 0; k < 1000; k += K) {       for (int m = 0; m < M; m++) {         if (dp[0][m][k]) {           sol = k;           pos = m;         }       }     }     printf( %d n , sol);     if (sol != -1) {       int help = sol;       stack<int> st;       for (int n = 0; n < N - 1; n++) {         st.push(W[n][pos][help]);         if (W[n][pos][help] == 0) {           help -= arr[n][pos];           pos--;         } else {           help -= arr[n][pos];           pos++;         }       }       printf( %d n , pos + 1);       while (!st.empty()) {         if (st.top() == 0) {           printf( R );         } else {           printf( L );         }         st.pop();       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long len;   string a, b;   const long long mo = 1000000007;   long long good[200001], bad[200001];   int x, y, n;   int main() {     cin >> a >> b >> n;     if (a.length() != b.length()) {       cout << 0;       return 0;     }     len = a.length();     bool f;     for (int i = 0; i < len; i++) {       f = false;       for (int j = 0; j < len; j++) {         if (b[j] != b[(j + i) % len]) {           f = true;           break;         }       }       if (f == false) {         x++;       } else {         y++;       }     }     if (a == b) {       good[0] = 1;       bad[0] = 0;     } else {       good[0] = 0;       bad[0] = 1;     }     f = false;     b = b + b;     bool p;     for (int i = 0; i < len; i++) {       p = false;       for (int j = 0; j < len; j++) {         if (b[j + i] != a[j]) {           p = true;           break;         }       }       if (p == false) {         f = true;         break;       }     }     if (f == false) {       cout << 0;       return 0;     }     for (int i = 1; i <= n; i++) {       good[i] =           ((good[i - 1] % mo) * ((x - 1) % mo) + (bad[i - 1] * x) % mo) % mo;       bad[i] =           ((good[i - 1] % mo) * (y % mo) + (bad[i - 1] % mo) * ((y - 1) % mo)) %           mo;     }     for (int i = 1; i <= n; i++) {     }     cout << good[n] % mo;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 5;   unordered_map<bitset<maxn>, int> ma, flag;   bitset<maxn> s[35];   int t1, t2;   void dfs1(bitset<maxn> z, int cost, int pos) {     if (pos > t1 + t2) {       if (flag[z] == 0)         ma[z] = cost;       else         ma[z] = min(ma[z], cost);       flag[z] = 1;       return;     }     dfs1(z ^ s[pos], cost + 1, pos + 1);     dfs1(z, cost, pos + 1);   }   int dfs2(bitset<maxn> z, int cost, int pos) {     int res = 1e9;     if (flag[z] != 0) res = cost + ma[z];     if (pos <= t1) {       res = min(res, dfs2(z ^ s[pos], cost + 1, pos + 1));       res = min(res, dfs2(z, cost, pos + 1));     }     return res;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m, d;     cin >> n >> m >> d;     t1 = m / 3;     t2 = m - t1;     for (int i = 1; i <= m; i++) {       int cnt;       cin >> cnt;       for (int j = 1; j <= cnt; j++) {         int x;         cin >> x;         s[i][x] = 1;       }     }     bitset<maxn> tmp;     dfs1(tmp, 0, t1 + 1);     while (d--) {       int cnt;       cin >> cnt;       bitset<maxn> q;       for (int i = 1; i <= cnt; i++) {         int x;         cin >> x;         q[x] = 1;       }       int ans = dfs2(q, 0, 1);       if (ans == 1e9)         cout << -1 <<   n ;       else         cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   constexpr int maxn = 3e3 + 4;   struct Node {     int la, nx;   } nodes[maxn];   int X[maxn], Y[maxn];   std::vector<int> yp[maxn];   int R, C, N, K;   int ordX[maxn], ord[maxn];   int ans[maxn], xans = 0;   inline void update(int x) {     int c = x;     for (int k = 1; k < K && c; ++k) c = nodes[c].nx;     xans -= ans[x];     if (!nodes[x].la)       ans[x] = X[c] * (R + 1 - X[x]);     else       ans[x] = X[c] * (X[nodes[x].la] - X[x]);     xans += ans[x];   }   int main() {     cin >> R >> C >> N >> K;     for (int i = 1; i <= N; ++i) {       cin >> X[i] >> Y[i];       yp[Y[i]].push_back(i);     }     iota(ordX + 1, ordX + N + 1, 1);     sort(ordX + 1, ordX + N + 1, [&](int a, int b) { return X[a] > X[b]; });     long long rans = 0;     for (int ups = 1; ups <= C; ++ups) {       xans = 0;       memset(ans, 0, sizeof(ans));       int cn = 0;       for (int i = 1; i <= N; ++i) {         if (Y[ordX[i]] >= ups) ord[++cn] = ordX[i];       }       memset(nodes, 0, sizeof(nodes));       for (int i = 1; i <= cn; ++i) {         nodes[ord[i]].la = ord[i - 1];         nodes[ord[i - 1]].nx = ord[i];       }       for (int i = 1; i <= cn; ++i) update(ord[i]);       rans += xans;       for (int i = C; i > ups; --i) {         for (int j : yp[i]) {           nodes[nodes[j].la].nx = nodes[j].nx;           nodes[nodes[j].nx].la = nodes[j].la;         }         for (int j : yp[i]) {           for (int k = 1, c = nodes[j].la; k < K && c; ++k, c = nodes[c].la)             update(c);           if (nodes[j].nx) update(nodes[j].nx);           xans -= ans[j];         }         rans += xans;       }     }     cout << rans << endl;     return 0;   }