#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > allModify;   class Polygon {    private:     int n;     vector<int> label;     vector<set<int> > adj;     void splitPolygon(Polygon &a, Polygon &b, int x, int y) const {       if (x > y) return (splitPolygon(a, b, y, x));       for (int i = (x), _b = (y); i <= _b; i = i + 1) a.label.push_back(label[i]);       for (int i = (0), _b = (x); i <= _b; i = i + 1) b.label.push_back(label[i]);       for (int i = (y), _b = (n - 1); i <= _b; i = i + 1)         b.label.push_back(label[i]);       a.n = a.label.size();       b.n = b.label.size();       a.adj.assign(a.n, set<int>());       b.adj.assign(b.n, set<int>());       for (int i = 0, _n = (n); i < _n; i = i + 1)         for (__typeof((adj[i]).begin()) it = (adj[i]).begin();              it != (adj[i]).end(); it++) {           int j = *it;           Polygon &cur = (x <= i && i <= y && x <= j && j <= y) ? a : b;           int idi = lower_bound(cur.label.begin(), cur.label.end(), label[i]) -                     cur.label.begin();           int idj = lower_bound(cur.label.begin(), cur.label.end(), label[j]) -                     cur.label.begin();           cur.adj[idi].insert(idj);           cur.adj[idj].insert(idi);         }       int idx, idy;       idx =           lower_bound(a.label.begin(), a.label.end(), label[x]) - a.label.begin();       idy =           lower_bound(a.label.begin(), a.label.end(), label[y]) - a.label.begin();       a.adj[idx].insert(idy);       a.adj[idy].insert(idx);       idx =           lower_bound(b.label.begin(), b.label.end(), label[x]) - b.label.begin();       idy =           lower_bound(b.label.begin(), b.label.end(), label[y]) - b.label.begin();       b.adj[idx].insert(idy);       b.adj[idy].insert(idx);     }     int prevInSet(const set<int> &s, set<int>::iterator it) const {       if (it == s.begin()) it = s.end();       it--;       return (*it);     }     int nextInSet(const set<int> &s, set<int>::iterator it) const {       it++;       if (it == s.end()) it = s.begin();       return (*it);     }     bool haveEdge(int i, int j) const { return (adj[i].find(j) != adj[i].end()); }     pair<int, int> tryChange(int i, set<int>::iterator it) const {       int x = prevInSet(adj[i], it);       int y = nextInSet(adj[i], it);       return (make_pair(x, y));     }     void fixChange(int u, int v, int x, int y) {       adj[u].erase(v);       adj[v].erase(u);       adj[x].insert(y);       adj[y].insert(x);       allModify.push_back(make_pair(label[u], label[v]));     }     void debug(void) const {       printf( POLYGON of %d n , n);       printf( LABEL:  );       for (int i = 0, _n = (n); i < _n; i = i + 1) printf( %d  , label[i]);       printf(  n );       for (int i = 0, _n = (n); i < _n; i = i + 1) {         printf( adj of %d:  , i);         for (__typeof((adj[i]).begin()) it = (adj[i]).begin();              it != (adj[i]).end(); it++)           printf( %d  , *it);         printf(  n );       }       printf( ___END___ n );     }       public:     Polygon() { n = 0; }     Polygon(int n) {       this->n = n;       for (int i = 0, _n = (n); i < _n; i = i + 1) label.push_back(i + 1);       adj.assign(n, set<int>());       for (int i = 0, _n = (n); i < _n; i = i + 1) {         adj[i].insert((i + 1 + n) % n);         adj[i].insert((i - 1 + n) % n);       }     }     void input(void) {       for (int love = 0, _n = (n - 3); love < _n; love = love + 1) {         int x, y;         scanf( %d%d , &x, &y);         x--;         y--;         adj[x].insert(y);         adj[y].insert(x);       }     }     void modify(const Polygon &goal) {       if (n < 4) return;       for (int i = 0, _n = (n); i < _n; i = i + 1)         for (__typeof((adj[i]).begin()) it = (adj[i]).begin();              it != (adj[i]).end(); it++)           if (*it != (i + 1) % n && *it != (i - 1 + n) % n &&               goal.haveEdge(i, *it)) {             int j = *it;             Polygon A, B, goalA, goalB;             splitPolygon(A, B, i, j);             goal.splitPolygon(goalA, goalB, i, j);             A.modify(goalA);             B.modify(goalB);             return;           }       for (int i = 0, _n = (n); i < _n; i = i + 1)         for (__typeof((adj[i]).begin()) it = (adj[i]).begin();              it != (adj[i]).end(); it++)           if (*it != (i + 1) % n && *it != (i - 1 + n) % n) {             pair<int, int> tmp = tryChange(i, it);             if (goal.haveEdge(tmp.first, tmp.second)) {               int j = *it;               fixChange(i, j, tmp.first, tmp.second);               Polygon A, B, goalA, goalB;               splitPolygon(A, B, tmp.first, tmp.second);               goal.splitPolygon(goalA, goalB, tmp.first, tmp.second);               A.modify(goalA);               B.modify(goalB);               return;             }           }       int chs = -1;       for (int i = 0, _n = (n); i < _n; i = i + 1)         if (goal.adj[i].size() < 3)           if (chs < 0 || adj[i].size() < adj[chs].size()) chs = i;       int u = chs;       int x = (u - 1 + n) % n;       int y = (u + 1 + n) % n;       while (adj[u].size() > 2) {         for (__typeof((adj[u]).begin()) it = (adj[u]).begin();              it != (adj[u]).end(); it++)           if (*it != x && *it != y) {             pair<int, int> tmp = tryChange(u, it);             fixChange(u, *it, tmp.first, tmp.second);             break;           }       }       Polygon A, B, goalA, goalB;       splitPolygon(A, B, x, y);       goal.splitPolygon(goalA, goalB, x, y);       A.modify(goalA);       B.modify(goalB);     }   };   void process(void) {     int n;     scanf( %d , &n);     Polygon src(n), des(n);     src.input();     des.input();     src.modify(des);     printf( %d n , (int)allModify.size());     for (__typeof((allModify).begin()) it = (allModify).begin();          it != (allModify).end(); it++)       printf( %d %d n , it->first, it->second);   }   int main(void) {     process();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 500 + 10;   const int pN = 1e6;   const int INF = 0x3f3f3f3f;   const int MOD = 1e9 + 7;   void add(long long &x, long long y) {     x += y;     x %= MOD;   }   int u, v, n, m;   void Solve(int x, int y, int z) {     int row = n, cul = m;     int nx, ny;     for (int i = 1; i <= x; i++) {       nx = v;       ny = row - u + 1;       u = nx;       v = ny;       swap(row, cul);     }     for (int i = 1; i <= y; i++) {       nx = u;       ny = cul - v + 1;       u = nx;       v = ny;     }     for (int i = 1; i <= z; i++) {       nx = cul - v + 1;       ny = u;       u = nx;       v = ny;       swap(row, cul);     }     cout << u <<     << v << endl;   }   int main() {     int x, y, z, p;     cin >> n >> m >> x >> y >> z >> p;     x %= 4;     y %= 2;     z %= 4;     for (int i = 0; i < p; i++) {       cin >> u >> v;       Solve(x, y, z);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = 3.141592653589793238462643383;   long long int gcd(long long int a, long long int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long int Ceil(long long int a, long long int b) {     if (a % b == 0)       return a / b;     else       return a / b + 1;   }   const int MAX = 400009;   const int MOD = 1e9 + 7;   const int inf = 1e9 + 10;   long long int deg[MAX], vis[MAX], dep[MAX];   vector<long long int> adj[MAX];   void dfs(int i, int d) {     vis[i] = 1;     dep[i] = d++;     for (int j = 0; j < ((int)((adj[i]).size())); ++j) {       int u = adj[i][j];       if (!vis[u]) dfs(u, d);     }   }   priority_queue<pair<long long int, pair<long long int, long long int> > > pq;   set<pair<long long int, pair<long long int, long long int> > > st;   vector<long long int> ans;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     for (int i = 0; i < (n); ++i) {       int p;       cin >> p;       p--;       if (p == -1) continue;       adj[i].push_back(p), adj[p].push_back(i);       deg[i]++, deg[p]++;     }     dfs(0, 1);     (memset((vis), 0, sizeof(vis)));     for (int i = 0; i < (n); ++i)       if (deg[i] % 2 == 0)         pq.push((make_pair((dep[i]), ((make_pair((i), (deg[i])))))));     long long int cnt = 0;     while (!pq.empty()) {       pair<long long int, pair<long long int, long long int> > c = pq.top();       pq.pop();       if (st.find(c) != st.end()) {         st.erase(c);         continue;       }       int i = c.second.first;       vis[i] = 1;       cnt++;       ans.push_back(i);       for (int j = 0; j < ((int)((adj[i]).size())); ++j) {         int u = adj[i][j];         if (vis[u]) continue;         deg[u]--;         if (deg[u] % 2 == 0)           pq.push((make_pair((dep[u]), ((make_pair((u), (deg[u])))))));         else           st.insert((make_pair((dep[u]), ((make_pair((u), (deg[u] + 1)))))));       }     }     if (cnt == n) {       cout <<  YES n ;       for (auto&(it) : (ans)) cout << it + 1 <<   n ;     } else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long n, m, a, b, ar[200][200], ans;   long arp[10000], fl, l;   void dfs(long v, long par) {     arp[v] = 1;     l++;     for (int i = 1; i <= n; i++)       if (ar[v][i] == 1) {         if (i == par) continue;         if (arp[i] == 1) {           fl = 1;           return;         }         dfs(i, v);       }   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> m;     for (int i = 1; i <= m; i++) {       cin >> a >> b;       ar[a][b] = ar[b][a] = 1;     }     for (int i = 1; i <= n; i++)       if (arp[i] == 0) {         fl = 0;         l = 0;         dfs(i, -1);         if (fl)           if (l % 2) ++ans;       }     n -= ans;     if (n % 2) ++ans;     cout << ans << endl;     cin.get();     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int S[2][500005 * 4], pre[500005], suf[500005];   long long hpr1[3][500005], hpr2[3][500005], hsf1[3][500005], hsf2[3][500005],       pww[3][500005];   int n, m, k;   long long pr[3] = {79, 103, 211};   bool flag;   char a[500005], b[500005];   vector<int> sss[500005], ppp[500005];   int get(int node, int bas, int son, int x, int y, int w) {     if (bas > y || son < x) return (w ? -1 : 1900000000);     if (bas >= x && son <= y) {       if (bas == son) {         if (S[w][node]) {           flag = true;           return bas;         }         return 1900000000;       }       if (w) {         if (S[w][node * 2 + 1])           return get(node * 2 + 1, ((bas + son) / 2) + 1, son, x, y, w);         else if (S[w][node * 2])           return get(node * 2, bas, ((bas + son) / 2), x, y, w);         else           return -1;       } else {         if (S[w][node * 2])           return get(node * 2, bas, ((bas + son) / 2), x, y, w);         else if (S[w][node * 2 + 1])           return get(node * 2 + 1, ((bas + son) / 2) + 1, son, x, y, w);         else           return 1900000000;       }     }     if (w) {       int res = get(node * 2 + 1, ((bas + son) / 2) + 1, son, x, y, w);       if (flag) return res;       return get(node * 2, bas, ((bas + son) / 2), x, y, w);     } else {       int res = get(node * 2, bas, ((bas + son) / 2), x, y, w);       if (flag) return res;       return get(node * 2 + 1, ((bas + son) / 2) + 1, son, x, y, w);     }   }   void up(int node, int bas, int son, int x, int val, int w) {     if (bas > x || son < x) return;     if (bas == x && son == x) {       S[w][node] = val;       return;     }     up(node * 2, bas, ((bas + son) / 2), x, val, w);     up(node * 2 + 1, ((bas + son) / 2) + 1, son, x, val, w);     S[w][node] = S[w][node * 2] + S[w][node * 2 + 1];   }   void build(int node, int bas, int son, int w) {     if (bas == son) {       S[w][node] = 1;       return;     }     build(node * 2, bas, ((bas + son) / 2), w);     build(node * 2 + 1, ((bas + son) / 2) + 1, son, w);     S[w][node] = S[w][node * 2] + S[w][node * 2 + 1];   }   pair<pair<long long, long long>, long long> suf2(int bas, int son) {     return {{hsf2[0][son] - hsf2[0][bas + 1] * pww[0][bas - son + 1],              hsf2[1][son] - hsf2[1][bas + 1] * pww[1][bas - son + 1]},             hsf2[2][son] - hsf2[2][bas + 1] * pww[2][bas - son + 1]};   }   pair<pair<long long, long long>, long long> suf1(int bas, int son) {     return {{hsf1[0][son] - hsf1[0][bas + 1] * pww[0][bas - son + 1],              hsf1[1][son] - hsf1[1][bas + 1] * pww[1][bas - son + 1]},             hsf1[2][son] - hsf1[2][bas + 1] * pww[2][bas - son + 1]};   }   pair<pair<long long, long long>, long long> pre2(int bas, int son) {     return {{hpr2[0][son] - hpr2[0][bas - 1] * pww[0][son - bas + 1],              hpr2[1][son] - hpr2[1][bas - 1] * pww[1][son - bas + 1]},             hpr2[2][son] - hpr2[2][bas - 1] * pww[2][son - bas + 1]};   }   pair<pair<long long, long long>, long long> pre1(int bas, int son) {     return {{hpr1[0][son] - hpr1[0][bas - 1] * pww[0][son - bas + 1],              hpr1[1][son] - hpr1[1][bas - 1] * pww[1][son - bas + 1]},             hpr1[2][son] - hpr1[2][bas - 1] * pww[2][son - bas + 1]};   }   void hashsuf() {     for (int i = n; i >= 1; i--) {       for (int j = 0; j < 3; j++) {         hsf1[j][i] = hsf1[j][i + 1] * pr[j] + a[i] -  a  + 1;       }     }     for (int i = m; i >= 1; i--) {       for (int j = 0; j < 3; j++) {         hsf2[j][i] = hsf2[j][i + 1] * pr[j] + b[i] -  a  + 1;       }     }   }   void hashpre() {     for (int i = 1; i <= n; i++) {       for (int j = 0; j < 3; j++) {         hpr1[j][i] = hpr1[j][i - 1] * pr[j] + a[i] -  a  + 1;       }     }     for (int i = 1; i <= m; i++) {       for (int j = 0; j < 3; j++) {         hpr2[j][i] = hpr2[j][i - 1] * pr[j] + b[i] -  a  + 1;       }     }   }   pair<int, int> getsuf(int from, int len) {     flag = false;     int pos = get(1, 1, n, from + len - 1, n - (k - len), 1);     int be = pos - len + 1;     int en = pos + (k - len);     if (be < from || en > n) return {1900000000, 1900000000};     return {be, en};   }   int main() {     scanf( %d %d %d , &n, &m, &k);     scanf( %s , a + 1);     scanf( %s , b + 1);     hashpre();     hashsuf();     pww[0][0] = pww[1][0] = pww[2][0] = 1;     for (int i = 1; i <= n; i++)       for (int j = 0; j < 3; j++) pww[j][i] = pww[j][i - 1] * pr[j];     for (int i = 1; i <= n; i++) {       int bas = 1, son = min(m, n - i + 1);       while (bas <= son) {         if (pre1(i, i + ((bas + son) / 2) - 1) == pre2(1, ((bas + son) / 2)))           bas = ((bas + son) / 2) + 1;         else           son = ((bas + son) / 2) - 1;       }       pre[i] = son;       ppp[son].push_back(i);       bas = 1, son = min(m, i);       while (bas <= son) {         if (suf1(i, i - ((bas + son) / 2) + 1) ==             suf2(m, m - ((bas + son) / 2) + 1))           bas = ((bas + son) / 2) + 1;         else           son = ((bas + son) / 2) - 1;       }       suf[i] = son;       sss[son].push_back(i);     }     for (int i = 1; i <= n; i++) {       if (pre[i] == m && m <= k) {         int mxen = min(n, i + k - 1);         int mnbe = max(1, i - k + 1);         if (mxen - 2 * k + 1 >= 1) {           printf( Yes n%d %d , mxen - 2 * k + 1, mxen - k + 1);           return 0;         }         if (mnbe + 2 * k - 1 <= n) {           printf( Yes n%d %d , mnbe, mnbe + k);           return 0;         }       }     }     build(1, 1, n, 0);     for (int i = 0; i <= min(k, m); i++) {       for (int j = 0; j < (int)sss[m - i].size(); j++)         up(1, 1, n, sss[m - i][j], 1, 1);       flag = false;       int pos = get(1, 1, n, k - i + 1, n, 0);       for (int j = 0; j < (int)ppp[i].size(); j++) up(1, 1, n, ppp[i][j], 0, 0);       if (m - i > k) continue;       int en = pos + i - 1;       int be = en - k + 1;       if (en > n || be < 1) continue;       pair<int, int> res = getsuf(en + 1, m - i);       if (res.first < en + 1 || res.second > n) continue;       printf( Yes n%d %d , be, res.first);       return 0;     }     printf( No );   }


#include <bits/stdc++.h>   int read() {     int r = 0, t = 1, c = getchar();     while (c <  0  || c >  9 ) {       t = c ==  -  ? -1 : 1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       r = (r << 3) + (r << 1) + (c ^ 48);       c = getchar();     }     return r * t;   }   const int N = 200010;   int n;   int c1, c2;   std::vector<int> e[N];   int dfs(int u, int f) {     int c = 0;     for (auto v : e[u])       if (v != f) c += dfs(v, u);     if (c) {       c1++;       c2 += c;       return 0;     } else       return 1;   }   int main() {     for (int T = (1), end_T = (read()); T <= end_T; T++) {       n = read();       c1 = c2 = 0;       for (int i = (1), end_i = (n); i <= end_i; i++) e[i].clear();       for (int i = (1), end_i = (n - 1); i <= end_i; i++) {         int u = read(), v = read();         e[u].push_back(v);         e[v].push_back(u);       }       dfs(1, 0);       printf( %d n , c2 - (c1 - 1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     vector<pair<long long, char> > v;     vector<long long> freq(27, 0);     string s2(s.length(),  1 );     vector<long long> vis(s.length() + 1, 0);     for (long long i = 0; i < s.length(); i++) {       freq[s[i] -  a ]++;     }     for (long long i = 0; i < 27; i++) {       char c = i +  a ;       if (freq[i] > 0) v.push_back({freq[i], c});     }     sort(v.begin(), v.end());     reverse(v.begin(), v.end());     long long n = s.length();     long long cnt = 0;     for (long long i = 2; i <= n; i += 2) {       vis[i] = 1;       cnt++;     }     for (long long i = 2; i <= n; i += 1) {       bool add = false;       if (!vis[i])         for (long long j = i; j <= n; j += i) {           if (vis[j]) add = true;         }       if (!add) continue;       for (long long j = i; j <= n; j += i) {         if (!vis[j]) cnt++;         vis[j] = 1;       }     }     if (v[0].first < (cnt)) {       cout <<  NO ;       return 0;     }     long long i;     for (i = 2; i <= n; i++) {       if (!vis[i]) continue;       s2[i - 1] = v[0].second;       v[0].first--;       vis[i] = 1;     }     long long k = 0;     for (i = 1; i <= n; i++) {       if (!vis[i]) {         if (v[k].first == 0) {           k++;         }         s2[i - 1] = v[k].second;         v[k].first--;       }     }     cout <<  YES n ;     cout << s2;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 405;   int a[MAXN], v[MAXN];   int n;   void work(int x) {     memset(v, 0, sizeof(v));     for (int i = 1; x != 0; i++) {       v[i] = x / a[i];       x %= a[i];     }   }   int work2(int x) {     int res = 0;     for (int i = 1; x != 0; i++) {       res += x / a[i];       x %= a[i];     }     return res;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     int ans = -1;     for (int i = 1; i < n; i++) {       work(a[i] - 1);       for (int j = i + 1; j <= n; j++) {         int s = 0, m = 0;         for (int k = i; k < j; k++) {           s += v[k] * a[k];           m += v[k];         }         s += (v[j] + 1) * a[j];         m += (v[j] + 1);         if (work2(s) > m) {           if (ans == -1 || s < ans) ans = s;         }       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef int arr32[5000010];   arr32 x, rk, l, r;   int n;   long long ans;   bool cmp(const int &a, const int &b) { return x[a] < x[b]; }   void erase(int x) { r[l[x]] = r[x], l[r[x]] = l[x]; }   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       scanf( %d , x + i);       rk[i] = i, l[i] = i - 1, r[i] = i + 1;     }     sort(rk + 1, rk + n + 1, cmp);     for (int i = 1; i <= n - 2; ++i) {       int p = rk[i];       if (l[p] == 0)         ans += x[p], erase(p);       else if (r[p] == n + 1)         ans += x[p], erase(p);       else         ans += min(x[l[p]], x[r[p]]), erase(p);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   int main() {     int ss, x, y, flag = 0, kk, flag2 = 1;     scanf( %d%d%d , &ss, &x, &y);     if (x == 1 || x == ss) flag++;     if (y == 1 || y == ss) flag++;     if (x >= ss / 2 && x <= ss / 2 + 1 && y >= ss / 2 && y <= ss / 2 + 1)       flag2 = 0;     if (flag == 0) kk = 9;     if (flag == 1) kk = 6;     if (flag == 2) kk = 4;     printf( %s n , (ss * ss >= kk * 2 && flag2) ?  YES  :  NO );   }


#include <bits/stdc++.h>   using namespace std;   long long ans, n, k, a, b;   int main() {     scanf( %I64d %I64d  , &n, &k);     scanf( %I64d %I64d , &a, &b);     long long t = n - 1;     t *= a;     if (k == 1) {       printf( %I64d , t);       return 0;     }     while (n > 1) {       if (n % k == 0)         ans += min(b, a * (n - (n / k))), n /= k;       else {         if (n - (n % k) > 0)           ans += (a * (n % k));         else           ans += (a * ((n % k) - 1));         n -= (n % k);       }     }     ans = min(ans, t);     printf( %I64d , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 60 + 5;   char a[N][N];   int l[N][N];   int r[N][N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int m, n;       cin >> n >> m;       for (int i = 1; i <= n; i++) cin >> (a[i] + 1);       int f1 = 0, f2 = 0;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           if (a[i][j] ==  A ) f1 = 1;           if (a[i][j] ==  P ) f2 = 1;         }       }       if (!f2) {         printf( 0 n );         continue;       }       if (!f1) {         printf( MORTAL n );         continue;       }       int visl = 0, visb = 0, visj = 0;       if (a[1][1] ==  A  || a[1][m] ==  A  || a[n][1] ==  A  || a[n][m] ==  A )         visj = 1;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           if (i == 1 || j == 1 || i == n || j == m) {             if (a[i][j] ==  A ) visb = 1;           }           if (a[i][j] ==  A ) l[i][j] = l[i][j - 1] + 1;         }         if (l[i][m] == m) visl = 1;       }       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           if (a[i][j] ==  A ) r[i][j] = r[i - 1][j] + 1;           if (r[n][j] == n) visl = 1;         }       }       int visjl = 0;       if (r[n][m] == n || l[n][m] == m || r[n][1] == n || l[1][m] == m) visjl = 1;       if (visjl)         printf( 1 n );       else if (visj)         printf( 2 n );       else if (visl && visb)         printf( 2 n );       else if (visb)         printf( 3 n );       else         printf( 4 n );       memset(l, 0, sizeof l);       memset(r, 0, sizeof r);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 2;   unsigned long long h[N];   int p[N];   bool ed[N], flg[N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());     unordered_map<unsigned long long, int> mp;     int n, m = 0, i, j, k;     unsigned long long tot = 0;     cin >> n;     for (i = 2; i <= n; i++) {       if (!ed[i]) p[m++] = i, h[i] = rnd();       for (j = 0; j < m && (k = i * p[j]) <= n; j++) {         ed[k] = 1;         h[k] = h[i] ^ h[p[j]];         if (i % p[j] == 0) break;       }       if (1 ^ n - i & 1) tot ^= h[i];     }     for (i = 3; i <= n; i++) h[i] ^= h[i - 1];     fill(flg + 1, flg + n + 1, 1);     if (tot == 0) goto out;     for (i = 1; i <= n; i++)       if (tot == h[i]) {         flg[i] = 0;         goto out;       }     for (i = 1; i <= n; i++) {       auto it = mp.find(tot ^ h[i]);       if (it != mp.end()) {         flg[i] = flg[it->second] = 0;         goto out;       }       mp[h[i]] = i;     }     flg[n >> 1] = flg[2] = flg[n] = 0;   out:     cout << accumulate(flg + 1, flg + n + 1, 0) <<   n ;     for (int i = 1; i <= n; i++)       if (flg[i]) cout << i <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     unsigned long long a, b;     cin >> a >> b;     while (a != 0 && b != 0) {       if (a >= 2 * b) {         a = a % (2 * b);       } else if (b >= 2 * a) {         b = b % (a * 2);       } else         break;     }     cout << a <<     << b << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double pi = 3.1415926535897932384626433;   const int mod = 1000000007;   const long long inf = 3e18;   long long n, m, k;   long long dp[5001][5001];   vector<long long> v;   long long mkc(long long i, long long j) {     if (j == 0 or i >= v.size()) {       return 0;     }     if (dp[i][j] != -1) return dp[i][j];     long long ans = 0;     ans = max(mkc(i + 1, j), v[i] + mkc(i + m, j - 1));     return dp[i][j] = ans;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     memset(dp, -1, sizeof(dp));     cin >> n >> m >> k;     vector<long long> a(n), pre(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     pre[0] = a[0];     for (long long i = 1; i < n; i++) {       pre[i] = pre[i - 1] + a[i];     }     for (long long i = n - 1; i >= m; i--) {       pre[i] = pre[i] - pre[i - m];     }     for (long long i = m - 1; i < n; i++) {       v.push_back(pre[i]);     }     cout << mkc(0, k);   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t;     cin >> t;     while (t--) {       long long x, y, n;       cin >> x >> y >> n;       long long rem = n % x;       long long ans = n - rem;       if (ans + y <= n) {         cout << ans + y <<   n ;       } else {         ans -= (x - y);         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T0, typename T1>   bool chkmin(T0 &a, const T1 &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T0, typename T1>   bool chkmax(T0 &a, const T1 &b) {     return a < b ? a = b, 1 : 0;   }   const int oo = 0x3f3f3f3f, Max_val = 314000000;   const int maxn = 100000, maxm = 100000;   int deg[maxn + 5];   pair<pair<int, int>, int> all[maxm + 5];   int tmp[maxm + 5] = {0};   vector<int> yyt[maxn + 5];   bool vis[maxn + 5] = {0};   int Min[maxn + 5];   int Max[maxn + 5];   vector<int> zzj[maxm + 5];   vector<int> owaski[maxn + 5];   int find(const int &first) {     if (vis[first]) return -2;     if (Max[first]) return Max[first];     vis[first] = 1;     Max[first] = -2;     for (int i = (0), _end_ = ((int((owaski[first]).size()))); i != _end_; ++i) {       const int &second = owaski[first][i];       int tmp = ::tmp[second];       for (int j = (0), _end_ = ((int((zzj[second]).size()))); j != _end_; ++j) {         const int &k = zzj[second][j];         int tmp0 = find(k);         if (tmp0 == -1)           goto yyt;         else if (tmp0 == -2)           tmp = -2;         else if (tmp != -2)           chkmin(tmp += tmp0, Max_val);       }       if (tmp == -2) {         Max[first] = -2;         vis[first] = 0;         return Max[first];       }       Max[first] = max(Max[first], tmp);     yyt:;     }     vis[first] = 0;     return Max[first];   }   int m, n;   int main() {     memset(Min, oo, sizeof Min);     scanf( %d%d , &m, &n);     for (int i = (0), _end_ = (m); i != _end_; ++i) {       scanf( %d , &all[i].first.first), --all[i].first.first;       static int nn;       scanf( %d , &nn);       all[i].first.second = tmp[i] = 0;       owaski[all[i].first.first].push_back(i);       for (int j = (0), _end_ = (nn); j != _end_; ++j) {         static int first;         scanf( %d , &first);         if (first == -1)           ++all[i].first.second, ++tmp[i];         else           --first, ++all[i].second, yyt[first].push_back(i),               zzj[i].push_back(first);       }     }     for (int i = (0), _end_ = (m); i != _end_; ++i)       if (!all[i].second) chkmin(Min[all[i].first.first], all[i].first.second);     priority_queue<pair<int, int>, vector<pair<int, int> >,                    greater<pair<int, int> > >         q;     for (int i = (0), _end_ = (n); i != _end_; ++i)       if (Min[i] < oo) q.push(make_pair(Min[i], i));     while (!q.empty()) {       int first = q.top().second;       q.pop();       if (vis[first]) continue;       vis[first] = 1;       for (int i = (0), _end_ = ((int((yyt[first]).size()))); i != _end_; ++i) {         const int &second = yyt[first][i];         all[second].first.second =             min(all[second].first.second + Min[first], Max_val);         if (!--all[second].second &&             chkmin(Min[all[second].first.first], all[second].first.second))           q.push(               make_pair(Min[all[second].first.first], all[second].first.first));       }     }     memset(vis, 0, sizeof vis);     memset(Max, 0, sizeof Max);     for (int i = (0), _end_ = (n); i != _end_; ++i)       if (Min[i] == oo) Min[i] = Max[i] = -1;     for (int i = (0), _end_ = (n); i != _end_; ++i) {       find(i);       printf( %d %d n , Min[i], Max[i]);     }     return 0;   }


#include <bits/stdc++.h>   int INF = 2147483647;   double INFD = 2147483647;   double PI = 3.14159265359;   using namespace std;   struct Child {     Child(long long v_, long long d_, long long p_) {       gone = false;       v = v_;       d = d_;       p = p_;     }     bool gone;     long long v;     long long d;     long long p;   };   int Enter(vector<Child>& kids, int base) {     long long v = kids[base].v, deduction = 0;     int gone_kid = -1;     kids[base].gone = true;     base++;     while (deduction < v && base < kids.size()) {       if (!kids[base].gone) {         kids[base].p -= v - deduction;         if (kids[base].p < 0 && gone_kid == -1) {           gone_kid = base;         }         deduction++;       }       base++;     }     return gone_kid;   }   void print(vector<Child>& kids) {     int i = 0;     while (i < kids.size()) {       Child c = kids[i];       cout << i + 1 <<  :   << c.v <<     << c.d <<     << c.p;       if (c.gone) {         cout <<   -------- ;       }       cout << endl;       i++;     }   }   void Deduce(vector<Child>& kids, int base) {     long long deduction = 0;     int i = base;     while (i < kids.size()) {       if (!kids[i].gone) {         kids[i].p -= deduction;         if (kids[i].p < 0) {           kids[i].gone = true;           deduction += kids[i].d;         }       }       i++;     }   }   int main() {     int n = 0;     cin >> n;     vector<Child> kids(n, Child(0, 0, 0));     vector<int> entered;     int i = 0;     while (i < n) {       cin >> kids[i].v >> kids[i].d >> kids[i].p;       i++;     }     i = 0;     while (i < n) {       if (!kids[i].gone) {         entered.push_back(i);         int gone_kid = Enter(kids, i);         if (gone_kid != -1) {           Deduce(kids, gone_kid);         }       }       i++;     }     cout << entered.size() << endl;     i = 0;     while (i < entered.size()) {       if (i) {         cout <<    ;       }       cout << entered[i] + 1;       i++;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 55;   int res[N][N][N][N];   int tmp[N][N], dat[N][N];   string s[N];   int n, m, q, a, b, c, d;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> q;     for (int i = 1; i <= n; i++) {       cin >> s[i];       for (int j = 1; j <= m; j++) dat[i][j] = (s[i][j - 1] ==  1 );     }     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= m; j++) {         if (i == 0 || j == 0) continue;         tmp[i][j] = tmp[i - 1][j] + tmp[i][j - 1] - tmp[i - 1][j - 1] + dat[i][j];       }     }     for (int xa = 1; xa <= n; xa++)       for (int ya = 1; ya <= m; ya++)         for (int xb = 1; xb <= n; xb++)           for (int yb = 1; yb <= m; yb++) {             int sum = tmp[xb][yb] - tmp[xa - 1][yb] - tmp[xb][ya - 1] +                       tmp[xa - 1][ya - 1];             res[xa][ya][xb][yb] = (sum == 0);           }     for (int xa = n; xa >= 2; xa--)       for (int ya = 1; ya <= m; ya++)         for (int xb = xa; xb <= n; xb++)           for (int yb = ya; yb <= m; yb++)             res[xa - 1][ya][xb][yb] += res[xa][ya][xb][yb];     for (int xa = 1; xa <= n; xa++)       for (int ya = m; ya >= 2; ya--)         for (int xb = xa; xb <= n; xb++)           for (int yb = ya; yb <= m; yb++)             res[xa][ya - 1][xb][yb] += res[xa][ya][xb][yb];     for (int xa = 1; xa <= n; xa++)       for (int ya = 1; ya <= m; ya++)         for (int xb = xa; xb <= n; xb++)           for (int yb = ya; yb <= m; yb++)             res[xa][ya][xb + 1][yb] += res[xa][ya][xb][yb];     for (int xa = 1; xa <= n; xa++)       for (int ya = 1; ya <= m; ya++)         for (int xb = xa; xb <= n; xb++)           for (int yb = ya; yb <= m; yb++)             res[xa][ya][xb][yb + 1] += res[xa][ya][xb][yb];     while (q--) {       cin >> a >> b >> c >> d;       cout << res[a][b][c][d] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t, n, k, a[200005];   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> t;     while (t--) {       cin >> n >> k;       for (long long i = 0; i < n; i++) cin >> a[i];       long long Min = k;       for (long long i = k; i < n; i++)         if (a[i] - a[i - k] < a[Min] - a[Min - k]) Min = i;       cout << (a[Min] + a[Min - k]) / 2 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long ax, ay, bx, by, cx, cy;   void check(long long xx, long long yy) {     if (cx == 0 && cy == 0) {       if (xx == 0 && yy == 0) {         cout <<  YES ;         exit(0);       } else         return;     }     if ((cx * (yy)-cy * (xx)) % (cx * cx + cy * cy) != 0 ||         (cx * (xx) + cy * (yy)) % (cx * cx + cy * cy) != 0)       return;     cout <<  YES ;     exit(0);   }   int main() {     cin >> ax >> ay >> bx >> by >> cx >> cy;     check(bx - ax, by - ay);     check(bx - ay, by + ax);     check(bx + ax, by + ay);     check(bx + ay, by - ax);     cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 356438;   int from[N], to[N], limL, nextp[N], headp[N], p[N], Lcnt[N], chcnt[N], num,       a[N], b[N], sz, c[N];   bool taboo[N], boo[N];   long long limW, W[N], Wcnt[N], ans;   inline int lowbit(int x) { return x & (-x); }   inline int ask(int x) {     int ans = 0;     while (x) {       ans += c[x];       x -= lowbit(x);     }     return ans;   }   inline void add(int x, int y) {     while (x < N) {       c[x] += y;       x += lowbit(x);     }   }   inline void addedge(int x, int y, long long z, int &tot) {     from[tot] = x;     to[tot] = y;     W[tot] = z;     nextp[tot] = headp[x];     headp[x] = tot;     ++tot;   }   void dfs(int x) {     a[sz] = x;     ++sz;     chcnt[x] = 1;     for (int i = headp[x]; i != -1; i = nextp[i]) {       if (taboo[i]) continue;       int y = to[i];       if (p[x] != -1 && from[p[x]] == y) continue;       p[y] = i;       dfs(y);       chcnt[x] += chcnt[y];     }   }   void dfs2(int x, bool b) {     boo[x] = b;     for (int i = headp[x]; i != -1; i = nextp[i]) {       if (taboo[i]) continue;       int y = to[i];       if (p[x] != -1 && from[p[x]] == y) continue;       p[y] = i;       Lcnt[y] = Lcnt[x] + 1;       Wcnt[y] = Wcnt[x] + W[i];       dfs2(y, b);     }   }   inline bool cmpb(int x, int y) {     if (Wcnt[x] != Wcnt[y]) return Wcnt[x] < Wcnt[y];     if (Lcnt[x] != Lcnt[y]) return Lcnt[x] < Lcnt[y];     return boo[x] < boo[y];   }   void work(int root) {     p[root] = -1;     Lcnt[root] = 0;     Wcnt[root] = 0;     sz = 0;     dfs(root);     if (sz == 1) return;     int tempsz = 0, tempedge = -1;     for (int i = 1; i < sz; ++i) {       int x = a[i];       if (min(chcnt[x], sz - chcnt[x]) > tempsz) {         tempsz = min(chcnt[x], sz - chcnt[x]);         tempedge = p[x];       }     }     taboo[tempedge] = taboo[tempedge ^ 1] = true;     Lcnt[from[tempedge]] = 0;     Wcnt[from[tempedge]] = 0;     p[from[tempedge]] = -1;     dfs2(from[tempedge], true);     Lcnt[to[tempedge]] = 0;     Wcnt[to[tempedge]] = 0;     p[to[tempedge]] = -1;     dfs2(to[tempedge], false);     for (int i = 0; i < sz; ++i) {       int x = a[i];       if (boo[x]) {         Lcnt[x] = limL - Lcnt[x] - 1;         Wcnt[x] = limW - Wcnt[x] - W[tempedge];       }       b[i] = x;     }     sort(b, b + sz, cmpb);     for (int i = 0; i < sz; ++i) {       int x = b[i];       if (boo[x]) {         if (Lcnt[x] < 0) continue;         ans += ask(Lcnt[x] + 1);       } else {         add(Lcnt[x] + 1, 1);       }     }     for (int i = 0; i < sz; ++i) {       int x = b[i];       if (boo[x]) {       } else {         add(Lcnt[x] + 1, -1);       }     }     work(to[tempedge]);     work(from[tempedge]);   }   int main() {     int n;     scanf( %d%d%I64d , &n, &limL, &limW);     int tot = 0;     memset(headp, -1, sizeof(headp));     for (int i = 2; i <= n; ++i) {       int j;       long long temp;       scanf( %d %I64d , &j, &temp);       addedge(i, j, temp, tot);       addedge(j, i, temp, tot);     }     work(1);     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int K = 7;   const int N = 2005;   int a[K][N];   vector<int> s[N];   int d[N], n, k, p;   void read() {     cin >> n >> k;     for (int i = 0; i < int(k); ++i) {       for (int j = 0; j < int(n); ++j) {         cin >> p;         --p;         if (i == 0)           a[i][j] = p;         else           a[i][p] = j;       }     }   }   inline bool can_be_next(const vector<int>& a, const vector<int>& b) {     assert(a.size() == b.size());     for (int i = 0; i < int(a.size()); ++i) {       if (b[i] <= a[i]) return false;     }     return true;   }   void solve() {     for (int i = 0; i < int(n); ++i) {       s[i] = vector<int>(k - 1);       for (int j = 1; j < k; ++j) s[i][j - 1] = a[j][a[0][i]];     }     int res = 0;     for (int i = 0; i < int(n); ++i) {       d[i] = 1;       for (int j = 0; j < int(i); ++j)         if (can_be_next(s[j], s[i])) d[i] = max(d[j] + 1, d[i]);       res = max(res, d[i]);     }     cout << res << endl;   }   int main() {     read();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     long long ans = 0;     for (int i = 1; i <= n; i++) ans += abs(a[i - 1] - i);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using point = pair<int, int>;   int D(point a, point b, point c) {     return a.first * (b.second - c.second) - a.second * (b.first - c.first) +            (b.first * c.second - b.second * c.first);   }   int main() {     int n, d;     cin >> n >> d;     vector<point> poligon;     poligon.push_back(make_pair(0, d));     poligon.push_back(make_pair(d, 0));     poligon.push_back(make_pair(n, n - d));     poligon.push_back(make_pair(n - d, n));     poligon.push_back(poligon[0]);     int k;     cin >> k;     vector<point> points(k);     for (auto &[x, y] : points) cin >> x >> y;     for (auto each : points) {       int s1 = 0, s2 = 0;       for (int i = 0; i < poligon.size() - 1; ++i) {         int test = D(each, poligon[i], poligon[i + 1]);         if (test > 0)           s1++;         else if (test < 0)           s2++;       }       if (s2 == 0 or s1 == 0)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> prevSmall(vector<long long> &a, long long n) {     vector<long long> res(n, -1);     for (long long i = 1; i < n; ++i) {       long long j = i - 1;       while (j >= 0 && a[j] >= a[i]) j = res[j];       res[i] = j;     }     return res;   }   vector<long long> nextSmall(vector<long long> &a, long long n) {     vector<long long> res(n, n);     for (long long i = n - 2; i >= 0; --i) {       long long j = i + 1;       while (j < n && a[j] > a[i]) j = res[j];       res[i] = j;     }     return res;   }   vector<long long> prevLarge(vector<long long> &a, long long n) {     vector<long long> res(n, -1);     for (long long i = 1; i < n; ++i) {       long long j = i - 1;       while (j >= 0 && a[j] <= a[i]) j = res[j];       res[i] = j;     }     return res;   }   vector<long long> nextLarge(vector<long long> &a, long long n) {     vector<long long> res(n, n);     for (long long i = n - 2; i >= 0; --i) {       long long j = i + 1;       while (j < n && a[j] < a[i]) j = res[j];       res[i] = j;     }     return res;   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     {       long long n;       cin >> n;       vector<long long> a(n);       for (long long i = 0; i < (n); ++i) cin >> a[i];       long long ans = 0;       auto pl = prevLarge(a, n), ps = prevSmall(a, n);       auto nl = nextLarge(a, n), ns = nextSmall(a, n);       for (long long i = 0; i < (n); ++i) {         long long lc = i - pl[i], rc = nl[i] - i;         ;         ans += lc * rc * a[i];         lc = i - ps[i], rc = ns[i] - i;         ;         ans -= lc * rc * a[i];       }       cout << ans;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[int(1e7 + 100)];   void aum(int i) {     if (i % 2) {       arr[i / 2 + 1] += arr[i];       arr[i / 2] += arr[i];     } else       arr[i / 2] += 2 * arr[i];   }   int main() {     ios_base::sync_with_stdio(0);     set<int> conj;     memset(arr, 0, sizeof arr);     int n, m, l, ans = int(2 * 1e9 + 100), ptr = 0;     cin >> n >> m;     long long cnt = n, carry = 0, slices = m;     for (int i = 0; i < n; ++i) {       cin >> l;       carry += l;       ++arr[l];     }     if (carry < m) {       cout <<  -1 n ;       return 0;     }     for (int i = int(1e7 + 100); cnt < m; --i) {       if (arr[i]) {         aum(i);         cnt += arr[i];         arr[i] = 0;       }     }     long long aux = 0;     for (int i = int(1e7 + 100); i >= 1; --i) {       if (arr[i]) {         conj.insert(i);         aux += arr[i];         if (aux >= m) {           ans = i;           break;         }       }     }     for (int i = int(1e7 + 100); i >= 1; --i) {       if (arr[i]) {         int go = (i % 2 == 1 ? 1 : 0);         if (i / 2 + go <= *(conj.begin())) {           break;         } else {           aum(i);           if (i % 2) conj.insert(i / 2 + 1);           conj.insert(i / 2);           conj.erase(i);           aux += arr[i];           while (1) {             int val = (*conj.begin());             if (aux - arr[val] < m) break;             aux -= arr[val];             conj.erase(conj.begin());           }         }       }     }     cout << max(1, *(conj.begin())) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   int value, ans;   int bads[200010], maxDelay[200010], extraDelay[200010], a[200010], v[200010];   vector<int> T[200010], val[200010], pre[200010], post[200010];   void dfs(int x, int f) {     bads[x] = 0;     maxDelay[x] = 0;     extraDelay[x] = 0;     for (int i = 0; i < T[x].size(); ++i) {       int y = T[x][i];       if (y != f) {         dfs(y, x);         bads[x] += bads[y];         if (bads[y] == 0) {           maxDelay[x] += maxDelay[y];         } else {           extraDelay[x] = max(extraDelay[x], maxDelay[y]);         }       }     }     if (a[x] < value) {       bads[x]++;       maxDelay[x] = 0;     } else {       maxDelay[x] += extraDelay[x] + 1;     }   }   void dfs(int x, int f, int fBads, int fMaxDelay) {     if (a[x] >= value) {       int finalVal = maxDelay[x] - extraDelay[x];       finalVal += (fBads == 0 ? fMaxDelay + extraDelay[x]                               : max(fMaxDelay, extraDelay[x]));       ans = max(ans, finalVal);     }     for (int i = 0; i < T[x].size(); ++i) {       int y = T[x][i];       if (y == f) {         val[x][i] = (fBads > 0 ? fMaxDelay : 0);       } else {         val[x][i] = (bads[y] > 0 ? maxDelay[y] : 0);       }     }     int sz = T[x].size();     pre[x][0] = val[x][0];     post[x][sz - 1] = val[x][sz - 1];     for (int i = 1; i < sz; ++i) pre[x][i] = max(val[x][i], pre[x][i - 1]);     for (int i = sz - 2; i >= 0; --i) post[x][i] = max(val[x][i], post[x][i + 1]);     for (int i = 0; i < T[x].size(); ++i) {       int y = T[x][i];       if (y == f) continue;       if (a[x] < value) {         dfs(y, x, bads[x] - bads[y] + fBads, 0);       } else {         int newBads = bads[x] - bads[y] + fBads;         int newMaxDelay =             maxDelay[x] - extraDelay[x] - (bads[y] == 0 ? maxDelay[y] : 0) +             (fBads == 0 ? fMaxDelay : 0) +             max((i == 0 ? 0 : pre[x][i - 1]), (i == sz - 1 ? 0 : post[x][i + 1]));         dfs(y, x, newBads, newMaxDelay);       }     }   }   void reset() {}   int check(int tval) {     reset();     value = tval;     ans = 0;     dfs(1, 0);     dfs(1, 0, 0, 0);     return (ans >= k);   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       v[i] = a[i];     }     sort(v + 1, v + n + 1);     for (int i = 1; i < n; ++i) {       int a, b;       cin >> a >> b;       T[a].push_back(b);       T[b].push_back(a);     }     for (int i = 1; i <= n; ++i) {       val[i].resize(T[i].size(), 0);       pre[i].resize(T[i].size(), 0);       post[i].resize(T[i].size(), 0);     }     int lo = 0, hi = n + 1;     while (hi - lo > 1) {       int mid = (lo + hi) / 2;       if (check(v[mid]))         lo = mid;       else         hi = mid;     }     cout << v[lo];   }


#include <bits/stdc++.h>   using namespace std;   bool dbg = 0;   void bad(string mes =  Impossible ) {     cout << mes;     exit(0);   }   template <typename T>   string bin(T x, int st = 2) {     string ans =   ;     while (x > 0) {       ans += char( 0  + x % st);       x /= st;     }     reverse(ans.begin(), ans.end());     return ans.empty() ?  0  : ans;   }   template <typename T>   T input() {     T ans = 0, m = 1;     char c =    ;     while (!((c >=  0  && c <=  9 ) || c ==  - )) {       c = getchar();     }     if (c ==  - ) m = -1, c = getchar();     while (c >=  0  && c <=  9 ) {       ans = ans * 10 + (c -  0 ), c = getchar();     }     return ans * m;   }   template <typename T>   void read(T& a) {     a = input<T>();   }   template <typename T>   void read(T& a, T& b) {     read(a), read(b);   }   template <typename T>   void read(T& a, T& b, T& c) {     read(a, b), read(c);   }   template <typename T>   void read(T& a, T& b, T& c, T& d) {     read(a, b), read(c, d);   }   void reads(string& second) {     string ans =   ;     char c =   n ;     while (c ==   n  || c ==    ) c = getchar();     while (c !=   n  && c !=     && c) ans += c, c = getchar();     second = ans;   }   const int inf = 1e9;   const double eps = 1e-9;   const int maxn = 1e6 + 10, base = 1e9 + 7;   const long long llinf = 1e18 + 5;   template <typename T>   T binpow(T n, T second) {     if (second <= 0) return 1LL;     if (second % 2 == 0) {       T b = binpow(n, second / 2);       return (1LL * b * b) % base;     } else {       return (1LL * binpow(n, second - 1) * n) % base;     }   }   int v[maxn], c[maxn];   int lg[maxn];   int mnSt[maxn][22];   int n, k;   void buildMn() {     for (int i = 0; i < n; i++) mnSt[i][0] = c[i];     for (int i = 1; i < 22; i++)       for (int j = 0; j + (1 << i) <= n; j++) {         mnSt[j][i] = min(mnSt[j][i - 1], mnSt[j + (1 << (i - 1))][i - 1]);       }   }   int mxSt[maxn][22];   void buildMx() {     for (int i = 0; i < n; i++) mxSt[i][0] = v[i];     for (int i = 1; i < 22; i++)       for (int j = 0; j + (1 << i) <= n; j++) {         mxSt[j][i] = max(mxSt[j][i - 1], mxSt[j + (1 << (i - 1))][i - 1]);       }   }   int get_min(int l, int r) {     int j = lg[r - l + 1];     return min(mnSt[l][j], mnSt[r - (1 << j) + 1][j]);   }   int get_max(int l, int r) {     int j = lg[r - l + 1];     return max(mxSt[l][j], mxSt[r - (1 << j) + 1][j]);   }   int main() {     for (int i = 2; i < maxn; i++) {       lg[i] = lg[i / 2] + 1;     }     read(n, k);     for (int i = 0; i < n; i++) read(v[i]), v[i] *= 100;     for (int i = 0; i < n; i++) read(c[i]);     buildMn();     buildMx();     long double res = 0;     vector<int> mda;     for (int i = 0; i < n; i++) {       int l = i, r = n - 1;       while (r - l > 1) {         int m = (l + r) >> 1;         if (get_min(i, m) < get_max(i, m))           r = m;         else           l = m;       }       int kek = -inf;       for (int j = max(i, l - 5); j <= min(n - 1, l + 5); j++) {         kek = max(kek, min(get_min(i, j), get_max(i, j)));       }       mda.push_back(kek);     }     sort((mda).begin(), (mda).end());     long double cf = (long double)n / k;     int first = n - 1;     for (int i = 0; first >= k - 1; i++, first--) {       res += (long double)mda[i] / cf;       cf *= (long double)n - i - 1;       cf /= (long double)n - k - i;     }     cout << fixed << setprecision(20) << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double _PI = 3.141592653589793;   double n, m, k, ans = 0;   signed main() {     scanf( %lf %lf %lf , &n, &m, &k);     ans += ((n * n * sqrt(3)) / 4) * (sqrt(n * n * 2 / 3)) / 3;     ans += (m * m) / 3 * (sqrt(m * m / 2));     double S = (k * k * 5) / (4 * tan(_PI / 5)), r = S / (2.5 * k);     ans += S * sqrt(3 * k * k / 4 - r * r) / 3;     printf( %0.12lf , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long b, d, s, mx, sm = 0;     cin >> b >> d >> s;     mx = max(b, max(s, d));     if (mx != b) b++;     if (mx != d) d++;     if (mx != s) s++;     sm += mx - b;     sm += mx - d;     sm += mx - s;     cout << sm << endl;     return 0;   }


#include <bits/stdc++.h>   const int MAX_N = 300000;   const int MAX_M = 300000;   std::vector<int> types[1 + MAX_N];   std::vector<int> neighbours[1 + MAX_N];   int c;   int colours[1 + MAX_M];   bool used[1 + MAX_M];   void dfs(int u = 1, int father = 0) {     for (int type : types[u])       if (colours[type] != 0) used[colours[type]] = true;     int i = 0, j = 1;     while (i < (int)types[u].size()) {       if (used[j]) {         j++;       } else {         if (colours[types[u][i]] == 0) {           colours[types[u][i]] = j;           j++;         }         i++;       }     }     for (int type : types[u]) used[colours[type]] = false;     for (int v : neighbours[u])       if (v != father) {         dfs(v, u);       }   }   int main(void) {     int n, m;     scanf( %d%d , &n, &m);     int c = 1;     for (int i = 1; i <= n; i++) {       int s;       scanf( %d , &s);       c = std::max(c, s);       for (int j = 0; j < s; j++) {         int type;         scanf( %d , &type);         types[i].push_back(type);       }     }     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d%d , &u, &v);       neighbours[u].push_back(v);       neighbours[v].push_back(u);     }     dfs();     for (int i = 1; i <= m; i++)       if (colours[i] == 0) colours[i] = 1;     printf( %d n , c);     for (int i = 1; i <= m; i++) printf( %d  , colours[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = (long int)3e5 + 5;   long long power(long long x, long long y) {     long long res = 1;     while (y > 0) {       if (y & 1) res = (res * x) % 1000000007;       y = y >> 1;       x = (x * x) % 1000000007;     }     return res;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long a, b, prev;     cin >> a >> b;     long long n = (sqrt(1 + 8 * (a + b)) - 1) / 2;     std::vector<long long> d1, d2;     for (long long i = n; i > 0; i--) {       if (a - i >= 0) {         d1.push_back(i);         a -= i;       } else if (b - i >= 0) {         d2.push_back(i);         b -= i;       }     }     cout << d1.size() << endl;     for (auto i : d1) cout << i <<    ;     cout << endl;     cout << d2.size() << endl;     for (auto i : d2) cout << i <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ni() {     int a;     scanf( %d , &a);     return a;   }   double nf() {     double a;     scanf( %lf , &a);     return a;   }   char sbuf[100005];   string ns() {     scanf( %s , sbuf);     return sbuf;   }   long long nll() {     long long a;     scanf( %lld , &a);     return a;   }   template <class T>   void out(T a, T b) {     bool first = true;     for (T i = a; i != b; ++i) {       if (!first) printf(   );       first = false;       cout << *i;     }     printf(  n );   }   template <class T>   void outl(T a, T b) {     for (T i = a; i != b; ++i) {       cout << *i <<   n ;     }   }   int n, m;   int moo(char c) {     if (c <=  9  && c >=  0 ) return c -  0 ;     return c -  A  + 10;   }   int a[6][6];   bool inside(int a) { return a >= 0 && a < 6; }   char ss[1005];   vector<string> ans;   void add(char c, int z, int num) {     int j;     sprintf(ss,  %c%d n , c, z + 1);     while (num) {       if (c ==  L ) {         int t = a[z][0];         for (j = (0); j < (((5))); ++j) a[z][j] = a[z][j + 1];         a[z][5] = t;       }       if (c ==  R ) {         int t = a[z][5];         for (j = (0); j < (((5))); ++j) a[z][5 - j] = a[z][4 - j];         a[z][0] = t;       }       if (c ==  U ) {         int t = a[0][z];         for (j = (0); j < (((5))); ++j) a[j][z] = a[j + 1][z];         a[5][z] = t;       }       if (c ==  D ) {         int t = a[5][z];         for (j = (0); j < (((5))); ++j) a[5 - j][z] = a[4 - j][z];         a[0][z] = t;       }       ans.push_back(ss);       --num;     }   }   int main() {     int i, j, k;     for (i = (0); i < (((6))); ++i) {       string s = ns();       for (j = (0); j < (((6))); ++j) a[i][j] = moo(s[j]);     }   startOver:     int done = 0;     for (i = (0); i < (((15))); ++i) {       for (j = (0); j < (((15))); ++j) {         int y = j;         int x = i - j;         if (inside(x) && inside(y)) {           int xx = -1, yy = -1;           int ii, jj;           for (ii = (0); ii < ((6)); ++ii)             for (jj = (0); jj < ((6)); ++jj)               if (a[ii][jj] == y * 6 + x) xx = jj, yy = ii;           ;           if (x == 5 && y == 0) x = 5;           if (xx == x && yy == y) {           } else {             bool needUp = 0;             if (yy == 5 && xx == 5) {               if (y == 5) {                 y = 4;                 add( U , x, 1);                 needUp = 1;               }               if (x < 4)                 add( L , 5, 4 - x);               else if (x > 4)                 add( R , 5, 1);               add( D , x, 5 - y);               add( L , 5, 1);               add( U , x, 5 - y);               if (x != 5) add( R , 5, 5 - x);               if (needUp) {                 add( D , x, 1);                 y = 5;               }             } else {               if (yy == 5 && xx != 4) {                 yy = 4;                 add( U , xx, 1);                 needUp = 1;               }               if (xx < 4)                 add( L , 5, 4 - xx);               else if (xx > 4)                 add( R , 5, 1);               add( D , xx, 5 - yy);               add( L , 5, 1);               add( U , xx, 5 - yy);               if (xx != 5) add( R , 5, 5 - xx);               if (needUp) {                 add( D , xx, 1);                 yy = 5;               }               needUp = 0;               if (y == 5) {                 y = 4;                 add( U , x, 1);                 needUp = 1;               }               if (x != 5) add( L , 5, 5 - x);               add( D , x, 5 - y);               add( R , 5, 1);               add( U , x, 5 - y);               if (x < 4)                 add( R , 5, 4 - x);               else if (x > 4)                 add( L , 5, 1);               if (needUp) {                 add( D , x, 1);                 y = 5;               }             }           }           if (a[y][x] != y * 6 + x) {             printf( %d %d n , x, y);             return 0;           }           ++done;         }         if (done == 34) {           if (a[5][5] != 35) {             add( L , 5, 3);             goto startOver;           }           printf( %d n , ans.size());           for (i = (0); i < (((ans.size()))); ++i) printf( %s , ans[i].c_str());           return 0;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int n = 10, dir[4][2] = {{1, 0}, {0, 1}, {1, -1}, {1, 1}};   int main() {     char b[n][n];     for (int i = 0; i < 10; i++)       for (int j = 0; j < n; j++) scanf(  %c , &b[i][j]);     bool found = false;     for (int i = 0; !found && i < n; i++)       for (int j = 0; !found && j < n; j++)         for (int k = 0; k < 4; k++) {           int p = 0, q = 0;           for (int l = 0; l < 5; l++) {             int x = i + dir[k][0] * l, y = j + dir[k][1] * l;             if (x < 0 || x >= n || y < 0 || y >= n) break;             ;             char c = b[x][y];             p += (c ==  X ), q += (c ==  . );           }           if (p == 4 && q == 1) found = true;         }     if (found)       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_ROW = 2000 + 7;   const int MAX_COLUMN = 200 + 7;   const int BASE = 2807;   const int MOD = 1e7 + 7;   int n, m;   char TableA[MAX_ROW][MAX_COLUMN], TableB[MAX_COLUMN][MAX_ROW];   int HashA[MAX_ROW][MAX_COLUMN], HashB[MAX_COLUMN][MAX_ROW], Pow2807[MAX_ROW],       HashFinding[MOD];   int GetHash(const int& L, const int& R, const int H[]) {     return (1LL * H[R] - 1LL * H[L - 1] * Pow2807[R - L + 1] + 1LL * MOD * MOD) %            MOD;   }   int main() {     ios::sync_with_stdio(false);     cout.tie(0), cin.tie(0);     Pow2807[0] = 1;     for (int i = 1; i < MAX_ROW; ++i)       Pow2807[i] = 1LL * Pow2807[i - 1] * BASE % MOD;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %s , TableA[i] + 1);     for (int j = 1; j <= m; ++j) scanf( %s , TableB[j] + 1);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         HashA[i][j] = (1LL * HashA[i][j - 1] * BASE + TableA[i][j]) % MOD;     for (int i = 1; i <= m; ++i)       for (int j = 1; j <= n; ++j)         HashB[i][j] = (1LL * HashB[i][j - 1] * BASE + TableB[i][j]) % MOD;     for (int j = 1; j <= (n - m + 1); ++j) {       int HashBase = 0;       for (int k = j; k <= (j + m - 1); ++k)         HashBase = (1LL * HashBase * Pow2807[m] + GetHash(1, m, HashA[k])) % MOD;       HashFinding[HashBase] = j;     }     for (int j = 1; j <= (n - m + 1); ++j) {       int HashTest = 0;       for (int i = 1; i <= m; ++i)         HashTest =             (1LL * HashTest * Pow2807[m] + GetHash(j, j + m - 1, HashB[i])) % MOD;       if (HashFinding[HashTest]) {         cout << HashFinding[HashTest] <<     << j <<   n ;         return 0;       }     }     cout <<  Error 404! n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long aa[15];     long long bb[15];     for (int i = 0; i < 14; i++) {       scanf( %lld , &aa[i]);       bb[i] = aa[i];     }     long long maxx = 0;     for (int i = 0; i < 14; i++) {       if (!aa[i]) continue;       long long sum = 0;       int c = aa[i] % 14;       for (int k = 1; k <= c; k++) {         if (i + k < 14)           bb[k + i]++;         else {           bb[(i + k) % 14]++;         }       }       for (int j = 0; j < 14; j++) {         if (j != i)           bb[j] += aa[i] / 14;         else           bb[j] = aa[i] / 14;         if (bb[j] % 2 == 0) sum += bb[j];       }       if (sum > maxx) maxx = sum;       for (int j = 0; j < 14; j++) bb[j] = aa[j];     }     cout << maxx << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimization( unroll-loops )   const int N = 1e5 + 5;   const long long int mod = 1e9 + 7;   const long long int Mod = 998244353;   const long double Pi = acos(-1);   const long long int Inf = 4e18;   using namespace std;   int n, m;   vector<pair<int, int> > e;   bool Check(int k) {     vector<bool> rem(n + 1, false);     vector<int> in[n + 1], out[n + 1], rank(n + 1, -1);     for (int i = 0; i <= k; i++) {       in[e[i].second].push_back(e[i].first);       out[e[i].first].push_back(e[i].second);     }     int cnt_in = 0, cnt_out = 0, one = -1, last = -1;     for (int i = 1; i <= n; i++) {       if ((int)in[i].size() == 0) {         cnt_in++;         one = i;       }       if ((int)out[i].size() == 0) {         cnt_out++;         last = i;       }     }     if (cnt_in * cnt_out != 1) return false;     rank[1] = one;     rank[n] = last;     int cur = one;     for (int i = 2; i <= n; i++) {       int fone = 0, nxt = -1;       for (auto u : out[cur]) {         int cnt = 0;         for (auto t : in[u]) {           if (rem[t] == false) cnt++;         }         if (cnt == 1) {           fone++;           nxt = u;         }       }       if (fone != 1)         return false;       else {         rem[cur] = true;         cur = nxt;         rank[i] = cur;       }     }     for (int i = 1; i <= n; i++) {       if (rank[i] == -1) return false;     }     return true;   }   void TestCase() {     cin >> n >> m;     for (int i = 1, u, v; i <= m; i++) {       cin >> u >> v;       e.push_back({u, v});     }     int lb = 0, ub = m - 1, ans = m;     while (lb <= ub) {       int mid = (lb + ub) / 2;       if (Check(mid)) {         ans = min(ans, mid);         ub = mid - 1;       } else         lb = mid + 1;     }     ans == m ? cout << -1 : cout << ans + 1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     int T = 1;     while (T--) {       TestCase();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios_base::sync_with_stdio(false);     vector<long long int> v;     long long int n, i;     cin >> n;     if (n == 1) {       cout << 1;       return 0;     }     for (long long int i = 1; i * i <= n; ++i) {       if (n % i == 0) {         if (i * i == n)           v.push_back(i);         else {           v.push_back(i);           v.push_back(n / i);         }       }     }     sort(v.begin(), v.end());     if (v.size() == 2)       cout << n;     else {       long long int flag = 0;       if (v.size() == 3) {         if (v[1] / v[0] != v[2] / v[1]) {           cout << 1;         } else           cout << v[1];       } else {         long long int ans = v[1] / v[0];         for (i = 1; i < v.size() - 1; i++) {           if (v[i + 1] != ans * v[i]) {             flag = 1;             break;           }         }         if (flag == 1)           cout << 1;         else {           cout << v[1];         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   const int mod = 1e9 + 7;   long long qpow(long long a, long long b) {     long long res = 1;     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   struct graph {     int head[maxn], nxt[maxn << 1], to[maxn << 1], w[maxn << 1], sz;     void init() { memset(head, -1, sizeof(head)); }     graph() { init(); }     void push(int a, int b, int c) {       nxt[sz] = head[a], to[sz] = b, w[sz] = c, head[a] = sz++;     }     int& operator[](const int a) { return to[a]; }   };   long long dist[maxn][40], fi[maxn][40], mi[maxn][40];   int to[maxn], w[maxn];   int main() {     int n;     long long k;     cin >> n >> k;     for (int i = 0; i < n; ++i) scanf( %d , &to[i]);     for (int i = 0; i < n; ++i) {       scanf( %d , &w[i]);       dist[i][0] = w[i];       mi[i][0] = w[i];       fi[i][0] = to[i];     }     for (int i = 1; i <= 39; ++i) {       for (int j = 0; j < n; ++j) {         dist[j][i] = dist[j][i - 1] + dist[fi[j][i - 1]][i - 1];         fi[j][i] = fi[fi[j][i - 1]][i - 1];         mi[j][i] = min(mi[j][i - 1], mi[fi[j][i - 1]][i - 1]);       }     }     for (int i = 0; i < n; ++i) {       long long mix = 1e18;       long long ans = 0;       long long res = k;       int now = i;       for (int j = 39; j >= 0; --j) {         if (res >= (1ll << j)) {           ans += dist[now][j];           mix = min(mix, mi[now][j]);           now = fi[now][j];           res -= (1ll << j);         }       }       printf( %lld %lld n , ans, mix);     }     return 0;   }


#pragma GCC target ( avx2 )   #pragma GCC optimize ( O3 )   #pragma GCC optimize ( unroll-loops )   #include <bits/stdc++.h>   using namespace std;      typedef long long int ll;   typedef long double ld;   typedef pair<ll, ll> pl;   typedef pair<ld, ld> pd;   typedef priority_queue<pl, vector<pl>, greater<pl> > pq;      template<class T, class S>   ostream& operator<<(ostream &o, pair<T, S> p) {       return o<< ( <<p.first<< ,  <<p.second<< ) ;   }      template<template<class, class...> class T, class... A>   typename enable_if<!is_same<T<A...>, string>(), ostream&>::type   operator<<(ostream &o, T<A...> V) {    o<< [ ;    for(auto a:V) o<<a<< ,  ;    return o<< ] ;   }      // This is tuples, _p is helper   template<ll i, class... T>   typename enable_if<i==sizeof...(T)>::type _p(ostream& o, tuple<T...> t) {}      template<ll i, class... T>   typename enable_if<i<sizeof...(T)>::type _p(ostream& o, tuple<T...> t) {       _p<i+1>(o << get<i>(t)<<  ,  , t);   }       template<class... T>   ostream& operator<<(ostream& o, tuple<T...> t) {       _p<0>(o<< ( , t);       return o<< ) ;   }         #define G(x) ll x; cin >> x;   #define Gs(s) string s; cin >> s;   #define Gd(x) ld x; cin >> x;   #define F(i, l, r) for(ll i = l; i < (r); i++)   #define F1(i, l, r) for(ll i = l; i >= (r); i--)   #define Fi(i, s) for(auto i = (s).begin(); i != (s).end(); i++)   #define L G(_t) F(_t1, 0, _t)   #define H(ar, n) vector<ll> ar(n); F(_i, 0, n) cin >> ar[_i];   #define A first   #define B second   #define PA(ar, n) F(_i, 0, n) cout << (ar)[_i] <<    ; cout << endl;    #define PB push_back   #define V vector   #define CASE cout <<  Case #  << _t1 + 1 <<  :      #define CC <<     <<   #define BE(ar) ar.begin(), ar.end()   #define CO(x) cout << (x) << endl;   #define D(x) ((x) -  0 )   #define EX(x) {cout << (x) << endl; return 0;}   #define S(ar, val) memset(ar, val, sizeof(ar));   ll M = 1000000007; //998244353;   ll inv(ll a, ll b){return 1<a ? b - inv(b%a,a)*b/a : 1;}   ll gcd(ll x, ll y){if(y == 0) return x; return gcd(y, x % y); }   pl neigh[4] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};      #define N 0      int main() {           ios_base::sync_with_stdio(0);           cin.tie(0);    L{     G(n)     if(n == 2){      cout <<  1 n2 1 n1 n2 1  << endl;      continue;     }     if(n == 3){      cout <<  5 n2 3 1 n2 n1 3 n3 2  << endl;      continue;     }     ll perm[n];     perm[n-1] = 1;     perm[0] = n-1;     F1(i, n-2, (n-1)/2+2){      perm[i] = i;     }     perm[(n-1)/2] = n;     perm[(n-1)/2+1]=2;     F(i, 1, (n-1)/2){      perm[i] = i+2;     }     ll x = (n-1)*(n-1);     ll c = 1;     ll p = n-1;     while(c < n-1){      if(c %2) p--;      c++;      x += p * p;     }     cout << x << endl;     F(i, 0, n){      cout << perm[i] <<    ;     }     cout << endl;     cout << n - 1 << endl;     F1(i, n-1, (n-1)/2+2){      cout << i <<     << 1 << endl;     }     F(i, 1, (n-1)/2+2){      cout << i <<     << n << endl;     }    }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     char cur = s[0];     int count = 1;     for (int i = 1; i < s.length(); i++) {       if (cur == s[i]) {         count++;         if (count == 7) {           cout <<  YES  << endl;           return 0;         }       } else {         count = 1;         cur = s[i];       }     }     cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int M = 1e9 + 7;   const long long int N = 2e5 + 5;   const long long int inf = 2e18;   long long int mod(long long int x) { return (x % M); }   long long int mod_minus(long long int a, long long int b) {     long long int ans = (mod(a) - mod(b));     if (ans < 0) ans = mod(ans + M);     return ans;   }   long long int mod_mul(long long int a, long long int b) {     return mod(mod(a) * mod(b));   }   long long int mod_add(long long int a, long long int b) { return mod(a + b); }   long long int power(long long int a, long long int n) {     if (n == 0)       return 1;     else if (n == 1)       return a;     long long int R = power(a, n / 2) % M;     if (n % 2 == 0) {       return mod(mod_mul(R, R));     } else {       return mod(mod_mul(mod_mul(R, a), mod(R)));     }   }   long long int mod_inv(long long int n) { return power(n, M - 2); }   long long int mod_div(long long int a, long long int b) {     long long int ans = mod(a);     long long int b1 = power(b, M - 2);     ans = mod(mod_mul(ans, b1));     return ans;   }   long long int fact_mod(long long int n) {     vector<long long int> fact(n + 1);     fact[0] = 1;     for (long long int i = 1; i < n + 1; i++) {       fact[i] = mod_mul(fact[i - 1], i);     }     return fact[n];   }   long long int nCr_mod(long long int n, long long int r) {     if (r == 0 || n == 0) return 1;     long long int fac[n + 1];     fac[0] = 1;     for (long long int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % M;     return (fac[n] * mod_inv(fac[r]) % M * mod_inv(fac[n - r]) % M) % M;   }   int main() {     long long int n;     cin >> n;     long long int i;     queue<pair<long long int, long long int>> q;     vector<pair<long long int, long long int>> v(n);     for (i = 0; i < n; i++) {       long long int x, y;       cin >> x >> y;       v[i] = {x, y};       if (x == 1) q.push({i, y});     }     vector<pair<long long int, long long int>> ans;     while (q.empty() == false) {       pair<long long int, long long int> temp = q.front();       q.pop();       ans.push_back({temp});       long long int x = temp.second;       v[x].first--;       v[x].second = temp.first ^ v[x].second;       if (v[x].first == 1) q.push({x, v[x].second});     }     set<pair<long long int, long long int>> s;     for (auto x : ans) {       if (!s.count({x.first, x.second}) && !s.count({x.second, x.first}))         s.insert({x.first, x.second});     }     cout << s.size() << endl;     for (auto x : s) cout << x.first <<     << x.second << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 0x3f3f3f3f;   const double eps = 1e-9;   string words[100005];   int type[100005];   int gender[100005];   void reverse(string &c) {     for (int i = (0); i < (int((c).size()) / 2); i++) {       char tmp = c[i];       c[i] = c[int((c).size()) - i - 1];       c[int((c).size()) - i - 1] = tmp;     }   }   int main() {     string line;     getline(cin, line);     stringstream ss(line);     int n = 0;     while (true) {       string word;       ss >> word;       if (ss.fail()) break;       words[n++] = word;     }     for (int i = (0); i < (n); i++) reverse(words[i]);     for (int i = (0); i < (n); i++) {       if (words[i].find( soil ) == 0) {         type[i] = 0;         gender[i] = 0;         continue;       };       if (words[i].find( alail ) == 0) {         type[i] = 0;         gender[i] = 1;         continue;       };       if (words[i].find( rte ) == 0) {         type[i] = 1;         gender[i] = 0;         continue;       };       if (words[i].find( arte ) == 0) {         type[i] = 1;         gender[i] = 1;         continue;       };       if (words[i].find( sitini ) == 0) {         type[i] = 2;         gender[i] = 0;         continue;       };       if (words[i].find( setini ) == 0) {         type[i] = 2;         gender[i] = 1;         continue;       };       gender[i] = -1;     }     if (n == 1 && gender[0] != -1) {       cout <<  YES n ;       return 0;     }     bool SameGender = true;     for (int i = (1); i < (n); i++)       if (gender[i] != gender[0]) SameGender = false;     if (!SameGender || gender[0] == -1) {       cout <<  NO n ;       return 0;     }     int p1, p2, p3;     p1 = 0;     while (p1 < n && type[p1] == 0) ++p1;     p2 = p1;     while (p2 < n && type[p2] == 1) ++p2;     p3 = p2;     while (p3 < n && type[p3] == 2) ++p3;     if (p3 == n && p2 == p1 + 1)       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 10;   const int mod = 1e9 + 7;   const int inf = 0x3f3f3f3f;   int a1[maxn], a2[maxn];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n, k;     cin >> n >> k;     long long ans = (n + 1) * n / 2;     for (int i = 1; i <= n; i++) a1[i] = i;     for (int i = 1; i <= n; i++) a2[i] = i;     if (ans > k) {       cout << -1 <<   n ;       return 0;     }     long long up = n / 2;     long long now = n - 1;     for (int i = 1; i <= up; i++) {       if (ans + now <= k) {         ans += now;         swap(a2[i], a2[i + now]);       } else {         swap(a2[i], a2[i + k - ans]);         ans = k;         break;       }       now -= 2;     }     cout << ans <<   n ;     for (int i = 1; i <= n; i++) cout << a1[i] << (i == n ?   n  :    );     for (int i = 1; i <= n; i++) cout << a2[i] << (i == n ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long total = 0;   struct c {     int can;     int tru;   };   int main() {     double a, b, c;     scanf( %lf%lf%lf , &a, &b, &c);     printf( %.100f n , a * (b) / (b + c));   }


#include <bits/stdc++.h>   using namespace std;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c* x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug& operator<<(const c&) {       return *this;     }   };   using ll = long long;   using ld = long double;   constexpr int nax = 1000 * 1000 + 105;   constexpr int infty = 1000 * 1000 * 1000 + 5;   constexpr int mod = 1000 * 1000 * 1000 + 7;   int main() {     int n;     scanf( %d , &n);     set<int> s;     for (int i = 0; i < n; i++) {       int a;       scanf( %d , &a);       s.insert(a);     }     printf( %d n , n + 1 - (int)s.size());     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( avx2 )   #pragma GCC optimize( Os )   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string str;     long long i, j, k, x = 0, y = 0, n, m, t, sum = 0, a = 0, b, l, c, d, id;     cin >> n;     set<long long> s;     for (i = 0; i < n; i++) {       cin >> a;       sum += a;       s.insert(a);     }     x = s.size();     if (x >= 4)       cout << -1;     else {       if (x == 1)         cout << 0;       else if (x == 2) {         a = *next(s.begin(), 0);         b = *next(s.begin(), 1);         if ((a + b) % 2 != 0)           cout << b - a;         else           cout << (b - a) / 2;       } else {         a = *next(s.begin(), 0);         b = *next(s.begin(), 1);         c = *next(s.begin(), 2);         t = c - b;         if (a + c == b * 2)           cout << t;         else           cout << -1;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int x, i;   } a[100020], b[100020], c[100020];   bool f(node a, node b) { return a.x < b.x; }   bool g(node a, node b) { return a.i < b.i; }   int main() {     int i, n, j;     cin >> n;     cout <<  YES n ;     for (i = 1; i <= n; i++) {       cin >> a[i].x, a[i].i = i, a[i].x += 2;     }     sort(a + 1, a + n + 1, f);     for (i = 1; i <= n / 3; i++)       b[i].i = c[i].i = a[i].i, b[i].x = i, c[i].x = a[i].x - i;     for (i = 1 + n / 3; i <= (n + n) / 3; i++)       b[i].i = c[i].i = a[i].i, c[i].x = i, b[i].x = a[i].x - i;     for (i = 1 + (n + n) / 3, j = i; i <= n; i++)       b[i].i = c[i].i = a[i].i, c[i].x = n - i + 1, b[i].x = a[i].x - c[i].x;     sort(b + 1, b + n + 1, g);     sort(c + 1, c + n + 1, g);     for (i = 1; i <= n; i++) cout << b[i].x - 1 <<    ;     cout << endl;     for (i = 1; i <= n; i++) cout << c[i].x - 1 <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> g[1100];   map<long long, bool> visited;   long long a, b;   map<long long, long long> parent;   void dfs(long long v) {     visited[v] = 1;     if (v > 1e9 || v == b) {       return;     }     if (!visited[2 * v]) {       parent[2 * v] = v;       dfs(2 * v);     }     if (!visited[10 * v + 1]) {       parent[10 * v + 1] = v;       dfs(10 * v + 1);     }   }   decltype(0) main() {     ios::sync_with_stdio(0);     cin >> a >> b;     dfs(a);     if (!visited[b]) {       cout <<  NO n ;     } else {       vector<long long> ans;       long long x = b;       while (x != parent[a]) {         ans.push_back(x);         x = parent[x];       }       cout <<  YES n  << (long long)(ans.size()) <<   n ;       for (long long i = (long long)(ans.size()) - 1; i >= 0; i--) {         cout << ans[i] <<    ;       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long X, Y, sum, exor, l, m;   int main() {     cin >> sum >> exor;     if (sum < exor || sum % 2 != exor % 2) {       cout <<  -1  << endl;       return 0;     }     X = (sum - exor) / 2;     Y = sum - X;     cout << X << endl << Y << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   vector<int> ans;   int row[5], col[20];   vector<int> v[3];   bool vis[20][11][11][11][11][11][1 << 5];   bool solve(int x, int r1, int r2, int r3, int r4, int r5, int mask) {     if (x == m) {       if (r1 + r2 + r3 + r4 + r5) return false;       return true;     }     bool &ret = vis[x][r1][r2][r3][r4][r5][mask];     if (ret) return false;     ret = true;     int a[5];     a[0] = r1, a[1] = r2, a[2] = r3, a[3] = r4, a[4] = r5;     int h = col[x];     for (int i = 0; i < v[h].size(); i++) {       int b[5] = {0};       int now = v[h][i];       for (int j = 0; j < n; j++) b[j] = a[j];       bool flag = true;       for (int j = 0; j < n; j++) {         if ((mask & (1 << j)) == 0 && now & (1 << j)) b[j]--;         if (b[j] < 0) {           flag = false;           break;         }       }       if (flag) {         if (solve(x + 1, b[0], b[1], b[2], b[3], b[4], now)) {           ans.push_back(now);           return true;         }       }     }     return false;   }   int main() {     scanf( %d , &n);     ;     scanf( %d , &m);     ;     for (int i = 0; i < n; i++) scanf( %d , &row[i]);     ;     for (int i = 0; i < m; i++) scanf( %d , &col[i]);     ;     for (int i = 0; i < 1 << n; i++) {       int c = 0, pre = 0;       for (int j = 0; j < n; j++) {         if (i & (1 << j) && !pre) {           pre = 1;           c++;         } else           pre = bool(i & (1 << j));       }       v[c].push_back(i);     }     solve(0, row[0], row[1], row[2], row[3], row[4], 0);     reverse(ans.begin(), ans.end());     for (int i = 0; i < n; i++) {       for (int j = 0; j < ans.size(); j++) {         if (ans[j] & (1 << i))           cout <<  * ;         else           cout <<  . ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   const unsigned long long base = 2333;   const int maxn = 1e2 + 55;   const int maxm = 1e4 + 50;   const int maxv = 1e6 + 5;   const int mod = 51123987;   long long Cal(string s, long long p) {     long long len = s.size();     long long sum = 0;     for (long long i = 0; i < len; i++) {       sum = (sum * 10 + s[i] -  0 ) % p;     }     return sum;   }   long long phi(long long n) {     long long res = n, m = n;     for (long long i = 2; i <= sqrt(m); i++) {       if (m % i == 0) res = res / i * (i - 1);       while (m % i == 0) m /= i;     }     if (m > 1) res = res / m * (m - 1);     return res;   }   long long Quick_pow(long long x, long long y, long long p) {     long long ans = 1, res = x;     while (y) {       if (y & 1) ans = ans * res % p;       res = res * res % p;       y >>= 1;     }     return ans;   }   int main() {     string b, n;     long long c;     cin >> b >> n >> c;     long long ph = phi(c);     long long bb = Cal(b, c);     long long nn = Cal(n, ph);     if (n.size() < 10) {       long long z = 0;       for (long long i = 0; i < n.size(); i++) z = (z * 10 + n[i] -  0 );       long long ans = ((bb - 1 + c) % c) * Quick_pow(bb, z - 1, c) % c;       if (ans == 0) ans = c;       printf( %lld n , ans);       return 0;     }     nn = (nn - 1 + ph) % ph + ph;     long long ans = ((bb - 1 + c) % c) * Quick_pow(bb, nn, c) % c;     if (ans == 0) ans = c;     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, m, x, y;       cin >> n >> m >> x >> y;       char a[n][m];       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cin >> a[i][j];         }       }       int county = 0, count = 0;       if (2 * x > y) {         for (int i = 0; i < n; i++) {           for (int j = 0; j < m; j++) {             if (a[i][j] ==  . ) count++;             if (j != m - 1) {               if (a[i][j] == a[i][j + 1] && a[i][j] ==  . ) {                 county++;                 j++;               }             }           }         }         cout << (county * y) + (count - county) * x;       } else {         for (int i = 0; i < n; i++) {           for (int j = 0; j < m; j++) {             if (a[i][j] ==  . ) count++;           }         }         cout << count * x;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> a[110];   int n, k;   vector<int> ans;   void solve() {     cin >> n >> k;     for (int i = (1); i <= (n); ++i) {       int x;       cin >> x;       a[i].first = x;       a[i].second = i;     }     sort(a + 1, a + 1 + n);     int s = 0;     for (int i = 1; i <= n; ++i) {       s += a[i].first;       if (s <= k)         ans.push_back(a[i].second);       else         break;     }     sort(ans.begin(), ans.end());     cout << ans.size() << endl;     {       for (int i = (0); i < (ans.size()); ++i) cout << ans[i] <<    ;       cout << endl;     };   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ii = pair<int, int>;   using i3 = pair<int, ii>;   using li = pair<ll, int>;   using lii = pair<ll, ii>;   using vi = vector<int>;   using vl = vector<ll>;   using vli = vector<li>;   using vii = vector<ii>;   using vi3 = vector<i3>;   using vlii = vector<lii>;   const int N = 1e5 + 5;   const int INF = 1e9 + 7;   const double eps = 1e-12, PI = acos(-1);   int n, m;   char s[N];   int mem[N][5];   int dp(int i, int rem) {     if (i == n) {       return rem == 0 ? 0 : INF;     }     int &ret = mem[i][rem];     if (ret != -1) return ret;     return ret = min(1 + dp(i + 1, rem),                      dp(i + 1, (rem * 10 + int(s[i] -  0 )) % 3));   }   vi ans;   void path(int i, int rem) {     if (i == n) return;     if (dp(i + 1, (rem * 10 + int(s[i] -  0 )) % 3) == dp(i, rem)) {       ans.push_back(s[i] -  0 );       path(i + 1, (rem * 10 + int(s[i] -  0 )) % 3);       return;     }     path(i + 1, rem);   }   void solve(int cs) {     scanf( %s , s);     n = strlen(s);     int mn = INF, idx = INF;     bool isZero = 0;     memset(mem, -1, sizeof mem);     for (int i = 0; s[i]; i++) {       if (s[i] ==  0 ) {         isZero = 1;         continue;       }       int curr = dp(i + 1, int(s[i] -  0 ) % 3) + i;       if (curr < mn) {         mn = curr;         idx = i;       }     }     if (idx == INF) {       printf( %d n , isZero ? 0 : -1);       return;     }     path(idx + 1, int(s[idx] -  0 ) % 3);     printf( %c , s[idx]);     for (int A : ans) printf( %d , A);     printf(  n );   }   int main() {     int t = 1;     for (int cs = 1; t--; cs++) {       solve(cs);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void seive(bool a[], long long int n) {     for (int i = 2; i * i <= n; i++) {       if (a[i] == true) {         for (int j = i * i; j <= n; j += i) a[j] = false;       }     }   }   int np(bool a[], long long int ppr, long long int n) {     ppr++;     while (ppr <= n) {       if (a[ppr] == true) return ppr;       ppr++;     }     return -1;   }   int main() {     long long int n, i, n_1 = 0, n_2 = 0, sum = 0, s;     int t;     cin >> n;     for (i = 0; i < n; i++) {       cin >> t;       if (t == 1)         n_1++;       else         n_2++;       sum += t;     }     bool a[sum + 1];     memset(a, true, sizeof(a));     seive(a, sum);     s = 0;     a[1] = false;     long long int ppr = 0, pr;     while (s <= sum && pr != -1) {       pr = np(a, ppr, sum);       if (pr - ppr >= 2 && pr != -1) {         s = pr;         int m = min(n_2, (pr - ppr) / 2);         n_2 -= m;         for (i = 0; i < m; i++) cout << 2 <<    ;         m = min(n_1, 2 * ((pr - ppr) / 2 - m));         n_1 -= m;         for (i = 0; i < m; i++) cout << 1 <<    ;         ppr = pr;       } else if (pr != -1) {         n_1--;         if (n_1 < 0) {           for (i = 0; i < n_1; i++) cout << 1 <<    ;           for (i = 0; i < n_2; i++) cout << 2 <<    ;           pr = -1;         } else {           cout << 1 <<    ;         }         ppr = pr;       } else {         for (i = 0; i < n_1; i++) cout << 1 <<    ;         for (i = 0; i < n_2; i++) cout << 2 <<    ;       }       if (n_1 <= 0 && n_2 <= 0) break;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, z, p, i, j, k, ans, t, a[105], b[105], d[105], f[105][10005];   bool cmp(int x, int y) { return x > y; }   int main() {     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]), z += a[i];     for (i = 1; i <= n; i++) scanf( %d , &b[i]), d[i] = b[i];     sort(d + 1, d + n + 1, cmp);     memset(f, 0x80, sizeof(f));     f[0][0] = 0;     for (i = 1; i <= n; i++) {       p += d[i];       if (p >= z) {         ans = i;         break;       }     }     for (i = 1; i <= n; i++)       for (j = p; j >= b[i]; j--)         for (k = 1; k <= ans; k++)           f[k][j] = max(f[k][j], f[k - 1][j - b[i]] + a[i]);     for (i = z; i <= p; i++) t = max(t, f[ans][i]);     printf( %d %d , ans, z - t);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int m[100002];   int main() {     memset(m, 255, sizeof m);     int runs;     scanf( %d , &runs);     for (int j = 0; j < runs; j++) {       int x, y, ret = 0;       scanf( %d%d , &x, &y);       for (int i = 1; i * i <= x; i++)         if (x % i == 0) {           if (m[i] < j - y) ret++;           if (i * i < x && m[x / i] < j - y) ret++;           m[i] = j;           m[x / i] = j;         }       printf( %d n , ret);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N_MAX = 1e5;   bool in[N_MAX + 1];   int t, n, a[N_MAX], m[N_MAX], pr[N_MAX + 1];   vector<int> inc;   vector<vector<int>> sol;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> t;     while (t--) {       cin >> n;       for (int i = 0; i < n; i++) cin >> a[i];       int k = 1, rem = n;       while ((k + 1) * (k + 2) <= 2 * n) k++;       for (int i = 1; i <= n; i++) in[i] = true;       sol.clear();       while (0 < rem) {         inc.clear();         for (int i = 0; i < n; i++) {           if (!in[a[i]]) continue;           if (inc.empty() || a[i] > inc.back()) {             pr[a[i]] = (inc.empty() ? -1 : inc.back());             inc.push_back(a[i]);           } else {             auto it = upper_bound(inc.begin(), inc.end(), a[i]);             *it = a[i];             pr[a[i]] = (it == inc.begin() ? -1 : *(it - 1));           }         }         if (inc.size() <= k) {           int first = sol.size();           sol.push_back(vector<int>());           m[first] = n + 1;           for (int i = 0; i < n; i++) {             if (!in[a[i]]) continue;             if (m[(int)sol.size() - 1] < a[i]) {               m[sol.size()] = a[i];               sol.push_back(vector<int>());               sol.back().push_back(a[i]);               continue;             }             int l = first, r = (int)sol.size() - 1;             while (l < r) {               int mid = (l + r) / 2;               if (m[mid] > a[i])                 r = mid;               else                 l = mid + 1;             }             sol[l].push_back(a[i]);             m[l] = a[i];           }           break;         }         stack<int> st;         int act = inc.back();         while (act != -1) {           st.push(act);           act = pr[act];         }         sol.push_back(vector<int>());         while (!st.empty()) {           sol.back().push_back(st.top());           st.pop();         }         for (int i : sol.back()) in[i] = false;         rem -= inc.size();         while (2 * rem < k * (k + 1)) k--;       }       cout << sol.size() <<   n ;       for (int i = 0; i < sol.size(); i++) {         cout << sol[i].size();         for (int s : sol[i]) cout <<     << s;         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, i1;   long long cont = 0;   int main() {     cin >> n;     for (long long i = 0; i < n; ++i) {       long long x, y;       cin >> x >> y;       if (x - y != 1) {         cout <<  NO  << endl;       } else {         long long p = x + y, s = 0;         for (long long j = 2; j <= sqrt(p); ++j) {           if (p % j == 0) {             s = 1;             break;           }         }         if (s == 0) {           cout <<  YES  << endl;         } else {           cout <<  NO  << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   bool Max(T1& a, T2 b) {     return a < b ? a = b, 1 : 0;   }   template <typename T1, typename T2>   bool Min(T1& a, T2 b) {     return a > b ? a = b, 1 : 0;   }   const int N = 200002, inf = 1e9 + 1;   int s[N], g[N], ans[N];   bool vi[N];   vector<int> cap[N * 10];   void solve() {     int n, m;     cin >> n;     int mncap = N * 10;     for (int i = 0; i < (n); ++i) {       cin >> s[i] >> g[i];       g[i] += s[i];       ans[i] = g[i];     }     int mn = inf;     for (int i = 0; i < (n); ++i) {       mn++;       Min(mn, g[i]);       Min(ans[i], mn);     }     mn = inf;     for (int i = n - 1; i >= (0); --i) {       mn++;       Min(mn, g[i]);       Min(ans[i], mn);     }     long long ret = 0;     for (int i = 0; i < (n); ++i) {       if (ans[i] < s[i]) {         cout << -1 <<   n ;         return;       }       ret += ans[i] - s[i];     }     cout << ret <<   n ;     for (int i = 0; i < (n); ++i) cout << ans[i] <<    ;     cout <<   n ;   }   void init() {}   int main(void) {     ios::sync_with_stdio(false);     cin.tie(0);     cout.setf(ios::fixed);     cout.precision(20);     init();     int TC = 1;     for (int TI = 1; TI <= (TC); ++TI) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Mod = 1e9 + 7;   const int N = 1000005;   long long frac[N + 5], inv[N + 5];   int n, a[N], y[N], Max;   long long ans;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     return x * f;   }   long long power(long long a, long long b) {     long long res = 1;     for (; b; b >>= 1) {       if (b & 1) res = res * a % Mod;       a = a * a % Mod;     }     return res;   }   long long C(int m, int n) { return frac[n] * inv[m] % Mod * inv[n - m] % Mod; }   int main() {     n = read();     frac[1] = 1;     frac[0] = 1;     for (int i = 2; i <= n + 2; i++) frac[i] = frac[i - 1] * i % Mod;     inv[n + 2] = power(frac[n + 2], Mod - 2);     for (int i = n + 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % Mod;     inv[0] = 1;     for (int i = 1; i <= n; i++) a[i] = read();     sort(a + 1, a + n + 1);     a[0] = -1;     for (int i = 1; i <= n; i++) {       Max = max(Max, a[i]);       if (a[i] == a[i - 1]) {         y[i] = y[i - 1];         continue;       }       for (int j = i; j >= 0; j--) {         if (a[j] != a[i]) {           y[i] = n - j;           break;         }       }       --y[i];     }     for (int i = 1; i <= n; i++) {       if (a[i] == Max) continue;       ans = (ans + (a[i] * frac[y[i]] % Mod * frac[n - y[i] - 1] % Mod *                     C(y[i] + 1, n) % Mod)) %             Mod;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   template <typename T>   using v = vector<T>;   const long long mod1 = 1e9 + 7;   const long long mod2 = 36028797018963913;   const long long inf = 2e18;   void read() {}   void print() {}   void debug() { cerr << endl; }   long long gcd(long long a, long long b) {     if (a > b) swap(a, b);     if (a) return gcd(a, b % a);     return b;   }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   struct vt {     long long x, y;     vt operator-() { return vt(-this->x, -this->y); }     vt operator+(vt b) { return {this->x + b.x, this->y + b.y}; }     vt operator-(vt b) { return *this + (-b); }     vt operator*(long long b) { return vt(this->x * b, this->y * b); }     long long operator*(vt b) { return this->x * b.x + this->y * b.y; }     vt operator/(long long b) { return vt(this->x / b, this->y / b); }     long long operator%(vt b) { return this->x * b.y - b.x * this->y; }     bool operator==(vt b) { return this->x == b.x && this->y == b.y; }     bool operator!=(vt b) { return !((*this) == b); }     bool collinear(vt b) { return (*this) % b == 0; }     bool opposite(vt b) { return this->collinear(b) && (*this) * b < 0; }     double length() { return sqrt(this->x * this->x + this->y * this->y); }     long long sqLength() { return this->x * this->x + this->y * this->y; }     vt() {}     vt(long long x, long long y) : x(x), y(y) {}     vt(long long x1, long long y1, long long x2, long long y2)         : x(x2 - x1), y(y2 - y1) {}   };   long long fact(long long a, long long mod) {     if (a == 1)       return 1;     else       return (a * fact(a - 1, mod)) % mod;   }   long long sign(long long a) {     if (a > 0) return 1;     if (a == 0) return 0;     return -1;   }   bool prime(long long a) {     if (a < 2) return 0;     if (a == 2) return 1;     for (long long b = 2; b <= ceil(sqrt(a)); b++)       if (a % b == 0) return 0;     return 1;   }   long long pow(long long a, long long b, long long mod) {     if (b == 0) return 1;     if (b == 1) return a;     long long res = pow(a, b / 2, mod);     res = (res * res) % mod;     if (b % 2) return (a * res) % mod;     return res;   }   long long hashStr(string& s) {     long long base = 26;     long long res = 0;     for (long long i = 0; i < ((long long)(s).size()); i++)       res = ((res * base) % mod2 + s[i] -  a  + 1) % mod2;     return (res + mod2) % mod2;   }   long long hashStr(long long prevHash, char curChar) {     return ((prevHash * 26) % mod2 + curChar -  a  + 1) % mod2;   }   struct node {     node *left = nullptr, *right = nullptr;     long long w, val, sz = 1;     node(long long val) : w((rand() << 16) + rand()), val(val) {}   };   long long siz(node* a) {     if (a == nullptr)       return 0;     else       return a->sz;   }   node* treap;   node* merge1(node* v1, node* v2) {     if (v1 == nullptr) return v2;     if (v2 == nullptr) return v1;     if (v1->w > v2->w) {       v1->right = merge1(v1->right, v2);       v1->sz = siz(v1->left) + siz(v1->right) + 1;       return v1;     } else {       v2->left = merge1(v1, v2->left);       v2->sz = siz(v2->left) + siz(v2->right) + 1;       return v2;     }   }   pair<node*, node*> split(node* w, long long k) {     if (w == nullptr) return {nullptr, nullptr};     if (siz(w->left) >= k) {       auto tmp = split(w->left, k);       w->left = tmp.second;       w->sz = siz(w->left) + siz(w->right) + 1;       return {tmp.first, w};     } else {       auto tmp = split(w->right, k - siz(w->left) - 1);       w->right = tmp.first;       w->sz = siz(w->left) + siz(w->right) + 1;       return {w, tmp.second};     }   }   ifstream fin( input.txt );   ofstream fout( output.txt );   template <typename Arg, typename... Args>   void read(Arg& arg, Args&... args) {     cin >> (arg);     read(args...);   }   template <typename Arg, typename... Args>   void print(Arg arg, Args... args) {     cout << (arg) <<    ;     print(args...);   }   template <typename Arg, typename... Args>   void debug(Arg arg, Args... args) {     cerr << (arg) <<    ;     debug(args...);   }   void prnt(node* a) {     if (a == nullptr) return;     prnt(a->left);     print(a->val);     prnt(a->right);   }   long long getInt() {     long long a;     cin >> a;     return a;   }   void run() {     long long w, d;     read(w, d);     long long a[w + 1];     for (long long i = (0); i < (w - 1); i++) cin >> a[i + 1];     a[w] = 1e9;     long long dp[w + 1];     dp[0] = 1e9;     a[0] = 1e9;     long long l = 0;     set<long long> p;     p.insert(0);     for (long long i = (1); i < (w + 1); i++) {       dp[i] = 0;       l = max(i - d, l);       while ((*p.begin()) < l) p.erase(*p.begin());       while (!p.empty()) {         long long cur = *p.begin();         if (dp[cur] <= a[i] - dp[i]) {           dp[i] += dp[cur];           dp[cur] = 0;           p.erase(cur);         } else {           long long mn = a[i] - dp[i];           dp[i] = a[i];           dp[cur] -= mn;           break;         }       }       if (dp[i]) p.insert(i);     }     cout << dp[w];   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int blacks(int *pos, int n);   int whites(int *pos, int n);   int main() {     int n;     cin >> n;     int board[100];     for (int i = 0; i < n; i++) {       board[i] = 0;     }     for (int i = 0; i < n / 2; i++) {       int temp;       cin >> temp;       board[temp - 1] = 1;     }     cout << min(blacks(board, n), whites(board, n));   }   int blacks(int *pos, int n) {     int i = 0;     int label = 0;     int result = 0;     while (i < n) {       if (pos[i] == 1) {         result += abs(i - label);         label += 2;       }       i++;     }     return result;   }   int whites(int *pos, int n) {     int i = 0;     int label = 1;     int result = 0;     while (i < n) {       if (pos[i] == 1) {         result += abs(i - label);         label += 2;       }       i++;     }     return result;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int i, n, f = 0;       cin >> n;       vector<int> v(n);       vector<int> use(n + 1, 0);       for (i = 0; i < n; i++) {         cin >> v[i];         if (v[i] < (i + 1)) {           f = 1;         }       }       if (f) {         cout <<  -1  << endl;         continue;       }       vector<int> a(n, 0);       for (i = 0; i < n; i++) {         if (!use[v[i]]) {           a[i] = v[i];           use[v[i]] = 1;         }       }       queue<int> q;       for (i = 1; i <= n; i++) {         if (!use[i]) {           q.push(i);         }       }       for (i = 0; i < n; i++) {         if (a[i] != 0) {           cout << a[i] <<    ;         } else {           cout << q.front() <<    ;           q.pop();         }       }       cout << endl;     }   }