#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     scanf( %d%d , &n, &m);     if (m < n - 1 || m > 2 * (n + 1)) {       printf( -1 n );     } else if (m == n || m == n - 1) {       int flg = 0;       while (m || n) {         if (!flg) {           printf( 0 );           n--;           flg = 1;         } else {           printf( 1 );           m--;           flg = 0;         }       }       printf(  n );     } else if (m == n + 1) {       while (n) {         printf( 10 );         n -= 1;       }       printf( 1 n );     } else {       while (m != n && n > 0) {         printf( 110 );         m -= 2;         n -= 1;       }       if (n == 0) {         while (m--) {           printf( 1 );         }       } else {         while (n--) {           printf( 10 );         }       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 99999999999999999;   long long n, value[200000], zarib[200000], LCM = 1, GCD, x = INF, ans, sum;   vector<int> Tree[200000];   long long gcd(long long l, long long r) {     if (!l) return r;     return gcd(r % l, l);   }   bool dfs(int v, int last, long long z) {     zarib[v] = z;     if (v && Tree[v].size() == 1 && zarib[v] > sum) return 0;     z *= (Tree[v].size() - 1);     if (!z) z = 1;     for (int i = 0; i < Tree[v].size(); i++)       if (Tree[v][i] != last)         if (!dfs(Tree[v][i], v, z)) return 0;     return 1;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> value[i];       sum += value[i];     }     for (int i = 1; i < n; i++) {       int l, r;       cin >> l >> r;       Tree[l - 1].push_back(r - 1);       Tree[r - 1].push_back(l - 1);     }     if (!dfs(0, -1, 1)) {       cout << sum << endl;       return 0;     }     for (int i = 1; i < n; i++)       if (Tree[i].size() == 1) {         GCD = gcd(LCM, zarib[i]);         LCM /= GCD;         LCM *= zarib[i];       }     for (int i = 1; i < n; i++)       if (Tree[i].size() == 1) {         zarib[i] = LCM / zarib[i];         x = min(x, value[i] / zarib[i]);       }     for (int i = 1; i < n; i++)       if (Tree[i].size() == 1) ans += value[i] - zarib[i] * x;     cout << ans << endl;   }


#include <bits/stdc++.h>   const int maxn = 100001;   int n, i, u, v, top, a[maxn], spt[maxn], vt[maxn], st[maxn], l[maxn];   long long sum, ans, tmp, val[maxn], uc, bc, ss, min;   std::vector<int> adj[maxn];   void dfs() {     top = 1;     st[1] = 1;     while (top > 0) {       u = st[top];       if (vt[u] < spt[u] && adj[u][vt[u]] == st[top - 1]) vt[u]++;       if (vt[u] < spt[u]) {         if (u == 1)           tmp *= spt[u];         else           tmp *= (spt[u] - 1);         if (tmp > sum) {           ans = -1;           return;         }         st[++top] = adj[u][vt[u]];         vt[u]++;       } else {         if (spt[u] == 1 && u != 1) {           val[u] = tmp;           uc = tmp;           l[++l[0]] = u;         }         top--;         if (st[top] == 1)           tmp /= spt[st[top]];         else           tmp /= (spt[st[top]] - 1);       }     }   }   long long ucln(long long a, long long b) {     while (a > 0 && b > 0)       if (a > b)         a %= b;       else         b %= a;     if (a > 0)       return a;     else       return b;   }   int main() {     std::cin >> n;     for (i = 1; i <= n; i++) {       std::cin >> a[i];       sum += a[i];     }     for (i = 1; i < n; i++) {       std::cin >> u >> v;       spt[u]++;       spt[v]++;       adj[u].push_back(v);       adj[v].push_back(u);     }     tmp = 1;     dfs();     if (ans == -1) {       std::cout << sum;       return 0;     }     bc = val[l[1]];     for (i = 2; i <= l[0]; i++) {       uc = ucln(bc, val[l[i]]);       bc /= uc;       if (bc > double(sum) / val[i]) {         std::cout << sum;         return 0;       }       bc *= val[l[i]];     }     min = 1000000000000000000;     for (i = 1; i <= l[0]; i++) {       val[l[i]] = bc / val[l[i]];       if (min > a[l[i]] / val[l[i]]) min = a[l[i]] / val[l[i]];     }     if (min == 0) {       std::cout << sum;       return 0;     }     for (i = 1; i <= l[0]; i++) ans += a[l[i]] - val[l[i]] * min;     std::cout << ans;     return 0;   }


#include <bits/stdc++.h>   int value(const std::vector<int>& xs, int base, int i) {     return (xs[(base + i) % xs.size()] + 10 - xs[base]) % 10;   }   bool lt(const std::vector<int>& xs, int a, int b) {     int N = xs.size();     for (int i = 0; i < N; ++i) {       int va = value(xs, a, i), vb = value(xs, b, i);       if (va != vb) return va < vb;     }     return false;   }   int main() {     int N;     std::cin >> N;     std::string s;     std::cin >> s;     std::vector<int> xs(N), is(N);     for (int i = 0; i < N; ++i) xs[i] = s[i] -  0 , is[i] = i;     std::sort(is.begin(), is.end(), [&](int a, int b) { return lt(xs, a, b); });     for (int i = 0; i < N; ++i) std::cout << value(xs, is[0], i);     std::cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int long long n, a[60], mx = 2000;       scanf( %lld , &n);       for (int i = 0; i < n; i++) {         scanf( %lld , &a[i]);       }       sort(a, a + n);       for (int i = 1; i < n; i++) {         mx = min(mx, abs(a[i - 1] - a[i]));       }       printf( %lld n , mx);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   long long f[N], g[N], a[N], ans;   int n, F1, F2, F3, F4, F5, F6, k;   inline void upd(long long &x, long long y) {     if (x < y) x = y;   }   inline int calc(int y, int x) {     if (x * 9 < y) return -1e9;     if (y % 3 == 0) return y / 3;     if ((x - 1) * 9 >= y) return y / 3;     return (y / 9) * 3;   }   int main() {     scanf( %d , &n);     scanf( %d%d%d%d%d%d , &F1, &F2, &F3, &F4, &F5, &F6);     scanf( %d , &k);     scanf( %d , &k);     for (int i = 0; i <= 9; i++) f[i] = (long long)calc(i, n) * F6;     for (int i = 0; i < N; i++) g[i] = -1e18;     for (int i = 0; i <= 99; i++) a[i] = (long long)calc(i, n) * F5;     for (int i = 0; i <= 9; i++) {       const int x = i * 10;       for (int j = 0; j <= 99 - x; j++) upd(g[x + j], f[i] + a[j]);     }     for (int i = 0; i <= 99; i++) f[i] = g[i], g[i] = -1e18;     for (int i = 0; i <= 999; i++) a[i] = (long long)calc(i, n) * F4;     for (int i = 0; i <= 99; i++) {       const int x = i * 10;       for (int j = 0; j <= 999 - x; j++) upd(g[x + j], f[i] + a[j]);     }     for (int i = 0; i <= 999; i++) f[i] = g[i], g[i] = -1e18;     for (int i = 0; i <= 9999; i++) a[i] = (long long)calc(i, n) * F3;     for (int i = 0; i <= 999; i++) {       const int x = i * 10;       for (int j = 0; j <= 9999 - x; j++) upd(g[x + j], f[i] + a[j]);     }     for (int i = 0; i <= 9999; i++) f[i] = g[i], g[i] = -1e18;     for (int i = 0; i <= 99999; i++) a[i] = (long long)calc(i, n) * F2;     for (int i = 0; i <= 9999; i++) {       const int x = i * 10;       for (int j = 0; j <= 99999 - x; j++) upd(g[x + j], f[i] + a[j]);     }     for (int i = 0; i <= 99999; i++) f[i] = g[i], g[i] = -1e18;     for (int i = 0; i <= k; i++) a[i] = (long long)calc(i, n) * F1;     for (int i = 0; i <= k / 10; i++) upd(ans, a[k - i * 10] + f[i]);     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     while (b) {       int r = a % b;       a = b;       b = r;     }     return a;   }   int64_t lcm(int a, int b) { return 1LL * a * b / gcd(a, b); }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     vector<int> next(n + 1);     for (int i = 1; i <= n; i++) cin >> next[i];     int64_t ans = 1;     vector<bool> cycle(n + 1);     for (int i = 1; i <= n; i++)       if (!cycle[i]) {         int node = i;         vector<int> vis(n + 1);         for (int j = 0; j < n + 1; j++) {           vis[node]++;           node = next[node];         }         if (vis[i] >= 2) {           int len = 0, node = i;           while (!cycle[node]) {             len++;             cycle[node] = true;             node = next[node];           }           ans = lcm(ans, len);         }       }     int mx = 0;     for (int i = 1; i <= n; i++)       if (!cycle[i]) {         int dist = 0, node = i;         while (!cycle[node]) {           dist++;           node = next[node];         }         mx = max(mx, dist);       }     int it = 1;     while (it * ans < mx) it++;     cout << it * ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t = 1;     while (t--) {       int n, k, inits[26];       char str[101];       bool poss = false, started[26] = {false};       scanf( %d%s , &k, str);       n = strlen(str);       if (n < k) {         puts( NO );         break;       }       int cnt = 0;       for (int i = 0, j = 0; i < n; i++) {         if (!started[str[i] -  a ]) {           started[str[i] -  a ] = 1;           cnt++;           inits[j] = i;           j++;         }         if (cnt == k) {           poss = true;           break;         }       }       if (!poss)         puts( NO );       else {         puts( YES );         int i = 0;         for (int j = 0; j < n; j++) {           if (j == inits[i + 1] - 1) {             printf( %c n , str[j]);             i++;           } else             printf( %c , str[j]);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     return (a.second < b.second);   }   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int lcm(long long int a, long long int b) {     return a * b / gcd(a, b);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int flag0 = 0, flag2 = 0, sum = 0;       for (long long int i = 0; i < s.length(); i++) {         if ((s[i] ==  0 ) && (flag0 == 0))           flag0 = 1;         else if (int(s[i] -  0 ) % 2 == 0)           flag2 = 1;         sum += int(s[i] -  0 );       }       if ((flag0) && (flag2) && (sum % 3 == 0))         cout <<  red               <<   n ;       else         cout <<  cyan               <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 20;   const int mod = 998244353;   struct node {     int sum, tot;     node(){};     node(int S, int T) {       sum = S;       tot = T;     }     bool check() { return ~sum && ~tot; }   } f[2][2][N][1 << 10];   int A[N], ret, pw[30], k;   int lowbit(int x) { return x & -x; }   int ccount(int x) {     int cnt = 0;     if (!x) return 0;     while (x) {       x ^= lowbit(x);       cnt++;     }     return cnt;   }   node dfs(bool lim, bool zero, int pos, int st) {     if (!pos) return node(0, 1);     node& now = f[lim][zero][pos][st];     if (now.check()) return now;     now.sum = now.tot = 0;     for (int i = 0; i <= (lim ? A[pos] : 9); i++) {       if (ccount(zero && !i ? 0 : st | (1 << i)) <= k) {         node nxt = dfs(lim && i == A[pos], zero && !i, pos - 1,                        zero && !i ? 0 : st | (1 << i));         now.sum =             (now.sum + nxt.sum + 1ll * i * nxt.tot % mod * pw[pos - 1] % mod) %             mod;         now.tot = (now.tot + nxt.tot) % mod;       }     }     f[lim][zero][pos][st] = now;     return now;   }   int calc(long long x) {     ret = 0;     memset(f, -1, sizeof(f));     while (x) {       A[++ret] = x % 10;       x /= 10;     }     return dfs(1, 1, ret, 0).sum;   }   int main() {     pw[0] = 1;     for (int i = 1; i <= 20; i++) pw[i] = 10ll * pw[i - 1] % mod;     long long l, r;     cin >> l >> r >> k;     cout << (mod + calc(r) - calc(l - 1)) % mod;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   const int dx[] = {0, 0, -1, 1};   const int dy[] = {-1, 1, 0, 0};   char mat[N][N];   int dist[N][N][3];   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> mat[i][j];       }     }     for (int i = 0; i < 3; i++) {       list<pair<int, int> > q;       for (int x = 0; x < n; x++) {         for (int y = 0; y < m; y++) {           if (mat[x][y] ==  1  + i) {             dist[x][y][i] = 0;             q.emplace_back(x, y);           } else             dist[x][y][i] = -1;         }       }       while (!q.empty()) {         int x = q.front().first, y = q.front().second;         q.pop_front();         for (int j = 0; j < 4; j++) {           int tx = x + dx[j], ty = y + dy[j];           if (tx >= 0 && tx < n && ty >= 0 && ty < m && mat[tx][ty] !=  # ) {             int td = dist[x][y][i] + (mat[tx][ty] ==  . );             if (dist[tx][ty][i] == -1 || dist[tx][ty][i] > td) {               dist[tx][ty][i] = td;               if (mat[tx][ty] ==  . )                 q.emplace_back(tx, ty);               else                 q.emplace_front(tx, ty);             }           }         }       }     }     int ans = 1000000000;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (dist[i][j][0] != -1 && dist[i][j][1] != -1 && dist[i][j][2] != -1) {           ans = min(ans, dist[i][j][0] + dist[i][j][1] + dist[i][j][2] -                              2 * (mat[i][j] ==  . ));         }       }     }     cout << (ans == 1000000000 ? -1 : ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1100;   template <class nmsl>   inline void read(nmsl &x) {     x = 0;     char ch = getchar(), w = 0;     while (!isdigit(ch)) w = (ch ==  - ), ch = getchar();     while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     x = w ? -x : x;   }   long long n, m, a, b, c, val[N], tot, cnt, ans[N], tt;   int main() {     read(n);     for (register int i = 1; i <= n; i++) read(val[i]), tot += val[i];     tt = val[1];     for (register int i = 2; i <= n; i++)       if (val[i] * 2 <= val[1]) ans[++cnt] = i, tt += val[i];     if (tt * 2 > tot) {       cout << cnt + 1 << endl << 1 <<    ;       for (register int i = 1; i <= cnt; i++) cout << ans[i] <<    ;     } else       cout << 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 100;   const double PI = 3.14159;   vector<string> v;   long long n, m, k, d, ans[101], c, sum, a[105][105];   set<string> st;   string s, b;   bool flag;   set<string>::iterator it;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cin >> n;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         cin >> a[i][j];       }     }     for (int i = 0; i < n; i++) {       c = 0;       for (int j = 0; j < n; j++) {         if (i == j) continue;         if (a[i][j] == 0 || a[i][j] == 2) c++;       }       if (c == n - 1) {         sum++;         d = i + 1;         ans[d / 32] |= (1 << (d % 32));       }     }     cout << sum << endl;     for (int i = 1; i <= n; i++) {       if (ans[i / 32] & (1 << (i % 32))) cout << i <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long int n, o = 0, t = 0;     cin >> n;     for (long long int i = 0; i < n; i++) {       long long int x;       cin >> x;       if (x == 1)         o++;       else         t++;     }     if (t) {       cout << 2 <<    ;       t--;     }     if (o) {       cout << 1 <<    ;       o--;     }     while (t--) cout << 2 <<    ;     while (o--) cout << 1 <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   string grid[101];   int nb, l = 200, r = -1, u = 200, d = -1;   int main() {     scanf( %d %d , &n, &m);     for (int i = 0; i < n; i++) cin >> grid[i];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++)         if (grid[i][j] ==  B ) {           nb++;           l = min(l, j);           r = max(r, j);           u = min(u, i);           d = max(d, i);         }     }     if (nb == 0) {       cout << 1 << endl;       return 0;     }     int h = d - u + 1, w = r - l + 1;     if (w > h) {       if (w > n) {         cout << -1 << endl;         return 0;       }       cout << w * w - nb << endl;     } else {       if (h > m) {         cout << -1 << endl;         return 0;       }       cout << h * h - nb << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)5e5 + 50;   int n;   int ans[N];   vector<int> G[N];   int q[N];   int dep[N];   vector<pair<int, int> > hist[N];   void upd(int u, int val) { q[u] = max(q[u], val); }   void calc(int v, int p) {     dep[v] = 0;     for (int nxt : G[v]) {       if (nxt == p) continue;       calc(nxt, v);       dep[v] = max(dep[nxt] + 1, dep[v]);     }   }   void merge(vector<pair<int, int> > h1, vector<pair<int, int> > h2) {     int r = 0;     if (r < h2.size() && h2[r].second == 1) r++;     for (int i = 0; i < h1.size(); i++) {       if (h1[i].second == 1) continue;       while (r < h2.size() && (h2[r].first >= h1[i].first)) {         r++;       }       if (r - 1 >= 0 && h2[r - 1].second > 1)         upd(2 * h1[i].first, h2[r - 1].second + h1[i].second - 2);     }   }   void dfs(int v, int p = -1, int up_dep = -1) {     vector<int> deps;     if (up_dep != -1) deps.push_back(up_dep + 1);     for (int nxt : G[v]) {       if (nxt != p) deps.push_back(dep[nxt] + 1);     }     sort(deps.begin(), deps.end(),          [](const int a, const int b) { return a > b; });     for (int i = 0; i < deps.size(); i++) {       upd(2 * deps[i], i + 1);       upd(2 * deps[i] - 1, i + 1);       if (i > 0 && deps[i - 1] > deps[i]) upd(2 * deps[i] + 1, i + 1);     }     for (int i = 0; i < deps.size(); i++) {       if (hist[v].empty() || hist[v].back().first != deps[i])         hist[v].push_back({deps[i], 1});       else         hist[v].back().second++;     }     for (int i = 1; i < hist[v].size(); i++)       hist[v][i].second += hist[v][i - 1].second;     for (int nxt : G[v]) {       if (nxt == p) continue;       dfs(nxt, v,           (deps.size() == 1 ? 0 : (deps[0] == dep[nxt] + 1 ? deps[1] : deps[0])));       merge(hist[nxt], hist[v]);       merge(hist[v], hist[nxt]);     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       a--, b--;       G[a].push_back(b);       G[b].push_back(a);     }     for (int i = 0; i < n; i++) {       ans[1] = max(ans[1], (int)G[i].size() + 1);     }     calc(0, -1);     dfs(0);     int rmax = 0;     for (int i = (n - 1) / 2 * 2 + 1; i >= 2; i -= 2) {       rmax = max(rmax, q[i]);       ans[i] = rmax;     }     rmax = 0;     for (int i = n / 2 * 2; i >= 2; i -= 2) {       rmax = max(rmax, q[i]);       ans[i] = rmax;     }     for (int i = 1; i <= n; i++) cout << max(ans[i], 1) <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 0x3f3f3f3f3f3f3f3f;   int n, m, a[100010];   int tot[100010], Lpos = 1, Rpos = 0;   long long f[100010], g[100010], MO;   inline int read() {     int x = 0, w = 0;     char ch = 0;     while (!isdigit(ch)) {       w |= ch ==  - ;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     return w ? -x : x;   }   void add(int x) { MO += (tot[x]++); }   void del(int x) { MO -= (--tot[x]); }   long long calc(int l, int r) {     while (Lpos < l) del(a[Lpos++]);     while (Lpos > l) add(a[--Lpos]);     while (Rpos < r) add(a[++Rpos]);     while (Rpos > r) del(a[Rpos--]);     return MO;   }   void solve(int L, int R, int st, int ed) {     if (st > ed || L > R) return;     int M = (st + ed) >> 1;     M = max(M, L + 1);     long long temp = inf, pos = 0;     for (int i = L; i <= R; i++)       if (temp > g[i] + calc(i + 1, M)) temp = g[pos = i] + calc(i + 1, M);     f[M] = temp;     solve(L, pos, st, M - 1);     solve(pos, R, M + 1, ed);   }   int main() {     n = read();     m = read();     for (int i = 1; i <= n; i++) a[i] = read();     memset(g, 0x3f, sizeof g);     g[0] = 0;     while (m-- > 0) {       solve(0, n - 1, 1, n);       swap(f, g);     }     cout << g[n] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9;   const double eps = (double)1e-8;   const int mod = (int)1000000007;   const int maxn = (int)2 * 1e5 + 5;   int n, x, y, mx;   int u[maxn], pr[maxn];   pair<int, int> t[maxn];   vector<pair<int, int> > a[maxn];   vector<int> ans;   list<int> q;   list<int>::iterator it1;   void dzen(int v, int p) {     pr[v] = p;     if (a[v].size() > 1 || v == 1) {       q.push_back(v);       u[v] = 0;       if (a[v][0].first == p) u[v] = 1;     }     mx = max((int)a[v].size(), mx);     for (int i = (0); i < (a[v].size()); i++) {       if (a[v][i].first == p) continue;       dzen(a[v][i].first, v);     }     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (int i = (2); i <= (n); i++) {       cin >> x >> y;       a[x].push_back(make_pair(y, i - 1));       a[y].push_back(make_pair(x, i - 1));     }     mx = 0;     dzen(1, 0);     memset(t, -1, sizeof t);     cout << mx <<   n ;     while (mx--) {       ans.clear();       for (list<int>::iterator it = q.begin(); it != q.end();) {         if (t[pr[*it]].first == mx && t[pr[*it]].second == *it) {           it++;           continue;         }         t[*it] = make_pair(mx, a[*it][u[*it]].first);         ans.push_back(a[*it][u[*it]].second);         u[*it]++;         if (u[*it] < (int)a[*it].size() && a[*it][u[*it]].first == pr[*it])           u[*it]++;         if (u[*it] == (int)a[*it].size()) {           it1 = it;           it++;           q.erase(it1);         } else           it++;       }       cout << ans.size() <<    ;       for (int i = (0); i < (ans.size()); i++) {         cout << ans[i] <<    ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5;   int vl[N + 2], tim, st[N + 2];   int par[N + 2], level[N + 2];   int sp[N + 2][22], n, ed[N + 2];   long long seg[5 * N], lazy[5 * N];   vector<int> adj[N + 2];   void too_lazy(int node, int lo, int hi) {     seg[node] += (lazy[node] * 1LL * (hi - lo + 1));     if (lo != hi) {       lazy[node * 2] += lazy[node];       lazy[node * 2 + 1] += lazy[node];     }     lazy[node] = 0;   }   void upd(int node, int lo, int hi, int lt, int rt, int up) {     too_lazy(node, lo, hi);     if (lo > rt || hi < lt) return;     if (lo >= lt && hi <= rt) {       lazy[node] += up;       too_lazy(node, lo, hi);       return;     }     int md = (lo + hi) / 2;     upd(node * 2, lo, md, lt, rt, up);     upd(node * 2 + 1, md + 1, hi, lt, rt, up);     seg[node] = seg[node * 2] + seg[node * 2 + 1];   }   long long query(int node, int lo, int hi, int lt, int rt) {     too_lazy(node, lo, hi);     if (lo > rt || hi < lt) return 0;     if (lo >= lt && hi <= rt) return seg[node];     int md = (lo + hi) / 2;     long long p1 = query(node * 2, lo, md, lt, rt);     long long p2 = query(node * 2 + 1, md + 1, hi, lt, rt);     return p1 + p2;   }   void dfs(int node, int fro, int h) {     ++tim;     st[node] = tim;     upd(1, 1, n, st[node], st[node], vl[node]);     par[node] = fro;     level[node] = h;     for (auto xx : adj[node]) {       if (xx == fro) continue;       dfs(xx, node, h + 1);     }     ed[node] = tim;   }   void sparse(void) {     memset(sp, -1, sizeof(sp));     for (int i = 1; i <= n; i++) sp[i][0] = par[i];     for (int j = 1; j <= 20; j++) {       for (int i = 1; i <= n; i++) {         if (sp[i][j - 1] == -1) continue;         sp[i][j] = sp[sp[i][j - 1]][j - 1];       }     }   }   int lca(int u, int v) {     if (level[u] < level[v]) swap(u, v);     for (int i = 20; i >= 0; i--) {       if (sp[u][i] == -1) continue;       if (level[sp[u][i]] >= level[v]) u = sp[u][i];     }     if (u == v) return u;     for (int i = 20; i >= 0; i--) {       if (sp[u][i] == -1 || sp[v][i] == -1) continue;       if (sp[u][i] == sp[v][i]) continue;       u = sp[u][i];       v = sp[v][i];     }     return par[u];   }   bool is_in_path(int u, int v, int w) {     if (w == -1) return false;     if (level[w] < level[lca(u, v)]) return false;     if (lca(u, w) == w || lca(v, w) == w)       return true;     else       return false;   }   void update(int u, int v, int rt, int x) {     if (is_in_path(u, v, rt)) {       upd(1, 1, n, st[1], ed[1], x);       return;     }     int lc = lca(u, v);     for (int i = 20; i >= 0; i--) {       if (sp[rt][i] == -1) continue;       if (level[sp[rt][i]] < level[lc]) continue;       if (!is_in_path(u, v, sp[rt][i])) rt = sp[rt][i];     }     if (is_in_path(u, v, par[rt])) {       upd(1, 1, n, st[1], ed[1], x);       upd(1, 1, n, st[rt], ed[rt], -x);     } else       upd(1, 1, n, st[lc], ed[lc], x);   }   void query(int v, int rt) {     if (v == rt) {       cout << query(1, 1, n, 1, n) << endl;       return;     }     for (int i = 20; i >= 0; i--) {       if (sp[rt][i] == -1) continue;       if (level[sp[rt][i]] > level[v]) rt = sp[rt][i];     }     if (par[rt] == v)       cout << query(1, 1, n, 1, n) - query(1, 1, n, st[rt], ed[rt]) << endl;     else       cout << query(1, 1, n, st[v], ed[v]) << endl;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int q;     cin >> n >> q;     for (int i = 1; i <= n; i++) cin >> vl[i];     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs(1, -1, 1);     sparse();     int rt = 1;     while (q--) {       int ty;       cin >> ty;       if (ty == 1) {         int v;         cin >> v;         rt = v;       } else if (ty == 2) {         int u, v, x;         cin >> u >> v >> x;         update(u, v, rt, x);       } else {         int v;         cin >> v;         query(v, rt);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string k;   long long n;   long long Judge(int L, int R) {     int flag = 0;     for (int i = L; i <= R; i++) {       if (k[i] !=  0 ) flag = 1;     }     if (flag == 1 && k[L] ==  0 ) return -1;     long long ans = 0;     for (int i = L; i <= R; i++) {       ans = (ans * 10 + (k[i] -  0 ));     }     if (ans >= n) {       return -1;     } else       return ans;   }   int getL(int l, int r) {     for (int i = l; i <= r; i++) {       if (k[i] !=  0 ) return i;     }     return r;   }   int main() {     cin >> n >> k;     long long ans = 0;     int len = k.size();     long long base = 1;     int l = len - 1;     int r = len - 1;     long long val;     while (r >= 0) {       for (int i = 0; i <= r; i++) {         if (r - i > 17) continue;         val = 0;         for (int j = i; j <= r; j++) val = val * 10 + k[j] -  0 ;         if (val < n) {           l = i;           break;         }       }       while (l < r && k[l] ==  0 ) l++;       ans = ans + base * val;       base *= n;       r = l - 1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 5000 + 5;   const int lim = 1000 * 1000 * 1000;   const int inf = 1000 * 1000 * 1000 + 5;   int a[maxN];   vector<int> v[maxN];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     v[0].push_back(a[0]);     for (int i = 1; i < n; i++) {       int fl = 1;       for (int j = 0; j < v[i - 1].size(); j++) {         if (v[i - 1][j] < a[i])           v[i].push_back(v[i - 1][j]);         else {           if (fl) v[i].push_back(a[i]), fl = 0;           v[i].push_back(v[i - 1][j]);         }       }       if (fl) v[i].push_back(a[i]);     }     int ans = 0;     for (int i = 0; i < n; i++)       ans += v[i].size() - 1 -              (lower_bound(v[i].begin(), v[i].end(), a[i]) - v[i].begin());     int cnt = 0;     int answer = inf;     for (int i = 0; i < n; i++)       for (int j = i + 1; j < n; j++) {         if (a[i] < a[j]) continue;         int tmp = ans;         tmp -= v[i].size() - 1 -                2 * (lower_bound(v[i].begin(), v[i].end(), a[i]) - v[i].begin());         tmp -= v[j].size() - 1 -                2 * (lower_bound(v[j].begin(), v[j].end(), a[j]) - v[j].begin());         tmp += v[i].size() -                2 * (lower_bound(v[i].begin(), v[i].end(), a[j]) - v[i].begin());         tmp += v[j].size() -                2 * (lower_bound(v[j].begin(), v[j].end(), a[i]) - v[j].begin());         if (a[i] > a[j])           tmp--;         else           tmp++;         if (tmp < answer)           answer = tmp, cnt = 1;         else if (tmp == answer)           cnt++;       }     cout << answer <<     << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   const int INF = 1e9 + 7;   const int MAXN = 3e5 + 10;   using namespace std;   int main() {     int a, b, c, d;     cin >> a >> b >> c >> d;     for (int i = a; i <= 2 * a; i++) {       for (int j = b; j <= 2 * b; j++) {         for (int k = c; k <= 2 * c; k++) {           if (i > j && j > k && d <= i && d <= j && d <= k) {             if (k <= d + d && j > d + d && i > d + d) {               cout << i <<   n  << j <<   n  << k;               return 0;             }           }         }       }     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double EPS = 1e-9;   int T, n;   int num[1000015], p[1000015], cnt, sum[1000015];   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d , &n);       for (int i = 1; i <= n; ++i) {         scanf( %d , &p[i]);       }       num[cnt = 1] = 1;       for (int i = 2; i <= n; ++i) {         if (p[i] == p[i - 1]) {           ++num[cnt];         } else {           num[++cnt] = 1;         }       }       for (int i = 1; i <= cnt; ++i) {         sum[i] = sum[i - 1] + num[i];       }       sum[cnt + 1] = INF;       int g = 0, s = 0, b = 0;       for (int i = 1, j = 2, k = 3; k <= n; ++i) {         while (j <= n && sum[j] - sum[i] <= sum[i]) ++j;         while (k < n && sum[k + 1] <= sum[cnt] / 2) ++k;         if (j <= n && k <= n && sum[i] < sum[k] - sum[j] &&             sum[k] <= sum[cnt] / 2) {           g = sum[i];           s = sum[j] - sum[i];           b = sum[k] - sum[j];         } else {           break;         }       }       printf( %d %d %d n , g, s, b);     }     return 0;   }


#include <algorithm>   #include <iostream>   #include <map>   #include <vector>   using namespace std;      using LL = long long;   #define mk make_pair      const int MAX_N = 3E5 + 100;   const int MAX_LOG_N = 18 + 1;      int n, m;   vector<int> G[MAX_N];      //=================================      int log2Static[MAX_N];   void log2Init(int n) {       int w = 1, j = 0;       while (w <= n) {           for (int i = w; i < 2 * w && i <= n; ++i) log2Static[i] = j;           w *= 2;           j += 1;       }   }      int dep[MAX_N];   int anster[MAX_N][MAX_LOG_N];      void getAnster(int u, int fat) {       dep[u] = dep[fat] + 1;       anster[u][0] = fat;       for (int i = 1; i <= log2Static[dep[u]]; ++i) {           anster[u][i] = anster[anster[u][i - 1]][i - 1];       }          for (int v : G[u]) {           if (fat == v) continue;           getAnster(v, u);       }   }      int getLCA(int u, int v) {       if (dep[u] < dep[v]) swap(u, v);       while (dep[u] > dep[v]) {           u = anster[u][log2Static[dep[u] - dep[v]]];       }       if (u == v) return u;          for (int i = log2Static[dep[u]]; i >= 0;) {           if (anster[u][i] == anster[v][i]) {               i -= 1;           } else {               u = anster[u][i];               v = anster[v][i];           }       }       return anster[u][0];   }      //=================================      class Path {      public:       int c, ui, vi;   };      int curSon[MAX_N];   LL pathCnt[MAX_N];   Path path[MAX_N];      vector<int> lcaPath[MAX_N];   vector<int> sidePath[MAX_N];      LL ans;   void dfs(int u, int fat) {       for (int i : sidePath[u]) {           if (!path[i].ui)               path[i].ui = curSon[path[i].c];           else               path[i].vi = curSon[path[i].c];       }          for (int v : G[u]) {           if (fat == v) continue;           curSon[u] = v;           dfs(v, u);           pathCnt[u] += pathCnt[v];       }          pathCnt[u] -= lcaPath[u].size();          LL tmp1 = 0;       map<pair<int, int>, LL> tmp2;       for (int i : lcaPath[u]) {           int tmp = 0;           tmp += pathCnt[path[i].ui] ? pathCnt[path[i].ui] - 1 : 0;           tmp += pathCnt[path[i].vi] ? pathCnt[path[i].vi] - 1 : 0;           tmp1 += pathCnt[u] - tmp - 1;              //处理双重边情况           if (path[i].ui && path[i].vi) {               tmp2[mk(path[i].ui, path[i].vi)] += 1;           }       }          for (auto it : tmp2) {           tmp1 += it.second * (it.second - 1);       }          //特殊处理       for (int i : lcaPath[u]) {           if (path[i].ui) pathCnt[path[i].ui] -= 1;           if (path[i].vi) pathCnt[path[i].vi] -= 1;       }       pathCnt[u] -= lcaPath[u].size();       //仅统计向上路径的       for (int i : lcaPath[u]) {           tmp1 += pathCnt[u] - pathCnt[path[i].ui] - pathCnt[path[i].vi];       }          ans += tmp1 / 2;   }      int main() {       ios::sync_with_stdio(false);       cin >> n;       for (int i = 1; i < n; ++i) {           int u, v;           cin >> u >> v;           G[u].push_back(v);           G[v].push_back(u);       }       log2Init(n);       getAnster(1, 0);       // init          cin >> m;       for (int i = 0; i < m; ++i) {           int u, v;           cin >> u >> v;              pathCnt[u] += 1;           pathCnt[v] += 1;              path[i].c = getLCA(u, v);           if (path[i].c == u) u = 0;           if (path[i].c == v) v = 0;           if (u > v) swap(u, v);              lcaPath[path[i].c].push_back(i);           if (u) sidePath[u].push_back(i);           if (v) sidePath[v].push_back(i);       }          dfs(1, 0);          cout << ans << endl;          return 0;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   long long int a[100010];   int n;   int main() {     int i, j;     while (scanf( %d , &n) != EOF) {       for (i = 1; i <= n; i++) {         scanf( %lld , &a[i]);       }       sort(a + 1, a + 1 + n);       long long sum = a[1];       int ans = 1;       for (i = 2; i <= n; i++) {         if (a[i] >= sum) {           ans++;           sum += a[i];         }       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 105;   const double EPS = 1e-9;   int n, k;   double e;   int px0, py0;   int x[MAXN], y[MAXN];   double dp[MAXN][2];   double dist(int x1, int y1, int x2, int y2) {     return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));   }   double prob(int ind, double rad) {     double _dist = dist(x[ind], y[ind], px0, py0);     if (_dist <= rad) return 1;     return exp(1 - pow(_dist, 2) / pow(rad, 2));   }   bool check(double rad) {     for (int i = 0; i < MAXN; i++) dp[i][0] = dp[i][1] = 0;     dp[0][0] = 1;     for (int i = 0; i < n; i++) {       double p = prob(i, rad);       for (int j = 0; j < MAXN - 1; j++) {         dp[j + 1][1] += dp[j][0] * p;         dp[j][1] += dp[j][0] * (1 - p);       }       for (int j = 0; j < MAXN; j++) dp[j][0] = dp[j][1], dp[j][1] = 0;     }     double sum = 0;     for (int i = 0; i < k; i++) sum += dp[i][0];     return sum < e;   }   int main() {     ios ::sync_with_stdio(0);     if (fopen( input.txt ,  r )) freopen( input.txt ,  r , stdin);     cin >> n >> k;     int _e;     cin >> _e;     e = 1. * _e / 1000;     cin >> px0 >> py0;     for (int i = 0; i < n; i++) cin >> x[i] >> y[i];     double lo = 0, hi = 5000;     while (abs(hi - lo) > 2 * EPS) {       double mid = (lo + hi) / 2;       if (check(mid))         hi = mid;       else         lo = mid + EPS;     }     cout << fixed << setprecision(10) << lo << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   inline T1 max(T1 a, T2 b) {     return a < b ? b : a;   }   template <typename T1, typename T2>   inline T1 min(T1 a, T2 b) {     return a < b ? a : b;   }   const char lf =   n ;   namespace ae86 {   const int bufl = 1 << 15;   char buf[bufl], *s = buf, *t = buf;   inline int fetch() {     if (s == t) {       t = (s = buf) + fread(buf, 1, bufl, stdin);       if (s == t) return EOF;     }     return *s++;   }   inline int ty() {     int a = 0;     int b = 1, c = fetch();     while (!isdigit(c)) b ^= c ==  - , c = fetch();     while (isdigit(c)) a = a * 10 + c - 48, c = fetch();     return b ? a : -a;   }   inline double td() {     double a = 0, d = 0.1;     int b = 1, c = fetch();     while (!isdigit(c)) b ^= c ==  - , c = fetch();     while (isdigit(c)) a = a * 10 + c - 48, c = fetch();     if (c !=  . ) return b ? a : -a;     while (c = fetch(), isdigit(c)) a += d * (c - 48), d *= 0.1;     return b ? a : -a;   }   }  // namespace ae86   using ae86::td;   using ae86::ty;   const int _ = 23, __ = 1 << 20;   int n, nn, lim, bcnt[__] = {0};   double ps[_], sps[__] = {0}, f[__] = {0}, ans[_] = {0};   int main() {     ios::sync_with_stdio(0), cout.tie(nullptr);     n = ty(), nn = 1 << n, lim = ty();     int cnts = 0;     for (int i = 0; i < n; i++) ps[i] = td(), cnts += ps[i] > 0;     if (cnts < lim) {       for (int i = 0; i < n; i++) cout <<  01 [ps[i] > 0] <<    n [i == n - 1];       return 0;     }     for (int i = 0; i < nn; i++)       for (int j = 0; j < n; j++)         if ((((i) >> (j)) & 1)) sps[i] += ps[j];     bcnt[0] = 0;     for (int i = 1; i < nn; i++) bcnt[i] = bcnt[i >> 1] + (i & 1);     f[0] = 1;     for (int i = 0; i < nn; i++) {       if (bcnt[i] == lim)         for (int j = 0; j < n; j++)           if ((((i) >> (j)) & 1)) ans[j] += f[i];       if (bcnt[i] >= lim) continue;       for (int j = 0; j < n; j++)         if (!(((i) >> (j)) & 1)) f[i | (1 << (j))] += f[i] * ps[j] / (1 - sps[i]);     }     for (int i = 0; i < n; i++)       cout << setprecision(11) << fixed << ans[i] <<    n [i == n - 1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   template <typename T>   vector<T>& operator--(vector<T>& v) {     for (auto& i : v) --i;     return v;   }   template <typename T>   vector<T>& operator++(vector<T>& v) {     for (auto& i : v) ++i;     return v;   }   template <typename T>   istream& operator>>(istream& is, vector<T>& v) {     for (auto& i : v) is >> i;     return is;   }   template <typename T>   ostream& operator<<(ostream& os, vector<T> v) {     for (auto& i : v) os << i <<    ;     return os;   }   template <typename T, typename U>   pair<T, U>& operator--(pair<T, U>& p) {     --p.first;     --p.second;     return p;   }   template <typename T, typename U>   pair<T, U>& operator++(pair<T, U>& p) {     ++p.first;     ++p.second;     return p;   }   template <typename T, typename U>   istream& operator>>(istream& is, pair<T, U>& p) {     is >> p.first >> p.second;     return is;   }   template <typename T, typename U>   ostream& operator<<(ostream& os, pair<T, U> p) {     os << p.first <<     << p.second;     return os;   }   template <typename T, typename U>   pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {     return make_pair(a.first - b.first, a.second - b.second);   }   template <typename T, typename U>   pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {     return make_pair(a.first + b.first, a.second + b.second);   }   template <typename T, typename U>   void umin(T& a, U b) {     if (a > b) a = b;   }   template <typename T, typename U>   void umax(T& a, U b) {     if (a < b) a = b;   }   struct Node {     bool end = false;     map<char, Node*> to;   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     vector<string> res(n);     cin >> res;     set<string> ress(res.begin(), res.end());     Node* root = new Node();     for (string s : res) {       auto node = root;       for (char c : s) {         if (!node->to.count(c)) node->to[c] = new Node();         node = node->to[c];       }       node->end = true;     }     int m;     cin >> m;     vector<string> tokens0;     {       string s;       getline(cin, s);       for (int it = 0; it < m; ++it) {         getline(cin, s);         for (int i = 0; i < s.size(); ++i) {           if (s[i] ==  # ) {             s.resize(i);             break;           }         }         int last = 0;         for (int i = 0; i <= s.size(); ++i) {           if (i == s.size() || s[i] ==    ) {             if (last != i) {               tokens0.push_back(s.substr(last, i - last));             }             last = i + 1;           }         }       }     }     void(0);     vector<pair<string, int>> tokens;     auto is_let = [&](char c) {       if ( a  <= c && c <=  z ) return true;       if ( A  <= c && c <=  Z ) return true;       if ( 0  <= c && c <=  9 ) return true;       if (c ==  $ ) return true;       if (c ==  _ ) return true;       return false;     };     auto parse_tokens = [&](string s) {       vector<pair<string, int>> tokens;       int ind = 0;       while (ind < s.size()) {         int was = ind;         auto node = root;         int best_sz = 0;         for (int i = ind; i < s.size(); ++i) {           char c = s[i];           if (!node->to.count(c)) break;           node = node->to[c];           if (node->end) best_sz = i + 1 - ind;         }         string best_res = s.substr(ind, best_sz);         void(0);         if ( 0  <= s[ind] && s[ind] <=  9 ) {           int it = ind;           while (it < s.size() && s[it] >=  0  && s[it] <=  9 ) ++it;           void(0);           if (it - ind > best_res.size()) {             tokens.emplace_back(s.substr(ind, it - ind), 1);             ind = it;           } else {             tokens.emplace_back(best_res, 0);             ind += best_res.size();           }         } else {           int it = ind;           while (it < s.size() && is_let(s[it])) ++it;           void(0);           if (it - ind > best_res.size()) {             tokens.emplace_back(s.substr(ind, it - ind), 2);             ind = it;           } else {             tokens.emplace_back(best_res, 0);             ind += best_res.size();           }         }         if (ind <= was) {           return make_pair(tokens, false);         }       }       return make_pair(tokens, true);     };     for (auto s : tokens0) {       auto v = parse_tokens(s).first;       for (auto p : v) tokens.push_back(p);     }     set<pair<int, string>> avail_names;     for (int ln = 1; ln <= 3; ++ln) {       int cnt = 1;       for (int i = 0; i < ln; ++i) {         cnt *= 26;       }       for (int i = 0; i < cnt; ++i) {         string t;         int x = i;         for (int j = 0; j < ln; ++j) {           t.push_back( a  + x % 26);           x /= 26;         }         if (ress.count(t)) continue;         avail_names.emplace(ln, t);       }     }     map<string, string> names;     for (auto& [s, tp] : tokens) {       if (tp != 2) continue;       if (!names.count(s)) {         names[s] = avail_names.begin()->second;         avail_names.erase(avail_names.begin());       }       s = names[s];     }     void(0);     string curs;     for (int i = 0; i < tokens.size(); ++i) {       cout << tokens[i].first;       curs += tokens[i].first;       if (i + 1 < tokens.size()) {         auto v1 = parse_tokens(curs).first;         auto v2 = parse_tokens(tokens[i + 1].first).first;         for (auto v : v2) v1.push_back(v);         auto [v3, res] = parse_tokens(curs + tokens[i + 1].first);         if (!res || v1 != v3) {           cout <<    ;           curs.clear();         }       }     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( Ofast )   const long long maxn = 3000 + 10, N = 2e5 + 10, SQ = 320, base = 800287,                   mod = 1e9 + 7, INF = 1e18 + 10, lg = 22;   const long double eps = 1e-4;   long long n, m, q, dis[maxn][maxn], res[N];   long long V[N], U[N], r[N], s[N], t[N];   vector<long long> query[N];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m >> q;     for (long long i = 0; i < m; ++i) {       cin >> V[i] >> U[i];       V[i]--, U[i]--;     }     for (long long i = 0; i < n; ++i) {       for (long long j = 0; j < n; ++j) {         dis[i][j] = (i != j ? INF : m);       }     }     for (long long i = 0; i < q; ++i) {       long long l;       cin >> l >> r[i] >> s[i] >> t[i];       l--, r[i]--, s[i]--, t[i]--;       query[l].push_back(i);     }     for (long long idx = m - 1; idx >= 0; --idx) {       dis[V[idx]][U[idx]] = dis[U[idx]][V[idx]] = idx;       for (long long i = 0; i < n; ++i) {         long long val = min(dis[V[idx]][i], dis[U[idx]][i]);         dis[V[idx]][i] = dis[U[idx]][i] = val;       }       for (auto i : query[idx]) {         if (dis[s[i]][t[i]] > r[i]) {           res[i] = 0;         } else {           res[i] = 1;         }       }     }     for (long long i = 0; i < q; ++i) {       cout << (res[i] ?  Yes n  :  No n );     }   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX_N = 1e3 + 10, INF = 1e18;   long long n, m, k, a[MAX_N][MAX_N], noe[MAX_N];   long long lab[MAX_N], dp[MAX_N][MAX_N];   pair<long long, long long> tar[MAX_N];   void filldp() {     dp[m + n - 1][0] = 1;     for (int i = m + n - 2; i >= 1; i--) {       for (int j = 0; j <= i; j++) {         if (noe[i + 1] == 0) {           dp[i][j] = dp[i + 1][j - 1] + dp[i + 1][j + 1];         } else if (noe[i + 1] == 1) {           dp[i][j] = dp[i + 1][j + 1];         } else {           dp[i][j] = dp[i + 1][j - 1];         }         dp[i][j] = min(dp[i][j], INF);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     memset(lab, 63, sizeof lab);     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> a[i][j];         lab[i + j - 1] = min(lab[i + j - 1], a[i][j]);       }     }     for (int i = 1; i <= n + m - 1; i++) {       tar[i] = {lab[i], i};     }     sort(tar + 1, tar + n + m);     for (int i = 1; i <= n + m - 1; i++) {       noe[tar[i].second] = 1;       filldp();       if (dp[1][1] >= k) {       } else {         noe[tar[i].second] = 2;         k -= dp[1][1];       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (noe[i + j - 1] == 1) {           cout <<  ( ;         } else {           cout <<  ) ;         }       }       cout << endl;     }   }


#include <bits/stdc++.h>   long long srf(int a, int b, int c) {     std::cout <<  1   << (a + 1) <<     << (b + 1) <<     << (c + 1) << std::endl;     long long val;     std::cin >> val;     return val;   }   bool scl(int a, int b, int c) {     std::cout <<  2   << (a + 1) <<     << (b + 1) <<     << (c + 1) << std::endl;     int val;     std::cin >> val;     return val > 0;   }   void accept(std::vector<int> ans) {     std::cout <<  0  ;     for (int i : ans) {       std::cout << (i + 1) <<    ;     }     std::cout << std::endl;     return;   }   signed main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(0);     std::cout.tie(0);     int n;     std::cin >> n;     std::vector<int> ans(n);     std::iota(ans.begin(), ans.end(), 0);     int pos =         std::min_element(ans.begin() + 1, ans.end(),                          [&ans](int f, int s) { return scl(ans[0], f, s); }) -         ans.begin();     std::swap(ans[1], ans[pos]);     std::map<int, long long> map;     for (int i = 2; i < n; i++) {       map[ans[i]] = srf(ans[0], ans[1], ans[i]);     }     pos = std::max_element(ans.begin() + 1, ans.end(),                            [&map](int f, int s) { return map[f] < map[s]; }) -           ans.begin();     std::swap(ans.back(), ans[pos]);     std::vector<int> left, right;     for (int i = 2; i < (ans.size() - 1); i++) {       if (scl(ans[0], ans.back(), ans[i])) {         left.push_back(ans[i]);       } else {         right.push_back(ans[i]);       }     }     std::vector<int> ans2 = {ans[0], ans[1]};     std::sort(right.begin(), right.end(),               [&map](int f, int s) { return map[f] < map[s]; });     for (int i : right) {       ans2.push_back(i);     }     ans2.push_back(ans.back());     std::sort(left.begin(), left.end(),               [&map](int f, int s) { return map[f] < map[s]; });     std::reverse(left.begin(), left.end());     for (int i : left) {       ans2.push_back(i);     }     accept(ans2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> arr;   vector<long long int> b;   long long int l, f;   long long int mid;   long long int bin_s(long long int x) {     l = 0;     f = b.size() - 1;     while (l <= f) {       mid = (l + f) / 2;       if (b[mid] > x) f = mid - 1;       if (b[mid] < x) l = mid + 1;       if (b[mid] == x) {         return 1;         break;       }     }     return 0;   }   int main() {     long long int n, i, j, k, q;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> q;       arr.push_back(q);     }     long long int min = arr[0];     for (i = 0; i < n; i++) {       if (arr[i] <= min) min = arr[i];     }     for (i = 0; i < n; i++) {       if (arr[i] == min) b.push_back(i);     }     long long int c;     long long int max = 0;     long long int ways;     for (i = 0; i < n; i++) {       c = bin_s(i);       if (c == 1) {         ways = min * n;       }       if (c == 0) {         if (b[b.size() - 1] > i) ways = (min * n) + b[l] - i;         if (b[b.size() - 1] < i) ways = (min * n) + n - i + b[0];       }       if (ways > max) max = ways;     }     cout << max << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace std;   long long powm(long long base, long long exp, long long mod = 1000000007) {     long long ans = 1;     while (exp) {       if (exp & 1) ans = (ans * base) % mod;       exp >>= 1, base = (base * base) % mod;     }     return ans;   }   long long n, u, v, a[200005], st[2][200005], en[2][200005], timer[2],       tour[2][200005], level[200005], parent[200005], ft[2][200005];   vector<long long> adj[200005];   void dfs(long long mode, long long curr, long long par, long long now) {     parent[curr] = par, level[curr] = now;     if (now == mode)       st[mode][curr] = (++timer[mode]), tour[mode][timer[mode]] = curr;     for (auto i : adj[curr])       if (i != par) dfs(mode, i, curr, now ^ 1);     if (now == mode) en[mode][curr] = timer[mode];   }   void update(long long mode, long long p, long long val) {     long long curr = p;     for (; curr <= 200005; curr += (curr & (-curr))) ft[mode][curr] += val;   }   void update(long long mode, long long a, long long b, long long val) {     update(mode, a, val), update(mode, b + 1, -val);   }   long long query(long long mode, long long idx) {     long long sum = 0, curr = idx;     for (; curr > 0; curr -= (curr & (-curr))) sum += ft[mode][curr];     return sum;   }   int main() {     long long type, x, val, m;     cin >> n >> m;     for (long long i = 1; i < n + 1; i++) cin >> a[i];     for (long long i = 0; i < n - 1; i++) {       cin >> u >> v;       adj[u].push_back(v), adj[v].push_back(u);     }     for (long long i = 0; i < 2; i++) dfs(i, 1, 0, 0);     while (m--) {       cin >> type;       if (type == 1) {         cin >> x >> val;         long long mode = level[x], tmp = adj[x].size();         long long p = st[mode][x], q = en[mode][x], first = -1, last = -1;         update(mode, p, q, val);         for (auto i : adj[x])           if (i != parent[x]) {             first = i;             break;           }         for (long long i = tmp - 1; i >= 0; i--)           if (adj[x][i] != parent[x]) {             last = adj[x][i];             break;           }         if (first != -1) {           mode ^= 1;           p = st[mode][first], q = en[mode][last];           update(mode, p, q, -val);         }       } else {         cin >> x;         cout << query(level[x], st[level[x]][x]) + a[x] << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int h = -1, e = -1, l1 = -1, l2 = -1, o = -1;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  h ) {         h = i;         break;       }     }     if (h == -1)       cout <<  NO ;     else {       for (int j = h + 1; j < s.size(); j++) {         if (s[j] ==  e ) {           e = j;           break;         }       }       if (e == -1)         cout <<  NO ;       else {         for (int k = e + 1; k < s.size(); k++) {           if (s[k] ==  l ) {             l1 = k;             break;           }         }         if (l1 == -1)           cout <<  NO ;         else {           for (int l = l1 + 1; l < s.size(); l++) {             if (s[l] ==  l ) {               l2 = l;               break;             }           }           if (l2 == -1)             cout <<  NO ;           else {             for (int m = l2 + 1; m < s.size(); m++) {               if (s[m] ==  o ) {                 o = m;                 break;               }             }             if (o == -1)               cout <<  NO ;             else               cout <<  YES ;           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int h, m, n;   void input() { scanf( %d%d%d , &h, &m, &n); }   set<int> s;   int head[200010], tail[200010], cnt[200010], idx[200010], nid;   void build() {     for (int i = 0; i < h; i++) s.insert(i);     memset(idx, -1, sizeof(idx));     memset(tail, -1, sizeof(tail));     for (int i = 0; i < h; i++) {       int st = nid;       if (idx[i] == -1)         for (int j = i; idx[j] == -1; j = (j + m) % h) {           head[j] = st;           idx[j] = nid++;         }       int ed = nid;       for (int j = i; tail[j] == -1; j = (j + m) % h) tail[j] = ed;     }     for (int i = 0; i < h; i++) cnt[i] = tail[i] - head[i];   }   long long ans;   map<int, int> pos;   void add(int id, int hash) {     set<int>::iterator it = s.lower_bound(idx[hash]);     if (it == s.end() || *it >= tail[hash]) it = s.lower_bound(head[hash]);     ans += (*it - idx[hash] + cnt[hash]) % cnt[hash];     pos[id] = *it;     s.erase(*it);   }   void del(int id) { s.insert(pos[id]); }   void solve() {     char op;     int id, hash;     while (n--) {       scanf(  %c%d , &op, &id);       if (op ==  + ) {         scanf( %d , &hash);         add(id, hash);       } else if (op ==  - ) {         del(id);       } else         puts( QQ );     }     printf( %I64d n , ans);   }   int main() {     input();     build();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 1005;   string a[MaxN], start[MaxN], finish[MaxN], st[MaxN], Fans[MaxN];   int n, m, ind, Find;   int b[MaxN];   void check() {     int flag = 0, flag1 = 0, i, j;     ind = 0;     for (i = 1; i <= n; i++) {       if (b[i] == 1) {         ind++;         st[ind] = a[i];       }     }     for (i = 1; i <= m; i++) {       flag = 0;       for (j = 1; j <= ind; j++) {         if ((st[j] == start[i]) || (st[j] == finish[i])) {           flag++;         }       }       if (flag >= 2) {         flag1 = 1;         break;       }     }     if ((flag1 == 0) && (ind > Find)) {       for (i = 1; i <= ind; i++) {         Fans[i] = st[i];       }       Find = ind;     }   }   void rec(int i) {     if (i > n) {       check();       return;     }     b[i] = 0;     rec(i + 1);     b[i] = 1;     rec(i + 1);   }   int main() {     int i, j;     Find = 0;     cin >> n >> m;     for (i = 1; i <= n; i++) {       cin >> a[i];     }     for (i = 1; i <= m; i++) {       cin >> start[i] >> finish[i];     }     rec(1);     sort(Fans + 1, Fans + Find + 1);     cout << Find << endl;     for (i = 1; i <= Find; i++) {       cout << Fans[i] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   long long n, m, a, b, x, y;   int main() {     cin >> n >> m >> x >> y >> a >> b;     long long k = gcd(a, b);     long long kk = min(n / (a / k), m / (b / k));     a = a * kk / k, b = b * kk / k;     long long x1, x2, y1, y2;     x1 = x - (a + 1) / 2, x2 = x1 + a;     y1 = y - (b + 1) / 2, y2 = y1 + b;     if (x1 < 0) x1 = 0;     x2 = x1 + a;     x2 = min(n, x2), x1 = x2 - a;     if (y1 < 0) y1 = 0;     y2 = y1 + b;     y2 = min(m, y2), y1 = y2 - b;     cout << x1 <<     << y1 <<     << x2 <<     << y2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 20005;   const int len = 400;   const int mod = 1e9 + 7;   int n, p, q, ans[N];   struct node {     int c, h, t;   } a[N];   vector<int> all, in[N * 4];   vector<pair<int, int> > lazy[N];   void upd(int l, int r, int x, int v = 1, int tl = 0, int tr = all.size() - 1) {     if (tl > r || tr < l || l > r) return;     if (l <= tl && tr <= r) {       in[v].push_back(x);       return;     }     int tm = (tl + tr) / 2;     upd(l, r, x, v * 2, tl, tm);     upd(l, r, x, v * 2 + 1, tm + 1, tr);   }   void solve(vector<int> now, int v = 1, int tl = 0, int tr = all.size() - 1) {     for (auto id : in[v]) {       for (int i = 4000; i >= 0; i--) {         if (i - a[id].c >= 0) {           now[i] = max(now[i], now[i - a[id].c] + a[id].h);         }       }     }     if (tl == tr) {       for (auto i : lazy[all[tl]]) {         int id = i.second;         int w = i.first;         ans[id] = now[w];       }       return;     }     int tm = (tl + tr) / 2;     solve(now, v * 2, tl, tm);     solve(now, v * 2 + 1, tm + 1, tr);   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> p;     for (int i = 1; i <= n; i++) {       cin >> a[i].c >> a[i].h >> a[i].t;     }     cin >> q;     for (int i = 1; i <= q; i++) {       int x, y;       cin >> x >> y;       all.push_back(x);       lazy[x].push_back(make_pair(y, i));     }     sort(all.begin(), all.end());     all.resize(unique(all.begin(), all.end()) - all.begin());     for (int i = 1; i <= n; i++) {       int l = lower_bound(all.begin(), all.end(), a[i].t) - all.begin();       int r =           upper_bound(all.begin(), all.end(), a[i].t + p - 1) - all.begin() - 1;       if (l <= r) upd(l, r, i);     }     vector<int> now;     now.resize(4001);     solve(now);     for (int i = 1; i <= q; i++) cout << ans[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int tes;     cin >> tes;     while (tes--) {       long long int n, m;       cin >> m >> n;       vector<pair<long long int, long long int>> arr(n);       for (long long int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second;       sort(arr.begin(), arr.end());       vector<long long int> flower(n);       vector<long long int> cum(n + 1);       for (long long int i = 0; i < n; i++) flower[i] = arr[i].first;       cum[n] = 0;       for (int i = n - 1; i >= 0; i--) cum[i] = cum[i + 1] + flower[i];       long long int ans, here, left;       ans = 0;       m--;       for (long long int i = 0; i < n; i++) {         here = 0;         long long int key = arr[i].second;         long long int ix =             (upper_bound(flower.begin(), flower.end(), key) - flower.begin());         if (ix < n - m) ix = n - m;         here = here + cum[ix];         left = m - (n - ix);         here = here + left * key;         if (i < ix)           here = here + arr[i].first;         else           here = here + key;         ans = max(ans, here);       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   struct SegTree {     const int INF = 1e9;     vector<pair<int, int>> max_vc;     vector<int> add;     int h = 1;     pair<int, int> combine(pair<int, int> a, pair<int, int> b) {       if (a.first == b.first)         return {a.first, a.second + b.second};       else         return max(a, b);     }     void push(int i) {       max_vc[i].first += add[i];       if (i < h) {         add[2 * i] += add[i];         add[2 * i + 1] += add[i];       }       add[i] = 0;     }     SegTree(int n, int v) {       while (h < n) h <<= 1;       max_vc.resize(2 * h, {v, 1});       add.resize(2 * h, 0);       for (int i = h - 1; i > 0; --i)         max_vc[i] = combine(max_vc[2 * i], max_vc[2 * i + 1]);     }     void recAdd(int a, int b, int v, int i, int ia, int ib) {       push(i);       if (ib < a || b < ia) return;       if (a <= ia && ib <= b) {         add[i] += v;         push(i);       } else {         int im = (ia + ib) >> 1;         recAdd(a, b, v, 2 * i, ia, im);         recAdd(a, b, v, 2 * i + 1, im + 1, ib);         max_vc[i] = combine(max_vc[2 * i], max_vc[2 * i + 1]);       }     }     pair<int, int> recGet(int a, int b, int i, int ia, int ib) {       push(i);       if (ib < a || b < ia) return {-INF, 0};       if (a <= ia && ib <= b) return max_vc[i];       int im = (ia + ib) >> 1;       return combine(recGet(a, b, 2 * i, ia, im),                      recGet(a, b, 2 * i + 1, im + 1, ib));     }     void addInt(int a, int b, int v) { recAdd(a, b, v, 1, 0, h - 1); }     pair<int, int> getInt(int a, int b) { return recGet(a, b, 1, 0, h - 1); }   };   int pre[300005], b[300005];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     SegTree seg(n, 0);     vector<pair<int, int>> maxes;     long long res = 0;     for (int i = 1; i <= n; ++i) {       int a;       cin >> a;       pre[i] = max(pre[i - 1], b[a]);       b[a] = i;       int p = pre[i] + 1;       seg.addInt(p, i, 1);       seg.addInt(i, i, -a);       pair<int, int> maxes_add = {a, i};       while ((!maxes.empty()) && (maxes.back().first <= a)) {         seg.addInt(maxes.back().second, maxes_add.second - 1,                    -(a - maxes.back().first));         maxes_add.second = maxes.back().second;         maxes.pop_back();       }       maxes.push_back(maxes_add);       pair<int, int> sub = seg.getInt(p, i);       if (sub.first == 0) res += sub.second;     }     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 10;   const double Pi = acos(-1);   inline long long read() {     long long x = 0, f = 1, ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch - 48, ch = getchar();     return x * f;   }   long long n, q;   long long cnt, head[N], deep[N];   struct Edge {     long long to, nxt, dis;   } edge[N << 1];   inline void AddEdge(long long u, long long v, long long w) {     edge[++cnt] = (Edge){v, head[u], w};     head[u] = cnt;     return;   }   void dfs(long long u, long long fa) {     for (long long i = head[u], v; i; i = edge[i].nxt) {       v = edge[i].to;       if (v == fa) continue;       deep[v] = deep[u] + edge[i].dis;       dfs(v, u);     }     return;   }   long long dep[N], up[N][20];   void dfs1(long long u, long long fa) {     dep[u] = dep[fa] + 1, up[u][0] = fa;     for (long long i = 1; dep[u] >= (1 << i); ++i)       up[u][i] = up[up[u][i - 1]][i - 1];     for (long long i = head[u], v; i; i = edge[i].nxt) {       v = edge[i].to;       if (v == fa) continue;       dfs1(v, u);     }     return;   }   inline long long LCA(long long u, long long v) {     if (dep[u] < dep[v]) swap(u, v);     for (register long long i = 19; i >= 0; --i)       if (dep[up[u][i]] >= dep[v]) u = up[u][i];     if (u == v) return u;     for (register long long i = 19; i >= 0; --i)       if (up[u][i] != up[v][i]) u = up[u][i], v = up[v][i];     return up[u][0];   }   struct Node {     long long f, id;   } nd[N];   long long F[N], siz[N];   long long GetF(long long x) { return F[x] == x ? x : F[x] = GetF(F[x]); }   void Merge(long long x, long long y) {     x = GetF(x), y = GetF(y);     if (x == y) return;     if (siz[x] > siz[y]) swap(x, y);     siz[y] += siz[x];     F[x] = F[y];     return;   }   long long f[N];   bool cmp(Node x, Node y) { return x.f < y.f; }   signed main() {     n = read();     for (register long long i = 2; i <= n; ++i) {       long long a = read(), b = read(), w = read();       AddEdge(a, b, w), AddEdge(b, a, w);     }     dfs(1, 0);     long long L, R, mx = 0;     for (register long long i = 1; i <= n; ++i)       if (deep[i] > mx) mx = deep[i], L = i;     deep[L] = 0, mx = 0, dfs(L, 0);     for (register long long i = 1; i <= n; ++i)       if (deep[i] > mx) mx = deep[i], R = i;     dfs1(L, 0);     for (register long long i = 1; i <= n; ++i) {       nd[i].id = i;       f[i] = nd[i].f = max(deep[i] + deep[L] - 2 * deep[LCA(i, L)],                            deep[i] + deep[R] - 2 * deep[LCA(i, R)]);     }     q = read();     sort(nd + 1, nd + n + 1, cmp);     while (q--) {       long long val = read();       long long l = n + 1;       for (register long long i = 1; i <= n; ++i) F[i] = i, siz[i] = 1;       long long ans = 0;       for (register long long r = n; r >= 1; --r) {         while (nd[r].f - nd[l - 1].f <= val && l > 1) {           --l;           long long u = nd[l].id;           for (long long i = head[u], v; i; i = edge[i].nxt) {             v = edge[i].to;             if (f[v] >= nd[l].f && f[v] <= nd[r].f)               Merge(u, v), ans = max(ans, siz[GetF(u)]);           }         }         --siz[GetF(nd[r].id)];       }       printf( %lld n , max(1ll, ans));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int m;     cin >> m;     unsigned long long int A[m], Ap[m];     unsigned long long int B[m];     map<unsigned long long int, vector<unsigned long long int> > mp;     for (long long int i = 0; i < m; i++) {       cin >> B[i];     }     for (long long int i = 0; i < m; i++) {       cin >> A[i];       Ap[i] = A[i];     }     sort(Ap, Ap + m);     sort(B, B + m);     for (int i = 0; i < m; i++) {       mp[Ap[i]].push_back(B[m - 1 - i]);     }     for (long long int i = 0; i < m; i++) {       cout << mp[A[i]].back() <<    ;       mp[A[i]].pop_back();     }   }   int main() {     std::ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const MAX = 200 * 1000 + 41;   long double const EPS = 1e-12;   bool eq(long double a, long double b) { return abs(a - b) < EPS; }   bool leq(long double a, long double b) { return (a < b || eq(a, b)); }   int n, w;   int x[MAX];   int v[MAX];   pair<long double, long double> a[MAX];   pair<int, int> b[MAX];   int const ADD = 0;   int const ASK = 1;   struct Event {     int y, s, t;     Event(){};     Event(int y, int s, int t) : y(y), s(s), t(t){};     bool operator<(const Event &a) const {       if (t != a.t) return t < a.t;       return y < a.y;     };   };   vector<Event> e[MAX];   int gethash(vector<long double> &v, long double x) {     int l = -1;     int r = (int)v.size();     while (r - l > 1) {       int h = (l + r) / 2;       if (leq(v[h], x)) {         l = h;       } else {         r = h;       }     }     return l;   }   void init() {     vector<long double> tmp;     for (int i = 1; i <= n; i++) {       long double l = (long double)v[i] / x[i] - (long double)w / x[i];       long double r = (long double)v[i] / x[i] + (long double)w / x[i];       a[i] = make_pair(l, r);       tmp.push_back(l);       tmp.push_back(r);     }     sort(tmp.begin(), tmp.end());     for (int i = 1; i <= n; i++) {       b[i] = make_pair(gethash(tmp, a[i].first), gethash(tmp, a[i].second));     }     for (int i = 1; i <= n; i++) {       int x = b[i].first;       int y = b[i].second;       e[x].push_back(Event(y, 41, ADD));       e[x].push_back(Event(y, -1, ASK));       e[MAX - 1].push_back(Event(y, 1, ASK));     }   }   namespace Fen {   int F[MAX];   void upd(int x, int v) {     while (x < MAX) {       F[x] += v;       x |= (x + 1);     }   }   int get(int x) {     int res = 0;     while (x >= 0) {       res += F[x];       x = (x & (x + 1)) - 1;     }     return res;   }   };  // namespace Fen   long long ans;   void solve() {     init();     for (int i = 1; i <= n; i++) {       while (false) {       };     }     for (int i = 0; i <= MAX - 1; i++) {       sort(e[i].begin(), e[i].end());       for (Event z : e[i]) {         if (z.t == ADD) {           Fen::upd(z.y, 1);         } else {           int q = Fen::get(z.y);           ans += q * z.s;         }       }     }     unordered_map<int, int> ql;     for (int i = 1; i <= n; i++) {       ql[b[i].first]++;       ans += ql[b[i].first] - 1;     }     printf( %lld n , ans);   }   int main() {     scanf( %d %d , &n, &w);     for (int i = 1; i <= n; i++) {       scanf( %d %d , &x[i], &v[i]);     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int nax = 1e5 + 5;   vector<int> w[nax];   bool del[nax];   int legs[nax];   void dfs(int a, int par = 0) {     if (w[a].size() <= 2) {       del[a] = true;       for (int i = 0; i < w[a].size(); i++) {         int b = w[a][i];         if (b != par) dfs(b, a);       }     }   }   int main() {     int n, b;     scanf( %d , &n);     for (int i = 0; i < n - 1; ++i) {       int a, b;       scanf( %d%d , &a, &b);       w[a].push_back(b);       w[b].push_back(a);     }     for (int a = 1; a <= n; ++a)       if (w[a].size() == 1) dfs(a);     for (int a = 1; a <= n; ++a) {       for (int i = 0; i < w[a].size(); i++) {         b = w[a][i];         if (del[b]) legs[a] = min(legs[a] + 1, 2);       }     }     for (int a = 1; a <= n; ++a)       if (!del[a]) {         int cnt = 0;         for (int i = 0; i < w[a].size(); i++) {           b = w[a][i];           if (!del[b] && w[b].size() - legs[b] > 1) ++cnt;         }         if (cnt > 2) {           puts( No );           return 0;         }       }     puts( Yes );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int intmax = 0x3f3f3f3f;   const long long lldmax = 0x3f3f3f3f3f3f3f3fll;   double eps = 1e-8;   template <class T>   inline void checkmin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   inline void checkmax(T &a, T b) {     if (b > a) a = b;   }   template <class T>   inline T sqr(T x) {     return x * x;   }   template <class T>   inline T lowbit(T n) {     return (n ^ (n - 1)) & n;   }   template <class T>   inline int countbit(T n) {     return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));   }   template <class T>   inline T checkmod(T n, T m) {     return (n % m + m) % m;   }   template <class T>   inline T gcd(T a, T b) {     if (a < 0) return gcd(-a, b);     if (b < 0) return gcd(a, -b);     return (b == 0) ? a : gcd(b, a % b);   }   template <class T>   inline T euclid(T a, T b, T &x, T &y) {     if (a < 0) {       T d = euclid(-a, b, x, y);       x = -x;       return d;     }     if (b < 0) {       T d = euclid(a, -b, x, y);       y = -y;       return d;     }     if (b == 0) {       x = 1;       y = 0;       return a;     } else {       T d = euclid(b, a % b, x, y);       T t = x;       x = y;       y = t - (a / b) * y;       return d;     }   }   template <class T>   inline int dblcmp(T a, const T b) {     a -= b;     return a > eps ? 1 : (a < -eps ? -1 : 0);   }   template <class T>   inline int sgn(T a) {     return a > eps ? 1 : (a < -eps ? -1 : 0);   }   const int N = 1100000;   long long a[N], cnt[N];   long long ch[N];   int n, k;   bool check(int m) {     for (int i = 0; i < m; ++i) ch[i] = 0;     long long del = k;     for (int i = 0; i < n; ++i) {       ch[a[i] % m]++;       long long tmp = ch[a[i] % m];       if (tmp > 1) del--;       if (del < 0) break;     }     return del >= 0;   }   int main() {     cin >> n >> k;     if (n <= k + 1) {       cout << 1 << endl;       return 0;     }     for (int i = 0; i < n; ++i) cin >> a[i];     sort(a, a + n);     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j < n; ++j) {         cnt[a[j] - a[i]]++;       }     }     long long ans = N;     for (int i = 1; i < N; ++i) {       int tmp = 0;       for (int j = i; j < N; j += i) {         tmp += cnt[j];       }       if (tmp <= k * (k + 1) / 2 && check(i)) {         ans = i;         break;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = numeric_limits<int>::max();   const long long M = 998244353, MAXN = 1e6 + 10;   long long n, c[MAXN];   long long mp(long long a, long long e) {     long long ret = 1;     while (e) {       if (e % 2) ret = ret * a % M;       a = a * a % M;       e /= 2;     }     return ret;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0);     cin >> n;     c[0] = 1;     for (int i = 0; i < n; i++)       c[i + 1] = c[i] * (n - i) % M * mp(i + 1, M - 2) % M;     long long ans = 0;     for (int i = 1; i <= n; i++) {       long long add = (i + 1) % 2 ? -1 : 1;       add *= c[i];       add = add * mp(3, n * (n - i) + i) % M;       ans = (ans + add) % M;     }     ans = ans * 2 % M;     for (int i = 0; i < n; i++) {       long long add = (i + 1) % 2 ? -1 : 1;       add *= c[i];       long long inner = (mp(1 - mp(3, i), n) - mp(-mp(3, i), n)) % M;       add = 3 * add * inner % M;       ans = (ans + add) % M;     }     while (ans < 0) ans += M;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> m1;   map<int, string> m2;   pair<long long, long long> r[128];   long long H[128], V[128], B[128], S[128];   vector<int> g1[128], g2[128];   int G[128];   int main() {     memset(H, 0, sizeof H);     memset(V, 0, sizeof V);     memset(G, 0, sizeof G);     memset(B, 0, sizeof B);     memset(S, 0, sizeof S);     int N;     cin >> N;     string s;     getline(cin, s);     while (N--) {       getline(cin, s);       istringstream sin(s);       string s2;       sin >> s2;       if (s2 ==  Widget ) {         string name;         sin >> name;         name = name.substr(0, name.find( ( ) + 1);         istringstream sin2(s.substr(s.find( ( ) + 1));         int t1, t2;         char c;         sin2 >> t1 >> c >> t2;         name = name.substr(0, name.size() - 1);         int id = m1.size();         m1[name] = id;         m2[id] = name;         r[id] = make_pair(t1, t2);       } else if (s2 ==  VBox ) {         string name;         sin >> name;         int id = m1.size();         m1[name] = id;         m2[id] = name;         V[id] = 1;       } else if (s2 ==  HBox ) {         string name;         sin >> name;         int id = m1.size();         m1[name] = id;         m2[id] = name;         H[id] = 1;       } else if (s.find( set_border ) != string::npos) {         string name = s.substr(0, s.find( . ));         int id = m1[name];         istringstream sin2(s.substr(s.find( ( ) + 1));         int sz;         sin2 >> sz;         B[id] = sz;       } else if (s.find( set_spacing ) != string::npos) {         string name = s.substr(0, s.find( . ));         int id = m1[name];         istringstream sin2(s.substr(s.find( ( ) + 1));         int sz;         sin2 >> sz;         S[id] = sz;       } else if (s.find( pack ) != string::npos) {         string name1 = s.substr(0, s.find( . ));         string name2 = s.substr(s.find( ( ) + 1);         name2 = name2.substr(0, name2.size() - 1);         int id1 = m1[name1], id2 = m1[name2];         g1[id1].push_back(id2);         G[id1]++;         g2[id2].push_back(id1);       }     }     int left = m1.size();     int done[128];     memset(done, 0, sizeof done);     N = m1.size();     while (left) {       int cur = -1;       for (int i = 0; i < (N); i++)         if (done[i] == 0 && G[i] == 0) {           cur = i;         }       done[cur] = 1;       for (int i = 0; i < (g2[cur].size()); i++) G[g2[cur][i]]--;       left--;       long long x = 0, y = 0;       if (V[cur] && g1[cur].size()) {         for (int i = 0; i < (g1[cur].size()); i++) {           x = max(x, r[g1[cur][i]].first);           y += r[g1[cur][i]].second;         }         x += 2 * B[cur];         y += 2 * B[cur];         y += (g1[cur].size() - 1) * S[cur];         r[cur] = make_pair(x, y);       }       if (H[cur] && g1[cur].size()) {         for (int i = 0; i < (g1[cur].size()); i++) {           y = max(y, r[g1[cur][i]].second);           x += r[g1[cur][i]].first;         }         x += 2 * B[cur];         y += 2 * B[cur];         x += (g1[cur].size() - 1) * S[cur];         r[cur] = make_pair(x, y);       }     }     for (typeof(m1.begin()) it = m1.begin(); it != m1.end(); it++) {       cout << it->first <<     << r[it->second].first <<                << r[it->second].second << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sh = 1e6;   const int ppr = 257;   const long long INF = 1e18;   const int inf = 1e9;   const int mod = 1e9 + 7;   const int N = 6e5 + 123;   const long double pi = 3.141592653589793238462643;   const int dx[] = {1, 0, -1, 0};   const int dy[] = {0, 1, 0, -1};   vector<int> g[N];   int T, n, a[N], b[N];   int main() {     cin >> T;     while (T--) {       cin >> n;       for (int i = 1; i <= n + 1; i++) g[i].clear(), b[i] = 0;       for (int i = 1; i <= n; i++) {         cin >> a[i];         if (a[i] == -1) a[i] = i + 1;         g[a[i]].push_back(i);       }       for (int i = 1; i <= n + 1; i++) sort(g[i].begin(), g[i].end());       queue<int> q;       int timer = n + 1;       q.push(n + 1);       while (!q.empty()) {         int v = q.front();         q.pop();         b[v] = timer--;         for (auto j : g[v]) {           if (b[j] > 0) continue;           q.push(j);         }       }       stack<int> st;       bool ok = 1;       for (int i = n; i >= 1; i--) {         while (!st.empty() && b[st.top()] < b[i]) st.pop();         if ((st.empty() && a[i] != n + 1) || (!st.empty() && a[i] != st.top())) {           ok = 0;           break;         }         st.push(i);       }       if (!ok) {         cout << -1 <<   n ;         continue;       }       for (int i = 1; i <= n; i++) cout << b[i] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cout.tie(NULL);     cin.tie(NULL);     ;     int n, m;     cin >> n >> m;     set<long long> s;     long long ans = 0;     long long a[n];     long long b[m];     long long mx = 0;     map<long long, long long> mp;     bool f = true;     set<long long> st;     for (int i = 0; i < n; i++) {       cin >> a[i];       mx = max(mx, a[i]);       s.insert(a[i]);       ans += m * a[i];       st.insert(a[i]);       mp[a[i]] += m - 1;     }     for (int i = 0; i < m; i++) {       cin >> b[i];       if (b[i] < mx) f = false;     }     if (!f) {       cout << -1 << endl;       return 0;     }     sort(b, b + m);     reverse(b, b + m);     for (int i = 0; i < m; i++) {       set<long long>::iterator it = s.upper_bound(b[i]);       --it;       if ((b[i] - *it) > 0 && st.find(b[i]) != st.end()) {         continue;       }       ans += b[i] - (*it);       mp[*it]--;       if (mp[*it] == 0) s.erase(it);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int val[100005], Max[100005], Min[100005];   int n;   int lef[100005], rig[100005];   void dfs(int r) {     if (lef[r] == 0) {       Max[r] = Min[r] = val[r];       return;     }     dfs(lef[r]);     dfs(rig[r]);     Max[r] = Max[rig[r]];     Min[r] = Min[lef[r]];   }   long long sum[100005];   int dep[100005];   void dfs(int r, long long s, int d) {     if (lef[r] == 0) {       sum[r] = s;       dep[r] = d;       return;     }     dfs(lef[r], s + Min[rig[r]], d + 1);     dfs(rig[r], s + Max[lef[r]], d + 1);   }   struct node {     int id, v;     friend bool operator<(node a, node b) { return a.v < b.v; }   } a[100005];   bool cmp(node a, int x) { return a.v < x; }   int main() {     scanf( %d , &n);     int x, y, root;     for (int i = 1; i <= n; i++) {       scanf( %d %d , &x, &y);       a[i].v = y;       a[i].id = i;       val[i] = y;       if (x == -1)         root = i;       else {         if (lef[x] == 0) {           lef[x] = i;         } else {           rig[x] = i;           if (val[lef[x]] > val[rig[x]]) {             swap(lef[x], rig[x]);           }         }       }     }     sort(a + 1, a + 1 + n);     dfs(root);     dfs(root, 0, 0);     int T;     scanf( %d , &T);     while (T--) {       scanf( %d , &x);       int t = lower_bound(a + 1, a + 1 + n, x, cmp) - a;       if (t == n + 1) t--;       t = a[t].id;       while (lef[t]) {         if (val[t] > x)           t = lef[t];         else           t = rig[t];       }       printf( %.10lf n , 1.0 * sum[t] / dep[t]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ans;   int father[1100];   int find(int x) {     if (father[x] == x) return x;     father[x] = find(father[x]);     return father[x];   }   bool Union(int x, int y) {     int f1 = find(x);     int f2 = find(y);     if (f1 == f2) return false;     if (f1 < f2)       father[f1] = f2;     else       father[f2] = f1;     ans++;     return true;   }   int n, m;   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) father[i] = i;     for (int i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       if (!Union(x, y)) {         cout <<  no  << endl;         return 0;       }     }     if (m != n - 1)       cout <<  no  << endl;     else       cout <<  yes  << endl;     return 0;   }


#include<bits/stdc++.h>   using namespace std;    const int N=1e5+77,B=330;    int n,q,a[N],t[N],b[N],l[N],r[N],m,g[N];    int nx(int x){return max(1,a[x]-t[b[x]]); }   int jp(int x){if(t[b[x]]>=B)return nx(x); return g[x]; }   void reb(int x)   {    for(int i=l[x]; i<=r[x]; i++)    {     int j=nx(i);      if(j<l[x])      g[i]=j;      else      g[i]=g[j];     }   }   void update(int x,int y,int v)   {    if(b[x]==b[y])    {     for(int i=x; i<=y; i++)      a[i]=max(1,a[i]-v);      reb(b[x]);      return;     }    for(int i=x; i<=r[b[x]]; i++)     a[i]=max(1,a[i]-v);     reb(b[x]);     for(int i=b[x]+1; i<b[y]; i++)    {     t[i]=min(t[i]+v,n);      if(t[i]<B)      reb(i);     }    for(int i=l[b[y]]; i<=y; i++)     a[i]=max(1,a[i]-v);     reb(b[y]);    }   void lca(int u,int v)   {    int lu=u,lv=v;     while(u!=v)    {     if(u<v)      swap(u,v),swap(lu,lv);      lu=u; u=jp(u);     }    u=lu,v=lv;     while(u!=v)    {     if(u<v)      swap(u,v);      u=nx(u);     }    printf( %d n ,u);    }   int main()   {    scanf( %d%d ,&n,&q);     for(int i=2; i<=n; i++)     scanf( %d ,&a[i]);     for(int x=1; x<=n; x+=B)    {     int y=min(x+B-1,n);      m++;      l[m]=x,r[m]=y;      for(int i=x; i<=y; i++)      b[i]=m;      reb(m);     }    while(q--)    {     int t;      scanf( %d ,&t);      if(t==1)     {      int x,y,v;       scanf( %d%d%d ,&x,&y,&v);       update(x,y,v);      }     else     {      int u,v;       scanf( %d%d ,&u,&v);       lca(u,v);      }    }      }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int i, j;     char s[1000];     while (gets(s)) {       int l = strlen(s);       n = 0;       int num_jian = 0, num_jia = 1;       for (i = 0; i < l; ++i) {         if (s[i] ==  = ) {           i += 2;           while (i != l) {             n *= 10;             n += s[i] -  0 ;             ++i;           }         } else if (s[i] ==  - )           num_jian++;         else if (s[i] ==  + )           num_jia++;       }       int v_jian = 1;       if (num_jian < num_jia) v_jian = n;       if (num_jia * n - num_jian < n || num_jia - num_jian * n > n)         printf( Impossible n );       else {         printf( Possible n );         j = 0;         for (i = 0; i < l; ++i) {           if (s[i] ==  ? ) {             if (i == 0 || s[i - 2] ==  + ) {               if (num_jia == 1)                 printf( %d , n + num_jian * v_jian);               else                 printf( %d , (n + num_jian * v_jian) / num_jia);               if ((n + num_jian * v_jian) / num_jia > 5) j = 1;               n -= (n + num_jian * v_jian) / num_jia;               num_jia--;             } else if (s[i - 2] ==  - )               printf( %d , v_jian);           } else             printf( %c , s[i]);         }         printf(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 66;   int power[MAX + 10];   int between[MAX + 10];   int extra[MAX + 10];   int ende[MAX + 10];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       long long foo;       cin >> foo;       int cc = 0;       bool good = true;       while (foo > 1) {         if (foo & 1) {           good = false;         }         cc++;         foo >>= 1;       }       if (good) {         power[cc]++;       } else {         between[cc]++;       }     }     int low = 1, high = power[0] + 1;     while (low < high) {       int mid = (low + high) >> 1;       int cur = mid;       for (int i = 0; i < MAX; i++) {         extra[i] = power[i] - cur;         int new_cur = min(cur, power[i + 1]);         ende[i] = cur - new_cur;         cur = new_cur;       }       bool fail = false;       int open = 0;       for (int i = MAX - 1; i >= 0; i--) {         open += ende[i];         open -= between[i];         open -= extra[i + 1];         if (open < 0) {           fail = true;           break;         }       }       open -= extra[0];       if (open < 0) {         fail = true;       }       if (fail) {         low = mid + 1;       } else {         high = mid;       }     }     if (low > power[0]) {       cout << -1 << endl;       return 0;     }     for (int i = low; i <= power[0]; i++) {       cout << i <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const N = int(2e5) + 7;   int const mod = int(1e9) + 7;   int a[N];   int ra[N], le[N];   int raB[N][31], leB[N][31];   int main() {     ios_base::sync_with_stdio(false);     ifstream in( input.txt );     int n;     cin >> n;     vector<int> st;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       while (st.size() > 0 && a[st.back()] <= a[i]) {         int ind = st.back();         st.pop_back();         int ll = 0;         if (st.size() > 0) {           ll = st.back();         }         le[ind] = ll;         ra[ind] = i;       }       st.push_back(i);       for (int j = 0; j <= 30; ++j) {         if (a[i] & (1 << j)) {           leB[i][j] = i;         } else {           leB[i][j] = leB[i - 1][j];         }       }     }     while (st.size() > 0) {       int ind = st.back();       st.pop_back();       int ll = 0;       if (st.size() > 0) {         ll = st.back();       }       le[ind] = ll;       ra[ind] = n + 1;     }     for (int i = n; i >= 0; --i) {       for (int j = 0; j <= 30; ++j) {         if (a[i] & (1 << j)) {           raB[i][j] = i;         } else {           raB[i][j] = raB[i + 1][j];         }       }     }     long long ans = 0;     for (int i = 1; i <= n; ++i) {       int indl = 0, indr = n + 1;       for (int j = 30; j >= 0; --j) {         if (!(a[i] & (1 << j)) && (leB[i - 1][j])) {           indl = max(indl, leB[i - 1][j]);         }       }       for (int j = 30; j >= 0; --j) {         if (!(a[i] & (1 << j)) && (raB[i + 1][j])) {           indr = min(indr, raB[i + 1][j]);         }       }       long long am1 = max(0, indl - le[i]);       long long am2 = max(0, i - max(indl, le[i]));       long long am3 = max(0, min(ra[i], indr) - i);       long long am4 = max(0, ra[i] - indr);       long long del = am1 * (am3 + am4) + am2 * am4;       ans += am1 * (am3 + am4) + am2 * am4;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, i, buc[26], cnt = 0, ok = 0, j, m, cntt = 0, dfn = 0, pre;   char a[2], s[100005];   int main() {     scanf( %d , &n);     pre = dfn;     cntt = 26;     for (i = 1; i <= n; i++) {       scanf( %s%s , a, s);       if (ok == 1) {         if (a[0] ==  ! ) cnt++;         if (a[0] ==  ? ) cnt++;       } else {         if (a[0] ==  ! ) {           dfn++;           cntt = 0;           m = strlen(s);           for (j = 0; j < m; j++) {             if (buc[s[j] -  a ] == pre) {               cntt++;               buc[s[j] -  a ] = dfn;             }           }           if (cntt == 1) ok = 1;           pre = dfn;         }         if (a[0] ==  . ) {           m = strlen(s);           for (j = 0; j < m; j++) {             if (buc[s[j] -  a ] == pre) {               cntt--;               buc[s[j] -  a ] = pre - 1;             }           }           if (cntt == 1) ok = 1;         }         if (a[0] ==  ? ) {           if (buc[s[0] -  a ] == pre) {             buc[s[0] -  a ] = pre - 1;             cntt--;           }           if (cntt == 1) ok = 1;         }       }     }     if (cnt)       printf( %d , cnt - 1);     else       printf( 0 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long double a, b;     int d;     while (n > 0) {       n -= 1;       cin >> a >> b;       if (a == b) {         d = 0;       } else if (a > b) {         d = ceil((a - b) / 10);       } else if (a < b) {         d = ceil((b - a) / 10);       }       cout << d << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int gi() {     int x = 0, o = 1;     char ch = getchar();     while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar();     if (ch ==  - ) o = -1, ch = getchar();     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     return x * o;   }   struct dat {     int len, x, y, z, w;     int getr() const {       if (!len) return 1e9;       if (len == 3) return z;       return w;     }     bool operator<(const dat &A) const { return getr() > A.getr(); }     void print() {       if (len == 3)         cout << 3 <<   n  << x <<     << y <<     << z <<   n ;       else         cout << 4 <<   n  << x <<     << y <<     << z <<     << w <<   n ;     }   } ans3[N], ans4[N];   int n, q, a[N];   void solve3() {     static int L[N], R[N], st[N];     int tp = 0;     for (int i = 1; i <= n; i++) {       while (tp && a[st[tp]] <= a[i]) --tp;       L[i] = st[tp];       st[++tp] = i;     }     tp = 0;     for (int i = n; i; i--) {       while (tp && a[st[tp]] <= a[i]) --tp;       R[i] = st[tp];       st[++tp] = i;     }     for (int i = 1; i <= n; i++)       if (L[i] && R[i]) ans3[L[i]] = max(ans3[L[i]], (dat){3, L[i], i, R[i], 0});     tp = 0;     for (int i = 1; i <= n; i++) {       while (tp && a[st[tp]] >= a[i]) --tp;       L[i] = st[tp];       st[++tp] = i;     }     tp = 0;     for (int i = n; i; i--) {       while (tp && a[st[tp]] >= a[i]) --tp;       R[i] = st[tp];       st[++tp] = i;     }     for (int i = 1; i <= n; i++)       if (L[i] && R[i]) ans3[L[i]] = max(ans3[L[i]], (dat){3, L[i], i, R[i], 0});   }   bool cmp1(int x, int y) { return a[x] < a[y]; }   bool cmp2(int x, int y) { return a[x] > a[y]; }   void solve4() {     static int st1[N], st2[N], occ[N];     set<int> can;     int tp1 = 0, tp2 = 0;     for (int i = n; i; i--) {       while (tp1 && a[st1[tp1]] > a[i]) {         int tmp = st1[tp1];         --occ[tmp];         if (!occ[tmp]) can.insert(tmp);         --tp1;       }       st1[++tp1] = i;       while (tp2 && a[st2[tp2]] < a[i]) {         int tmp = st2[tp2];         --occ[tmp];         if (!occ[tmp]) can.insert(tmp);         --tp2;       }       st2[++tp2] = i;       occ[i] = 2;       int p = st1[lower_bound(st1 + 1, st1 + tp1 + 1, i, cmp1) - st1 - 1];       int q = st2[lower_bound(st2 + 1, st2 + tp2 + 1, i, cmp2) - st2 - 1];       if (p && q) {         auto it = can.lower_bound(max(p, q) + 1);         if (it != can.end()) {           int t = *it;           p = *lower_bound(st1 + 1, st1 + tp1 + 1, t, greater<int>());           q = *lower_bound(st2 + 1, st2 + tp2 + 1, t, greater<int>());           ans4[i] = (dat){4, i, min(p, q), max(p, q), t};         }       }     }   }   int main() {     cin >> n >> q;     for (int i = 1; i <= n; i++) a[i] = gi();     solve3();     solve4();     for (int i = n - 1; i; i--) {       ans3[i] = max(ans3[i], ans3[i + 1]);       ans4[i] = max(ans4[i], ans4[i + 1]);     }     for (int i = 1; i <= q; i++) {       int l = gi(), r = gi();       if (ans4[l].getr() <= r)         ans4[l].print();       else if (ans3[l].getr() <= r)         ans3[l].print();       else         puts( 0 );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100 + 10;   vector<int> v[maxn];   set<int> s;   int num[maxn];   bool cmp(int x, int y) { return num[x] > num[y]; }   int main() {     int n, m, k;     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) cin >> num[i];     for (int i = 1; i <= n; i++) {       int tmp;       cin >> tmp;       v[tmp].push_back(i);     }     for (int i = 0; i < k; i++) {       int tmp;       cin >> tmp;       s.insert(tmp);     }     for (int i = 1; i <= n; i++) sort(v[i].begin(), v[i].end(), cmp);     int ans = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j < int(v[i].size()); j++) {         if (s.count(v[i][j])) {           ans++;           s.erase(v[i][j]);         }       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool f(int k, int n) {     int s = k;     while (k > 0) {       s += k % 10;       k /= 10;     }     return s == n;   }   int main() {     int i, n;     vector<int> ans;     cin >> n;     for (i = n - 100; i <= n + 100; ++i) {       if (f(i, n)) {         ans.push_back(i);       }     }     cout << ans.size() << endl;     for (i = 0; i < ans.size(); ++i) {       cout << ans[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 100000 + 10;   const double PI = M_PI;   const double eps = 1e-8;   int dcmp(double a) {     if (fabs(a) < eps) return 0;     if (a > 0) return 1;     return -1;   }   struct Point {     double x, y;     Point operator+(const Point &A) { return (Point){A.x + x, A.y + y}; }     Point operator-(const Point &A) { return (Point){x - A.x, y - A.y}; }   } P, Poly[Maxn];   int n;   struct Line {     Point P;     Point V;   } Line[Maxn];   double CalDJ(Point A, Point B) { return A.x * B.x + A.y * B.y; }   double CalCJ(Point A, Point B) { return A.x * B.y - A.y * B.x; }   double CalLen(Point A) { return sqrt(CalDJ(A, A)); }   double CalDIS(Point P, Point A, Point B) {     Point v1 = B - A, v2 = P - A, v3 = P - B;     if (dcmp(CalDJ(v1, v2)) < 0) return CalLen(v2);     if (dcmp(CalDJ(v1, v3)) > 0) return CalLen(v3);     return fabs(CalCJ(v1, v2) / CalLen(v1));   }   double Getdis(Point Q) {     long long dx = Q.x - P.x, dy = Q.y - P.y;     long long ret = dx * dx + dy * dy;     return ret;   }   double Max, Min;   void solve() {     scanf( %d , &n);     scanf( %lf%lf , &P.x, &P.y);     for (int i = 1; i <= n; i++) scanf( %lf%lf , &Poly[i].x, &Poly[i].y);     Min = Getdis(Poly[1]);     for (int i = 1; i <= n; i++) {       double di = Getdis(Poly[i]);       if (di > Max) Max = di;       if (di < Min) Min = di;     }     Poly[n + 1] = Poly[1];     for (int i = 1; i <= n; i++) {       double dis = CalDIS(P, Poly[i], Poly[i + 1]);       dis = dis * dis;       if (dis < Min) Min = dis;     }     double ans = PI * (Max - Min);     printf( %lf n , ans);   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   int vis[26], cnt[26], h[26];   set<int> e[26];   vector<string> S[1005];   int mp[26];   bool operator<=(string a, string b) {     for (int i = 0; i < min(a.size(), b.size()); i++) {       if (a[i] == b[i]) continue;       return mp[a[i] -  a ] < mp[b[i] -  a ];     }     return a.size() <= b.size();   }   int main() {     ios_base::sync_with_stdio(false);     string s;     int n, k, p;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> p;       for (int i = 1; i <= k; i++) {         cin >> s, S[p].push_back(s);         for (char c : s) h[c -  a ] = 1;       }     }     vector<string> v;     for (int i = 0; i < n; i++)       for (string s : S[i]) v.push_back(s);     for (int i = 1; i < v.size(); i++) {       string &s1 = v[i - 1], &s2 = v[i];       for (int i = 0; i < min(s1.size(), s2.size()); i++) {         if (s1[i] != s2[i]) {           int c1 = s1[i] -  a , c2 = s2[i] -  a ;           if (e[c1].find(c2) == e[c1].end()) e[c1].insert(c2), cnt[c2]++;           break;         }       }     }     vector<int> q;     int m = 0;     for (int i = 0; i < 26; i++) m += (h[i]);     for (int i = 0; i < 26; i++)       if (cnt[i] == 0 and h[i]) q.push_back(i);     for (int k = 0; k < q.size(); k++) {       int i = q[k];       vis[i] = 1;       for (int j : e[i]) {         if (vis[j]) continue;         cnt[j]--;         if (cnt[j] == 0) q.push_back(j);       }     }     bool ok = 1;     for (int i = 0; i < q.size(); i++) mp[q[i]] = i;     for (int i = 1; i < v.size(); i++) ok &= (v[i - 1] <= v[i]);     if (q.size() != m or !ok) cout <<  IMPOSSIBLE  <<   n , exit(0);     for (int i : q) cout << (char)(i +  a );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b;   int main() {     int t;     cin >> t;     while (t--) {       cin >> n >> a >> b;       if (abs(a - b) > 1 || a + b > n - 2)         cout << -1;       else {         int l, r, x;         x = a + b;         if (a == b) {           l = n, r = n - x;           while (r < l) {             cout << l <<     << r <<    ;             l--, r++;           }           if (r == l) cout << r <<    ;           for (int i = n - x - 1; i >= 1; i--) cout << i <<    ;         } else if (a > b) {           l = n, r = n - x;           while (r < l) {             cout << r <<     << l <<    ;             r++, l--;           }           if (r == l) cout << r <<    ;           for (int i = n - x - 1; i >= 1; i--) cout << i <<    ;         } else {           r = 1, l = x + 1;           while (r < l) {             cout << l <<     << r <<    ;             l--, r++;           }           if (r == l) cout << r <<    ;           for (int i = x + 2; i <= n; i++) cout << i <<    ;         }       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1234567;   int query(vector<long long> a) {     int n = a.size();     for (int i = 2; i * i <= n; i++) {       if (n % i == 0) {         if (n % (i * i) != 0) {           for (int j = 0; j < n; j += i) {             long long d = a[j];             int k = j;             do {               a[k] -= d;               k = (k + n / i) % n;             } while (k != j);           }         }         vector<vector<long long> > w(i);         for (int j = 0; j < n; j++) w[j % i].push_back(a[j]);         bool f = 1;         for (int j = 0; j < i; j++)           if (!query(w[j])) {             f = 0;             break;           }         return f;       }     }     for (int i = 1; i < a.size(); i++)       if (a[i] != a[0]) return 0;     return 1;   }   int main() {     int n;     string s;     cin >> n >> s;     vector<long long> a(n);     for (int i = 0; i < n; i++) a[i] = s[i] -  0 ;     cout << (query(a) ?  YES  :  NO );     return 0;   }