#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 200100, M = 1000000009;   string s;   long long n, ans = 0, a[MAXN], b[MAXN];   void obra(long long v) {     long long tc = 0, k = a[v];     for (long long i = 0; i < n; i++) {       b[i] = a[i] + k / n;       if (i == v) b[i] -= a[i];     }     k %= 14;     for (long long i = 0; i < k; i++) {       b[(v + 1 + i) % n]++;     }     for (long long i = 0; i < n; i++) {       if (b[i] % 2 == 0) {         tc += b[i];       }     }     if (ans < tc) ans = tc;   }   int main() {     ios_base::sync_with_stdio(0);     n = 14;     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     for (long long i = 0; i < n; i++) obra(i);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long arr[n];     for (int i = 0; i < n; i++) scanf( %lld , &arr[i]);     map<long long, int> mp;     long long sum = 0;     for (int i = n - 1; i >= 0; i--) {       sum += arr[i];       mp[sum] = i;     }     long long ans = 0;     long long cnt = 0;     for (int i = 0; i < n; i++) {       cnt += arr[i];       if (mp[cnt] > i && mp[cnt] != 0) {         ans = max(cnt, ans);       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   template <typename T>   T &chmin(T &a, const T &b) {     return a = min(a, b);   }   template <typename T>   T &chmax(T &a, const T &b) {     return a = max(a, b);   }   template <typename T>   T inf;   template <>   constexpr int inf<int> = 1e9;   template <>   constexpr ll inf<ll> = 1e18;   template <>   constexpr ld inf<ld> = 1e30;   struct yes_no : numpunct<char> {     string_type do_truename() const { return  Yes ; }     string_type do_falsename() const { return  No ; }   };   int N, K;   int A[501000];   int main() {     locale loc(locale(), new yes_no);     cout << boolalpha;     cout.imbue(loc);     scanf( %d%d , &K, &N);     for (int i = 0; i < (int)(N); i++) scanf( %d , &A[i]);     sort(A, A + N);     set<pair<ll, ll>> index_value, value_index;     for (int i = 0; i < (int)(N - 1); i++) {       const int val = A[i + 1] - A[i];       index_value.emplace(i, val);       value_index.emplace(val, i);     }     index_value.emplace(-1, inf<ll>);     index_value.emplace(N, inf<ll>);     value_index.emplace(inf<ll>, -1);     value_index.emplace(inf<ll>, N);     ll res = 0;     for (int loop = 0; loop < (int)(K); loop++) {       int v, l, r;       ll val, lval, rval;       tie(val, v) = *begin(value_index);       auto it = index_value.lower_bound(make_pair(v, val));       tie(l, lval) = *prev(it);       tie(r, rval) = *next(it);       index_value.erase(make_pair(v, val));       index_value.erase(make_pair(l, lval));       index_value.erase(make_pair(r, rval));       value_index.erase(make_pair(val, v));       value_index.erase(make_pair(lval, l));       value_index.erase(make_pair(rval, r));       index_value.emplace(v, lval + rval - val);       value_index.emplace(lval + rval - val, v);       res += val;     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> childs[100000 + 1];   long long s[100000 + 1];   int p[100000 + 1];   long long a[100000 + 1];   bool flag = false;   void fill(int v, long long prevsum) {     if (s[v] != -1) {       a[v] = s[v] - prevsum;       if (a[v] < 0) flag = true;       for (int i = 0; i < childs[v].size(); ++i) {         fill(childs[v][i], s[v]);       }     } else {       long long mi = 1000000001;       for (int i = 0; i < childs[v].size(); ++i) {         mi = min(mi, s[childs[v][i]]);       }       if (mi == 1000000001) mi = prevsum;       a[v] = mi - prevsum;       if (a[v] < 0) flag = true;       for (int i = 0; i < childs[v].size(); ++i) {         fill(childs[v][i], mi);       }     }   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       childs[i].clear();     }     for (int i = 2; i < n + 1; ++i) {       cin >> p[i];       childs[p[i]].push_back(i);     }     for (int i = 1; i < n + 1; ++i) {       cin >> s[i];     }     fill(1, 0);     if (flag)       cout << -1 << endl;     else {       long long sum = 0;       for (int i = 1; i < n + 1; ++i) {         sum += a[i];       }       cout << sum << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   string a, b;   int i, ax, ay, bx, by;   int main() {     cin >> a >> b;     for (i = 0; i < a.length(); i++)       if (a[i] ==  1 )         ax++;       else         ay++;     for (i = 0; i <= b.length(); i++)       if (b[i] ==  1 )         bx++;       else         by++;     if (ax % 2 == 1) ax++;     if (ax < bx)       cout <<  NO ;     else       cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:200000 )   const int INF = 1e9 + 7;   const double EPS = 1e-6;   const double pi = acos(-1);   const int MOD = 1e9 + 7;   const int N = 1e5 + 1000;   using namespace std;   double mid;   struct node {     int pos, val;     node() { pos = val = 0; }     node(int a, int b) {       pos = a;       val = b;     }     bool operator<(node a) const {       if (val != a.val) return val < a.val;       if (abs(mid - pos * 1.0) != abs(mid - a.pos * 1.0))         return abs(mid - pos * 1.0) < abs(mid - a.pos * 1.0);       return pos < a.pos;     }   };   int n, m, ans[N];   set<node> st;   int main() {     cin >> n >> m;     mid = (m + 1) * 0.5;     for (int i = 1; i <= m; i++) {       st.insert(node(i, 0));     }     for (int i = 1; i <= n; i++) {       auto it = *st.begin();       st.erase(it);       it.val++;       ans[i] = it.pos;       st.insert(it);     }     for (int i = 1; i <= n; i++) cout << ans[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, op;   string tes;   int main() {     cin >> n >> k >> ws >> tes;     op = 0;     for (int i = 1; i < n; i++)       if (tes.substr(0, i) == tes.substr(n - i, i)) op = i;     cout << tes;     tes = tes.substr(op, tes.length());     for (int i = 1; i < k; i++) cout << tes;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char x[100100];   long x2[100100];   long mes[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   long wr = 0;   long res, cur, repsmax = -1;   long reps = 0;   long tmp1, tmp2, tmp3;   string s1;   int compare(const void *a, const void *b) { return (*(long *)a - *(long *)b); }   long check(long z) {     if ((isdigit(x[z])) && (isdigit(x[z + 1])) && (isdigit(x[z + 3])) &&         (isdigit(x[z + 4])) && (isdigit(x[z + 6])) && (isdigit(x[z + 7])) &&         (isdigit(x[z + 8])) && (isdigit(x[z + 9]))) {       if ((x[z + 2] == x[z + 5]) && (x[z + 2] ==  - )) {         tmp1 =             ((long)(x[z]) - (long)( 0 )) * 10 + ((long)(x[z + 1]) - (long)( 0 ));         tmp2 = ((long)(x[z + 3]) - (long)( 0 )) * 10 +                ((long)(x[z + 4]) - (long)( 0 ));         tmp3 = ((long)(x[z + 6]) - (long)( 0 )) * 1000 +                ((long)(x[z + 7]) - (long)( 0 )) * 100 +                (((long)(x[z + 8]) - (long)( 0 )) * 10) +                ((long)(x[z + 9]) - (long)( 0 ));         if ((tmp2 <= 12) && (tmp3 <= 2015) && (tmp3 >= 2013)) {           if ((tmp1 <= mes[tmp2]) && (tmp2 != 0) && (tmp1 != 0)) {             x2[wr++] = tmp1 + tmp2 * 100 + tmp3 * 10000;           }         }       }     }     if ((!isdigit(x[z + 9])) && (x[z + 9] !=  - )) {       return -2;     }   }   int main(int argc, char *argv[]) {     scanf( %s , &x);     long j = 0;     while (check(j++) > -2) {     }     qsort(x2, wr - 1, sizeof(long), compare);     cur = x2[0];     for (long j = 0; j < wr; j++) {       if (cur == x2[j]) {         reps++;       } else {         if (reps > repsmax) {           repsmax = reps;           res = cur;         }         cur = x2[j];         reps = 1;       }     }     if (reps > repsmax) {       repsmax = reps;       res = cur;     }     cur = x2[j];     reps = 1;     ldiv_t divresult1;     divresult1 = ldiv(res, 100);     if (divresult1.rem < 10) {       cout <<  0 ;     }     cout << divresult1.rem <<  - ;     ldiv_t divresult2;     divresult2 = ldiv(res, 10000);     if (((divresult2.rem - divresult1.rem) / 100) < 10) {       cout <<  0 ;     }     cout << (divresult2.rem - divresult1.rem) / 100 <<  - ;     ldiv_t divresult3;     divresult3 = ldiv(res, 100000000);     cout << (divresult3.rem - divresult2.rem) / 10000;     return EXIT_SUCCESS;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int n, m;   vector<pair<int, int> > e[N];   vector<int> ans[2];   bool fla[2], flag;   int E[N], d[N];   vector<int> cnt[2];   void DFS(int first, int o, int res) {     if (flag) return;     d[first] = o;     cnt[o].push_back(first);     for (auto& i : e[first]) {       if (flag) return;       int second = (i.second + o) % 2;       if (d[i.first] == -1)         DFS(i.first, second != res, res);       else if (d[i.first] != (second != res))         flag = true;     }   }   void SOLVE(int res) {     memset(d, -1, sizeof(d));     flag = false;     for (int i(1); i <= n; i++)       if (d[i] == -1) {         cnt[0].clear();         cnt[1].clear();         DFS(i, 0, res);         if (flag) {           fla[res] = true;           return;         }         for (bool ___ = 1; ___; ___ = 0) {           int mx = (cnt[1].size() < cnt[0].size());           for (auto& i : cnt[mx]) ans[res].push_back(i);         }       }   }   int main() {     n = ({       long long first = 0;       int n = 0, c = getchar();       for (; !isdigit(c); c = getchar()) n = c ==  - ;       for (; isdigit(c); c = getchar()) first = first * 10 + c -  0 ;       n ? -first : first;     }),     m = ({       long long first = 0;       int n = 0, c = getchar();       for (; !isdigit(c); c = getchar()) n = c ==  - ;       for (; isdigit(c); c = getchar()) first = first * 10 + c -  0 ;       n ? -first : first;     });     for (int i(1); i <= m; i++) {       int first = ({         long long first = 0;         int n = 0, c = getchar();         for (; !isdigit(c); c = getchar()) n = c ==  - ;         for (; isdigit(c); c = getchar()) first = first * 10 + c -  0 ;         n ? -first : first;       }),           second = ({             long long first = 0;             int n = 0, c = getchar();             for (; !isdigit(c); c = getchar()) n = c ==  - ;             for (; isdigit(c); c = getchar()) first = first * 10 + c -  0 ;             n ? -first : first;           });       int c = ({                 char c = getchar();                 while (c ==     || c ==   n ) c = getchar();                 c;               }) ==  R ;       e[first].push_back({second, c});       e[second].push_back({first, c});     }     SOLVE(0);     SOLVE(1);     for (bool ___ = 1; ___; ___ = 0) {       int res;       if (fla[0] && fla[1])         return cout << -1 <<   n , 0;       else if (fla[0])         res = 1;       else if (fla[1])         res = 0;       else if (ans[0].size() < ans[1].size())         res = 0;       else         res = 1;       cout << ans[res].size() <<   n ;       for (auto& i : ans[res]) cout << i <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   class BLongtailHedgehog {    public:     int N, M;     vector<int> dist, deg_in, deg;     vector<vector<int>> G;     void input(std::istream& in, std::ostream& out) {       ios_base::sync_with_stdio(false);       in >> N >> M;       dist = vector<int>(N, -1);       deg_in = vector<int>(N, 0);       deg = vector<int>(N, 0);       G = vector<vector<int>>(N, vector<int>());       for (int(i) = (0); (i) < (M); ++(i)) {         int u, v;         in >> u >> v;         u--, v--;         if (u > v) swap(u, v);         G[u].push_back(v);         deg_in[v]++, deg[u]++, deg[v]++;       }     }     void bfs(int s) {       queue<int> q;       dist[s] = 1, q.push(s);       while (!q.empty()) {         int u = q.front();         q.pop();         for (auto v : G[u]) {           deg_in[v]--;           dist[v] = max(dist[v], dist[u] + 1);           if (deg_in[v] == 0) {             q.push(v);           }         }       }     }     void solve(std::istream& in, std::ostream& out) {       input(in, out);       for (int(i) = (0); (i) < (N); ++(i)) {         if (dist[i] == -1) bfs(i);       }       long long ans = 0;       for (int(i) = (0); (i) < (N); ++(i)) ans = max(ans, 1LL * dist[i] * deg[i]);       out << ans <<   n ;     }   };   int main() {     BLongtailHedgehog solver;     std::istream& in(std::cin);     std::ostream& out(std::cout);     solver.solve(in, out);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void hi() {     static long long i = 0;     cout <<  Check Point :   << ++i <<   n ;     return;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long t;     t = 1;     while (t--) {       string str;       cin >> str;       long long n = str.length();       stack<char> stk;       for (long long i = 0; i < n; i++) {         if (!stk.empty() && ((stk.top() ==  +  && str[i] ==  + ) ||                              ((stk.top() ==  -  && str[i] ==  - )))) {           stk.pop();         } else {           stk.push(str[i]);         }       }       if (stk.empty()) {         cout <<  Yes               <<   n ;       } else {         cout <<  No               <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 400010;   int gcds[N], a[N], b[N];   int c[N], mx[N];   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i - 1]);       gcds[i] = gcd(i, n);     }     long long ans = 0;     for (int i = 1; i < n; i++) {       if (n % i) continue;       for (int j = 1; j <= n; j++) c[j] = c[j - 1] + (i == gcds[j]);       for (int j = 0; j < i; j++) mx[j] = a[j];       for (int j = i; j < n; j++) mx[j % i] = max(mx[j % i], a[j]);       for (int j = 0; j < n; j++) b[j] = b[j + n] = mx[j % i] == a[j];       for (int j = 2 * n - 2; j >= 0; j--)         if (b[j]) b[j] += b[j + 1];       for (int j = 0; j < n; j++) ans += c[min(n - 1, b[j])];     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   long long int a[300000];   int main() {     long long int n, i, hei, bai;     while (scanf( %lld , &n) != EOF) {       hei = bai = 0;       for (i = 0; i < n; i++) {         scanf( %lld , &a[i]);         if (i % 2) {           hei += (a[i] + 1) / 2;           bai += a[i] / 2;         } else {           bai += (a[i] + 1) / 2;           hei += a[i] / 2;         }       }       printf( %lld n , bai < hei ? bai : hei);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = INT_MAX;   const long long INFL = LLONG_MAX;   const int output_precision = 15;   const long double pi = acos(-1);   const bool debug = true;   int N;   int A[5];   int check() {     vector<int> B(4);     for (int(i) = 1, j1234 = 4; (i) <= j1234; (i)++) B[i - 1] = A[i];     sort(B.begin(), B.end());     int x1 = B[0] + B[1] + B[2] + B[3];     int x2 = 2 * (B[1] + B[2]);     int x3 = 4 * (B[3] - B[0]);     return x1 == x2 && x2 == x3;   }   int main() {     ios_base::sync_with_stdio(0);     cout.precision(output_precision);     cout << fixed;     cout.tie(0);     cin >> N;     for (int(i) = 1, j1234 = N; (i) <= j1234; (i)++) cin >> A[i];     if (N == 0) {       cout <<  YES  <<   n ;       cout << 1 <<   n ;       cout << 1 <<   n ;       cout << 3 <<   n ;       cout << 3 <<   n ;       return 0;     }     if (N == 1) {       int a = A[1];       cout <<  YES  <<   n ;       cout << a <<   n ;       cout << 3 * a <<   n ;       cout << 3 * a <<   n ;       return 0;     }     if (N == 2) {       for (int(x) = 1, j1234 = 1500; (x) <= j1234; (x)++)         for (int(y) = 1, j1234 = 1500; (y) <= j1234; (y)++) {           A[3] = x;           A[4] = y;           if (check()) {             cout <<  YES  <<   n ;             cout << x <<   n ;             cout << y <<   n ;             return 0;           }         }       cout <<  NO  <<   n ;       return 0;     }     if (N == 3) {       for (int(i) = 1, j1234 = 1500; (i) <= j1234; (i)++) {         A[4] = i;         if (check()) {           cout <<  YES  <<   n ;           cout << i <<   n ;           return 0;         }       }       cout <<  NO  <<   n ;       return 0;     }     if (N == 4) {       if (check()) {         cout <<  YES  <<   n ;       } else {         cout <<  NO  <<   n ;       }       return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   void islam() {     ios::sync_with_stdio(false);     cin.tie(NULL);   }   void get(long long &x) { cin >> x; }   class ghost {    public:     ghost() {       get(n);       for (int i = 0; i < n; i++) {         get(arr[i]);         s += arr[i];       }       s /= n;     }     void solve() {       long long c = 0;       for (int i = 0; i < n; i++) {         c += s - arr[i];         a += abs(c);       }     }     void print() { cout << a; }       private:     long long n, s;     long long arr[50000 + 10], a = 0;   };   int main() {     islam();     ghost x;     x.solve();     x.print();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isAscending(const vector<int64_t>& vec) {     for (int i = 1; i < vec.size(); ++i) {       if (vec[i - 1] >= vec[i]) {         return false;       }     }     return true;   }   bool Requal(const vector<int64_t>& av, const vector<int64_t>& bv) {     for (int i = 0; i < av.size(); ++i) {       if (av[i] != bv[bv.size() - i - 1]) {         return false;       }     }     return true;   }   bool Equal(const vector<int64_t>& av, const vector<int64_t>& bv) {     for (int i = 0; i < av.size(); ++i) {       if (av[i] != bv[i]) {         return false;       }     }     return true;   }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     int len;     cin >> len;     vector<int64_t> av(len), bv(len);     for (auto&& e : av) {       cin >> e;     }     for (auto&& e : bv) {       cin >> e;     }     if (len == 1) {       if (av[0] == bv[0]) {         cout <<  SMALL  << endl;         cout << 0 << endl;         cout << endl;       } else {         cout <<  IMPOSSIBLE  << endl;       }       return 0;     }     if (len == 2) {       int64_t total = 0;       vector<pair<char, int64_t>> buf;       string res;       if (av[0] > av[1]) {         swap(av[0], av[1]);         res +=  R ;       }       while (1) {         if (bv[0] > bv[1]) {           swap(bv[0], bv[1]);           buf.push_back({ R , 1});         }         if (bv[0] == av[0]) {           if (bv[1] >= av[1] && ((bv[1] - av[1]) % bv[0]) == 0) {             buf.push_back({ P , (bv[1] - av[1]) / bv[0]});             total += (bv[1] - av[1]) / bv[0];           } else {             total = -1;           }           break;         }         if (bv[0] == bv[1]) {           total = -1;           break;         }         buf.push_back({ P , bv[1] / bv[0]});         bv[1] = bv[1] % bv[0];         total += buf.back().second;         if (bv[1] == 0) {           bv[1] = bv[0];           total--;           buf.back().second--;         }       }       if (total < 0) {         cout <<  IMPOSSIBLE  << endl;         return 0;       }       if (total > 200000) {         cout <<  BIG  << endl;         cout << total << endl;       } else {         cout <<  SMALL  << endl;         for (auto it = buf.rbegin(); it != buf.rend(); ++it) {           for (int i = 0; i < it->second; ++i) {             res += it->first;           }         }         cout << res.size() << endl;         cout << res << endl;       }       return 0;     }     int64_t total = 0;     string res;     while (1) {       if (Equal(av, bv)) {         break;       }       if (Requal(av, bv)) {         res +=  R ;         break;       }       if (bv[0] > bv[1]) {         reverse(begin(bv), end(bv));         res +=  R ;       }       if (isAscending(bv) and bv[0] > 0) {         for (int i = len; i-- > 1;) {           bv[i] -= bv[i - 1];         }         res +=  P ;         ++total;       } else {         res =   ;         break;       }     }     if (res ==   ) {       cout <<  IMPOSSIBLE  << endl;     } else if (total > 200000) {       cout <<  BIG  << endl;       cout << total << endl;     } else {       cout <<  SMALL  << endl;       reverse(begin(res), end(res));       cout << res.size() << endl;       cout << res << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   queue<int> fila;   struct G {     double d;     int m, p, r;   } g[250002];   bool mk[250002];   int a2[9000000];   bool cmp2(int a, int b) { return g[a].d < g[b].d; }   int pos = 0;   int ind = 0;   struct T {     int m;     int* e;     void pdt(int a, int* b) { m = a, e = b; }   } mas[2 * 250002];   bool cmp(T a, T b) { return a.m < b.m; }   int ls[4 * 250002];   int s[4 * 250002][2];   int r[4 * 250002];   int build(int a, int b, int f) {     int p = ind++;     ls[p] = pos;     r[p] = b;     int i = ls[f];     for (int i = ls[f]; a2[i] >= 0; i++) {       if (g[a2[i]].m >= a && g[a2[i]].m <= b) a2[pos] = a2[i], pos++;     }     a2[pos] = -1;     pos++;     if (a != b)       s[p][0] = build(a, (a + b) / 2, p),       s[p][1] = build(((a + b) / 2) + 1, b, p);     else       s[p][0] = s[p][1] = -1;     return p;   }   void get(int p, int power, int ray) {     if (r[p] <= power) {       for (int i = ls[p]; a2[i] >= 0; i++) {         if (g[a2[i]].d <= ray + 1e-2) {           if (!mk[a2[i]]) fila.push(a2[i]), mk[a2[i]] = true;           ls[p] = i + 1;         } else           break;       }     } else {       get(s[p][0], power, ray);       if (power > r[s[p][0]]) get(s[p][1], power, ray);     }   }   int main() {     double x0, y0;     int pp, rr, n;     scanf( %lf %lf %d %d %d , &x0, &y0, &pp, &rr, &n);     int tot = 0;     mas[tot++].pdt(pp, &pp);     double x, y;     for (int i = 0; i < n; i++) {       scanf( %lf %lf %d %d %d , &x, &y, &g[i].m, &g[i].p, &g[i].r);       x -= x0;       y -= y0;       g[i].d = sqrt(x * x + y * y);       mk[i] = false;       a2[i] = i;       mas[tot++].pdt(g[i].m, &g[i].m), mas[tot++].pdt(g[i].p, &g[i].p);     }     a2[n] = -1;     sort(mas, mas + tot, cmp);     int v = 1;     int tmp = mas[0].m;     *mas[0].e = v;     for (int i = 1; i < tot; i++) {       if (mas[i].m != tmp) v++;       tmp = mas[i].m;       *mas[i].e = v;     }     sort(a2, a2 + n, cmp2);     r[0] = v;     ls[0] = 0;     ind = 1;     pos = n + 1;     if (n >= 2) s[0][0] = build(1, v / 2, 0), s[0][1] = build((v / 2) + 1, v, 0);     get(0, pp, rr);     int resp = 0;     while (fila.size()) {       resp++;       int u = fila.front();       fila.pop();       get(0, g[u].p, g[u].r);     }     printf( %d n , resp);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int run(int n) {     int i, a[n];     for (i = 0; i < n; i++) cin >> a[i];     sort(a, a + i);     int ans = 0;     int tempCount = 0;     for (i = 0; i < n; i++) {       tempCount++;       if (a[i] == tempCount) {         ans++;         tempCount = 0;       }     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     int n;     while (t--) {       cin >> n;       cout << run(n) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, k;     cin >> t;     while (t--) {       cin >> n >> k;       for (int i = 0; i < n; ++i) cout << (char)(i % k +  a );       cout << endl;     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   namespace io {   template <typename T>   inline void read(T &x) {     bool flag = false;     char c = getchar();     while ((c <  0  || c >  9 ) && c !=  - ) c = getchar();     c ==  -  ? flag = true, x = 0 : x = c -  0 ;     c = getchar();     while (c >=  0  && c <=  9 ) x = (x << 3) + (x << 1) + c -  0 , c = getchar();     if (flag) x = -x;   }   template <typename T>   inline void write(T x) {     char st[10010];     int cnt = 0;     if (x < 0) x = -x, putchar( - );     if (x == 0) putchar( 0 );     while (x) {       st[++cnt] = x % 10 + 48;       x /= 10;     }     while (cnt) {       putchar(st[cnt--]);     }   }   }  // namespace io   using namespace io;   int main() {     int a, ans = 0;     read(a);     while (a) {       ans += (a % 8 == 1);       a /= 8;     }     write(ans);     puts(  );     return 0;   }


#include <bits/stdc++.h>   const int N = 300010;   const int MOD = 1e9 + 7;   struct xxx {     int id;     char c;   } s[N];   std::string a, b, c;   int gap, n;   int sa[N], lcp[N], rank[N], tmp[N], bucket[N], cnt[N][3], ans[N], dp[19][N],       fw[N];   void update(int i, int v) {     while (i < N) {       fw[i] = (fw[i] + v) % MOD;       i += i & -i;     }   }   int query(int i) {     int res = 0;     while (i) {       res = (res + fw[i]) % MOD;       i -= i & -i;     }     if (res < 0) res += MOD;     return res;   }   int rmq(int f, int t) {     int k = 31 - __builtin_clz(t - f + 1);     int x = dp[k][f];     int y = dp[k][t - (1 << k) + 1];     if (lcp[x] < lcp[y]) return x;     return y;   }   void solve(int f, int t, int h) {     if (t - f + 1 < 3) return;     int m = rmq(f, t - 1);     int c0 = cnt[t][0] - cnt[f - 1][0];     int c1 = cnt[t][1] - cnt[f - 1][1];     int c2 = cnt[t][2] - cnt[f - 1][2];     int v = lcp[m];     int num = 1ll * c0 * c1 % MOD * c2 % MOD;     update(v + 1, -num);     update(h + 1, num);     solve(f, m, v);     solve(m + 1, t, v);   }   bool cmp2(int i, int j) {     i += gap;     j += gap;     if (i < n && j < n) return rank[i] < rank[j];     return i > j;   }   bool cmp(int i, int j) {     if (rank[i] != rank[j]) return rank[i] < rank[j];     i += gap;     j += gap;     if (i < n && j < n) return rank[i] < rank[j];     return i > j;   }   int main() {     std::cin >> a >> b >> c;     int an = int(a.size()), bn = int(b.size()), cn = int(c.size());     for (int i = 0; i < an; ++i) s[i].id = 0, s[i].c = a[i];     for (int i = 0; i < bn; ++i) s[i + an + 1].id = 1, s[i + an + 1].c = b[i];     for (int i = 0; i < cn; ++i)       s[i + an + bn + 2].id = 2, s[i + an + bn + 2].c = c[i];     s[an].c = 1;     s[an + bn + 1].c = 2;     n = an + bn + cn + 2;     int rn = 255;     for (int i = 0; i < n; ++i) {       sa[i] = i;       rank[i] = s[i].c;     }     for (gap = 1;; gap <<= 1) {       int p = 0;       for (int i = n - 1; i + gap >= n; --i) tmp[p++] = i;       std::fill(bucket, bucket + rn, 0);       for (int i = 0; i + gap < n; ++i) ++bucket[rank[i + gap]];       for (int i = 1; i < rn; ++i) bucket[i] += bucket[i - 1];       for (int i = 0; i + gap < n; ++i) tmp[--bucket[rank[i + gap]] + p] = i;       std::fill(bucket, bucket + rn, 0);       for (int i = 0; i < n; ++i) ++bucket[rank[i]];       for (int i = 1; i < rn; ++i) bucket[i] += bucket[i - 1];       for (int i = n - 1; i >= 0; --i) sa[--bucket[rank[tmp[i]]]] = tmp[i];       tmp[0] = 0;       for (int i = 1; i < n; ++i) tmp[i] = tmp[i - 1] + cmp(sa[i - 1], sa[i]);       for (int i = 0; i < n; ++i) rank[sa[i]] = tmp[i];       if (tmp[n - 1] == n - 1) break;       rn = tmp[n - 1] + 1;     }     for (int i = 0, k = 0; i < n; ++i) {       if (rank[i] == n - 1) continue;       for (int j = sa[rank[i] + 1]; s[i + k].c == s[j + k].c;) ++k;       lcp[rank[i]] = k;       if (k) --k;     }     for (int i = 0; i < n - 1; ++i) dp[0][i] = i;     for (int j = 1; 1 << j < n; ++j)       for (int i = 0; i + (1 << j) - 1 < n - 1; ++i) {         int x = dp[j - 1][i];         int y = dp[j - 1][i + (1 << (j - 1))];         dp[j][i] = lcp[x] < lcp[y] ? x : y;       }     assert(s[sa[0]].c == 1);     assert(s[sa[1]].c == 2);     for (int i = 2; i < n; ++i) {       cnt[i][0] = cnt[i - 1][0];       cnt[i][1] = cnt[i - 1][1];       cnt[i][2] = cnt[i - 1][2];       ++cnt[i][s[sa[i]].id];     }     int sz = std::min(an, std::min(bn, cn));     solve(2, n - 1, 0);     for (int i = 1; i <= sz; ++i) std::cout << query(i) <<    ;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1073741824 )   using namespace std;   const int N = 3e5 + 100;   int col[N], d[N][2];   vector<int> v[N];   int main() {   #pragma warning(disable : 4996)     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       int a, b;       scanf( %d%d , &a, &b);       a--, b--;       v[a].push_back(b);       v[b].push_back(a);     }     queue<int> q;     for (int i = 0; i < n; i++) {       d[i][0] = v[i].size();       if (d[i][0] > 1) q.push(i);     }     while (!q.empty()) {       int node = q.front();       q.pop();       if (d[node][col[node]] > 1) {         col[node] ^= 1;         for (int to : v[node]) {           d[to][col[node]]++;           d[to][col[node] ^ 1]--;           if (d[to][col[to]] > 1) q.push(to);         }       }     }     for (int i = 0; i < n; i++) printf( %d , col[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000005;   const long long mod = 1e9 + 7;   const int N = 1e6;   vector<int> rec[maxn];   long long fac[maxn], ans;   int n, m;   int ord[maxn], bit;   bool comp(int a, int b) { return rec[a][bit] < rec[b][bit]; }   void mysort(int l, int r, int j) {     if (l == r) return;     bit = j;     sort(ord + l, ord + r + 1, comp);     int p = l;     while (p <= r && rec[ord[p]][j] == -1) ++p;     int cnt = 1;     for (int i = l + 1; i < p; i++)       if (rec[ord[i]][j - 1] == rec[ord[i - 1]][j - 1])         ++cnt;       else {         (ans *= fac[cnt]) %= mod;         cnt = 1;       }     (ans *= fac[cnt]) %= mod;     int t = p;     while (t <= r) {       p = t;       while (p <= r && rec[ord[p]][j] == rec[ord[t]][j]) ++p;       mysort(t, p - 1, j + 1);       t = p;     }   }   int main() {     fac[0] = 1ll;     for (int i = 1; i <= N; i++) fac[i] = fac[i - 1] * i % mod;     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) rec[i].push_back(0);     for (int g, i = 1; i <= n; i++) {       scanf( %d , &g);       for (int v, j = 1; j <= g; j++) scanf( %d , &v), rec[v].push_back(i);     }     for (int i = 1; i <= m; i++) {       sort(rec[i].begin(), rec[i].end());       rec[i].push_back(-1);       ord[i] = i;     }     ans = 1ll;     mysort(1, m, 1);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Power(int base, int exp) {     int ans = 1;     while (exp > 0) {       if (exp & 1) ans = (1LL * ans * base) % 1000000007;       exp = exp >> 1;       base = (1LL * base * base) % 1000000007;     }     return ans;   }   vector<map<long double, int> > p_CntS(2005);   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long int n;     cin >> n;     long double x[n], y[n];     for (int i = 0; i < n; i += 1) cin >> x[i] >> y[i];     long double slope;     for (int p1 = 0; p1 < n; p1 += 1)       for (int p2 = 0; p2 < n; p2 += 1) {         if (p1 == p2) continue;         slope = y[p2] - y[p1];         if (x[p2] - x[p1] == 0)           p_CntS[p1][600.0]++;         else           p_CntS[p1][300.0 + slope / (x[p2] - x[p1])]++;       }     long long int ans = (1LL * n * (n - 1) * (n - 2)) / 6, ans_ = 0;     int cnt;     for (int i = 0; i < n; i += 1) {       map<long double, int>::iterator it = p_CntS[i].begin();       while (it != p_CntS[i].end()) {         cnt = (*it).second;         ans_ += 1LL * cnt * (cnt - 1) / 2;         it++;       }     }     cout << ans - ans_ / 3 <<   n ;   }


#include <bits/stdc++.h>   int ans = 0;   double H = 100, len = 100000;   int F[110000], T[110000], FF[110000], TT[110000];   bool vis[110000];   int judge(bool fuck, double st, double t) {     int ret = 0;     int top, flo;     double point = st;     memset(vis, 0, sizeof(vis));     while (point < len) {       top = (int)ceil(point);       flo = (int)floor(point);       if (fuck) {         if (T[top] < 0 || T[flo] < 0 || T[top] != T[flo]) {           return -1;         }         if (vis[TT[top]] == 1) return -1;         ret += T[top];         vis[TT[top]] = 1;         fuck = !fuck;       } else {         if (F[top] < 0 || F[flo] < 0 || F[top] != F[flo]) {           return -1;         }         if (vis[FF[top]] == 1) return -1;         ret += F[top];         vis[FF[top]] = 1;         fuck = !fuck;       }       point += t;     }     if (ans < ret) ans = ret;     return ret;   }   int work() {     int n, i, j, val, x, y;     char c;     double A, B, t;     double hl, hr;     scanf( %lf%lf%d , &hl, &hr, &n);     memset(T, -1, sizeof(T));     memset(F, -1, sizeof(F));     for (i = 1; i <= n; i++) {       scanf( %d %c%d%d , &val, &c, &x, &y);       if (c ==  T ) {         for (j = x; j <= y; j++) T[j] = val, TT[j] = i;       } else {         for (j = x; j <= y; j++) F[j] = val, FF[j] = i;       }     }     A = hl / H;     B = hr / H;     for (i = 0; i <= 100; i++) {       if ((i & 1) == 0) {         t = len / (A + B + i);         judge(0, A * t, t);       }       if ((i & 1) == 1) {         t = len / ((1 - A) + B + i);         judge(1, (1 - A) * t, t);       }       if ((i & 1) == 1) {         t = len / (A + (1 - B) + i);         judge(0, A * t, t);       }       if ((i & 1) == 0) {         t = len / ((1 - A) + (1 - B) + i);         judge(1, (1 - A) * t, t);       }     }     printf( %d n , ans);     return 1;   }   int main() {     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct point {     int x, y;     point() {}     point(int x, int y) : x(x), y(y) {}   } p[1010];   point operator-(const point &a, const point &b) {     return point(a.x - b.x, a.y - b.y);   }   long long operator*(const point &a, const point &b) {     return (long long)a.x * b.y - (long long)a.y * b.x;   }   int main() {     int n, i, x, y, low, high;     cin >> n;     for (i = 0; i < n; i++) {       cin >> x >> y;       p[i] = point(x, y);     }     low = p[0].x;     high = p[1].x;     if (low > high) swap(low, high);     p[n] = p[0];     for (i = 0; i < n; i++) {       while (low <= high && (p[i + 1] - p[i]) * (point(low, p[0].y) - p[i]) > 0)         low++;       while (low <= high && (p[i + 1] - p[i]) * (point(high, p[0].y) - p[i]) > 0)         high--;     }     cout << high - low + 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500000;   const long long minx = -0x5fffffffffffffff;   struct nn {     long long x, y;     nn() { y = minx; }   };   int n, u, v;   nn num[N];   vector<int> node[N];   long long dfs(int f, int x) {     for (int i = 0; i < node[x].size(); i++) {       if (node[x][i] == f) continue;       long long r = dfs(x, node[x][i]);       num[x].y += r;     }     return num[x].y;   }   long long dfs1(int f, int x, long long &ans) {     long long m1 = minx;     long long m2 = minx;     for (int i = 0; i < node[x].size(); i++) {       if (node[x][i] == f) continue;       long long r = dfs1(x, node[x][i], ans);       if (m1 < r) {         m2 = m1;         m1 = r;       } else if (m2 < r) {         m2 = r;       }     }     if (m2 != minx) {       if (ans < m1 + m2) ans = m1 + m2;     }     return m1 > num[x].y ? m1 : num[x].y;   }   int main() {     long long ans;     while (scanf( %d , &n) != EOF) {       ans = minx;       for (int i = 0; i < N; i++) node[i].clear();       for (int i = 1; i <= n; i++) {         scanf( %lld , &num[i].x);         num[i].y = num[i].x;       }       for (int i = 0; i < n - 1; i++) {         scanf( %d%d , &u, &v);         node[u].push_back(v);         node[v].push_back(u);       }       dfs(0, 1);       dfs1(0, 1, ans);       if (ans != minx)         printf( %lld n , ans);       else         printf( Impossible n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n, k;     cin >> n >> k;     if (k > (n * n + 1) / 2) {       cout <<  NO ;       return 0;     }     cout <<  YES n ;     int tmp = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (j % 2 == i % 2 && tmp != k) {           cout <<  L ;           tmp++;         } else           cout <<  S ;       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   using namespace chrono;   const int infinity = (int)1e9 + 42;   const int64_t llInfinity = (int64_t)1e18 + 256;   const int module = (int)1e9 + 7;   const long double eps = 1e-8;   mt19937_64 randGen(system_clock().now().time_since_epoch().count());   inline void raiseError(string errorCode) {     cerr <<  Error :   << errorCode << endl;     exit(42);   }   inline int readAns(int req) {     cout << req << endl;     int q;     cin >> q;     if (q == 0) {       exit(0);     }     return q;   }   signed main() {     ios_base::sync_with_stdio(false);     int m, n;     cin >> m >> n;     vector<int> v(n);     for (int i = 0; i < n; i++) {       v[i] = readAns(m) * -1;     }     int l = 1, r = m - 1;     int p = 0;     while (l != r) {       int m = (l + r) / 2;       int ans = readAns(m) * v[p];       p++;       p %= n;       if (ans < 0) {         r = m - 1;       } else {         l = m + 1;       }     }     readAns(l);     assert(false);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     string s, t;     cin >> n >> m >> s >> t;     bool can = false;     if (s == t)       can = true;     else {       if (n <= m + 1) {         for (int i = 0; i < int(n); i++) {           if (s[i] ==  * ) {             if (s.substr(0, i) == t.substr(0, i) &&                 s.substr(i + 1, string::npos) ==                     t.substr(m - (n - (i + 1)), string::npos))               can = true;           }         }       }     }     cout << (can ?  YES  :  NO ) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[200001], b[200001], c[200001];   int main() {     long long n, ans = 0;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     for (int i = 0; i < n; i++) c[i] = a[i] - b[i];     sort(c, c + n);     for (int i = n - 1;; i--) {       if (c[i] <= 0) break;       int ans1 = &c[i] - upper_bound(c, c + n, 0 - c[i]);       ans += ans1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a, b;     if (n == 2) {       cout << 2 << endl;     } else       cout << 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   unsigned long long preLef[N], preRig[N], sum[N], a[N];   map<unsigned long long, unsigned long long> f, g;   int n, m;   unsigned long long Rand() {     return (unsigned long long)rand() * rand() * rand() +            (unsigned long long)rand() * rand() * rand();   }   int main() {     srand(time(0));     scanf( %d%d , &n, &m);     for (int i = 0, l, r; i < n; ++i) {       scanf( %d%d , &l, &r);       unsigned long long t = Rand();       a[l] ^= t;       a[r + 1] ^= t;       preLef[l] ^= t;       preRig[r] ^= t;     }     for (int i = 1; i <= m + 1; ++i) {       a[i] ^= a[i - 1];       sum[i] = sum[i - 1] ^ a[i];       preLef[i] ^= preLef[i - 1];       preRig[i] ^= preRig[i - 1];     }     g[0] = 0;     f[0] = 1;     unsigned long long ans = 0;     for (int i = 1; i <= m; ++i) {       unsigned long long t = sum[i] ^ preLef[i];       ans += f[t] * i - g[t];       t = sum[i] ^ preRig[i];       g[t] += i;       ++f[t];     }     for (int i = 1, lf = 0; i <= m; ++i)       if (!a[i])         ans -= (unsigned long long)(i - lf + 1) * (i - lf) / 2;       else         lf = i;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e4 + 5;   int n, a[N];   int main() {     ios ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     if (fopen( main.in ,  r )) freopen( main.in ,  r , stdin);     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     int cnt = 0;     for (int i = 2; i < n; i++)       if (a[i - 1] == 1 && a[i + 1] == 1 && a[i] == 0) a[i + 1] = 0, cnt++;     cout << cnt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[10];   int main() {     int n, i, len;     long long wi = 1, he = 1;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %s , s);       len = strlen(s);       if (len == 4) {         wi++;         he++;       } else if (!strcmp(s,  UL ) || !strcmp(s,  DR ))         wi++;       else if (!strcmp(s,  UR ) || !strcmp(s,  DL ))         he++;     }     printf( %I64d n , wi * he);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string rotate(string s, int off, int* a) {     string new_s(s);     for (int i = 0; i < 4; i++) new_s[a[(i + off) % 4] - 1] = s[a[i] - 1];     return new_s;   }   string rotate1(string s, int off) {     int a[] = {1, 3, 5, 6};     return rotate(s, off, a);   }   string rotate2(string s, int off) {     int a[] = {2, 6, 4, 3};     return rotate(s, off, a);   }   int main() {     string s;     cin >> s;     sort(s.begin(), s.end());     map<string, int> m;     int ans = 0;     do {       bool flg = true;       map<string, int> mm;       for (int i = 0; i < 4; i++) {         string b = rotate1(s, i);         for (int j = 0; j < 4; j++) {           string bb = rotate2(b, j);           if (mm.find(bb) == mm.end())             if (m.find(bb) != m.end()) flg = false;           mm[bb] = m[bb] = 1;         }       }       if (flg) ans++;     } while (next_permutation(s.begin(), s.end()));     cout << ans;     return 0;   }


// Problem: A. Searching Local Minimum   // Contest: Codeforces - Codeforces Round #700 (Div. 1)   // URL: https://codeforces.com/contest/1479/problem/A   // Memory Limit: 512 MB   // Time Limit: 2000 ms   // Powered by CP Editor (https://github.com/cpeditor/cpeditor)      #include <iostream>   #include <iosfwd>   #include <iomanip>   #include <cstdio>   #include <cstring>   #include <cstdlib>   #include <ctime>   #include <cmath>   #include <cassert>   #include <cctype>   #include <climits>   #include <vector>   #include <bitset>   #include <set>   #include <queue>   #include <math.h>   #include <stack>   #include <map>   #include <deque>   #include <string>   #include <list>   #include <iterator>   #include <sstream>   #include <complex>   #include <fstream>   #include <functional>   #include <numeric>   #include <utility>   #include <algorithm>   #include <assert.h>   #include <unordered_map>   #pragma GCC optimize ( -O3 )      using namespace std;      #define watch(x) cout << (#x) <<   =   << (x) << endl   #define PI double(2 * acos(0.0))   #define LL long long   #define MOD 1000000007   #define all(x) (x).begin(), (x).end()   #define INF 1e15   int query(int a, int n){         if(a == 0 || a == n + 1){               return 1e7;         }         cout <<  ?   << a << endl;         int p;         cin >> p;         return p;   }   int helper(int a, int n){         int left = a - 1;         int right = a + 1;         int mid = a;         int p = query(left, n);         int q = query(mid, n);         int r = query(right, n);         if(p > q && q < r){               return 0;         }         if(p < q){               return 2;         }         else{               return 1;         }   }   int main() {         int n;         cin >> n;         int start = 1;         int end = n;         int ans = -1;         while(start <= end){               int mid = (start + end) / 2;               int p = helper(mid, n);               if(p == 0){                     ans = mid;                     break;               }               if(p == 1){                     start = mid + 1;               }else{                     end = mid - 1;               }         }         cout <<  !   << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = INT_MAX;   const long long INFL = LLONG_MAX;   const long double pi = acos(-1);   class SumSegTree {    private:     vector<long long> sum, lazy;     int N;     void check_index(int i) {       if (i < 0 || N <= i) {         throw  index out of bound ;       }     }       public:     SumSegTree(int N) {       this->N = 1;       while (this->N < N) this->N <<= 1;       this->sum.resize(this->N << 1);       this->lazy.resize(this->N << 1);     }     long long get_sum(int l, int r) {       check_index(l);       check_index(r);       return get_sum(l, r, 0, N - 1, 1);     }     void add(int l, int r, int val) {       check_index(l);       check_index(r);       add(l, r, val, 0, N - 1, 1);     }     void delegate(int a, int b, int n) {       sum[n] += (b - a + 1) * lazy[n];       if (a < b) {         lazy[n * 2] += lazy[n];         lazy[n * 2 + 1] += lazy[n];       }       lazy[n] = 0;     }     long long get_sum(int l, int r, int a, int b, int n) {       if (b < l || r < a) return 0;       if (lazy[n] != 0) delegate(a, b, n);       if (l <= a && b <= r) return sum[n];       int m = (a + b) / 2;       return get_sum(l, r, a, m, n * 2) + get_sum(l, r, m + 1, b, n * 2 + 1);     }     void add(int l, int r, long long val, int a, int b, int n) {       if (b < l || r < a) return;       if (l <= a && b <= r) {         if (a == b)           sum[n] += val;         else           lazy[n] += val;       } else {         int m = (a + b) / 2;         if (lazy[n] != 0) delegate(a, b, n);         sum[n] += (min(b, r) - max(a, l) + 1) * val;         add(l, r, val, a, m, n * 2);         add(l, r, val, m + 1, b, n * 2 + 1);       }     }   };   int N, M;   vector<int> adj[300100];   vector<pair<int, long long> > query[300100];   SumSegTree segt(300005);   long long ans[300100];   void dfs(int u, int prev, int h) {     for (int(i) = 0; (i) < (int((query[u]).size())); (i)++) {       int dist = query[u][i].first;       long long value = query[u][i].second;       segt.add(h, min(N, h + dist), value);     }     ans[u] = segt.get_sum(h, h);     for (int(i) = 0; (i) < (int((adj[u]).size())); (i)++) {       int v = adj[u][i];       if (v == prev) continue;       dfs(v, u, h + 1);     }     for (int(i) = 0; (i) < (int((query[u]).size())); (i)++) {       int dist = query[u][i].first;       long long value = query[u][i].second;       segt.add(h, min(N, h + dist), -value);     }   }   int main() {     ios_base::sync_with_stdio(0);     cout.precision(15);     cout << fixed;     cout.tie(0);     cin.tie(0);     cin >> N;     for (int(i) = 1; (i) <= (N - 1); (i)++) {       int a, b;       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);     }     cin >> M;     for (int(i) = 1; (i) <= (M); (i)++) {       int v, d;       long long x;       cin >> v >> d >> x;       query[v].push_back(make_pair(d, x));     }     dfs(1, 0, 0);     for (int(i) = 1; (i) <= (N); (i)++)       cout << ans[i] << (i + 1 <= N ?     :   n );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     pair<int, int> home, uni;     cin >> home.first >> home.second >> uni.first >> uni.second;     int count;     cin >> count;     int solution = 0;     int64_t a, b, c;     for (int i = 0; i < count; i++) {       cin >> a >> b >> c;       bool h_p = a * home.first + b * home.second + c > 0;       bool u_p = a * uni.first + b * uni.second + c > 0;       if ((h_p && !u_p) || (!h_p && u_p)) {         solution++;       }     }     cout << solution << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n;     char str[102][102];     scanf( %d , &n);     bool row, col;     row = col = false;     for (int i = 0; i < n; i++) {       scanf( %s , str[i]);       bool bolong = false;       for (int j = 0; j < n; j++) {         if (str[i][j] ==  . ) {           bolong = true;         }       }       if (!bolong) {         row = true;       }     }     if (!row) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           if (str[i][j] ==  . ) {             printf( %d %d n , i + 1, j + 1);             break;           }         }       }       return 0;     }     for (int i = 0; i < n; i++) {       bool bolong = false;       for (int j = 0; j < n; j++) {         if (str[j][i] ==  . ) bolong = true;       }       if (!bolong) col = true;     }     if (!col) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           if (str[j][i] ==  . ) {             printf( %d %d n , j + 1, i + 1);             break;           }         }       }     } else       printf( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937_64 rng(       (unsigned)chrono::system_clock::now().time_since_epoch().count());   const long double error = 1e-7;   const long double PI = acosl(-1);   const int nmax = 2e5 + 10;   int master[nmax];   int sz[nmax];   void init(int u) {     master[u] = u;     sz[u] = 1;     return;   }   int fnd(int u) {     if (master[u] == u) return u;     return master[u] = fnd(master[u]);   }   bool unite(int u, int v) {     u = fnd(u);     v = fnd(v);     if (u == v) return false;     if (sz[u] < sz[v]) swap(u, v);     sz[u] += sz[v];     master[v] = u;     return true;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int n, m;     cin >> n >> m;     vector<long long int> A(n + 1), B(m + 1);     for (int i = 1; i <= n; i++) {       cin >> A[i];     }     for (int i = 1; i <= m; i++) {       cin >> B[i];     }     vector<pair<long long int, pair<int, int>>> edges;     long long int totcost = 0;     for (int i = 1; i <= n; i++) {       int k;       cin >> k;       for (int j = 0; j < k; j++) {         int x;         cin >> x;         edges.emplace_back(A[i] + B[x], make_pair(i, n + x));         totcost += A[i] + B[x];       }     }     for (int i = 1; i <= n + m; i++) init(i);     sort(edges.begin(), edges.end());     reverse(edges.begin(), edges.end());     long long int ans = 0;     for (auto e : edges) {       if (unite(e.second.first, e.second.second)) ans += e.first;     }     ans = totcost - ans;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int L[100010];   int P[100010][20];   vector<int> pp[100010][20];   int T[100010];   vector<int> tt[100010];   int arr[100010];   vector<int> graph[100010];   vector<int> fnc(vector<int> &a, vector<int> &b) {     vector<int> ans;     int aa = a.size(), bb = b.size(), i, j, shajib = 0;     for (i = 0, j = 0; i + j < aa + bb && shajib < 10;) {       if (i == aa) {         ans.push_back(b[j]);         j++;         shajib++;       } else if (j == bb) {         ans.push_back(a[i]);         i++;         shajib++;       } else {         if (a[i] == b[j]) {           i++;         } else if (a[i] > b[j]) {           ans.push_back(b[j]);           j++;           shajib++;         } else {           ans.push_back(a[i]);           i++;           shajib++;         }       }     }     return ans;   }   void dfs(int src, int par) {     if (src == -1)       L[par] = 1;     else       L[par] = L[src] + 1;     T[par] = src;     int len = graph[par].size();     for (int i = 0; i < (int)graph[par].size(); i++) {       int v = graph[par][i];       if (v != src) {         dfs(par, v);       }     }   }   vector<int> query(int n, int p, int q) {     int log = 1, i, j, temp;     if (L[p] < L[q]) {       temp = q;       q = p;       p = temp;     }     log = log2(n);     vector<int> os;     os = fnc(os, tt[p]);     for (j = log; j >= 0; j--) {       if (L[p] - (1 << j) >= L[q]) {         os = fnc(os, pp[p][j]);         p = P[p][j];       }     }     if (p == q) return os;     os = fnc(os, tt[q]);     for (j = log; j >= 0; j--) {       if (P[p][j] != -1 && P[p][j] != P[q][j]) {         os = fnc(os, pp[p][j]);         os = fnc(os, pp[q][j]);         p = P[p][j], q = P[q][j];       }     }     os = fnc(os, pp[p][0]);     return os;   }   void lca(int n) {     memset(P, -1, sizeof(P));     int i, j;     for (i = 1; i <= n; i++) {       P[i][0] = T[i];       if (T[i] != -1) pp[i][0] = fnc(pp[i][0], tt[T[i]]);     }     for (j = 1; (1 << j) < n; j++) {       for (i = 1; i <= n; i++) {         if (P[i][j - 1] != -1) {           P[i][j] = P[P[i][j - 1]][j - 1];           pp[i][j] = fnc(pp[i][j], pp[P[i][j - 1]][j - 1]);           pp[i][j] = fnc(pp[i][j], pp[i][j - 1]);         }       }     }   }   int main() {     int i, j, n, m, t, a, b, k, c;     scanf( %d%d%d , &n, &m, &k);     for (i = 0; i < n - 1; i++) {       scanf( %d%d , &a, &b);       graph[a].push_back(b);       graph[b].push_back(a);     }     for (i = 1; i <= m; i++) {       scanf( %d , &a);       tt[a].push_back(i);     }     for (i = 1; i <= n; i++) {       sort(tt[i].begin(), tt[i].end());       a = tt[i].size();       while (a > 10) {         tt[i].pop_back();         a--;       }     }     dfs(-1, 1);     lca(n);     vector<int> os;     for (i = 1; i <= k; i++) {       scanf( %d%d%d , &a, &b, &c);       os = query(n, a, b);       c = min(c, (int)os.size());       a = 0;       printf( %d  , c);       for (auto it = os.begin(); a < c && it != os.end(); a++, it++) {         printf( %d  , (*it));       }       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[107];   map<string, int> f;   vector<pair<int, string>> b;   bool cmp(pair<int, string> u, pair<int, string> v) { return u.first > v.first; }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= m; i++) {       string s;       cin >> s;       if (f[s] == 0)         f[s] = 1;       else         f[s]++;     }     sort(a + 1, a + n + 1);     for (map<string, int>::iterator it = f.begin(); it != f.end(); ++it) {       b.push_back(make_pair(it->second, it->first));     }     sort(b.begin(), b.end(), cmp);     long long minP = 0, maxP = 0;     for (int i = 0; i < b.size(); i++) {       minP += b[i].first * a[i + 1];       maxP += b[i].first * a[n - i];     }     cout << minP <<     << maxP << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     long long cos, rlev;     bool operator<(const node &a) const { return cos < a.cos; }   } f[300005];   int main() {     int n, k;     while (scanf( %d%d , &n, &k) != EOF) {       for (int i = 1; i <= n; i++) {         scanf( %I64d , &f[i].cos);         f[i].rlev = i;       }       priority_queue<node> p;       long long ans = 0;       for (int i = 1; i <= k; i++) p.push(f[i]);       for (int i = k + 1; i <= k + n; i++) {         if (i <= n) p.push(f[i]);         node now = p.top();         p.pop();         if (i >= now.rlev) {           ans += (i - now.rlev) * now.cos;           f[now.rlev].rlev = i;         }       }       printf( %I64d n , ans);       for (int i = 1; i < n; i++) {         printf( %I64d  , f[i].rlev);       }       printf( %I64d n , f[n].rlev);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mul(long long a, long long b, long long p = 1000000007) {     return ((a % p) * (b % p)) % p;   }   long long add(long long a, long long b, long long p = 1000000007) {     return (a % p + b % p) % p;   }   void input(long long a[], long long sz) {     for (long long i = 0; i < sz; i++) cin >> a[i];   }   void print(long long a[], long long sz) {     for (long long i = 0; i < sz; i++) {       if (i == sz - 1)         cout << a[i] <<   n ;       else         cout << a[i] <<    ;     }   }   long long maxr(long long a[], long long sz) {     long long ma;     for (long long i = 0; i < sz; i++) {       if (i == 0)         ma = a[i];       else if (a[i] > ma)         ma = a[i];     }     return ma;   }   long long minr(long long a[], long long sz) {     long long mi;     for (long long i = 0; i < sz; i++) {       if (i == 0)         mi = a[i];       else if (a[i] < mi)         mi = a[i];     }     return mi;   }   long long isprm(long long n) {     if (n <= 1) return 0;     if (n <= 3) return 1;     if (n % 2 == 0 || n % 3 == 0) return 0;     for (int i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return 0;     return 1;   }   long long power(long long x, long long y, long long p = 1000000007) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long modInverse(long long n, long long p = 1000000007) {     return power(n, p - 2, p);   }   long long ncrMod(long long n, long long r, long long p = 1000000007) {     if (r == 0) return 1;     long long fac[n + 1];     fac[0] = 1;     for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;     return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %            p;   }   bool comp(const pair<int, int> &a, const pair<int, int> &b) {     if (a.first == b.first) {       return (a.second > b.second);     }     return (a.first < b.first);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       long long q = n / k;       long long r = n % k;       if ((r % 2 == 1 && k % 2 == 0) || (r % 2 == 1 && q == 1) || q == 0)         cout <<  NO n ;       else {         cout <<  YES n ;         if (r % 2 == 0) {           cout << q + r <<    ;           for (long long i = 0; i < k - 1; i++) cout << q <<    ;           cout <<   n ;         } else {           cout << q + r <<    ;           for (long long i = 0; i < k - 1; i++) {             if (i % 2 == 0)               cout << q - 1 <<    ;             else               cout << q + 1 <<    ;           }           cout <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 20;   struct edge {     int v, l, r;     edge() {}     edge(int a, int b, int c) {       v = a;       l = b;       r = c;     }   };   struct nd {     int no, lim, to;     nd() {}     nd(int a, int b, int c) {       no = a;       lim = b;       to = c;     }   };   bool operator<(const nd& a, const nd& b) {     if (a.no != b.no) {       return a.no > b.no;     }     if (a.lim != b.lim) {       return a.lim > b.lim;     }     return a.to < b.to;   }   bool operator<(const edge& a, const edge& b) {     if (a.l != b.l) {       return a.l < b.l;     }     return a.r < b.r;   }   vector<edge> G[2 * maxn];   int now[2 * maxn];   int main() {     int n, m, u, v, l, r;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d%d%d , &u, &v, &l, &r);       if (l & 1) {         if (r & 1) {           G[u].push_back(edge(v + n, l + 1, r - 1));           G[v].push_back(edge(u + n, l + 1, r - 1));           G[u + n].push_back(edge(v, l, r - 2));           G[v + n].push_back(edge(u, l, r - 2));         } else {           if (l + 1 < r) {             G[u].push_back(edge(v + n, l + 1, r - 2));             G[v].push_back(edge(u + n, l + 1, r - 2));           }           G[u + n].push_back(edge(v, l, r - 1));           G[v + n].push_back(edge(u, l, r - 1));         }       } else {         if (r & 1) {           G[u].push_back(edge(v + n, l, r - 1));           G[v].push_back(edge(u + n, l, r - 1));           if (l + 1 < r) {             G[u + n].push_back(edge(v, l + 1, r - 2));             G[v + n].push_back(edge(u, l + 1, r - 2));           }         } else {           G[u].push_back(edge(v + n, l, r - 2));           G[v].push_back(edge(u + n, l, r - 2));           G[u + n].push_back(edge(v, l + 1, r - 1));           G[v + n].push_back(edge(u, l + 1, r - 1));         }       }     }     for (int i = 1; i <= n + n; i++) {       sort(G[i].begin(), G[i].end());     }     priority_queue<nd> Q;     Q.push(nd(0, 0, 1));     int ans = 0x3f3f3f3f;     nd t;     while (!Q.empty()) {       t = Q.top();       int u = t.to, no = t.no, lim = t.lim;       Q.pop();       if (u == n || u == n * 2) {         ans = min(ans, no);       }       for (int& i = now[u]; i < G[u].size(); i++) {         if (lim < G[u][i].l) {           break;         }         if (no <= G[u][i].r) {           Q.push(nd(max(no + 1, G[u][i].l + 1), G[u][i].r + 1, G[u][i].v));         }       }     }     if (ans == 0x3f3f3f3f) {       printf( -1 n );     } else {       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000008;   int n, m, k, d, i, j, e;   int tmp[N], pos[N], p[N], c[N];   char s[N];   int main() {     scanf( %s %d , s, &m);     n = strlen(s);     while (m--) {       scanf( %d%d , &k, &d);       for (i = 0; i < n; i++) {         c[i] = (i + 1) % n;         pos[i] = p[i] = i;       }       int t = 0;       for (i = 0; i < d; i++)         for (j = i; j < k; t++, j += d) p[t] = j;       for (i = 0; i < n; ++i) {         c[i] = p[c[i]];       }       for (e = n - k + 1; e; e >>= 1) {         for (i = 0; i < n; ++i) tmp[i] = c[i];         if (e & 1)           for (i = 0; i < n; ++i) pos[i] = c[pos[i]];         for (i = 0; i < n; ++i) c[i] = tmp[c[i]];       }       for (i = 0; i < n; ++i) tmp[(i + n - k + 1) % n] = s[pos[i]];       for (i = 0; i < n; ++i) putchar(s[i] = (char)tmp[i]);       putchar(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   vector<int> ke[1000010];   bool cx[1000010], visited[1000010];   void dfs(int u) {     visited[u] = true;     for (int v : ke[u])       if (!visited[v]) dfs(v);   }   long long c2(int x) { return (1LL * x * (x - 1)) >> 1; }   int main() {     if (fopen( a.txt ,  r )) {       freopen( a.txt ,  r , stdin);     } else       ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m;     int own = 0;     for (int i = 1; i <= m; i++) {       int u, v;       cin >> u >> v;       cx[u] = true;       cx[v] = true;       if (u != v) {         ke[u].push_back(v);         ke[v].push_back(u);       } else         own++;     }     int cnt = 0;     for (int u = 1; u <= n; u++)       if (cx[u] && !visited[u]) {         dfs(u);         cnt++;       }     if (cnt != 1) {       cout << 0;       exit(0);     };     long long res = 0;     for (int i = 1; i <= n; i++) res += c2(int(ke[i].size()));     res += c2(own);     res += 1LL * own * (m - own);     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   int get_point(int N) {     int score[10] = {25, 18, 15, 12, 10, 8, 6, 4, 2, 1};     if (N > 10) return 0;     return score[N - 1];   }   int main() {     int T;     map<string, int> n_map;     map<string, string> n_win;     cin >> T;     for (int i = 0; i < (T); ++i) {       int N;       cin >> N;       for (int j = 0; j < (N); ++j) {         string t_s;         cin >> t_s;         n_map[t_s] += get_point(j + 1);         if (n_win[t_s] ==   )           n_win[t_s] =  0000000000000000000000000000000000000000000000000000 ;         n_win[t_s][j] += 1;       }     }     map<string, int>::iterator it;     int n_max1 = 0, n_max2 = 0;     string n_ret1 =   , n_ret2 =   ;     n_win[  ] =  0000000000000000000000000000000000000000000000000000 ;     for (it = n_map.begin(); it != n_map.end(); it++) {       if (n_max1 < (*it).second) {         n_ret1 = (*it).first;         n_max1 = (*it).second;       }       if (n_max1 == (*it).second && n_win[n_ret1] < n_win[(*it).first]) {         n_ret1 = (*it).first;         n_max1 = (*it).second;       }       if (n_win[n_ret2][0] < n_win[(*it).first][0]) {         n_ret2 = (*it).first;         n_max2 = (*it).second;       }       if (n_win[n_ret2][0] == n_win[(*it).first][0] && n_max2 < (*it).second) {         n_ret2 = (*it).first;         n_max2 = (*it).second;       }       if (n_win[n_ret2][0] == n_win[(*it).first][0] && n_max2 == (*it).second) {         if (n_win[n_ret2] < n_win[(*it).first]) {           n_ret2 = (*it).first;           n_max2 = (*it).second;         }       }     }     cout << n_ret1 << endl;     cout << n_ret2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 40;   const int F = N / 2 + N % 2;   const int S = N - F;   const long long N_MASKS = (1ll << N);   const long long F_MASKS = (1ll << F);   const long long S_MASKS = (1ll << S);   double liquid;   int n, f, s;   long long adj[N];   int firstSizes[F_MASKS];   long long firstAvailables[F_MASKS];   int secondSizes[S_MASKS];   long long secondAvailables[S_MASKS];   int maxPossible[S_MASKS];   void readData() {     cin >> n >> liquid;     f = n / 2 + n % 2;     s = n - f;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         long long a;         cin >> a;         adj[i] |= (a << j);       }     }   }   void findCliques(int sta, int cur, int fin, long long mask, int size,                    long long available, int* sizes, long long* availables) {     if (cur == fin) {       return;     }     findCliques(sta, cur + 1, fin, mask, size, available, sizes, availables);     if ((1ll << cur) & available) {       mask |= (1ll << cur);       ++size;       available &= adj[cur];       sizes[mask >> sta] = size;       availables[mask >> sta] = available;       findCliques(sta, cur + 1, fin, mask, size, available, sizes, availables);     }   }   int findPossibles(long long available) {     if (maxPossible[available]) {       return maxPossible[available];     }     if (available == 0) {       return 0;     }     if (secondSizes[available]) {       return maxPossible[available] = secondSizes[available];     }     for (int i = 0; i < s; ++i) {       if (available & (1ll << i)) {         maxPossible[available] =             max(maxPossible[available], findPossibles(available & ~(1ll << i)));       }     }     return maxPossible[available];   }   int findMaxClique() {     findCliques(0, 0, f, 0, 0, (1ll << n) - 1, firstSizes, firstAvailables);     findCliques(f, f, n, 0, 0, (1ll << n) - 1, secondSizes, secondAvailables);     int res = findPossibles((1 << s) - 1);     for (int mask = 0; mask < (1 << f); ++mask) {       if (firstSizes[mask]) {         res = max(res,                   firstSizes[mask] + findPossibles(firstAvailables[mask] >> f));       }     }     return res;   }   double solve() {     int x = findMaxClique();     return 0.5 * liquid * liquid * (x - 1.) / x;   }   int main() {     ios_base::sync_with_stdio(false);     cout << setprecision(20) << fixed;     readData();     cout << solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};   long long power(long long a, long long b) {     long long res = 1;     while (b) {       if (b % 2) res = (res * a) % 5;       b = b >> 1;       a = (a * a) % 5;     }     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long i, n, t;     cin >> n >> t;     long long ans = 1e16;     pair<long long, long long> p;     for (i = 0; i < t; i++) {       long long a, b;       cin >> a >> b;       if (b - a + 1 < ans) {         ans = b - a + 1;       }     }     cout << ans <<   n ;     long long c = 0;     for (i = 0; i < n; i++) {       if (i % ans == 0) {         c = 0;         cout << c <<    ;         c++;       } else {         cout << c <<    ;         c++;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200005;   vector<int> lk[maxn];   int fa[maxn], dep[maxn], a[maxn], b[maxn], n;   void dfs(int now, int pre) {     fa[now] = pre;     dep[now] = dep[pre] + 1;     for (auto p : lk[now])       if (p != pre) dfs(p, now);   }   vector<int> cir;   int rt, lp, lq;   bool find_circle() {     cir.clear();     static bool vis[maxn];     int p = 0;     for (int i = 1; i <= n; i++)       if (a[i] != b[i] && dep[i] > dep[p]) p = i;     if (!p) return 1;     for (int u = p; vis[u] = 1, cir.push_back(u), a[fa[u]] != b[fa[u]]; u = fa[u])       ;     int q = 0;     for (int i = 1; i <= n; i++)       if (a[i] != b[i] && !vis[i] && dep[i] > dep[q]) q = i;     if (q) {       reverse(cir.begin(), cir.end());       for (int u = q; vis[u] = 1, cir.push_back(u), a[fa[u]] != b[fa[u]];            u = fa[u])         ;     } else       q = fa[cir.back()];     for (int i = 1; i <= n; i++)       if (a[i] != b[i] && !vis[i]) return 0;     int u, v;     for (u = p, v = q; u != v; u = fa[u])       if (dep[u] < dep[v]) swap(u, v);     rt = u;     if (cir.size() != dep[p] + dep[q] - 2 * dep[rt]) return 0;     lp = p, lq = q;     return 1;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) scanf( %d , &b[i]);     for (int i = 1, u, v; i < n; i++) {       scanf( %d%d , &u, &v);       lk[u].push_back(v), lk[v].push_back(u);     }     int pa = find(a + 1, a + n + 1, 0) - a, pb = find(b + 1, b + n + 1, 0) - b;     dep[0] = -1;     dfs(pb, 0);     for (int u = pa; u != pb; u = fa[u]) swap(a[u], a[fa[u]]);     long long ans = dep[pa];     if (!find_circle()) {       printf( -1 n );       return 0;     }     if (!cir.size()) {       printf( 0 %d n , ans);       return 0;     }     int gap = 0, len = cir.size();     for (int i = 0; i < len; i++)       if (b[cir[i]] == a[cir[0]]) gap = i;     for (int i = 1; i < len; i++)       if (b[cir[(i + gap) % len]] != a[cir[i]]) {         printf( -1 n );         return 0;       }     static bool vis[maxn];     for (int i = pa; i; i = fa[i]) vis[i] = 1;     if (vis[cir[0]] || vis[cir.back()]) {       if (vis[cir.back()]) reverse(cir.begin(), cir.end()), gap = len - gap;       int po = cir.size();       for (int i = 0; i < cir.size(); i++)         if (!vis[cir[i]]) {           po = i;           break;         }       ans = ans - po +             min(po + gap * 1ll * (len + 1),                 abs((len - gap) * 1ll * (len + 1) - po));     } else       ans = (ans + min(gap, len - gap) * 1ll * (len + 1));     for (; !vis[rt]; rt = fa[rt]) ans += 2;     if (lp > lq) swap(lq, lp);     printf( %d %d %lld n , lp, lq, ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5010;   struct Edge {     int v, u, f, r, c;     Edge() {}     Edge(int v, int u, int f, int c, int r) : v(v), u(u), f(f), c(c), r(r) {}   };   const int inf = 1e9;   const int C = 4000;   int n, m, k, nflow, s, t, parent[N], number[N], a[N], b[N];   int dist[N], pl[N];   vector<Edge> g[N];   queue<pair<int, int> > q;   vector<int> ga, gb;   void addEdge(int a, int b, int u, int c) {     g[a].push_back(Edge(b, u, 0, c, g[b].size()));     g[b].push_back(Edge(a, 0, 0, -c, g[a].size() - 1));   }   int MinCostFlow() {     int flow = 0;     int cost = 0;     while (true) {       for (int i = 1; i <= nflow; i++) dist[i] = inf;       dist[s] = 0;       q.push(make_pair(0, s));       while (!q.empty()) {         int v = (q.front()).second;         int d = -(q.front()).first;         q.pop();         if (dist[v] != d) continue;         if (d == inf) break;         for (int i = 0; i < g[v].size(); i++)           if (g[v][i].f < g[v][i].u && dist[g[v][i].v] > d + g[v][i].c) {             dist[g[v][i].v] = d + g[v][i].c;             q.push(make_pair(-dist[g[v][i].v], g[v][i].v));             number[g[v][i].v] = i;             parent[g[v][i].v] = v;           }       }       if (dist[t] == inf) break;       int v = t, addflow = 1e9;       while (v != s) {         int u = parent[v];         addflow = min(addflow, g[u][number[v]].u - g[u][number[v]].f);         v = u;       }       v = t;       while (v != s) {         int u = parent[v];         cost += addflow * g[u][number[v]].c;         g[u][number[v]].f += addflow;         g[v][g[u][number[v]].r].f -= addflow;         v = u;       }       flow += addflow;     }     return cost;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) scanf( %d , &b[i]);     s = n + 3;     t = n + 4;     for (int i = 1; i <= n; i++) {       addEdge(n + 1, i, 1, -a[i]);       addEdge(n + 2, i, 1, -b[i]);     }     addEdge(s, n + 1, m, 0);     addEdge(s, n + 2, k, 0);     for (int i = 1; i <= n; i++) {       addEdge(i, t, 1, 0);     }     nflow = n + 4;     int ans = -MinCostFlow();     cout << ans << endl;     for (int j = 0; j < g[n + 1].size(); j++) {       int v = g[n + 1][j].v;       if (v >= 1 && v <= n && g[n + 1][j].f == 1) {         ga.push_back(v);       }     }     for (int j = 0; j < g[n + 2].size(); j++) {       int v = g[n + 2][j].v;       if (v >= 1 && v <= n && g[n + 2][j].f == 1) {         gb.push_back(v);       }     }     for (int i = 0; i < ga.size(); i++) cout << ga[i] <<    ;     cout << endl;     for (int i = 0; i < gb.size(); i++) cout << gb[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int n;   double a[N];   double max_poor(double x) {     int i;     double pre, mpre, ret = 0;     pre = mpre = 0;     for (i = 0; i < n; i++) {       pre += a[i] - x;       ret = max(ret, abs(pre - mpre));       mpre = min(mpre, pre);     }     pre = mpre = 0;     for (i = 0; i < n; i++) {       pre += a[i] - x;       ret = max(ret, abs(pre - mpre));       mpre = max(mpre, pre);     }     return ret;   }   int main() {     cin.tie(0);     ios::sync_with_stdio(0);     int i;     double l = 0.0, h = 0.0, m1, m2;     cin >> n;     for (i = 0; i < n; h = max(h, abs(a[i])), i++) cin >> a[i];     l = -h;     for (i = 0; i < 300; i++) {       m1 = l + (h - l) / 3;       m2 = h - (h - l) / 3;       if (max_poor(m1) > max_poor(m2))         l = m1;       else         h = m2;     }     printf( %.17f n , max_poor(l));     return 0;   }


#include <bits/stdc++.h>   const long long MOD = 1000000007;   const int MAX = 1e9 + 7;   const int MIN = -1e9 + 7;   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long b, g;     cin >> b >> g;     long long n;     cin >> n;     if (b >= n && g >= n) {       cout << n + 1;     } else if (b + g == n) {       cout << 1;     } else if (b >= n || g >= n) {       cout << min(b, g) + 1;     } else {       cout << b + g - n + 1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = int(5e4) + 100;   const int MOD = int(360);   const int oo = INT_MAX;   const long double PI = acos(-1.0);   const long double EPS = 1e-9;   struct pt {     long double x, y;     pt() {}     pt(long double x, long double y) : x(x), y(y) {}   };   inline long double x(pt P) { return P.x; }   inline long double y(pt P) { return P.y; }   istream& operator>>(istream& in, pt& p) {     long double x, y;     in >> x >> y;     p = pt(x, y);     return in;   }   ostream& operator<<(ostream& out, const pt& p) {     out <<  (  << double(p.x) <<  ,   << double(p.y) <<  ) ;     return out;   }   pt operator+(const pt& A, const pt& B) { return pt(x(A) + x(B), y(A) + y(B)); }   pt operator-(const pt& A, const pt& B) { return pt(x(A) - x(B), y(A) - y(B)); }   pt operator*(const pt& A, const pt& B) {     return pt(x(A) * x(B) - y(A) * y(B), x(A) * y(B) + y(A) * x(B));   }   pt operator*(const pt& A, const long double& B) {     return pt(x(A) * B, y(A) * B);   }   pt operator*(const long double& B, const pt& A) {     return pt(x(A) * B, y(A) * B);   }   pt operator/(const pt& A, const long double& B) {     return pt(x(A) / B, y(A) / B);   }   inline long double norm(pt A) { return x(A) * x(A) + y(A) * y(A); }   inline long double abs(pt A) { return sqrt(norm(A)); }   inline long double cross(pt A, pt B) { return x(A) * y(B) - y(A) * x(B); }   inline long double dot(pt A, pt B) { return x(A) * x(B) + y(A) * y(B); }   inline bool same(long double a, long double b) {     return a + EPS > b && b + EPS > a;   }   inline bool samePt(pt A, pt B) { return same(x(A), x(B)) && same(y(A), y(B)); }   inline long double dist(pt A, pt B) { return abs(B - A); }   inline pt rotcw(pt p) { return p * pt(0, -1); }   inline bool segContains(pt X, pt A, pt B) {     if (samePt(X, A) || samePt(X, B)) return 1;     if (!same(0, cross(A - X, B - X))) return 0;     return (dot(A - X, B - X) < EPS);   }   pt linesIntersection(pt A, pt B, pt C, pt D) {     long double x = cross(C, D - C) - cross(A, D - C);     x /= cross(B - A, D - C);     return A + x * (B - A);   }   double signedArea(const vector<pt>& P) {     double result = 0.0;     for (int i = 0; i < (int)P.size() - 1; i++) result += cross(P[i], P[i + 1]);     return result / 2.0;   }   vector<pt> reorganize(vector<pt>& P) {     int n = int(P.size());     vector<pt> R(n);     if (P.size() == 1) {       R[0] = P[0];       return R;     }     if (signedArea(P) > EPS) {       reverse(P.begin(), P.end());     }     int s = 0;     for (int i = 1; i < n; ++i) {       if (x(P[s]) > x(P[i]) || (x(P[s]) == x(P[i]) && y(P[s]) > y(P[i]))) {         s = i;       }     }     R[0] = P[s];     for (int i = (s + 1) % n, j = 1; i != s; i = (i + 1) % n, ++j) {       if (samePt(P[i], P[(i - 1 + n) % n])) {         j--;         continue;       }       R[j] = P[i];     }     R[n - 1] = R[0];     return R;   }   vector<pt> convexPolygonSum(vector<pt>& P, vector<pt>& Q) {     P = reorganize(P);     Q = reorganize(Q);     int n = int(P.size()), m = int(Q.size());     vector<pt> R(n + m - 1);     R[0] = (P[0] + Q[0]);     int i = 1, j = 1, k = 1;     for (; i < n && j < m; ++k) {       if (cross(P[i] - P[i - 1], Q[j] - Q[j - 1]) < -EPS) {         R[k] = R[k - 1] + (P[i] - P[i - 1]);         ++i;       } else {         R[k] = R[k - 1] + (Q[j] - Q[j - 1]);         ++j;       }     }     while (i < n) {       R[k] = R[k - 1] + (P[i] - P[i - 1]);       ++i;       ++k;     }     while (j < m) {       R[k] = R[k - 1] + (Q[j] - Q[j - 1]);       ++j;       ++k;     }     vector<pt> T;     T.push_back(R[0]);     for (int i = 1; i + 1 < int(R.size()); ++i) {       if (same(cross(R[i] - R[i - 1], R[i + 1] - R[i - 1]), 0.0)) continue;       T.push_back(R[i]);     }     T.push_back(T[0]);     return T;   }   int inConvexPolygon(pt X, long double mnx, long double mxx, vector<pt>& P) {     if (x(X) < mnx || x(X) > mxx) return -1;     int lo = 1, hi = int(P.size()) - 1, mi;     while (lo <= hi) {       mi = (lo + hi) / 2;       if (cross(P[mi] - P[0], X - P[0]) < -EPS) {         lo = mi + 1;       } else {         hi = mi - 1;       }     }     lo = hi;     if (hi == -1) return -1;     long double c = cross(X - P[lo], X - P[lo + 1]);     if (same(c, 0.0)) return (segContains(X, P[lo], P[lo + 1]) ? 0 : -1);     if (c > EPS) return -1;     return 1;   }   int l, n, m, q;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     while (cin >> l) {       vector<pt> P1(l + 1);       for (int i = 0; i < l; ++i) {         cin >> P1[i];       }       cin >> n;       vector<pt> P2(n + 1);       for (int i = 0; i < n; ++i) {         cin >> P2[i];       }       cin >> m;       vector<pt> P3(m + 1);       for (int i = 0; i < m; ++i) {         cin >> P3[i];       }       P1[l] = P1[0];       P2[n] = P2[0];       P3[m] = P3[0];       vector<pt> P = convexPolygonSum(P2, P3);       P = convexPolygonSum(P1, P);       long double mnx = x(P[0]);       long double mxx = -oo;       for (int i = 0; i < int(P.size()); ++i) {         mxx = max(mxx, x(P[i]));       }       cin >> q;       for (int i = 0; i < q; ++i) {         pt cur;         cin >> cur;         cur = 3.0 * cur;         if (inConvexPolygon(cur, mnx, mxx, P) >= 0) {           cout <<  YES n ;         } else {           cout <<  NO n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k = 0;     string home;     cin >> n;     cin >> home;     for (int i = 0; i < n; i++) {       string now;       cin >> now;       now = now.substr(5, 3);       if (now == home) k++;     }     if (k == n - k)       cout <<  home ;     else       cout <<  contest ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   using min_heap = priority_queue<T, vector<T>, greater<T>>;   struct RangeIncrement {     vector<long long> tree, lazy;     RangeIncrement(int n) : tree(n << 2), lazy(n << 2) {}     void refresh(int i, int l, int r) {       if (lazy[i] != 0) {         tree[i] += lazy[i] * (r - l + 1);         if (l < r) {           int lc = i << 1, rc = (i << 1) + 1;           lazy[lc] += lazy[i];           lazy[rc] += lazy[i];         }         lazy[i] = 0;       }     }     void update(int i, int l, int r, int ul, int ur, long long x) {       refresh(i, l, r);       if (r < ul || l > ur) {         return;       } else if (ul <= l && r <= ur) {         lazy[i] += x;         refresh(i, l, r);       } else {         int m = l + r >> 1;         int lc = i << 1, rc = (i << 1) + 1;         update(lc, l, m, ul, ur, x);         update(rc, m + 1, r, ul, ur, x);         tree[i] = tree[lc] + tree[rc];       }     }     long long query(int i, int l, int r, int ql, int qr) {       refresh(i, l, r);       if (r < ql || l > qr) {         return 0;       } else if (ql <= l && r <= qr) {         return tree[i];       } else {         int m = l + r >> 1;         int lc = i << 1, rc = (i << 1) + 1;         return query(lc, l, m, ql, qr) + query(rc, m + 1, r, ql, qr);       }     }   };   template <class T>   struct DisjointRanges {     struct Range {       int l, r;       T x;       Range(int l, const pair<int, T>& p) : l(l), r(p.first), x(p.second) {}     };     map<int, pair<int, T>> s;     vector<Range> update(int l, int r, T x) {       vector<Range> bye;       auto last = s.upper_bound(r);       if (last != s.begin()) {         auto it = prev(last);         if (it->second.first > r) {           last = s.emplace_hint(last, r + 1, it->second);           it->second.first = r;         }       }       auto first = s.lower_bound(l);       if (first != s.begin()) {         auto it = prev(first);         if (it->second.first >= l) {           bye.emplace_back(l, it->second);           it->second.first = l - 1;         }       }       while (first != last) {         bye.emplace_back(first->first, first->second);         first = s.erase(first);       }       s.emplace_hint(last, l, make_pair(r, x));       return bye;     }   };   int main() {     int n, m;     scanf( %d %d , &n, &m);     RangeIncrement ri(n);     DisjointRanges<int> dr;     for (int i = 1; i <= n; i++) {       dr.update(i, i, i);     }     while (m--) {       int o, l, r, x;       scanf( %d %d %d , &o, &l, &r);       switch (o) {         case 1: {           scanf( %d , &x);           auto v = dr.update(l, r, x);           for (auto it : v) {             ri.update(1, 1, n, it.l, it.r, abs(x - it.x));           }           break;         }         case 2: {           printf( %lld n , ri.query(1, 1, n, l, r));           break;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int cnt[100100];   int R[100100];   int n;   long long power(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % 1000000007;       a = (a * a) % 1000000007;       b >>= 1;     }     return res;   }   int main() {     memset(cnt, 0, sizeof(cnt));     scanf( %d , &n);     int maxA = 0;     for (int i = 1; i <= n; i++) {       int tmp;       scanf( %d , &tmp);       cnt[tmp]++;       maxA = max(maxA, tmp);     }     for (int i = maxA; i >= 1; i--) cnt[i] += cnt[i + 1];     long long ans = 0;     for (int i = 1; i <= maxA; i++) {       int tot = 0;       for (int j = 1; j * j <= i; j++) {         if (i % j == 0) {           R[tot++] = j;           if (j * j != i) {             R[tot++] = i / j;           }         }       }       sort(R, R + tot);       long long tmp =           (power(tot, cnt[R[tot - 1]]) -            power(tot - 1, cnt[R[tot - 1]]) % 1000000007 + 1000000007) %           1000000007;       for (int j = 0; j < tot - 1; j++)         tmp = (tmp * power(j + 1, cnt[R[j]] - cnt[R[j + 1]])) % 1000000007;       ans = (ans + tmp) % 1000000007;     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 500100;   const int maxp = 40;   int n, q;   double d[maxn][maxp];   int p[maxn];   void upd(int v, int k, double x) {     while (v && k < maxp) {       double a = d[v][k];       d[v][k] *= x;       x = (d[v][k] + 1) / (a + 1);       v = p[v];       k++;     }   }   int main() {     ios_base::sync_with_stdio(0);     for (int i = 0; i < maxn; i++)       for (int j = 0; j < maxp; j++) d[i][j] = 1;     cin >> q;     n = 1;     for (int i = 0; i < q; i++) {       int qtype, v;       cin >> qtype >> v;       if (qtype == 1) {         p[++n] = v;         upd(n, 0, 0);       } else {         double ans = 0;         for (int j = 1; j < maxp; j++) ans += (1 - d[v][j]);         cout << fixed << setprecision(10) << ans <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll INF = 1e14 + 7;   using vi = vector<int>;   using pii = pair<int, int>;   using pli = pair<ll, int>;   int ask(int i, int j) {     cout <<  ?   << i <<     << j << endl;     int ans;     cin >> ans;     return ans;   }   int main() {     int n;     cin >> n;     vi ans(n + 4), curr(n + 4);     curr[0] = ask(1, n);     curr[1] = ask(2, n);     ans[1] = curr[0] - curr[1];     for (int i = 2; i <= n - 1; i++) {       curr[i] = ask(i - 1, i);       ans[i] = curr[i] - ans[i - 1];     }     ans[n] = curr[0];     for (int i = 1; i < n; i++) {       ans[n] -= ans[i];     }     cout <<  ! ;     for (int i = 1; i <= n; i++) {       cout <<     << ans[i];     }     cout << endl;     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[200005];   int b[200005];   bool f[200005];   int main() {     int q, n;     cin >> q;     while (q--) {       cin >> n;       for (int i = 1; i <= n; i++) {         cin >> a[i];         b[i] = 1;         f[i] = 0;       }       for (int i = 1; i <= n; i++) {         int j = i;         if (f[i] == 1) continue;         f[i] = 1;         int ctr = 1;         while (a[j] != i) {           ctr++;           f[j] = 1;           j = a[j];         }         j = i;         while (a[j] != i) {           b[j] = ctr;           j = a[j];         }       }       for (int i = 1; i <= n; i++) cout << b[i] <<    ;       cout <<   n ;     }   }


#include <bits/stdc++.h>   inline int readChar();   template <class T = int>   inline T readInt();   template <class T>   inline void writeInt(T x, char end = 0);   inline void writeChar(int x);   inline void writeWord(const char* s);   static const int buf_size = 4096;   inline int getChar() {     static char buf[buf_size];     static int len = 0, pos = 0;     if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin);     if (pos == len) return -1;     return buf[pos++];   }   inline int readChar() {     int c = getChar();     while (c <= 32) c = getChar();     return c;   }   template <class T>   inline T readInt() {     int s = 1, c = readChar();     T x = 0;     if (c ==  - ) s = -1, c = getChar();     while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar();     return s == 1 ? x : -x;   }   static int write_pos = 0;   static char write_buf[buf_size];   inline void writeChar(int x) {     if (write_pos == buf_size)       fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;     write_buf[write_pos++] = x;   }   template <class T>   inline void writeInt(T x, char end) {     if (x < 0) writeChar( - ), x = -x;     char s[24];     int n = 0;     while (x || !n) s[n++] =  0  + x % 10, x /= 10;     while (n--) writeChar(s[n]);     if (end) writeChar(end);   }   inline void writeWord(const char* s) {     while (*s) writeChar(*s++);   }   struct Flusher {     ~Flusher() {       if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;     }   } flusher;   using namespace std;   struct static_graph {     int colors[500000];     int firstv[500000];     set<int> pool;     static const int N = 500000;     static const int M = 900000;     struct edge {       int v;       edge* next;     } edges[M];     int nedges;     struct node {       vector<int>* ice;       int prev_colors;       bool depth_use;       edge* firstedge;       inline void init() { firstedge = NULL; };       inline edge* add_edge(edge* e) {         e->next = firstedge;         firstedge = e;         return firstedge;       };     } nodes[N];     int nnodes;     static_graph() { nnodes = 0, nedges = 0; };     inline node& new_node() {       node& r = nodes[nnodes++];       return r;     }     inline edge* new_edge() { return &(edges[nedges++]); };     inline edge* add_edge(int a, int b) {       edge* e = new_edge();       e->v = b;       return nodes[a].add_edge(e);     };     inline void add_bi_edge(int a, int b) {       add_edge(a, b);       add_edge(b, a);     };     void depth_go(int index, int prev) {       node& cur = nodes[index];       if (cur.depth_use++) return;       for (int x : (*cur.ice))         if (colors[x]) pool.erase(colors[x]);       for (int x : (*cur.ice))         if (!colors[x]) {           colors[x] = (*pool.upper_bound(0));           firstv[x] = index;           pool.erase(colors[x]);         }       for (int x : (*cur.ice)) pool.insert(colors[x]);       for (edge* e = cur.firstedge; e; e = e->next) {         depth_go(e->v, index);       };     };     void depth_run(int first) {       chrono::time_point<chrono::high_resolution_clock> start, end;       start = std::chrono::high_resolution_clock::now();       for (int i = 0; i < nnodes; i++) {         node& cur = nodes[i];         cur.depth_use = false;       };       depth_go(first, -1);     };     vector<node*> depth_to_wide(int first) {}     void read_graph(istream* f){};   };   static_graph g;   int main() {     istream& f = cin;     ios_base::sync_with_stdio(false);     cin.tie(NULL);     chrono::time_point<chrono::high_resolution_clock> start, end;     start = std::chrono::high_resolution_clock::now();     int n, m, c, x = 0;     n = readInt<int>();     m = readInt<int>();     for (int i = 0; i < n; i++) {       c = readInt<int>();       static_graph::node& cur = g.new_node();       cur.ice = new vector<int>();       cur.ice->resize(c);       cur.prev_colors = 0;       for (int t = 0; t < c; t++) {         x = readInt<int>();         (*cur.ice)[t] = x - 1;       };       sort((*cur.ice).begin(), (*cur.ice).end());       ;     };     for (int i = 0; i < n - 1; i++) {       int a, b;       a = readInt<int>(), b = readInt<int>();       g.add_bi_edge(a - 1, b - 1);     };     for (int i = 0; i < m; i++) g.colors[i] = 0;     for (int i = 0; i < m; i++) g.pool.insert(i + 1);     g.depth_run(0);     int mx = 1;     for (int i = 0; i < m; i++) {       mx = max(mx, g.colors[i]);       if (!g.colors[i]) g.colors[i] = 1;     }     cout << mx << endl;     for (int i = 0; i < m; i++) cout << g.colors[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   long long pw(long long a, long long b) {     if (b == 0) return 1;     if (b % 2 == 1)       return (a * pw(a * a, b / 2));     else       return (1 * pw(a * a, b / 2));   }   void solve() {     long long n, k;     cin >> n >> k;     long long a[n], b[k];     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     int n1 = 0;     for (int i = 0; i < k; i++) {       cin >> b[i];       if (b[i] == 1) n1++;     }     sort(b, b + k);     long long ans = 0;     for (int i = 0; i < n1; i++) ans += (a[n - 1 - i] * 2);     long long st = 0, en = n - 1 - n1;     for (long long i = k - 1; i >= n1; i--) {       if (b[i] == 1) {         ans += (a[en] * 2);         en--;       } else {         ans += a[en];         b[i]--;         en--;         ans += a[st];         st += b[i];       }     }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     long long lvl1, lvl2, id;   };   node t[500005];   bool cmp(struct node x, struct node y) {     if (x.lvl1 < y.lvl1)       return true;     else       return false;   }   vector<node> sh;   vector<long long> adj[500005];   long long vis[500005], lvl1[500005], lvl2[500005];   void bfs1(long long n = 1) {     queue<long long> q;     q.push(n);     vis[n] = 1;     lvl1[n] = 0;     while (!q.empty()) {       long long x = q.front();       q.pop();       for (auto &cv : adj[x]) {         if (vis[cv]) continue;         q.push(cv);         vis[cv] = 1;         lvl1[cv] = lvl1[x] + 1;       }     }   }   void bfs2(long long n) {     queue<long long> q;     q.push(n);     vis[n] = 1;     lvl2[n] = 0;     while (!q.empty()) {       long long x = q.front();       q.pop();       for (auto &cv : adj[x]) {         if (vis[cv]) continue;         q.push(cv);         vis[cv] = 1;         lvl2[cv] = lvl2[x] + 1;       }     }   }   node combine(node x, node y) {     if (x.lvl2 > y.lvl2)       return x;     else       return y;   }   void build(long long n) {     for (long long i = n - 1; i > 0; --i)       t[i] = combine(t[i << 1], t[i << 1 | 1]);   }   node query(long long l, long long r, long long n) {     node res;     res.lvl2 = res.lvl1 = res.id = 0;     for (l += n, r += n; l < r; l >>= 1, r >>= 1) {       if (l & 1) res = (combine(t[l++], res));       if (r & 1) res = combine(t[--r], res);     }     return res;   }   long long arr[500005];   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long i, j, k, l, m, n, x, y;     cin >> n >> m >> k;     for (i = 0; i < k; i++) {       cin >> arr[i];     }     for (i = 0; i < m; i++) {       cin >> x;       cin >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     bfs1();     memset(vis, 0, sizeof vis);     bfs2(n);     for (i = 0; i < k; i++) {       node temp;       temp.id = arr[i];       temp.lvl1 = lvl1[arr[i]];       temp.lvl2 = lvl2[arr[i]];       sh.push_back(temp);     }     sort(sh.begin(), sh.end(), cmp);     for (i = 0; i < sh.size(); i++) {       t[i + k].lvl1 = sh[i].lvl1;       t[i + k].lvl2 = sh[i].lvl2;       t[i + k].id = sh[i].id;     }     build(k);     long long ans = 0;     for (i = 0; i + 1 < sh.size(); i++) {       long long temp;       temp = sh[i].lvl1;       temp++;       temp += query(i + 1, sh.size(), sh.size()).lvl2;       ans = max(ans, temp);     }     cout << min(lvl2[1], ans) << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4786)   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   const double eps = 1e-8;   const double PI = acos(-1.0);   const int MAXN = 200005;   const int mod = 1000000007;   const long long llinf = (long long)(1e18) + 500;   const int inf = 0x3f3f3f3f;   int x[1005];   double y[1005];   int main() {     int i, j, k, m;     int n, r;     scanf( %d%d , &n, &r);     for (i = 1; i <= n; i++) {       scanf( %d , x + i);       y[i] = r * 1.0;     }     for (i = 2; i <= n; i++) {       for (j = i - 1; j >= 1; j--) {         int dx = abs(x[i] - x[j]);         if (dx > 2 * r) continue;         y[i] = max(y[i], y[j] + sqrt(4 * r * r * 1.0 - dx * 1.0 * dx));       }     }     for (i = 1; i <= n; i++) {       printf( %.10lf%c , y[i], i == n ?   n  :    );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char str[1000005 + 1];   int main() {     int ans = 0, i;     stack<int> S;     scanf( %s , str);     for (i = 0; str[i] !=   0 ; ++i) {       if (str[i] ==  ( ) S.push(i);       if (str[i] ==  )  && !S.empty()) {         S.pop();         ans += 2;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int y, b, r;     cin >> y >> b >> r;     while (1) {       if (r - 1 <= b && r - 2 <= y) {         break;       }       --r;     }     cout << 3 * r - 3;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string n;     cin >> n;     long long even = 0;     long long odd = 0;     long long len = n.length();     long long aeven = 0;     long long aodd = 0;     long long beven = 0;     long long bodd = 0;     for (int i = 0; i < len; i++) {       if (n[i] ==  a ) {         if (i % 2 == 1) {           aodd++;           odd += aodd;           even += aeven;         } else {           aeven++;           odd += aeven;           even += aodd;         }       } else {         if (i % 2 == 1) {           bodd++;           odd += bodd;           even += beven;         } else {           beven++;           odd += beven;           even += bodd;         }       }     }     cout << even <<     << odd << endl;   }


#include <bits/stdc++.h>   using namespace std;   int const N = 100000;   long long a[N + 1];   long long f[N * 3];   inline int lc(int root) { return root << 1; }   inline int rc(int root) { return (root << 1) | 1; }   void build(int root, int l, int r) {     f[root] = -1;     if (l == r) {       f[root] = a[l];       return;     }     int mid = (l + r) >> 1;     build(lc(root), l, mid);     build(rc(root), mid + 1, r);     if (f[lc(root)] == f[rc(root)]) {       f[root] = f[lc(root)];     }   }   void update(int root, int l, int r, int fi, int la, long long val) {     if (fi == l && la == r) {       f[root] = val;       return;     }     int mid = (l + r) >> 1;     f[root] = -1;     if (fi > mid) {       update(rc(root), mid + 1, r, fi, la, val);     } else if (la <= mid) {       update(lc(root), l, mid, fi, la, val);     } else {       update(lc(root), l, mid, fi, mid, val);       update(rc(root), mid + 1, r, mid + 1, la, val);     }     if (f[lc(root)] == f[rc(root)]) {       f[root] = f[lc(root)];     }   }   long long query(int root, int l, int r, int fi, int la) {     if (f[root] != -1) {       return f[root];     }     int mid = (l + r) >> 1;     if (fi > mid) {       return query(rc(root), mid + 1, r, fi, la);     } else if (la <= mid) {       return query(lc(root), l, mid, fi, la);     } else {       return max(query(lc(root), l, mid, fi, mid),                  query(rc(root), mid + 1, r, mid + 1, la));     }   }   void work() {     int n;     int m;     long long w, h;     long long res;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %I64d , &a[i]);     }     build(1, 1, n);     scanf( %d , &m);     while (m--) {       scanf( %I64d%I64d , &w, &h);       res = query(1, 1, n, 1, w);       printf( %I64d n , res);       update(1, 1, n, 1, w, res + h);     }   }   int main() {     work();     return 0;   }