#include <bits/stdc++.h>   long long mod = 1e9 + 7, mod2 = 998244353;   const int N = int(3e5) + 999;   using namespace std;   long long fac[10000000];   long long gcd(long long x, long long y) {     if (y == 0) return x;     return gcd(y, x % y);   }   long long fexp(long long a, long long b, long long m) {     long long ans = 1;     while (b) {       if (b & 1) ans = ans * a % m;       b /= 2;       a = a * a % m;     }     return ans;   }   long long inverse(long long a, long long p) { return fexp(a, p - 2, p); }   long long ncr(long long n, long long r, long long p) {     if (r == 0) return 1;     return (fac[n] * inverse(fac[n - r], p) % p * inverse(fac[r], p) % p) % p;   }   vector<long long> za(string s) {     long long n = s.size();     vector<long long> z(n);     long long x = 0, y = 0, p = 0;     for (long long i = 1; i < n; i++) {       z[i] = max(p, min(z[i - x], y - i + 1));       while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {         x = i;         y = i + z[i];         z[i]++;       }     }     return z;   }   void subset(long long a[], long long k) {     for (int i = 1; i < pow(2, k); i++) {       for (int j = 0; j < k; j++) {         if (i & 1 << j) {           cout << a[j] <<    ;         }       }       cout << endl;     }   }   vector<long long> pr(string s) {     long long n = s.length();     vector<long long> pi(n);     for (int i = 1; i < n; i++) {       int j = pi[i - 1];       while (j > 0 && s[i] != s[j]) j = pi[j - 1];       if (s[i] == s[j]) j++;       pi[i] = j;     }     return pi;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n, val;     cin >> n;     long long a[n], c1 = 0, c2 = 0, ans = 0, t = 0;     for (long long i = 0; i < n; i++) {       cin >> a[i];       if (t % 2 == 0)         c1++;       else         c2++;       if (a[i] < 0) t++;       if (t % 2 == 0)         ans += c1;       else         ans += c2;     }     cout << n * (n + 1) / 2 - ans <<     << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void chkmax(T& x, U y) {     if (x < y) x = y;   }   template <typename T, typename U>   inline void chkmin(T& x, U y) {     if (y < x) x = y;   }   int main() {     int h, m, s, A, B;     scanf( %d%d%d%d%d , &h, &m, &s, &A, &B);     double a = h % 12 + 1.0 * m / 60 + 1.0 * s / 3600;     double b = 0.2 * m + 1.0 * s / 300;     double c = 0.2 * s;     for (int i = 0; i < 2; i++) {       bool flg = 1;       for (int x = A; x != B; x = x % 12 + 1) {         if (x % 12 <= a && a <= x % 12 + 1) flg = 0;         if (x % 12 <= b && b <= x % 12 + 1) flg = 0;         if (x % 12 <= c && c <= x % 12 + 1) flg = 0;       }       if (flg) return puts( yes ), 0;       swap(A, B);     }     puts( no );     return 0;   }


#include <bits/stdc++.h>   using namespace std;      int main() {    ios_base::sync_with_stdio(false);    cin.tie(NULL);        long long int T;    cin>>T;    while(T--)    {     string S;     cin>>S;          long long int A[100];     long long int x=0;     long long int val=0;     long long int N=S.length();          long long int first,mid,last;     A[0]=0;A[1]=0;A[2]=0;     string str=  ;          for(long long int i=0;i<N;i++)     {         char c;         c=S[i];         if(c== ? )         A[x]++;         else          {             str+=c;             x++;         }              }             // cout<<A[0]<<   <<A[1]<<   <<A[2]<<endl;     if(str== () )     {        val=1;                       }     else if(str== )( )     {         if((A[0]>=1)&&(A[2]>=1))         {             val=1;         }     }          if(val==0)     cout<< NO <<endl;     else if(val==1&&(N%2==0))     cout<< YES <<endl;     else     cout<< NO <<endl;                 }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   const int inf = 1000 * 1000 * 1000;   const int mod = 1000 * 1000 * 1000 + 7;   mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());   int n, s, d;   bitset<N> a[(1 << 20) + 2];   bitset<N> b[(1 << 10) + 2];   bitset<N> mas[33];   bitset<N> A;   int cnt[(1 << 20) + 2];   unordered_map<bitset<N>, int> mp;   int main() {     cin >> n >> s >> d;     for (int i = 1; i <= s; i++) {       int c;       scanf( %d , &c);       mas[i] = 0;       while (c--) {         int a;         scanf( %d , &a);         mas[i][a] = 1;       }     }     int x = min(s, 20);     int y = s - x;     cnt[0] = 0;     for (int i = 1; i < (1 << 20); i++) {       for (int j = 0; j < 20; j++) {         if ((i >> j) & 1) {           cnt[i]++;         }       }     }     b[0] = 0;     mp[b[0]] = 0;     for (int i = 1; i < (1 << x); i++) {       int pos;       for (int j = 0; j < x; j++) {         if ((i >> j) & 1) {           pos = j;           break;         }       }       b[i] = b[i ^ (1 << pos)] ^ mas[pos + 1];       if (mp.find(b[i]) == mp.end()) {         mp[b[i]] = cnt[i];       } else {         mp[b[i]] = min(mp[b[i]], cnt[i]);       }     }     a[0] = 0;     for (int i = 1; i < (1 << y); i++) {       int pos;       for (int j = 0; j < y; j++) {         if ((i >> j) & 1) {           pos = j;           break;         }       }       a[i] = a[i ^ (1 << pos)] ^ mas[pos + 1 + x];     }     while (d--) {       int c;       scanf( %d , &c);       A = 0;       while (c--) {         int a;         scanf( %d , &a);         A[a] = 1;       }       int answ = inf;       for (int i = 0; i < (1 << y); i++) {         if (mp.find(A ^ a[i]) != mp.end()) {           answ = min(answ, cnt[i] + mp[A ^ a[i]]);         }       }       if (answ == inf) answ = -1;       printf( %d n , answ);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, i, k = 0;       cin >> n;       int a[n];       map<int, int> m;       set<int> s;       for (i = 0; i < n; i++) {         cin >> a[i];         m[a[i]]++;         k = max(k, m[a[i]]);       }       for (i = 0; i < n; i++) s.insert(a[i]);       int mx = s.size();       int ans = max(min(mx - 1, k), min(mx, k - 1));       cout << ans <<   n ;     }   }


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


#include <bits/stdc++.h>   struct help {     long int money;     long int friends;   };   void fusion_sort(struct help *p, long int n);   void bablesort(struct help *p, long int n);   int main() {     long int n, m;     scanf( %ld %ld , &n, &m);     struct help *p = (struct help *)malloc(sizeof(struct help) * n);     for (int i = 0; i < n; i++) scanf( %ld %ld , &p[i].money, &p[i].friends);     if (n == 1) {       printf( %ld , p[0].friends);       return 0;     }     if (n > 1000)       fusion_sort(p, n);     else       bablesort(p, n);     unsigned long long max_friend = 0;     unsigned long long cur_sum = 0;     for (int b = 0, e = 0; e < n;)       if (p[e].money - p[b].money >= m) {         if (cur_sum > max_friend) {           max_friend = cur_sum;         }         cur_sum -= p[b].friends;         b++;       } else {         cur_sum += p[e].friends;         e++;       }     if (cur_sum > max_friend) max_friend = cur_sum;     printf( %lld , max_friend);     return 0;   }   void fusion_sort(struct help *p, long int n) {     if (n > 2) {       fusion_sort(p, n / 2);       fusion_sort((p + n / 2), n / 2 + n % 2);     }     struct help temp;     for (int i = 0, j = n / 2; i < j && j < n; i++)       if (p[i].money > p[j].money) {         temp.friends = p[j].friends;         temp.money = p[j].money;         for (int k = j; k > i; k--) {           p[k].friends = p[k - 1].friends;           p[k].money = p[k - 1].money;         }         p[i].friends = temp.friends;         p[i].money = temp.money;         j++;       }   }   void bablesort(struct help *p, long int n) {     long int fr;     long int mo;     while (n > 1) {       for (int i = 0; i < n - 1; i++)         if (p[i].money > p[i + 1].money) {           fr = p[i].friends;           mo = p[i].money;           p[i].friends = p[i + 1].friends;           p[i].money = p[i + 1].money;           p[i + 1].friends = fr;           p[i + 1].money = mo;         }       n--;     }   }


#include <bits/stdc++.h>   using LL = long long;   const LL Inf = (LL)1e18;   const int N = 5000 + 5;   int s, e, n;   LL x[N], a[N], b[N], c[N], d[N];   LL dp[2][N];   inline LL lo(int p) { return d[p] - x[p]; }   inline LL ro(int p) { return x[p] + c[p]; }   inline LL li(int p) { return b[p] - x[p]; }   inline LL ri(int p) { return x[p] + a[p]; }   inline void _min(LL &a, LL b) {     if (a > b) a = b;   }   LL work() {     if (s > e) {       for (int i = 1; i <= n; ++i) {         x[i] = -x[i];         std::swap(a[i], b[i]);         std::swap(c[i], d[i]);       }       for (int i = 1; i < n + 1 - i; ++i) {         std::swap(x[i], x[n + 1 - i]);         std::swap(a[i], a[n + 1 - i]);         std::swap(b[i], b[n + 1 - i]);         std::swap(c[i], c[n + 1 - i]);         std::swap(d[i], d[n + 1 - i]);       }       s = n + 1 - s;       e = n + 1 - e;     }     for (int i = 0; i < 2; ++i) {       for (int j = 0; j < N; ++j) {         dp[i][j] = Inf;       }     }     int cur = 0, nex = 1;     dp[cur][1] = li(1) + lo(1);     for (int i = 2; i <= n; ++i) {       for (int left = 1; left <= i - 1; ++left) {         if (dp[cur][left] == Inf) continue;         for (int q = 0; q < 2; ++q) {           for (int w = 0; w < 2; ++w) {             if (i == s && w == 0) continue;             if (i == e && q == 1) continue;             if (s < i && i < e) {               if (left == 1 && q == 0) continue;             }             LL cost = 0;             cost += q == 0 ? ro(i) : lo(i);             cost += w == 0 ? ri(i) : li(i);             int n_left = left;             if (q == w) {               if (q == 0)                 n_left--;               else                 n_left++;             }             _min(dp[nex][n_left], dp[cur][left] + cost);           }         }       }       std::swap(cur, nex);       std::fill(dp[nex], dp[nex] + N, Inf);     }     LL ret = dp[cur][0];     if (s < e) {       ret -= li(s) + ro(e);     } else {       ret -= ri(s) + lo(e);     }     return ret;   }   int main() {     std::ios::sync_with_stdio(false);     std::cin >> n >> s >> e;     for (int i = 1; i <= n; ++i) std::cin >> x[i];     for (int i = 1; i <= n; ++i) std::cin >> a[i];     for (int i = 1; i <= n; ++i) std::cin >> b[i];     for (int i = 1; i <= n; ++i) std::cin >> c[i];     for (int i = 1; i <= n; ++i) std::cin >> d[i];     std::cout << work() << std::endl;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > g;   bitset<(int)1e6> us;   bool isCyclic(int v, int p = -1) {     us.set(v);     for (int i = 0; i < g[v].size(); i++) {       if (us.test(g[v][i]) && g[v][i] != p) return 0;       if (us.test(g[v][i])) continue;       bool cures = isCyclic(g[v][i], v);       if (!cures) return 0;     }     return 1;   }   int main() {     int n, m;     cin >> n >> m;     g.resize(n + 1);     for (int i = 0; i < m; i++) {       int v, to;       cin >> v >> to;       g[to].push_back(v);       g[v].push_back(to);     }     int sum = 0;     for (int i = 1; i <= n; i++) {       if (us.test(i)) continue;       sum += isCyclic(i);     }     cout << sum << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   long long num[5005], sum[5005], dp[5005][5005];   int main() {     int i, j;     while (scanf( %d%d%d , &n, &m, &k) != EOF) {       for (i = 0; i < n; i++) {         scanf( %I64d , &num[i]);       }       memset(sum, 0, sizeof(sum));       for (i = 0; i <= m - 1; i++) {         sum[0] += num[i];       }       for (i = 1; i + m <= n; i++) {         sum[i] = sum[i - 1];         sum[i] -= num[i - 1];         sum[i] += num[i + m - 1];       }       memset(dp, 0, sizeof(dp));       for (j = 1; j <= k; j++) {         for (i = n - m * j; i >= 0; i--) {           dp[i][j] = max(dp[i + 1][j], dp[i + m][j - 1] + sum[i]);         }       }       long long res = 0;       for (i = 0; i <= n - m * k; i++) {         res = max(dp[i][k], res);       }       cout << res << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 20001;   const int MAX_LOG = 20;   using ll = long long;   using pii = pair<int, int>;   using pil = pair<int, ll>;   using pll = pair<ll, ll>;   const double delta = 1e-6;   const long long mod = 1e9 + 7;   const int magic_const = 1e9;   const ll INF = 2e18 + 2;   const ll BIG_DT = 1e12;   mt19937 gen(54323244234);   inline ll modoper(ll x) { return ((x % mod) + mod) % mod; }   ll gcd(ll x, ll y) { return y == 0 ? x : gcd(y, x % y); }   inline ll get_rand(ll x) { return (gen() % x + x) % x; }   template <typename T>   inline void sort(vector<T>& x) {     sort(x.begin(), x.end());   }   template <typename T>   inline void reverse(vector<T>& x) {     reverse(x.begin(), x.end());   }   template <typename T>   inline void set_max(T& x, T y) {     x = max(x, y);   }   template <typename T>   inline void set_min(T& x, T y) {     x = min(x, y);   }   ll bin_pow(ll x, ll pow) {     if (pow == 0) return 1;     if ((pow & 2) == 1) {       return (bin_pow(x, pow - 1) * x) % mod;     } else {       auto tmp = bin_pow(x, pow / 2);       return (tmp * tmp) % mod;     }   }   struct fenwick_tree {     vector<int> a;     fenwick_tree(int n) : a(n + 2) {}     void add(int pos, int delta) {       for (pos++; pos < a.size(); pos += pos & -pos) a[pos] += delta;     }     int get_sum(int pos) {       int sum = 0;       for (pos++; pos > 0; pos -= pos & -pos) sum += a[pos];       return sum;     }   };   vector<double> ys(MAX_N);   vector<vector<double>> matrix(3, vector<double>(3));   double find_det() {     double answer = 0.0;     for (int i = 0; i < 3; i++) {       double pr = 1.0;       for (int j = 0; j < 3; j++) {         pr *= matrix[j][(i + j) % 3];       }       answer += pr;     }     for (int i = 0; i < 3; i++) {       double pr = 1.0;       for (int j = 0; j < 3; j++) {         pr *= matrix[j][(i - j + 3) % 3];       }       answer -= pr;     }     return answer;   }   void set_point_in_vector(vector<double>& a, int x) {     a[0] = x * x;     a[1] = x;     a[2] = 1.0;   }   void nullify(int x1, int x2, int x3) {     set_point_in_vector(matrix[0], x1);     set_point_in_vector(matrix[1], x2);     set_point_in_vector(matrix[2], x3);   }   void set_st_r(int x1, int x2, int x3, int column) {     nullify(x1, x2, x3);     matrix[0][column] = ys[x1];     matrix[1][column] = ys[x2];     matrix[2][column] = ys[x3];   }   pair<double, pair<double, double>> find_eq(int x1, int x2, int x3) {     nullify(x1, x2, x3);     double det = find_det();     set_st_r(x1, x2, x3, 0);     double det_1 = find_det();     set_st_r(x1, x2, x3, 1);     double det_2 = find_det();     set_st_r(x1, x2, x3, 2);     double det_3 = find_det();     return {det_1 / det, {det_2 / det, det_3 / det}};   }   void solve() {     int n, d;     cin >> n >> d;     vector<int> a(n, 1);     int sum_now = (n * (n - 1)) / 2 - d;     for (int l = 1, r = a.size() - 1; l < r; r--) {       while (l < r && (sum_now - r + l < 0 || a[l] == a[l - 1] * 2)) {         l++;       }       if (l >= r) break;       sum_now -= r;       sum_now += l;       a[l]++;       a[r]--;     }     if (sum_now != 0) {       cout <<  NO n ;       return;     }     cout <<  YES n ;     a.insert(a.begin(), 0);     vector<int> pref_sums = a;     for (int i = 1; i < pref_sums.size(); i++) {       pref_sums[i] += pref_sums[i - 1];     }     for (int i = 2; i < pref_sums.size(); i++) {       for (int k = 0; k < a[i]; k++) {         cout << pref_sums[i - 2] + 1 + k / 2 <<    ;       }     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     srand(23321);     int t = 1;     cin >> t;     for (int i = 0; i < t; i++) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const int N = 200005;   int vis[N], tab[N], i, a, b, sum, n, m, vis0[N], vis1[N];   vector<int> graf0[N], graf1[N];   string s;   bool cyc(int w) {     vis[w] = 1;     for (auto& w2 : graf0[w])       if (vis[w2] == 1)         return true;       else if (vis[w2] == 0 && cyc(w2))         return true;     vis[w] = 2;     return false;   }   void go(int w, vector<int> graf[N], int* vis) {     tab[w] = 1;     vis[w] = 1;     for (auto& w2 : graf[w])       if (!vis[w2]) go(w2, graf, vis);   }   int main() {     scanf( %d %d , &n, &m);     while (m--) {       scanf( %d %d , &a, &b);       graf0[a].push_back(b);       graf1[b].push_back(a);     }     for (i = 1; i <= n; i++)       if (vis[i] == 0 && cyc(i)) {         printf( -1 n );         return 0;       }     sum = 0;     for (i = 1; i <= n; i++)       if (tab[i] == 0) {         go(i, graf0, vis0);         go(i, graf1, vis1);         s +=  A ;         sum++;       } else {         s +=  E ;         go(i, graf0, vis0);         go(i, graf1, vis1);       }     printf( %d n%s n , sum, s.c_str());     return 0;   }


#include <bits/stdc++.h>   using ll = long long;   using ld = long double;   using namespace std;   const int MAXN = 2000228;   const ll INF = (1ll << 50);   set<pair<ll, ll>> s;   void ins(ll x, ll y) {}   int main() {     ios_base::sync_with_stdio(false);     cout.setf(ios::fixed);     cout.precision(20);     cout.tie(nullptr);     cin.tie(nullptr);     int n;     cin >> n;     ll p = INF;     s.emplace(p - 1, 0);     for (int i = 0; i < n; ++i) {       ll x;       cin >> x;       if (x >= p) {         continue;       }       p = x;       vector<pair<ll, ll>> rmed;       while (!s.empty()) {         auto val = *s.rbegin();         if (val.first < x - 1) {           break;         }         s.erase(val);         rmed.push_back(val);       }       pair<ll, ll> tmp{x, -1};       s.emplace(tmp);       ll last = 0;       for (auto& val : rmed) {         ll dv = val.first / x;         last = max(last, val.second + x * (dv - 1) * i);         ll md = val.first % x;         ll nvl = x * dv * i + val.second;         auto it = s.lower_bound(pair<ll, ll>{md, nvl});         if (it->second >= nvl) {           continue;         }         auto lit = it;         while (lit != s.begin()) {           auto nit = lit;           --nit;           if (nit->second > nvl) {             break;           }           lit = nit;         }         s.erase(lit, it);         s.emplace(md, nvl);       }       while (!s.empty() && s.rbegin()->second <= last) {         auto it = s.end();         --it;         s.erase(it);       }       s.emplace(x - 1, last);     }     ll ans = 0;     for (auto& val : s) {       ans = max(ans, val.first * n + val.second);     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   typedef long long ll;   typedef long double ld;   typedef string str;      #define pb push_back   #define ppp pop_back   #define pii pair<int,int>   #define fi first   #define se second   #define stie std::tie   #define vec vector   #define forn(i, l, r) for (int i=l; i<=r; i++)   #define forb(i, r, l) for (int i=r; i>=l; i--)   #define emp empty   #define beg begin   #define ins insert   #define cle clear   #define era erase   #define que queue   #define pque priority_queue   #define mset multiset   #define deq deque   #define sta stack   #define con const   #define rsz resize   #define ass assign   #define lowb lower_bound   #define uppb upper_bound      template<class T> void mini(T& a, T b) { a = min(a, b); }   template<class T> void maxi(T& a, T b) { a = max(a, b); }   template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> a) { return out << a.first <<     << a.second; }   template<class T> ostream& operator<<(ostream& out, vector<T> a) { for (auto& i : a) out << i <<    ; return out; }   template<class T> ostream& operator<<(ostream& out, set<T> a) { for (auto& i : a) out << i <<    ; return out; }   template<class T> ostream& operator<<(ostream& out, multiset<T> a) { for (auto& i : a) out << i <<    ; return out; }   template<class T1, class T2> ostream& operator<<(ostream& out, map<T1, T2> a) { for (auto& i : a) out << i <<   n ; return out; }   template<class T1, class T2> ostream& operator<<(ostream& out, unordered_map<T1, T2> a) { for (auto& i : a) out << i <<   n ; return out; }   template<class T> ostream& operator<<(ostream& out, queue<T> a) { while (!a.empty()) { out << a.front() <<    ; a.pop(); } return out; }   template<class T> ostream& operator<<(ostream& out, deque<T> a) { while (!a.empty()) { out << a.front() <<    ; a.pop_front(); } return out; }   template<class T> ostream& operator<<(ostream& out, priority_queue<T> a) { while (!a.empty()) { out << a.top() <<   n ; a.pop(); } return out; }   template<class T> void out(T a) { cout << a << endl; }   template<class T1, class T2> void out(T1 a, T2 b) { cout << a <<     << b << endl; }   template<class T1, class T2, class T3> void out(T1 a, T2 b, T3 c) { cout << a <<     << b <<     << c << endl; }   template<class T1, class T2, class T3, class T4> void out(T1 a, T2 b, T3 c, T4 d) { cout << a <<     << b <<     << c <<     << d << endl; }   template<class T> void out(vector<vector<T>> a) { for (vector<T> i : a) out(i); }   template<class T> void out_(T a[], int l, int r) { for (int i = l; i <= r; i++) cout << a[i] <<    ; cout << endl; }   void out() { cout <<  OK  << endl; }   template<class T> void sort(vector<T>& a) { sort(a.begin(), a.end()); }   template<class T> void uniq(vector<T>& a) { sort(a); a.erase(unique(a.begin(), a.end()), a.end()); }   template<class T> vector<T> set_int(vector<T> a, vector<T> b) { sort(a); sort(b); vector<T> res; set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res)); return res; }   clock_t start_time; void start_timer() { start_time = clock(); }   double get_time() { return (double)(clock() - start_time) / CLOCKS_PER_SEC; }   #if __SIZEOF_INT128__ >= 16   typedef __int128 LL;   istream& operator>>(istream& in, __int128& a) { int64_t b; cin >> b; a = b; return in; }   ostream& operator<<(ostream& out, const __int128 a) {    unsigned __int128 b = a < 0 ? -a : a; char buf[128]; char* c = end(buf); do { --c; *c =  0123456789 [b % 10]; b /= 10; } while (b);    if (a < 0) { --c; *c =  - ; } int len = end(buf) - c; out.rdbuf()->sputn(c, len); return out;   }   #endif      con int N = 1e5 + 5;   con int K = 25;      int a[N];   vec<int> g[N];   int dp[N][2 * K];   bool ans[N];   int k;      void dfs(int v = 1, int p = 0) {    dp[v][0] = a[v];    for (int u : g[v]) {     if (u == p) continue;     dfs(u, v);     forn(i, 1, k-1) {      dp[v][i] ^= dp[u][i-1];     }     dp[v][0] ^= dp[u][k-1];    }   }      void dfs2(int v = 1, int p = 0, vec<int> cur = vec<int>(2 * K, 0)) {    forn(i, 0, k-1) {     cur[i] ^= dp[v][i];    }    int odd = 0;    forn(i, k/2, k-1) {     odd ^= cur[i];    }    ans[v] = odd;    for (int u : g[v]) {     if (u == p) continue;     vec<int> nxt = cur;     forn(i, 1, k-1) {      nxt[i] ^= dp[u][i-1];     }     nxt[0] ^= dp[u][k-1];     int last = nxt[k-1];     forb(i, k-1, 1) {      nxt[i] = nxt[i-1];     }     nxt[0] = last;     dfs2(u, v, nxt);    }   }      void Solve() {    int n;    cin >> n >> k;    k *= 2;    forn(i, 1, n-1) {     int u, v;     cin >> u >> v;     g[u].pb(v);     g[v].pb(u);    }    forn(i, 1, n) cin >> a[i];    dfs();    dfs2();    forn(i, 1, n) {     cout << ans[i] <<    ;    }    cout <<   n ;   }      int main() {    ios_base::sync_with_stdio(0); cin.tie(0);   #ifdef LOCAL    freopen( input.txt ,  r , stdin);   #endif    start_timer();    Solve();    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t-- > 0) {       int n;       cin >> n;       vector<vector<int>> a;       int b[n + 1];       for (int i = 0; i <= n; i++) b[i] = 0;       int count = 0;       int s = 0;       for (int i = 0; i < n; i++) {         vector<int> x;         int k;         cin >> k;         int c = 0;         for (int j = 0; j < k; j++) {           int q;           cin >> q;           x.push_back(q);           if (c == 0) {             if (b[q] == 0) {               b[q]++;               count++;               c = 1;             }           }         }         if (c == 0) s = i;         a.push_back(x);       }       if (count == n) {         cout <<  OPTIMAL  << endl;       } else {         s++;         int f = 0;         for (int i = 0; i < n; i++) {           if (b[i + 1] == 0) {             f = i;           }         }         f++;         cout <<  IMPROVE  << endl;         cout << s <<     << f << endl;       }     }     return 0;   }


//Hare Krishna   //author: Dipjoy Basak   //dip_10      #include <bits/stdc++.h>   using namespace std;      #define endl   n    #define ll long long   #define int ll   #define ld long double   #define pb push_back   #define mp make_pair   #define ff first   #define ss ssecond   #define vi vector<int>   #define pi pair<int, int>   #define vpi vector<pair<int, int>>   #define rep(i, l, r) for (int i = l; i <= r; i++)   #define rrep(i, r, l) for (int i = r; i >= l; i--)   #ifndef ONLINE_JUDGE   #define debug(x)              cerr << #x <<    ;        _print(x);                cerr << endl;   #else   #define debug(x) ;   #endif   #define maxn 1000005   #define INF INT_MAX   const int mod = 1000000007;      void _print(int t) { cerr << t; }   void _print(string t) { cerr << t; }   void _print(char t) { cerr << t; }   void _print(ld t) { cerr << t; }   void _print(double t) { cerr << t; }      template <class T, class V>   void _print(pair<T, V> p);   template <class T>   void _print(vector<T> v);   template <class T>   void _print(set<T> v);   template <class T, class V>   void _print(map<T, V> v);   template <class T>   void _print(multiset<T> v);   template <class T, class V>   void _print(pair<T, V> p)   {       cerr <<  { ;       _print(p.ff);       cerr <<  , ;       _print(p.ss);       cerr <<  } ;   }   template <class T>   void _print(vector<T> v)   {       cerr <<  [  ;       for (T i : v)       {           _print(i);           cerr <<    ;       }       cerr <<  ] ;   }   template <class T>   void _print(set<T> v)   {       cerr <<  [  ;       for (T i : v)       {           _print(i);           cerr <<    ;       }       cerr <<  ] ;   }   template <class T>   void _print(multiset<T> v)   {       cerr <<  [  ;       for (T i : v)       {           _print(i);           cerr <<    ;       }       cerr <<  ] ;   }   template <class T, class V>   void _print(map<T, V> v)   {       cerr <<  [  ;       for (auto i : v)       {           _print(i);           cerr <<    ;       }       cerr <<  ] ;   }      //---------------------------------------------------------------------------------------------------------------------------------------      void solve()   {       int n;       cin >> n;       int a[n];       int idx = -1, mn = INT_MAX;       rep(i, 0, n - 1)       {           cin >> a[i];           if (a[i] < mn)           {               mn = a[i];               idx = i;           }       }       cout << n - 1 << endl;       int cnt = 1;       rep(i, idx + 1, n - 1)       {           cout << idx + 1 <<     << i + 1 <<     << mn <<     << mn + cnt << endl;           cnt++;       }       cnt = 1;       rrep(i, idx - 1, 0)       {           cout << idx + 1 <<     << i + 1 <<     << mn <<     << mn + cnt << endl;           cnt++;       }   }   int32_t main()   {       ios_base::sync_with_stdio(false);       cin.tie(NULL);       cout.tie(NULL);       cout << setprecision(15) << fixed;       int t;       t = 1;       cin >> t;       while (t--)       {           solve();       }       return 0;   }


#include <bits/stdc++.h>   const double pi = 3.14159265358979323846;   using namespace std;   long long fastexp(long long a, long long b) {     long long ans = 1;     long long temp = a;     while (b > 0) {       if (b & 1) ans *= (temp);       temp *= temp;       b >>= 1;     }     return ans;   }   long long GCD(long long a, long long b) {     if (a == 0) return b;     return GCD(b % a, a);   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     long long* A = new long long[n];     for (long long i = 0; i < n; i++) cin >> A[i];     long long m;     cin >> m;     long long* B = new long long[m];     for (long long i = 0; i < m; i++) cin >> B[i];     long long count = 0;     long long i = 0;     long long j = 0;     bool f = true;     while (i < n && j < m) {       long long cum1 = A[i++];       long long cum2 = B[j++];       bool flag = false;       while (i < n && j < m) {         if (cum1 == cum2) {           flag = true;           break;         }         if (cum1 > cum2) {           cum2 += B[j];           j++;         } else {           cum1 += A[i];           i++;         }       }       if (i == n) {         while (j < m) cum2 += B[j++];       }       if (j == m) {         while (i < n) cum1 += A[i++];       }       if (cum1 == cum2) flag = true;       if (!flag) {         f = false;         break;       }       count++;     }     if (!f)       cout <<  -1             <<   n ;     else       cout << count <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long int x = 0;     for (int i = 2; i <= n; ++i) {       if (i & 1)         x = (x * 3 - 3) % 1000000007;       else         x = (x * 3 + 3) % 1000000007;     }     cout << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   char s[500010];   int c[30][500010];   int then(int x) { return x & -x; }   void in(int o, int x, int k) {     while (x <= N) {       c[o][x] += k;       x += then(x);     }   }   int out(int o, int x) {     int re = 0;     while (x > 0) {       re += c[o][x];       x -= then(x);     }     return re;   }   int main() {     int Q;     scanf( %s%d , s + 1, &Q);     N = strlen(s + 1);     for (int i = 1; i <= N; i++) in(s[i] -  a , i, 1);     for (int i = 1; i <= Q; i++) {       int o;       scanf( %d , &o);       if (o == 1) {         int k;         char m;         scanf( %d %c , &k, &m);         in(s[k] -  a , k, -1);         in(m -  a , k, 1);         s[k] = m;       } else {         int x, y;         scanf( %d%d , &x, &y);         int give = 0;         for (int i = 0; i <= 25; i++)           if (out(i, y) - out(i, x - 1)) give++;         printf( %d n , give);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s =   ;     vector<int> fib;     fib.push_back(1);     fib.push_back(1);     for (int i = 2; i < 17; ++i) {       fib.push_back(fib[i - 1] + fib[i - 2]);     }     for (int i = 1; i <= n; ++i) {       if (binary_search(fib.begin(), fib.end(), i) == 1) {         s.append(1u,  O );       } else {         s.append(1u,  o );       }     }     cout << s <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a >> b;     int l = b.size();     int st = a.size() - 1;     int ar[l + 3];     memset(ar, -1, sizeof ar);     ar[l] = st + 1;     for (int i = l - 1; i >= 0; i--) {       while (st >= 0 && a[st] != b[i]) {         st--;       }       if (st >= 0) {         ar[i] = st;         st--;       } else         break;     }     int ans = 0;     int pre = 0;     for (int i = 0; i < a.size(); i++) {       int la = ar[pre];       if (la != -1) ans = max(ans, la - i);       if (pre < l && a[i] == b[pre]) {         pre++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int N = 60 + 10, M = (1 << 16) + 10, MOD = 1e9 + 7,                       INF = 2e9 + 10;   const long double PI = acos(-1);   int n, a[N], in[N], fac[N], msk[N], all;   long long int dp[M][N], ans = 1;   vector<int> adj[N], S, T;   bool mrk[N], ad[N][N];   long long int Power(long long int x, long long int y) {     if (y == 0) {       return 1ll;     }     return ((y % 2 ? x : 1) * Power((x * x) % MOD, y / 2)) % MOD;   }   void Dfs(int u) {     mrk[u] = 1;     if (in[u])       T.push_back(u);     else       S.push_back(u);     for (int v : adj[u]) {       if (!mrk[v]) {         Dfs(v);       }     }     return;   }   void Solve() {     memset(dp, 0, sizeof(dp));     int t = T.size(), s = S.size();     if (t < 2) return;     for (int i = 0; i < t; i++) {       msk[i] = 0;       for (int j = 0; j < s; j++) {         if ((a[T[i]] % a[S[j]]) == 0) {           msk[i] |= (1 << j);         }       }       dp[msk[i]][1]++;     }     for (int k = 1; k < t; k++) {       for (int mask = 1; mask < (1 << s); mask++) {         int cnt = 0;         for (int i = 0; i < t; i++) {           if ((msk[i] | mask) == mask) {             cnt++;           } else if (0 < (msk[i] & mask)) {             dp[mask | msk[i]][k + 1] += dp[mask][k];             dp[mask | msk[i]][k + 1] %= MOD;           }         }         if (k < cnt) {           dp[mask][k + 1] += (cnt - k) * dp[mask][k];           dp[mask][k + 1] %= MOD;         }       }     }     ans = (ans * dp[(1 << s) - 1][t]) % MOD;     ans = (ans * Power(fac[t - 1], MOD - 2)) % MOD;     all += (t - 1);     return;   }   void Dfs_All() {     for (int i = 0; i < n; i++) {       if (!mrk[i]) {         Dfs(i);         Solve();         S.clear();         T.clear();       }     }     return;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     fac[0] = 1;     for (int i = 1; i < N; i++) {       fac[i] = (fac[i - 1] * i) % MOD;     }     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       for (int j = 0; j < i; j++) {         int u = i, v = j;         if (a[v] < a[u]) swap(u, v);         if ((a[v] % a[u]) == 0) {           adj[u].push_back(v);           adj[v].push_back(u);           in[v]++;         }       }     }     Dfs_All();     ans = (ans * fac[all]) % MOD;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int MAXN = 3e6 + 9;   long long n, k;   long long two(long long x) {     long long base = 1, ans = 0;     while (base * x <= n) {       long long f = min(n, base * (x + 1) - 1);       ans += f - base * x + 1;       base *= 2;     }     return ans;   }   long long one(long long x) {     if (x % 2 == 0) return two(x + 1) + two(x);     return two(x);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     int t = 1;     while (t--) {       cin >> n >> k;       long long N = n, ans = 1;       while (N) {         for (long long i = 0; i <= 10; i++) {           if (i >= N) break;           if (one(N - i) >= k) ans = max(ans, N - i);         }         N /= 2;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isused[26];   string s, t;   vector<pair<int, int> > v;   int abs(int num) {     if (num < 0)       return -num;     else       return num;   }   int main(void) {     cin >> s >> t;     int slen = s.size();     for (int i = 0; i < slen; i++) isused[(s[i] -  a )] = true;     int tlen = t.size();     for (int i = 0; i < tlen; i++) {       if (isused[(t[i] -  a )] == false) {         cout << -1 << endl;         return 0;       }     }     for (int i = 0; i < tlen;) {       pair<int, int> temp = make_pair(-1, -1);       for (int j = 0; j < slen; j++) {         if (t[i] == s[j]) {           int k1 = 0;           while (i + k1 + 1 < tlen && j + k1 + 1 < slen &&                  t[i + k1 + 1] == s[j + k1 + 1])             k1++;           int k2 = 0;           while (i + k2 + 1 < tlen && j - k2 - 1 >= 0 &&                  t[i + k2 + 1] == s[j - k2 - 1])             k2++;           if (k1 >= k2) {             if (temp.first == -1)               temp = make_pair(j, j + k1);             else if (abs(temp.second - temp.first) < k1)               temp = make_pair(j, j + k1);           } else {             if (temp.first == -1)               temp = make_pair(j, j - k2);             else if (abs(temp.first - temp.second) < k2)               temp = make_pair(j, j - k2);           }         }       }       if (temp.first <= temp.second)         i += (temp.second - temp.first) + 1;       else         i += (temp.first - temp.second) + 1;       v.push_back(make_pair(temp.first + 1, temp.second + 1));     }     cout << v.size() << endl;     for (int i = 0; i < v.size(); i++)       cout << v[i].first <<     << v[i].second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, totx, tot, hangx[2005], liex[2005];   double ans;   char s[2005];   long long func(int n, int m, int a[]) {     long long ret = 0;     for (int i = 1; i <= n; i++) {       long long sum = 0, cnt = m - a[i];       for (int j = 1; j <= n; j++) {         if (a[j])           sum += (m - 1) * abs(i - j);         else           sum += m * abs(i - j);       }       if (a[i])         ret += (m - 1) * sum;       else         ret += m * sum;       if (a[i]) {         int l = i - 1, r = i + 1;         while (a[l + 1] < a[l]) {           cnt += m - a[l];           l--;         }         while (a[r - 1] < a[r]) {           cnt += m - a[r];           r++;         }         ret += 4 * cnt * (a[i] - 1);       }     }     return ret;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> s;       for (int j = 1; j <= m; j++) {         if (s[j - 1] ==  X ) {           hangx[i] = j;           liex[j] = i;           totx++;         }       }     }     tot = n * m - totx;     ans = (func(n, m, hangx) + func(m, n, liex)) * 1.0 / tot / tot;     cout << fixed << setprecision(12) << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, a[1000005], ans = 0;   inline long long read() {     long long red = 0, f_f = 1;     char ch = getchar();     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) f_f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) red = red * 10 + ch -  0 , ch = getchar();     return red * f_f;   }   signed main() {     n = (1ll << read()), m = read();     for (long long i = 0; i < n; i++) a[i] = read(), ans += a[i];     printf( %.6lf n , 1.0 * ans / n);     while (m--) {       long long x = read(), y = read();       ans -= a[x];       a[x] = y;       ans += a[x];       printf( %.6lf n , 1.0 * ans / n);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false), cin.tie(nullptr);     int n, k;     cin >> n >> k;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     int res = 0;     for (int i = 0; i < k; i++) {       int x = 0, y = 0;       for (int j = i; j < n; j += k) {         if (a[j] == 1)           x++;         else           y++;       }       res += min(x, y);     }     cout << res <<   n ;     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;   }   template <class T>   inline void gn(T &first) {     char c, sg = 0;     while (c = getchar(), (c >  9  || c <  0 ) && c !=  - )       ;     for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ;          c = getchar())       first = (first << 1) + (first << 3) + c -  0 ;     if (sg) first = -first;   }   template <class T>   inline void print(T first) {     if (first < 0) {       putchar( - );       return print(-first);     }     if (first < 10) {       putchar( 0  + first);       return;     }     print(first / 10);     putchar(first % 10 +  0 );   }   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;   }   char s[100011];   pair<int, int> dp[100011];   int cnt[100011], mx[100011][2];   int main() {     int n, m;     cin >> n >> s + 1 >> m;     for (int i = 1; i <= n; i++) {       if (s[i] !=  b ) mx[i][0] = mx[i - 1][1] + 1;       if (s[i] !=  a ) mx[i][1] = mx[i - 1][0] + 1;       cnt[i] = cnt[i - 1] + (s[i] ==  ? );       dp[i] = dp[i - 1];       if (mx[i][!(m & 1)] >= m) {         if (dp[i - m].first + 1 == dp[i].first) {           smin(dp[i].second, dp[i - m].second + cnt[i] - cnt[i - m]);         }         if (dp[i - m].first + 1 > dp[i].first) {           dp[i].first = dp[i - m].first + 1;           dp[i].second = dp[i - m].second + cnt[i] - cnt[i - m];         }       }     }     cout << dp[n].second << endl;     return 0;   }


#include <bits/stdc++.h>   const double eps = 1e-10;   using namespace std;   using ll = long long;   using ul = unsigned long long;   using PII = pair<int, int>;   const int NN = 1011101;   const int NZ = 511100;   const int MM = 151;   const int need = (1 << 30) - 1;   int n, m, s, x, i, j, t, a, b, k, c, r, col[NN];   int l;   ll in[NN];   int comp[NN];   PII gr[NN];   int main() {     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d %d , &a, &b);       a--, b--;       gr[i + i] = {a, b};       gr[i + i + 1] = {b, a};     }     set<int> sv;     for (int i = 0; i < n; i++) sv.insert(i);     m += m;     sort(gr, gr + m);     vector<vector<int>> answww;     while (!sv.empty()) {       int ff = *sv.begin();       sv.erase(ff);       queue<int> qq;       qq.push(ff);       vector<int> _comp;       while (!qq.empty()) {         int v = qq.front();         qq.pop();         _comp.push_back(v);         vector<int> rv;         for (auto it : sv) {           int vv = it;           if (!binary_search(gr, gr + m, make_pair(v, it))) {             qq.push(vv);             rv.push_back(vv);           }         }         for (int i = 0; i < rv.size(); i++) {           sv.erase(rv[i]);         }       }       answww.push_back(_comp);     }     printf( %d n , answww.size());     for (int i = 0; i < answww.size(); i++) {       printf( %d  , answww[i].size());       for (int j = 0; j < answww[i].size(); j++) {         printf(  %d , answww[i][j] + 1);       }       puts(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (b == 0) {       return a;     }     gcd(b, a % b);   }   int n, m, a, b, t;   bool eo = 0;   int main() {     cin >> n >> m;     n++;     m++;     for (int i = 1; i <= n; i++) {       if (i == 1)         cin >> a;       else         cin >> t;     }     for (int i = 1; i <= m; i++) {       if (i == 1)         cin >> b;       else         cin >> t;     }     if (a * b < 0) eo = 1;     if (n > m) {       if (eo) cout <<  - ;       cout <<  Infinity  << endl;     }     if (n < m) {       cout <<  0/1 ;     }     if (n == m) {       a = abs(a), b = abs(b);       int t = gcd(a, b);       if (eo) cout <<  - ;       a /= t, b /= t;       cout << a <<  /  << b;     }   }


#include <bits/stdc++.h>   using namespace std;   bool test_cases;   void init(bool k) {     test_cases = k;     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   struct test_cases {     void prepare_left(long long *great_left, long long n, long long *a) {       stack<long long> st;       great_left[0] = -1;       st.push(a[0]);       for (long long i = 1; i < n; i++) {         while (!st.empty() and st.top() < a[i]) {           st.pop();         }         if (st.empty()) {           great_left[i] = -1;         } else {           great_left[i] = st.top();         }         st.push(a[i]);       }     }     void prepare_right(long long *great_right, long long n, long long *a) {       stack<long long> st;       great_right[n - 1] = -1;       st.push(a[n - 1]);       for (long long i = n - 2; i >= 0; i--) {         while (!st.empty() and st.top() < a[i]) {           st.pop();         }         if (st.empty()) {           great_right[i] = -1;         } else {           great_right[i] = st.top();         }         st.push(a[i]);       }     }     void test_case(long long test) {       long long n;       cin >> n;       long long a[n];       long long great_left[n];       long long great_right[n];       for (long long i = 0; i < n; i++) cin >> a[i];       long long ans = 0;       for (long long i = 0; i < n - 1; i++) {         ans = max(ans, a[i] ^ a[i + 1]);       }       prepare_left(great_left, n, a);       prepare_right(great_right, n, a);       for (long long i = 0; i < n; i++) {         long long left = great_left[i];         long long right = great_right[i];         if (left != -1) {           ans = max(ans, left ^ a[i]);         }         if (right != -1) {           ans = max(ans, right ^ a[i]);         }         if (left != -1 and right != -1) {           ans = max(ans, left ^ right);         }       }       cout << ans <<   n ;     }   };   int32_t main() {     init(false);     long long tc;     if (test_cases) {       cin >> tc;     } else {       tc = 1;     }     for (long long i = 0; i < tc; i++) {       struct test_cases o;       o.test_case(i + 1);     }     return 0;   };


#include <bits/stdc++.h>   using namespace std;   bool solve(vector<int>& arr, int m, int n, int k) {     vector<int> dp(n + 1, 0);     int last = 0;     for (int i = k; i <= n; i++) {       int l = dp[i - k] + 1;       if (arr[i] - arr[l] <= m) last = i;       dp[i] = last;     }     return dp[n] == n;   }   int main() {     int n, k;     cin >> n >> k;     vector<int> arr(n + 1);     for (int i = 1; i <= n; i++) cin >> arr[i];     sort(arr.begin() + 1, arr.end());     int s = 0;     int g = 1e9 + 1;     int ans = -1;     while (s != g) {       int m = (s + g) / 2;       if (solve(arr, m, n, k)) {         g = m;         ans = m;       } else {         if (s == m) {           break;         }         s = m;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5, INF = N;   int n, L, R, ANS_U, ANS_V, ANS;   struct st {     int nxt, to, w, dis;   } ed[N << 1];   int head[N], num;   void add(int from, int to, int w) {     ed[++num].to = to, ed[num].w = w;     ed[num].nxt = head[from];     head[from] = num;   }   int sum, rt, siz[N], mx[N];   bool vis[N];   void getrt(int x, int fa) {     mx[x] = 0;     for (int u, i = head[x]; i; i = ed[i].nxt) {       u = ed[i].to;       if (u == fa || vis[u]) continue;       getrt(u, x), mx[x] = max(mx[x], siz[u]);     }     mx[x] = max(mx[x], sum - siz[x]);     if (mx[x] < mx[rt]) rt = x;   }   int mxd[N], V[N];   vector<int> E[N];   void dfs_init(int x, int fa) {     mxd[x] = 0, siz[x] = 1;     for (int u, i = head[x]; i; i = ed[i].nxt) {       u = ed[i].to;       if (vis[u] || u == fa) continue;       dfs_init(u, x), mxd[x] = max(mxd[x], mxd[u] + 1), siz[x] += siz[u];     }   }   bool cmp(int a, int b) { return mxd[a] < mxd[b]; }   void work(int x) {     dfs_init(x, 0);     int h = 0;     for (int i = head[x]; i; i = ed[i].nxt)       if (!vis[ed[i].to]) V[++h] = ed[i].to;     sort(V + 1, V + 1 + h, cmp);     for (int i = 1; i <= h; i++) E[x].push_back(V[i]);   }   void solve_init(int x) {     work(x), vis[x] = 1;     for (int u, i = head[x]; i; i = ed[i].nxt) {       u = ed[i].to;       if (vis[u]) continue;       rt = 0, sum = siz[u], getrt(u, x), solve_init(rt);     }   }   int f[N], a[N], s[N], id_a[N], id_s[N], ans_u, ans_v, ans;   void dfs(int x, int fa, int dep) {     if (f[x] > a[dep]) a[dep] = f[x], id_a[dep] = x;     for (int u, i = head[x]; i; i = ed[i].nxt) {       u = ed[i].to;       if (vis[u] || u == fa) continue;       f[u] = f[x] + ed[i].dis, dfs(u, x, dep + 1);     }   }   int q[N], hd, tail;   void calc(int x) {     dfs_init(x, 0);     int len = 0;     for (int i = head[x]; i; i = ed[i].nxt)       if (!vis[ed[i].to]) f[ed[i].to] = ed[i].dis, ++mxd[ed[i].to];     for (int i = 0; i <= mxd[x]; i++) s[i] = -N;     s[0] = 0, id_s[0] = x;     for (auto u : E[x]) {       if (vis[u]) continue;       for (int i = 0; i <= mxd[u]; i++) a[i] = -N;       a[0] = 0, id_a[0] = x;       dfs(u, x, 1), hd = 1, tail = 0;       if (L <= len)         for (int i = min(R, len); i > L; i--) {           while (hd <= tail && s[q[tail]] <= s[i]) --tail;           q[++tail] = i;         }       for (int i = 0; i <= mxd[u]; i++) {         while (hd <= tail && q[hd] > R - i) ++hd;         if (len >= L - i && L - i >= 0) {           while (hd <= tail && s[L - i] >= s[q[tail]]) --tail;           q[++tail] = L - i;         }         if (hd <= tail && s[q[hd]] + a[i] > ans)           ans = s[q[hd]] + a[i], ans_u = id_a[i], ans_v = id_s[q[hd]];       }       for (int i = 0; i <= mxd[u]; i++)         if (a[i] > s[i]) s[i] = a[i], id_s[i] = id_a[i];       len = max(len, mxd[u]);     }   }   void solve(int x) {     calc(x), vis[x] = 1;     for (int u, i = head[x]; i; i = ed[i].nxt) {       u = ed[i].to;       if (vis[u]) continue;       rt = 0, sum = siz[u], getrt(u, x), solve(rt);     }   }   int main() {     int u, v, w, l = 0, r = 0;     scanf( %d%d%d , &n, &L, &R);     for (int i = 1; i < n; i++)       scanf( %d%d%d , &u, &v, &w), add(u, v, w), add(v, u, w), r = max(r, w);     mx[0] = N, rt = 0, sum = n, getrt(1, 0), solve_init(rt);     while (l <= r) {       int mid = (l + r) >> 1;       for (int i = 1; i <= num; i++) ed[i].dis = (ed[i].w >= mid) ? 1 : -1;       for (int i = 1; i <= n; i++) vis[i] = 0;       mx[0] = N, ans = -N, rt = 0, sum = n, getrt(1, 0), solve(rt);       if (ans >= 0)         ANS_U = ans_u, ANS_V = ans_v, ANS = mid, l = mid + 1;       else         r = mid - 1;     }     printf( %d %d n , ANS_U, ANS_V);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int light[3000];   int agr[3000];   int main() {     int n;     scanf( %d , &n);     int m = pow(2, n + 1) - 1;     for (int i = 2; i <= m; i++) {       scanf( %d , &light[i]);     }     int ans = 0;     for (int i = m; i >= 3; i--) {       if (i % 2 == 0) continue;       int a = light[i] + agr[i];       int b = light[i - 1] + agr[i - 1];       int c = a;       if (a != b) {         c = max(a, b);         ans += abs(b - a);       }       agr[i / 2] = c;       agr[(i - 1) / 2] = c;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   long long k, wyn;   long long tab[27][27];   map<long long, long long> odp[27][27];   void back1(int, int, long long);   void back2(int, int, long long);   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) cin >> tab[i][j];     }     back1(1, 1, tab[1][1]);     back2(n, m, k);     if (n == 1 && m == 1 && tab[1][1] == k)       cout << 1;     else       cout << wyn;     return 0;   }   void back1(int x, int y, long long akt) {     if (x + y == n + 1) {       odp[x][y][akt]++;       return;     }     if (x + 1 <= n) back1(x + 1, y, (akt ^ tab[x + 1][y]));     if (y + 1 <= m) back1(x, y + 1, (akt ^ tab[x][y + 1]));   }   void back2(int x, int y, long long akt) {     if (x + y == n + 1) {       wyn += odp[x][y][akt];       return;     }     if (x - 1 >= 1) back2(x - 1, y, (akt ^ tab[x][y]));     if (y - 1 >= 1) back2(x, y - 1, (akt ^ tab[x][y]));   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int calc(int a, int x, int n) { return max(0, min(x - (a + 1) / 2, n - a)); }   void solve(istream &in, ostream &out) {     int n, m, x, y, a, b;     in >> n >> m >> x >> y >> a >> b;     int d = gcd(a, b);     a /= d;     b /= d;     int t = min(n / a, m / b);     a *= t;     b *= t;     int x1 = calc(a, x, n);     int y1 = calc(b, y, m);     int x2 = x1 + a;     int y2 = y1 + b;     out << x1 <<     << y1 <<     << x2 <<     << y2 << endl;   }   int main() {     solve(cin, cout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string a, b;     cin >> a >> b;     n *= 2;     int n1 = 0, n2 = 0, q = 0;     for (int i = 0; i < n; ++i)       if (a[i] ==  1 )         if (b[i] ==  1 )           q = !q;         else           ++n1;       else if (b[i] ==  1 )         ++n2;     n1 += q;     if (n1 == n2 - 1 || n1 == n2)       cout <<  Draw ;     else if (n1 > n2)       cout <<  First ;     else       cout <<  Second ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool a[18][18];   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) a[i][j] = 1;     }     map<string, int> mp;     string name;     vector<string> names(n);     for (int i = 0; i < n; i++) {       cin >> name;       mp[name] = i;       names[i] = name;     }     string s1, s2;     for (int i = 0; i < m; i++) {       cin >> s1 >> s2;       a[mp[s1]][mp[s2]] = 0;       a[mp[s2]][mp[s1]] = 0;     }     int res = 0;     vector<int> v;     vector<int> vres;     for (int i = 1; i < (1 << n); i++) {       v.clear();       for (int j = 0; j < n; j++) {         if ((1 << j) & i) {           v.push_back(j);         }       }       bool b = true;       for (int j = 0; j < v.size(); j++) {         for (int k = j + 1; k < v.size(); k++) {           if (!a[v[j]][v[k]]) {             b = false;             break;           }         }       }       if (b) {         if (v.size() > res) {           res = v.size();           vres = v;         }       }     }     cout << res << endl;     vector<string> team;     for (int i = 0; i < vres.size(); i++) {       team.push_back(names[vres[i]]);     }     sort(team.begin(), team.end());     for (int i = 0; i < team.size(); i++) {       if (i != 0) cout << endl;       cout << team[i];     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[2010], b[2010];   unordered_map<int, int> mp;   int main() {     int n;     scanf( %d , &n);     int ans = 0;     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       mp[a[i]] = 1;     }     for (int i = 0; i < n; i++) {       scanf( %d , &b[i]);       mp[b[i]] = 1;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (mp[a[i] ^ b[j]] == 1) ans++;       }     }     if (ans % 2 == 1) {       puts( Koyomi );     } else {       puts( Karen );     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   int n, sum;   int cnt[100005], from[100005], to[100005], c[100005];   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) scanf( %d%d , &from[i], &to[i]);     for (int i = 1; i <= n; i++) scanf( %d , &c[i]);     for (int i = 1; i < n; i++) {       if (c[from[i]] != c[to[i]]) {         cnt[from[i]]++;         cnt[to[i]]++;         sum++;       }     }     for (int i = 1; i <= n; i++) {       if (cnt[i] == sum) {         printf( YES n%d n , i);         return 0;       }     }     printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int solve(int t) {     int a, b, c, rem = 0, sum = 0;     while (t != 0) {       rem = t % 10;       sum += rem;       t /= 10;     }     if (sum % 4 == 0) return 1;     return 0;   }   int main() {     int t, n, flag = 0;     cin >> t;     while (1) {       flag = solve(t);       if (flag == 1) {         cout << t <<   n ;         break;       } else         t++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace IO {   const int __S = (1 << 20) + 5;   char __buf[__S], *__H, *__T;   inline char getc() {     if (__H == __T) __T = (__H = __buf) + fread(__buf, 1, __S, stdin);     if (__H == __T) return -1;     return *__H++;   }   template <class __I>   inline void read(__I &__x) {     __x = 0;     int __fg = 1;     char __c = getc();     while (!isdigit(__c) && __c !=  - ) __c = getc();     if (__c ==  - ) __fg = -1, __c = getc();     while (isdigit(__c)) __x = __x * 10 + __c -  0 , __c = getc();     __x *= __fg;   }   inline void readd(double &__x) {     __x = 0;     double __fg = 1.0;     char __c = getc();     while (!isdigit(__c) && __c !=  - ) __c = getc();     if (__c ==  - ) __fg = -1.0, __c = getc();     while (isdigit(__c)) __x = __x * 10.0 + __c -  0 , __c = getc();     if (__c !=  . ) {       __x = __x * __fg;       return;     } else       while (!isdigit(__c)) __c = getc();     double __t = 1e-1;     while (isdigit(__c))       __x = __x + 1.0 * (__c -  0 ) * __t, __t = __t * 0.1, __c = getc();     __x = __x * __fg;   }   inline void reads(char *__s, int __x) {     char __c = getc();     int __tot = __x - 1;     while (__c <  !  || __c >  ~ ) __c = getc();     while (__c >=  !  && __c <=  ~ ) __s[++__tot] = __c, __c = getc();     __s[++__tot] =   0 ;   }   char __obuf[__S], *__oS = __obuf, *__oT = __oS + __S - 1, __c, __qu[55];   int __qr;   inline void flush() {     fwrite(__obuf, 1, __oS - __obuf, stdout);     __oS = __obuf;   }   inline void putc(char __x) {     *__oS++ = __x;     if (__oS == __oT) flush();   }   template <class __I>   inline void print(__I __x) {     if (!__x) putc( 0 );     if (__x < 0) putc( - ), __x = -__x;     while (__x) __qu[++__qr] = __x % 10 +  0 , __x /= 10;     while (__qr) putc(__qu[__qr--]);   }   inline void prints(const char *__s, const int __x) {     int __len = strlen(__s + __x);     for (int __i = __x; __i < __len + __x; __i++) putc(__s[__i]);   }   inline void printd(double __x, int __d) {     long long __t = (long long)floor(__x);     print(__t);     putc( . );     __x -= (double)__t;     while (__d--) {       double __y = __x * 10.0;       __x *= 10.0;       int __c = (int)floor(__y);       putc(__c +  0 );       __x -= floor(__y);     }   }   inline void el() { putc(  n ); }   inline void sp() { putc(   ); }   }  // namespace IO   using namespace IO;   int cnt, sz[400005], usz[400005], n, x, y, g[400005], to[800005], he[400005],       ne[800005], fm[400005], um[400005], sm[400005], ans[400005];   void dfs1(int x, int fa) {     sz[x] = 1;     for (int i = he[x], y; ~i; i = ne[i])       if ((y = to[i]) != fa) {         dfs1(y, x), sz[x] += sz[y];         if (sz[y] > n / 2)           g[x] = y, sm[x] = max(sm[x], fm[y]),           sm[x] > fm[x] ? swap(sm[x], fm[x]) : void(0);         else           sm[x] = max(sm[x], sz[y]), sm[x] > fm[x] ? swap(sm[x], fm[x]) : void(0);       }     usz[x] = n - sz[x];     if (usz[x] > n / 2) g[x] = -1;   }   void dfs2(int x, int fa) {     if (usz[x] <= n / 2)       um[x] = usz[x];     else {       if (fm[fa] == sz[x])         um[x] = sm[fa];       else         um[x] = fm[fa];       um[x] = max(um[x], um[fa]);     }     for (int i = he[x], y; ~i; i = ne[i])       if ((y = to[i]) != fa) dfs2(y, x);   }   void add(int x, int y) {     to[cnt] = y;     ne[cnt] = he[x];     he[x] = cnt++;   }   int main() {     memset(he, -1, sizeof(he));     read(n);     for (int i = 1; i < n; i++) read(x), read(y), add(x, y), add(y, x);     dfs1(1, 0);     dfs2(1, 0);     for (int i = 1; i <= n; i++) {       if (!g[i])         ans[i] = 1;       else if (g[i] == -1 && usz[i] - um[i] <= n / 2)         ans[i] = 1;       else if (g[i] != -1 && sz[g[i]] - fm[g[i]] <= n / 2)         ans[i] = 1;     }     for (int i = 1; i <= n; i++) print(ans[i]), sp();     flush();   }


#include <bits/stdc++.h>   using namespace std;   long long int power(long long int a, long long int b) {     if (b == 0) return 1;     if (b == 1) return a;     long long int r = power(a, b / 2) % 1000000007;     if (b % 2 == 0)       return (r * r) % 1000000007;     else       return (((r * r) % 1000000007) * a) % 1000000007;   }   int prime[1000009];   int sumprime[1000009];   void sieve(int n) {     prime[0] = prime[1] = 1;     for (int i = 2; i <= sqrt(n); i++) {       if (!prime[i]) {         for (int j = 2 * i; j <= n; j = j + i) {           prime[j]++;         }       }     }     for (int i = 1; i <= n; i++) {       if (!prime[i]) {         sumprime[i] = sumprime[i - 1] + 1;       } else {         sumprime[i] = sumprime[i - 1];       }     }   }   bool comp(const pair<long long int, long long int> &a,             const pair<long long int, long long int> &b) {     return (a.second < b.second);   }   const int N = 5 * 1e5 + 9;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     string s;     cin >> s;     int n = s.length();     int flag = 0;     map<char, int> mp;     mp[ A ]++;     mp[ H ]++;     mp[ I ]++;     mp[ M ]++;     mp[ O ]++;     mp[ o ]++;     mp[ T ]++;     mp[ U ]++;     mp[ V ]++;     mp[ v ]++;     mp[ W ]++;     mp[ w ]++;     mp[ X ]++;     mp[ x ]++;     mp[ Y ]++;     mp[ b ]++;     mp[ d ]++;     mp[ p ]++;     mp[ q ]++;     if (n % 2) {       char c = s[n / 2];       if (c ==  p  || c ==  q  || c ==  d  || c ==  b ) {         flag++;       }     }     for (int i = 0; i < n; i++) {       if (mp.find(s[i]) == mp.end()) {         flag++;         break;       }       if (s[i] ==  b ) {         if (s[n - i - 1] !=  d ) {           flag++;           break;         }         continue;       }       if (s[i] ==  d ) {         if (s[n - i - 1] !=  b ) {           flag++;           break;         }         continue;       }       if (s[i] ==  p ) {         if (s[n - i - 1] !=  q ) {           flag++;           break;         }         continue;       }       if (s[i] ==  q ) {         if (s[n - i - 1] !=  p ) {           flag++;           break;         }         continue;       }       if (s[i] != s[n - i - 1]) {         flag++;         break;       }     }     if (flag) {       cout <<  NIE n ;       return 0;     }     cout <<  TAK n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     std::string a;     std::getline(cin, a);     ;     std::string b;     std::getline(cin, b);     ;     int team1 = 0;     int team2 = 0;     for (int i = 0; i < a.size(); i = i + 2) {       if (a[i] == b[i]) {       } else if ((a[i] ==  8  && b[i] ==  [ ) || (a[i] ==  [  && b[i] ==  ( ) ||                  (a[i] ==  (  && b[i] ==  8 )) {         team1++;       } else {         team2++;       }     }     if (team1 == team2) {       cout <<  TIE ;     } else if (team1 > team2) {       cout <<  TEAM 1 WINS ;     } else {       cout <<  TEAM 2 WINS ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, x, y, z;     cin >> a >> b >> c >> x >> y >> z;     int a1, b1, c1, a2, b2, c2, m = 0, n = 0;     if (a > x) {       if (abs(a - x) >= 2) {         if ((a - x) % 2 == 0)           m = m + a - x;         else           m = m + a - x - 1;       }     } else       n = n + a - x;     if (b > y) {       if (abs(b - y) >= 2) {         if ((b - y) % 2 == 0)           m = m + b - y;         else           m = m + b - y - 1;       }     } else       n = n + b - y;     if (c > z) {       if (abs(c - z) >= 2) {         if ((c - z) % 2 == 0)           m = m + c - z;         else           m = m + c - z - 1;       }     } else       n = n + c - z;     if (m >= n * -2)       cout <<  Yes  << endl;     else       cout <<  No  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   int R, C;   string t[2222];   int s1[2222][2222];   int main() {     memset(s1, 0, sizeof(s1));     cin >> R >> C;     for (int r = 0; r < (int)R; r++) {       cin >> t[r];       for (int c = 0; c < (int)C; c++) {         if (t[r][c] ==  w ) {           s1[r + 1][c + 1]++;         }       }     }     for (int r = 0; r < (int)2221; r++) {       for (int c = 0; c < (int)2221; c++) {         s1[r + 1][c + 1] += (s1[r + 1][c] + s1[r][c + 1] - s1[r][c]);       }     }     int lo = 0;     int hi = max(R, C);     while (lo < hi - 1) {       int len = (lo + hi) / 2;       bool isSome = false;       for (int r = 0; r + len - 1 < max(R, C); r++) {         for (int c = 0; c + len - 1 < max(R, C); c++) {           if (s1[r + len][c + len] - s1[r + len][c] - s1[r][c + len] + s1[r][c] ==               s1[2221][2221]) {             isSome = true;             break;           }         }         if (isSome) break;       }       if (isSome)         hi = len;       else         lo = len;     }     cerr << hi << endl;     bool done = false;     for (int r = 0; r + hi - 1 < R; r++) {       for (int c = 0; c + hi - 1 < C; c++) {         int big = s1[r + hi][c + hi] - s1[r][c + hi] - s1[r + hi][c] + s1[r][c];         int small = 0;         if (hi != 1 && hi != 2) {           small = s1[r + hi - 1][c + hi - 1] - s1[r + 1][c + hi - 1] -                   s1[r + hi - 1][c + 1] + s1[r + 1][c + 1];         }         if (big - small == s1[2221][2221]) {           for (int i = 0; i < hi; i++) {             if (t[r][c + i] ==  . ) t[r][c + i] =  + ;             if (t[r + hi - 1][c + i] ==  . ) t[r + hi - 1][c + i] =  + ;             if (t[r + i][c] ==  . ) t[r + i][c] =  + ;             if (t[r + i][c + hi - 1] ==  . ) t[r + i][c + hi - 1] =  + ;           }           done = true;           break;         }         if (done) break;       }       if (done) break;     }     if (done) {       for (int r = 0; r < (int)R; r++) {         for (int c = 0; c < (int)C; c++) {           cout << t[r][c];         }         cout << endl;       }       cout << endl;     } else {       cout << -1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double EPS = 1e-9;   int N, K;   double L, V1, V2;   bool f(double t) {     if (L / V1 < t - EPS) return true;     int nGroups = N / K;     if (N % K != 0) nGroups++;     double distByBus = (t * V1 * V2 - L * V2) / (V1 - V2);     double tByBus = distByBus / V2;     double posBus = distByBus;     double tElapsed = tByBus;     nGroups--;     while (nGroups > 0) {       double posGroup = tElapsed * V1;       if (posGroup > L - EPS) break;       double tMeet = (posBus - posGroup) / (V1 + V2);       double posMeet = posGroup + tMeet * V1;       tElapsed += tMeet;       if (L - posMeet < distByBus - EPS) {         tElapsed += (L - posMeet) / V2;         posBus = L;       } else {         tElapsed += tByBus;         posBus = posMeet + distByBus;       }       nGroups--;     }     return tElapsed < t + EPS;   }   int main() {     cin >> N >> L >> V1 >> V2 >> K;     double lo = 0, hi = 1e9 + 5;     int i = 0;     while (hi - lo > EPS && i++ < 100) {       double mid = (lo + hi) / 2;       if (f(mid)) {         hi = mid;       } else         lo = mid;     }     cout << fixed << setprecision(10) << hi << endl;     return 0;   }


#include <bits/stdc++.h>   inline char nc() {     static char buf[1000000], *p1 = buf, *p2 = buf;     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)                ? EOF                : *p1++;   }   inline void read(int &x) {     static char c;     for (c = getchar(); !( 0  <= c && c <=  9 ); c = getchar())       ;     for (x = 0; ( 0  <= c && c <=  9 ); c = getchar()) x = x * 10 + c - 48;   }   void write(int x) {     if (x < 10) {       putchar( 0  + x);       return;     }     write(x / 10);     putchar( 0  + x % 10);   }   inline void writeln(int x) {     if (x < 0) putchar( - ), x *= -1;     write(x);     putchar(  n );   }   inline void writel(int x) {     if (x < 0) putchar( - ), x *= -1;     write(x);     putchar(   );   }   using namespace std;   int T, a[1007], sb, ans[1007 * 1007], n, tot;   void play(int x) {     ans[++tot] = x;     a[x] ^= a[x + 2], a[x + 2] ^= a[x], a[x] ^= a[x + 2];     a[x + 1] ^= a[x + 2], a[x + 2] ^= a[x + 1], a[x + 1] ^= a[x + 2];   }   signed main() {     read(T);     while (T--) {       read(n);       tot = 0;       for (int i = 1; i <= n; i++) read(a[i]);       for (int i = 1; i <= n - 2; i++) {         sb = i;         for (int j = i + 1; j <= n; j++)           if (a[j] < a[sb]) sb = j;         while (sb >= i + 2) play(sb - 2), sb -= 2;         if (sb == i + 1) {           play(i), play(i);           continue;         }       }       for (int i = n; i >= 3; i--) {         sb = i;         for (int j = i - 1; j; j--)           if (a[j] > a[sb]) sb = j;         while (sb <= i - 2) play(sb), play(sb), sb += 2;         if (sb == i - 1) {           play(sb - 1);           continue;         }       }       for (int i = 1; i < n; i++)         if (a[i] > a[i + 1]) tot = -1;       writeln(tot);       if (tot <= 0) continue;       for (int i = 1; i < tot; i++) writel(ans[i]);       writeln(ans[tot]);     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;       #define ll long long   #define mod 1000000007     #define pb push_back   #define ff first   #define ss second   #define all(a) (a).begin(),(a).end()   #define fr(i,a,b) for(int i=a;i<b;i++)   #define mapit(i,mp) for(auto i = mp.begin(); i!=mp.end(); i++)   #define pii pair<int,int>      int cte[100001] ;   int cto[100001] ;       void solve(){       int n;          cin>>n;          int a[n];          fr(i,0,n){               cin>>a[i];          }              int b[n];              fr(i,0,n){               b[i] = a[i];          }              sort(b,b+n);                  for(int i = 0; i<n; i++){               if(i%2 == 0){                   cte[a[i]]++;               }else{                   cto[a[i]]++;               }          }              bool flag = true;              for(int i = 0; i<n; i++){               if(i%2 == 0){                   if(cte[b[i]] == 0){                       flag = false;                       break;                   }                   cte[b[i]]--;               }else{                   if(cto[b[i]] == 0){                       flag = false;                       break;                   }                   cto[b[i]]--;               }          }              if(flag){               cout<< YES <<  n ;          }else{               cout<< NO <<  n ;          }   }       int main(){       #ifndef ONLINE_JUDGE           freopen( input.txt ,  r , stdin);           freopen( output.txt ,  w , stdout);       #endif           int t = 1;       cin>>t;          while(t--)      {           fr(i,0,100001){               cte[i] = 0;           }               fr(i,0,100001){               cto[i] = 0;           }              solve();      }      return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   string s;   vector<int> pos[26];   int xd[26];   int main() {     cin >> s;     n = s.size();     for (int i = 0; i < n; i++) {       pos[s[i] -  a ].push_back(i);       xd[s[i] -  a ]++;     }     double ans = 0;     for (char c =  a ; c <=  z ; c++) {       int best = 0;       for (int shift = 0; shift < n; shift++) {         vector<int> cnt(26, 0);         for (int i : pos[c -  a ]) {           cnt[s[(i + shift) % n] -  a ]++;         }         int nunique = 0;         for (int i = 0; i < 26; i++) {           if (cnt[i] == 1) {             nunique++;           }         }         best = max(best, nunique);       }       ans += 1.0 * best / n;     }     printf( %.10lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e+5;   bool vis[maxn];   int main() {     int T, n, m, l;     cin.sync_with_stdio(false);     cout.sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     while (cin >> n) {       memset(vis, false, sizeof vis);       int pos = n;       int now = 0;       cout <<  1  ;       for (int i = 0; i < n; i++) {         int x;         cin >> x;         now++;         vis[x] = true;         while (vis[pos]) {           pos--;           now--;         }         cout << now + 1 <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> d[101000];   int k[101000];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &k[i]);       for (int j = 0; j < k[i]; j++) {         int a;         scanf( %d , &a);         d[i].push_back(a);       }       sort(d[i].begin(), d[i].end());     }     int res = 0;     for (int i = 0; i < n; i++) {       int a = (i + n - 1) % n;       int b = (i + 1) % n;       for (int j = 0; j < k[i] - 1; j++) {         vector<int>::iterator ini =             lower_bound(d[a].begin(), d[a].end(), d[i][j]);         vector<int>::iterator fim =             lower_bound(d[a].begin(), d[a].end(), d[i][j + 1]);         int cta = fim - ini;         ini = lower_bound(d[b].begin(), d[b].end(), d[i][j]);         fim = lower_bound(d[b].begin(), d[b].end(), d[i][j + 1]);         if (fim - ini != cta) {           res++;         }       }     }     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     cin >> n >> m >> k;     if (n <= min(m, k))       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   template <class C>   void mini(C& _a4, C _b4) {     _a4 = min(_a4, _b4);   }   template <class C>   void maxi(C& _a4, C _b4) {     _a4 = max(_a4, _b4);   }   template <class TH>   void _dbg(const char* sdbg, TH h) {     cerr << sdbg <<  =  << h << endl;   }   template <class TH, class... TA>   void _dbg(const char* sdbg, TH h, TA... a) {     while (*sdbg !=  , ) cerr << *sdbg++;     cerr <<  =  << h <<  , ;     _dbg(sdbg + 1, a...);   }   struct Dinic {     struct Edge {       long long v, c, inv;     };       public:     Dinic() { n = -1; }     void AddEdge(long long a, long long b, long long cap, long long bi_dir) {       if (n < max(a, b)) {         n = max(n, max(a, b));         ResizeVectors();       }       e_orig[a].push_back(Edge{b, cap, ((long long)(e_orig[b]).size())});       e_orig[b].push_back(           Edge{a, bi_dir * cap, ((long long)(e_orig[a]).size()) - 1});     }     long long MaxFlow(long long s, long long t) {       e = e_orig;       long long result = 0;       while (Bfs(s, t)) {         for (long long i = 0; i <= n; i++) {           beg[i] = 0;         }         result += Dfs(s, t, kInf);       }       return result;     }     vector<bool> MinCut(long long s, long long t) {       assert(!Bfs(s, t));       vector<bool> res(n + 1);       for (long long i = 0; i <= n; i++) {         if (dis[i] <= n) {           res[i] = true;         }       }       return res;     }       private:     long long n;     vector<vector<Edge>> e_orig, e;     vector<long long> dis, beg;     vector<pair<long long, long long>> cut;     bool Bfs(long long s, long long t) {       for (long long i = 0; i <= n; i++) {         dis[i] = n + 1;       }       dis[s] = 0;       vector<long long> que;       que.push_back(s);       for (long long i = 0; i < ((long long)(que).size()); i++) {         long long v = que[i];         for (auto edge : e[v]) {           long long nei = edge.v;           if (edge.c && dis[nei] > dis[v] + 1) {             dis[nei] = dis[v] + 1;             que.push_back(nei);             if (nei == t) {               cut.clear();               return true;             }           } else if (!edge.c) {             cut.push_back(make_pair(v, nei));           }         }       }       return false;     }     long long Dfs(long long v, long long t, long long min_cap) {       long long result = 0;       if (v == t || min_cap == 0) {         return min_cap;       }       for (long long& i = beg[v]; i < ((long long)(e[v]).size()); i++) {         long long nei = e[v][i].v;         long long c = e[v][i].c;         if (dis[nei] == dis[v] + 1 && c > 0) {           long long flow_here = Dfs(nei, t, min(min_cap, c));           result += flow_here;           min_cap -= flow_here;           e[v][i].c -= flow_here;           e[nei][e[v][i].inv].c += flow_here;         }         if (min_cap == 0) {           break;         }       }       return result;     }     void ResizeVectors() {       e_orig.resize(n + 2);       beg.resize(n + 2);       dis.resize(n + 2);     }     static const long long kInf = 1e18;   };   struct GomoryHu {     vector<vector<pair<long long, long long>>> graph;     vector<vector<long long>> nodes;     vector<bool> visited;     vector<long long> groupId, contrId;     vector<long long> edgeFrom, edgeTo, edgeLen;     long long wnode;     long long n;     GomoryHu(long long N) : graph(N), visited(N), groupId(N), contrId(N), n(N) {}     void addEdge(long long u, long long v, long long cap) {       graph[u].emplace_back(v, cap);       graph[v].emplace_back(u, cap);     }     void dfs(long long v, long long type) {       visited[v] = true;       contrId[v] = type;       for (long long i = 0; i < ((long long)(edgeFrom).size()); i++) {         if (edgeFrom[i] == v && !visited[edgeTo[i]]) {           dfs(edgeTo[i], type);         }       }     }     vector<vector<pair<long long, long long>>> run() {       vector<long long> allNodes(n);       iota((allNodes).begin(), (allNodes).end(), 0);       nodes = vector<vector<long long>>{allNodes};       edgeFrom.clear();       edgeTo.clear();       edgeLen.clear();       fill((groupId).begin(), (groupId).end(), 0);       for (long long step = 0; step < n - 1; step++) {         Dinic dinic;         for (long long i = 0; i < ((long long)(nodes).size()); i++) {           if (((long long)(nodes[i]).size()) > 1) {             wnode = i;             break;           }         }         random_shuffle((nodes[wnode]).begin(), (nodes[wnode]).end());         fill((visited).begin(), (visited).end(), false);         visited[wnode] = true;         for (long long i = 0; i < ((long long)(edgeFrom).size()); i++) {           if (edgeFrom[i] == wnode) {             dfs(edgeTo[i], edgeTo[i]);           }         }         for (long long v = 0; v < n; v++) {           long long a = groupId[v] == wnode ? v : contrId[groupId[v]] + n;           for (auto& P : graph[v]) {             long long b = groupId[P.first] == wnode                               ? P.first                               : contrId[groupId[P.first]] + n;             dinic.AddEdge(a, b, P.second, 0);           }         }         dinic.AddEdge(2 * n, 2 * n, 0, 0);         long long a = nodes[wnode][0], b = nodes[wnode][1],                   f = dinic.MaxFlow(a, b);         auto cut = dinic.MinCut(a, b);         for (long long i = 0; i < ((long long)(edgeFrom).size()); i++) {           if (edgeFrom[i] != wnode && edgeTo[i] == wnode &&               !cut[edgeFrom[i] + n]) {             edgeTo[i] = step + 1;           }           if (edgeTo[i] != wnode && edgeFrom[i] == wnode && !cut[edgeTo[i] + n]) {             edgeFrom[i] = step + 1;           }         }         edgeFrom.push_back(wnode);         edgeTo.push_back(step + 1);         edgeLen.push_back(f);         edgeFrom.push_back(step + 1);         edgeTo.push_back(wnode);         edgeLen.push_back(f);         vector<long long> A, B;         for (long long v : nodes[wnode]) {           (cut[v] ? A : B).push_back(v);           if (!cut[v]) {             groupId[v] = step + 1;           }         }         nodes[wnode] = A;         nodes.push_back(B);       }       vector<vector<pair<long long, long long>>> tree(n);       for (long long i = 0; i < ((long long)(edgeFrom).size()); i++) {         tree[nodes[edgeFrom[i]][0]].emplace_back(nodes[edgeTo[i]][0], edgeLen[i]);       }       return tree;     }   };   int main() {     long long N, M;     cin >> N >> M;     GomoryHu gomory(N);     for (long long i = 0; i < M; i++) {       long long u, v, c;       cin >> u >> v >> c;       gomory.addEdge(u - 1, v - 1, c);     }     auto V = gomory.run();     vector<long long> connRoot(N);     vector<list<long long>> vertList(N);     iota((connRoot).begin(), (connRoot).end(), 0);     for (long long i = 0; i < N; i++) {       vertList[i].push_back(i);     }     vector<tuple<long long, long long, long long>> edges;     for (long long i = 0; i < N; i++) {       for (auto& P : V[i]) {         if (i < P.first) {           edges.emplace_back(-P.second, i, P.first);         }       }     }     sort((edges).begin(), (edges).end());     long long sum = 0;     for (auto E : edges) {       long long from = get<1>(E), to = get<2>(E);       sum -= get<0>(E);       from = connRoot[from];       to = connRoot[to];       if (from > to) {         swap(from, to);       }       (from, to);       vertList[from].splice(vertList[from].end(), vertList[to]);       for (long long i = 0; i < N; i++) {         if (connRoot[i] == to) {           connRoot[i] = from;         }       }     }     cout << sum <<   n ;     for (long long v : vertList[0]) {       cout << v + 1 <<    ;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using uint = unsigned int;   template <class T>   using V = vector<T>;   template <class T>   using VV = vector<vector<T>>;   template <class T, class U>   void chmax(T& x, U y) {     if (x < y) x = y;   }   template <class T, class U>   void chmin(T& x, U y) {     if (y < x) x = y;   }   template <class T>   void mkuni(V<T>& v) {     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());   }   template <class S, class T>   ostream& operator<<(ostream& o, const pair<S, T>& p) {     return o <<  (  << p.first <<  ,  << p.second <<  ) ;   }   template <class T>   ostream& operator<<(ostream& o, const vector<T>& vc) {     o <<  { ;     for (const T& v : vc) o << v <<  , ;     o <<  } ;     return o;   }   constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }   template <unsigned int mod_>   struct ModInt {     using uint = unsigned int;     using ll = long long;     using ull = unsigned long long;     constexpr static uint mod = mod_;     uint v;     ModInt() : v(0) {}     ModInt(ll _v) : v(normS(_v % mod + mod)) {}     explicit operator bool() const { return v != 0; }     static uint normS(const uint& x) { return (x < mod) ? x : x - mod; }     static ModInt make(const uint& x) {       ModInt m;       m.v = x;       return m;     }     ModInt operator+(const ModInt& b) const { return make(normS(v + b.v)); }     ModInt operator-(const ModInt& b) const { return make(normS(v + mod - b.v)); }     ModInt operator-() const { return make(normS(mod - v)); }     ModInt operator*(const ModInt& b) const { return make((ull)v * b.v % mod); }     ModInt operator/(const ModInt& b) const { return *this * b.inv(); }     ModInt& operator+=(const ModInt& b) { return *this = *this + b; }     ModInt& operator-=(const ModInt& b) { return *this = *this - b; }     ModInt& operator*=(const ModInt& b) { return *this = *this * b; }     ModInt& operator/=(const ModInt& b) { return *this = *this / b; }     ModInt& operator++(int) { return *this = *this + 1; }     ModInt& operator--(int) { return *this = *this - 1; }     ll extgcd(ll a, ll b, ll& x, ll& y) const {       ll p[] = {a, 1, 0}, q[] = {b, 0, 1};       while (*q) {         ll t = *p / *q;         for (int i = 0; i < int(3); i++) swap(p[i] -= t * q[i], q[i]);       }       if (p[0] < 0)         for (int i = 0; i < int(3); i++) p[i] = -p[i];       x = p[1], y = p[2];       return p[0];     }     ModInt inv() const {       ll x, y;       extgcd(v, mod, x, y);       return make(normS(x + mod));     }     ModInt pow(ll p) const {       if (p < 0) return inv().pow(-p);       ModInt a = 1;       ModInt x = *this;       while (p) {         if (p & 1) a *= x;         x *= x;         p >>= 1;       }       return a;     }     bool operator==(const ModInt& b) const { return v == b.v; }     bool operator!=(const ModInt& b) const { return v != b.v; }     friend istream& operator>>(istream& o, ModInt& x) {       ll tmp;       o >> tmp;       x = ModInt(tmp);       return o;     }     friend ostream& operator<<(ostream& o, const ModInt& x) { return o << x.v; }   };   using mint = ModInt<998244353>;   V<mint> fact, ifact;   mint Choose(int a, int b) {     if (b < 0 || a < b) return 0;     return fact[a] * ifact[b] * ifact[a - b];   }   void InitFact(int N) {     fact.resize(N);     ifact.resize(N);     fact[0] = 1;     for (int i = 1; i <= int(N - 1); i++) fact[i] = fact[i - 1] * i;     ifact[N - 1] = fact[N - 1].inv();     for (int i = N - 2; i >= 0; i--) ifact[i] = ifact[i + 1] * (i + 1);   }   void test() {     int N, M;     cin >> N >> M;     V<mint> f(N + 1);     for (int x = int(N) - 1; x >= 0; x--) {       void(0);       mint sum_pi = 0;       V<mint> p(N - x + 1), q(N - x + 1);       for (int i = 0; i < int(N - x + 1); i++) {         mint pi = 0;         mint qi = 0;         for (int j = 0; j < int(x + 1); j++) {           mint pij = ifact[N + M] * fact[N + M - (i + j) - 1] * M * fact[i + j] *                      Choose(N - x, i) * Choose(x, j);           pi += pij;           qi += pij * (i + j + 1);         }         p[i] = pi, q[i] = qi;         void(0);         void(0);         void(0);         sum_pi += pi;       }       {         mint sum = 0;         for (int i = 0; i < int(N - x + 1); i++) {           sum += q[i];           if (i) sum += p[i] * f[x + i];         }         f[x] = sum / (mint(1) - p[0]);       }       assert(sum_pi == 1);     }     V<mint> df(N);     for (int i = 0; i < int(N); i++) df[i] = f[i] - f[i + 1];     reverse(df.begin(), df.end());     for (int i = int(N) - 1; i >= 0; i--) df[i] /= df[0];     cout << df << endl;   }   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     cout << fixed << setprecision(20);     InitFact(3000000);     int N, M;     cin >> N >> M;     mint ans = 0;     for (int i = 0; i < int(N); i++) {       if (i == 0)         ans += mint(M).inv();       else         ans += (mint(M + 1) * (i + 1)).inv();     }     ans *= M;     ans *= N + M + 1;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T &x) {     x = 0;     bool f = 0;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = 1;     for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);     if (f) x = -x;   }   template <typename F>   inline void write(F x, char ed =   n ) {     static short st[30];     short tp = 0;     if (x < 0) putchar( - ), x = -x;     do st[++tp] = x % 10, x /= 10;     while (x);     while (tp) putchar( 0  | st[tp--]);     putchar(ed);   }   template <typename T>   inline void Mx(T &x, T y) {     x < y && (x = y);   }   template <typename T>   inline void Mn(T &x, T y) {     x > y && (x = y);   }   const int N = 50005;   int a[N], n;   long long ask(int l, int r) {     cout <<  ?   << l <<     << r << endl;     fflush(stdout);     long long t;     cin >> t;     return t;   }   int main() {     read(n);     long long t1 = ask(1, 2), t2 = ask(1, 3), t3 = ask(2, 3);     a[3] = t2 - t1, a[2] = t3 - a[3], a[1] = t1 - a[2];     for (int i = 4; i <= n; i++) a[i] = ask(i - 1, i) - a[i - 1];     cout <<  !  ;     for (int i = 1; i <= n; i++) cout << a[i] <<    ;     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using i64 = int64_t;   using i32 = int32_t;   template <class T, class U>   void init_n(vector<T>& v, size_t n, U x) {     v = vector<T>(n, x);   }   template <class T>   void init_n(vector<T>& v, size_t n) {     init_n(v, n, T());   }   template <class T>   void read_n(vector<T>& v, size_t n, size_t o = 0) {     v = vector<T>(n + o);     for (size_t i = o; i < n + o; ++i) cin >> v[i];   }   template <class T>   void read_n(T a[], size_t n, size_t o = 0) {     for (size_t i = o; i < n + o; ++i) cin >> a[i];   }   template <class T>   T gabs(const T& x) {     return max(x, -x);   }   void solve() {     int n;     cin >> n;     vector<int> p(n + 1);     vector<int> q(n + 1);     for (int i = 1; i <= n; ++i) {       cin >> p[i];       q[p[i]] = i;     }     string res(n,  0 );     res[0] =  1 ;     for (int i = 2, l = q[1], r = q[1]; i <= n; ++i) {       l = min(l, q[i]);       r = max(r, q[i]);       if (i == r - l + 1) res[i - 1] =  1 ;     }     cout << res <<   n ;   }   int q;   int main() {     cin >> q;     while (q--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using lint = long long;   using P = pair<int, int>;   using vec = vector<int>;   using mat = vector<vector<int>>;   constexpr int MOD = 1000000007;   const int INF = 1 << 30;   bool is_prime(int n) {     if (n <= 1) return false;     for (int i = 2; i * i <= n; i++) {       if (n % i == 0) return false;     }     return true;   }   vec get_primes() {     vec p;     for (int i = 2; i <= 1000; i++) {       if (is_prime(i)) p.push_back(i);     }     return p;   }   int main() {     int t;     cin >> t;     for (int _ = 0; _ < (int)(t); _++) {       int n;       cin >> n;       vec a(n);       for (int i = 0; i < (int)(n); i++) cin >> a[i];       auto p = get_primes();       vec c(n, -1);       int now = 1;       for (auto&& pm : p) {         bool f = false;         for (int i = 0; i < (int)(n); i++) {           if (c[i] == -1) {             if (a[i] % pm == 0) c[i] = now, f = true;           }         }         if (f) now++;       }       int mx = -1;       for (int i = 0; i < (int)(n); i++) mx = max(mx, c[i]);       cout << mx <<   n ;       for (int i = 0; i < (int)(n); i++) {         if (i != 0) cout <<    ;         cout << c[i];       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   inline long long rd() {     long long _x = 0;     int _ch = getchar(), _f = 1;     for (; !isdigit(_ch) && (_ch !=  - ) && (_ch != EOF); _ch = getchar())       ;     if (_ch ==  - ) {       _f = 0;       _ch = getchar();     }     for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch -  0 ;     return _f ? _x : -_x;   }   void write(long long _x) {     if (_x >= 10)       write(_x / 10), putchar(_x % 10 +  0 );     else       putchar(_x +  0 );   }   inline void wrt(long long _x, char _p) {     if (_x < 0) putchar( - ), _x = -_x;     write(_x);     if (_p) putchar(_p);   }   int Q = 13;   int n, cnt, to[1005];   std::vector<int> vt[13];   long long r[13];   int main() {     n = rd();     for (int i = 1; i < (1 << Q); i++)       if (__builtin_popcount(i) == 6) {         cnt++;         to[cnt] = i;         for (int j = 0; j < Q; j++)           if (i & (1 << j)) {             vt[j].push_back(cnt);           }         if (cnt == n) break;       }     for (int i = 0; i < 13; i++)       if (vt[i].size()) {         printf( ? %d  , (int)vt[i].size());         for (auto j : vt[i]) {           printf( %d  , j);         }         printf(  n );         fflush(stdout);         r[i] = rd();       }     printf( !  );     for (int i = 1; i <= n; i++) {       long long res = 0;       for (int j = 0; j < Q; j++)         if (!(to[i] & (1 << j))) {           res |= r[j];         }       printf( %lld  , res);     }     printf(  n );     fflush(stdout);   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const int arr = 2e5 + 10;   const int ar = 2e3 + 10;   const long double pi = acos(-1);   const long double eps = 1e-10;   const long long md = 1e9 + 7;   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   #pragma GCC optimize( unroll-loops )   const int buf_size = 4096;   int buf_pos, buf_len;   unsigned char buf[buf_size];   inline bool is_eof() {     if (buf_pos == buf_len) {       buf_pos = 0;       buf_len = fread(buf, 1, buf_size, stdin);       if (buf_len == 0) {         return 1;       }     }     return 0;   }   inline int get_char() { return is_eof() ? -1 : buf[buf_pos++]; }   inline int read_char() {     int c = get_char();     while (c != -1 && c <= 32) {       c = get_char();     }     return c;   }   inline int read_int() {     int x = 0;     int c = read_char();     int m = 1;     if (c ==  - ) {       m = -1;       c = get_char();     }     while ( 0  <= c && c <=  9 ) {       x = x * 10 + c -  0 ;       c = get_char();     }     return x * m;   }   int my_top[arr];   int s[arr];   int t[arr];   int where_go[arr];   int pred[arr];   vector<int> reb[arr];   int pos[arr];   int last_pos = 0;   int a[arr];   int n, m;   int ans = 0;   void foo(int* a, int n, int d) {     if (d == 1) {       for (int i = 0; i < n; i++) {         a[i]++;         ans += (a[i] == 1);       }     } else {       for (int i = 0; i < n; i++) {         a[i]--;         ans -= (a[i] == 0);       }     }   }   void dooo(int q, int d) {     while (q != -1) {       int top = my_top[q];       foo(a + pos[top], pos[q] - pos[top] + 1, d);       q = pred[top];     }   }   void work(int q) {     if (q > 0) {       if (a[pos[q]] > 0) {         ans--;       }       a[pos[q]] -= md;       dooo(pred[q], 1);     } else {       q = -q;       a[pos[q]] += md;       if (a[pos[q]] > 0) {         ans++;       }       dooo(pred[q], -1);     }   }   void dfs1(int now, int p = -1) {     pred[now] = p;     s[now] = 1;     for (auto wh : reb[now]) {       if (wh != p) {         dfs1(wh, now);         s[now] += s[wh];         if (where_go[now] == 0 || s[where_go[now]] < s[wh]) {           where_go[now] = wh;         }       }     }   }   void dfs2(int now, int p = -1) {     if (!my_top[now]) {       my_top[now] = now;     }     pos[now] = last_pos++;     if (where_go[now]) {       my_top[where_go[now]] = my_top[now];       dfs2(where_go[now], now);     }     for (auto wh : reb[now]) {       if (wh != p && wh != where_go[now]) {         dfs2(wh, now);       }     }   }   int main() {     n = read_int();     m = read_int();     for (int i = 2; i <= n; i++) {       int u;       u = read_int();       reb[u].push_back(i);       reb[i].push_back(u);     }     for (int i = 1; i <= n; i++) {       t[i] = read_int();     }     dfs1(1);     dfs2(1);     for (int i = 1; i <= n; i++) {       a[pos[i]] = -t[i];     }     while (m--) {       int q;       q = read_int();       work(q);       cout << ans <<    ;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = int(1e5) + 10, mod = int(1e9) + 7;   const long long inf = (long long)(2e18);   int n, q;   vector<int> g[N];   int first[N], c[N];   long long t[N], a[N], b[N], add[N];   set<pair<long long, int> > all;   multiset<long long> st[N];   set<pair<long long, int> >::iterator it;   multiset<long long>::iterator is;   long long mn, mx;   int p[N], pn;   void globaldel(int i) {     if (st[i].size() > 0) {       it = all.find(make_pair(*st[i].begin() + add[i], i));       if (it != all.end()) all.erase(it);       it = all.find(make_pair(*st[i].rbegin() + add[i], i));       if (it != all.end()) all.erase(it);     }   }   void globaladd(int i) {     if (st[i].size() > 0) {       all.insert(make_pair(*st[i].begin() + add[i], i));       all.insert(make_pair(*st[i].rbegin() + add[i], i));     }   }   void Del(int i) {     globaldel(first[i]);     st[first[i]].erase(st[first[i]].find(a[i]));     globaladd(first[i]);   }   void Add(int i) {     globaldel(first[i]);     st[first[i]].insert(a[i]);     globaladd(first[i]);   }   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %I64d , &t[i]);     for (int i = 1; i <= n; i++) {       scanf( %d , &first[i]);       g[first[i]].push_back(i);       c[first[i]]++;     }     for (int i = 1; i <= n; i++) {       long long val = t[i] / (c[i] + 2);       b[i] = t[i] - (c[i] + 1) * val;       a[i] += b[i];       for (int j = 0; j < g[i].size(); j++) {         int to = g[i][j];         a[to] += val;       }       a[first[i]] += val;     }     for (int i = 1; i <= n; i++) {       for (int j = 0; j < g[i].size(); j++) {         int to = g[i][j];         st[i].insert(a[to]);       }       if (st[i].size() > 0) {         all.insert(make_pair(*st[i].rbegin(), i));         all.insert(make_pair(*st[i].begin(), i));       }     }     int tp, i, j;     for (int qq = 1; qq <= q; qq++) {       scanf( %d , &tp);       if (tp == 1) {         scanf( %d%d , &i, &j);         Del(i);         Del(j);         Del(first[j]);         globaldel(j);         a[j] -= b[j];         c[j]++;         long long val = t[j] / (c[j] + 2);         a[i] += t[j] / (c[j] + 1);         ;         b[j] = t[j] - (c[j] + 1) * val;         a[j] += b[j];         val -= t[j] / (c[j] + 1);         a[i] -= add[j];         add[j] += val;         a[first[j]] += val;         a[j] += t[i] / (c[i] + 2);         a[i] += add[first[i]];         Add(j);         Add(first[j]);         int v = first[i];         Del(v);         Del(first[v]);         globaldel(v);         first[i] = j;         a[i] -= t[v] / (c[v] + 2);         a[v] -= t[i] / (c[i] + 2);         a[v] -= b[v];         c[v]--;         val = t[v] / (c[v] + 2);         b[v] = t[v] - (c[v] + 1) * val;         a[v] += b[v];         val -= t[v] / (c[v] + 3);         add[v] += val;         a[first[v]] += val;         Add(i);         Add(v);         Add(first[v]);         globaladd(j);         globaladd(v);       } else if (tp == 2) {         scanf( %d , &i);         printf( %I64d n , a[i] + add[first[i]]);       } else {         printf( %I64d %I64d n , (*all.begin()).first, (*all.rbegin()).first);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct ask {     int l, r, where;   } q[210000];   const int mo = 1e9 + 7, N = 1000000;   int A[210000], x[210000], pd[1100000], n, m, B[210000], where[1100000],       ans[210000];   vector<int> p[1100000];   int quick(int k1, int k2) {     int k3 = 1;     while (k2) {       if (k2 & 1) k3 = 1ll * k3 * k1 % mo;       k2 >>= 1;       k1 = 1ll * k1 * k1 % mo;     }     return k3;   }   int compare(ask k1, ask k2) { return k1.r < k2.r; }   void add(int k1, int k2) {     if (k1 == 0) return;     for (; k1 <= n; k1 += k1 & (-k1)) B[k1] = 1ll * B[k1] * k2 % mo;   }   int find(int k1) {     int ans = 1;     for (; k1; k1 -= k1 & (-k1)) ans = 1ll * ans * B[k1] % mo;     return ans;   }   void addin(int k1, int k2) {     int w = 1ll * (k1 - 1) * quick(k1, mo - 2) % mo;     add(where[k1], quick(w, mo - 2));     add(k2, w);     where[k1] = k2;   }   void insert(int k1) {     int w = x[k1];     for (int i = 0; i < p[w].size(); i++) {       int k2 = p[w][i];       addin(k2, k1);     }   }   int getans(int L, int R) {     return 1ll * A[R] * quick(A[L - 1], mo - 2) % mo * find(R) % mo *            quick(find(L - 1), mo - 2) % mo;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &x[i]);     scanf( %d , &m);     ;     A[0] = 1;     for (int i = 1; i <= n; i++) A[i] = 1ll * A[i - 1] * x[i] % mo;     for (int i = 1; i <= m; i++) {       scanf( %d%d , &q[i].l, &q[i].r);       q[i].where = i;     }     sort(q + 1, q + m + 1, compare);     for (int i = 1; i <= n; i++) B[i] = 1;     for (int i = 2; i <= N; i++)       if (pd[i] == 0)         for (int j = i; j <= N; j += i) {           pd[j] = 1;           p[j].push_back(i);         }     int now = 1;     for (int i = 1; i <= n; i++) {       insert(i);       while (now <= m && q[now].r == i)         ans[q[now].where] = getans(q[now].l, q[now].r), now++;     }     for (int i = 1; i <= m; i++) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   using LD = long double;   const int MX = 1 << 18 | 3;   int N, a[MX], Q;   LL sum;   int main() {     ios::sync_with_stdio(0);     cout << fixed << setprecision(10);     cin >> N >> Q;     for (int i = 0; i < (1 << N); i++) cin >> a[i], sum += a[i];     cout << (LD)sum / (1 << N) <<   n ;     while (Q--) {       int u, x;       cin >> u >> x;       sum = sum - a[u] + x;       a[u] = x;       cout << (LD)sum / (1 << N) <<   n ;     }     return 0;   }