#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9;   const long long MD = 1e9 + 7;   const double pi = acos(-1);   struct segmentTree {     long long tl = 1, tr = (1ll << 21);     segmentTree* left = nullptr;     segmentTree* right = nullptr;     long long val = 0;     segmentTree(long long tl, long long tr) : tl(tl), tr(tr) {}     segmentTree() = default;     void createSons() {       long long tm = (tl + tr) / 2;       if (!left) {         left = new segmentTree(tl, tm);       }       if (!right) {         right = new segmentTree(tm + 1, tr);       }     }     void update(long long pos, long long val) {       if (tl == tr) {         this->val = val;         return;       }       long long tm = (tl + tr) / 2;       createSons();       if (pos <= tm) {         left->update(pos, val);       } else {         right->update(pos, val);       }       this->val = (left->val) ^ (right->val);     }     long long get(long long l, long long r) {       if (l > r) {         return 0;       }       if (l == tl && tr == r) {         return val;       }       long long tm = (tl + tr) / 2;       createSons();       return (left->get(l, min(r, tm))) ^ (right->get(max(tm + 1, l), r));     }   };   ostream& operator<<(ostream& stream, const vector<long long>& v) {     for (auto i : v) {       stream << i <<   n ;     }     return stream;   }   pair<long long, long long> operator+=(pair<long long, long long>& a,                                         const pair<long long, long long>& b) {     a.first += b.first;     a.second += b.second;     return a;   };   long long sqr(long long a) { return a * a % MD; }   long long binpow(long long a, long long b) {     if (b == 0) {       return 1;     }     if (b % 2 == 0) {       return sqr(binpow(a, b / 2));     }     return a * binpow(a, b - 1) % MD;   }   long long fact(long long a) {     long long res = 1;     for (long long i = 2; i <= a; i++) {       res *= i;       res %= MD;     }     return res;   }   struct edge {     long long u, v, c;     bool operator<(const edge& other) const {       if (c == other.c) {         if (u == other.u) {           return v < other.v;         }         return u < other.u;       }       return c < other.c;     }   };   struct query {     long long u, v, k, ind;     bool operator<(const query& other) const {       if (k == other.k) {         if (u == other.u) {           if (ind == other.ind) {             return ind < other.ind;           }           return v < other.v;         }         return u < other.u;       }       return k < other.k;     }   };   vector<long long> g[100006];   long long timer = 1;   long long subTreeSize[100006];   long long pred[100006];   long long tIn[100006];   long long tOut[100006];   void dfs(long long v) {     tIn[v] = timer++;     subTreeSize[v] = 1;     for (auto i : g[v]) {       if (!tIn[i]) {         pred[i] = v;         dfs(i);         subTreeSize[v] += subTreeSize[i];       }     }     tOut[v] = timer++;   }   bool isPred(long long a, long long b) {     return (tIn[a] <= tIn[b] && tOut[b] <= tOut[a]);   }   long long chainNumOfVertex[100006], inChainPositionOfVertex[100006],       chainSize[100006], chainRoot[100006], chainsCount;   vector<segmentTree> segmTrees;   struct HLD {     void build(long long v, long long chainNum) {       chainNumOfVertex[v] = chainNum;       inChainPositionOfVertex[v] = chainSize[chainNum];       chainSize[chainNum]++;       if (chainSize[chainNum] == 1) {         chainRoot[chainNum] = v;       }       long long maxChild = -1;       for (auto i : g[v]) {         if (i != pred[v] &&             (maxChild == -1 || subTreeSize[i] > subTreeSize[maxChild])) {           maxChild = i;         }       }       if (maxChild != -1) {         for (auto i : g[v]) {           if (i == pred[v]) {             continue;           }           if (i == maxChild) {             build(i, chainNum);           } else {             segmTrees.push_back(segmentTree());             build(i, ++chainsCount);           }         }       }     }     long long get(long long a, long long b) {       long long res = 0;       for (long long i = 1; i <= 2; i++) {         while (1) {           if (isPred(chainRoot[chainNumOfVertex[a]], b)) {             break;           }           segmTrees[chainNumOfVertex[a]].tl = 0;           segmTrees[chainNumOfVertex[a]].tr = chainSize[chainNumOfVertex[a]];           res ^=               segmTrees[chainNumOfVertex[a]].get(0, inChainPositionOfVertex[a]);           a = pred[chainRoot[chainNumOfVertex[a]]];         }         swap(a, b);       }       if (inChainPositionOfVertex[a] > inChainPositionOfVertex[b]) {         swap(a, b);       }       segmTrees[chainNumOfVertex[a]].tl = 0;       segmTrees[chainNumOfVertex[a]].tr = chainSize[chainNumOfVertex[a]];       res ^= segmTrees[chainNumOfVertex[a]].get(inChainPositionOfVertex[a] + 1,                                                 inChainPositionOfVertex[b]);       return res;     }     void update(long long a, long long val) {       segmTrees[chainNumOfVertex[a]].tl = 0;       segmTrees[chainNumOfVertex[a]].tr = chainSize[chainNumOfVertex[a]];       segmTrees[chainNumOfVertex[a]].update(inChainPositionOfVertex[a], val);     }     void add(edge e) {       if (pred[e.u] == e.v) {         update(e.u, e.c);       } else {         update(e.v, e.c);       }     }   };   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       string st;       cin >> st;       st =  0  + st;       long long ans = 0;       for (long long i = 1; i < st.size(); i++) {         if (st[i] ==  1 ) {           ans = max(2 * i, ans);           ans = max((n - i + 1) * 2, ans);         }       }       cout << max(ans, n) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   void ran() { srand(chrono::steady_clock::now().time_since_epoch().count()); }   long long get_rand() {     long long a = rand();     long long b = rand();     return a * (RAND_MAX + 1ll) + b;   }   void solve() {     int n, m;     cin >> n >> m;     vector<int> subs[n + 1], inc[n + 1];     int labels[n + 1];     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       subs[a].push_back(b);       inc[b].push_back(a);     }     vector<int> rem;     for (int i = 1; i <= n; i++) {       int from0 = 0, from1 = 0, from2 = 0, total = 0;       for (auto x : inc[i]) {         total++;         if (labels[x] == 0) {           from0++;         } else if (labels[x] == 1) {           from1++;         } else {           from2++;         }       }       if (from2 == total) {         labels[i] = 0;       } else if (from1 == 0) {         labels[i] = 1;       } else {         rem.push_back(i);         labels[i] = 2;       }     }     cout << rem.size() << endl;     for (auto x : rem) cout << x <<    ;     cout << endl;   }   signed main() {     ran();     fast();     int t = 1;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   struct segment {     int pos;     int len;     int type;   };   struct segmentByLength {     segment seg;     bool operator<(segmentByLength o) const {       segment seg2 = o.seg;       if (seg.len != seg2.len)         return seg.len > seg2.len;       else {         return seg.pos < seg2.pos;       }     }   };   struct segmentByPosition {     segment seg;     bool operator<(segmentByPosition o) const {       segment seg2 = o.seg;       return seg.pos < seg2.pos;     }   };   set<segmentByLength> lengthSet;   set<segmentByPosition> positionSet;   void add_segment(segment s) {     lengthSet.insert({s});     positionSet.insert({s});   }   void remove_segment(segment s) {     lengthSet.erase({s});     positionSet.erase({s});   }   int main() {     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     a.push_back(1000000001);     int start = 0;     for (int i = 0; i < a.size(); i++) {       if (i > 0 and a[i] != a[i - 1]) {         segment s = {start, (i - 1) - start + 1, a[i - 1]};         lengthSet.insert({{s}});         positionSet.insert({{s}});         start = i;       }     }     int sol = 0;     while (!lengthSet.empty()) {       segment s = (*lengthSet.begin()).seg;       segment left, right;       bool leftExists = true, rightExists = true;       auto lbIterator = positionSet.lower_bound({s});       if (lbIterator != positionSet.begin()) {         --lbIterator;         left = (*lbIterator).seg;       } else {         leftExists = false;       }       auto ubIterator = positionSet.upper_bound({s});       if (ubIterator != positionSet.end()) {         right = (*ubIterator).seg;       } else {         rightExists = false;       }       remove_segment(s);       if (leftExists and rightExists and left.type == right.type) {         segment merged = {left.pos, left.len + right.len, left.type};         remove_segment(left);         remove_segment(right);         add_segment(merged);       }       sol++;     }     cout << sol <<   n ;     return 0;   }


#include <bits/stdc++.h>   int main() {     int q;     scanf( %d , &q);     while (q--) {       long long k;       int x;       scanf( %lld%d , &k, &x);       printf( %lld n , x + 9 * (k - 1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long input[(long long)1e5 + 1];   long long ans = 0;   long long n;   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> input[i];     for (int i = n - 2; i >= 0; i--) {       if (input[i] > input[i + 1]) ans += input[i] - input[i + 1];     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 5;   int a[MAXN], x, f, b, n;   vector<int> v;   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     cin >> f >> b;     sort(a, a + n);     n = unique(a, a + n) - a;     for (int i = 0; i < n; i++) v.push_back(a[i]);     int tmp;     while (f != b) {       tmp = f - 1;       for (int i = v.size() - 1; ~i; i--) {         if (f - f % v[i] < b)           v.erase(v.begin() + i);         else           tmp = min(tmp, f - f % v[i]);       }       x++;       f = tmp;     }     cout << x;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int g[100010];   int main(void) {     int A, T, N, M;     scanf( %d , &N);     for (int i = (int)(3); i <= (int)(N); i++) {       set<int> st;       A = 2000000000;       for (int j = (int)(2); j <= (int)(N); j++) {         int k = i - j * (j + 1) / 2;         if (k < 0) break;         if (k % j != 0) continue;         int l = 1 + k / j, r = j + k / j, nim = 0;         while (l <= r) {           nim = nim ^ g[l++];         }         st.insert(nim);         if (nim == 0) A = min(A, j);       }       int res = 0;       while (st.find(res) != st.end()) res++;       g[i] = res;     }     if (g[N])       printf( %d n , A);     else       printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void pr(vector<T> &v) {     for (int i = 0; i < (int)(v).size(); i++) cout << v[i] <<    ;     cout << endl;   }   template <typename T>   void pr(vector<vector<T>> &v) {     for (int i = 0; i < (int)(v).size(); i++) {       pr(v[i]);     }   }   template <typename T>   void re(T &first) {     cin >> first;   }   template <typename T>   void re(vector<T> &a) {     for (int i = 0; i < (int)(a).size(); i++) re(a[i]);   }   template <class Arg, class... Args>   void re(Arg &first, Args &...rest) {     re(first);     re(rest...);   }   template <typename T>   void pr(T first) {     cout << first << endl;   }   template <class Arg, class... Args>   void pr(const Arg &first, const Args &...rest) {     cout << first <<    ;     pr(rest...);     cout << endl;   }   void ps() { cout << endl; }   template <class T, class... Ts>   void ps(const T &t, const Ts &...ts) {     cout << t;     if (sizeof...(ts)) cout <<    ;     ps(ts...);   }   int n;   vector<int> cur;   int cnt(vector<int> &v, int mn) {     int a = 0;     for (int i = 0; i < (int)(v).size(); i++) {       if (mn < n && v[i] == cur[mn]) {         mn++;         a++;       }     }     return (int)(v).size() - a;   }   int rec(vector<int> v, int mn) {     if ((int)(v).size() == 0) return 0;     int ind = 0;     for (int i = 0; i < (int)(v).size(); i++) {       if (v[i] == cur[mn]) ind = i;     }     int xx = cnt(v, mn);     v.erase(v.begin() + ind);     int yy = 1 + rec(v, mn + 1);     int ack = min(xx, yy);     return ack;   }   void solve() {     re(n);     vector<int> a(n);     re(a);     cur.clear();     cur = a;     sort(cur.begin(), cur.end());     pr(rec(a, 0));   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     re(t);     for (int tt = 0; tt < t; tt++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int m, n, l, s = 0;     cin >> n >> m >> l;     vector<int> a(n);     vector<int> otvet;     for (int i = 0; i < n; ++i) cin >> a[i];     if (n == 1) {       for (int i = 0; i < m; ++i) {         int x;         cin >> x;         if (x == 0) {           if (a[0] <= l)             otvet.push_back(0);           else             otvet.push_back(1);           continue;         }         int y, z;         cin >> y >> z;         if (a[0] <= l) a[0] += z;       }       for (int i = 0; i < otvet.size(); ++i) cout << otvet[i] << endl;       return 0;     }     int i = 0;     while (i < n) {       if (a[i] <= l) {         ++i;         continue;       }       int j = i;       while (a[j] > l && j < n) {         j++;       }       s++;       i = j;     }     for (int i = 0; i < m; ++i) {       int x;       cin >> x;       if (x == 0) {         otvet.push_back(s);         continue;       }       int y, z;       cin >> y >> z;       y--;       if (a[y] > l) continue;       a[y] += z;       if (a[y] > l) {         if (y > 0 && y < n - 1) {           if (a[y - 1] <= l && a[y + 1] <= l)             s++;           else if (a[y - 1] > l && a[y + 1] > l) {             s--;           }         } else if (y == 0) {           if (a[y + 1] <= l) s++;         } else if (y == n - 1) {           if (a[y - 1] <= l) s++;         }       }     }     for (int i = 0; i < otvet.size(); ++i) cout << otvet[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void io() {     freopen( input.txt ,  r , stdin);     freopen( output.txt ,  w , stdout);   }   long long int a[100001], n, k, pos = 1000000000000000;   map<long long int, long long int> m;   set<long long int> s;   int main() {     cin >> n >> k;     s.clear();     for (long long int i = 0; i < n; i++) {       cin >> a[i];       if (s.size() < k) m[a[i]]++;       s.insert(a[i]);       if (s.size() == k) pos = min(pos, i);     }     if (pos == 1000000000000000) {       cout << -1 <<     << -1 << endl;       return 0;     }     for (long long int i = 0; i < pos + 1; i++) {       m[a[i]]--;       if (m[a[i]] == 0) {         cout << i + 1 <<     << pos + 1 << endl;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int n;     int64_t res1 = 0, res2 = 0, sum = 0;     bool is;     std::cin >> n;     std::vector<int> m(n);     for (int i = 0; i < n; i++) {       std::cin >> m[i];       sum += m[i];     }     is = sum % n != 0;     sum /= n;     for (int i = 0; i < n; i++) {       if (m[i] < sum) {         res1 += sum - m[i];       } else if (is) {         if (m[i] > sum + 1) {           res2 += m[i] - sum - 1;         }       } else {         if (m[i] > sum) {           res2 += m[i] - sum;         }       }     }     std::cout << std::max(res1, res2);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 20009;   int INF = 2e9;   struct edge {     int to, cap, rev;     edge() {}     edge(int tt, int cc, int rr) {       to = tt;       cap = cc;       rev = rr;     }   };   vector<edge> Graph[N];   int level[N];   int iter[N];   void add_edge(int from, int to, int cap) {     Graph[from].push_back(edge(to, cap, Graph[to].size()));     Graph[to].push_back(edge(from, 0, Graph[from].size() - 1));   }   void bfs(int s) {     memset(level, -1, sizeof(level));     queue<int> que;     level[s] = 0;     que.push(s);     while (que.size()) {       int v = que.front();       que.pop();       for (int i = 0; i < Graph[v].size(); i++) {         edge &e = Graph[v][i];         if (e.cap > 0 && level[e.to] < 0) {           level[e.to] = level[v] + 1;           que.push(e.to);         }       }     }   }   int dfs(int v, int t, int f) {     if (v == t) return f;     for (int &i = iter[v]; i < Graph[v].size(); i++) {       edge &e = Graph[v][i];       if (e.cap > 0 && level[v] < level[e.to]) {         int d = dfs(e.to, t, min(f, e.cap));         if (d > 0) {           e.cap -= d;           Graph[e.to][e.rev].cap += d;           return d;         }       }     }     return 0;   }   int max_flow(int s, int t) {     int flow = 0;     while (1) {       bfs(s);       if (level[t] < 0) return flow;       memset(iter, 0, sizeof(iter));       int f;       while ((f = dfs(s, t, INF)) > 0) {         flow += f;       }     }     return flow;   }   int a[105], b[105], res[105][105], SumIn, SumOut;   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++)       scanf( %d , &a[i]), add_edge(0, i + 1, a[i]), SumIn += a[i];     for (int i = 0; i < n; i++)       scanf( %d , &b[i]), add_edge(i + 1 + n, 2 * n + 1, b[i]), SumOut += b[i];     for (int i = 0; i < n; i++) add_edge(i + 1, i + 1 + n, 1e6);     for (int i = 0; i < m; i++) {       int u, v;       scanf( %d%d , &u, &v);       add_edge(u, v + n, 1e6);       add_edge(v, u + n, 1e6);     }     if (SumIn != SumOut || SumIn != max_flow(0, 2 * n + 1))       return 0 * printf( NO n );     printf( YES n );     for (int i = 1; i <= n; i++)       for (auto &e : Graph[i])         if (e.to > n) res[i - 1][e.to - n - 1] = Graph[e.to][e.rev].cap;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) printf( %d  , res[i][j]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n, i, j;       cin >> n;       long long int arr[n][n];       for (i = 0; i < n; i++) {         string s;         cin >> s;         for (j = 0; j < n; j++) {           long long int temp;           temp = s[j] - 48;           arr[i][j] = temp;         }       }       int flag = 1;       for (i = 0; i < n; i++) {         for (j = 0; j < n; j++) {           if (arr[i][j] == 1) {             if (j != n - 1 && i != n - 1) {               if (arr[i + 1][j] != 1 && arr[i][j + 1] != 1) {                 flag = 0;                 break;               }             }           }         }         if (flag == 0) break;       }       if (flag == 1) {         cout <<  YES n ;       } else {         cout <<  NO n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int DX[8] = {1, -1, 0, 0, 1, 1, -1, -1};   const int DY[8] = {0, 0, 1, -1, 1, -1, 1, -1};   const int intmax = 0x7fffffff;   const int mod = 1000000007;   int n, m;   int f[205];   int p[205];   vector<pair<int, int> > op;   int main() {     scanf( %d%d , &n, &m);     int cnt = 0;     for (int i = 0; i < m; i++) {       int nk;       scanf( %d , &nk);       for (int j = 0; j < nk; j++) {         int a;         scanf( %d , &a);         cnt++;         f[a] = cnt;         p[cnt] = a;       }     }     int pemp;     for (int i = 1; i <= n; i++)       if (!f[i]) pemp = i;     for (int j = 1; j <= cnt; j++) {       if (f[j] != j) {         if (!f[j] || j == pemp) {           pemp = p[j];           op.push_back(make_pair(p[j], j));           p[j] = j;           f[j] = j;           continue;         }         f[pemp] = f[j];         p[f[j]] = pemp;         op.push_back(make_pair(j, pemp));         int now = j;         while (f[now] != now && now <= cnt && now >= 1) {           op.push_back(make_pair(p[now], now));           int tmp = now;           f[now] = now;           now = p[now];           p[tmp] = tmp;         }         pemp = now;       }     }     printf( %d n , op.size());     for (int i = 0; i < op.size(); i++) {       printf( %d %d n , op[i].first, op[i].second);     }   }


#include <bits/stdc++.h>   using namespace std;   inline char nc() {     static char buf[100000], *p1 = buf, *p2 = buf;     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)                ? EOF                : *p1++;   }   inline void read(int &x) {     char c = nc(), b = 1;     for (; !(c >=  0  && c <=  9 ); c = nc())       if (c ==  - ) b = -1;     for (x = 0; c >=  0  && c <=  9 ; x = x * 10 + c -  0 , c = nc())       ;     x *= b;   }   const int N = 55;   const int M = 2505;   struct edge {     int u, v, w, f;     int next;   } G[M << 1];   int head[N], inum = 1;   inline void add(int u, int v, int w, int f, int p) {     G[p].u = u;     G[p].v = v;     G[p].w = w;     G[p].f = f;     G[p].next = head[u];     head[u] = p;   }   inline void link(int u, int v, int w, int f) {     add(u, v, w, f, ++inum);     add(v, u, -w, 0, ++inum);   }   int n, m, P;   int S, T, Mincost;   int Q[N * M], l, r;   int dis[N], pre[N], ins[N];   inline bool SPFA() {     for (int i = 1; i <= n; i++) dis[i] = 1 << 29, pre[i] = 0, ins[i] = 0;     l = r = -1;     Q[++r] = S, dis[S] = 0, ins[S] = 1;     while (l < r) {       int u = Q[++l];       ins[u] = 0;       for (int p = head[u]; p; p = G[p].next)         if (G[p].f && dis[G[p].v] > dis[u] + G[p].w) {           dis[G[p].v] = dis[u] + G[p].w;           pre[G[p].v] = p;           if (!ins[G[p].v]) Q[++r] = G[p].v, ins[G[p].v] = 1;         }     }     if (dis[T] == 1 << 29) return 0;     Mincost += dis[T];     for (int p = pre[T]; p; p = pre[G[p].u]) G[p].f--, G[p ^ 1].f++;     return 1;   }   int _, Qry[123456];   double Ans[123456];   int main() {     int u, v, c, d;     read(n);     read(m);     S = 1;     T = n;     for (int i = 1; i <= m; i++)       read(u), read(v), read(d), c = 1, link(u, v, d, c);     read(_);     for (int i = 0; i < _; i++) read(Qry[i]);     double ans = 1e20;     int m = 0;     for (int i = 0; i < _; i++) Ans[i] = 1e20;     while (SPFA()) {       m++;       for (int i = 0; i < _; i++)         Ans[i] = min(Ans[i], ((double)(Mincost + Qry[i])) / m);       ans = min(ans, (double)(Mincost + P) / m);     }     for (int i = 0; i < _; i++) printf( %.10f n , Ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long getint() {     char ch = getchar();     long long f = 1, x = 0;     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return f * x;   }   const int N = 200200;   int n;   struct dat {     long long w, h, c;   } a[N];   int bg[N], ed[N];   int cnt;   int ans;   bool operator<(const dat &a, const dat &b) {     return (a.w == b.w) ? a.h < b.h : a.w < b.w;   }   long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }   bool validate() {     for (int i = 2; i <= cnt; i++) {       if (ed[i] - bg[i] != ed[i - 1] - bg[i - 1]) return false;       for (int j = bg[i], k = bg[i - 1]; j <= ed[i]; j++, k++) {         if (a[j].h != a[k].h) return false;       }       long long g1, g2;       for (int j = bg[i], k = bg[i - 1]; j < ed[i]; j++, k++) {         g1 = gcd(a[j].c, a[j + 1].c);         g2 = gcd(a[k].c, a[k + 1].c);         if (a[j].c / g1 != a[k].c / g2 || a[j + 1].c / g1 != a[k + 1].c / g2)           return false;       }     }     return true;   }   void init() {     n = getint();     for (int i = 1; i <= n; i++) {       a[i].w = getint();       a[i].h = getint();       a[i].c = getint();     }     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n; i++) {       ++cnt;       bg[cnt] = i;       while (a[i].w == a[i + 1].w) i++;       ed[cnt] = i;     }   }   int main() {     init();     if (!validate()) {       puts( 0 );       return 0;     }     long long g = a[1].c;     for (int i = 2; i <= n; i++) {       g = gcd(g, a[i].c);     }     for (long long i = 1; i * i <= g; i++) {       if (g % i == 0) {         ans += (i * i == g) ? 1 : 2;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   priority_queue<long long, vector<long long>, greater<long long>> q;   int a[100005];   int main() {     int n, x;     long long b = 0, s;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) {       scanf( %d , &x);       s = 0;       q.push(a[i] + b);       while (!q.empty() && q.top() <= b + x) {         s += q.top() - b;         q.pop();       }       printf( %I64d  , s + (long long)q.size() * x);       b += x;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   using minpq = priority_queue<T, vector<T>, greater<T>>;   struct node {     int val, le, ri;     node() : val(1e9 + 5), le(0), ri(0) {}   };   const int N = 1e5 + 5;   int n, r, m, p, q, u, v, a[N], depth[N], root[N], tin[N], tout[N], ti = 1;   vector<int> adj[N];   bool vis[N];   vector<node> tree;   void dfs(int x) {     vis[x] = true;     tin[x] = ti++;     for (int y : adj[x]) {       if (!vis[y]) {         depth[y] = 1 + depth[x];         dfs(y);       }     }     tout[x] = ti;   }   int build(int l, int r) {     int k = ((int)(tree).size());     tree.emplace_back();     if (l < r) {       int m = (l + r) / 2;       tree[k].le = build(l, m);       tree[k].ri = build(m + 1, r);       tree[k].val = min(tree[tree[k].le].val, tree[tree[k].ri].val);     }     return k;   }   int upd(int i, int l, int r, int idx, int x) {     int k = ((int)(tree).size());     tree.emplace_back();     if (l < r) {       int m = (l + r) / 2;       if (idx <= m) {         tree[k].le = upd(tree[i].le, l, m, idx, x);         tree[k].ri = tree[i].ri;       } else {         tree[k].le = tree[i].le;         tree[k].ri = upd(tree[i].ri, m + 1, r, idx, x);       }       tree[k].val = min(tree[tree[k].le].val, tree[tree[k].ri].val);     } else {       tree[k].val = x;     }     return k;   }   int query(int i, int l, int r, int L, int R) {     if (r < L || R < l) return 1e9 + 5;     if (L <= l && r <= R) return tree[i].val;     int m = (l + r) / 2;     return min(query(tree[i].le, l, m, L, R), query(tree[i].ri, m + 1, r, L, R));   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> r;     for (int i = (1); i < (n + 1); i++) {       cin >> a[i];     }     for (int i = (0); i < (n - 1); i++) {       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs(r);     vector<pair<int, int>> ve;     for (int i = (1); i < (n + 1); i++) {       ve.emplace_back(depth[i], i);     }     sort((ve).begin(), (ve).end());     root[0] = build(1, n);     int prevdepth = 0;     for (auto &p : ve) {       int u = p.second;       root[depth[u]] = upd(root[prevdepth], 1, n, tin[u], a[u]);       prevdepth = depth[u];     }     cin >> m;     int last = 0;     while (m--) {       cin >> p >> q;       int x = 1 + ((p + last) % n);       int k = (q + last) % n;       last = query(root[min(prevdepth, depth[x] + k)], 1, n, tin[x], tout[x] - 1);       cout << last <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long a[200005];   long long b[200005];   vector<long long> edge[200005];   vector<long long> roots;   vector<bool> visited;   long long max_val[200005];   long long ans = 0;   long long dfs(long long v) {     visited[v] = true;     long long ret = 0;     for (long long next : edge[v]) {       if (!visited[next]) {         ret += max(dfs(next), (long long)0);       }     }     ans += ret + a[v];     return max_val[v] = ret + a[v];   }   void dfs2(long long v) {     visited[v] = true;     for (long long next : edge[v]) {       if (!visited[next]) {         if (max_val[next] >= 0) {           dfs2(next);         }       }     }     printf( %lld  , v);     for (long long next : edge[v]) {       if (!visited[next]) {         dfs2(next);       }     }   }   int main() {     long long n;     scanf( %lld , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld , a + i);     }     for (int j = 1; j <= n; j++) {       long long x;       scanf( %lld , &x);       if (x != -1) {         edge[x].push_back(j);       } else {         roots.push_back(j);       }     }     visited = vector<bool>(200005, false);     for (int i = 0; i < roots.size(); i++) {       dfs(roots[i]);     }     printf( %lld n , ans);     visited = vector<bool>(200005, false);     for (int i = 0; i < roots.size(); i++) {       dfs2(roots[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, n;     cin >> a >> b >> n;     long long l, t, m;     for (int i = 0; i < n; ++i) {       cin >> l >> t >> m;       long long sl = a + b * (l - 1), sr;       if (t < sl) {         cout << -1 << endl;         continue;       }       int lf = l, rt = (t - a) / b + 1;       int r = (lf + rt) / 2;       while (lf != r) {         sr = a + b * (r - 1);         if ((sl + sr) * (r - l + 1) / 2 <= t * m) {           lf = r;         } else {           rt = r - 1;         }         r = (lf + rt) / 2;       }       if ((sl + a + b * (rt - 1)) * (rt - l + 1) / 2 <= t * m) {         r = rt;       }       cout << r << endl;     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int32_t n;     std::cin >> n;     int32_t* arr = new int32_t[n];     for (int32_t i = 0; i < n; i++) {       std::cin >> arr[i];     }     std::string ans =   ;     char last =  b ;     for (int32_t i = n - 1; i >= 0; i--) {       if (arr[i] == 0) continue;       int32_t length = i + 1;       for (int32_t j = 0; j < arr[i]; j++) {         ans += std::string(length, last);         last = last ==  a  ?  b  :  a ;       }       for (int32_t j = i - 1; j >= 0; j--) arr[j] -= arr[i] * (i - j + 1);     }     std::cout << ans;     return 0;   };


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     scanf( %d %d , &a, &b);     if (a == 0 && b == 0)       printf( NO );     else if (fabs(a - b) == 1 || a - b == 0)       printf( YES );     else       printf( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int charToDec(char c) {     if ((c >=  0 ) && (c <=  9 )) return c -  0 ;     if ((c >=  A ) && (c <=  Z )) return c -  A  + 10;   }   char decToChar(int c) {     if ((c >= 0) && (c <= 9)) return c +  0 ;     if ((c >= 10) && (c <= 26)) return c +  A  - 10;   }   long long hzToDec(string s, int a) {     long long r = 0;     for (int i = 0; i < s.length(); i++) {       r *= a;       r += charToDec(s[i]);     }     return r;   }   string decToHz(long long x, int a) {     string s =   ;     while (x > 0) {       s = decToChar(x % a) + s;       x = x / a;     }     if (s ==   ) s =  0 ;     return s;   }   string toRome(long long X) {     string r[3100];     r[3000] =  MMM ;     r[2000] =  MM ;     r[1000] =  M ;     r[900] =  CM ;     r[800] =  DCCC ;     r[700] =  DCC ;     r[600] =  DC ;     r[500] =  D ;     r[400] =  CD ;     r[300] =  CCC ;     r[200] =  CC ;     r[100] =  C ;     r[90] =  XC ;     r[80] =  LXXX ;     r[70] =  LXX ;     r[60] =  LX ;     r[50] =  L ;     r[40] =  XL ;     r[30] =  XXX ;     r[20] =  XX ;     r[10] =  X ;     r[9] =  IX ;     r[8] =  VIII ;     r[7] =  VII ;     r[6] =  VI ;     r[5] =  V ;     r[4] =  IV ;     r[3] =  III ;     r[2] =  II ;     r[1] =  I ;     return r[X / 1000 * 1000] + r[(X % 1000) / 100 * 100] +            r[(X % 100) / 10 * 10] + r[X % 10];   }   int main() {     int a, b;     string bb, x;     long long x10;     cin >> a >> bb >> x;     x10 = hzToDec(x, a);     if (bb ==  R ) {       cout << toRome(x10);     } else       cout << decToHz(x10, atoi(bb.c_str()));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + ch -  0 ;       ch = getchar();     }     return x * f;   }   namespace mincost {   const int N = 1e5 + 10, M = 5e5 + 10;   int vis[N], dfn, head[N], cnt, Cnt, S, T, inf = 1e9, dis[N], cur[N];   ll Dis[N], INF = 1e18;   struct Edge {     int v, next;     ll f, c;   } e[M << 1];   inline void init(int _s, int _t, int _n) {     S = _s;     T = _t;     Cnt = _n;     for (register int i = 0; i < Cnt; ++i) head[i] = -1;     cnt = 0;   }   inline void add(int u, int v, ll ff, ll f, ll c, ll f2 = 0) {     e[cnt] = {v, head[u], f, c};     head[u] = cnt++;     e[cnt] = {u, head[v], f2, -c};     head[v] = cnt++;   }   inline void spfa() {     static int que[N];     static bool vis[N];     int p = 0, q = 0;     for (register int i = 0; i < Cnt; ++i) Dis[i] = inf;     Dis[S] = 0;     que[q++] = S;     vis[S] = 1;     while (p != q) {       int u = que[p++];       if (p == N) p = 0;       vis[u] = 0;       for (register int i = head[u]; ~i; i = e[i].next) {         int v = e[i].v, w = e[i].c;         if (e[i].f && Dis[v] > Dis[u] + w) {           Dis[v] = Dis[u] + w;           if (!vis[v]) {             vis[v] = 1;             que[q++] = v;             if (q == N) q = 0;           }         }       }     }   }   inline void Dijkstra() {     static pair<ll, ll> que[M];     static ll dis[N];     for (register int i = 0; i < Cnt; ++i) {       dis[i] = inf;       cur[i] = head[i];     }     dis[S] = 0;     int Head = 0;     que[Head++] = make_pair(0, S);     push_heap(que, que + Head);     while (Head > 0) {       auto x = que[0];       pop_heap(que, que + Head);       --Head;       int u = x.second;       if (dis[u] != -x.first) continue;       for (register int i = head[u]; ~i; i = e[i].next) {         int v = e[i].v, w = dis[u] + e[i].c + Dis[u] - Dis[v];         if (e[i].f && w < dis[v]) {           dis[v] = w;           que[Head++] = make_pair(-dis[v], v);           push_heap(que, que + Head);         }       }     }     for (register int i = 0; i < Cnt; ++i) Dis[i] += dis[i];   }   ll dinic(int u, ll flow) {     if (u == T) return flow;     ll ret = 0, f;     vis[u] = dfn;     for (register int i = cur[u]; ~i; cur[u] = i = e[i].next)       if (e[i].f && vis[e[i].v] != dfn && Dis[u] + e[i].c == Dis[e[i].v]) {         f = dinic(e[i].v, min(flow, e[i].f));         e[i].f -= f;         e[i ^ 1].f += f;         flow -= f;         ret += f;         if (!flow) break;       }     if (ret) vis[u] = -1;     return ret;   }   inline ll MCMF() {     for (register int i = 0; i < Cnt; ++i) Dis[i] = 0;     spfa();     ll res = 0, Flow = 0, f;     while (dfn++, Dijkstra(), f = dinic(S, INF)) {       Flow += f;       res += Dis[T] * f;     }     return res;   }   }  // namespace mincost   const int N = 210;   int n, a[N][N];   int main() {     n = read();     ll Res = 0, ans = 0;     for (register int i = 0; i < n; ++i)       for (register int j = 0; j < n; ++j) a[i][j] = read();     int S = (n - 2) * (n - 2), T = (n - 2) * (n - 2) + 1;     mincost::init(S, T, T + 1);     for (register int i = 1; i < n - 1; ++i)       for (register int j = 0; j < n - 1; ++j)         if (a[i][j] == 0 && a[i][j + 1] == 0)           mincost::add((i - 1) * (n - 2) + j - 1, (i - 1) * (n - 2) + j, 0, 1, 0,                        1);         else if (a[i][j] == 0 && a[i][j + 1] > 0) {           mincost::add((i - 1) * (n - 2) + j - 1, T, 0, 2, a[i][j + 1]);           Res += a[i][j + 1];           mincost::add(S, (i - 1) * (n - 2) + j - 1, 0, 1, 0);         } else if (a[i][j] > 0 && a[i][j + 1] == 0) {           mincost::add((i - 1) * (n - 2) + j, T, 0, 2, a[i][j]);           Res += a[i][j];           mincost::add(S, (i - 1) * (n - 2) + j, 0, 1, 0);         }     for (register int i = 0; i < n - 1; ++i)       for (register int j = 1; j < n - 1; ++j)         if (a[i][j] == 0 && a[i + 1][j] == 0)           mincost::add((i - 1) * (n - 2) + j - 1, i * (n - 2) + j - 1, 0, 1, 0,                        1);         else if (a[i][j] == 0 && a[i + 1][j] > 0) {           mincost::add((i - 1) * (n - 2) + j - 1, T, 0, 2, a[i + 1][j]);           Res += a[i + 1][j];           mincost::add(S, (i - 1) * (n - 2) + j - 1, 0, 1, 0);         } else if (a[i][j] > 0 && a[i + 1][j] == 0) {           mincost::add(i * (n - 2) + j - 1, T, 0, 2, a[i][j]);           Res += a[i][j];           mincost::add(S, i * (n - 2) + j - 1, 0, 1, 0);         }     for (register int i = 0; i < n - 1; ++i) ans += abs(a[0][i] - a[0][i + 1]);     for (register int i = 0; i < n - 1; ++i)       ans += abs(a[n - 1][i] - a[n - 1][i + 1]);     for (register int i = 0; i < n - 1; ++i) ans += abs(a[i][0] - a[i + 1][0]);     for (register int i = 0; i < n - 1; ++i)       ans += abs(a[i][n - 1] - a[i + 1][n - 1]);     printf( %lld n , ans - mincost::MCMF() + Res);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(4)   using namespace std;   char _buf[100000], *_p1 = _buf, *_p2 = _buf;   inline int gi() {     int x = 0, f = 1;     char ch = (_p1 == _p2 && (_p2 = (_p1 = _buf) + fread(_buf, 1, 100000, stdin),                               _p1 == _p2)                    ? EOF                    : *_p1++);     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) f = -1;       ch = (_p1 == _p2 && (_p2 = (_p1 = _buf) + fread(_buf, 1, 100000, stdin),                            _p1 == _p2)                 ? EOF                 : *_p1++);     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 3) + (x << 1) + (ch ^ 48);       ch = (_p1 == _p2 && (_p2 = (_p1 = _buf) + fread(_buf, 1, 100000, stdin),                            _p1 == _p2)                 ? EOF                 : *_p1++);     }     return (f == 1) ? x : -x;   }   inline int max(int a, int b) { return a > b ? a : b; }   inline int min(int a, int b) { return a < b ? a : b; }   const int maxn = 5e5 + 5;   int n, p[maxn], sz, maxpos, maxdis, fa[maxn], Q;   struct node {     int u, v, w;   };   vector<node> edg;   struct edge {     int v, w, nxt;     edge(int vv = 0, int ww = 0, int nn = 0) { v = vv, w = ww, nxt = nn; }   } e[maxn << 1];   inline void add(int u, int v, int w) {     e[++sz] = edge(v, w, p[u]);     p[u] = sz;   }   struct segmentree {     static const int maxn = 2e6 + 5;     int rt;     int dep[maxn], tot, dfn[maxn], siz[maxn], len[maxn], rk[maxn];     bool tag[maxn];     int maxv[maxn][2];     inline void pushup(int u) {       for (int i = 0; i <= 1; ++i) {         if (maxv[u << 1][i] > maxv[u << 1 | 1][i])           maxv[u][i] = maxv[u << 1][i];         else           maxv[u][i] = maxv[u << 1 | 1][i];       }     }     inline void pushdown(int u) {       if (tag[u]) {         swap(maxv[u << 1][0], maxv[u << 1][1]);         swap(maxv[u << 1 | 1][0], maxv[u << 1 | 1][1]);         tag[u << 1] ^= 1, tag[u << 1 | 1] ^= 1, tag[u] = 0;       }     }     inline void dfs(int u, int fa) {       dfn[u] = ++tot;       siz[u] = 1;       rk[tot] = u;       for (int i = p[u]; i != -1; i = e[i].nxt) {         int v = e[i].v;         if (v == fa) continue;         if (e[i].w == 1)           len[v] = len[u] ^ 1;         else           len[v] = len[u];         dep[v] = dep[u] + 1;         dfs(v, u);         siz[u] += siz[v];       }     }     inline void build(int u, int l, int r) {       if (l == r) {         maxv[u][len[rk[l]]] = dep[rk[l]];         return;       }       int mid = (l + r) >> 1;       build(u << 1, l, mid);       build(u << 1 | 1, mid + 1, r);       pushup(u);     }     inline int query(int u, int l, int r, int x, int y) {       if (x <= l && y >= r) return maxv[u][0];       int mid = (l + r) >> 1, ret = 0;       pushdown(u);       if (x <= mid) ret = query(u << 1, l, mid, x, y);       if (y > mid) ret = max(ret, query(u << 1 | 1, mid + 1, r, x, y));     }     inline void update(int u, int l, int r, int x, int y) {       if (x <= l && y >= r) {         tag[u] ^= 1;         swap(maxv[u][0], maxv[u][1]);         return;       }       int mid = (l + r) >> 1;       pushdown(u);       if (x <= mid) update(u << 1, l, mid, x, y);       if (y > mid) update(u << 1 | 1, mid + 1, r, x, y);       pushup(u);     }     inline void change(int u, int v) {       if (dep[u] > dep[v]) swap(u, v);       update(1, 1, n, dfn[v], dfn[v] + siz[v] - 1);     }   } tree1, tree2;   inline void dfs2(int u, int f, int dis) {     if (dis > maxdis) {       maxpos = u, maxdis = dis;     }     for (int i = p[u]; i != -1; i = e[i].nxt) {       int v = e[i].v;       if (v == f) continue;       dfs2(v, u, dis + 1);     }   }   inline void init() {     dfs2(1, 0, 0);     tree1.rt = maxpos;     maxdis = 0;     dfs2(maxpos, 0, 0);     tree2.rt = maxpos;     tree1.dfs(tree1.rt, 0);     tree2.dfs(tree2.rt, 0);     tree1.build(1, 1, n);     tree2.build(1, 1, n);   }   inline void input() {     memset(p, -1, sizeof(p));     n = gi();     node x;     edg.push_back(x);     for (int i = 1; i <= n - 1; ++i) {       x.u = gi(), x.v = gi(), x.w = gi();       edg.push_back(x);       add(x.u, x.v, x.w);       add(x.v, x.u, x.w);     }   }   inline void solve() {     Q = gi();     while (Q--) {       int x = gi();       int u = edg[x].u, v = edg[x].v;       tree1.change(u, v);       tree2.change(u, v);       printf( %d n , max(tree1.query(1, 1, n, 1, n), tree2.query(1, 1, n, 1, n)));     }   }   int main() {     input();     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     string s1( ABC ), s2( ACB ), s3( BAC ), s4( BCA ), s5( CAB ), s6( CBA );     int state = 0;     if (s.find(s1) != -1)       state = 1;     else if (s.find(s2) != -1)       state = 1;     else if (s.find(s3) != -1)       state = 1;     else if (s.find(s4) != -1)       state = 1;     else if (s.find(s5) != -1)       state = 1;     else if (s.find(s6) != -1)       state = 1;     else       state = 0;     if (state == 0)       cout <<  No  << endl;     else       cout <<  Yes  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int q;     cin >> q;     for (int qq = 0; qq < int(q); qq++) {       string s;       cin >> s;       int n;       cin >> n;       vector<int> b(n);       for (int i = 0; i < int(n); i++) cin >> b[i];       vector<vector<int>> groups;       while (true) {         vector<int> pos;         for (int i = 0; i < int(n); i++)           if (b[i] == 0) pos.push_back(i);         if (pos.empty()) break;         groups.push_back(pos);         for (int i = 0; i < int(n); i++)           if (b[i] == 0)             b[i] = INT_MAX;           else             for (int pp : pos) b[i] -= abs(i - pp);       }       map<char, int> cnts;       for (int i = 0; i < int(s.size()); i++) cnts[s[i]]++;       auto j = cnts.rbegin();       string t(n,  ? );       for (auto g : groups) {         while (j->second < g.size()) j++;         for (int pp : g) t[pp] = j->first;         j++;       }       cout << t << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   int n, m, q, u, v, w, l, r, ans, par[N], col[N], mark[N];   vector<pair<int, pair<int, int> > > yal, edge[N * N];   vector<int> cmp[N];   void add(int x) {     for (int i = 0; i < edge[x].size(); i++) yal.push_back(edge[x][i]);   }   int Get_par(int u) {     if (par[u] < 0) return u;     return par[u] = Get_par(par[u]);   }   void query(int l, int r) {     r++;     for (l += m, r += m; l < r; l >>= 1, r >>= 1) {       if (l & 1) add(l++);       if (r & 1) add(--r);     }   }   void reset(int x) {     cmp[x].clear();     cmp[x].push_back(x);   }   void merge(int u, int v, int s) {     u = Get_par(u), v = Get_par(v);     if (par[u] > par[v]) swap(u, v);     for (int i = 0; i < cmp[v].size(); i++)       cmp[u].push_back(cmp[v][i]), col[cmp[v][i]] ^= s;     par[u] += par[v], par[v] = u;   }   void solve(int x) {     vector<pair<int, pair<int, int> > > vec;     for (int i = 0; i < edge[x].size(); i++)       par[edge[x][i].second.first] = par[edge[x][i].second.second] = -1,       reset(edge[x][i].second.first), reset(edge[x][i].second.second);     for (int i = 0; i < edge[x].size(); i++) {       u = edge[x][i].second.first, v = edge[x][i].second.second;       if (Get_par(u) == Get_par(v)) {         if (col[u] == col[v]) {           vec.push_back(edge[x][i]);           break;         }       } else         merge(u, v, col[u] == col[v]), vec.push_back(edge[x][i]);     }     edge[x].clear();     edge[x] = vec;   }   void merge_t(int u, int v, int an) {     int p1 = 0, p2 = 0;     while (p1 < edge[u].size() || p2 < edge[v].size()) {       if (p2 == edge[v].size() ||           (p1 < edge[u].size() && edge[u][p1].first > edge[v][p2].first))         edge[an].push_back(edge[u][p1++]);       else         edge[an].push_back(edge[v][p2++]);     }   }   void build() {     for (int i = m - 1; i >= 1; i--) {       merge_t(i * 2, i * 2 + 1, i);       solve(i);     }   }   void clear() {     ans = -1, yal.clear();     fill(col, col + N, 0);     fill(par, par + N, -1);     for (int i = 1; i < n + 1; i++) reset(i);   }   int main() {     cin >> n >> m >> q;     for (int i = 0; i < m; i++) {       scanf( %d%d , &u, &v);       ;       scanf( %d , &w);       ;       edge[i + m].push_back(make_pair(w, make_pair(u, v)));     }     build();     for (int io = 0; io < q; io++) {       clear();       scanf( %d%d , &l, &r);       ;       --l, --r;       query(l, r);       sort(yal.begin(), yal.end());       reverse(yal.begin(), yal.end());       for (int i = 0; i < yal.size(); i++) {         u = yal[i].second.first, v = yal[i].second.second;         if (Get_par(u) == Get_par(v)) {           if (col[u] == col[v]) {             ans = yal[i].first;             break;           }         } else           merge(u, v, col[u] == col[v]);       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long mod_v(long long num) {     if (num >= 0)       return (num % 1000000007);     else       return (num % 1000000007 + 1000000007) % 1000000007;   }   long long bigmod(long long b, long long p, long long m) {     long long res = 1 % m, x = b % m;     while (p) {       if (p & 1) res = (res * x) % m;       x = (x * x) % m;       p >>= 1;     }     return res;   }   class node {    public:     string str;     int a, b;     node() {}     node(string str, int a, int b) {       this->str = str;       this->a = a;       this->b = b;     }   };   vector<node> v;   map<string, bool> mark;   int main() {     int n, K, temp;     scanf( %d , &K);     n = 1 << K;     if (n == 1) {       printf( + n );       return 0;     }     string str =   ;     for (int i = 1; i <= n / 2; i++) str +=  + ;     for (int i = 1; i <= n / 2; i++) str +=  * ;     v.push_back(node(str, -1, -1));     mark[str] = 1;     int x = n / 4;     for (int i = 1; i <= x; i = i * 2) {       str =   ;       temp = 1;       for (int j = 1; j <= n; j += i) {         temp = (temp + 1) % 2;         for (int k = 0; k < i; k++) {           if (temp == 0)             str +=  + ;           else             str +=  * ;         }       }       v.push_back(node(str, -1, -1));       mark[str] = 1;     }     int ind = 0, len;     while (v.size() < n) {       bool flag = false;       for (int i = 0; i < v.size() && i < n; i++) {         if (v[ind].a == i || v[ind].b == i || ind == i) continue;         str =   ;         int o = 0, e = 0;         for (int j = 0; j < n; j++) {           if (v[ind].str[j] == v[i].str[j]) {             str +=  + ;             o++;           } else {             str +=  * ;             e++;           }         }         if (mark[str] == 1 || o != e) continue;         v.push_back(node(str, ind, i));         mark[str] = 1;         flag = true;       }       if (!flag) break;       ind++;     }     if (v.size() < n) {       str =   ;       for (int i = 0; i < n; i++) str +=  + ;       v.push_back(node(str, -1, -1));     }     for (int i = 0; i < v.size(); i++) cout << v[i].str <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c = getchar();     while (c !=  -  && c < 48) c = getchar();     if (c ==  - ) f = -1, c = getchar();     while (c > 47) x = x * 10 + (c ^ 48), c = getchar();     return f * x;   }   const int maxn = 3e3 + 7;   const int inf = 0x3f3f3f3f;   const int mod = 1e9 + 7;   int t[maxn];   inline void Max(long long &x, long long y) {     if (x < y) x = y;   }   int main() {     int n = read(), k = read();     vector<vector<long long>> a(n + 1, vector<long long>(k + 1, 0));     for (int i = 1; i <= n; ++i) {       int t = read();       ::t[i] = t;       for (int j = 1; j <= t; ++j) {         int d = read();         if (j <= k) a[i][j] = a[i][j - 1] + d;       }     }     long long ans = 0;     function<void(int, int, vector<long long> &)> solve =         [&](int l, int r, vector<long long> &dp) {           if (l == r) {             for (int i = 0, up = min(k, t[l]); i <= up; ++i) {               Max(ans, dp[k - i] + a[l][i]);             }             return;           }           int m = (l + r) / 2;           vector<long long> dp1(dp);           for (int i = m + 1; i <= r; ++i) {             for (int j = k; j >= t[i]; --j) {               Max(dp1[j], dp1[j - t[i]] + a[i][t[i]]);             }           }           solve(l, m, dp1);           for (int i = l; i <= m; ++i) {             for (int j = k; j >= t[i]; --j) {               Max(dp[j], dp[j - t[i]] + a[i][t[i]]);             }           }           solve(m + 1, r, dp);         };     vector<long long> dp(k + 1, -1e18);     dp[0] = 0;     solve(1, n, dp);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int Direction[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};   const int Nmax = 303000;   long double n, m;   long double ans = 0;   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m;     long double r = n * m;     for (int k = 1; k <= min(n, m); k++) {       long double add = 1. * k * k / (long double)n;       for (double i = n - k + 1; i <= n; i++)         add = (add * (long double)(i)) / (long double)(i - n + k);       for (double i = r - m - n + k + 1; i <= r - n; i++)         add = (add * (long double)(i)) / (long double)(i + n - k);       for (double i = 1; i <= k; i++)         add = (add) * (m - k + i) / (long double)(r - k + i);       ans += add;     }     cout.precision(16);     cout << fixed << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int a[n + 1];       int b[2 * n + 1];       int temp[2 * n + 1];       for (int i = 1; i <= 2 * n; i++) temp[i] = 0;       bool con1 = false, con2 = false;       for (int i = 1; i <= n; i++) {         cin >> a[i];         if (a[i] == 1) con1 = true;         if (a[i] == 2 * n) con2 = true;         temp[a[i]] = 1;       }       if (con1 == false or con2 == true)         cout <<  -1  << endl;       else {         int check = 0;         for (int i = 1; i <= n; i++) {           b[2 * i - 1] = a[i];           int j;           for (j = a[i] + 1; j <= 2 * n; j++) {             if (temp[j] == 0) {               temp[j] = 1;               b[2 * i] = j;               break;             }           }           if (j == 2 * n + 1) {             check = -1;           }         }         if (check != -1) {           for (int i = 1; i <= 2 * n; i++) cout << b[i] <<    ;           cout << endl;         } else           cout <<  -1  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 998244353;   double eps = 1e-8;   vector<int> b, c;   int vis[2100000], a[210000], ans[210000], x;   int read() {     int v = 0, f = 1;     char c = getchar();     while (c < 48 || 57 < c) {       if (c ==  - ) f = -1;       c = getchar();     }     while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar();     return v * f;   }   void ins(int u) {     int t = u;     for (int &v : b) u = min(u, u ^ v);     if (u) {       b.push_back(u);       c.push_back(t);       for (int i = (int)(int)b.size() - 1; i >= (int)1; i--)         if (b[i] > b[i - 1])           swap(b[i], b[i - 1]);         else           break;     }   }   void dfs(int u, int s) {     ans[s] = u;     vis[u] = 1;     if (s == (1 << x) - 1) return;     for (int &v : c)       if (!vis[u ^ v]) {         dfs(u ^ v, s + 1);         return;       }   }   int main() {     int n = read();     for (int i = (int)1; i <= (int)n; i++) a[i] = read();     sort(a + 1, a + n + 1);     x = 0;     int j = 1;     for (int i = (int)1; i <= (int)19; i++) {       while (j <= n && a[j] <= ((1 << i) - 1)) {         ins(a[j]);         j++;       }       if (b.size() == i) x = i;     }     b.clear();     c.clear();     for (int i = (int)1; i <= (int)n; i++)       if (a[i] <= ((1 << x) - 1))         ins(a[i]);       else         break;     dfs(0, 0);     printf( %d n , x);     for (int i = (int)0; i <= (int)(1 << x) - 1; i++) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const int MAX = 1e9 + 5;   int i, j, w1, w2, k, n, m, a[N], b[N], calca[N], calcb[N], temp;   vector<pair<int, int> > ans, adasd;   bool ctr(int s) {     int set = 0;     int p1 = 0;     int p2 = 0;     int last1 = 0;     int last2 = 0;     int lastt = 0;     while (1) {       if (w1 == last1 && w2 == last2)         if (((p1 > p2 && lastt == 1) || (p2 > p1 && lastt == 2)) && p1 != p2) {           temp = max(p1, p2);           return true;         } else           return false;       if (last2 + s <= w2 && b[last2 + s] < a[last1 + s]) {         p2++;         lastt = 2;         last2 += s;         last1 = lower_bound(a + 1, a + w1 + 1, b[last2]) - a - 1;       } else if (last1 + s <= w1 && a[last1 + s] < b[last2 + s]) {         p1++;         lastt = 1;         last1 += s;         last2 = lower_bound(b + 1, b + w2 + 1, a[last1]) - b - 1;       } else         return false;     }   }   int main() {     cin >> n;     int asdasdasda;     for (int i = 1; i <= 200001; i++) a[i] = b[i] = 100000000;     int x;     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       if (x == 1)         a[++w1] = i;       else         b[++w2] = i;     }     for (int i = 1; i <= n; i++)       if (ctr(i)) ans.push_back(make_pair(temp, i));     sort(ans.begin(), ans.end());     cout << ans.size() <<   n ;     for (int i = 0; i <= (int)ans.size() - 1; i++)       cout << ans[i].first <<     << ans[i].second <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = int64_t;   struct node {     int C[2];     ll good;     ll bad;     char mrk;     node() : C{0, 0}, good{0}, bad{0}, mrk{0} {}     int& operator[](int i) { return C[i]; }   };   using trie_t = vector<node>;   trie_t T;   string curr;   struct Guard {     Guard(char c) { curr.push_back(c); }     ~Guard() { curr.pop_back(); }   };   int insert(const string& s, int i = 0, int r = 0) {     if (i == s.size()) return r;     if (!T[r][s[i] -  0 ]) {       T[r][s[i] -  0 ] = T.size();       T.emplace_back();     }     return insert(s, i + 1, T[r][s[i] -  0 ]);   }   string read() {     string ans;     int a, b, c, d, x;     scanf(  %d.%d.%d.%d , &a, &b, &c, &d);     if (getchar() !=  / )       x = 32;     else       scanf( %d , &x);     ll num = a;     num = (num << 8) + b;     num = (num << 8) + c;     num = (num << 8) + d;     num >>= (32 - x);     for (; x > 0; x--, num >>= 1) ans.push_back( 0  + char(num & 1ll));     reverse(begin(ans), end(ans));     return ans;   }   void sum(int r) {     T[r].good = (T[r].mrk ==  + );     T[r].bad = (T[r].mrk ==  - );     if (T[r][0]) {       sum(T[r][0]);       T[r].good += T[T[r][0]].good;       T[r].bad += T[T[r][0]].bad;     }     if (T[r][1]) {       sum(T[r][1]);       T[r].good += T[T[r][1]].good;       T[r].bad += T[T[r][1]].bad;     }   }   bool imp = false;   vector<string> ans;   void solve(int r) {     if (T[r].good * T[r].bad == 0) {       if (T[r].bad > 0) {         ans.push_back(curr);       }       return;     }     if ((T[r].mrk ==  +  && T[r].bad > 0) || (T[r].mrk ==  -  && T[r].good > 0)) {       imp = true;       return;     }     if (T[r][0]) {       Guard g( 0 );       solve(T[r][0]);     }     if (T[r][1]) {       Guard g( 1 );       solve(T[r][1]);     }   }   void display(string s) {     int a, b, c, d, x;     x = s.size();     ll num = 0;     for (int i = 0; i < s.size(); i++) num = 2ll * num + (s[i] -  0 );     num <<= (32 - x);     d = num % (1 << 8);     num >>= 8;     c = num % (1 << 8);     num >>= 8;     b = num % (1 << 8);     num >>= 8;     a = num % (1 << 8);     num >>= 8;     printf( %d.%d.%d.%d/%d n , a, b, c, d, x);   }   int main() {     int n;     int root = 0;     T.emplace_back();     scanf(  %d , &n);     for (int i = 0; i < n; i++) {       char c;       while (isspace(c = getchar()))         ;       int leaf = insert(read());       if (T[leaf].mrk) {         if (T[leaf].mrk != c) imp = true;       } else         T[leaf].mrk = c;     }     sum(root);     solve(root);     if (imp) {       puts( -1 );       return 0;     }     printf( %d n , ans.size());     for (auto s : ans) display(s);   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     long long n, k;     cin >> n >> k;     vector<vector<char>> board(n, vector<char>(n,  . ));     vector<vector<long long>> cnt(n, vector<long long>(n, 0));     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < n; j++) {         cin >> board[i][j];       }     }     for (long long i = 0; i < n; i++) {       for (long long j = 0; j <= n - k; j++) {         bool flag = true;         for (long long x = j; x < j + k; x++) {           if (board[i][x] !=  . ) {             flag = false;             break;           }         }         if (!flag) continue;         for (long long x = j; x < j + k; x++) {           cnt[i][x]++;         }       }     }     for (long long i = 0; i <= n - k; i++) {       for (long long j = 0; j < n; j++) {         bool flag = true;         for (long long x = i; x < i + k; x++) {           if (board[x][j] !=  . ) {             flag = false;             break;           }         }         if (!flag) continue;         for (long long x = i; x < i + k; x++) {           cnt[x][j]++;         }       }     }     long long mxX = 0, mxY = 0;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < n; j++) {         if (cnt[i][j] > cnt[mxX][mxY]) {           mxX = i;           mxY = j;         }       }     }     cout << mxX + 1 <<     << mxY + 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const N = 1e5 + 10;   using ll = long long;   ll const INF = 0x3f3f3f3f3f3f3f3fll;   struct edge {     ll to, cost, id;     edge(ll to = 0, ll cost = 0, ll id = 0) : to(to), cost(cost), id(id) {}   };   vector<edge> e[N];   ll d[N];   vector<ll> p[N];   ll n, m, k;   queue<ll> q;   bool inq[N];   bool v[N];   bool can_change[N];   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> m >> k;     for (ll i = 0, u, v, w; i < m; ++i) {       cin >> u >> v >> w;       e[u].push_back(edge(v, w, i + 1));       e[v].push_back(edge(u, w, i + 1));     }     for (ll i = 0, s, y; i < k; ++i) {       cin >> s >> y;       e[1].push_back(edge(s, y, m + i + 1));       e[s].push_back(edge(1, y, m + i + 1));     }     memset(d, 0x3f, sizeof(d));     memset(can_change, true, sizeof(can_change));     d[1] = 0;     q.push(1);     inq[1] = true;     while (!q.empty()) {       ll x = q.front();       q.pop();       inq[x] = false;       for (auto &ed : e[x]) {         ll y = ed.to, w = ed.cost, id = ed.id;         if (d[y] > d[x] + w) {           d[y] = d[x] + w;           p[y].clear();           if (id > m) {             can_change[y] = true;             p[y].push_back(id);           } else {             can_change[y] = false;           }           if (!inq[y]) {             inq[y] = true;             q.push(y);           }         } else if (d[y] == d[x] + w) {           if (id > m) {             if (can_change[y]) p[y].push_back(id);           } else {             can_change[y] = false;             p[y].clear();           }         }       }     }     for (ll i = 1; i <= n; ++i)       if (!p[i].empty()) {         bool skip = false;         for (auto id : p[i]) {           if (v[id - m]) {             skip = true;             break;           }         }         if (!skip) v[p[i].front() - m] = true;       }     ll cnt = 0;     for (ll i = 1; i <= k; ++i)       if (!v[i]) ++cnt;     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, i, j, l = 0;     cin >> k >> i;     for (j = 1; j <= 10; j++) {       l = k * j;       if (l % 10 == 0 || l % 10 == i) {         cout << j;         break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long int m, n, f, h[200000], fakt[200000] = {1};   vector<int> v[200000];   long long int fe(long long int m, long long int n) {     if (!n) return 1;     long long int t = fe(m, n / 2);     t = t * t % 1000000007LL;     if (n & 1) t = t * m % 1000000007LL;     return t;   }   long long int comb(long long int m, long long int n) {     return fakt[m] * fe(fakt[n], 1000000007LL - 2) % 1000000007LL *            fe(fakt[m - n], 1000000007LL - 2) % 1000000007LL;   }   long long int find(long long int n) { return comb(n - 1, f - 1); }   long long int rec(int ind, long long int carp, int s) {     if (n / carp < f) return 0;     if (ind == v[n].size()) {       if (s & 1)         return (-find(n / carp) + 1000000007LL) % 1000000007LL;       else         return find(n / carp);     }     return (rec(ind + 1, carp, s) + rec(ind + 1, carp * v[n][ind], s + 1)) %            1000000007LL;   }   int main() {     ios_base::sync_with_stdio(false);     for (int i = 2; i <= 100005; i++)       if (!h[i]) {         for (int j = 2 * i; j <= 100005; j += i) {           v[j].push_back(i);           h[j] = 1;         }       }     for (int i = 1; i <= 100005; i++) fakt[i] = fakt[i - 1] * i % 1000000007LL;     cin >> m;     while (m--) {       cin >> n >> f;       if (f == 1 and n != 1)         cout << 0 << endl;       else if (f == 1)         cout << 1 << endl;       else         cout << rec(0, 1, 0) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long T;     cin >> T;     while (T--) {       long long n, a[100005];       cin >> n;       for (long long i = 1; i <= n; i++) {         cin >> a[i];       }       long long ans = 0;       for (long long i = 1; i < n; i++) {         long long t = a[i] * a[i + 1];         ans = max(ans, t);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Nx = 200005;   int ct[Nx], MMsize, A, B, tree[Nx * 4];   map<int, int> MM;   vector<int> pos[Nx];   int query(int a, int b, int key) {     if (A <= a && b <= B) return tree[key];     if (B < a || b < A) return 0;     int m = (a + b) / 2;     return query(a, m, key * 2) + query(m + 1, b, key * 2 + 1);   }   void insert(int a, int b, int key) {     if (A < a || A > b) return;     tree[key]++;     if (a == b) return;     int m = (a + b) / 2;     insert(a, m, key * 2);     insert(m + 1, b, key * 2 + 1);   }   int main() {     int N;     scanf( %d , &N);     for (int i = 1, v, p; i <= N; i++) {       scanf( %d , &v);       if ((p = MM[v]) == 0) p = MM[v] = ++MMsize;       pos[p].push_back(i);     }     for (map<int, int>::iterator i = MM.begin(); i != MM.end(); i++) {       int p = i->second;       for (vector<int>::iterator j = pos[p].begin(); j != pos[p].end(); j++) {         int v = *j;         for (int k = 1; k < N; k++) {           if ((k * (v - 1) + 2) > N) break;           A = k * (v - 1) + 2;           B = min(N, k * v + 1);           ct[k] += query(1, N, 1);         }       }       for (vector<int>::iterator j = pos[p].begin(); j != pos[p].end(); j++) {         A = *j;         insert(1, N, 1);       }     }     for (int i = 1; i < N; i++) {       printf( %d , ct[i]);       if (i == (N - 1))         putchar(  n );       else         putchar(32);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, M;   vector<int> adj[100002], f, radj[100002];   queue<int> Q1, Q2;   vector<int> vis;   int main(int argc, char **argv) {     cin >> N >> M;     f = vector<int>(N);     for (int i = (0); i < (N); ++i) cin >> f[i];     for (int i = (0); i < (M); ++i) {       int first, second;       cin >> first >> second;       --first, --second;       adj[first].push_back(second);       radj[second].push_back(first);     }     vis = vector<int>(N);     for (int i = (0); i < (N); ++i)       if (f[i] == 1)         Q1.push(i), vis[i] |= 1;       else if (f[i] == 2)         Q2.push(i), vis[i] |= 2;     while (!Q1.empty()) {       int u = Q1.front();       Q1.pop();       for (int i = (0); i < (adj[u].size()); ++i)         if (!(vis[adj[u][i]] & 1) && f[adj[u][i]] != 1) {           vis[adj[u][i]] |= 1;           Q1.push(adj[u][i]);         }     }     while (!Q2.empty()) {       int u = Q2.front();       Q2.pop();       for (int i = (0); i < (radj[u].size()); ++i)         if (!(vis[radj[u][i]] >> 1)) {           vis[radj[u][i]] |= 2;           if (f[radj[u][i]] != 1) Q2.push(radj[u][i]);         }     }     for (int i = (0); i < (N); ++i) printf( %d n , vis[i] == 3);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> disTime, finTime, depth, a;   vector<vector<int> > graph;   int n, maxSize, tr0[200005], tr1[200005];   vector<bool> vis;   int tim;   void update(int in, int val, int* tr) {     while (in <= maxSize) {       tr[in] += val;       in += (in & (-in));     }   }   int getSum(int in, int* tr) {     int sum = 0;     while (in) {       sum += tr[in];       in -= (in & (-in));     }     return sum;   }   void dfs(int idx, int d) {     vis[idx] = true;     depth[idx] = d;     disTime[idx] = tim++;     for (auto ch : graph[idx]) {       if (vis[ch]) continue;       dfs(ch, d + 1);     }     finTime[idx] = tim;   }   int main() {     int m, u, v, x;     cin >> n >> m;     a.resize(n);     graph.resize(n);     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n - 1; i++) {       scanf( %d %d , &u, &v);       graph[u - 1].push_back(v - 1);       graph[v - 1].push_back(u - 1);     }     disTime.resize(n);     finTime.resize(n);     depth.resize(n);     vis.resize(n);     tim = 1;     for (int i = 0; i < n; i++) vis[i] = false;     dfs(0, 0);     maxSize = tim;     for (int i = 0; i < maxSize + 1; i++) tr1[i] = tr0[i] = 0;     for (int i = 0; i < m; i++) {       scanf( %d , &x);       switch (x) {         case 1:           scanf( %d %d , &u, &v);           if (depth[u - 1] % 2) {             update(disTime[u - 1], v, tr1);             update(finTime[u - 1], -v, tr1);             update(disTime[u - 1], -v, tr0);             update(finTime[u - 1], v, tr0);           } else {             update(disTime[u - 1], v, tr0);             update(finTime[u - 1], -v, tr0);             update(disTime[u - 1], -v, tr1);             update(finTime[u - 1], v, tr1);           }           break;         case 2:           scanf( %d , &u);           if (depth[u - 1] % 2)             printf( %d n , (a[u - 1] + getSum(disTime[u - 1], tr1)));           else             printf( %d n , (a[u - 1] + getSum(disTime[u - 1], tr0)));           break;       }     }     disTime.clear();     finTime.clear();     depth.clear();     vis.clear();     graph.clear();     a.clear();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 2010;   int A[M][M];   int col[M];   int diag[M];   int main() {     ios_base::sync_with_stdio(false);     int i, j, k, n;     char str[M];     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %s , str);       for (j = 0; j < n; j++) A[i][j] = str[j] -  0 ;     }     int sum = 0;     for (i = n - 1; i > 0; i--) {       int inv = 0;       for (j = 0; j < i; j++) {         if (A[i][j] != (inv ^ col[j])) {           inv ^= 1;           sum++;           diag[i] ^= 1;           diag[j] ^= 1;           col[j] ^= 1;         }       }     }     memset((col), (0), sizeof(col));     for (i = 0; i < n - 1; i++) {       int inv = 0;       for (j = n - 1; j > i; j--) {         if (A[i][j] != (inv ^ col[j])) {           inv ^= 1;           sum++;           diag[i] ^= 1;           diag[j] ^= 1;           col[j] ^= 1;         }       }     }     for (i = 0; i < n; i++)       if (A[i][i] != diag[i]) sum++;     printf( %d n , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mo = 998244853;   const long long INF = 0x3f3f3f3f;   template <typename _T>   void read(_T &x) {     _T f = 1;     x = 0;     char s = getchar();     while (s >  9  || s <  0 ) {       if (s ==  - ) f = -1;       s = getchar();     }     while (s >=  0  && s <=  9 ) {       x = (x << 3) + (x << 1) + (s ^ 48);       s = getchar();     }     x *= f;   }   long long n, m, fac[4005], inv[4005], f[4005], ans, g[4005];   void init() {     fac[0] = inv[0] = fac[1] = inv[1] = f[1] = 1;     for (long long i = 2; i <= 4e3; i++) {       fac[i] = 1ll * fac[i - 1] * i % mo;       f[i] = 1ll * (mo - mo / i) * f[mo % i] % mo;       inv[i] = 1ll * inv[i - 1] * f[i] % mo;     }   }   long long C(long long x, long long y) {     if (x < 0 || y < 0 || x < y) return 0;     return 1ll * fac[x] * inv[y] % mo * inv[x - y] % mo;   }   signed main() {     init();     read(n);     read(m);     long long d = n - m < 0 ? 0 : n - m;     for (long long i = d; i <= n; i++) g[i] = C(n + m, n - m >= i ? n : n - i);     for (long long i = d; i <= n; i++)       (ans += i * (g[i] - g[i + 1] + mo) % mo) %= mo;     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 21;   int n, m, k;   int ls;   bool vis[N];   vector<pair<int, int> > e[N];   void dfs(int u, int sum, int x) {     if (u > n) {       ls++;       return;     }     if (ls >= k) {       return;     }     dfs(u + 1, sum, x);     for (int i = 0; i < (int)e[u].size(); ++i) {       int w = e[u][i].first, v = e[u][i].second;       if (sum + w <= x && !vis[v]) {         vis[v] = 1;         dfs(u + 1, sum + w, x);         vis[v] = 0;       }     }   }   bool ok(int x) {     ls = 0;     memset(vis, 0, sizeof(vis));     dfs(1, 0, x);     return ls >= k;   }   int main() {     scanf( %d %d %d , &n, &m, &k);     for (int i = 0; i < (m); ++i) {       int u, v, w;       scanf( %d %d %d , &u, &v, &w);       e[u].push_back({w, v});     }     int l = 0, r = 100000;     while (l + 1 < r) {       int mid = l + r >> 1;       if (ok(mid))         r = mid;       else         l = mid;     }     int ans = -1;     if (ok(l) || ok(r)) ans = ok(l) ? l : r;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 450007;   int read() {     int ans = 0, f = 1, c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       ans = ans * 10 + (c -  0 );       c = getchar();     }     return ans * f;   }   int n, k, sum1, sum2;   int x[M], y[M], f[M];   int main() {     n = read();     for (int i = 1; i <= n; i++) {       k = read();       if (k) x[sum1++] = k;     }     for (int i = 1; i <= n; i++) {       k = read();       if (k) y[sum2++] = k;     }     int now = sum1 - 1;     for (int i = 0; i < now; i++) x[sum1++] = x[i];     for (int i = 1; i < sum2; i++) {       int j = f[i];       while (j && y[i] != y[j]) j = f[j];       f[i + 1] = (y[i] == y[j]) ? f[j] + 1 : 0;     }     int j = 0;     for (int i = 0; i < sum1; i++) {       while (j && x[i] != y[j]) j = f[j];       if (x[i] == y[j]) j++;       if (j == sum2) break;     }     if (j == sum2)       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod;   long long binPow(long long a, long long q) {     a %= mod;     if (q == 0) return 1;     if (q % 2 == 1)       return a * binPow(a * a, q / 2) % mod;     else       return binPow(a * a, q / 2) % mod;   }   bool prime(int x) {     if (x % 2 == 0) return false;     if (x % 3 == 0) return false;     for (int i = 4; i <= sqrt(x); i++)       if (x % i == 0) return false;     return true;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     if (n > 4 && prime(n) == false) {       cout <<  NO ;       return 0;     }     cout <<  YES n ;     if (n == 1)       cout << 1;     else if (n == 2)       cout << 1 << endl << 2;     else if (n == 3)       cout << 1 << endl << 2 << endl << 3;     else if (n == 4)       cout << 1 << endl << 3 << endl << 2 << endl << 4;     else {       mod = n;       cout << 1 << endl;       int cnt = 2;       while (cnt != n) {         long long t = cnt * binPow(cnt - 1, n - 2) % mod;         t %= n;         cout << t << endl;         cnt++;       }       cout << n;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, S = 0;     cin >> a >> b >> c;     while (a > 0 && b > 1 && c > 3) {       a--;       b -= 2;       c -= 4;       S += 7;     }     cout << S;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10, INF = 2147483647;   int a[MAXN];   int main() {     int T, n, i, res = 0;     scanf( %d , &T);     while (T--) {       res = 0;       scanf( %d , &n);       for (i = 1; i <= n; i++) scanf( %d , &a[i]);       sort(a + 1, a + n + 1);       int ed = a[n];       res = a[n];       bool f2 = 0, f3 = 0, f5 = 0;       for (i = 1; i <= n; i++) {         if (a[i] * 2 == ed) f2 = true;         if (a[i] * 3 == ed) f3 = true;         if (a[i] * 5 == ed) f5 = true;       }       if (f2 && f3 && f5) res = max(res, ed / 2 + ed / 3 + ed / 5);       for (i = 1; i <= n; i++)         if (a[n] % a[i] == 0) a[i] = INF;       sort(a + 1, a + n + 1);       while (a[n] == INF) n--;       res = max(res, ed + a[n]);       for (i = n - 1; i >= 1; i--)         if (a[n] % a[i] != 0) {           res = max(res, ed + a[n] + a[i]);           break;         }       printf( %d n , res);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1001;   int n, m, h, A[M], w, a;   int main() {     scanf( %d%d%d , &n, &m, &h);     for (int i = 1; i <= m; i++) scanf( %d , &A[i]), w += A[i];     w--, n--;     a = w - (A[h] - 1);     if (n > w) {       puts( -1 );       return 0;     }     if (n > a) {       puts( 1 );       return 0;     }     double ans = 1.0;     int x = a - n, y = w - n;     for (int i = a + 1; i <= w; i++) ans = ans / (i * 1.0);     for (int i = x + 1; i <= y; i++) ans = ans * (i * 1.0);     printf( %.7lf n , 1.0 - ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k;     cin >> n >> k;     vector<long long int> arr(n);     long long int r = n;     for (long long int i = 0; i < k; i++) {       long long int temp;       cin >> temp;       for (long long int j = temp - 1; j < r; j++) arr[j] = temp;       r = min(r, temp - 1);     }     for (auto z : arr) cout << z <<    ;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 23;   const int MOD = 1e9 + 9;   const int MAXN = 1e3 + 100;   struct item {     int val, type, id;     bool operator<(const item &rhs) const { return val > rhs.val; }   };   vector<item> items[2];   vector<item> cart[MAXN];   int main() {     ios_base::sync_with_stdio(0);     int n, k;     cin >> n >> k;     for (int i = 0; i < n; ++i) {       item it;       it.id = i + 1;       cin >> it.val >> it.type;       items[it.type - 1].push_back(it);     }     for (int i = 0; i < 2; ++i) {       sort(items[i].begin(), items[i].end());     }     for (int i = 0; i < min((int)items[0].size(), k); ++i) {       cart[i].push_back(items[0][i]);     }     for (int i = k; i < items[0].size(); ++i) {       cart[k - 1].push_back(items[0][i]);     }     int pnr = items[1].size() - 1, cnr = k - 1;     while (cnr >= 0 && cart[cnr].size() == 0) {       cart[cnr--].push_back(items[1][pnr--]);     }     int pi = 0, ci = 0;     while (pi <= pnr) {       if (ci == k - 1 || cart[ci][0].val <= items[1][pi].val) {         cart[ci].push_back(items[1][pi++]);       } else {         ++ci;       }     }     double anssum = 0;     for (int i = 0; i < k; ++i) {       bool stool = false;       int mn = INF;       for (auto it : cart[i]) {         if (it.type == 1) stool = true;         mn = min(mn, it.val);         anssum += it.val;       }       if (stool) {         anssum -= mn / 2.0;       }     }     cout.precision(1);     cout << fixed << anssum <<   n ;     for (int i = 0; i < k; ++i) {       cout << cart[i].size();       for (auto it : cart[i]) {         cout <<     << it.id;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int K = 64, N = 3e5 + 7;   int a[N];   int sum[N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       long long x;       cin >> x;       a[i] = __builtin_popcountll(x);       sum[i] = sum[i - 1] + a[i];     }     long long ans = 0;     int odd = 0, even = 1;     for (int i = 1; i <= n; i++) {       if (sum[i] % 2) {         ans += odd;         odd++;       } else {         ans += even;         even++;       }       int mx = 0, sm = 0;       for (int j = 0; j < K && j < i; j++) {         int cur = i - j;         mx = max(mx, a[cur]);         sm += a[cur];         if (sm % 2 == 0 && 2 * mx > sm) ans--;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int ans = 0, n;   int numDistDig(int n) {     vector<int> arr(10, 0);     while (n > 0) {       arr[n % 10]++;       n /= 10;     }     int ans = 0, i;     for (i = 0; i < 10; i++) ans += (arr[i] > 0);     return ans;   }   void dfs(int num) {     if (num > 0 && num <= n) ans++;     if (ans >= 100000000) return;     int i;     for (i = 0; i < 10; i++) {       int num2 = num * 10 + i;       if (num2 > 0 && numDistDig(num2) <= 2) dfs(num2);     }   }   int main() {     scanf( %d , &n);     dfs(0);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int r, n, d;     int kol = 0;     cin >> n >> d;     int *a = new int[n];     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         if (i != j && abs(a[i] - a[j]) <= d) {           kol++;         }     cout << kol;     return 0;   }


#include <bits/stdc++.h>   const int ms = 500500;   std::string str[ms], ans[ms];   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(NULL);     int n;     std::cin >> n;     for (int i = 0; i < n; i++) std::cin >> str[i];     ans[n - 1] = str[n - 1];     for (int i = n - 2; i >= 0; i--) {       int type = 0;       int to = 0;       for (; to < str[i].size() && to < ans[i + 1].size(); to++) {         if (str[i][to] < ans[i + 1][to])           type = 1;         else if (str[i][to] > ans[i + 1][to])           type = 2;         if (type) break;       }       if (type == 0 || type == 2) {         for (int j = 0; j < to; j++) ans[i] += str[i][j];       } else if (type == 1) {         ans[i] = str[i];       }     }     for (int i = 0; i < n; i++) std::cout << ans[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 205;   const int P = 998244353;   template <typename T>   void chkmax(T &x, T y) {     x = max(x, y);   }   template <typename T>   void chkmin(T &x, T y) {     x = min(x, y);   }   template <typename T>   void read(T &x) {     x = 0;     int f = 1;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -f;     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;     x *= f;   }   template <typename T>   void write(T x) {     if (x < 0) x = -x, putchar( - );     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   template <typename T>   void writeln(T x) {     write(x);     puts(  );   }   int a[MAXN], w[MAXN];   int dp[MAXN][MAXN], binom[MAXN][MAXN];   int power(int x, int y) {     if (y == 0) return 1;     int tmp = power(x, y / 2);     if (y % 2 == 0)       return 1ll * tmp * tmp % P;     else       return 1ll * tmp * tmp % P * x % P;   }   int getdown(int x, int y) {     int ans = 1;     for (int i = 1; i <= y; i++) ans = 1ll * ans * (x - i + 1) % P;     return ans;   }   int getup(int x, int y) {     int ans = 1;     for (int i = 1; i <= y; i++) ans = 1ll * ans * (x + i - 1) % P;     return ans;   }   void update(int &x, int y) {     x += y;     if (x >= P) x -= P;   }   int main() {     int n, m;     read(n), read(m);     for (int i = 1; i <= n; i++) read(a[i]);     int s = 0, g = 0, b = 0;     for (int i = 1; i <= n; i++) {       read(w[i]), s += w[i];       if (a[i] == 0)         b += w[i];       else         g += w[i];     }     dp[0][0] = 1;     for (int i = 0; i <= m - 1; i++) {       for (int j = 0; j <= i; j++) {         int tmp = dp[i][j], cnt = s + j - (i - j);         update(dp[i + 1][j],                1ll * tmp * (b - (i - j)) % P * power(cnt, P - 2) % P);         update(dp[i + 1][j + 1], 1ll * tmp * (g + j) % P * power(cnt, P - 2) % P);       }     }     for (int i = 0; i <= m; i++) {       binom[i][0] = 1;       for (int j = 1; j <= i; j++)         binom[i][j] = (binom[i - 1][j - 1] + binom[i - 1][j]) % P;     }     for (int i = 1; i <= n; i++) {       int ans = w[i];       if (a[i] == 0) {         for (int j = 0; j <= m; j++) {           if (dp[m][m - j] == 0) continue;           int tmp = dp[m][m - j];           for (int k = 0; k <= j; k++)             update(ans, P - 1ll * tmp * k % P * getdown(w[i], k) % P *                                 getdown(b - w[i], j - k) % P * binom[j][k] % P *                                 power(getdown(b, j), P - 2) % P);         }       } else {         for (int j = 0; j <= m; j++) {           if (dp[m][j] == 0) continue;           int tmp = dp[m][j];           for (int k = 0; k <= j; k++)             update(ans, 1ll * tmp * k % P * getup(w[i], k) % P *                             getup(g - w[i], j - k) % P * binom[j][k] % P *                             power(getup(g, j), P - 2) % P);         }       }       writeln(ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, c = 0;     cin >> n;     if (n == 1 || n == 2)       cout << 0 << endl;     else if (n % 2 == 0) {       int temp = 1;       while (n >= temp * 2) {         temp *= 2;       }       cout << (n - temp) / 2 << endl;     } else       cout << n / 2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   map<pair<int, char>, int> table;   int main() {     string home, away;     int n, x, y, i;     char card, team;     cin >> home >> away;     cin >> n;     for (i = 0; i < n; i++) {       cin >> x >> team >> y >> card;       if (card ==  y ) table[make_pair(y, team)]++;       if (card ==  r ) {         if (table[make_pair(y, team)] < 2)           table[make_pair(y, team)] = 2;         else           continue;       }       if (table[make_pair(y, team)] == 2) {         if (team ==  a )           cout << away <<    ;         else           cout << home <<    ;         cout << y <<     << x << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MXN = 100010;   const int MXX = 400050;   struct Segment_Tree {     int x, add;   } tt[MXX];   int m, v[MXN], ans[MXN];   void Build(int l, int r, int idx) {     tt[idx].x = 0;     tt[idx].add = 0;     if (l == r) {       return;     }     int mid = (l + r) >> 1;     Build(l, mid, idx << 1);     Build(mid + 1, r, idx << 1 | 1);   }   inline void PushDown(int l, int r, int idx) {     tt[idx << 1].add += tt[idx].add;     tt[idx << 1].x += tt[idx].add;     tt[idx << 1 | 1].add += tt[idx].add;     tt[idx << 1 | 1].x += tt[idx].add;     tt[idx].add = 0;   }   inline void PushUp(int l, int r, int idx) {     tt[idx].x = max(tt[idx << 1].x, tt[idx << 1 | 1].x);   }   void Change(int L, int R, int x, int l, int r, int idx) {     if (L <= l && r <= R) {       tt[idx].add += x;       tt[idx].x += x;       return;     }     PushDown(l, r, idx);     int mid = (l + r) >> 1;     if (L <= mid) Change(L, R, x, l, mid, idx << 1);     if (mid + 1 <= R) Change(L, R, x, mid + 1, r, idx << 1 | 1);     PushUp(l, r, idx);   }   int Query(int L, int R, int l, int r, int idx) {     if (L <= l && r <= R) {       return tt[idx].x;     }     PushDown(l, r, idx);     int mid = (l + r) >> 1, ans = -0x3f3f3f3f;     if (L <= mid) ans = max(ans, Query(L, R, l, mid, idx << 1));     if (mid + 1 <= R) ans = max(ans, Query(L, R, mid + 1, r, idx << 1 | 1));     PushUp(l, r, idx);     return ans;   }   int w;   inline bool Check(int x) {     int tmp = Query(x, m, 1, m, 1);     if (tmp > 0) {       w = x;       return 1;     }     return 0;   }   inline void Mybin(int low, int high) {     w = 0;     for (int mid = (low + high) >> 1; low <= high;          (Check(mid) ? low = mid + 1 : high = mid - 1), mid = (low + high) >> 1)       ;   }   inline void solve() {     int p, t, x;     scanf( %d , &m);     Build(1, m, 1);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &t, &p);       if (p == 0) {         Change(1, t, -1, 1, m, 1);       } else {         scanf( %d , &x);         v[t] = x;         Change(1, t, 1, 1, m, 1);       }       Mybin(1, m);       if (w == 0)         ans[i] = -1;       else         ans[i] = v[w];     }     for (int i = 1; i <= m; i++) printf( %d n , ans[i]);   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     vector<int> values;     int T;     cin >> T;     while (T--) {       values.clear();       map<int, int> indexes;       bool flag = false;       int n, bindex;       cin >> n;       for (int i = 0; i < n; i++) {         int value;         cin >> value;         values.push_back(value);         indexes[value] = i;       }       for (int i = n; i > 0; i--) {         int j = indexes[i];         if (j != 0 && j != n - 1) {           if (values[j - 1] < i && values[j + 1] < i) {             flag = true;             bindex = j;             break;           }         }       }       if (flag) {         cout <<  YES               <<   n ;         cout << bindex <<     << bindex + 1 <<     << bindex + 2 <<   n ;       } else         cout <<  NO               <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 7, M = 20;   char g;   struct tree {     int a, b, c, t;   } tr[N << 1];   struct edge {     int a, b;   } ed[N], edg[N];   set<int> hig[N];   map<int, int> s[N];   int n, m, k, siz, res, tota, totb, lint, themin, w[N], fa[N], fb[N], st[N],       dep[N], pos[N], ret[N], ffa[N], fson[N], low[N], dfn[N], dfsn[N], e[N][M];   inline int read() {     int num = 0;     g = getchar();     while (g < 48 || 57 < g) g = getchar();     while (47 < g && g < 58)       num = (num << 1) + (num << 3) + g - 48, g = getchar();     return num;   }   inline int Min(int a, int b) {     if (a < b) return a;     return b;   }   inline int Max(int a, int b) {     if (a > b) return a;     return b;   }   inline void addedgea(int a, int b) {     tota++, ed[tota].a = b, ed[tota].b = fa[a], fa[a] = tota;   }   inline void addedgeb(int a, int b) {     totb++, edg[totb].a = b, edg[totb].b = fb[a], fb[a] = totb;   }   inline void tarjan(int u) {     dfn[u] = low[u] = ++siz, st[++st[0]] = u;     for (int i = fa[u]; i > 0; i = ed[i].b) {       int x = ed[i].a;       if (dfn[x])         low[u] = Min(low[u], dfn[x]);       else {         tarjan(x), low[u] = Min(low[u], low[x]);         if (low[x] >= dfn[u]) {           res++, addedgeb(res, u), addedgeb(u, res);           while (st[st[0] + 1] != x)             addedgeb(res, st[st[0]]), addedgeb(st[st[0]--], res);         }       }     }   }   inline void find(int u) {     int z = 1;     ret[u] = 1;     while ((1 << z) < dep[u]) e[u][z] = e[e[u][z - 1]][z - 1], z++;     for (int i = fb[u]; i > 0; i = edg[i].b) {       int x = edg[i].a;       if (fa[u] != x) {         fa[x] = u, dep[x] = dep[u] + 1, e[x][0] = u, find(x);         if (u > n) {           if (!s[u][w[x]]) hig[u].insert(w[x]);           s[u][w[x]]++;         }         if (ret[x] > ret[fson[u]]) fson[u] = x;         ret[u] += ret[x];       }     }   }   inline void search(int u) {     if (!ffa[u]) ffa[u] = u;     dfsn[u] = ++lint;     if (fson[u])       ffa[fson[u]] = ffa[u], search(fson[u]);     else       fson[u] = u;     for (int i = fb[u]; i > 0; i = edg[i].b) {       int x = edg[i].a;       if (fa[u] != x && fson[u] != x) search(x);     }   }   inline void build(int a, int b, int c) {     tr[c].a = a, tr[c].b = b, tr[c].c = (a + b) >> 1;     if (a == b) {       pos[a] = c;       return;     }     build(tr[c].a, tr[c].c, c << 1), build(tr[c].c + 1, tr[c].b, c << 1 | 1);   }   inline void inser(int u, int x) {     tr[u].t = x, u = u >> 1;     while (u > 0) tr[u].t = Min(tr[u << 1].t, tr[u << 1 | 1].t), u = u >> 1;   }   inline int lca(int a, int b) {     if (dep[a] < dep[b]) swap(a, b);     int z = dep[a] - dep[b];     for (int x = 18; x >= 0; x--)       if (z & (1 << x)) a = e[a][x];     if (a == b) return a;     for (int x = 18; x >= 0; x--)       if (e[a][x] != e[b][x]) a = e[a][x], b = e[b][x];     return e[a][0];   }   inline void searchans(int a, int b, int c) {     if (tr[c].a == a && tr[c].b == b) {       themin = Min(tr[c].t, themin);       return;     }     if (a <= tr[c].c) searchans(a, Min(b, tr[c].c), c << 1);     if (tr[c].c < b) searchans(Max(tr[c].c + 1, a), b, c << 1 | 1);   }   inline void write(int u) {     if (u > 9) write(u / 10);     putchar(u % 10 +  0 );   }   int main() {     n = read(), m = read(), k = read(), res = n, build(1, n << 1, 1);     int a, b, c;     for (int i = 1; i <= n; i++) w[i] = read();     for (int i = 1; i <= m; i++)       a = read(), b = read(), addedgea(a, b), addedgea(b, a);     tarjan(1), fa[1] = 0, dep[1] = 1, find(1), search(1);     for (int i = 1; i <= n; i++) inser(pos[dfsn[i]], w[i]);     for (int i = n + 1; i <= res; i++) inser(pos[dfsn[i]], *hig[i].begin());     for (int i = 1; i <= k; i++) {       g = getchar();       while (g !=  A  && g !=  C ) g = getchar();       if (g ==  C ) {         a = read(), b = read();         if (fa[a]) {           s[fa[a]][w[a]]--;           if (!s[fa[a]][w[a]]) hig[fa[a]].erase(w[a]);           w[a] = b;           if (!s[fa[a]][w[a]]) hig[fa[a]].insert(w[a]);           s[fa[a]][w[a]]++;           inser(pos[dfsn[fa[a]]], *hig[fa[a]].begin());         }         w[a] = b, inser(pos[dfsn[a]], w[a]);       } else {         a = read(), b = read(), c = lca(a, b), themin = 1e9 + 7;         while (ffa[a] != ffa[c])           searchans(dfsn[ffa[a]], dfsn[a], 1), a = fa[ffa[a]];         while (ffa[b] != ffa[c])           searchans(dfsn[ffa[b]], dfsn[b], 1), b = fa[ffa[b]];         searchans(dfsn[c], dfsn[a], 1), searchans(dfsn[c], dfsn[b], 1);         if (c > n) themin = Min(themin, w[fa[c]]);         write(themin), putchar(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   static const int INF = 0x3f3f3f3f;   static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;   vector<string> split(string s, string sep) {     string act =   ;     vector<string> res;     for (int c = 0; c < s.size(); c++) {       bool split = false;       for (int c2 = 0; c2 < sep.size(); c2++) {         if (sep[c2] == s[c]) {           split = true;           break;         }       }       if (split) {         res.push_back(act);         act =   ;       } else {         act = act + s[c];       }     }     res.push_back(act);     return res;   }   string removeChars(string s, string filters) {     string res =   ;     for (int c = 0; c < s.size(); c++) {       bool fine = true;       for (int c2 = 0; c2 < filters.size(); c2++) {         if (filters[c2] == s[c]) {           fine = false;           break;         }       }       if (fine) res = res + s[c];     }     return res;   }   string replaceChars(string s, string from, string to) {     for (int c = 0; c < s.size(); c++) {       for (int c2 = 0; c2 < from.size(); c2++) {         if (from[c2] == s[c]) {           s[c] = to[c2 % to.size()];           break;         }       }     }     return s;   }   struct MaximumFlow {     static const int InfCapacity = INF;     struct Edge {       int to;       int capacity;       int rev;     };     vector<vector<Edge> > g;     void init(int n) { g.assign(n, vector<Edge>()); }     void add(int i, int j, int capacity) {       Edge e, f;       e.to = j, f.to = i;       e.capacity = capacity, f.capacity = 0;       g[i].push_back(e);       g[j].push_back(f);       g[i].back().rev = (int)g[j].size() - 1;       g[j].back().rev = (int)g[i].size() - 1;     }     void addB(int i, int j, int capacity) {       Edge e, f;       e.to = j, f.to = i;       e.capacity = capacity, f.capacity = capacity;       g[i].push_back(e);       g[j].push_back(f);       g[i].back().rev = (int)g[j].size() - 1;       g[j].back().rev = (int)g[i].size() - 1;     }     int maximumFlow(int s, int t) {       int n = g.size();       vector<int> level(n);       int total = 0;       bool update;       do {         update = false;         fill(level.begin(), level.end(), -1);         level[s] = 0;         queue<int> q;         q.push(s);         for (int d = n; !q.empty() && level[q.front()] < d;) {           int u = q.front();           q.pop();           if (u == t) d = level[u];           for (auto e = (g[u]).begin(); e != (g[u]).end(); ++e)             if (e->capacity > 0 && level[e->to] == -1)               q.push(e->to), level[e->to] = level[u] + 1;         }         vector<int> iter(n);         for (int i = 0; i < n; i++) iter[i] = (int)g[i].size() - 1;         while (1) {           int f = augment(level, iter, s, t, InfCapacity);           if (f == 0) break;           total += f;           update = true;         }       } while (update);       return total;     }     int augment(vector<int> &level, vector<int> &iter, int u, int t, int f) {       if (u == t || f == 0) return f;       int lv = level[u];       if (lv == -1) return 0;       level[u] = -1;       for (; iter[u] >= 0; --iter[u]) {         Edge &e = g[u][iter[u]];         if (level[e.to] <= lv) continue;         int l = augment(level, iter, e.to, t, min(f, e.capacity));         if (l == 0) continue;         e.capacity -= l;         g[e.to][e.rev].capacity += l;         level[u] = lv;         return l;       }       return 0;     }   };   int main() {     vector<int> sizes(6);     for (int c = 0; c < sizes.size(); c++) cin >> sizes[c];     int n;     cin >> n;     MaximumFlow m;     m.init(n + 6 + 2);     for (int c = 0; c < 6; c++) {       m.add(c + 2, 1, sizes[c]);     }     string poss[6] = { S ,  M ,  L ,  XL ,  XXL ,  XXXL };     for (int c = 0; c < n; c++) {       m.add(0, 8 + c, 1);       string line;       cin >> line;       vector<string> possibilities = split(line,  , );       for (int c2 = 0; c2 < possibilities.size(); c2++) {         for (int c3 = 0; c3 < 6; c3++) {           if (poss[c3] == possibilities[c2]) {             m.add(8 + c, c3 + 2, 1);           }         }       }     }     int res = m.maximumFlow(0, 1);     if (res != n) {       cout <<  NO ;       return 0;     }     cout <<  YES  << endl;     for (int c = 0; c < n; c++) {       for (int c2 = 0; c2 < m.g[8 + c].size(); c2++) {         if (m.g[8 + c][c2].capacity == 0) {           cout << poss[m.g[8 + c][c2].to - 2] << endl;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int ret = 0, t = 1;     char c = getchar();     while ((c <  0  || c >  9 ) && c !=  - ) c = getchar();     if (c ==  - ) t = -1, c = getchar();     while (c >=  0  && c <=  9 ) ret = ret * 10 + c -  0 , c = getchar();     return ret * t;   }   const int MAXN = 1001000, mod = 1e9 + 7;   int qpow(int x, int y) {     if (y < 0) return qpow(qpow(x, -y), mod - 2);     int ret = 1;     while (y) {       if (y & 1) ret = 1ll * ret * x % mod;       x = 1ll * x * x % mod;       y >>= 1;     }     return ret;   }   int k, n;   int FAC[MAXN], pre[MAXN], suf[MAXN];   int fac(int x) { return (x < 0 && (x & 1)) ? -FAC[-x] : FAC[abs(x)]; }   int main() {     k = read(), n = read();     int i, j;     FAC[0] = pre[0] = 1, suf[n + 3] = 1;     for (i = 1; i <= n + 2; i++)       FAC[i] = 1ll * FAC[i - 1] * i % mod,       pre[i] = 1ll * pre[i - 1] * (k - i) % mod;     for (i = n + 2; i >= 0; i--) suf[i] = 1ll * suf[i + 1] * (k - i) % mod;     int Y = 0, ans = 0;     for (i = 1; i <= n + 2; i++) {       Y = (Y + qpow(i, n)) % mod;       int tmp = 1ll * Y * pre[i - 1] % mod * suf[i + 1] % mod *                 qpow(fac(i - 1), -1) % mod * qpow(fac(i - n - 2), -1) % mod;       ans = (ans + tmp) % mod;     }     printf( %d n , (ans + mod) % mod);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     scanf( %d%d%d , &n, &a, &b);     int tmp = abs(b);     int x = tmp % n;     if (b > 0) {       int ans = a + x;       while (ans >= n) {         ans %= n;       }       if (ans == 0)         printf( %d n , n);       else         printf( %d n , ans);     } else if (b == 0)       printf( %d n , a);     else {       int ans = a - x;       if (ans <= 0)         printf( %d n , n + ans);       else         printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long q;   long long n, k, a, b;   int main() {     cin >> q;     while (q--) {       cin >> k >> n >> a >> b;       long long l = 0, r = n, mid, ans = -1;       while (l <= r) {         mid = (l + r) / 2;         if (mid * a + (n - mid) * b >= k)           r = mid - 1;         else           l = mid + 1, ans = mid;       }       cout << ans << endl;     }     return 0;   }