#include <bits/stdc++.h>   using namespace std;   long long n, q, ar[200000], ans[200000];   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 0; i < n; i++) cin >> ar[i];     for (int i = 0; i < n - 1; i++) {       q = 1;       while (i + q * 2 < n) q *= 2;       ar[i + q] += ar[i];       if (i) ans[i] = ans[i - 1];       ans[i] += ar[i];       ar[i] = 0;     }     for (int i = 0; i < n - 1; i++) cout << ans[i] << endl;     cin.get();     cin.get();     return 0;   }


#include <bits/stdc++.h>   int main() {     static int aa[200000], pp[200000], qq[200000];     int n, k, i, p, q;     long long x, xk, ans, a;     scanf( %d%d%lld , &n, &k, &x);     for (i = 0; i < n; i++) scanf( %d , &aa[i]);     for (i = 0, p = 0; i < n; i++) {       pp[i] = p;       p |= aa[i];     }     for (i = n - 1, q = 0; i >= 0; i--) {       qq[i] = q;       q |= aa[i];     }     xk = 1;     while (k--) xk *= x;     ans = 0;     for (i = 0; i < n; i++)       if (ans < (a = pp[i] | aa[i] * xk | qq[i])) ans = a;     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   double ans;   int cnt[200];   vector<int> v[200];   int main() {     cin >> s;     double n = s.size();     for (int i = 0; i < n; i++) v[s[i]].push_back(i);     for (char i =  a ; i <=  z ; i++) {       if (!v[i].empty()) {         int x = v[i][0];         int y = 0;         for (int k = 0; k < n; k++) {           memset(cnt, 0, sizeof cnt);           for (int j = 0; j < v[i].size(); j++) {             v[i][j]++;             v[i][j] %= (int)n;             cnt[s[v[i][j]]]++;           }           int cur = 0;           for (int j =  a ; j <=  z ; j++) cur += (cnt[j] == 1);           y = max(y, cur);         }         ans += y;       }     }     cout << fixed << setprecision(8) << (ans / n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sz = 2e5;   int n, k;   long long add[sz], cost[sz], pre[sz], suf[sz], mnpre[sz], mnsuf[sz],       bestSufTake[sz], bestSufTakeFromPre[sz];   void input() {     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> add[i];     for (int i = 0; i < n; i++) cin >> cost[i];   }   void initPreSuf() {     for (int i = 0; i < n; i++) pre[i] = add[i] + (i ? pre[i - 1] : 0);     for (int i = n - 1; i >= 0; i--)       suf[i] = add[i] + (i != n - 1 ? suf[i + 1] : 0);     for (int i = 0; i < n; i++)       mnpre[i] = (i ? min(mnpre[i - 1], cost[i]) : cost[i]);     for (int i = n - 1; i >= 0; i--)       mnsuf[i] = (i != n - 1 ? min(mnsuf[i + 1], cost[i]) : cost[i]);     for (int i = n - 1; i >= 0; i--)       bestSufTake[i] = max(bestSufTake[i + 1], suf[i] - cost[i]);     for (int i = 0; i < n; i++) {       if (!i)         bestSufTakeFromPre[i] = suf[i] - cost[i];       else         bestSufTakeFromPre[i] = max(bestSufTakeFromPre[i - 1], suf[i] - cost[i]);     }   }   void solveZero() { cout << max(0LL, bestSufTake[0]) << endl; }   void solveEven() {     long long bst = max(0LL, bestSufTake[0]);     for (int i = 0; i < n - 1; i++) bst = max(bst, pre[n - 1] - cost[i]);     cout << bst << endl;   }   void solveOne() {     long long bst = max(0LL, bestSufTake[1]);     for (int i = 1; i < n - 1; i++)       bst = max(bst, pre[i] - mnpre[i] + bestSufTake[i + 1]);     for (int i = 1; i < n - 1; i++)       bst = max(bst, max(bestSufTakeFromPre[i - 1] - add[i], bestSufTake[i + 1]) +                          max(0LL, add[i] - cost[i]));     cout << bst << endl;   }   void solveOdd() {     long long bst = max(0LL, bestSufTake[1]);     long long best = add[0] - cost[0];     for (int i = n - 2; i >= 0; i--) {       if (i == 1) continue;       bst = max(bst, pre[n - 1] - cost[i]);     }     if (n == 4) {       bst = max(bst, pre[n - 1] - cost[1]);       bst = max(bst, pre[n - 1] - cost[0]);     } else {       bst = max(bst, pre[n - 1] - add[n - 1] - cost[1]);       for (int i = 0; i < n; i++) {         if (i == 1 || i == 2) continue;         bst = max(bst, pre[n - 1] - add[i] - cost[1]);       }     }     cout << bst << endl;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     input();     initPreSuf();     if (k == 0)       solveZero();     else if (k == 1)       solveOne();     else if (k % 2 == 0)       solveEven();     else       solveOdd();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, i, x, y, lsum = 1, root, tot, Ans1, Ans2;   int head[400010], f[400010], size[400010], d[400010], p[400010], l[400010],       g[400010], o[400010];   vector<int> son[400010];   map<int, int> M;   struct Edge {     int t, next, l;   } e[400010 * 8];   inline int Find(int x) { return (x == p[x]) ? x : p[x] = Find(p[x]); }   inline void Add(int s, int t) {     e[lsum].t = t;     e[lsum].next = head[s];     head[s] = lsum++;   }   inline int Abs(int x) { return (x < 0) ? -x : x; }   inline void Swap(int &a, int &b) { a ^= b ^= a ^= b; }   inline int Min(int a, int b) { return (a < b) ? a : b; }   inline int Max(int a, int b) { return (a > b) ? a : b; }   inline int read() {     int p = 0, f = 1;     char c = getchar();     while (c < 48 || c > 57) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >= 48 && c <= 57) p = (p << 1) + (p << 3) + c - 48, c = getchar();     return p * f;   }   inline void Maketree(int x, int fa) {     int i = 0;     size[x] = 1;     for (i = head[x]; i; i = e[i].next) {       if (e[i].t == fa) continue;       f[e[i].t] = x;       son[x].push_back(e[i].t);       Maketree(e[i].t, x);       size[x] += size[e[i].t];     }   }   inline void Dfs(int x) {     if (!son[x].size()) {       o[p[x]] = 1;       return;     }     for (auto i : son[x]) {       Dfs(i);       g[p[x]] += o[p[i]];       o[p[x]] += g[p[i]];     }     if (g[p[x]] && o[p[x]]) Ans1 = 3;   }   int main() {     n = read();     for (i = 1; i < n; i++) {       x = read();       y = read();       Add(x, y);       Add(y, x);       d[x]++;       d[y]++;     }     for (i = 1; i <= n; i++)       if (d[i] > 1) {         root = i;         break;       }     Maketree(root, 0);     for (i = 1; i <= n; i++) p[i] = n + i;     for (i = 1; i <= n; i++)       if (!son[i].size()) p[i] = p[f[i]];     for (i = 1; i <= n; i++)       if (p[i] == n + i) p[i] = i;     for (i = 1; i <= n; i++)       if (!M[p[i]]) Ans2++, M[p[i]]++;     Ans1 = 1;     Dfs(root);     printf( %d %d n , Ans1, Ans2 - 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long n, l = 1, r;     cin >> n;     r = n;     if (n == 1) {       cout << 1 << endl << 1;       return 0;     }     if (n == 2) {       cout << 1 << endl << 1;       return 0;     }     if (n == 3) {       cout << 2 << endl << 1 <<     << 3;       return 0;     }     if (n == 4) {       cout << 4 << endl << 2 <<     << 4 <<     << 1 <<     << 3;       return 0;     }     if (n == 5) {       cout << 5 << endl << 1 <<     << 3 <<     << 5 <<     << 2 <<     << 4;       return 0;     }     vector<long long> a(n + 1);     long long f = 0;     for (long long i = 1; i <= n; i++) {       if (f) {         a[l] = i;         l++;       } else {         a[r] = i;         r--;       }       f += 1;       f %= 2;     }     for (long long i = 1; i < n; i++) {       if (abs(a[i] - a[i + 1]) == 1) {         swap(a[i + 1], a[n]);       }     }     cout << n <<   n ;     ;     for (long long i = 1; i <= n; i++) {       cout << a[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void smin(T &a, const U &b) {     if (a > b) a = b;   }   template <typename T, typename U>   inline void smax(T &a, const 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 T1, class T2>   inline void gn(T1 &x1, T2 &x2) {     gn(x1), gn(x2);   }   template <class T1, class T2, class T3>   inline void gn(T1 &x1, T2 &x2, T3 &x3) {     gn(x1, x2), gn(x3);   }   template <class T1, class T2, class T3, class T4>   inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {     gn(x1, x2, x3), gn(x4);   }   template <class T1, class T2, class T3, class T4, class T5>   inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {     gn(x1, x2, x3, x4), gn(x5);   }   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 );   }   template <class T>   inline void println(T first) {     print(first);     putchar(  n );   }   template <class T>   inline void printsp(T first) {     print(first);     putchar(   );   }   template <class T1, class T2>   inline void print(T1 x1, T2 x2) {     printsp(x1), println(x2);   }   template <class T1, class T2, class T3>   inline void print(T1 x1, T2 x2, T3 x3) {     printsp(x1), printsp(x2), println(x3);   }   template <class T1, class T2, class T3, class T4>   inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {     printsp(x1), printsp(x2), printsp(x3), println(x4);   }   template <class T1, class T2, class T3, class T4, class T5>   inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {     printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);   }   int power(int a, int b, int m, int ans = 1) {     for (; b; b >>= 1, a = 1LL * a * a % m)       if (b & 1) ans = 1LL * ans * a % m;     return ans;   }   int a[300300], id[300300];   set<int> s;   bool cmp(int u, int v) {     if (a[u] > a[v])       return 1;     else if (a[u] == a[v]) {       if (u > v)         return 1;       else         return 0;     } else       return 0;   }   double calc(int p) {     s.insert(p);     double A = 0;     double B = 0;     set<int>::iterator pre, nxt;     pre = nxt = s.find(p);     for (int i = 0; i < 50 and pre != s.begin(); i++) {       pre--;       A += (double)(*nxt - *pre) / (1ll << i);       nxt--;     }     pre = nxt = s.find(p);     nxt++;     for (int i = 0; i < 50 and nxt != s.end(); i++) {       B += (double)(*nxt - *pre) / (1ll << i);       pre++;       nxt++;     }     return A * B / 2;   }   int main() {     int n;     gn(n);     for (int i = 0; i < n; i++) {       gn(a[i]);       id[i] = i;     }     sort(id, id + n, cmp);     double ans = 0;     s.insert(-1);     s.insert(n);     for (int i = 0; i < n; i++) {       ans += calc(id[i]) * a[id[i]];     }     ans /= ((long long)n * n);     printf( %.6lf n , ans);   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   ostream& operator<<(ostream& os, vector<T> v) {     os <<  [ ;     string sep;     for (const T& el : v) os << sep << el, sep =  ,  ;     return os <<  ] ;   }   void dbg_out() { cerr << endl; }   template <typename Head, typename... Tail>   void dbg_out(Head H, Tail... T) {     cerr <<     << H;     dbg_out(T...);   }   int dsu_find(vector<int>& parents, int a) {     return (parents[a] == a) ? a : (parents[a] = dsu_find(parents, parents[a]));   }   bool dsu_union(vector<int>& parents, int a, int b) {     int pa = dsu_find(parents, a);     int pb = dsu_find(parents, b);     if (pa != pb) {       parents[pa] = pb;       return true;     }     return false;   }   void run() {     int m, n;     cin >> m >> n;     vector<long long> a(m + 1), b(n + 1);     for (int i = 1; i < m + 1; i++) cin >> a[i];     for (int j = 1; j < n + 1; j++) cin >> b[j];     auto cost = [&](int x, int y) -> long long {       int u = min(x, y), v = max(x, y);       return a[u] + b[v - m];     };     long long tot_cost = 0;     vector<pair<long long, pair<int, int> > > edgelist;     for (int i = 1; i < m + 1; i++) {       int s;       cin >> s;       for (int x = 0; x < s; x++) {         int j;         cin >> j;         edgelist.push_back(make_pair(a[i] + b[j], make_pair(i, m + j)));         tot_cost += (a[i] + b[j]);       }     }     sort(edgelist.begin(), edgelist.end(),          greater<pair<long long, pair<int, int> > >());     vector<int> parents(m + n + 1);     for (int i = 1; i < m + n + 1; i++) parents[i] = i;     long long mst_cost = 0;     for (pair<long long, pair<int, int> > e : edgelist) {       ;       if (dsu_find(parents, e.second.first) !=           dsu_find(parents, e.second.second)) {         mst_cost += e.first;         dsu_union(parents, e.second.first, e.second.second);         ;       }     }     cout << tot_cost - mst_cost <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     run();   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const int mod = 998244353;   int sum(int a, int b) {     int s = a + b;     if (s >= mod) s -= mod;     return s;   }   int mult(int a, int b) { return (1LL * a * b) % mod; }   int pw(int a, int b) {     if (b == 0) return 1;     if (b & 1) return mult(a, pw(a, b - 1));     int res = pw(a, b / 2);     return mult(res, res);   }   int sub(int a, int b) {     int s = a - b;     if (s < 0) s += mod;     return s;   }   int n;   const int maxN = (int)1e6 + 100;   string s;   int invfact[maxN];   int fact[maxN];   int inv[maxN];   int cnk(int a, int b) {     if (a < b || b < 0) return 0;     return mult(fact[a], mult(invfact[b], invfact[a - b]));   }   int tp[maxN];   int cnt0[maxN], cnt1[maxN];   int c_pref[maxN];   int c_2[maxN];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     inv[1] = fact[0] = fact[1] = invfact[0] = invfact[1] = 1;     for (int i = 2; i < maxN; i++) {       fact[i] = mult(fact[i - 1], i);       inv[i] = mult(inv[mod % i], mod - mod / i);       invfact[i] = mult(invfact[i - 1], inv[i]);     }     cin >> s;     n = s.size();     int que = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  ( )         tp[i + 1] = 1;       else if (s[i] ==  ) )         tp[i + 1] = 0;       else         tp[i + 1] = 2;       que += tp[i + 1] == 2;     }     cnt1[0] = 0;     for (int i = 1; i <= n; i++) {       cnt1[i] = cnt1[i - 1] + (tp[i] == 1);     }     cnt0[n + 1] = 0;     for (int i = n; i >= 1; i--) {       cnt0[i] = cnt0[i + 1] + (tp[i] == 0);     }     c_pref[que + 1] = 0;     for (int i = que; i >= 0; i--) {       c_pref[i] = sum(c_pref[i + 1], cnk(que, i));     }     c_2[que] = 0;     for (int i = que - 1; i >= 0; i--) {       c_2[i] = sum(c_2[i + 1], cnk(que - 1, i));     }     int val = 0;     int que1 = 0;     for (int pref = 1; pref < n; pref++) {       if (tp[pref] == 0) continue;       if (tp[pref] == 1) {         int at_most = min(que + 1, que1 + 1 + cnt1[pref - 1] - cnt0[pref + 1]);         at_most = max(at_most, 0);         val = sum(val, c_pref[at_most]);         que1 += (tp[pref] == 2);       } else {         int at_most = min(que + 1, que1 + 1 + cnt1[pref - 1] - cnt0[pref + 1]);         at_most = max(at_most, 0);         val = sum(val, c_2[at_most]);         que1 += (tp[pref] == 2);       }     }     cout << val;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const static int inf = 10000000;   const static int mod = 1000000007;   int check(const vector<string>& s, const vector<string>& t) {     int n = s.size();     vector<int> idx(n);     for (int i = 0; i < (int)(n); i++) {       idx[i] = i;     }     int ret = 0;     do {       vector<string> ss(n);       for (int i = 0; i < (int)(n); i++) {         ss[i] = s[idx[i]];       }       int pos = 0;       for (int i = 0; i < t.size() && pos < s.size(); ++i) {         if (ss[pos] == t[i]) {           ++pos;         }       }       if (pos != s.size()) {         continue;       }       int inv = 0;       for (int i = 0; i < (int)(n); i++) {         for (int j = i + 1; j < n; ++j) {           if (idx[i] > idx[j]) {             ++inv;           }         }       }       int buf = n * (n - 1) / 2 - inv + 1;       ret = max(ret, buf);     } while (next_permutation((idx).begin(), (idx).end()));     return ret;   }   int main() {     int n;     while (cin >> n) {       vector<string> s(n);       for (int i = 0; i < (int)(n); i++) {         cin >> s[i];       }       int mxp = 0;       int idx = -1;       int k;       cin >> k;       for (int i = 0; i < (int)(k); i++) {         int m;         cin >> m;         vector<string> t(m);         for (int j = 0; j < (int)(m); j++) {           cin >> t[j];         }         int p = check(s, t);         if (p > mxp) {           mxp = p;           idx = i;         }       }       if (idx == -1) {         cout <<  Brand new problem!  << endl;       } else {         cout << idx + 1 << endl;         cout <<  [: ;         for (int i = 0; i < (int)(mxp); i++) {           cout <<  | ;         }         cout <<  :]  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   bool ask(int x, int y) {     if (x < 0 || y > n) return 0;     string s;     cout << 1 <<     << x <<     << y << endl;     cin >> s;     return s ==  TAK ;   }   int solve(int l, int r) {     if (l > r) return -1;     int mid;     while (l < r) {       mid = (l + r) >> 1;       if (ask(mid, mid + 1))         r = mid;       else         l = mid + 1;     }     return l;   }   int main() {     int i, j;     cin >> n >> k;     int ans1 = solve(1, n);     int ans2 = solve(1, ans1 - 1);     if (!ask(ans2, ans1)) ans2 = solve(ans1 + 1, n);     cout << 2 <<     << ans1 <<     << ans2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200200;   const int INF = 1000000007;   struct Ramp {     int i, x, d, t, p;     bool operator<(int o) const { return x < o; }     bool operator<(const Ramp& o) const { return x < o.x; }   } r[MAXN], rr;   struct Edge {     int a, b, c, t;     Edge(int a, int b, int c, int t = -1) : a(a), b(b), c(c), t(t) {}   };   vector<Edge> e[MAXN];   int d[MAXN];   const Edge* pre[MAXN];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) {       r[i].i = i + 1;       scanf( %d%d%d%d , &r[i].x, &r[i].d, &r[i].t, &r[i].p);       r[n + i].i = -1;       r[n + i].x = r[i].x + r[i].d;     }     n += n;     r[n].x = 0;     r[n + 1].x = m;     r[n].i = r[n + 1].i = -1;     n += 2;     sort(r, r + n);     for (int i = 1; i < n; ++i) {       e[i - 1].push_back(Edge(i - 1, i, r[i].x - r[i - 1].x));       e[i].push_back(Edge(i, i - 1, r[i].x - r[i - 1].x));     }     for (int i = 0; i < n; ++i) {       if (r[i].i == -1 || r[i].x < r[i].p) {         continue;       }       rr.x = r[i].x - r[i].p;       int a = lower_bound(r, r + n, rr) - r;       rr.x = r[i].x + r[i].d;       int b = lower_bound(r, r + n, rr) - r;       for (int aa = a - 1; aa <= a; ++aa) {         for (int bb = b; bb <= b; ++bb) {           if (0 <= aa && aa < bb && bb < n) {             int cc = r[i].p + r[i].t;             cc += abs(r[i].x - r[i].p - r[aa].x);             cc += abs(r[i].x + r[i].d - r[bb].x);             e[aa].push_back(Edge(aa, bb, cc, r[i].i));           }         }       }     }     priority_queue<pair<int, int>, vector<pair<int, int> >,                    greater<pair<int, int> > >         q;     fill(d, d + n, INF);     fill(pre, pre + n, (Edge*)NULL);     d[0] = 0;     q.push({d[0], 0});     while (!q.empty()) {       int t = q.top().first;       int v = q.top().second;       q.pop();       if (d[v] != t) {         continue;       }       for (const Edge& w : e[v]) {         if (d[w.b] > t + w.c) {           d[w.b] = t + w.c;           q.push({d[w.b], w.b});           pre[w.b] = &w;         }       }     }     printf( %d n , d[n - 1]);     vector<int> ans;     for (int i = n - 1; i > 0; i = pre[i]->a) {       if (pre[i]->t != -1) {         ans.push_back(pre[i]->t);       }     }     reverse(ans.begin(), ans.end());     printf( %d n , (int)ans.size());     for (int i : ans) {       printf( %d  , i);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<string> st;   set<string>::iterator it;   vector<long long> v, vv;   int main() {     int n, a[100000], cnt = 0, cnt2 = 0, k;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] > 0)         cnt++;       else if (a[i] < 0)         cnt2++;     }     if (n % 2 == 0)       k = n / 2;     else       k = (n / 2) + 1;     if (cnt > cnt2) {       if (cnt >= k)         cout << 2 << endl;       else         cout << 0 << endl;     } else {       if (cnt2 >= k)         cout << -2 << endl;       else         cout << 0 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[12][12];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) scanf( %d , &a[i][j]);     for (int k = 1; k <= n; k++) {       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           a[i][j] = min(a[i][j], a[i][k] + a[k][j]);         }       }     }     int ans = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) ans = max(ans, a[i][j]);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int64_t ans;   int n, m, ve, ts[1000001], nn[1000001], t, cut[1000001], p[1000001],       vis[1000001], low[1000001];   vector<int> e[1000001];   void dfs(int i) {     low[i] = ts[i] = ++t;     vis[i] = 1;     for (auto&& x : e[i])       if (!vis[x]) {         p[x] = i;         dfs(x);         low[i] = min(low[i], low[x]);         if (low[x] > ts[i]) cut[x] = 1;       } else if (x != p[i])         low[i] = min(low[i], low[x]);   }   int main() {     scanf( %d%d , &n, &m);     if (m < 2) return puts( 0 ), 0;     for (int _ = 0; _ < m; ++_) {       int s, t;       scanf( %d%d , &s, &t);       if (s == t)         ++nn[s];       else         e[s].push_back(t), e[t].push_back(s);     }     for (int i = 1; i <= n; ++i)       if (e[i].size()) {         dfs(i);         break;       }     for (int i = 1; i <= n; ++i)       if ((nn[i] || e[i].size()) && !vis[i]) return puts( 0 ), 0;     int64_t slc = accumulate(nn, nn + n + 1, 0);     ans = slc * (slc - 1);     for (int i = 1; i <= n; ++i) {       int no = nn[i];       nn[i] = 0;       nn[i] = e[i].size();       ans += nn[i] * int64_t(nn[i] - 1);       ans += slc * nn[i];     }     cout << ans / 2;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, l;     cin >> n >> l;     vector<int> flags(n + 2);     flags[0] = 0, flags[n + 1] = l;     for (int i = 0; i < n; ++i) {       cin >> flags[i + 1];     }     vector<double> s(n + 2);     vector<double> e(n + 2);     s[0] = 0;     e[n + 1] = 0;     for (int i = 1; i < n + 2; ++i) {       s[i] = s[i - 1] + (flags[i] - flags[i - 1]) * 1.0 / i;       e[n - i + 1] =           e[n - i + 2] + (flags[n - i + 2] - flags[n - i + 1]) * 1.0 / (i);     }     for (int i = 0; i < n + 2; ++i) {       if (abs(s[i] - e[i]) < 1e-6) {         cout << s[i] <<   n ;         return;       } else if (s[i] > e[i]) {         double sspd = i;         double espd = n - i + 2;         double dist = flags[i] - flags[i - 1];         double result = espd * e[i] + sspd * s[i - 1] + dist;         result = result / (sspd + espd);         cout << result <<   n ;         return;       }     }   }   int main() {     cout << setprecision(9);     cout << fixed;     int tests;     cin >> tests;     while (tests) {       solve();       tests--;     }   }


#include <bits/stdc++.h>   using namespace std;   string str;   int ss;   int par[10001];   int ans[2][10001];   int root(int x) {     if (par[x] != x) par[x] = root(par[x]);     return par[x];   }   map<string, int> m;   int main() {     while (getline(cin, str)) {       char ty = str[0];       int k = 2;       int z = 0;       int p = 0;       string tt =   ;       tt += ty;       tt +=    ;       while (k < str.size()) {         tt +=     ;         string t =   ;         k++;         while (k < str.size() && str[k] !=     ) t += str[k], k++;         tt += t;         z++;         if (k < str.size()) {           if (!m.count(tt)) {             m[tt] = ++ss;             par[m[tt]] = m[tt];           }           if (z > 1) {             int xx = root(m[tt]);             int yy = root(p);             if (xx != yy) {               par[xx] = yy;               ans[0][yy]++;             }           }           p = m[tt];         } else {           ans[1][root(p)]++;         }       }     }     int mx1 = 0, mx2 = 0;     for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) {       mx1 = max(mx1, ans[0][it->second]);       mx2 = max(mx2, ans[1][it->second]);     }     cout << mx1 <<     << mx2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const unsigned long long maxn = 2e6 + 10;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string v;     cin >> v;     for (long i = 0; i < v.size(); i++) {       if (v[i] !=  1  && v[i] !=  4 ) {         cout <<  NO  << endl;         return 0;       }     }     if (v[0] !=  1 ) {       cout <<  NO  << endl;       return 0;     }     unsigned long long p = 0;     for (long i = 1; i < v.size(); i++) {       if (v[i] ==  4  && v[i - 1] ==  4 ) {         if (v[i + 1] ==  4 ) {           cout <<  NO  << endl;           p = 1;           return 0;         }       }     }     if (p == 0) {       cout <<  YES  << endl;       return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100100;   int n, m;   int was[MAXN];   vector<int> g[MAXN];   bool was2[3][MAXN];   bool dfs(int v, int col = 1) {     was[v] = col;     for (int i = 0; i < (int)g[v].size(); ++i) {       if (!was[g[v][i]]) {         if (dfs(g[v][i], 3 - col)) {           return true;         }       } else if (was[v] == was[g[v][i]])         return true;     }     return false;   }   int dfs2(int v, int col) {     was2[col][v] = true;     int res = was[v] == col;     for (int i = 0; i < (int)g[v].size(); ++i) {       if (!was2[col][g[v][i]]) {         res += dfs2(g[v][i], col);       }     }     return res;   }   unsigned long long C(int n, int k) {     if (k > n) return 0;     unsigned long long res = 1;     for (int i = 0; i < k; ++i) res *= n - i;     for (int i = 2; i <= k; ++i) res /= i;     return res;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0, a, b; i < m; ++i) {       scanf( %d%d , &a, &b);       g[a].push_back(b);       g[b].push_back(a);     }     for (int i = 1; i <= n; ++i) {       if (!was[i] && dfs(i)) {         puts( 0 1 );         return 0;       }     }     unsigned long long one = 0;     for (int i = 1; i <= n; ++i) {       if (!was2[1][i]) {         one += C(dfs2(i, 1), 2);         one += C(dfs2(i, 2), 2);       }     }     if (one) {       cout << 1 <<     << one << endl;       return 0;     }     if (m) {       cout << 2 <<     << m * 1ll * (n - 2) <<   n ;       return 0;     }     cout << 3 <<     << C(n, 3) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int longpre(string y) {     string s = y +  ? ;     reverse(y.begin(), y.end());     s += y;     int n = s.length();     int lps[n];     for (int i = 0; i < n; i++) lps[i] = 0;     int i = 0;     int j = 1;     while (j < n) {       if (s[i] == s[j]) {         lps[j] = i + 1;         i++;         j++;       } else {         if (i == 0) {           lps[j] = 0;           j++;         } else {           i = lps[i - 1];         }       }     }     return lps[n - 1];   }   int main() {     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int k = 0;       int i = 0;       int n = s.length();       int j = n - 1;       while (i < j) {         if (s[i] == s[j])           k++;         else           break;         i++;         j--;       }       string t =   ;       if (k > 0) {         t += s.substr(0, k);       }       string mid = s.substr(k, n - 2 * k);       if (mid.length() > 0) {         int prefix = longpre(mid);         reverse(mid.begin(), mid.end());         int suffix = longpre(mid);         if (prefix > suffix) {           reverse(mid.begin(), mid.end());           t += mid.substr(0, prefix);         } else {           t += mid.substr(0, suffix);         }       }       t += s.substr(n - k, k);       cout << t << endl;     }   }


#include<bits/stdc++.h>   #define fort(i,a,b) for(long long i=a;i<=b;i++)   #define forn(i,a,b) for(long long i=a;i>=b;i--)   #define rep(i,a,b) for(long long i=a;i<b;i++)   #define x first   #define y second   #define pb push_back   #define MP make_pair   using namespace std;   const long long  N=2e5+5;   long long  s1,s2,d,c,test,n,l[N][20],f[N],kq[N],h[N],cnt[N],kt[N],timer,tin[N],tout[N];   vector<long long > a[N];   void dfs(long long  u,long long  pa)   {       tin[u]=++timer;       h[u]=h[pa]+1;       l[u][0]=pa;       cnt[u]++;       for(long long  v:a[u])           if (v!=pa)       {           dfs(v,u);           f[u]+=cnt[u]*cnt[v];           cnt[u]+=cnt[v];       }       f[u]+=(n-cnt[u])*cnt[u];       tout[u]=++timer;   }   void build_lca()   {       fort(i,1,log2(n))        fort(j,1,n)         l[j][i]=l[l[j][i-1]][i-1];   }   long long  lca(long long  u,long long  v)   {       if (h[u]<h[v]) swap(u,v);       forn(i,log2(h[u]),0)        if (h[l[u][i]]>=h[v]) u=l[u][i];       if (u==v) return u;       forn(i,log2(h[u]),0)        if (l[u][i]!=l[v][i])        {            u=l[u][i];            v=l[v][i];        }       return l[u][0];   }   void chinh(long long  x)   {       if (kt[x]) return;       kt[x]=1;       chinh(l[x][0]);   }   bool con(long v,long u)   {       return tin[u]<tin[v]&&tout[v]<tout[u];   }   int main()   {       ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);       cin>>test;       while (test--)       {           cin>>n;           fort(i,1,n-1)           {               long long  u,v;               cin>>u>>v;               u++;v++;               a[u].pb(v);               a[v].pb(u);           }           dfs(1,0);           build_lca();           kq[1]=n*(n-1)/2-f[1];           kq[n+1]=1;           fort(i,1,n)            if (a[i].size()>2) kq[n+1]=0;           d=1;c=1;s1=0;s2=0;           kt[1]=1;           fort(i,2,n)            if (!kt[i])            {                if (d!=c)                {                   if (c==1)                   {                       if (lca(i,d)!=1&&lca(i,d)!=d&&kt[lca(i,d)])                       {                           kq[i]=s2*cnt[d];                           break;                       }                   } else                   {                       if (lca(i,d)!=d&&lca(i,c)!=c)                       {                           kq[i]=cnt[d]*cnt[c];                           break;                       }                   }                   if (con(i,d))                       {                              chinh(i);                           s1=cnt[d]-cnt[i];                           kq[i]=s1*s2;                           d=i;                           s1=cnt[d];                       } else                   if (con(i,c))                       {                           chinh(i);                           if (c==1) s2-=cnt[i];else                               s2=cnt[c]-cnt[i];                           kq[i]=s1*s2;                           c=i;                           s2=cnt[c];                       }               } else               {                   chinh(i);                   for(long long  v:a[1])                       if (!kt[v]) s2+=cnt[v];                   s2++;s1=cnt[i];                   kq[i]=f[1]-cnt[i]*s2;                   d=i;               }           }           fort(i,1,n+1) cout<<kq[i]<<   ;cout<<  n ;           fort(i,1,n) a[i].clear();           fort(i,1,n+1) kq[i]=0;           fort(i,1,n) cnt[i]=0;           fort(i,1,n)            fort(j,0,log2(n)) l[i][j]=0;           fort(i,1,n) kt[i]=0;           fort(i,1,n) f[i]=0;           timer=0;       }   }


#include <bits/stdc++.h>   template <typename T>   void MACRO_VAR_Scan(T& t) {     std::cin >> t;   }   template <typename First, typename... Rest>   void MACRO_VAR_Scan(First& first, Rest&... rest) {     std::cin >> first;     MACRO_VAR_Scan(rest...);   }   template <typename T>   void MACRO_VEC_ROW_Init(int n, T& t) {     t.resize(n);   }   template <typename First, typename... Rest>   void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) {     first.resize(n);     MACRO_VEC_ROW_Init(n, rest...);   }   template <typename T>   void MACRO_VEC_ROW_Scan(int p, T& t) {     std::cin >> t[p];   }   template <typename First, typename... Rest>   void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) {     std::cin >> first[p];     MACRO_VEC_ROW_Scan(p, rest...);   }   template <class T>   inline T CHMAX(T& a, const T b) {     return a = (a < b) ? b : a;   }   template <class T>   inline T CHMIN(T& a, const T b) {     return a = (a > b) ? b : a;   }   template <class T>   std::vector<std::vector<T>> VV(int n, int m, T init = T()) {     return std::vector<std::vector<T>>(n, std::vector<T>(m, init));   }   template <typename S, typename T>   std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) {     os <<  (  << p.first <<  ,   << p.second <<  ) ;     return os;   }   using ll = long long;   using ull = unsigned long long;   using ld = long double;   using PAIR = std::pair<int, int>;   using PAIRLL = std::pair<ll, ll>;   constexpr int INFINT = 1 << 30;   constexpr int INFINT_LIM = (1LL << 31) - 1;   constexpr ll INFLL = 1LL << 60;   constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62);   constexpr double EPS = 1e-10;   constexpr int MOD = 998244353;   constexpr double PI = 3.141592653589793238462643383279;   template <class T, size_t N>   void FILL(T (&a)[N], const T& val) {     for (auto& x : a) x = val;   }   template <class ARY, size_t N, size_t M, class T>   void FILL(ARY (&a)[N][M], const T& val) {     for (auto& b : a) FILL(b, val);   }   template <class T>   void FILL(std::vector<T>& a, const T& val) {     for (auto& x : a) x = val;   }   template <class ARY, class T>   void FILL(std::vector<std::vector<ARY>>& a, const T& val) {     for (auto& b : a) FILL(b, val);   }   bool g[20][20];   bool h[20][20];   bool dp[16][1 << 16][16];   signed main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     ;     int n, m;     MACRO_VAR_Scan(n, m);     ;     std::vector<std::vector<int>> a(n, std::vector<int>(m));     for (auto& R : a)       for (auto& w : R) std::cin >> w;     ;     auto check = [&](int d) {       FILL(g, true);       FILL(h, true);       FILL(dp, false);       for (int i = 0; i < int(n); ++i)         for (int j = 0; j < int(n); ++j) {           for (int k = 0; k < int(m); ++k) {             if (std::abs(a[i][k] - a[j][k]) < d) {               g[i][j] = false;               break;             }           }           for (int k = 0; k < int(m - 1); ++k) {             if (std::abs(a[i][k] - a[j][k + 1]) < d) {               h[i][j] = false;               break;             }           }         }       for (int root = 0; root < int(n); ++root) {         dp[root][1 << root][root] = true;         for (int mask = 0; mask < int(1 << n); ++mask)           for (int i = 0; i < int(n); ++i) {             if (!dp[root][mask][i]) continue;             if (!(mask & 1 << i)) continue;             for (int j = 0; j < int(n); ++j) {               if (!g[i][j]) continue;               if (mask & 1 << j) continue;               dp[root][mask | 1 << j][j] = true;             }           }         for (int i = 0; i < int(n); ++i)           if (n == 1 || i != root) {             if (dp[root][(1 << n) - 1][i] && h[i][root]) return true;           }       }       return false;     };     int ng = 1000000009, ok = 0;     while (ng - ok > 1) {       int me = (ok + ng) / 2;       if (check(me))         ok = me;       else         ng = me;     }     std::cout << (ok);     std::cout <<   n ;     ;     return 0;   }


#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];   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++) {           prime[j]++;         }       }     }   }   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);     int n;     cin >> n;     long long int a[n];     vector<pair<long long int, int> > v;     for (int i = 0; i < n; i++) {       cin >> a[i];       v.push_back({a[i], i + 1});     }     sort(v.begin(), v.end());     if (n == 2 || n == 3) {       cout <<  1 n ;       return 0;     }     map<long long int, long long int> mp;     for (int i = 0; i < n - 1; i++) {       mp[v[i + 1].first - v[i].first]++;     }     if (mp.size() > 3) {       cout <<  -1 n ;       return 0;     }     if (mp.size() == 1) {       cout << v[0].second <<   n ;       return 0;     }     if (n == 4) {       if (v[1].first + v[3].first == 2 * v[2].first) {         cout << v[0].second <<   n ;         return 0;       }       if (v[0].first + v[3].first == 2 * v[2].first) {         cout << v[1].second <<   n ;         return 0;       }       if (v[0].first + v[3].first == 2 * v[1].first) {         cout << v[2].second <<   n ;         return 0;       }       if (v[0].first + v[2].first == 2 * v[1].first) {         cout << v[3].second <<   n ;         return 0;       }       cout <<  -1 n ;       return 0;     }     for (int i = 0; i < n; i++) {       if (i == 0) {         mp[v[i + 1].first - v[i].first]--;         if (mp[v[i + 1].first - v[i].first] == 0) {           mp.erase(v[i + 1].first - v[i].first);         }         if (mp.size() == 1) {           cout << v[0].second <<   n ;           return 0;         }         mp[v[i + 1].first - v[i].first]++;       } else if (i == n - 1) {         mp[v[i].first - v[i - 1].first]--;         if (mp[v[i].first - v[i - 1].first] == 0) {           mp.erase(v[i].first - v[i - 1].first);         }         if (mp.size() == 1) {           cout << v[n - 1].second <<   n ;           return 0;         }         mp[v[i].first - v[i - 1].first]++;       } else {         mp[v[i + 1].first - v[i].first]--;         mp[v[i].first - v[i - 1].first]--;         if (mp[v[i + 1].first - v[i].first] == 0) {           mp.erase(v[i + 1].first - v[i].first);         }         if (mp[v[i].first - v[i - 1].first] == 0) {           mp.erase(v[i].first - v[i - 1].first);         }         mp[v[i + 1].first - v[i - 1].first]++;         if (mp.size() == 1) {           cout << v[i].second <<   n ;           return 0;         }         mp[v[i + 1].first - v[i - 1].first]--;         if (mp[v[i + 1].first - v[i - 1].first] == 0) {           mp.erase(v[i + 1].first - v[i - 1].first);         }         mp[v[i + 1].first - v[i].first]++;         mp[v[i].first - v[i - 1].first]++;       }     }     cout <<  -1 n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int Q;     cin >> Q;     while (Q-- > 0) {       int n;       cin >> n;       vector<int> dp(2001, inf);       dp[0] = 0;       for (int i = 0; i < n; ++i) {         int a;         cin >> a;         vector<int> aux(dp);         fill(dp.begin(), dp.end(), inf);         for (int j = 0; j < 2001; ++j) {           if (aux[j] != inf) {             dp[max(0, j - a)] = min(dp[max(0, j - a)], aux[j] + a);             if (j + a < 2001) {               dp[j + a] = min(dp[j + a], max(0, aux[j] - a));             }           }         }       }       int res = inf;       for (int i = 0; i <= 2000; ++i) {         if (dp[i] != inf) {           res = min(res, i + dp[i]);         }       }       cout << res <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long x[100007], y[100007], xx, yy, m, p, q;   long long ans;   int get(int p, int q) {     while ((x[q] - x[p]) * (yy - y[p]) - (y[q] - y[p]) * (xx - x[p]) <= 0)       q = q % n + 1;     q = (q + n - 2) % n + 1;     return q;   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) scanf( %I64d%I64d , &x[i], &y[i]);     for (cin >> m; m; m--) {       cin >> xx >> yy;       int now = 0;       ans = 0;       for (int i = 1; i <= n; i++)         if ((xx - x[i]) * (y[i % n + 1] - y[i]) -                 (yy - y[i]) * (x[i % n + 1] - x[i]) <             0)           now++;       if (now != 0 && now != n) {         cout << 0 << endl;         continue;       }       for (p = 1, q = get(1, 1); p <= n; p++, q = get(p, q)) {         int r = ((q - p) % n + n) % n;         ans += ((long long)r) * (r - 1) / 2;       }       ans = ((long long)n * (n - 1) * (n - 2) / 6 - ans);       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 100010, Block = 500, TB = 300;   struct query {     int l, r, id;     bool operator<(const query &a) const {       if (l / TB != a.l / TB) return l / TB < a.l / TB;       return r < a.r;     }   } q[MaxN];   long long ans[MaxN];   int cnt[MaxN], num[MaxN], a[MaxN];   vector<int> large;   bool vis[MaxN];   int n, m;   int tmp[MaxN];   long long get_ans() {     for (int i = 1; i <= Block * 2; ++i) tmp[i] = num[i];     long long ret = 0;     int x = 1;     for (int i = 1; i <= Block; ++i) {       if (!tmp[i]) continue;       if (tmp[i] >= 2) {         ret += (tmp[i] / 2) * i * 2;         tmp[i * 2] += tmp[i] / 2;         tmp[i] &= 1;       }       if (tmp[i] & 1) {         x = max(i + 1, x);         while (tmp[x] == 0 && i + x <= Block * 2) ++x;         if (i + x > Block * 2) break;         ret += i + x;         ++tmp[i + x];         --tmp[x];       }       tmp[i] = 0;     }     priority_queue<int, vector<int>, greater<int> > Q;     for (int i = 1; i <= Block * 2; ++i)       for (int j = 1; j <= tmp[i]; ++j) Q.push(i);     for (auto i : large)       if (cnt[i] > Block * 2) Q.push(cnt[i]);     while (Q.size() >= 2) {       int x = Q.top();       Q.pop();       int y = Q.top();       Q.pop();       ret += x + y;       Q.push(x + y);     }     return ret;   }   void reverse(int x) {     int u = a[x];     if (vis[x]) {       --num[cnt[u]];       --cnt[u];       ++num[cnt[u]];     } else {       --num[cnt[u]];       ++cnt[u];       ++num[cnt[u]];     }     vis[x] ^= 1;   }   void move(int &x, int p) {     while (x > p) {       reverse(x);       --x;     }     while (x < p) {       ++x;       reverse(x);     }   }   int main() {     scanf( %d , &n);     int mx = 0;     for (int i = 1; i <= n; ++i) {       scanf( %d , a + i);       ++cnt[a[i]];       mx = max(mx, a[i]);     }     for (int i = 1; i <= mx; ++i)       if (cnt[i] > Block * 2) large.push_back(i);     memset(cnt, 0, sizeof(cnt));     scanf( %d , &m);     for (int i = 1; i <= m; ++i) {       scanf( %d%d , &q[i].l, &q[i].r);       q[i].id = i;     }     sort(q + 1, q + m + 1);     int l = 0, r = 0;     for (int i = 1; i <= m; ++i) {       move(l, q[i].l - 1);       move(r, q[i].r);       if (q[i].l == q[i - 1].l && q[i].r == q[i - 1].r)         ans[q[i].id] = ans[q[i - 1].id];       else         ans[q[i].id] = get_ans();     }     for (int i = 1; i <= m; ++i) printf( %I64d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, size[55];   double f[55][55], fac[55];   int cnt, head[55], to[110], nxt[110];   void add(int x, int y) {     cnt++;     to[cnt] = y;     nxt[cnt] = head[x];     head[x] = cnt;   }   double c(int n, int m) {     if (m < 0 || m > n) {       return 0.0;     } else {       return fac[n] / fac[m] / fac[n - m];     }   }   void dfs(int x, int fa) {     size[x] = 1;     f[x][0] = 1.0;     double tmp[55];     for (int i = head[x]; i; i = nxt[i]) {       int y = to[i];       if (y == fa) {         continue;       }       dfs(y, x);       for (int j = 0; j < size[x] + size[y]; j++) {         tmp[j] = 0.0;       }       for (int j = 0; j < size[y]; j++) {         for (int k = 0; k < size[x]; k++) {           for (int h = j + k + 1; h <= size[y] + k; h++) {             tmp[h] += c(h, k) * c(size[x] + size[y] - 1 - h, size[x] - 1 - k) *                       f[x][k] * f[y][j] / 2.0;           }           tmp[j + k] += 1.0 * (size[y] - j) * c(j + k, j) *                         c(size[x] + size[y] - 1 - j - k, size[x] - 1 - k) *                         f[x][k] * f[y][j];         }       }       size[x] += size[y];       for (int j = 0; j < size[x]; j++) {         f[x][j] = tmp[j];       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       add(x, y);       add(y, x);     }     fac[0] = 1.0;     for (int i = 1; i <= n; i++) {       fac[i] = fac[i - 1] * i;     }     for (int i = 1; i <= n; i++) {       dfs(i, 0);       printf( %.10lf n , f[i][n - 1] / fac[n - 1]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const int SIZE = 1e6 + 5;   const long long INF = 1LL << 58;   const double eps = 1e-10;   char d[2009][2009];   char a[2009][2009];   int dy[4] = {-1, 0, 1, 0};   int dx[4] = {0, 1, 0, -1};   int dy2[8] = {-2, -1, 0, 1, 2, 1, 0, -1};   int dx2[8] = {0, 1, 2, 1, 0, -1, -2, -1};   char v2[5] =  ^>v< ;   char v[5] =  v<^> ;   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < (n); ++i) {       scanf( %s , (d[i]));       for (int j = 0; j < (m); ++j) {         if (d[i][j] ==  * )           a[i][j] =  * ;         else           a[i][j] =  . ;       }     }     queue<pair<int, int> > q;     for (int i = 0; i < (n); ++i) {       for (int j = 0; j < (m); ++j) {         if (d[i][j] ==  * ) continue;         q.push(make_pair(i, j));       }     }     while (((int)(q).size())) {       pair<int, int> cur = q.front();       q.pop();       int y = cur.first, x = cur.second;       if (a[y][x] !=  . ) continue;       int ck = -1;       for (int k = 0; k < (4); ++k) {         int ny = y + dy[k], nx = x + dx[k];         if (ny < 0 || ny >= n || nx < 0 || nx >= m) continue;         if (a[ny][nx] ==  . ) {           if (ck != -1) {             ck = -1;             break;           } else             ck = k;         }       }       if (ck == -1) continue;       int ny = y + dy[ck], nx = x + dx[ck];       a[y][x] = v[ck];       a[ny][nx] = v2[ck];       for (int k = 0; k < (8); ++k) {         int ny = y + dy2[k], nx = x + dx2[k];         if (ny < 0 || ny >= n || nx < 0 || nx >= m) continue;         q.push(make_pair(ny, nx));       }     }     for (int i = 0; i < (n); ++i) {       for (int j = 0; j < (m); ++j) {         if (a[i][j] ==  . ) {           puts( Not unique );           return 0;         }       }     }     for (int i = 0; i < (n); ++i) {       for (int j = 0; j < (m); ++j) {         printf( %c , a[i][j]);       }       puts(  );     }   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > hop, top;   int h, t, R;   struct State {     int h, t;     int c;     State() {}     State(int h, int t, int c) : h(h), t(t), c(c) {}     bool operator<(const State o) const {       if (h != o.h) return h < o.h;       return t < o.t;     }   };   vector<vector<int> > G;   pair<int, int> BFS(State init) {     queue<State> q;     q.push(init);     map<State, int> vis;     int ii = 0;     vis[init] = 0;     G.resize(1);     while (q.size()) {       State& s = q.front();       State ns;       for (int i = 0; i < min((int)hop.size(), s.h); i++) {         ns = State(s.h - i - 1 + hop[i].first, s.t + hop[i].second, s.c + 1);         if (ns.h == ns.t && ns.h == 0) {           return make_pair(ns.c, 1);         }         if (ns.h + ns.t > R) {           if (!vis.count(ns)) {             vis[ns] = ++ii;             G.resize(ii + 1);             G[vis[s]].push_back(ii);           }           continue;         }         if (!vis.count(ns)) {           q.push(ns);           vis[ns] = ++ii;           G.resize(ii + 1);           G[vis[s]].push_back(ii);         } else {           G[vis[s]].push_back(vis[ns]);         }       }       for (int i = 0; i < min((int)top.size(), s.t); i++) {         ns = State(s.h + top[i].first, s.t + top[i].second - i - 1, s.c + 1);         if (ns.h == ns.t && ns.h == 0) {           return make_pair(ns.c, 1);         }         if (ns.h + ns.t > R) {           if (!vis.count(ns)) {             vis[ns] = ++ii;             G.resize(ii + 1);             G[vis[s]].push_back(ii);           }           continue;         }         if (!vis.count(ns)) {           q.push(ns);           vis[ns] = ++ii;           G.resize(ii + 1);           G[vis[s]].push_back(ii);         } else {           G[vis[s]].push_back(vis[ns]);         }       }       q.pop();     }     return make_pair(0, -1);   }   enum colors { WHITE, GREY, BLACK };   vector<int> color;   bool hasLoop;   void DFS(int node) {     if (hasLoop) return;     color[node] = GREY;     for (int i = 0; i < G[node].size(); i++) {       if (hasLoop) return;       if (color[G[node][i]] == WHITE) {         DFS(G[node][i]);       } else if (color[G[node][i]] == GREY) {         hasLoop = 1;         return;       }     }     color[node] = BLACK;   }   void Input() {     cin >> h >> t >> R;     int hs, ts;     cin >> hs;     hop.resize(hs);     for (int i = 0; i < hs; i++) {       cin >> hop[i].first >> hop[i].second;     }     cin >> ts;     top.resize(ts);     for (int i = 0; i < ts; i++) {       cin >> top[i].first >> top[i].second;     }   }   vector<int> dp;   int recurse(int node) {     if (dp[node] != -1) return dp[node];     int mx = 0;     for (int i = 0; i < G[node].size(); i++) {       mx = max(mx, recurse(G[node][i]));     }     return dp[node] = mx + 1;   }   void Solve() {     Input();     State init(h, t, 0);     auto ret = BFS(init);     if (ret.second == 1) {       cout <<  Ivan  << endl;       cout << ret.first;       return;     }     color.resize(G.size());     DFS(0);     if (hasLoop) {       cout <<  Draw ;       return;     }     dp.assign(G.size(), -1);     cout <<  Zmey  << endl << recurse(0) - 1;   }   int main() { Solve(); }


#include <bits/stdc++.h>   using namespace std;   int N;   int K[1010];   pair<int, int> nex[1010][2520];   int P, Q;   int num[1010][2520];   int vis[1010][2520];   class SCC {    public:     static const int MV = 1010 * 2520;     vector<vector<int> > SC;     int NV, GR[MV];       private:     vector<int> E[MV], RE[MV], NUM;     int vis[MV];       public:     void init(int NV) {       this->NV = NV;       for (int i = 0; i < MV; i++) {         E[i].clear();         RE[i].clear();       }     }     void add_edge(int x, int y) {       E[x].push_back(y);       RE[y].push_back(x);     }     void dfs(int cu) {       vis[cu] = 1;       for (int i = 0; i < E[cu].size(); i++)         if (!vis[E[cu][i]]) dfs(E[cu][i]);       NUM.push_back(cu);     }     void revdfs(int cu, int ind) {       int i;       vis[cu] = 1;       GR[cu] = ind;       SC[ind].push_back(cu);       for (i = 0; i < (RE[cu].size()); i++)         if (!vis[RE[cu][i]]) revdfs(RE[cu][i], ind);     }     void scc() {       int c = 0;       SC.clear();       SC.resize(MV);       NUM.clear();       memset(vis, 0, sizeof(vis));       for (int i = 0; i < NV; i++)         if (!vis[i]) dfs(i);       memset(vis, 0, sizeof(vis));       for (int i = NUM.size() - 1; i >= 0; i--)         if (!vis[NUM[i]]) {           SC[c].clear();           revdfs(NUM[i], c);           sort(SC[c].begin(), SC[c].end());           c++;         }       SC.resize(c);     }   };   int dfs(int v, int val, vector<int>& V) {     if (vis[v][val] == 2) {       set<int> S;       int i;       for (i = V.size() - 1; i >= 0; i--) {         S.insert(V[i] / 2520);         if (V[i] == v * 2520 + val) break;       }       num[v][val] = S.size();     } else if (vis[v][val] == 0) {       vis[v][val] = 2;       V.push_back(v * 2520 + val);       num[v][val] = dfs(nex[v][val].first, nex[v][val].second, V);       V.pop_back();     }     vis[v][val] = 1;     return num[v][val];   }   void solve() {     int i, j, k, l, r, x, y;     string s;     cin >> N;     for (i = 0; i < (N); i++) {       cin >> K[i];       K[i] = (K[i] % 2520 + 2520) % 2520;     }     for (i = 0; i < (N); i++) {       vector<int> E;       cin >> x;       E.resize(x);       for (j = 0; j < (x); j++) cin >> E[j];       for (j = 0; j < (2520); j++) {         nex[i][j].first = E[(j + K[i]) % x] - 1;         nex[i][j].second = (j + K[i]) % 2520;       }     }     for (i = 0; i < (N); i++)       for (j = 0; j < (2520); j++)         if (vis[i][j] == 0) {           vector<int> V;           dfs(i, j, V);         }     cin >> Q;     while (Q--) {       cin >> x >> y;       x--;       y = (y % 2520 + 2520) % 2520;       cout << num[x][y] << endl;     }   }   int main(int argc, char** argv) {     string s;     int i;     if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);     for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ;     for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   void ingraph(vector<long long> graph[], long long m) {     long long x, y;     for (long long i = 0; i < m; i++) {       cin >> x >> y;       x--, y--;       graph[x].push_back(y);       graph[y].push_back(x);     }   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return a * b / gcd(a, b); }   long long modmulti(long long a, long long b) {     return ((a % mod) * 1ll * (b % mod)) % mod;   }   long long modadd(long long a, long long b) {     long long asdfgh = ((a % mod) + (b % mod) + mod + mod) % mod;     asdfgh = (asdfgh + mod) % mod;     return asdfgh;   }   long long modpower(long long a, long long n) {     if (n == 0) return 1;     if (n == 1) return a % mod;     long long b = modpower(a, n / 2);     b = modmulti(b, b);     if (n % 2 == 0) return b;     return modmulti(a, b);   }   long long modinv(long long a) { return modpower(a, mod - 2); }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long sum = 0;       vector<long long> vec;       for (long long i = 1; i <= n; i = i * 2) {         vec.push_back(i);         n = n - i;       }       if (n > 0) {         vec.push_back(n);       }       sort(vec.begin(), vec.end());       cout << (long long)(vec).size() - 1 <<   n ;       for (long long i = 1; i < (long long)(vec).size(); i++)         cout << vec[i] - vec[i - 1] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int n = s.length();       if (n < 3) {         cout << 0 << endl;         continue;       }       if (n == 3) {         if (s ==  101  || s ==  010 )           cout << 1 << endl;         else           cout << 0 << endl;         continue;       }       long long int zero = 0, one = 0;       for (long long int i = 0; i < n; i++) {         if (s[i] ==  1 )           one++;         else           zero++;       }       if (one == n || zero == n) {         cout << 0 << endl;         continue;       }       long long int done = 0, dzero = 0;       long long int min1 = INT_MAX, min2 = INT_MAX;       for (long long int i = 0; i < n; i++) {         if (s[i] ==  1 )           done++;         else           dzero++;         min1 = min(min1, (dzero + one - done));         min2 = min(min2, (done + zero - dzero));       }       cout << min(min1, min2) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, neg = 0;   char s[1009];   int main() {     vector<int> ans;     scanf( %[^=]=%d , s, &n);     ans.push_back(n);     for (int i = 1; s[i]; i++) {       if (s[i] ==    ) continue;       if (s[i] ==  - )         neg = 1;       else if (s[i] ==  + )         neg = 0;       else if (s[i] ==  ?  && !neg) {         ans[0] -= 1;         ans.push_back(1);       } else if (s[i] ==  ? ) {         int c = 0;         ans[0] += 1;         ans.push_back(-1);       }     }     int c = 1;     while (ans[0] > n && c < ans.size()) {       if (ans[c] > 0 && ans[c] < n) {         ans[0]--;         ans[c]++;       } else         c++;     }     c = 1;     while (ans[0] <= 0 && c < ans.size()) {       if (ans[c] < 0 && abs(ans[c]) < n) {         ans[0]++;         ans[c]--;       } else         c++;     }     if (ans[0] <= 0 || ans[0] > n) {       printf( Impossible n );       return 0;     }     printf( Possible n );     printf( %d , ans[0]);     for (int i = 1; i < ans.size(); i++) {       if (ans[i] > 0)         printf(  + %d , ans[i]);       else         printf(  - %d , abs(ans[i]));     }     printf(  = %d n , n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int x, int y) {     if (y == 0) return x;     return gcd(y, x % y);   }   int main() {     int n;     cin >> n;     int ansx = 0, ansy = 0;     for (int i = 1; i < n; i++) {       int j = n - i;       if (j <= i) break;       if (gcd(i, j) != 1) continue;       ansx = i;       ansy = j;     }     cout << ansx <<     << ansy << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f, N = 1005, MOD = 100000007;   const double eps = 1e-6;   int a[15], m, ans[N], ct;   char b;   int main() {     ct = 0;     for (int i = 1; i <= 10; i++) {       scanf( %c , &b);       if (b ==  1 ) {         a[ct++] = i;       }     }     scanf( %d , &m);     bool flag = 0;     int s[2], tmp;     int i, j, tail;     for (int k = 0; k < ct; k++) {       s[0] = s[1] = 0;       s[0] = ans[0] = a[k];       tail = k;       tmp = 1;       for (i = 1; i < m; i++) {         for (j = 0; j < ct; j++) {           if (j == tail) continue;           if (s[tmp] + a[j] > s[tmp ^ 1]) {             s[tmp] += a[j];             ans[i] = a[j];             tail = j;             tmp ^= 1;             break;           }         }         if (j == ct) {           flag = 0;           break;         }       }       if (i == m) {         flag = 1;         break;       }     }     if (flag) {       printf( YES n );       for (int k = 0; k < m; k++) {         printf( %d  , ans[k]);       }       puts(  );     } else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e17;   long long n, k, niv[200005];   vector<long long> muchii[200005];   bool isleaf[200005], used[200005];   long long red, blue, par[200005];   vector<long long> line;   long long timp, in[200005], out[200005];   struct date {     long long minim, best;   } arb[1400005];   long long lazy[1400005];   void dfs(int nod) {     if (nod == 1)       niv[nod] = 1;     else if (muchii[nod].size() == 1)       isleaf[nod] = 1;     timp++;     line.push_back(nod);     in[nod] = timp;     for (auto i : muchii[nod])       if (i != par[nod]) {         niv[i] = niv[nod] + 1;         par[i] = nod;         dfs(i);       }     out[nod] = timp;   }   void prop(int nod) {     long long val = lazy[nod];     arb[nod * 2].minim += val;     arb[nod * 2 + 1].minim += val;     lazy[nod * 2] += val;     lazy[nod * 2 + 1] += val;   }   void update(int nod, int st, int dr, int a, int b, long long val) {     if (lazy[nod] != 0 && st != dr) prop(nod);     lazy[nod] = 0;     if (st >= a && dr <= b) {       arb[nod].minim += val;       if (st == dr && isleaf[line[st]])         arb[nod].best = line[st];       else         lazy[nod] += val;       return;     }     int mij = (st + dr) / 2;     if (a <= mij) update(nod * 2, st, mij, a, b, val);     if (b > mij) update(nod * 2 + 1, mij + 1, dr, a, b, val);     arb[nod].minim = arb[nod].best = 0;     if (arb[nod * 2].best > 0 && arb[nod * 2].minim > arb[nod].minim)       arb[nod] = arb[nod * 2];     if (arb[nod * 2 + 1].best > 0 && arb[nod * 2 + 1].minim > arb[nod].minim)       arb[nod] = arb[nod * 2 + 1];   }   int main() {     cin >> n >> k;     for (int i = 1; i < n; i++) {       int a, b;       cin >> a >> b;       muchii[a].push_back(b);       muchii[b].push_back(a);     }     line.push_back(0);     dfs(1);     for (int i = 1; i <= n; i++) update(1, 1, n, in[i], in[i], niv[i]);     long long ans = -INF;     blue = n;     red = 0;     while (red <= k) {       long long val = red * (n - red);       long long x = min(n / 2, blue);       val -= x * (n - x);       ans = max(ans, val);       long long nod = arb[1].best;       red++;       while (nod > 0 && !used[nod]) {         used[nod] = 1;         update(1, 1, n, in[nod], out[nod], -1);         blue--;         nod = par[nod];       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int g[65]{0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,  4,  5,  5,  5,  5, 5, 5,             6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7,  7,  8,  8,  8,  8, 8, 8,             8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10};   const int MAXN = 1000007;   int s[MAXN];   map<pair<int, long long>, int> dp;   int grundy(pair<int, long long> s) {     if (dp.find(s) != dp.end()) return dp[s];     long long mask = s.second;     int &res = dp[s];     set<int> S;     for (int i = 1; i <= 60; i++) {       if (!(mask & (1ll << i)) && s.first >= i) {         S.insert(grundy(make_pair(s.first - i, (mask | (1ll << i)))));       }     }     while (S.count(res) > 0) res++;     return res;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &s[i]);     }     if (n == 1)       printf( NO n );     else {       int XOR = 0;       for (int i = 0; i < n; i++) {         XOR ^= grundy(make_pair(s[i], 0));       }       if (XOR == 0)         printf( YES n );       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void cline() { cout <<   n ; }   template <typename T, typename... V>   void cline(T t, V... v) {     cout << t;     if (sizeof...(v)) cout <<    ;     cline(v...);   }   void cspc() { cout <<    ; }   template <typename T, typename... V>   void cspc(T t, V... v) {     cout << t;     if (sizeof...(v)) cout <<    ;     cspc(v...);   }   int n;   int a[105];   int dp[104][52][52][2];   int calc(int indx, int even, int odd, int pre) {     if (indx == n) return 0;     if (dp[indx][even][odd][pre] != -1) return dp[indx][even][odd][pre];     if (indx == 0) {       if (a[indx] == 0) {         if (even == 0) {           return dp[indx][even][odd][pre] = calc(indx + 1, even, odd - 1, 1);         } else if (odd == 0) {           return dp[indx][even][odd][pre] = calc(indx + 1, even - 1, odd, 0);         } else {           return dp[indx][even][odd][pre] = min(calc(indx + 1, even, odd - 1, 1),                                                 calc(indx + 1, even - 1, odd, 0));         }       } else {         return dp[indx][even][odd][pre] = calc(indx + 1, even, odd, a[indx] % 2);       }     } else {       if (a[indx] == 0) {         if (even == 0) {           if (pre % 2 == 0) {             return dp[indx][even][odd][pre] =                        calc(indx + 1, even, odd - 1, 1) + 1;           } else {             return dp[indx][even][odd][pre] = calc(indx + 1, even, odd - 1, 1);           }         } else if (odd == 0) {           if (pre % 2 == 1) {             return dp[indx][even][odd][pre] =                        calc(indx + 1, even - 1, odd, 0) + 1;           } else {             return dp[indx][even][odd][pre] = calc(indx + 1, even - 1, odd, 0);           }         } else {           if (pre % 2 != 0) {             return dp[indx][even][odd][pre] =                        min(calc(indx + 1, even, odd - 1, 1),                            calc(indx + 1, even - 1, odd, 0) + 1);           } else {             return dp[indx][even][odd][pre] =                        min(calc(indx + 1, even, odd - 1, 1) + 1,                            calc(indx + 1, even - 1, odd, 0));           }         }       } else {         if (a[indx] % 2 != pre % 2) {           return dp[indx][even][odd][pre] =                      calc(indx + 1, even, odd, a[indx] % 2) + 1;         } else {           return dp[indx][even][odd][pre] =                      calc(indx + 1, even, odd, a[indx] % 2);         }       }     }   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     int even = n / 2, odd = (n + 1) / 2;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] % 2 == 0)         even--;       else         odd--;       if (a[i] == 0) even++;     }     for (int in1 = 0; in1 < 104; in1++) {       for (int in2 = 0; in2 < 52; in2++) {         for (int in3 = 0; in3 < 52; in3++) {           dp[in1][in2][in3][0] = -1;           dp[in1][in2][in3][1] = -1;         }       }     }     cspc(calc(0, even, odd, 0));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 3;   int n, m, dp[N], vis[2][N];   vector<int> adj[2][N];   vector<pair<int, int>> e;   void dfs(int u, int dir) {     vis[dir][u] = 1;     for (int v : adj[dir][u])       if (!vis[dir][v]) dfs(v, dir);   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> m;     for (int i = 0, u, v; i < m; i++) {       cin >> u >> v;       adj[0][u].push_back(v);       adj[1][v].push_back(u);       e.push_back({u, v});     }     dfs(1, 0), dfs(n, 1);     for (int i = 0; i < n - 1; i++)       for (auto [u, v] : e)         if (vis[0][u] & vis[1][v]) {           dp[u] = min(dp[u], dp[v] + 2);           dp[v] = min(dp[v], dp[u] - 1);         }     for (auto [u, v] : e)       if (vis[0][u] & vis[1][v] & (dp[u] > dp[v] + 2 || dp[v] > dp[u] - 1))         return cout <<  No  << endl, 0;     cout <<  Yes  << endl;     for (auto [u, v] : e)       cout << (vis[0][u] & vis[1][v] ? dp[u] - dp[v] : 1) << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e6 + 10;   long long a[maxn], b[maxn], ans[maxn];   int32_t main() {     long long n;     cin >> n;     for (long long i = 0; i < n; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(b, b + n);     if (b[0] == b[n - 1]) {       if (b[0] == 0) {         cout <<  YES  <<   n ;         for (long long i = 0; i < n; i++) cout <<  1  ;       } else         cout <<  NO ;       exit(0);     }     long long pos = -1, mx = b[n - 1];     for (long long i = 0; i < n; i++) {       if (a[i] == mx && a[(i - 1 + n) % n] != mx) pos = i;     }     ans[pos] = a[pos];     bool bad = 1;     for (long long i = (pos - 1 + n) % n; i != pos; i = (i - 1 + n) % n) {       if (a[i] == 0 && bad)         ans[i] = ans[(i + 1) % n] * 2LL, bad = 0;       else         ans[i] = ans[(i + 1) % n] + a[i];     }     cout <<  YES  <<   n ;     for (long long i = 0; i < n; i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 105;   long long l, r, dp[N];   long long gao(long long n) {     long long m = n, ans = 0;     int bit[N], len = 0;     while (n) {       bit[++len] = n % 2;       n /= 2;     }     for (int i = 2; i < len; i++) {       memset(dp, 0, sizeof(dp));       dp[1] = 1;       for (int j = 2; j < i; j++) {         if (i % j) continue;         dp[j] += 1LL << (j - 1);       }       for (int j = 1; j <= i; j++) {         for (int k = 2; k * j < i; k++)           if (i % (k * j) == 0) dp[j * k] -= dp[j];       }       for (int j = 1; j <= i; j++) ans += dp[j];     }     for (int i = len; i <= len; i++) {       memset(dp, 0, sizeof(dp));       for (int j = 1; j < i; j++) {         if (i % j) continue;         for (int k = len; k > len - j; k--) {           if (k != len && bit[k] == 1) {             dp[j] += 1LL << (k - (len - j) - 1);           }         }         long long sum = 0;         for (int k = len; k > len - j; k--) {           if (bit[k] == 0) continue;           for (int r = k; r > 0; r -= j) {             sum += 1LL << (r - 1);           }         }         if (sum <= m) dp[j]++;       }       for (int j = 1; j <= i; j++) {         for (int k = 2; k * j < i; k++)           if (i % (k * j) == 0) dp[j * k] -= dp[j];       }       for (int j = 1; j <= i; j++) ans += dp[j];     }     return ans;   }   int main() {     while (cin >> l >> r) {       cout << gao(r) - gao(l - 1) << endl;     }     return 0;   }


#include <bits/stdc++.h>   int setBit(int N, int pos) { return N = N | (1 << pos); }   int resetBit(int N, int pos) { return N = N & ~(1 << pos); }   bool checkBit(int N, int pos) { return (bool)(N & (1 << pos)); }   using namespace std;   const int maxn = 100000;   int n, m, cost[maxn + 5], okayRoad[maxn + 5], par[maxn + 5];   vector<int> graph[maxn + 5], flag[maxn + 5];   map<pair<int, int>, int> mapp, out;   map<pair<int, int>, int>::iterator it;   void bfs() {     queue<int> q;     int i, u, v;     memset(cost, 63, sizeof(cost));     ;     cost[1] = 0;     par[1] = 1;     q.push(1);     while (!q.empty()) {       u = q.front();       q.pop();       int siz = (int)graph[u].size();       for (i = 0; i < siz; i++) {         v = graph[u][i];         if (cost[u] + 1 < cost[v]) {           cost[v] = cost[u] + 1;           okayRoad[v] = okayRoad[u] + flag[u][i];           par[v] = u;           q.push(v);         } else if (cost[u] + 1 == cost[v] &&                    okayRoad[u] + flag[u][i] > okayRoad[v]) {           okayRoad[v] = okayRoad[u] + flag[u][i];           par[v] = u;           q.push(v);         }       }     }   }   void giveOutput() {     int u, v = n;     vector<int> path;     path.push_back(n);     while (v != par[v]) {       v = par[v];       path.push_back(v);     }     int i, j, siz = (int)path.size();     for (i = 1; i < siz; i++) {       u = path[i - 1];       v = path[i];       if (u > v) swap(u, v);       if (mapp[pair<int, int>(u, v)] == 0) out[pair<int, int>(u, v)] = 1;       mapp[pair<int, int>(u, v)] = -1;     }     for (__typeof((mapp).begin()) it = (mapp).begin(); it != (mapp).end(); it++) {       if (it->second == 1) out[it->first] = 0;     }     printf( %d n , (int)out.size());     for (__typeof((out).begin()) it = (out).begin(); it != (out).end(); it++) {       cout << it->first.first <<     << it->first.second <<     << it->second            <<   n ;     }   }   int main() {     int i, u, v, w;     scanf( %d %d , &n, &m);     for (i = 0; i < m; i++) {       scanf( %d %d %d , &u, &v, &w);       if (u > v) swap(u, v);       graph[u].push_back(v);       graph[v].push_back(u);       flag[u].push_back(w);       flag[v].push_back(w);       mapp[pair<int, int>(u, v)] = w;     }     bfs();     giveOutput();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[301][301], h, w, dp[301][301];   int used[601][601];   int dx[] = {0, 1, -1, 0, 0, 1, -1, 1, -1};   int dy[] = {0, 0, 0, 1, -1, 1, 1, -1, -1};   int read() {     int x;     scanf( %d , &x);     return x;   }   bool check(int x, int y) {     if (x >= 1 && x <= h && y >= 1 && y <= w && !used[x][y])       return true;     else       return false;   }   void dfs(int x, int y, int component) {     used[x][y] = component;     for (int i = 1; i <= 8; i++) {       int kx = dx[i] + x, ky = dy[i] + y;       if (check(kx, ky) && a[x][y] == a[kx][ky]) {         dfs(kx, ky, component);       }     }   }   void init() {     for (int i = 0; i <= 600; i++) {       for (int j = 0; j <= 600; j++) {         used[i][j] = 0;       }     }   }   int main() {     int q;     q = read();     while (q--) {       h = read(), w = read();       init();       for (int i = 1; i <= h; i++) {         for (int j = 1; j <= w; j++) {           a[i][j] = read();         }       }       int cntfirst = 0, cntsecond = 0;       for (int i = 1; i <= h; i++) {         for (int j = 1; j <= w; j++) {           if (!used[i][j]) dfs(i, j, cntfirst + 1), cntfirst++;         }       }       for (int i = h / 2 + 1; i <= h; i++) {         for (int j = 1; j <= w; j++) {           dp[i - h / 2][j] = a[i][j];         }       }       for (int i = 1; i <= h / 2; i++) {         for (int j = 1; j <= w; j++) {           dp[i + h / 2][j] = a[i][j];         }       }       for (int i = 1; i <= h; i++) {         for (int j = 1; j <= w; j++) {           a[i][j] = dp[i][j];         }       }       init();       for (int i = 1; i <= h; i++) {         for (int j = 1; j <= w; j++) {           if (!used[i][j]) dfs(i, j, cntsecond + 1), cntsecond++;         }       }       if (cntfirst <= cntsecond) {         printf( NO n );       } else {         printf( YES n );       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 22e4;   char s[N * 2];   int l[N * 2], r[N * 2];   int main() {     ios::sync_with_stdio(0);     int n, k;     cin >> n >> k >> s;     for (int i = 0; i < n; i++) s[i + n] = s[i];     for (int i = 0, lx = -2e9; i < n * 2; i++) {       if (s[i] == s[i + 1]) lx = i + 1;       l[i + 1] = lx;     }     for (int i = n * 2 - 1, rx = 2e9; i > 0; i--) {       if (s[i] == s[i - 1]) rx = i - 1;       r[i - 1] = rx;     }     for (int i = 0; i < n; i++) {       if (i + n - l[i + n] < r[i] - i) {         if (i + n - l[i + n] <= k)           cout << s[l[i + n]];         else if (k & 1)           cout << (s[i] ==  W  ?  B  :  W );         else           cout << s[i];       } else {         if (r[i] - i <= k)           cout << s[r[i]];         else if (k & 1)           cout << (s[i] ==  W  ?  B  :  W );         else           cout << s[i];       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = 2.0 * acos(0.0);   const double E = exp(1);   template <class T1>   void deb(T1 e) {     cout << e << endl;   }   template <class T1, class T2>   void deb(T1 e1, T2 e2) {     cout << e1 <<     << e2 << endl;   }   template <class T1, class T2, class T3>   void deb(T1 e1, T2 e2, T3 e3) {     cout << e1 <<     << e2 <<     << e3 << endl;   }   template <class T1, class T2, class T3, class T4>   void deb(T1 e1, T2 e2, T3 e3, T4 e4) {     cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl;   }   template <class T1, class T2, class T3, class T4, class T5>   void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {     cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl;   }   template <class T1, class T2, class T3, class T4, class T5, class T6>   void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {     cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6          << endl;   }   template <typename T>   inline T sqr(T n) {     return n * n;   }   inline double hypot(double x, double y) { return sqrt(sqr(x) + sqr(y)); }   template <typename T>   inline T pmod(T n, T m) {     return ((n % m) + m) % m;   }   template <typename T>   T power(T n, int p) {     return (p == 0) ? (T)(1) : (sqr(power(n, p >> 1)) * ((p & 1) * (n - 1) + 1));   }   template <typename T>   T bigmod(T n, int p, T m) {     return (p == 0)                ? (T)(1)                : ((sqr(bigmod(n, p >> 1, m)) % m) * ((p & 1) * (n - 1) % m + 1)) %                      m;   }   bool dp[21][(1 << 6)][11][11][11][11][11];   int row[6], col[23];   int r, c;   vector<int> save[10];   int calc(int mask) {     int cnt = 0;     int prv = 0;     for (int i = 0; i < r; i++) {       if (!prv && (mask & (1 << i))) {         cnt++;       }       prv = mask & (1 << i);     }     return cnt;   }   char grid[6][21];   int tmp[10];   bool complet = 0;   void rec(int now, int prv_maks, int r1, int r2, int r3, int r4, int r5) {     if (r1 < 0 || r2 < 0 || r3 < 0 || r4 < 0 || r5 < 0) return;     if (complet) return;     if (dp[now][prv_maks][r1][r2][r3][r4][r5]) return;     dp[now][prv_maks][r1][r2][r3][r4][r5] = 1;     if (now == c) {       if (r1 + r2 + r3 + r4 + r5 == 0) {         for (int i = 0; i < r; ++i) {           for (int j = 0; j < c; ++j) printf( %c , grid[i][j]);           printf(  n );         }         complet = 1;       }       return;     }     int mask = 0;     for (int i = 0; i < (int)save[col[now]].size(); i++) {       mask = save[col[now]][i];       tmp[0] = r1;       tmp[1] = r2;       tmp[2] = r3;       tmp[3] = r4;       tmp[4] = r5;       for (int j = 0; j < r; j++) {         if (!(prv_maks & (1 << j)) && (mask & (1 << j))) {           tmp[j]--;         }       }       for (int j = 0; j < r; j++) {         grid[j][now] = (mask & (1 << j)) ?  *  :  . ;       }       rec(now + 1, mask, tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);       if (complet) return;     }   }   int main() {     scanf( %d %d , &r, &c);     for (int i = 0; i < r; ++i) scanf( %d , &row[i]);     for (int i = 0; i < c; ++i) {       scanf( %d , &col[i]);     };     int lim = 1 << r;     for (int i = 0; i < (1 << r); i++) {       save[calc(i)].push_back(i);     }     rec(0, 0, row[0], row[1], row[2], row[3], row[4]);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int p, d;     cin >> p >> d;     long long int temp = 1;     long long int po[19];     for (int i = 0; i <= 18; ++i) {       po[i] = temp;       temp *= 10;     }     int i = 1;     temp = p;     while (1) {       long long int t = temp % po[i];       t /= po[i - 1];       long long int x = temp;       x -= (t * po[i - 1]);       x += (9 * po[i - 1]);       if (p - x >= 0 && p - x <= d) {         i++;         temp = x;         continue;       }       if ((p - x + po[i]) <= d && x - po[i] >= 0) {         x -= po[i];         temp = x;         i++;       } else         break;     }     cout << temp;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[5005], dp[5005], b[5005];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     dp[0] = 0;     for (int i = 1; i <= n; i++) {       int p = i, s = a[i];       while (p > 0 && s < b[p - 1]) {         s += a[p - 1];         p--;       }       dp[i] = dp[p - 1] + i - p;       b[i] = s;     }     printf( %d n , dp[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int maxn = 1e5 + 5;   vector<int> a[maxn];   int res[maxn];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       int q, w;       scanf( %d%d , &q, &w);       a[q].push_back(w);     }     int ms = 0;     for (int i = 1; i <= m; i++) {       int sump = a[i].size();       sort(a[i].begin(), a[i].end());       ms = max(sump, ms);       int sum = 0;       for (int j = sump - 1; j >= 0; j--) {         sum += a[i][j];         if (sum >= 0)           res[sump - j] += sum;         else           break;       }     }     int max1 = 0;     for (int i = 1; i <= ms; i++) max1 = max(res[i], max1);     printf( %d n , max1);   }


#include <bits/stdc++.h>   using namespace std;   long long s(long long x) {     long long sum = 0;     while (x) {       sum += x % 10;       x /= 10;     }     return sum;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long T;     T = 1;     while (T--) {       long long n, i, x, ans = -1;       cin >> n;       x = sqrt(n);       for (i = 1; i <= 90; i++) {         long long l, r;         l = 1;         r = x;         while (l <= r) {           long long m = l + (r - l) / 2;           long long z = m * m + m * i;           if (z == n && s(m) == i)             ans = m, r = m - 1;           else if (z < n)             l = m + 1;           else             r = m - 1;         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int N = 4000;   const int INF = 1e9;   struct edge {     int to, c, f, back;     edge(int to, int c, int f, int back) : to(to), c(c), f(f), back(back) {}   };   vector<edge> g[N];   vector<int> d(N), ptr(N);   int n, h, m;   int s, t;   void add_edge(int v, int u, int c) {     g[v].push_back({u, c, 0, (int)g[u].size()});     g[u].push_back({v, 0, 0, (int)g[v].size() - 1});   }   bool bfs() {     fill(d.begin(), d.end(), INF);     d[s] = 0;     queue<int> q;     q.push(s);     while ((int)q.size()) {       int v = q.front();       q.pop();       for (auto i : g[v]) {         if (i.c > i.f && d[i.to] == INF) {           d[i.to] = d[v] + 1;           q.push(i.to);         }       }     }     return d[t] != INF;   }   int dfs(int v, int cur_flow = INF) {     if (v == t || cur_flow == 0) return cur_flow;     for (int i = ptr[v]; i < (int)g[v].size(); i++) {       int u = g[v][i].to;       if (d[v] + 1 == d[u]) {         int push = dfs(u, min(cur_flow, g[v][i].c - g[v][i].f));         if (push) {           g[v][i].f += push;           g[u][g[v][i].back].f -= push;           return push;         }       }       ptr[v]++;     }     return 0;   }   int maxFlow() {     int ret = 0;     while (bfs()) {       int cur = dfs(s);       fill(ptr.begin(), ptr.end(), 0);       while (cur) {         ret += cur;         cur = dfs(s);       }     }     return ret;   }   int get_hash(int x, int y) { return x * 52 + y; }   signed main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> h >> m;     s = 0, t = N - 1;     for (int i = 0; i < n; i++) {       add_edge(s, get_hash(i, 0), INF);       for (int j = 0; j <= h; j++) {         add_edge(get_hash(i, j), get_hash(i, j + 1), h * h - j * j);         add_edge(get_hash(i, j + 1), get_hash(i, j), INF);       }       add_edge(get_hash(i, h), t, 0);     }     int st = 50 * 55 + 10;     assert(st + m < N);     for (int i = 0; i < m; i++) {       int l, r, x, c;       cin >> l >> r >> x >> c;       if (x == h) continue;       l--;       r--;       for (int j = l; j <= r; j++) {         add_edge(get_hash(j, x + 1), st + i, INF);       }       add_edge(st + i, t, c);     }     cout << n * h * h - maxFlow() <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   string A[18][18], s;   int n, m, U[101], us, J[3][3];   bool B[1001];   int r1, c1, r2, c2, size;   int tonum(char ch) {     if (ch ==  C ) return 1;     if (ch ==  D ) return 2;     if (ch ==  H ) return 3;     if (ch ==  S ) return 4;     if (ch ==  T ) return 8;     if (ch ==  J ) return 9;     if (ch ==  Q ) return 10;     if (ch ==  K ) return 11;     if (ch ==  A ) return 12;     return (int)ch - 50;   }   inline bool inside(int a, int b, int c) {     if (b > c) swap(b, c);     return (b <= a && a <= c);   }   inline bool ff(int r, int c) {     bool B[13];     for (int i = 0; i <= 12; i++) B[i] = false;     for (int i = r; i <= r + 2; i++)       for (int j = c; j <= c + 2; j++) {         if (B[tonum(A[i][j][0])]) return false;         B[tonum(A[i][j][0])] = true;       }     return true;   }   inline bool check() {     for (int i = 1; i <= size; i++)       for (int j = i + 1; j <= size; j++) {         r1 = (i % m) ? i / m + 1 : i / m;         c1 = (i % m) ? (i % m) : m;         r2 = (j % m) ? j / m + 1 : j / m;         c2 = (j % m) ? (j % m) : m;         if (!(inside(r2, r1, r1 + 2) && inside(c2, c1, c1 + 2)) &&             !(inside(r2, r1, r1 + 2) && inside(c2 + 2, c1, c1 + 2)) &&             inside(r1 + 2, 1, n) && inside(r2 + 2, 1, n) &&             inside(c1 + 2, 1, m) && inside(c2 + 2, 1, m)) {           if (ff(r1, c1) && ff(r2, c2)) return true;         }       }     return false;   }   char torank(int k) {     int r = (k % 4) ? k / 4 + 1 : k / 4;     if (r <= 8) return (char)(r + 49);     if (r == 9) return  T ;     if (r == 10) return  J ;     if (r == 11) return  Q ;     if (r == 12) return  K ;     if (r == 13) return  A ;   }   char tosuit(int k) {     int r = k % 4;     if (r == 0) return  S ;     if (r == 1) return  C ;     if (r == 2) return  D ;     if (r == 3) return  H ;   }   inline bool replacee(int j, int k) {     if (J[j][1] == 0) return false;     A[J[j][1]][J[j][2]][0] = torank(k);     A[J[j][1]][J[j][2]][1] = tosuit(k);     return true;   }   string print(int k) {     char A[3];     A[0] = torank(k);     A[1] = tosuit(k);     A[2] =   0 ;     string str(A);     return str;   }   void printt() {     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) cout << A[i][j][0] << A[i][j][1] <<    ;       cout << endl;     }     cout << endl;   }   int main() {     bool rep1, rep2;     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         cin >> s;         A[i][j].assign(s);         if (s[0] ==  J ) {           if (s[1] ==  1 ) {             J[1][1] = i;             J[1][2] = j;           } else if (s[1] ==  2 ) {             J[2][1] = i;             J[2][2] = j;           }         }         B[tonum(s[0]) * 4 + tonum(s[1])] = true;       }     size = n * m;     for (int i = 1; i <= 52; i++)       if (!B[i]) U[us++] = i;     for (int i = 0; i < us; i++)       for (int j = 0; j < us; j++)         if (i != j) {           rep1 = replacee(1, U[i]);           rep2 = replacee(2, U[j]);           if (check()) {             cout <<  Solution exists.  << endl;             if (!rep1 && !rep2)               cout <<  There are no jokers.  << endl;             else if (rep1 && rep2)               cout <<  Replace J1 with   << print(U[i]) <<   and J2 with                      << print(U[j]) <<  .  << endl;             else if (rep1)               cout <<  Replace J1 with   << print(U[i]) <<  .  << endl;             else if (rep2)               cout <<  Replace J2 with   << print(U[j]) <<  .  << endl;             cout <<  Put the first square to (  << r1 <<  ,   << c1 <<  ).                   << endl;             cout <<  Put the second square to (  << r2 <<  ,   << c2 <<  ).                   << endl;             return 0;           }         }     cout <<  No solution.  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n;     int x;     cin >> t;     while (t--) {       cin >> n;       map<int, int> m;       for (int i = 0; i < n; i++) {         cin >> x;         m[x] = 1;       }       cout << m.size() << endl;     }   }


#include <bits/stdc++.h>   int comp(const void *a, const void *b) { return *(int *)a - *(int *)b; }   int min(int a, int b) { return a < b ? a : b; }   int max(int a, int b) { return a > b ? a : b; }   int rev(int n) {     int r = 0;     for (int i = 0; i < 8; i++) r = r | (((n >> i) & 1) << (7 - i));     return r;   }   char c;   int n;   int main() {     int p = 0, t;     while (1) {       scanf( %c , &c);       if (c < 32 || c > 126) break;       t = (256 + rev(p) - rev(c)) % 256;       p = c;       printf( %d n , t);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-9;   const long long int mod = 1e9 + 7;   long long int pow(long long int x, long long int y) {     long long int sum = 1;     for (long long int i = 0; i < y; ++i) {       sum *= x;       sum %= mod;     }     return sum;   }   int main(void) {     long long int k, p;     cin >> p >> k;     if (k == 0) {       cout << pow(p, p - 1) << endl;       return 0;     }     if (k == 1) {       cout << pow(p, p) << endl;       return 0;     }     int o = 1;     long long int prod = k;     while (prod != 1) {       prod *= k;       prod %= p;       o++;     }     assert((p - 1) % o == 0);     cout << pow(p, (p - 1) / o) << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long sum1 = 0, sum2 = 0;     vector<long long> one, two;     int last = 0;     for (int i = 0; i < n; i++) {       int temp;       scanf( %d , &temp);       if (temp < 0) {         two.push_back(abs(temp));         sum2 += abs(temp);         last = 2;       } else {         one.push_back(temp);         sum1 += temp;         last = 1;       }     }     if (sum1 != sum2) {       if (sum1 > sum2)         puts( first );       else         puts( second );       return 0;     }     for (int i = 0; i < min(one.size(), two.size()); i++) {       if (one[i] != two[i]) {         if (one[i] < two[i])           puts( second );         else if (one[i] > two[i])           puts( first );         return 0;       }     }     if (one.size() != two.size()) {       if (one.size() > two.size())         puts( first );       else         puts( second );       return 0;     }     if (last == 1)       puts( first );     else       puts( second );   }


#include <bits/stdc++.h>   using namespace std;   const int MXN = 300300;   int n, a[MXN], match[MXN], cnt[MXN];   map<int, int> prevMatch[MXN];   int main() {     int T;     scanf( %d , &T);     for (int t = 1; t <= T; ++t) {       scanf( %d , &n);       for (int i = 1; i <= n; ++i) {         scanf( %d , &a[i]);         match[i] = cnt[i] = 0;         prevMatch[i].clear();       }       for (int i = 2; i <= n; ++i) {         if (a[i] == a[i - 1]) {           match[i] = i - 1;         } else {           match[i] = max(0, prevMatch[i - 1][a[i]]);         }         if (match[i] > 1) {           swap(prevMatch[i], prevMatch[match[i] - 1]);           prevMatch[i][a[match[i] - 1]] = match[i] - 1;         }       }       long long res = 0;       for (int i = 2; i <= n; ++i) {         if (match[i] == 0) continue;         cnt[i] = cnt[match[i] - 1] + 1;         res += cnt[i];       }       cout << res <<   n ;     }     fclose(stdin);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 998244353;   long long power(long long x, long long y) {     long long z = 1;     while (y > 0) {       if (y % 2) z = z * x;       x = x * x;       y /= 2;     }     return z;   }   long long gcd(long long a, long long b) {     if (a < b) return gcd(b, a);     if (b == 0) return a;     return gcd(b, a % b);   }   long long min(long long a, long long b) {     if (a < b) return a;     return b;   }   long long max(long long a, long long b) {     if (a > b) return a;     return b;   }   void solve() {     string s;     cin >> s;     vector<long long> v;     long long n = s.size(), cnt = 0;     for (long long i = 0; i < n; i++) {       if (s[i] ==  1 ) v.push_back(i);     }     if (v.empty()) {       cout << 0 << endl;       return;     }     for (long long i = v[0]; i < v[v.size() - 1]; i++) {       if (s[i] ==  0 ) cnt++;     }     cout << cnt << endl;   }   int main() {     long long t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   inline int lb(int a) { return a & (-a); }   long long a[100100], bit[7][15][100100];   int n, m, t;   inline void adj(int z, int zm, int pos, long long val) {     for (int i = pos; i <= n; i += lb(i)) bit[z][zm][i] += val;   }   inline long long qry(int z, int zm, int pos) {     if (pos <= 0) return 0;     long long rt = 0;     for (int i = pos; i > 0; i -= lb(i)) rt += bit[z][zm][i];     return rt;   }   inline long long sum(int z, int zm, int l, int r) {     int z2 = z * 2 - 2;     int lb = l + zm - 1, rb = r;     if (lb > r) return 0;     while (rb % z2 != lb % z2) rb--;     lb -= z2;     if (rb < l) return 0;     return qry(z, rb % z2, rb) - qry(z, lb % z2, lb);   }   inline int s(int z, int i) {     int mod = i % (2 * z - 2);     if (mod == 0)       return 2;     else if (mod <= z)       return mod;     else       return 2 * z - mod;   }   inline void update(int p, int v) {     for (int z = 2; z <= 6; z++) {       int z2 = z * 2 - 2;       adj(z, p % z2, p, v - a[p]);     }     a[p] = v;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int in;       scanf( %d , &in);       update(i, in);     }     scanf( %d , &m);     while (m-- > 0) {       scanf( %d , &t);       if (t == 1) {         int p, v;         scanf( %d%d , &p, &v);         update(p, v);       } else {         int l, r, z;         scanf( %d%d%d , &l, &r, &z);         long long ans = 0;         int z2 = z * 2 - 2;         for (int i = 1; i <= z2; i++) {           ans += s(z, i) * sum(z, i, l, r);         }         printf( %I64d n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const long long MAX = 1e6 + 5;   bool cmp(const pair<long long, long long> &a,            const pair<long long, long long> &b) {     if (a.first < b.first)       return true;     else if (a.first == b.first) {       if (b.second > a.second) return true;       return false;     }     return false;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long tc = 1, i, j;     while (tc--) {       long long n;       cin >> n;       long long a, b, c, d, e, f;       cin >> a >> b >> c;       cin >> d >> e >> f;       long long mi, ma;       mi = max(0LL, max(c - f - e, max(b - d - e, a - d - f)));       ma = min(a, e) + min(b, f) + min(c, d);       cout << mi <<     << ma <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   int n, fa[200005][2], sz[200005][2];   long long ans;   int find(int x, int c) {     return fa[x][c] == x ? x : (fa[x][c] = find(fa[x][c], c));   }   void merge(int x, int y, int c) {     x = find(x, c), y = find(y, c);     if (x == y) return;     fa[y][c] = fa[x][c], sz[x][c] += sz[y][c];   }   int main() {     scanf( %d , &n);     for (register int i = 1; i <= n; ++i)       fa[i][0] = fa[i][1] = i, sz[i][0] = sz[i][1] = 1;     for (register int i = 1, u, v, w; i < n; ++i)       scanf( %d%d%d , &u, &v, &w), merge(u, v, w);     for (register int i = 1; i <= n; ++i)       ans += 1ll * sz[find(i, 0)][0] * sz[find(i, 1)][1] - 1;     printf( %lld , ans);   }


#include <bits/stdc++.h>   using namespace std;   double dot(complex<double> a, complex<double> b) {     return (conj(a) * b).real();   }   double cross(complex<double> a, complex<double> b) {     return (conj(a) * b).imag();   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long double x, y, minangle = 600;     int n, s, e;     cin >> n;     vector<pair<long double, int>> v;     for (int i = 1; i <= n; i++) {       cin >> x >> y;       v.push_back({atan2(x, y) * 180.0 / 3.14159265359, i});       while (v.back().first < 0) v.back().first += 360.0;       while (v.back().first > 360.0) v.back().first -= 360.0;     }     sort(v.begin(), v.end());     for (int i = 0; i < v.size(); i++) {       if (min(abs(v[i].first - v[(i + 1) % n].first),               360.0 - abs(v[i].first - v[(i + 1) % n].first)) -               minangle <=           -1e-20) {         minangle = min(abs(v[i].first - v[(i + 1) % n].first),                        360.0 - abs(v[i].first - v[(i + 1) % n].first));         s = v[i].second;         e = v[(i + 1) % n].second;       }     }     cout << s <<     << e;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const signed long long Infinity = 1000000001;   const long double Epsilon = 1e-9;   template <typename T, typename U>   ostream& operator<<(ostream& os, const pair<T, U>& p) {     return os <<  (  << p.first <<  ,  << p.second <<  ) ;   }   template <typename T>   ostream& operator<<(ostream& os, const vector<T>& V) {     os <<  [ ;     for (__typeof(V.begin()) i = V.begin(); i != V.end(); ++i)       os << *i << (i - V.begin() + 1 == ((int)V.size()) ?    :  , );     os <<  ] n ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const set<T>& S) {     os <<  ( ;     for (__typeof(S.begin()) i = S.begin(); i != S.end(); ++i)       os << *i << (*i == *S.rbegin() ?    :  , );     os <<  ) n ;     return os;   }   template <typename T, typename U>   ostream& operator<<(ostream& os, const map<T, U>& M) {     os <<  { ;     for (__typeof(M.begin()) i = M.begin(); i != M.end(); ++i)       os << *i << (i->first == M.rbegin()->first ?    :  , );     os <<  } n ;     return os;   }   int N, K;   void read_data() { scanf( %d %d , &N, &K); }   void solve() {     if (N == K) {       printf( -1 n );     } else {       for (int(i) = (1); (i) <= (N - K - 1); (i)++) printf( %d  , i + 1);       printf( 1  );       for (int(i) = (N - K + 1); (i) <= (N); (i)++) printf( %d  , i);     }   }   int main() {     read_data();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   char S[500050];   int Aint[4 * 500050];   int pa, pb, pv;   long long A[500050];   void update(int k, int st, int dr) {     if (pa <= st && dr <= pb) {       Aint[k] += pv;       return;     }     int m = (st + dr) / 2;     if (pa <= m) {       update(2 * k, st, m);     }     if (pb > m) {       update(2 * k + 1, m + 1, dr);     }   }   void goUpdates(int p) {     int a = p + 1;     int b = N - p;     if (a > b) {       swap(a, b);     }     pa = 1;     pb = a;     pv = 1;     update(1, 1, N);     if (b + 1 <= N) {       pa = b + 1;       pb = N;       pv = -1;       update(1, 1, N);     }   }   void go(int k, int st, int dr, long long sum) {     sum += Aint[k];     if (st == dr) {       A[st] = sum;       return;     }     int m = (st + dr) / 2;     go(2 * k, st, m, sum);     go(2 * k + 1, m + 1, dr, sum);   }   int main() {     cin.sync_with_stdio(false);     fgets(S, sizeof(S), stdin);     N = 0;     for (int i = 0; S[i] !=   n  && S[i] !=   0 ; i++, N++)       ;     for (int i = 0; i < N; i++) {       if (S[i] ==  I  || S[i] ==  E  || S[i] ==  A  || S[i] ==  O  ||           S[i] ==  U  || S[i] ==  Y ) {         goUpdates(i);       }     }     go(1, 1, N, 0);     double ans = 0.0;     for (int i = 1; i <= N; i++) {       A[i] += A[i - 1];       ans += (double)A[i] / i;     }     cout << fixed << setprecision(10) << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300009;   map<short, int> f[maxn];   int a[maxn], n, K, p, q, d;   int sum[maxn], i, j, k;   int main() {     scanf( %d%d , &n, &K);     d = n / K;     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     for (i = 2; i <= n; i++) sum[i] = sum[i - 1] + a[i] - a[i - 1];     f[0][0] = 0;     for (i = 1; i <= n; i++) {       for (k = 0; k <= 1; k++) {         if (i >= d)           for (map<short, int>::iterator j = f[i - d].begin();                j != f[i - d].end(); j++)             if (j->first + k <= n % K) {               if (!f[i].count(j->first + k))                 f[i][j->first + k] = j->second + sum[i] - sum[i - d + 1];               else                 f[i][j->first + k] =                     min(f[i][j->first + k], j->second + sum[i] - sum[i - d + 1]);             }         d++;       }       if (i >= d) f[i - d].clear();       d -= 2;     }     printf( %d , f[n][n % K]);     return 0;   }