#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:1024000000,1024000000 )   const double pi = acos(-1.0), eps = 1e-8;   const int maxm = 1100000, inf = 0x3f3f3f3f;   bool f[maxm];   vector<int> v[maxm];   int top, st[maxm];   int a[maxm];   void dfs(int x, int pre) {     f[x] = true;     int i;     a[x] ^= 1;     st[top++] = x;     for (i = 0; i < v[x].size(); i++) {       if (f[v[x][i]] == false) {         dfs(v[x][i], x);         st[top++] = x;         a[x] ^= 1;       }     }     if (a[x] != 0 && pre != -1) {       st[top++] = pre;       a[pre] ^= 1;       st[top++] = x;       a[x] ^= 1;     }   }   int main() {     int n, m, i, x, y, begin;     while (scanf( %d%d , &n, &m) == 2) {       for (i = 1; i <= n; i++) v[i].clear();       while (m--) {         scanf( %d%d , &x, &y);         v[x].push_back(y);         v[y].push_back(x);       }       for (i = 1; i <= n; i++) {         scanf( %d , &a[i]);         f[i] = false;       }       top = 0;       for (i = 1; i <= n; i++) {         if (a[i] == 1) {           dfs(i, -1);           begin = i;           break;         }       }       if (i == n + 1) {         printf( 0 n );         continue;       }       for (i = begin + 1; i <= n; i++) {         if (a[i] != 0) {           printf( -1 n );           break;         }       }       if (i <= n) continue;       if (a[begin] == 1)         begin = 1;       else         begin = 0;       printf( %d n , top - begin);       for (i = begin; i < top; i++) {         printf( %d , st[i]);         if (i == top - 1)           printf(  n );         else           printf(   );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 11111, MAX_M = 222222;   class Flow_Algorithm {    public:     int g[MAX_M], nxt[MAX_M], flow[MAX_M], c[MAX_N], h[MAX_N], nm;     int d[MAX_N], q[MAX_N];     int S, T;     void addedge(int x, int y, int w) {       g[nm] = y;       nxt[nm] = c[x];       c[x] = nm;       flow[nm] = w;       nm++;     }     void add(int x, int y, int w) {       addedge(x, y, w);       addedge(y, x, 0);     }     bool bfs() {       memset(d, -1, sizeof d);       d[q[1] = S] = 0;       for (int l = 1, r = 1; l <= r; ++l) {         int i = q[l];         for (int k = c[i]; ~k; k = nxt[k]) {           int j = g[k];           if (!~d[j] && flow[k]) d[q[++r] = j] = d[i] + 1;         }       }       return d[T] != -1;     }     int dfs(int i, int now) {       if (!now) return 0;       if (i == T) return now;       int w = 0;       for (int &k = c[i]; ~k; k = nxt[k]) {         int j = g[k];         if (d[j] == d[i] + 1) {           int res;           if (flow[k] && ((res = dfs(j, min(now - w, flow[k]))) > 0)) {             flow[k] -= res;             flow[k ^ 1] += res;             w += res;             if (w == now) return w;           }         }       }       return w;     }     void sets(int _S, int _T) {       S = _S;       T = _T;       nm = 0;       memset(c, -1, sizeof c);     }     int Dinic() {       memcpy(h, c, sizeof h);       int ans = 0;       while (bfs()) {         ans += dfs(S, 1000000000);         memcpy(c, h, sizeof c);       }       return ans;     }   } G;   int A[55], B[55], C[55], L[55], R[55];   int n, m;   int nd[55][222];   int calc(int i, int x) { return x * x * A[i] + x * B[i] + C[i]; }   int main() {     scanf( %d%d , &n, &m);     for (int i = (1); i <= (n); ++i) scanf( %d%d%d , &A[i], &B[i], &C[i]);     for (int i = (1); i <= (n); ++i) scanf( %d%d , &L[i], &R[i]);     int tot = 0;     for (int i = (1); i <= (n); ++i)       for (int j = (L[i] - 1); j <= (R[i]); ++j) nd[i][j + 100] = ++tot;     int S = tot + 1, T = tot + 2;     G.sets(S, T);     for (int i = (1); i <= (n); ++i) {       G.add(S, nd[i][L[i] - 1 + 100], 1000000000);       for (int j = (L[i]); j <= (R[i]); ++j)         G.add(nd[i][j - 1 + 100], nd[i][j + 100], 20000000 - calc(i, j));       G.add(nd[i][R[i] + 100], T, 1000000000);     }     for (int i = (1); i <= (m); ++i) {       int u, v, d;       scanf( %d%d%d , &u, &v, &d);       for (int j = (L[u] - 1); j <= (R[u]); ++j) {         int p = j - d, pt = 0;         if (p < L[v])           pt = nd[v][L[v] - 1 + 100];         else           pt = nd[v][p + 100];         G.add(nd[u][j + 100], pt, 1000000000);       }     }     int ans = G.Dinic();     ans = n * 20000000 - ans;     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   constexpr int N = 2e5 + 3;   vector<int> c[N];   vector<int> e[N];   int main() {     ios_base::sync_with_stdio(false);     int n, m, q;     cin >> n >> m >> q;     for (int it = 0; it < q; ++it) {       int r, C;       cin >> r >> C;       c[r].push_back(C);     }     for (int i = 1; i <= n; ++i) {       int esz = c[i].size();       for (int j = 1; j < esz; ++j) {         int prev = c[i][j - 1], now = c[i][j];         e[prev].push_back(now);         e[now].push_back(prev);       }     }     vector<int> vis(m + 1, 0);     int cc = 0;     for (int i = 1; i <= m; ++i) {       if (vis[i] == 0) {         ++cc;         function<void(int)> dfs = [&](int i) {           assert(i >= 1 && i <= m);           if (vis[i] != 0) {             assert(vis[i] == cc);             return;           }           vis[i] = cc;           for (int v : e[i]) {             dfs(v);           }         };         dfs(i);       }     }     int cnt_empty_r = 0;     for (int i = 1; i <= n; ++i) {       if (c[i].empty()) {         ++cnt_empty_r;       }     }     cout << cnt_empty_r + cc - 1 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   static const int INF = 0x3f3f3f3f;   static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;   template <typename T>   T mulmod(T a, T b, T m) {     long long int q =         (long long int)(((long double)a * (long double)b) / (long double)m);     long long int r = a * b - q * m;     if (r > m) r %= m;     if (r < 0) r += m;     return r;   }   template <typename T>   T MOD(T a, T b) {     return (a < b ? a : a % b);   }   template <typename T>   T expo(T e, T n) {     T x = 1, p = e;     while (n) {       if (n & 1) x = x * p;       p = p * p;       n >>= 1;     }     return x;   }   template <typename T>   T power(T e, T n, T m) {     T x = 1, p = e;     while (n) {       if (n & 1) x = mulmod(x, p, m);       p = mulmod(p, p, m);       n >>= 1;     }     return x;   }   template <typename T>   T gcd(T a, T b) {     if (a % b) return gcd(b, a % b);     return b;   }   template <typename T>   T lcm(T a, T b) {     return (a * (b / gcd(a, b)));   }   template <typename T>   T add(T a, T b, T Mod) {     a += b;     return (a >= Mod ? a - Mod : a);   }   template <typename T>   T sub(T a, T b, T Mod) {     a -= b;     return (a < 0 ? a + Mod : a);   }   const int Mod = 1000000007;   const int N = 100005;   int MX = 1000001;   int a[N];   struct segmentTree {     int tree[6 * N];     int lazy[6 * N];     void modify(int node, int v) {       tree[node] += v;       lazy[node] += v;     }     void update(int node, int x, int y, int p, int q, int val) {       if (lazy[node]) {         modify(node << 1, lazy[node]);         modify(node << 1 | 1, lazy[node]);         lazy[node] = 0;       }       if (x >= p and y <= q) {         modify(node, val);         return;       }       int mid = x + y >> 1;       if (p <= mid) update(node << 1, x, mid, p, q, val);       if (q > mid) update(node << 1 | 1, mid + 1, y, p, q, val);       tree[node] = max(tree[node << 1], tree[node << 1 | 1]);     }     int query(int node, int x, int y) {       if (lazy[node]) {         modify(node << 1, lazy[node]);         modify(node << 1 | 1, lazy[node]);         lazy[node] = 0;       }       if (tree[node] <= 0) return -1;       if (x == y) return a[x];       int mid = x + y >> 1;       int ret = query(node << 1 | 1, mid + 1, y);       if (ret == -1) {         ret = query(node << 1, x, mid);       }       return ret;     }   } T;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int m;     cin >> m;     for (int i = 0; i <= m - 1; i++) {       int p, t;       cin >> p >> t;       if (t == 0) {         T.update(1, 1, m, 1, p, -1);       } else {         int x;         cin >> x;         a[p] = x;         T.update(1, 1, m, 1, p, 1);       }       cout << T.query(1, 1, m) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 100005;   const long long M = 998244353;   long long pow1(long long a, long long b) {     int res = 1;     while (b > 0) {       if (b & 1) {         res = (res * a) % M;       }       a = (a * a) % M;       b >>= 1;     }     return res % M;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long T;     T = 1;     cin >> T;     while (T--) {       long long n, m;       cin >> n >> m;       long long ans[n][m];       for (long long i = 0; i < n; i++) {         for (long long j = 0; j < m; j++) {           long long num;           cin >> num;           if (num % 2 != (i + j) % 2)             ans[i][j] = num + 1;           else             ans[i][j] = num;           cout << ans[i][j] <<    ;         }         cout <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k, i, j;     cin >> n >> k;     long long int x = 1;     long long int ans = n - 1;     for (i = 0; i < k; i++) {       vector<long long int> v;       long long int m;       cin >> m;       long long int t = -1;       for (j = 0; j < m; j++) {         long long int y;         cin >> y;         if (y == 1) t = j;         v.push_back(y);       }       if (t != -1) {         for (j = t + 1; j < m; j++) {           if (v[j] == v[j - 1] + 1)             x++;           else             break;         }       }       ans += m - 1;     }     cout << ans - (2 * (x - 1));   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int num, hight, temp, width = 0;     cin >> num >> hight;     vector<int> v;     for (int i = 0; i < num; i++) {       cin >> temp;       v.push_back(temp);       if (v[i] > hight)         width += 2;       else         width++;     }     cout << width;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = 2 * acos(0);   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int q;     cin >> q;     while (q--) {       int n, r;       cin >> n >> r;       set<int> s;       vector<int> v;       for (long long i = 0; i < n; i++) {         int a;         cin >> a;         s.insert(a);       }       for (auto u : s) v.push_back(u);       int cnt = 0;       for (int i = v.size() - 1; i >= 0; i--) {         if (v[i] - r * cnt > 0) cnt++;       }       cout << cnt <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int n, a[N], b[N], posa[N], posb[N], dif[N], ans, ord[N], t1;   multiset<int> rig, lef;   int abss(const int i) { return i < 0 ? -i : i; }   bool cmp(int i, int j) { return dif[i] < dif[j]; }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       posa[a[i]] = i;     }     for (int i = 1; i <= n; ++i) {       scanf( %d , &b[i]);       posb[b[i]] = i;     }     ans = n;     for (int i = 1; i <= n; ++i) {       ans = min(ans, abss(posa[i] - posb[i]));       dif[i] = posb[i] - posa[i];       if (dif[i] > 0) {         rig.insert(dif[i]);       } else {         lef.insert(-dif[i]);       }       ord[i] = i;     }     sort(ord + 1, ord + 1 + n, cmp);     t1 = 1;     printf( %d n , ans);     for (int i = 2; i <= n; ++i) {       lef.erase(lef.find(posa[b[i - 1]] - i + 1));       rig.insert(n - posa[b[i - 1]] + i - 1);       while (rig.size() > 0 && *rig.begin() - i + 1 == 0) {         rig.erase(rig.find(*rig.begin()));         lef.insert((int)(-i + 1));       }       ans = min(*lef.begin() + i - 1,                 rig.size() > 0 ? *rig.begin() - i + 1 : n + 1);       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int MAX = 100005;   const int MAX1 = 1005;   const int MAX2 = 105;   const int LEN = 105;   const int BASE = 1000000000;   const double EPS = 1e-7;   const int MOD = 1000000007;   long long pow(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % MOD;       b /= 2;       a = (a * a) % MOD;     }     return ans;   }   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ifstream in(  );     ofstream out(  );     clock_t begin = clock();     int q;     cin >> q;     while (q--) {       int n;       cin >> n;       int arr[MAX];       for (int i = (0); i < (n); ++i) {         cin >> arr[i];       }       sort(arr, arr + n);       cout << min(n - 2, arr[n - 2] - 1) <<   n ;     }     clock_t end = clock();     double esecs = double(end - begin) / CLOCKS_PER_SEC;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T S(T a) {     return a * a;   }   template <class T>   inline string tostring(T a) {     ostringstream os(  );     os << a;     return os.str();   }   template <typename T>   inline long long tolong(T a) {     long long res;     istringstream os(a);     os >> res;     return res;   }   template <typename T>   inline T gcd(T a, T b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   template <typename T>   inline T bigmod(T a, T b, T m) {     if (b == 0)       return 1;     else if (b % 2 == 0)       return S(bigmod(a, b / 2, m)) % m;     else       return (a % m * bigmod(a, b - 1, m)) % m;   }   const int inf = (int)1e9 + 5;   const long long linf = (long long)1e16 + 5;   const long long modd = (long long)1e9 + 7;   const int mod = 10000007;   void pr(int f) {     if (f == 1)       cout <<  YES n ;     else       cout <<  NO n ;   }   bool isprime(long long n) {     if (n < 2) return 0;     if (n == 2)       return 1;     else {       for (int i = 2; i * i <= n; i++)         if (n % i == 0) return 0;     }     return 1;   }   map<int, int> mp;   multiset<string>::iterator it;   deque<char> q;   const int nax = 10e6 + 5;   int t[nax], l[100005], d[100005];   int main() {     ios_base::sync_with_stdio(0);     long long int n, m;     cin >> n >> m;     long long int low, high;     high = ((n - (m - 1)) * (n - m)) / 2;     long long int quo = n / m;     long long int rem = n % m;     low = rem * ((quo + 1) * (quo)) / 2 + (m - rem) * (quo * (quo - 1)) / 2;     cout << low <<     << high;   }


#include <bits/stdc++.h>   const int inf = (1ll << 30) - 1;   const int maxn = (int)1e5 + 10;   using namespace std;   int n;   vector<int> g[100100];   int a[100100];   int in[100100];   int out[100100];   int dp[100100];   map<int, int> t[400400];   int timer = 0;   void dfs(int v) {     in[v] = timer;     a[timer] = v;     timer++;     dp[v] = 1;     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i];       dfs(to);       dp[v] += dp[to];     }     out[v] = timer - 1;   }   void build(int v, int l, int r) {     if (l == r) {       t[v][dp[a[l]]]++;     } else {       int mid = (l + r) >> 1;       build(v * 2, l, mid);       build(v * 2 + 1, mid + 1, r);       for (int i = l; i <= r; i++) t[v][dp[a[i]]]++;     }   }   int getu(int v, int l, int r, int tl, int tr, int x) {     if (tl > tr) return inf;     if (l == tl && tr == r) {       map<int, int>::iterator pos = t[v].lower_bound(x);       if (pos != t[v].end()) return pos->first;       return inf;     }     int mid = (l + r) >> 1;     return min(getu(v * 2, l, mid, tl, min(tr, mid), x),                getu(v * 2 + 1, mid + 1, r, max(tl, mid + 1), tr, x));   }   int getd(int v, int l, int r, int tl, int tr, int x) {     if (tl > tr) return -inf;     if (l == tl && tr == r) {       map<int, int>::iterator pos = t[v].lower_bound(x + 1);       if (pos != t[v].begin()) return (--pos)->first;       return -inf;     }     int mid = (l + r) >> 1;     return max(getd(v * 2, l, mid, tl, min(tr, mid), x),                getd(v * 2 + 1, mid + 1, r, max(tl, mid + 1), tr, x));   }   void ADD(int v, int l, int r, int pos, int x) {     t[v][x]++;     if (l == r) {       return;     }     int mid = (l + r) >> 1;     if (pos <= mid) {       ADD(v * 2, l, mid, pos, x);     } else       ADD(v * 2 + 1, mid + 1, r, pos, x);   }   void DEL(int v, int l, int r, int pos, int x) {     t[v][x]--;     if (t[v][x] == 0) t[v].erase(x);     if (l == r) {       return;     }     int mid = (l + r) >> 1;     if (pos <= mid) {       DEL(v * 2, l, mid, pos, x);     } else       DEL(v * 2 + 1, mid + 1, r, pos, x);   }   int ans[100100];   set<int> PA;   void calc(int v, int szz) {     int cv = dp[v];     DEL(1, 0, n - 1, in[v], cv);     PA.insert(dp[v]);     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i];       cv = szz + dp[v] - dp[to];       calc(to, szz + dp[v] - dp[to]);     }     PA.erase(dp[v]);     vector<pair<int, int> > cur;     if (szz) cur.push_back(make_pair(szz, 0));     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i];       cur.push_back(make_pair(dp[to], to));     }     sort(cur.begin(), cur.end());     int d = cur.size();     if (cur.size() <= 1 || cur[d - 2].first == cur.back().first) {       ans[v] = cur.back().first;       ADD(1, 0, n - 1, in[v], dp[v]);       return;     }     ans[v] = cur[d - 1].first;     int sz = (cur[0].first + cur[d - 1].first) / 2 - cur[0].first;     int xx;     int V = cur[d - 1].second;     if (V)       xx = getu(1, 0, n - 1, in[V], out[V], sz);     else {       xx = min(getu(1, 0, n - 1, 0, in[v] - 1, sz),                getu(1, 0, n - 1, out[v] + 1, n - 1, sz));       set<int>::iterator it = PA.lower_bound(sz + dp[v]);       if (it != PA.end()) xx = min(xx, *it - dp[v]);     }     ans[v] = min(ans[v], max(cur[d - 2].first,                              max(cur[0].first + xx, cur[d - 1].first - xx)));     if (V)       xx = getd(1, 0, n - 1, in[V], out[V], sz);     else {       xx = max(getd(1, 0, n - 1, 0, in[v] - 1, sz),                getd(1, 0, n - 1, out[v] + 1, n - 1, sz));       set<int>::iterator it = PA.upper_bound(sz + dp[v]);       if (it != PA.begin()) xx = max(xx, *(--it) - dp[v]);     }     ans[v] = min(ans[v], max(cur[d - 2].first,                              max(cur[0].first + xx, cur[d - 1].first - xx)));     ADD(1, 0, n - 1, in[v], dp[v]);   }   void solve() {     scanf( %d , &n);     int root = 0;     for (int i = 1, x, y; i <= n; i++) {       scanf( %d%d , &x, &y);       if (x)         g[x].push_back(y);       else         root = y;     }     if (n == 1) {       printf( 0 n );       return;     }     if (n == 2) {       printf( 1 n1 n );       return;     }     dfs(root);     build(1, 0, n - 1);     calc(root, 0);     for (int i = 1; i <= n; i++) {       printf( %d n , ans[i]);     }   }   int main() {     int t = 1;     for (int i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     bool zero = false;     int *arr = new int[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     int neg = 0;     for (int i = 0; i < n; i++) {       if (arr[i] < 0) neg++;       if (arr[i] == 0) {         zero = true;       }     }     for (int i = 0; k > 0 && i < neg;) {       arr[i] = arr[i] * -1;       k--;       i++;     }     long long int ans = 0;     for (int i = 0; i < n; i++) {       ans += arr[i];     }     int min = INT_MAX;     for (int i = 0; i < n; i++) {       if (arr[i] < min) {         min = arr[i];       }     }     if (k % 2 == 0) {       cout << ans;     } else {       cout << ans - 2 * min;     }     return 0;   }


#include<bits/stdc++.h>   #define fo(i, n) for(int i=0; i<n; i++)   #define boost ios::sync_with_stdio(false); cin.tie(0)   #define ll long long int   using namespace std;      int main()   {   boost;       int t; cin>>t;       while(t--){           int n, max_freq = 0, i; cin>>n;           map<int, int> m;           for(i=0; i<n; i++){               int k; cin>>k;               max_freq = max(max_freq, ++m[k]);           }           cout<<max_freq<<  n ;       }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500001;   int arr[N];   vector<pair<int, int> > v[N];   int ans[N], n, m;   int bit[N];   map<int, int> last;   void update(int idx, int val) {     for (; idx <= n; idx += idx & -idx) {       bit[idx] = min(bit[idx], val);     }   }   int query(int idx) {     int res = n + 1;     for (; idx; idx -= idx & -idx) {       res = min(res, bit[idx]);     }     return res;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       scanf( %d , &arr[i]);       bit[i] = n + 1;     }     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d %d , &x, &y);       v[y].push_back({x, i});     }     for (int i = 1; i <= n; i++) {       if (last.find(arr[i]) != last.end())         update(n - last[arr[i]] + 1, i - last[arr[i]]);       last[arr[i]] = i;       int x = v[i].size();       for (int j = 0; j < x; ++j) {         ans[v[i][j].second] = query(n - v[i][j].first + 1);       }     }     for (int i = 1; i <= m; i++) {       printf( %d n , (ans[i] == n + 1) ? -1 : ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(false);     long long int n, i, t, k, c, s = 0, x, sum = 0, st1 = 0, st2 = 0, curr = 0;     cin >> n;     vector<pair<long long int, long long int> > v;     vector<long long int> p;     for (i = 0; i < n; i++) {       cin >> k >> c;       v.push_back(make_pair(c, k));     }     cin >> t;     for (i = 0; i < t; i++) {       cin >> x;       s = x;       p.push_back(s);     }     p.push_back(100000000000000);     sort(v.begin(), v.end());     while (st1 < n) {       if (curr + v[st1].second < p[st2]) {         sum += (st2 + 1) * (v[st1].first) * (v[st1].second);         curr += v[st1].second;         st1++;       } else {         long long int rem = p[st2] - curr;         curr += rem;         sum += (rem) * (st2 + 1) * (v[st1].first);         v[st1].second -= rem;         st2++;         if (!v[st1].second) {           st1++;         }       }     }     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   namespace io {   const int SI = 1 << 21 | 1;   char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];   int f, t;   inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; }   inline void pc(char x) {     *OS++ = x;     if (OS == OT) flush();   }   template <class I>   inline void rd(I &x) {     for (f = 1, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                                 IS == IT ? EOF : *IS++)                               : *IS++);          c <  0  || c >  9 ;          c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                          IS == IT ? EOF : *IS++)                        : *IS++))       if (c ==  - ) f = -1;     for (x = 0; c >=  0  && c <=  9 ; x = (x << 3) + (x << 1) + (c & 15),         c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                         IS == IT ? EOF : *IS++)                       : *IS++))       ;     x *= f;   }   inline void rds(char *s, int &x) {     for (c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                          IS == IT ? EOF : *IS++)                        : *IS++);          c < 33 || c > 126;          c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                          IS == IT ? EOF : *IS++)                        : *IS++))       ;     for (x = 0; c >= 33 && c <= 126;          s[++x] = c, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),                                      IS == IT ? EOF : *IS++)                                    : *IS++))       ;     s[x + 1] =   0 ;   }   template <class I>   inline void print(I x, char k =   n ) {     if (!x) pc( 0 );     if (x < 0) pc( - ), x = -x;     while (x) ch[++t] = x % 10 +  0 , x /= 10;     while (t) pc(ch[t--]);     pc(k);   }   inline void prints(string s) {     int x = s.length();     while (t < x) pc(s[t++]);     pc(  n ), t = 0;   }   struct Flush {     ~Flush() { flush(); }   } flusher;   }  // namespace io   using io::print;   using io::prints;   using io::rd;   using io::rds;   const int N = 3e5 + 7;   int n, a[N], sx[N], tx, sn[N], tn;   map<int, int> p;   struct T {     int l, r, x, c, z;   } t[N << 2];   long long ans;   void build(int p, int l, int r) {     t[p].l = l, t[p].r = r, t[p].c = r - l + 1;     if (l == r) return;     build((p << 1), l, ((t[p].l + t[p].r) >> 1)),         build(((p << 1) | 1), ((t[p].l + t[p].r) >> 1) + 1, r);   }   inline void add(int p, int x) { t[p].x += x, t[p].z += x; }   void upd(int p, int l, int r, int x) {     if (t[p].l >= l && t[p].r <= r) return add(p, x);     if (t[p].z) add((p << 1), t[p].z), add(((p << 1) | 1), t[p].z), t[p].z = 0;     if (l <= ((t[p].l + t[p].r) >> 1)) upd((p << 1), l, r, x);     if (r > ((t[p].l + t[p].r) >> 1)) upd(((p << 1) | 1), l, r, x);     t[p].x = min(t[(p << 1)].x, t[((p << 1) | 1)].x);     t[p].c = (t[(p << 1)].x == t[p].x ? t[(p << 1)].c : 0) +              (t[((p << 1) | 1)].x == t[p].x ? t[((p << 1) | 1)].c : 0);   }   int main() {     rd(n);     for (int i = 1, x, y; i <= n; i++) rd(x), rd(y), a[x] = y;     build(1, 1, n);     for (int i = 1; i <= n; i++) {       while (tx && a[sx[tx]] < a[i])         upd(1, sx[tx - 1] + 1, sx[tx], -a[sx[tx]]), --tx;       while (tn && a[sn[tn]] > a[i])         upd(1, sn[tn - 1] + 1, sn[tn], a[sn[tn]]), --tn;       upd(1, p[a[i]] + 1, i, -1), p[a[i]] = sx[++tx] = sn[++tn] = i;       upd(1, sx[tx - 1] + 1, i, a[i]), upd(1, sn[tn - 1] + 1, i, -a[i]);       ans += t[1].c;     }     print(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, m;   int nt, kt, mt;   int su[100004];   long long sum = 0;   double res;   int main() {     scanf( %d%d%d , &n, &k, &m);     nt = n;     kt = k;     mt = m;     for (int i = 0; i < n; i++) {       scanf( %d , su + i);       sum += su[i];     }     res = 1.0 * sum / n;     sort(su, su + n);     long long ts = 0;     for (int i = 1; i <= n; i++) {       ts += su[n - i];       long long rm = m - n + i;       long long te = i;       long long tk = k;       if (rm < 0) {         continue;       }       res = max(res, (ts + min(rm, te * tk)) * 1.0 / te);     }     printf( %lf n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 100;   const int MOD = 1e9 + 7;   const int oo = 1e9 + 100;   const long double EPS = 1e-9;   const string DIRS =  UDLR ;   int n, x, y;   char str[MAXN];   int acc[256][MAXN];   int get(char ch, int fr, int to) {     if (to < fr) return 0;     return acc[ch][to] - acc[ch][fr - 1];   }   int main() {     while (scanf( %d %s %d %d , &n, str + 1, &x, &y) == 4) {       for (auto& e : DIRS) {         acc[e][0] = 0;         for (int i = 1; i <= n; ++i) {           acc[e][i] = acc[e][i - 1] + (str[i] == e);         }       }       int init_x = acc[ R ][n] - acc[ L ][n];       int init_y = acc[ U ][n] - acc[ D ][n];       if (init_x == x && init_y == y) {         puts( 0 );         continue;       }       if (abs(x) + abs(y) > n || (n - abs(x) - abs(y)) % 2 != 0) {         puts( -1 );         continue;       }       int lo = 1, hi = n;       vector<int> aux(256);       while (lo <= hi) {         int mi = (lo + hi) >> 1;         bool ok = false;         for (int i = 1; i + mi - 1 <= n; ++i) {           for (auto& e : DIRS) aux[e] = get(e, 1, i - 1) + get(e, i + mi, n);           int xx = aux[ R ] - aux[ L ];           int yy = aux[ U ] - aux[ D ];           int rem = mi - (abs(x - xx) + abs(y - yy));           if (rem >= 0) ok = true;         }         if (ok)           hi = mi - 1;         else           lo = mi + 1;       }       printf( %d n , lo);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[200010], b[200010];   bool used[200010];   int main() {     int n, k = 0, i, x;     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     for (i = 1; i <= n; i++) scanf( %d , &b[i]);     int ans = 1 - accumulate(b + 1, b + n + 1, 0) % 2;     for (i = 1; i <= n; i++)       if (!used[i]) {         used[i] = true;         x = a[i];         while (!used[x]) {           used[x] = true;           x = a[x];         }         k++;       }     if (k > 1) ans += k;     printf( %d n , ans);     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 n, m, i, cnt = 0, sum = 0, ans = 0, x, j;     string s;     vector<string> v;     cin >> n >> m;     int a[m];     for (i = 0; i < n; i++) {       cin >> s;       v.push_back(s);     }     for (i = 0; i < m; i++) cin >> a[i];     for (i = 0; i < m; i++) {       int arr[5] = {0};       for (j = 0; j < n; j++) {         x = v[j][i] -  A ;         arr[x]++;       }       sort(arr, arr + 5);       ans += arr[4] * a[i];     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   void chekarre(int* arr, int n) {     cout <<  [ ;     for (int i = 0; i < n; i++) cout << arr[i] <<    ;     cout <<  ]  << endl;   }   bool comp(int i, int j) { return (i > j); }   int a[2000007];   int main() {     int n;     long long sum = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     reverse(a, a + n);     while (n) {       for (int j = 0; j < n; j++) {         sum += a[j];       }       n = n / 4;     }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b;   int ans, nn;   char x[100010];   int change(int t) {     int rtn = 0, bit = 1;     while (t) {       rtn += (t % 10) * bit;       bit *= ans;       t /= 10;     }     return rtn;   }   int main() {     scanf( %d%d , &a, &b);     int aa = a, bb = b;     while (aa) {       ans = max(ans, aa % 10);       aa /= 10;     }     while (bb) {       ans = max(ans, bb % 10);       bb /= 10;     }     ans += 1;     a = change(a);     b = change(b);     a += b;     while (a) {       x[nn++] = a % ans +  0 ;       a /= ans;     }     printf( %d , nn);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long OO = 1e8;   const double EPS = (1e-7);   int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }   const int N = 100;   bool adjMatrixBool[N][N];   int adjMatrix[N][N];   vector<int> adjMatrixAll[N][N];   map<pair<int, int>, int> adjMatrixMap;   vector<vector<int> > adjList1;   vector<vector<pair<int, int> > > adjList2;   vector<bool> visited;   bool valid(int i, int j) { return 1; }   char arr[303][303], x, b;   bool prime(long long n) {     long long i;     for (i = 2; i <= sqrt(n); i++) {       if (n % i == 0) return false;     }     return true;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); }   int main() {     ios_base::sync_with_stdio(false);     long long n;     cin >> n;     string s;     cin >> s;     long long arr[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     long long ans = INT_MAX;     for (int i = 0; i < n - 1; i++) {       if (s[i] ==  R  and s[i + 1] ==  L )         ans = min(ans, abs((arr[i + 1] - arr[i]) / 2));     }     if (ans == INT_MAX)       cout <<  -1  << endl;     else       cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const long long INF = 1000 * 1000 * 1000 + 7;   const long long LINF = INF * (long long)INF;   const int MOD = 1000 * 1000 * 1000 + 7;   const int MAX = 10000 + 47;   long long F[MAX];   vector<int> R;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int s, k;     cin >> s >> k;     int p = 0;     F[0] = 1;     for (int i = (1); i < (MAX); i++) {       for (int j = (max(0, i - k)); j < (i); j++) {         F[i] += F[j];       }       if (F[i] > s) {         p = i;         break;       }     }     for (int i = (p)-1; i >= (0); i--) {       if (s >= F[i]) {         s -= F[i];         R.push_back(F[i]);       }     }     if ((int)R.size() == 1) R.push_back(0);     cout << (int)R.size() << endl;     for (int i = (0); i < ((int)R.size()); i++) {       cout << R[i] <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const double PI = acos(-1.0);   const long long int mod = 1e9 + 7;   const int MAXN = 1e6 + 5;   void cp() {     long long int n, i, k;     cin >> n >> k;     long long int a[n], sum = 0;     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     for (i = 1; i < n; i++) {       if (a[i] < k) {         sum += (k - a[i]) / a[0];       }     }     cout << sum;     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t;     t = 1;     cin >> t;     while (t--) {       cp();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = INT_MAX;   const long long INFL = LLONG_MAX;   int dx[] = {1, -1, 0, 0};   int dy[] = {0, 0, 1, -1};   int main() {     ios_base::sync_with_stdio(0);     cout.precision(15);     cout << fixed;     cout.tie(0);     cin.tie(0);     int n, k;     cin >> n >> k;     int p = 2 * n + 1;     int a[p + 2];     for (int i = 1; i <= p; i++) cin >> a[i];     a[0] = -INF;     a[p + 1] = +INF;     for (int i = 1; i <= p && k > 0; i++)       if (i % 2 == 0 && a[i] > a[i - 1] && a[i] > a[i + 1] &&           a[i] - a[i - 1] > 1 && a[i] - a[i + 1] > 1) {         k--;         a[i]--;       }     for (int i = 1; i <= p; i++) cout << a[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[300005];   long long c[300005] = {0};   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     int i;     for (i = 1; i <= n - 1; i++) cin >> a[i];     long long m = 0;     long long r = 0;     for (i = 1; i <= n - 1; i++) {       r += a[i];       m = min(m, r);     }     m = abs(m) + 1;     long long y = m;     if (m > n || c[m]) {       cout << -1;       return 0;     }     c[m] = 1;     for (i = 1; i <= n - 1; i++) {       m += a[i];       if (m > n || c[m]) {         cout << -1;         return 0;       }       c[m] = 1;     }     cout << y <<    ;     for (i = 1; i <= n - 1; i++) {       y += a[i];       cout << y <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int myrand(int m) { return (rand() * (1 << 15) + rand()) % m; }   int main() {     srand(time(0));     int n, ind, x;     cin >> n >> ind >> x;     --ind;     vector<int> left(n);     vector<bool> vis(n, 0);     for (int i = 0; i < n; ++i) left[i] = i;     vis[ind] = 1;     random_shuffle(left.begin(), left.end());     int best = -1;     for (int i = 0; i < min(1000, n); ++i) {       int v, ii = left[i];       cout <<  ?   << ii + 1 <<   n ;       fflush(stdout);       cin >> v >> ii;       if (v < x && v > best) {         ind = left[i];         best = max(best, v);       }     }     if (ind < 0) {       cout <<  ! -1 n ;       fflush(stdout);       return 0;     }     int itt, LIM = 999;     for (itt = 0; itt < LIM; ++itt) {       cout <<  ?   << ind + 1 <<   n ;       fflush(stdout);       int v;       cin >> v >> ind;       --ind;       if (v >= x) {         best = v;         break;       }       if (ind < 0) break;     }     if (itt == LIM) {       int j = 1;       while (1) {         j = j + 1;       }     }     cout <<  !   << ((best < x) ? -1 : best) <<   n ;     fflush(stdout);   }


#include <bits/stdc++.h>   using namespace std;   bool done1[33][2][2], done2[33][2][2], done3[33][2][2];   long long memo1[33][2][2], memo2[33][2][2], memo3[33][2][2];   long long lim;   int n;   long long dp1(int, int, int);   long long dp2(int, int, int);   long long dp3(int, int, int);   long long dp1(int i, int s1, int s2) {     if (done1[i][s1][s2]) return memo1[i][s1][s2];     done1[i][s1][s2] = true;     long long &ret = memo1[i][s1][s2] = 0;     int hd = (((lim) >> (n - 1 - i)) & 1), rr = (((lim) >> (i)) & 1);     if (i * 2 + 1 == n) {       if (!s1)         ret = 1;       else if (s2)         ret = hd != 0;       else {         ret = 1;       }     } else if (i * 2 == n) {       if (s1 && s2)         ret = 0;       else         ret++;     } else {       ret += dp2(i + 1, s1 && hd == 0, 0 > rr || (0 == rr && s2));       ret += dp3(i + 1, s1 && hd == 0, 1 > rr || (1 == rr && s2));     }     return ret;   }   long long dp2(int i, int s1, int s2) {     if (done2[i][s1][s2]) return memo2[i][s1][s2];     done2[i][s1][s2] = true;     long long &ret = memo2[i][s1][s2] = 0;     int hd = (((lim) >> (n - 1 - i)) & 1), rr = (((lim) >> (i)) & 1);     if (i * 2 + 1 == n) {       if (!s1)         ret = 2;       else if (s2)         ret = hd != 0;       else {         ret = hd + 1;       }     } else if (i * 2 == n) {       if (s1 && s2)         ret = 0;       else         ret++;     } else {       for (int x = 0; x <= (s1 ? hd : 1); x++)         ret += dp2(i + 1, s1 && hd == x, x > rr || (x == rr && s2));       int ns1 = s1 && hd == 0, ns2 = 1 > rr || (1 == rr && s2);       if (!ns1)         ret += 1LL << (n - 2 * (i + 1));       else         ret += ((lim >> (i + 1)) & ((1LL << (n - 2 * (i + 1))) - 1)) + 1;       if (ns1 && ns2) ret--;     }     return ret;   }   long long dp3(int i, int s1, int s2) {     if (done3[i][s1][s2]) return memo3[i][s1][s2];     done3[i][s1][s2] = true;     long long &ret = memo3[i][s1][s2] = 0;     int hd = (((lim) >> (n - 1 - i)) & 1), rr = (((lim) >> (i)) & 1);     if (i * 2 + 1 == n) {       if (!s1)         ret = 1;       else if (s2)         ret = hd != 0;       else {         ret = 1;       }     } else if (i * 2 == n) {       if (s1 && s2)         ret = 0;       else         ret++;     } else {       ret += dp3(i + 1, s1 && hd == 0, 1 > rr || (1 == rr && s2));       if (!s1 || hd == 1)         ret += dp3(i + 1, s1 && hd == 1, 0 > rr || (0 == rr && s2));       int ns1 = s1 && hd == 0, ns2 = 0 > rr || (0 == rr && s2);       if (!ns1)         ret += 1LL << (n - 2 * (i + 1));       else         ret += ((lim >> (i + 1)) & ((1LL << (n - 2 * (i + 1))) - 1)) + 1;       if (ns1 && ns2) ret--;     }     return ret;   }   void output(long long x, int n) {     for (int i = 0; i < n; i++) cout << (((x) >> (n - i - 1)) & 1);     cout <<   n ;   }   long long work(long long LIM) {     lim = LIM;     memset(done1, 0, sizeof(done1));     memset(done2, 0, sizeof(done2));     memset(done3, 0, sizeof(done3));     return dp1(0, 1, 0);   }   int main() {     long long rank;     cin >> n >> rank;     rank++;     long long lb = 0, rb = (1LL << n) - 1;     for (long long mid = (lb + rb) / 2; lb < rb - 1; mid = (lb + rb) / 2) {       if (work(mid) >= rank)         rb = mid;       else         lb = mid;     }     if (work(rb) == rank)       output(rb, n);     else       printf( -1 n );   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     long long n;     cin >> n;     map<string, long long> count;     for (long long i = 0; i < n; i++) {       string s;       cin >> s;       count[s]++;     }     string s1 =  a , s2 =  a ;     for (map<string, long long>::iterator it = count.begin(); it != count.end();          ++it) {       if (s1 ==  a )         s1 = (*it).first;       else         s2 = (*it).first;     }     if (count[s1] > count[s2])       cout << s1 <<   n ;     else       cout << s2 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int to(int x, int y) { return (x * 60) + (y); }   long long int gcd(long long int a, long long int b) {     return b == 0 ? a : gcd(b, a % b);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int arr[n + 1];     for (int i = 1; i <= n; i++) cin >> arr[i];     int mx = 0, ans = 0;     for (int i = 1; i <= n; i++) {       if (arr[i] == i and mx <= i) {         ans++;         continue;       }       if (arr[i] == i) {         continue;       }       mx = max(arr[i], mx);     }     cout << ans;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   int N;   char s[507][507];   int rX[] = {-1, 0, 1, 0};   int rY[] = {0, -1, 0, 1};   int spoj[507][507];   int rozm[1000007];   int akt[1000007];   int terRozm;   int wyn;   void dfs(int y, int x, int num) {     spoj[y][x] = num;     terRozm++;     for (int i = 0; i < 4; i++) {       int nx = x + rX[i], ny = y + rY[i];       if (nx >= 1 && nx <= N && ny >= 1 && ny <= N) {         if (s[ny][nx] ==  .  && spoj[ny][nx] == 0) dfs(ny, nx, num);       }     }   }   void dolicz(int y1, int x1, int y2, int x2, bool kk = false) {     for (int y = y1; y <= y2; y++) {       if (y < 1 || y > N) continue;       for (int x = x1; x <= x2; x++) {         if (x < 1 || x > N) continue;         if (s[y][x] ==  . ) {           int sp = spoj[y][x];           akt[sp]++;           if (akt[sp] == 1) wyn += rozm[sp];         } else if (kk) {           wyn++;         }       }     }   }   void odejmij(int y1, int x1, int y2, int x2, bool kk = false) {     for (int y = y1; y <= y2; y++) {       if (y < 1 || y > N) continue;       for (int x = x1; x <= x2; x++) {         if (x < 1 || x > N) continue;         if (s[y][x] ==  . ) {           int sp = spoj[y][x];           akt[sp]--;           if (akt[sp] == 0) wyn -= rozm[sp];         } else if (kk) {           wyn--;         }       }     }   }   int main() {     int K;     scanf( %d%d , &N, &K);     for (int i = 1; i <= N; i++) scanf( %s , s[i] + 1);     int ileSpoj = 0;     for (int i = 1; i <= N; i++)       for (int w = 1; w <= N; w++)         if (s[i][w] ==  .  && spoj[i][w] == 0) {           terRozm = 0;           dfs(i, w, ++ileSpoj);           rozm[ileSpoj] = terRozm;         }     int maxi = 0;     for (int i = 1; i <= N - K + 1; i++) {       dolicz(i, 1, i + K - 1, K, true);       dolicz(i - 1, 1, i - 1, K);       dolicz(i + K, 1, i + K, K);       dolicz(i, 0, i + K - 1, 0);       dolicz(i, K + 1, i + K - 1, K + 1);       maxi = std::max(wyn, maxi);       for (int x = 2; x <= N + 2; x++) {         odejmij(i, x - 2, i + K - 1, x - 2);         odejmij(i, x - 1, i + K - 1, x - 1, true);         dolicz(i, x - 1, i + K - 1, x - 1);         odejmij(i, x + K - 1, i + K - 1, x + K - 1);         dolicz(i, x + K - 1, i + K - 1, x + K - 1, true);         dolicz(i, x + K, i + K - 1, x + K);         odejmij(i - 1, x - 1, i - 1, x - 1);         dolicz(i - 1, x + K - 1, i - 1, x + K - 1);         odejmij(i + K, x - 1, i + K, x - 1);         dolicz(i + K, x + K - 1, i + K, x + K - 1);         maxi = std::max(wyn, maxi);       }     }     printf( %d , maxi);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, x, c, a[200001], tin[200001], tout[200001], sz[200001], seg[524288];   string s;   bool lazy[524288];   vector<int> G[200001];   void dfs(int i) {     tin[i] = ++c;     ++sz[i];     for (int j : G[i]) {       dfs(j);       sz[i] += sz[j];     }     tout[i] = c;   }   void upd(int l, int r, int s = 1, int e = n, int i = 1) {     if (lazy[i]) {       seg[i] = (e - s + 1) - seg[i];       if (s != e) {         lazy[2 * i] ^= 1;         lazy[2 * i + 1] ^= 1;       }       lazy[i] = 0;     }     if (s > r || e < l) return;     if (s >= l && e <= r) {       seg[i] = (e - s + 1) - seg[i];       if (s != e) {         lazy[2 * i] ^= 1;         lazy[2 * i + 1] ^= 1;       }       return;     }     int mid = (s + e) / 2;     upd(l, r, s, mid, 2 * i);     upd(l, r, mid + 1, e, 2 * i + 1);     seg[i] = seg[2 * i] + seg[2 * i + 1];   }   int que(int l, int r, int s = 1, int e = n, int i = 1) {     if (lazy[i]) {       seg[i] = (e - s + 1) - seg[i];       if (s != e) {         lazy[2 * i] ^= 1;         lazy[2 * i + 1] ^= 1;       }       lazy[i] = 0;     }     if (s > r || e < l) return 0;     if (s >= l && e <= r) return seg[i];     int mid = (s + e) / 2;     return que(l, r, s, mid, 2 * i) + que(l, r, mid + 1, e, 2 * i + 1);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 2; i <= n; ++i) {       cin >> x;       G[x].push_back(i);     }     for (int i = 1; i <= n; ++i) cin >> a[i];     dfs(1);     for (int i = 1; i <= n; ++i)       if (a[i]) upd(tin[i], tin[i]);     cin >> m;     while (m--) {       cin >> s >> x;       if (s ==  pow )         upd(tin[x], tout[x]);       else         cout << que(tin[x], tout[x]) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e8 + 1;   vector<vector<int> > adj;   vector<bool> visited;   int contadorNodos = 0;   void DFS(int nodo) {     contadorNodos++;     for (auto it : adj[nodo]) {       DFS(it);     }   }   void letras(string &s) {     for (int i = 0; i < s.size(); i++) {       s[i] = tolower(s[i]);     }   }   int main() {     int n;     cin >> n;     map<string, int> m;     adj.resize(205);     visited.resize(205, false);     int j = 1;     for (int i = 0; i < n; i++) {       string a, b, c;       cin >> a >> b >> c;       letras(a);       letras(c);       if (m[a] == 0) {         m[a] = j;         j++;       }       if (m[c] == 0) {         m[c] = j;         j++;       }       adj[m[a]].push_back(m[c]);     }     int res = 0;     for (int i = 1; i < j; i++) {       contadorNodos = 0;       DFS(i);       res = max(res, contadorNodos);     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   const bool online_judge = true;   const long long inf = 1LL << 60;   long long toInt(string s) {     long long res;     stringstream ss;     ss << s;     ss >> res;     return res;   }   string toString(long long n) {     stringstream ss;     ss << n;     return ss.str();   }   long long w, h;   vector<string> f;   typedef struct {     long long x;     long long y;     long long dp;     long long cp;   } state;   state memo[55][55][5][5];   long long dx[4] = {0, 1, 0, -1};   long long dy[4] = {-1, 0, 1, 0};   long long ex[4] = {-1, 0, 1, 0};   long long ey[4] = {0, -1, 0, 1};   state solve(long long y, long long x, long long dp, long long cp) {     if (memo[y][x][dp][cp].x != -1) return memo[y][x][dp][cp];     state ans;     long long nx = x + dx[dp];     long long ny = y + dy[dp];     long long mx = x + ex[(dp + (cp ? 2 : 0)) % 4];     long long my = y + ey[(dp + (cp ? 2 : 0)) % 4];     if (f[ny][nx] == f[y][x]) {       ans = solve(ny, nx, dp, cp);     } else if (f[my][mx] == f[y][x]) {       ans = solve(my, mx, dp, cp);     } else {       char c = f[ny][nx];       if (c ==  0 ) {         if (cp == 0) {           ans.y = y;           ans.x = x;           ans.dp = dp;           ans.cp = 1;         } else {           ans.y = y;           ans.x = x;           ans.dp = (dp + 1) % 4;           ans.cp = 0;         }       } else {         ans.x = nx;         ans.y = ny;         ans.dp = dp;         ans.cp = cp;       }     }     memo[y][x][dp][cp] = ans;     return ans;   }   void run() {     long long m, n;     cin >> m >> n;     for (long long _ = 0; _ < m; _++) {       string tmp;       cin >> tmp;       f.push_back(tmp);     }     w = f[0].size();     h = m;     for (long long i = 0; i < m; i++) {       f[i] =  0  + f[i] +  0 ;     }     f.insert(f.begin(), string(w + 2,  0 ));     f.push_back(string(w + 2,  0 ));     for (long long i = 1; i <= h; i++) {       for (long long j = 1; j <= w; j++) {         for (long long dp = 0; dp <= 3; dp++) {           for (long long cp = 0; cp <= 1; cp++) {             memo[i][j][dp][cp].x = -1;           }         }       }     }     for (long long i = 1; i <= h; i++) {       for (long long j = 1; j <= w; j++) {         for (long long dp = 0; dp <= 3; dp++) {           for (long long cp = 0; cp <= 1; cp++) {             solve(i, j, dp, cp);           }         }       }     }     long long x = 1, y = 1, dp = 1, cp = 0;     while (n--) {       state next = memo[y][x][dp][cp];       x = next.x;       y = next.y;       dp = next.dp;       cp = next.cp;     }     cout << f[y][x] << endl;   }   int main(int argc, char *argv[]) {     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 100;   struct node {     int flag, a, b;     long long sum;   } t[maxn * 8];   int a[maxn], cnt[maxn * 2], js[maxn * 2];   void pushdown(int x, int l, int r) {     if (t[x].flag) {       int num = t[x].flag;       t[2 * x].sum += num * l;       t[2 * x + 1].sum += num * r;       t[2 * x].flag += num;       t[2 * x + 1].flag += num;       t[x].flag = 0;     }   }   int ask(int x, int l, int r, int pos) {     int mid;     if (l == r) {       return t[x].sum;     }     mid = (l + r) / 2;     pushdown(x, mid - l + 1, r - mid);     if (pos <= mid)       return ask(2 * x, l, mid, pos);     else       return ask(2 * x + 1, mid + 1, r, pos);   }   void add(int x, int l, int r, int ql, int qr, int val) {     int mid;     if (ql == l && qr == r) {       t[x].flag += val;       t[x].sum += (qr - ql + 1);       return;     }     mid = (l + r) / 2;     pushdown(x, mid - l + 1, r - mid);     if (qr <= mid) {       add(2 * x, l, mid, ql, qr, val);     } else {       if (ql > mid)         add(2 * x + 1, mid + 1, r, ql, qr, val);       else {         add(2 * x, l, mid, ql, mid, val);         add(2 * x + 1, mid + 1, r, mid + 1, qr, val);       }     }     t[x].sum = t[2 * x].sum + t[2 * x + 1].sum;   }   void build(int x, int l, int r) {     int mid;     t[x].flag = 0;     t[x].sum = 0;     if (l == r) {       return;     }     mid = (l + r) / 2;     build(2 * x, l, mid);     build(2 * x + 1, mid + 1, r);   }   int top;   node s[maxn];   int cmp(node a, node b) { return a.sum < b.sum; }   int main() {     node e;     int T, n, flag, ans, k, up;     scanf( %d , &T);     while (T--) {       scanf( %d%d , &n, &k);       for (int i = 1; i <= 2 * k; i++) {         cnt[i] = 0;         js[i] = 0;       }       build(1, 1, k * 2);       for (int i = 1; i <= n; i++) {         scanf( %d , &a[i]);       }       if (n == 2) {         printf( 0 n );         continue;       }       top = 0;       if (n % 2)         up = n / 2 + 1;       else         up = n / 2;       for (int i = 1; i <= up; i++) {         e.a = a[i];         e.b = a[n - i + 1];         if (e.a > e.b) swap(e.a, e.b);         e.sum = e.a + e.b;         s[++top] = e;         js[e.sum]++;       }       sort(s + 1, s + top + 1, cmp);       int last = 0;       ans = n;       for (int i = 1; i <= top; i++) {         e = s[i];         if (e.sum == last) {           add(1, 1, 2 * k, e.a + 1, e.b + k, 1);           continue;         }         cnt[e.sum] += n / 2 - js[e.sum] + i - 1 - ask(1, 1, 2 * k, e.sum);         add(1, 1, 2 * k, e.a + 1, e.b + k, 1);         last = e.sum;       }       for (int i = 1; i <= top / 2; i++) {         swap(s[i], s[top - i + 1]);       }       build(1, 1, k * 2);       last = 0;       ans = n;       for (int i = 1; i <= top; i++) {         e = s[i];         if (e.sum == last) {           add(1, 1, 2 * k, e.a + 1, e.b + k, 1);           continue;         }         cnt[e.sum] += i - 1 - ask(1, 1, 2 * k, e.sum);         ans = min(ans, cnt[e.sum]);         add(1, 1, 2 * k, e.a + 1, e.b + k, 1);         last = e.sum;       }       for (int i = 1; i <= 2 * k; i++) {         if (js[i]) continue;         ans = min(ans, n - ask(1, 1, 2 * k, i));       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, a[100001], b[100001];   vector<long long> v[100001];   signed main() {     cin >> n;     long long i;     for (i = 1; i < n + 1; i++) {       cin >> a[i];       v[a[i]].push_back(i);     }     bool ok = 1;     long long clr = 1;     for (i = 0; i < 100001; i++) {       if (!v[i].empty()) {         long long k = v[i].size();         long long j;         if (k % (n - i) != 0) {           ok = 0;           break;         }         long long t = k / (n - i);         long long temp = 1;         j = 0;         while (temp <= t) {           while (j < temp * (n - i)) {             b[v[i][j]] = clr;             j++;           }           clr++;           temp++;         }       }     }     if (ok) {       cout <<  Possible n ;       for (i = 1; i < n + 1; i++) cout << b[i] <<    ;     } else       cout <<  Impossible ;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool uin(T &a, T b) {     return a > b ? (a = b, true) : false;   }   template <class T>   bool uax(T &a, T b) {     return a < b ? (a = b, true) : false;   }   const long long maxn = 1000001, mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int ar[n];     map<int, int> mapp;     for (long long i = 0; i < (long long)(n); ++i) {       cin >> ar[i];       mapp[ar[i]] = i;     }     set<int> s;     s.insert(-1);     s.insert(n);     int ans = 0;     for (int i = n; i >= 1; i--) {       int idx = mapp[i];       auto it = s.lower_bound(idx);       auto iit = s.upper_bound(idx);       if (it != s.begin()) it--;       int a = *it + 1;       int b = *iit - 1;       if (idx - a < b - idx) {         for (long long j = (long long)(a); j <= (long long)(idx - 1); ++j) {           int acha = mapp[i - ar[j]];           if (acha > idx && acha <= b) {             ans++;           }         }       } else {         for (long long j = (long long)(idx + 1); j <= (long long)(b); ++j) {           int acha = mapp[i - ar[j]];           if (acha < idx && acha >= a) {             ans++;           }         }       }       s.insert(mapp[i]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Trie {     Trie *nx[2];     int cnt;     Trie() {       nx[0] = nx[1] = nullptr;       cnt = 0;     }   };   Trie *root;   void ins(long long x) {     Trie *now = root;     for (int bit = 45; bit >= 0; --bit) {       int p = (x >> bit) & 1LL;       if (now->nx[p] == nullptr) now->nx[p] = new Trie();       now = now->nx[p];       now->cnt++;     }   }   long long query(long long x) {     Trie *now = root;     long long ret = 0LL;     for (int bit = 45; bit >= 0; --bit) {       int p = (x >> bit) & 1LL;       p ^= 1;       if (now->nx[p] != nullptr && now->nx[p]->cnt > 0) {         ret += 1LL << bit;         now = now->nx[p];       } else {         now = now->nx[p ^ 1];       }       if (now == nullptr || now->cnt == 0) break;     }     return ret;   }   const int MXSZ = 1e5;   int n;   long long a[MXSZ + 5];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     root = new Trie();     cin >> n;     long long curr = 0LL;     for (int i = 1; i <= n; i++) {       cin >> a[i];       curr ^= a[i];       ins(curr);     }     long long mx = 0LL;     mx = max(mx, query(0));     curr = 0LL;     for (int i = n; i >= 1; --i) {       curr ^= a[i];       mx = max(mx, query(curr));     }     cout << mx <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int b[5000];   int f[5000];   int main() {     scanf(  %d , &n);     for (int i = 0; i < (int)(n); i++) scanf(  %d , b + i);     for (int i = 0; i < (int)(n); i++) b[i]--;     for (int i = 0; i < n; i++) {       int c[5000] = {};       pair<int, int> best = make_pair(0, 0);       for (int j = i; j < n; j++) {         c[b[j]]++;         best = max(best, make_pair(c[b[j]], -b[j]));         f[-best.second]++;       }     }     for (int i = 0; i < (int)(n); i++) printf( %d  , f[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   const int maxn = 3000010;   int n, b;   int pre[maxn], vis[maxn];   vector<int> prime;   struct init {     init() {       memset(pre, 0, sizeof pre);       prime.clear();       for (int i = 2; i < maxn; i++) {         if (pre[i] == 0) {           pre[i] = i;           prime.emplace_back(i);         }         for (int j = 0; j < prime.size() && i * prime[j] < maxn; j++) {           if (prime[j] > pre[i]) break;           pre[i * prime[j]] = prime[j];         }       }     }   } init_;   bool ok(int x) {     if (x == 1) return 0;     return pre[x] == 0;   }   signed main() {     while (cin >> n) {       memset(vis, 0, sizeof vis);       for (int i = 1; i <= 2 * n; i++) {         scanf( %d , &b);         vis[b]++;       }       for (int i = maxn - 1; i > 1; i--) {         if (pre[i] == i) continue;         while (vis[i] > 0) {           printf( %d  , i);           vis[i]--;           vis[i / pre[i]]--;         }       }       for (int i = 2; i < maxn; i++) {         while (vis[i] > 0) {           printf( %d  , i);           vis[i]--;           vis[prime[i - 1]]--;         }       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0), cin.tie(0);     string s;     cin >> s;     sort(s.begin(), s.end());     reverse(s.begin(), s.end());     cout << s[0];     for (long long int(i) = (1); (i) < (s.length()); (i)++) {       if (s[i] == s[i - 1])         cout << s[i];       else         break;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       if ((i * (i + 1)) / 2 == n) {         cout <<  YES  << endl;         return 0;       }       if ((i * (i + 1)) / 2 > n) {         cout <<  NO  << endl;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool KT(long long k, long long n) {     long long take = 0, cur = n;     while (cur > 0) {       long long temp = min(k, cur);       take += temp;       cur -= temp;       cur -= cur / 10;     }     if (take * 2 >= n) return true;     return false;   }   int main() {     long long n;     cin >> n;     long long left = 1, right = n, res = 0;     while (left <= right) {       long long mid = (left + right) / 2;       if (KT(mid, n)) {         res = mid;         right = mid - 1;       } else {         left = mid + 1;       }     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct in {     int x, y;   } a[300000];   int get(int prex, int prey, int x, int y, int k) {     int num, ans = 100000000;     if (prex <= k) {       num = (prex + x + k - 1) / k - 1;       if (y == num)         ans = prex + x - num * k;       else if (y > num && y <= (long long)x * k)         ans = 1;     }     if (prey <= k) {       num = (x + k - 1) / k - 1;       if (y == num)         ans = min(ans, x - num * k);       else if (y > num && y - k + prey <= (long long)(x - 1) * k)         ans = 1;     }     return ans;   }   int main() {     int N, K, pre0 = 0, pre1 = 0, next0, next1;     scanf( %d%d , &N, &K);     for (int i = 1; i <= N; i++) scanf( %d , &a[i].x);     for (int i = 1; i <= N; i++) scanf( %d , &a[i].y);     for (int i = 1; i <= N; i++) {       next0 = get(pre0, pre1, a[i].x, a[i].y, K);       next1 = get(pre1, pre0, a[i].y, a[i].x, K);       pre0 = next0;       pre1 = next1;     }     if (pre0 <= K || pre1 <= K)       puts( YES );     else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> vi;   void primefact(int n) {     while (n % 2 == 0) {       vi.push_back(2);       n /= 2;     }     int i = 3;     while (i <= n) {       while (n % i == 0) {         vi.push_back(i);         n /= i;       }       i += 2;     }     if (n > 2) vi.push_back(n);   }   int main() {     int n, k, last = 1, i;     cin >> n >> k;     primefact(n);     if (k > vi.size())       cout <<  -1  << endl;     else {       for (i = 0; i < k - 1; i++) cout << vi[i] <<    ;       for (i = k - 1; i < vi.size(); i++) last *= vi[i];       cout << last;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   const long long INF = 1e9;   const long long MOD = 1000000007;   int prevs[20];   int main() {     ios ::sync_with_stdio(0);     cin.tie(0);     int n, q;     cin >> n >> q;     int arr[n];     for (int i = 0; i < n; i++) cin >> arr[i];     int ar[n][20];     for (int i = 0; i < n; i++)       for (int j = 0; j < 20; j++) ar[i][j] = n + 3;     for (int i = n - 1; i >= 0; i--) {       int x = 1;       for (int j = 0; j < 20; j++) {         if ((x << j) & arr[i]) {           ar[i][j] = i;           if (prevs[j] != 0)             for (int k = 0; k < 20; k++)               ar[i][k] = min(ar[i][k], ar[prevs[j]][k]);           prevs[j] = i;         }       }     }     while (q--) {       int x, y;       cin >> x >> y;       x--, y--;       if (!arr[x] || !arr[y]) {         cout <<  Fou  << endl;         continue;       }       int p = 1;       int l = 0;       for (int j = 0; j < 20; j++)         if (p << j & arr[y])           if (ar[x][j] <= y) {             l = 1;             continue;           }       if (l == 1)         cout <<  Shi  << endl;       else         cout <<  Fou  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 1000 + 10;   const int INF = 0x7f7f7f7f;   const double eps = 1e-10;   const double pi = acos(-1.0);   inline int realcmp(double a, double b) {     return (a > b + eps) ? 1 : ((a + eps < b) ? -1 : 0);   }   int main() {     double hh, mm;     ios::sync_with_stdio(0);     scanf( %lf:%lf , &hh, &mm);     hh += mm / 60;     while (realcmp(hh, 12) >= 0) hh -= 12;     printf( %.9lf  , 30.0 * hh);     printf( %.9lf n , 6.0 * mm);     return 0;   }


#include <bits/stdc++.h>   struct point {     long x;     long y;     long z;   };   const long maxn = 5001;   const double inf = 2000000000;   const double eps = 1.0e-8;   point a[maxn];   double nnew, min;   long n, q, choice1, choice2;   double d[maxn];   double sqr(double x) { return (x * x); }   double dist(point a, point b) {     return (pow(sqr(a.x - b.x) + sqr(a.y - b.y) + sqr(a.z - b.z), 0.5));   }   int main() {     scanf( %ld , &n);     for (q = 1; q <= n; q++) scanf( %ld%ld%ld , &a[q].x, &a[q].y, &a[q].z);     min = inf;     for (long h = 2; h <= n; h++) d[h] = dist(a[1], a[h]);     for (choice1 = 2; choice1 <= n; choice1++)       for (choice2 = choice1 + 1; choice2 <= n; choice2++) {         nnew = (d[choice1] + d[choice2] + dist(a[choice1], a[choice2])) / 2;         if (min - nnew > eps) min = nnew;       }     printf( %0.8lf , min);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, i, y, ans = 0, q;     scanf( %d%d , &n, &x);     y = int(sqrt(x));     if (n < y) y = n;     for (i = 1; i <= y; i++) {       if (x % i == 0) {         q = x / i;         if (q <= n) {           if (i == q)             ans++;           else             ans += 2;         }       }     }     if (x == 1)       printf( %d n , 1);     else       printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 1e9;   struct Data {     int p1, p2, p3, p4;     Data(int a = mx, int b = mx, int c = mx, int d = mx) {       p1 = a, p2 = b, p3 = c, p4 = d;     }   };   inline Data UN(Data a, Data b) {     Data c;     c.p1 = min(a.p1 + b.p1, a.p2 + b.p3), c.p2 = min(a.p1 + b.p2, a.p2 + b.p4);     c.p3 = min(a.p3 + b.p1, a.p4 + b.p3), c.p4 = min(a.p3 + b.p2, a.p4 + b.p4);     c.p1 = min(c.p1, mx), c.p2 = min(c.p2, mx), c.p3 = min(c.p3, mx),     c.p4 = min(c.p4, mx);     return c;   }   struct node {     int l, r;     Data s;   } T[200010 << 2];   char G[3][200010];   void build(int rt, int l, int r) {     T[rt].l = l, T[rt].r = r;     int mid = (l + r) >> 1;     if (l == r) {       if (G[1][l] ==  . ) T[rt].s.p1 = 1;       if (G[2][l] ==  . ) T[rt].s.p4 = 1;       if (G[1][l] ==  .  && G[2][l] ==  . ) T[rt].s.p2 = T[rt].s.p3 = 2;       return;     }     build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r);     T[rt].s = UN(T[rt << 1].s, T[rt << 1 | 1].s);   }   Data query(int rt, int start, int end) {     int l = T[rt].l, r = T[rt].r, mid = (l + r) >> 1;     if (start <= l && r <= end) return T[rt].s;     if (end <= mid) return query(rt << 1, start, end);     if (start > mid) return query(rt << 1 | 1, start, end);     return UN(query(rt << 1, start, end), query(rt << 1 | 1, start, end));   }   int main() {     int n, q;     scanf( %d%d , &n, &q);     scanf( %s%s , G[1] + 1, G[2] + 1);     build(1, 1, n);     while (q--) {       int a, b;       scanf( %d%d , &a, &b);       int l = a > n ? a - n : a, r = b > n ? b - n : b, res;       if (l > r) swap(l, r), swap(a, b);       Data tmp = query(1, l, r);       if (a <= n && b <= n) res = tmp.p1;       if (a <= n && b > n) res = tmp.p2;       if (a > n && b <= n) res = tmp.p3;       if (a > n && b > n) res = tmp.p4;       if (res < 1e8)         printf( %d n , res - 1);       else         printf( -1 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t, x, y, a, b;     scanf( %lld , &t);     while (t--) {       scanf( %lld %lld %lld %lld , &x, &y, &a, &b);       bool yes = false;       int i = 0;       int tens = 1000000;       while (x <= y) {         if (x == y) {           yes = true;           break;         }         while (x + a * tens > y - b * tens && tens > 1) tens /= 10;         x += tens * a;         y -= tens * b;         i += tens;       }       printf( %d n , yes ? i : -1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(228);   const int N = 404;   int mod;   int fact[N], d0[N], d[N], d2[N][N];   int main() {     int qq;     cin >> qq >> mod;     fact[0] = 1;     for (int i = 1; i < N; ++i) fact[i] = (1LL * fact[i - 1] * i) % mod;     d[1] = 1;     d0[1] = 1;     for (int n = 2; n < N; ++n) {       d0[n] = fact[n];       for (int k = 1; k < n; ++k)         d0[n] = (d0[n] - 1LL * d0[k] * fact[n - k]) % mod;       if (d0[n] < 0) d0[n] += mod;     }     d2[0][0] = 1;     for (int n = 1; n < N; ++n) {       d2[n][1] = fact[n];       for (int k = 2; k <= n; ++k)         for (int x = 1; x < n; ++x)           d2[n][k] = (d2[n][k] + 1LL * fact[x] * d2[n - x][k - 1]) % mod;     }     for (int n = 2; n < N; ++n) {       int dz = 0;       for (int x = 1; x < n - 1; ++x)         for (int y = x; y < n - 1; ++y) {           int c = fact[y - x + 1];           c = (1LL * c * d0[x]) % mod;           c = (1LL * c * d0[n - y - 1]) % mod;           dz += c;           if (dz >= mod) dz -= mod;         }       dz = (dz * 2) % mod;       int cd = fact[n];       cd = (cd - dz + mod) % mod;       for (int k = 3; k < n; ++k) {         cd = (cd - 1LL * d2[n][k] * d[k]) % mod;       }       if (n != 2) {         for (int x = 1; x < n; ++x) cd = (cd - 1LL * d0[x] * d0[n - x]) % mod;         for (int x = 1; x < n; ++x) cd = (cd - 1LL * d0[x] * d0[n - x]) % mod;       }       cd = (cd + mod) % mod;       d[n] = cd;     }     while (qq--) {       int x;       cin >> x;       cout << d[x] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2020;   vector<tuple<int, int, int> > edges;   vector<int> g[N];   vector<int> back_edge[N];   bool global;   int num[N], low[N], low2[N], cnt, pai[N], pw[N], aux[N], prev_pai[N],       prev_pw[N], sobe[N], prev_sobe[N];   int edg_used, edg_u, edg_v, edg_w;   void dfs(int u) {     num[u] = low[u] = ++cnt;     bool flag = false;     for (auto w : g[u]) {       int x, v, p;       tie(x, v, p) = edges[w];       if (v == u) swap(v, x);       if (edg_used == w) continue;       if (!num[v]) {         pai[v] = u;         pw[v] = p;         sobe[v] = w;         dfs(v);         low[u] = min(low[u], low[v]);       } else if (flag or pai[u] != v) {         low[u] = min(low[u], num[v]);         if (!global and num[u] > num[v]) back_edge[u].push_back(w);       } else if (pai[u] == v)         flag = true;     }   }   int main(void) {     int n, m;     scanf( %d %d , &n, &m);     int s, t;     scanf( %d %d , &s, &t);     for (int i = 0; i < m; i++) {       int u, v, w;       scanf( %d %d %d , &u, &v, &w);       g[u].push_back(i);       g[v].push_back(i);       edges.emplace_back(u, v, w);     }     edg_used = -1;     dfs(s);     global = true;     int S = s;     if (!low[t]) {       printf( 0 n0 n );       return 0;     }     int ans = 0x3f3f3f3f;     int edg;     int v = t;     while (v != s) {       int u = pai[v];       if (low[v] > num[u]) {         if (pw[v] < ans) edg = sobe[v];         ans = min(ans, pw[v]);       }       v = u;     }     int dupla = INT_MAX;     int x, y;     for (int i = 1; i <= n; i++) {       if (back_edge[i].empty()) continue;       int k = 0, ans = 0x3f3f3f3f;       for (int j = 0; j < back_edge[i].size(); j++) {         int e = back_edge[i][j];         int x, v, p;         tie(x, v, p) = edges[e];         if (v == i) swap(v, x);         if (num[v] < ans) k = j;       }       vector<int> c[5];       cnt = 0;       for (int j = 1; j <= n; j++) {         c[0].push_back(num[j]);         c[1].push_back(low[j]);         c[2].push_back(pai[j]);         c[3].push_back(pw[j]);         c[4].push_back(sobe[j]);         num[j] = low[j] = 0;       }       tie(edg_u, edg_v, edg_w) = edges[back_edge[i][k]];       edg_used = back_edge[i][k];       dfs(s);       ans = 0x3f3f3f3f;       int v = t, edg;       while (v != s) {         int u = pai[v];         if (low[v] > num[u]) {           if (pw[v] < ans) edg = sobe[v];           ans = min(ans, pw[v]);         }         v = u;       }       if (ans != 0x3f3f3f3f and ans + edg_w < dupla) {         dupla = ans + edg_w;         x = back_edge[i][k];         y = edg;       }       for (int j = 1; j <= n; j++) {         num[j] = c[0][j - 1];         low[j] = c[1][j - 1];         pai[j] = c[2][j - 1];         pw[j] = c[3][j - 1];         sobe[j] = c[4][j - 1];       }     }     for (int i = 1; i <= n; i++) {       prev_pai[i] = pai[i];       prev_pw[i] = pw[i];       prev_sobe[i] = sobe[i];     }     for (int i = 1; i <= n; i++) {       if (i == s) continue;       cnt = 0;       for (int j = 1; j <= n; j++) num[j] = low[j] = 0;       tie(edg_u, edg_v, edg_w) = edges[prev_sobe[i]];       edg_used = prev_sobe[i];       dfs(s);       if (!low[t]) continue;       int ans = 0x3f3f3f3f, v = t, edg;       while (v != s) {         int u = pai[v];         if (low[v] > num[u]) {           if (pw[v] < ans) edg = sobe[v];           ans = min(ans, pw[v]);         }         v = u;       }       if (ans != 0x3f3f3f3f and ans + edg_w < dupla) {         dupla = ans + edg_w;         x = prev_sobe[i];         y = edg;       }     }     if (ans == 0x3f3f3f3f and dupla == INT_MAX) {       printf( -1 n );     } else if (ans < dupla and ans != 0x3f3f3f3f)       printf( %d n1 n%d n , ans, edg + 1);     else       printf( %d n2 n%d %d n , dupla, x + 1, y + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int i, j, k;     j = (n * 2) - 1;     cout << j <<              <<  2 ;     cout << endl <<  1  2 ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   vector<pair<int, int> > adj[N];   int ans[N];   int off[N];   int lvl[N], anc[N][25];   int n, a, b, k;   void dfs(int u = 0, int par = 0, int l = 0) {     lvl[u] = l;     anc[u][0] = par;     for (auto nxt : adj[u])       if (nxt.first != par) dfs(nxt.first, u, l + 1);   }   void buildLCA() {     int lg = ceil(log2(n));     for (int j = 1; j < lg; j++)       for (int i = 0; i < n; i++) anc[i][j] = anc[anc[i][j - 1]][j - 1];   }   int LCA(int i, int j) {     int lg = ceil(log2(n));     int st = lg;     if (lvl[i] > lvl[j]) swap(i, j);     int cur = lvl[j];     for (; st >= 0; st--)       if (cur - (1 << st) >= lvl[i]) cur -= (1 << st), j = anc[j][st];     if (i == j) return 2 * i - j;     cur = lvl[i];     for (st = lg; st >= 0; st--)       if (anc[i][st] != anc[j][st])         cur -= (1 << st), i = anc[i][st], j = anc[j][st];     return anc[i][0];   }   int solve(int u = 0, int par = 0) {     int cnt = off[u];     for (auto nxt : adj[u]) {       if (par == nxt.first) continue;       int x = solve(nxt.first, u);       ans[nxt.second] = x;       cnt += x;     }     return cnt;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       scanf( %d%d , &a, &b);       a--, b--;       adj[a].push_back({b, i});       adj[b].push_back({a, i});     }     dfs();     buildLCA();     scanf( %d , &k);     for (int i = 0; i < k; i++) {       scanf( %d%d , &a, &b);       a--, b--;       off[a]++, off[b]++;       off[LCA(a, b)] -= 2;     }     solve();     for (int i = 0; i < n - 1; i++) printf( %d%c , ans[i],    n [i == n - 2]);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   struct Point {     int x, y, type, index;   } num[20005];   int ans[10005];   Point c;   bool cmp(Point a, Point b) {     return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x) > 0;   }   bool cmp2(Point a, Point b) { return (a.x < b.x) || (a.x == b.x && a.y < b.y); }   void f(int l, int r) {     if (l >= r) return;     int i;     swap(num[l], num[min_element(num + l, num + r + 1, cmp2) - num]);     for (i = l + 1; i <= r; i++) {       num[i].x -= num[l].x;       num[i].y -= num[l].y;     }     num[l].x = num[l].y = 0;     c = num[l];     sort(num + l + 1, num + r + 1, cmp);     int pre = num[l].type, minv = 1000000000, id = -1;     for (i = l + 1; i <= r; i++) {       pre += num[i].type;       if (!pre && num[i].type != num[l].type && max(i - l - 1, r - i) < minv) {         minv = max(i - l - 1, r - i);         id = i;       }     }     if (num[l].type == 1)       ans[num[id].index] = num[l].index;     else       ans[num[l].index] = num[id].index;     f(l + 1, id - 1);     f(id + 1, r);   }   int main() {     int i, n;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d%d , &num[i].x, &num[i].y);       num[i].type = -1;       num[i].index = i + 1;     }     for (i = 0; i < n; i++) {       scanf( %d%d , &num[n + i].x, &num[n + i].y);       num[n + i].type = 1;       num[n + i].index = i + 1;     }     f(0, 2 * n - 1);     for (i = 1; i <= n; i++) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int mn = 201000, inf = 1 << 30;   struct node {     int x, y;   } pt[mn], sub[mn];   struct point {     int k, x, y, r;   } e[mn * 2];   int s1[mn * 2], s2[mn * 2];   int xs[mn * 2], dis[mn], sa[mn], L;   int n, m, i, j, step, lo, hi, mid;   bool cmp(node a, node b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }   bool cmp1(point a, point b) { return a.x < b.x || (a.x == b.x && a.k < b.k); }   bool subway(int i, int j) { return dis[i] > dis[j]; }   struct tree {     int tt, rt[mn], s[mn * 20], le[mn * 20], ri[mn * 20];     int get(const int &p, int l, int r, int x, int y) {       if (x <= l && y >= r) return s[p];       int m = (l + r) / 2, res = 0;       if (x <= m) res += get(le[p], l, m, x, y);       if (y > m) res += get(ri[p], m + 1, r, x, y);       return res;     }     void add(const int &t, int &p, int l, int r, int k) {       s[p = ++tt] = s[t] + 1;       if (l == r) return;       int m = (l + r) / 2;       if (k <= m)         add(le[t], le[p], l, m, k), ri[p] = ri[t];       else         add(ri[t], ri[p], m + 1, r, k), le[p] = le[t];     }     void ins(int i, int v) { add(rt[i - 1], rt[i], 1, L, v); }     int query(int x, int y, int l, int r) {       if (x > y || l > r || x > m) return 0;       return get(rt[y], 1, L, l, r) - get(rt[x - 1], 1, L, l, r);     }   } T;   point eva(int k, int x, int y, int r) {     point res;     res.k = k, res.x = x, res.y = y, res.r = r;     return res;   }   void cross(int &l, int &r, int &x, int &y, int i, int t) {     l = max(l, pt[i].x - t), x = max(x, pt[i].y - t);     r = min(r, pt[i].x + t), y = min(y, pt[i].y + t);   }   void add(int *s, int p, int v) {     for (; p <= L; p += p & (-p)) s[p] = max(s[p], v);   }   int query(int *s, int p) {     int res = -inf;     for (; p; p -= p & (-p)) res = max(res, s[p]);     return res;   }   void get(point p) {     int y = lower_bound(xs + 1, xs + 1 + L, p.y) - xs;     if (p.k == 0)       add(s1, y, p.x + p.y), add(s2, L - y + 1, p.x - p.y);     else {       int t1 = p.x + p.y - query(s1, y), t2 = p.x - p.y - query(s2, L - y + 1);       dis[p.r] = min(dis[p.r], min(t1, t2));     }   }   void ugly(node &p) {     int X = p.x - p.y, Y = p.x + p.y;     p.x = X, p.y = Y;   }   void nearest() {     int i, tt = 0;     for (i = 1; i <= n; ++i) xs[++L] = pt[i].y;     for (i = 1; i <= m; ++i) xs[++L] = sub[i].y;     sort(xs + 1, xs + 1 + L);     L = unique(xs + 1, xs + 1 + L) - xs - 1;     for (i = 1; i <= n; ++i) e[++tt] = eva(1, pt[i].x, pt[i].y, i);     for (i = 1; i <= m; ++i) e[++tt] = eva(0, sub[i].x, sub[i].y, i);     sort(e + 1, e + 1 + tt, cmp1);     fill(dis + 1, dis + 1 + n, inf);     fill(s1 + 1, s1 + L + 1, -inf), fill(s2 + 1, s2 + 1 + L, -inf);     for (i = 1; i <= tt; ++i) get(e[i]);     for (i = 1; i <= tt; ++i) e[i].x = -e[i].x;     sort(e + 1, e + 1 + tt, cmp1);     fill(s1 + 1, s1 + L + 1, -inf), fill(s2 + 1, s2 + 1 + L, -inf);     for (i = 1; i <= tt; ++i) get(e[i]);   }   int main() {     scanf( %d%d , &n, &m);     for (i = 1; i <= n; ++i) scanf( %d%d , &pt[i].x, &pt[i].y);     for (i = 1; i <= m; ++i) scanf( %d%d , &sub[i].x, &sub[i].y);     nearest();     for (i = 1; i <= n; ++i) ugly(pt[i]);     for (i = 1; i <= m; ++i) ugly(sub[i]);     sort(sub + 1, sub + 1 + m, cmp);     L = 0;     for (i = 1; i <= n; ++i) xs[++L] = pt[i].y;     for (i = 1; i <= m; ++i) xs[++L] = sub[i].y;     sort(xs + 1, xs + 1 + L), L = unique(xs + 1, xs + 1 + L) - xs - 1;     for (i = 1; i <= m; ++i)       T.ins(i, lower_bound(xs + 1, xs + 1 + L, sub[i].y) - xs);     for (i = 1; i <= n; ++i) sa[i] = i;     sort(sa + 1, sa + 1 + n, subway);     lo = 0, hi = dis[sa[1]];     int mix, mxx, miy, mxy, ct, l, r, x, y;     node su;     while (lo < hi) {       int mid = (lo + hi) / 2;       mix = miy = -inf, mxx = mxy = inf;       for (i = 1; i <= n; ++i) {         if (dis[sa[i]] > mid)           cross(mix, mxx, miy, mxy, sa[i], mid);         else {           ct = mid - dis[sa[i]];           su.y = -inf, su.x = mix - ct;           l = lower_bound(sub + 1, sub + 1 + m, su, cmp) - sub;           su.y = inf, su.x = mxx + ct;           r = lower_bound(sub + 1, sub + 1 + m, su, cmp) - sub - 1;           x = lower_bound(xs + 1, xs + 1 + L, miy - ct) - xs,           y = lower_bound(xs + 1, xs + 1 + L, mxy + ct + 1) - xs - 1;           if (T.query(l, r, x, y))             break;           else             cross(mix, mxx, miy, mxy, sa[i], mid);         }         if (mix > mxx || miy > mxy) break;       }       if (mix <= mxx && miy <= mxy)         hi = mid;       else         lo = mid + 1;     }     printf( %d n , lo);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void smin(T &a, U b) {     if (a > b) a = b;   }   template <typename T, typename U>   inline void smax(T &a, U b) {     if (a < b) a = b;   }   inline void gn(long long &first) {     int sg = 1;     char c;     while (((c = getchar()) <  0  || c >  9 ) && c !=  - )       ;     c ==  -  ? (sg = -1, first = 0) : (first = c -  0 );     while ((c = getchar()) >=  0  && c <=  9 ) first = first * 10 + c -  0 ;     first *= sg;   }   inline void gn(int &first) {     long long t;     gn(t);     first = t;   }   inline void gn(unsigned long long &first) {     long long t;     gn(t);     first = t;   }   inline void gn(double &first) {     double t;     scanf( %lf , &t);     first = t;   }   inline void gn(long double &first) {     double t;     scanf( %lf , &t);     first = t;   }   int power(int a, int b, int m, int ans = 1) {     for (; b; b >>= 1, a = 1LL * a * a % m)       if (b & 1) ans = 1LL * ans * a % m;     return ans;   }   int N, M, E, W;   int dis[101000];   queue<int> Q;   int pre[101000], edg[101000][3], cnt[101000], val[101000];   map<pair<int, int>, int> state, flag;   vector<int> adj[101000];   int len = 0x3f3f3f3f, tot;   void Dijk(int s) {     memset(dis, 0x3f, sizeof(dis));     Q.push(s);     dis[s] = 0;     cnt[s] = 0;     val[s] = W;     while (!Q.empty()) {       int u = Q.front();       Q.pop();       for (int i = 0; i < adj[u].size(); i++) {         int v = adj[u][i], k;         if (!state[pair<int, int>(u, v)])           k = val[u] + 1;         else           k = val[u] - 1;         if (dis[v] > dis[u] + 1) {           dis[v] = dis[u] + 1;           pre[v] = u;           val[v] = k;           Q.push(v);         } else if (dis[v] == dis[u] + 1 && val[v] > k) {           val[v] = k;           pre[v] = u;           Q.push(v);         }       }     }   }   int main() {     int u, v, w;     scanf( %d%d , &N, &M);     for (int i = 0; i < M; i++) {       scanf( %d%d%d , &u, &v, &w);       adj[u].push_back(v);       adj[v].push_back(u);       state[pair<int, int>(u, v)] = w;       state[pair<int, int>(v, u)] = w;       edg[i][0] = u;       edg[i][1] = v;       edg[i][2] = w;       W += w;     }     Dijk(1);     u = N;     while (u != 1) {       flag[pair<int, int>(u, pre[u])] = 1;       flag[pair<int, int>(pre[u], u)] = 1;       u = pre[u];     }     tot = 0;     for (int i = 0; i < M; i++) {       u = edg[i][0];       v = edg[i][1];       w = edg[i][2];       if (!flag[pair<int, int>(u, v)] && w == 1)         tot++;       else if (flag[pair<int, int>(u, v)] && w == 0)         tot++;     }     printf( %d n , tot);     for (int i = 0; i < M; i++) {       u = edg[i][0];       v = edg[i][1];       w = edg[i][2];       if (!flag[pair<int, int>(u, v)] && w == 1) {         printf( %d %d 0 n , u, v);       } else if (flag[pair<int, int>(u, v)] && w == 0) {         printf( %d %d 1 n , u, v);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 35005;   struct Node {     int l, r;     int lazy;     int Max;   } node[maxn << 2];   int n, k;   int a[maxn];   int pre[maxn];   int dp[maxn];   void push_down(int root) {     node[root << 1].lazy += node[root].lazy;     node[root << 1 | 1].lazy += node[root].lazy;     node[root << 1].Max += node[root].lazy;     node[root << 1 | 1].Max += node[root].lazy;     node[root].lazy = false;   }   void push_up(int root) {     node[root].Max = max(node[root << 1].Max, node[root << 1 | 1].Max);   }   void build(int l, int r, int root) {     node[root].l = l, node[root].r = r;     node[root].lazy = 0;     if (l == r) {       node[root].Max = dp[l];       return;     }     int mid = (l + r) >> 1;     build(l, mid, root << 1);     build(mid + 1, r, root << 1 | 1);     push_up(root);   }   void update(int l, int r, int root) {     if (node[root].l >= l && node[root].r <= r) {       node[root].lazy += 1;       node[root].Max += 1;       return;     }     if (node[root].lazy) push_down(root);     int mid = (node[root].l + node[root].r) >> 1;     if (l <= mid) update(l, r, root << 1);     if (r > mid) update(l, r, root << 1 | 1);     push_up(root);   }   int query(int l, int r, int root) {     if (node[root].l >= l && node[root].r <= r) {       return node[root].Max;     }     if (node[root].lazy) push_down(root);     int t1 = 0, t2 = 0;     int mid = (node[root].l + node[root].r) >> 1;     if (l <= mid) t1 = query(l, r, root << 1);     if (r > mid) t2 = query(l, r, root << 1 | 1);     push_up(root);     if (t1 > t2) return t1;     return t2;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     memset(dp, 0, sizeof(dp));     for (int i = 1; i <= k; i++) {       build(0, n, 1);       for (int j = 0; j < maxn; j++) pre[j] = 0;       for (int j = 1; j <= n; j++) {         update(pre[a[j]], j - 1, 1);         pre[a[j]] = j;         dp[j] = query(0, j - 1, 1);       }     }     printf( %d n , dp[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void small(long long m, long long s) {     vector<long long> v;     long long p = (s % 9 == 0) ? s / 9 - 1 : s / 9;     for (long long i = 0; i < p; i++) {       v.push_back(9);       s -= 9;     }     if (m - p == 1) {       v.push_back(s);     }     if (m - p >= 2) {       s--;       v.push_back(s);       for (long long i = 0; i < m - p - 2; i++) {         v.push_back(0);       }       v.push_back(1);     }     for (long long i = v.size() - 1; i >= 0; i--) {       cout << v[i];     }     cout <<    ;   }   void great(long long m, long long s) {     vector<long long> v;     long long k = s / 9;     for (long long i = 0; i < k; i++) {       v.push_back(9);       s -= 9;     }     if (s > 0) {       v.push_back(s);       k++;     }     for (long long i = 0; i < m - k; i++) {       v.push_back(0);     }     for (long long i = 0; i < v.size(); i++) {       cout << v[i];     }   }   int main() {     long long m, s, h;     string k;     vector<int> v;     cin >> m >> s;     if (s % 9 == 0) {       long long t = 0;       if (m == 1 && s == 0) {         cout <<  0                <<  0 ;         t = 1;       }       if (t == 0) {         if (m < s / 9 || s == 0) {           cout <<  -1                  <<  -1 ;         } else {           small(m, s);           great(m, s);         }       }     } else if (s % 9 != 0) {       long long t = 0;       if (m == 1 && s == 0) {         cout <<  0                <<  0 ;         t = 1;       }       if (t == 0) {         if (m < s / 9 + 1 || s == 0) {           cout <<  -1                  <<  -1 ;         } else {           small(m, s);           great(m, s);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1e9 + 7;   long long int inf = 1e9 + 7;   void solve() {     long long int n;     cin >> n;     if (n <= 2) {       cout <<  -1  << endl;     } else {       long long int i;       for (i = n; i >= 1; i--) {         cout << i <<    ;       }       cout << endl;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s, t;     cin >> s >> t;     int cost = 0;     for (int i = 0; i < n;) {       if (s[i] != t[i]) {         if (i + 1 < n && s[i] != s[i + 1] && s[i + 1] != t[i + 1]) {           i += 2;         } else {           i++;         }         cost++;       } else {         i++;       }     }     cout << cost << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 1;     char 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;   }   const long long N = 100010;   struct edge {     long long v, nxt;   } e[N << 1];   long long head[N], cnt, n, x, y, a[N], dp[N][2], f[N], g[N], h[N];   inline void add(long long u, long long v) {     e[++cnt].v = v;     e[cnt].nxt = head[u];     head[u] = cnt;   }   inline void dfs(long long u, long long fa) {     dp[u][0] = dp[u][1] = f[u] = g[u] = a[u];     h[u] = 0;     for (long long i = head[u]; i; i = e[i].nxt) {       long long v = e[i].v;       if (v == fa) continue;       dfs(v, u);       dp[u][0] = max(dp[u][0], max(dp[v][0], dp[u][1] + dp[v][1]));       dp[u][0] = max(dp[u][0], max(f[u] + g[v], f[v] + g[u]));       dp[u][1] = max(dp[u][1], max(dp[v][1], f[u] + f[v]));       g[u] = max(g[u], a[u] + g[v]);       g[u] = max(g[u], max(f[u] + dp[v][1], f[v] + a[u] + h[u]));       h[u] = max(h[u], dp[v][1]);       f[u] = max(f[u], f[v] + a[u]);     }   }   inline void mian() {     n = read();     for (long long i = 1; i <= n; ++i) a[i] = read();     for (long long i = 1; i <= n - 1; ++i) {       x = read();       y = read();       add(x, y);       add(y, x);     }     dfs(1, 0);     printf( %lld n , dp[1][0]);   }   signed main() {     long long qwq = 1;     while (qwq--) mian();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       string s;       string t;       cin >> s >> t;       int ans = 1;       int last = INT_MIN;       vector<int> v[26];       int dp[2][26];       for (int i = 0; i < s.length(); i++) {         v[s[i] -  a ].push_back(i);       }       for (int i = 0; i < 26; i++) {         dp[0][i] = v[i].size();         dp[1][i] = 0;       }       for (int i = 0; i < t.length(); i++) {         if (v[t[i] -  a ].empty()) {           ans = -1;           break;         } else {           int temp = dp[1][t[i] -  a ];           vector<int>::iterator lower, l;           l = v[t[i] -  a ].begin() + temp;           lower = lower_bound(l, v[t[i] -  a ].end(), last);           if (lower - v[t[i] -  a ].begin() == dp[0][t[i] -  a ]) {             ans++;             last = v[t[i] -  a ][0];             for (int j = 0; j < 26; j++) dp[1][j] = 0;             dp[1][t[i] -  a ] = 1;           } else {             last = v[t[i] -  a ][lower - v[t[i] -  a ].begin()];             dp[1][t[i] -  a ] = lower - v[t[i] -  a ].begin() + 1;           }         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   stack<long long> num;   stack<char> fuhao;   vector<int> pos;   void calc() {     char cc = fuhao.top();     fuhao.pop();     long long a = num.top();     num.pop();     long long b = num.top();     num.pop();     long long ans;     if (cc ==  * )       ans = a * b;     else       ans = a + b;     num.push(ans);   }   long long solve(string p) {     int len = p.length();     for (int i = 0; i < len; i++) {       if (p[i] >=  0  && p[i] <=  9 ) {         num.push(p[i] -  0 );       } else if (p[i] ==  ( ) {         fuhao.push(p[i]);       } else if (p[i] ==  ) ) {         while (fuhao.top() !=  ( ) calc();         fuhao.pop();       } else if (p[i] ==  * ) {         fuhao.push(p[i]);       } else if (p[i] ==  + ) {         while (!fuhao.empty() && fuhao.top() ==  * ) {           calc();         }         fuhao.push(p[i]);       }     }     while (!fuhao.empty()) calc();     long long ans = num.top();     return ans;   }   int main() {     cin >> s;     int len = s.length();     pos.push_back(-1);     for (int i = 1; i < len; i += 2)       if (s[i] ==  * ) pos.push_back(i);     pos.push_back(len);     long long ans = 0;     for (int i = 0; i < pos.size() - 1; i++) {       for (int j = i + 1; j < pos.size(); j++) {         string temp = s;         temp.insert(pos[i] + 1, 1,  ( );         temp.insert(pos[j] + 1, 1,  ) );         ans = max(ans, solve(temp));       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[4] = {1, 0, -1, 0};   int dy[4] = {0, 1, 0, -1};   long long n, m, k;   long long arr[300001];   long long arr2[300001];   int main() {     std::ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     cin.ignore();     for (int test_case = 0; test_case < t; ++test_case) {       int flg = 0;       long long ans1 = 0, ans2 = 0;       string ans[3] = { NO ,  YES ,  R };       string s1, s2 =   ;       cin >> n >> k;       for (int i = 0; i < n; ++i) {         cin >> arr[i];       }       int i;       long long mn = 10000000000;       long long mx = 0;       for (i = 0; i < n; ++i) {         if (mn < arr[i]) {           if (mn != 10000000000) {             ans1 += mx - mn;             mx = 0;             mn = 10000000000;           }         }         mx = max(mx, arr[i]);         mn = min(mn, arr[i]);       }       ans1 += mx;       cout << ans1;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = int(1e6 + 3);   vector<vector<int> > v;   char second[600000];   int n, m;   int main() {     scanf( %d%d n , &n, &m);     v.resize(n);     for (int i = 0; i < n; ++i) {       gets(second);       v[i].resize(m);       for (int j = 0; j < m; ++j) {         v[i][j] = (second[j] ==  . ) ? -1 : second[j] -  0 ;       }     }     int res = 1;     for (int i = 0; i < n; ++i) {       int dir = -1;       for (int j = 0; j < m; ++j)         if (v[i][j] != -1) {           dir = (v[i][j] > 2) ? 1 : 0;           if (j & 1) dir = 1 - dir;           break;         }       if (dir != -1) {         for (int j = 0; j < m; ++j)           if (v[i][j] != -1) {             int d2 = (v[i][j] > 2) ? 1 : 0;             if (j & 1) d2 = 1 - d2;             if (dir != d2) res = 0;           }       } else if (dir == -1)         res = (res * 2) % mod;     }     for (int j = 0; j < m; ++j) {       int dir = -1;       for (int i = 0; i < n; ++i)         if (v[i][j] != -1) {           dir = (v[i][j] > 1 && v[i][j] < 4) ? 1 : 0;           if (i & 1) dir = 1 - dir;           break;         }       if (dir != -1) {         for (int i = 0; i < n; ++i)           if (v[i][j] != -1) {             int d2 = (v[i][j] > 1 && v[i][j] < 4) ? 1 : 0;             if (i & 1) d2 = 1 - d2;             if (dir != d2) res = 0;           }       } else if (dir == -1)         res = (res * 2) % mod;     }     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   const int maxn = 1e5 + 5;   using namespace std;   int a[maxn];   int vis[maxn];   int step[maxn * 2];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       int temp = a[i];       int step1 = 0, step2 = 0, step3 = 0;       while (temp < maxn) {         step1++;         vis[temp]++;         temp = temp << 1;         step[temp] += step1;       }       while (a[i] >= 1) {         if (a[i] & 1 && a[i] != 1) {           int temp1 = a[i] = a[i] >> 1;           step2++;           step3 = step2;           vis[temp1]++;           step[temp1] += step3;           while (temp1 < maxn) {             step3++;             temp1 = temp1 << 1;             vis[temp1]++;             step[temp1] += step3;           }         } else {           step2++;           a[i] = a[i] >> 1;           vis[a[i]]++;           step[a[i]] += step2;         }       }     }     int ans = 1e9;     for (int i = 1; i < maxn; i++) {       if (vis[i] == n) {         ans = min(step[i], ans);       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   const int mod = int(1e9) + 7;   const int N = int(1e6) + 7;   long long int two[N];   void pre() {     two[0] = 1;     for (int i = (int)1; i < (int)N; i++) {       two[i] = (two[i - 1] * 2) % mod;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     pre();     string s;     cin >> s;     int cnt = 0;     long long int ans = 0;     for (int i = (int)0; i < (int)(int)(s.size()); i++) {       if (s[i] ==  b ) {         ans = (ans + (two[cnt] - 1)) % mod;         continue;       }       cnt++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI =       3.14159265358979323846264338327950288419716939937510582097494459230;   void swaps(char *x, char *y) {     char temp;     temp = *x;     *x = *y;     *y = temp;   }   void swapi(int *a, int *b) {     int temp;     temp = *a;     *a = *b;     *b = temp;   }   unsigned long long gcd(unsigned long long a, unsigned long long b) {     if (a == 0) return b;     if (b == 0) return a;     if (a == 1 || b == 1) return 1;     if (a == b) return a;     if (a > b)       return gcd(b, a % b);     else       return gcd(a, b % a);   }   long long Tree[4000010];   int V[4000010];   int N;   int Time;   int Outtime[4000010];   int Intime[4000010];   int Lvl[4000010];   int maxval;   bool vis[4000010];   std::vector<std::vector<int> > Edge(2000010);   void Add(int idx, int val) {     for (int i = idx; i <= maxval; i += (i & -i)) Tree[i] += val;   }   long long Query(int idx) {     long long S = 0;     for (int i = idx; i > 0; i -= (i & -i)) S += Tree[i];     return S;   }   void dfs(int idx) {     vis[idx] = true;     Intime[idx] = ++Time;     vis[idx] = true;     for (int i = 0; i < Edge[idx].size(); i++) {       if (vis[Edge[idx][i]]) continue;       Lvl[Edge[idx][i]] = Lvl[idx] + 1;       dfs(Edge[idx][i]);     }     Outtime[idx] = ++Time;   }   int main() {     int Cases = 1;     for (int Case = 0; Case < Cases; Case++) {       int u, v, opt, node, Q;       cin >> N >> Q;       for (int i = 1; i <= N; i++) cin >> V[i];       for (int i = 0; i < N - 1; i++) {         cin >> u >> v;         Edge[u].push_back(v);         Edge[v].push_back(u);       }       dfs(1);       maxval = Outtime[1];       for (int i = 0; i < Q; i++) {         cin >> opt;         opt -= 1;         if (opt == 0) {           cin >> node >> v;           if (Lvl[node] % 2) {             Add(Intime[node], -v);             Add(Outtime[node], v);           } else {             Add(Intime[node], v);             Add(Outtime[node], -v);           }         } else {           cin >> node;           if (Lvl[node] % 2)             cout << V[node] - Query(Intime[node]) << endl;           else             cout << V[node] + Query(Intime[node]) << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   long long int binpow(long long int a, long long int b) {     if (b == 0) return 1;     long long int res = binpow(a, b / 2);     if (b % 2)       return res * res * a;     else       return res * res;   }   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> v(n, 0);       for (int i = 0; i < v.size(); i++) cin >> v[i];       sort(v.begin(), v.end(), greater<int>());       for (int i = 0; i < n; i++) cout << v[i] <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long LLINF = 1e18;   const int INF = 1e9;   const int MOD = 20101009;   const int HMOD = 999959;   const int VMOD = 5000000;   const int MAXN = 1e5 + 10;   const int MAXM = 5e5 + 10;   const int INV = 10050505;   const long long LLINV = 1e18;   const double eps = 1e-3;   int dirx[5] = {0, -1, 1, 0, 0}, diry[5] = {0, 0, 0, -1, 1};   int n, d, m;   int cnt1, cnt2;   long long a[MAXN], b[MAXN];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     scanf( %d%d%d , &n, &d, &m);     for (int i = 1, x; i <= n; ++i) {       scanf( %d , &x);       if (x <= m)         a[++cnt1] = x;       else         b[++cnt2] = x;     }     sort(a + 1, a + 1 + cnt1, greater<int>());     sort(b + 1, b + 1 + cnt2, greater<int>());     if (cnt2 == 0) {       long long sum = 0;       for (int i = 1; i <= n; ++i) sum += a[i];       printf( %lld n , sum);       return 0;     }     for (int i = 1; i <= cnt1; ++i) a[i] = a[i - 1] + a[i];     for (int i = 1; i <= cnt2; ++i) b[i] = b[i - 1] + b[i];     long long ans = 0;     for (int i = 1; i <= cnt2; ++i) {       long long tmp = (long long)(i - 1) * d;       long long oth = n - i;       long long ned = cnt1 - (max(0LL, tmp - (cnt2 - i)));       if (oth >= tmp) ans = max(ans, b[i] + a[ned]);     }     printf( %lld n , ans);     return 0;   }