#include <bits/stdc++.h>   using namespace std;   int x, y, q;   void exgcd(int a, int b) {     int temp;     if (b == 0) {       x = 1;       y = 0;       q = a;     } else {       exgcd(b, a % b);       temp = x;       x = y;       y = temp - a / b * y;     }   }   int c3, c4, c5;   int k3, k4, k5;   int f, x0, Why_Cant_I_Use_y0_In_Eclipse;   int up, down;   void init() {     up = 0xfffffff;     down = -0xfffffff;     down = max(down, (-x * f) / x0);     if (x * f + x0 * down < 0) down++;     up = min(up, (k4 - x * f) / x0);     if (x * f + x0 * up > k4) up--;     up = min(up, (y * f - k4) / Why_Cant_I_Use_y0_In_Eclipse);     if (y * f - Why_Cant_I_Use_y0_In_Eclipse * up < k4) up--;   }   int o3, o4, o5;   int ans;   void get_o(int t) {     o3 = x * f + x0 * t;     o4 = k4;     o5 = y * f - Why_Cant_I_Use_y0_In_Eclipse * t;   }   void solve(int s) {     exgcd(c3, c5);     if (s % q != 0) return;     f = s / q;     x0 = c5 / q;     Why_Cant_I_Use_y0_In_Eclipse = c3 / q;     {       init();       down = max(down, (c4 * k4 - c3 * x * f) / (c3 * x0));       if (c3 * x * f + c3 * x0 * down < c4 * k4) down++;       up = min(up, (c5 * y * f - c4 * k4) / (c5 * Why_Cant_I_Use_y0_In_Eclipse));       if (c5 * y * f - c4 * k4 < c5 * Why_Cant_I_Use_y0_In_Eclipse * up) up--;       if (down <= up) {         int judge = s - 2 * c4 * k4;         if (judge < ans) {           ans = judge;           get_o(down);         }       }     }     {       init();       up = min(up, (c4 * k4 - c3 * x * f) / (c3 * x0));       if (c4 * k4 - c3 * x * f < c3 * x0 * up) up--;       down =           max(down, (c5 * y * f - c4 * k4) / (c5 * Why_Cant_I_Use_y0_In_Eclipse));       if (c5 * y * f - c5 * Why_Cant_I_Use_y0_In_Eclipse * down > c4 * k4) down++;       if (down <= up) {         int judge = 2 * c4 * k4 - s;         if (judge < ans) {           ans = judge;           get_o(up);         }       }     }     {       init();       down = max(down, (c4 * k4 - c3 * x * f) / (c3 * x0));       if (c3 * x * f + c3 * x0 * down < c4 * k4) down++;       down =           max(down, (c5 * y * f - c4 * k4) / (c5 * Why_Cant_I_Use_y0_In_Eclipse));       if (c5 * y * f - c5 * Why_Cant_I_Use_y0_In_Eclipse * down > c4 * k4) down++;       if (down <= up) {         int judge = c3 * (x * f + x0 * down) -                     c5 * (y * f - Why_Cant_I_Use_y0_In_Eclipse * down);         if (judge < ans) {           ans = judge;           get_o(down);         }       }     }     {       init();       up = min(up, (c4 * k4 - c3 * x * f) / (c3 * x0));       if (c4 * k4 - c3 * x * f < c3 * x0 * up) up--;       up = min(up, (c5 * y * f - c4 * k4) / (c5 * Why_Cant_I_Use_y0_In_Eclipse));       if (c5 * y * f - c4 * k4 < c5 * Why_Cant_I_Use_y0_In_Eclipse * up) up--;       if (down <= up) {         int judge = -c3 * (x * f + x0 * up) +                     c5 * (y * f - Why_Cant_I_Use_y0_In_Eclipse * up);         if (judge < ans) {           ans = judge;           get_o(up);         }       }     }   }   int main() {     int n, s;     scanf( %d %d , &n, &s);     c3 = c4 = c5 = 0;     while (n--) {       int f;       scanf( %d , &f);       if (f == 3) c3++;       if (f == 4) c4++;       if (f == 5) c5++;     }     ans = 0xfffffff;     for (int i = 0; i <= s; i++) {       if (c4 * i > s) break;       k4 = i;       solve(s - c4 * k4);     }     if (ans == 0xfffffff)       printf( -1 n );     else {       printf( %d %d %d n , o3, o4, o5);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long int, long long int> mp;   int main() {     int n, c = 0, x = 0, a[200], i, j;     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];       c += a[i];     }     if (n == 1) x = 1;     if (n == 2 && a[0] == a[1]) x = 1;     if (x == 1)       cout << -1;     else {       cout << 1 << endl;       for (i = 0; i < n; i++) {         if (2 * a[i] != c) {           cout << i + 1;           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mxN = 5e4 + 7;   const int inf = 1e5 + 7;   const long long mod = 1e5 + 7;   const long long infll = 1e18 + 7;   long long a[mxN], b[mxN];   map<long long, bool> mp;   int n;   bool check = false;   bool ok(long long x) {     for (int i = 1; i <= n; i++) a[i] = b[i];     mp.clear();     for (int i = 1; i <= n; i++) {       if (a[i] <= x) {         if (mp[a[i]] == true) {           while (mp[a[i]] == true && a[i] != 1) {             a[i] /= 2;           }           if (mp[a[i]] == true) return false;         }         mp[a[i]] = true;       } else {         while ((a[i] > x || mp[a[i]] == true) && a[i] != 1) {           a[i] /= 2;         }         if (mp[a[i]] == true) return false;         mp[a[i]] = true;       }       if (check == true) cout << a[i] <<    ;     }     return true;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n; i++) b[i] = a[i];     long long l = 0;     long long r = infll;     while (l + 1 < r) {       long long m = (l + r) / 2;       if (ok(m))         r = m;       else         l = m;     }     check = true;     ok(r);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return x * f;   }   int n, a[300100], q;   struct node {     int x;     int val;   };   bool operator<(node a, node b) { return a.val < b.val; }   priority_queue<node> heap;   set<int> tree;   int main() {     n = read(), q = read();     for (int i = 1; i <= n; ++i) {       a[i] = read();       tree.insert(a[i]);     }     sort(a + 1, a + 1 + n);     for (int i = 1; i < n; ++i) {       heap.push({a[i], a[i + 1] - a[i]});     }     if (tree.size() <= 2) {       puts( 0 );     } else {       cout << (*(--tree.end())) - (*tree.begin()) - heap.top().val << endl;     }     for (int i = 1; i <= q; ++i) {       int t = read(), p = read();       if (t == 0) {         set<int>::iterator it;         it = tree.lower_bound(p);         set<int>::iterator lst = it;         lst--;         if (it != tree.end()) {           set<int>::iterator nxt = it;           nxt++;           heap.push({(*lst), (*nxt) - (*lst)});           tree.erase(it);         }       } else {         tree.insert(p);         set<int>::iterator it;         it = tree.lower_bound(p);         set<int>::iterator nxt = it;         nxt++;         set<int>::iterator lst = it;         lst--;         heap.push({(*lst), (*it) - (*lst)});         heap.push({(*it), (*nxt) - (*it)});       }       bool flag = false;       do {         flag = false;         int x = heap.top().x, val = heap.top().val;         set<int>::iterator it;         it = tree.find(x);         set<int>::iterator nxt = it;         if (it != tree.end()) nxt++;         if (it == tree.end() || nxt == tree.end()) {           heap.pop();           flag = true;         } else {           if (val != (*nxt) - (*it)) {             heap.pop();             flag = true;           }         }       } while (flag && heap.size());       if (tree.size() <= 2) {         puts( 0 );       } else {         cout << (*(--tree.end())) - (*tree.begin()) - heap.top().val;         puts(  );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000051;   long long n;   string c;   struct btree {     int chnum;     int child[2];     int num;     int typ;     int id;   } tre[N];   int dfx[N][2];   int tot = 0;   int has[N];   void predfs(int x) {     if (tre[x].typ == 0) return;     if (tre[x].typ == 1) {       predfs(tre[x].child[0]);       predfs(tre[x].child[1]);       tre[x].num = tre[tre[x].child[0]].num & tre[tre[x].child[1]].num;     }     if (tre[x].typ == 2) {       predfs(tre[x].child[0]);       predfs(tre[x].child[1]);       tre[x].num = tre[tre[x].child[0]].num | tre[tre[x].child[1]].num;     }     if (tre[x].typ == 3) {       predfs(tre[x].child[0]);       predfs(tre[x].child[1]);       tre[x].num = tre[tre[x].child[0]].num ^ tre[tre[x].child[1]].num;     }     if (tre[x].typ == 4) {       predfs(tre[x].child[0]);       tre[x].num = !tre[tre[x].child[0]].num;     }   }   void dfs(int x) {     if (tre[x].typ == 0) has[tre[x].id] = dfx[tot][tre[x].num ^ 1];     if (tre[x].typ == 1) {       tot++;       dfx[tot][0] = dfx[tot - 1][0 & tre[tre[x].child[1]].num];       dfx[tot][1] = dfx[tot - 1][1 & tre[tre[x].child[1]].num];       dfs(tre[x].child[0]);       dfx[tot][0] = dfx[tot - 1][0 & tre[tre[x].child[0]].num];       dfx[tot][1] = dfx[tot - 1][1 & tre[tre[x].child[0]].num];       dfs(tre[x].child[1]);       tot--;     }     if (tre[x].typ == 2) {       tot++;       dfx[tot][0] = dfx[tot - 1][0 | tre[tre[x].child[1]].num];       dfx[tot][1] = dfx[tot - 1][1 | tre[tre[x].child[1]].num];       dfs(tre[x].child[0]);       dfx[tot][0] = dfx[tot - 1][0 | tre[tre[x].child[0]].num];       dfx[tot][1] = dfx[tot - 1][1 | tre[tre[x].child[0]].num];       dfs(tre[x].child[1]);       tot--;     }     if (tre[x].typ == 3) {       tot++;       dfx[tot][0] = dfx[tot - 1][0 ^ tre[tre[x].child[1]].num];       dfx[tot][1] = dfx[tot - 1][1 ^ tre[tre[x].child[1]].num];       dfs(tre[x].child[0]);       dfx[tot][0] = dfx[tot - 1][0 ^ tre[tre[x].child[0]].num];       dfx[tot][1] = dfx[tot - 1][1 ^ tre[tre[x].child[0]].num];       dfs(tre[x].child[1]);       tot--;     }     if (tre[x].typ == 4) {       tot++;       dfx[tot][0] = dfx[tot - 1][1];       dfx[tot][1] = dfx[tot - 1][0];       dfs(tre[x].child[0]);       tot--;     }   }   void init() {     cin >> n;     for (int i = 1; i <= n; i++) has[i] = -1;     for (int i = 1; i <= n; i++) {       cin >> c;       tre[i].id = i;       if (c ==  IN ) {         tre[i].chnum = 0;         tre[i].typ = 0;         scanf( %d , &tre[i].num);       }       if (c ==  AND ) {         tre[i].chnum = 2;         tre[i].typ = 1;         tre[i].num = -1;         scanf( %d%d , &tre[i].child[0], &tre[i].child[1]);       }       if (c ==  OR ) {         tre[i].chnum = 2;         tre[i].typ = 2;         tre[i].num = -1;         scanf( %d%d , &tre[i].child[0], &tre[i].child[1]);       }       if (c ==  XOR ) {         tre[i].chnum = 2;         tre[i].typ = 3;         tre[i].num = -1;         scanf( %d%d , &tre[i].child[0], &tre[i].child[1]);       }       if (c ==  NOT ) {         tre[i].chnum = 1;         tre[i].typ = 4;         tre[i].num = -1;         scanf( %d , &tre[i].child[0]);       }     }     dfx[0][0] = 0;     dfx[0][1] = 1;     predfs(1);     dfs(1);     for (int i = 1; i <= n; i++)       if (has[i] != -1) printf( %d , has[i]);     printf(  n );   }   int main() {     int T;     T = 1;     while (T--) init();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2e5 + 15;   inline long long read() {     long long x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 1) + (x << 3) + c -  0 ;       c = getchar();     }     return x * f;   }   long long T, n, tot, last, len[maxn], size[maxn], link[maxn], nxt[maxn][30];   inline long long extend(long long last, long long x) {     long long now = ++tot, pos = last, tmp, cln;     len[now] = len[pos] + 1;     size[now]++;     while (pos != 0 && nxt[pos][x] == 0) nxt[pos][x] = now, pos = link[pos];     if (!pos) {       link[now] = 1;       return now;     }     tmp = nxt[pos][x];     if (len[tmp] == len[pos] + 1) {       link[now] = tmp;       return now;     }     cln = ++tot;     len[cln] = len[pos] + 1;     for (long long i = 1; i <= 26; i++) nxt[cln][i] = nxt[tmp][i];     link[cln] = link[tmp];     link[tmp] = link[now] = cln;     while (pos != 0 && nxt[pos][x] == tmp) nxt[pos][x] = cln, pos = link[pos];     return now;   }   string s;   long long beg[maxn], nex[maxn], to[maxn], e;   inline void add(long long x, long long y) {     e++;     nex[e] = beg[x];     beg[x] = e;     to[e] = y;   }   long long val[maxn], k;   inline void dfs(long long x) {     for (long long i = beg[x]; i; i = nex[i]) dfs(to[i]), size[x] += size[to[i]];     val[size[x]] += (len[x] - len[link[x]]);   }   inline void pre() {     for (long long i = 0; i <= tot; i++) {       val[i] = beg[i] = len[i] = size[i] = link[i] = 0;       for (long long j = 1; j <= 26; j++) nxt[i][j] = 0;     }     e = tot = 0;   }   signed main() {     T = read();     while (T--) {       pre();       cin >> s;       n = s.size();       s =     + s;       last = ++tot;       for (long long i = 1; i <= n; i++) last = extend(last, s[i] - 96);       for (long long i = 2; i <= tot; i++) add(link[i], i);       dfs(1);       long long ans = 0;       for (long long i = n; i >= 1; i--) ans += val[i] * i * i;       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct poi {     int x, y;     poi operator-(poi p) { return (poi){x - p.x, y - p.y}; }     long long operator*(poi p) { return 1ll * x * p.y - 1ll * y * p.x; }   } A[205], a[205];   bool cmp(poi x, poi y) { return x * y > 0; }   bool turn(int x, int y, int z) { return (a[y] - a[x]) * (a[z] - a[y]) > 0; }   int b[205 << 1], v[205 * 205], f[205 * 205], tot;   int q[205][205], l[205], r[205];   int n, nn, m;   long long d[205][205][55], w[55], ans;   void add(int x, int y) { v[++tot] = y, f[tot] = b[x], b[x] = tot; }   void link(int x, int y) {     for (; l[x] < r[x] && turn(q[x][l[x]], x, y); ++l[x]) link(q[x][l[x]], y);     add(x + nn, y);     add(y, x);     q[y][r[y]++] = x;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %d%d , &A[i].x, &A[i].y);     ans = 0;     for (int i = 1; i <= n; ++i) {       nn = 0;       for (int j = 1; j <= n; ++j)         if (i != j && A[i].x <= A[j].x) a[++nn] = A[j] - A[i];       sort(a + 1, a + nn + 1, cmp);       memset(b, 0, sizeof b);       tot = 0;       memset(l, 0, sizeof l);       memset(r, 0, sizeof r);       for (int j = 1; j <= nn - 1; ++j) link(j, j + 1);       for (int j = 1; j <= nn; ++j)         for (int k = b[j + nn]; k; k = f[k])           for (int p = 0; p <= m; ++p) d[j][v[k]][p] = 0;       for (int j = nn; j; --j) {         memset(w, 0, sizeof w);         int k = b[j + nn];         for (int p = b[j]; p; p = f[p]) {           d[v[p]][j][0] = 0;           d[v[p]][j][1] = a[v[p]] * a[j];           for (int u = 2; u <= m; ++u)             d[v[p]][j][u] = w[u - 1] > 0 ? w[u - 1] + a[v[p]] * a[j] : 0;           for (; k && turn(v[p], j, v[k]); k = f[k])             for (int u = 1; u <= m; ++u)               if (d[j][v[k]][u - 1] > w[u - 1])                 w[u - 1] = d[j][v[k]][u - 1],                       d[v[p]][j][u] = w[u - 1] + a[v[p]] * a[j];         }       }       for (int j = 1; j <= nn; ++j)         for (int k = b[j + nn]; k; k = f[k]) ans = max(ans, d[j][v[k]][m - 2]);     }     printf( %lld , ans >> 1);     puts(ans & 1 ?  .50  :  .00 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int a, b, c, d;       cin >> a >> b >> c >> d;       cout << max(a + b, c + d) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, sz;   int par[4000005];   int findpar(int x) {     if (par[x] == x) return par[x];     par[x] = findpar(par[x]);     return par[x];   }   int cr, cc;   int pr, pc;   void decode(int x) {     if (x < m) {       cr = 0;       cc = x;     } else if (x < m + n - 1) {       cr = x - m + 1;       cc = m - 1;     } else if (x < 2 * m + n - 2) {       cr = n - 1;       cc = m - 1 - (x - (m + n - 2));     } else {       cr = n - 1 - (x - (2 * m + n - 3));       cc = 0;     }   }   int encode(int r, int c) {     if (r == 0) return c;     if (c == m - 1) return m - 1 + r;     if (r == n - 1) return m + n - 2 + m - 1 - c;     if (c == 0) return 2 * m + n - 3 + n - 1 - r;   }   bool inrange(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; }   void go(int r, int c, int dr, int dc) {     int x;     if (dr == -1 && dc == -1) x = min(r, c);     if (dr == -1 && dc == 1) x = min(r, m - 1 - c);     if (dr == 1 && dc == -1) x = min(n - 1 - r, c);     if (dr == 1 && dc == 1) x = min(n - 1 - r, m - 1 - c);     pr = r + x * dr;     pc = c + x * dc;   }   int main() {     cin >> n >> m;     sz = 2 * (n + m) - 4;     for (int i = 0; i < sz; ++i) par[i] = i;     for (int i = 0; i < sz; ++i) {       decode(i);       for (int dr = -1; dr <= 1; dr += 2)         for (int dc = -1; dc <= 1; dc += 2) {           if (inrange(cr + dr, cc + dc)) {             go(cr, cc, dr, dc);             int x = encode(pr, pc);             int px = findpar(x);             int pi = findpar(i);             if (px != pi) {               par[max(pi, px)] = min(pi, px);             }           }         }     }     set<int> st;     for (int i = 0; i < sz; ++i) st.insert(findpar(i));     cout << st.size() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long r;     cin >> r;     if ((r - 1) % 2 == 0 && r > 3)       cout << 1 <<     << (r - 3) / 2;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int const nmax = 3000;   int const modulo = 1000000007;   char v[1 + nmax][1 + nmax];   int dp[1 + nmax][1 + nmax];   int solve(int x, int y, int x2, int y2) {     for (int i = 1; i <= x2; i++)       for (int j = 1; j <= y2; j++) dp[i][j] = 0;     dp[x][y] = 1;     for (int i = 1; i <= x2; i++)       for (int j = 1; j <= y2; j++) {         dp[i][j] += dp[i - 1][j] + dp[i][j - 1];         if (modulo <= dp[i][j]) dp[i][j] -= modulo;         if (v[i][j] ==  # ) dp[i][j] = 0;       }     return dp[x2][y2];   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) cin >> v[i][j];     int result = 1LL * solve(1, 2, n - 1, m) * solve(2, 1, n, m - 1) % modulo;     int result2 = 1LL * solve(1, 2, n, m - 1) * solve(2, 1, n - 1, m) % modulo;     cout << (modulo + result - result2) % modulo;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int fastpow(int a, int b) {     int ans = 1;     for (; b; b >>= 1, a = a * a)       if (b & 1) ans = ans * a;     return ans;   }   int num;   int main() {     int n;     scanf( %d , &n);     string a;     cin >> a;     for (int i = 0; i < a.length(); i++) n += (a[i] -  0 ) * fastpow(10, i);     printf( %d , n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     for (int z = 0; z < n; z++) {       long long a, b, c;       cin >> a >> b;       while ((c = a | a + 1) <= b) a = (a | a + 1);       cout << a << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   const double PI = atan(1.0) * 4;   const int di[4] = {-1, 0, 1, 0};   const int dj[4] = {0, -1, 0, 1};   const long long INF = (long long)2e18 + 50;   const int maximum = numeric_limits<int>::min();   const int minimum = numeric_limits<int>::max();   constexpr int N = 2e6, P = 1000000007;   int a[N + 1], b[N + 1];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     for (int i = 1; i <= N; ++i) {       a[i] = (2ll * a[i - 2] + a[i - 1]) % P;       if (b[i - 1] && b[i - 2]) {         a[i] = (a[i] + 1) % P;       } else {         b[i] = 1;       }     }     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       cout << 4ll * a[n] % P <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   const int N = 5050;   int a[N];   int dp[N][N];   int cnt[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cerr.setstate(ios_base::failbit);     cin >> n >> k;     for (int i = 1; i < (n + 1); i++) cin >> a[i];     sort(a + 1, a + n + 1);     int j = n;     for (int i = (n + 1) - 1; i >= 1; i--) {       while (j > 0 && abs(a[j] - a[i]) <= 5) j--;       cnt[i] = i - j;     }     for (int i = 1; i < (n + 1); i++)       for (int j = 1; j < (k + 1); j++) {         int &ans = dp[i][j];         ans = max(ans, dp[i - 1][j]);         ans = max(ans, dp[i - cnt[i]][j - 1] + cnt[i]);       }     cout << dp[n][k] <<   n ;   }


#include  bits/stdc++.h    using namespace std;   #define ll long long   #define llu unsigned long long   #define PB push_back   #define F first   #define S second   #define sz(a) ((ll)a.size())   const ll MOD = 1000000007;   const ll INF = 1000000000000000007;   #ifdef ONLINE_JUDGE   #define ck(x...)   #endif      void AC_aaega(){    ll n;    cin>>n;    vector < vector < ll > > vv(n, vector < ll >(n, 0));    vector < ll > k1(10, INF);    vector < ll > k2(10, -1);    vector < ll > q1(10, INF);    vector < ll > q2(10, -1);    for(ll i =0;i<n;i++){     string s;     cin>>s;     for(ll j= 0;j<n;j++){      vv[i][j] = s[j] -  0 ;      k1[vv[i][j]] = min(k1[vv[i][j]], j);      k2[vv[i][j]] = max(k2[vv[i][j]], j);      q1[vv[i][j]] = min(q1[vv[i][j]], i);      q2[vv[i][j]] = max(q2[vv[i][j]], i);     }    }     ck(k1, k2, q1, q2);    for(ll i= 0;i<10;i++){     ll ans =0, ff =-1, ss = -1;     for(ll j =0;j<n;j++){      for(ll k =0;k<n;k++){       if(vv[j][k] == i){        if(ff == -1){         ff = k;        }        else{         ss = k;        }       }      }      if(ss != -1 && ff != -1){       ans = max(ans, (ss - ff) * max(j, n - 1 - j));       ck(j, ss, ff);      }      if(ff != -1){       ll qw = q1[i], wq = q2[i];       if(qw != INF && wq != -1){        ans = max(ans, max(abs(n-1-ff), ff) * max(j - qw, wq - j));       }       // else if(qw != -1){       //  ans = max(ans, max(abs(n-1-ff), ff) * j-qw);       // }      }      if(ss != -1){       ll qw = q1[i], wq = q2[i];       if(qw != INF && wq != -1){        ans = max(ans, max(abs(n-1-ss), ss) * max(j - qw, wq - j));       }       // else if(qw != -1){       //  ans = max(ans, max(abs(n-1-ff), ff) * j-qw);       // }      }      ff = ss = -1;     }     ck(ans);     for(ll j =0;j<n;j++){      for(ll k =0;k<n;k++){       if(vv[k][j] == i){        if(ff == -1){         ff = k;        }        else{         ss = k;        }       }      }      if(ss != -1 && ff != -1){       ans = max(ans, (ss - ff) * max(j, n - 1 - j));      }      if(ff != -1){       ll qw = k1[i], wq = k2[i];       if(qw != INF && wq != -1){        ans = max(ans, max(abs(n-1-ff), ff) * max(j - qw, wq - j));       }       ck(ff, qw, wq);       // else if(qw != -1){       //  ans = max(ans, max(abs(n-1-ff), ff) * j-qw);       // }      }      if(ss != -1){       ll qw = k1[i], wq = k2[i];       if(qw != INF && wq != -1){        ans = max(ans, max(abs(n-1-ss), ss) * max(j - qw, wq - j));       }       ck(ss, qw, wq);       // else if(qw != -1){       //  ans = max(ans, max(abs(n-1-ff), ff) * j-qw);       // }      }      ff = ss = -1;     }     cout<<ans<<   ;    }    cout<<  n ;   }      int main(){    ios_base::sync_with_stdio(0);    cin.tie(0);    cout.tie(0);    cerr.tie(0);    #ifndef ONLINE_JUDGE      freopen( /home/theviralv/Desktop/input.txt ,  r , stdin);      freopen( /home/theviralv/Desktop/output.txt ,  w , stdout);      freopen( /home/theviralv/Desktop/err.txt ,  w , stderr);    #endif    ll testcases = 1;    cin>>testcases;    while(testcases--){     AC_aaega();    }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 0.001;   const int mod = 1000000007;   int main() {     int n, pos, l, r, ans;     scanf( %d%d%d%d , &n, &pos, &l, &r);     if (l == 1 && r == n)       ans = 0;     else if (l == 1)       ans = abs(pos - r) + 1;     else if (r == n)       ans = abs(pos - l) + 1;     else       ans = min(abs(pos - l), abs(pos - r)) + r - l + 2;     printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int q, s1, s2, s3, w, m, e;     vector<long long int> v;     cin >> q;     long long int n;     for (int i = 0; i < q; i++) {       cin >> n;       long long int a[n];       s1 = 0;       s2 = 0;       s3 = 0;       for (int j = 0; j < n; j++) {         cin >> a[j];         if (a[j] % 3 == 0) {           s3++;         } else if (a[j] % 3 == 1) {           s1++;         } else if (a[j] % 3 == 2) {           s2++;         }       }       w = (s3 + min(s1, s2));       m = s1;       e = s2;       m -= min(s1, s2);       e -= min(s1, s2);       s1 = m;       s2 = e;       w += s1 / 3;       w += s2 / 3;       v.push_back(w);     }     for (int i = 0; i < q; i++) {       cout << v[i] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 10;   const int inf = 1e9;   struct node {     int s, ind;   } f[maxn];   int sum[maxn];   int cmp(node a, node b) {     if (a.s == b.s) return a.ind < b.ind;     return a.s < b.s;   }   int main() {     int i, n, c, re = 0, value;     scanf( %d%d , &n, &c);     for (i = 1; i <= n; i++) {       scanf( %d , &f[i].s);       f[i].ind = i;       sum[i] = sum[i - 1] + (f[i].s == c);     }     sort(f + 1, f + n + 1, cmp);     for (i = 1; i <= n; i++) {       if (f[i].s == c) continue;       if (f[i].s != f[i - 1].s) {         value = 1;         re = max(re, value);         continue;       }       value -= (sum[f[i].ind - 1] - sum[f[i - 1].ind]);       if (value < 0) value = 0;       value++;       re = max(re, value);     }     printf( %d , re + sum[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void print_err() { cerr <<   n ; }   template <class T, class... Arg>   void print_err(T x, Arg &&...args) {     cerr << x <<    ;     print_err(args...);   }   template <class T>   void print_container(T &cont) {     for (auto iter : cont) {       cerr << iter <<    ;     }     cerr <<   n ;   }   int mod = 998244353;   long long ceil(long long a, long long b) { return (a + b - 1) / b; }   long long gcd(long long a, long long b) {     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   vector<int> d4x{0, 0, 1, -1};   vector<int> d4y{-1, 1, 0, 0};   vector<int> d8x{0, 0, 1, -1, 1, 1, -1, -1};   vector<int> d8y{1, -1, 0, 0, -1, 1, 1, -1};   vector<long long> B(1e5 + 1, -1), C(1e5 + 1, -1);   void solve(int test) {     ;     int n;     cin >> n;     vector<int> A(n);     for (int &a : A) {       cin >> a;     }     long long ans = 0;     vector<long long> dp1, dp2;     int j;     long long val;     for (int i = n - 1; i >= 0; --i) {       for (auto iter : dp1) {         j = iter;         val = B[j];         if (val < 0) {           ;         }         if (val > 0) {           if (j >= A[i]) {             if (C[A[i]] == -1) {               dp2.push_back(A[i]);               C[A[i]] = val;               C[A[i]] %= mod;             } else {               C[A[i]] += val;               C[A[i]] %= mod;             }           } else {             if (C[A[i] / ceil(A[i], j)] == -1) {               dp2.push_back(A[i] / ceil(A[i], j));               C[A[i] / ceil(A[i], j)] = val;               ans += (i + 1) * (ceil(A[i], j) - 1) * val;               ans %= mod;               C[A[i] / ceil(A[i], j)] %= mod;             } else {               C[A[i] / ceil(A[i], j)] += val;               ans += (i + 1) * (ceil(A[i], j) - 1) * val;               ans %= mod;               C[A[i] / ceil(A[i], j)] %= mod;             }           }         }         B[j] = -1;       }       dp1.clear();       swap(dp1, dp2);       swap(B, C);       if (B[A[i]] == -1) {         dp1.push_back(A[i]);         B[A[i]] = 1;       } else {         B[A[i]]++;         B[A[i]] %= mod;       }     }     for (int j : dp1) {       B[j] = -1;     }     cout << ans << endl;   }   int main() {     int i = 1;     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int test_cases;     cin >> test_cases;     for (i = 1; i <= test_cases; ++i) solve(i);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       bool impr = false;       bool taken[n + 1];       for (int i = 0; i <= n; i++) {         taken[i] = false;       }       int notTaken = -1;       for (int i = 0; i < n; i++) {         int k;         cin >> k;         bool done = false;         for (int i = 0; i < k; i++) {           int next;           cin >> next;           if (!done && !taken[next]) {             taken[next] = true;             done = true;           }         }         if (!done) {           impr = true;           notTaken = i + 1;         }       }       if (impr) {         cout <<  IMPROVE  << endl;         int good = 0;         for (int i = 1; i <= n; i++) {           if (taken[i] == false) {             good = i;           }         }         cout << notTaken <<     << good << endl;       } else {         cout <<  OPTIMAL  << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long int N, T;   void solve() {     cin >> N;     vector<long long int> cnt(N + 1);     long long int x;     for (long long int i = 0; i < N; i++) {       cin >> x;       cnt[x]++;     }     long long int ans = 0;     for (long long int s = 2; s <= 2 * N; s++) {       long long int cur = 0;       for (long long int i = 1; i < (s + 1) / 2; i++) {         if (s - i > N)           continue;         else {           cur += min(cnt[i], cnt[s - i]);         }       }       if (s % 2 == 0) {         cur += cnt[s / 2] / 2;       }       ans = max(ans, cur);     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> T;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[200001], out[20];   int len[200001];   int ans1[2000001], ans2[2000001];   vector<int> v[2000001];   int digit[6];   bool solve(int n, int i, int sum) {     if (!i) {       if (ans1[sum] >= 10) return false;       ans2[n] = sum;       return true;     }     for (int j = 1, k = 10; k <= i * 10; j++, k *= 10) {       if (solve(n, i / k, sum + i % k)) {         v[n].push_back(j);         return true;       }     }     return false;   }   int main() {     int n;     for (int i = 1; i <= 2000000; i++) {       int j = 0;       for (; digit[j] == 9; digit[j++] = 0)         ;       digit[j]++;       int sum = 0;       for (int j = 0; j < 6; j++) sum += digit[j];       ans1[i] = sum;     }     for (int i = 1; i <= 2000000; i++) {       v[i].reserve(7);       solve(i, i, 0);     }     scanf( %d%s , &n, s);     int sum;     while (1) {       if (n > 10) {         int t = rand() % min(n / 2, 500);         for (int i = 0; i < t; i++) len[i] = 2;         for (int i = 2 * t; i < n; i++) len[i - t] = 1;         random_shuffle(len, len + n - t);       } else         for (int i = 0; i < n; i++) len[i] = 1;       sum = 0;       for (int i = 0, j = 0; j < n; j += len[i++])         sum += len[i] == 1 ? s[j] -  0  : (s[j] -  0 ) * 10 + s[j + 1] -  0 ;       if (!v[sum].empty()) break;     }     for (int i = 0, j = 0; j < n; j += len[i++]) {       if (i) putchar( + );       putchar(s[j]);       if (len[i] > 1) putchar(s[j + 1]);     }     putchar(  n );     sprintf(out,  %d , sum);     int j = 0;     for (int i : v[sum]) {       if (j) putchar( + );       for (int k = 0; k < i; k++) putchar(out[j + k]);       j += i;     }     putchar(  n );     sum = ans2[sum];     sprintf(out,  %d , sum);     for (int i = 0; out[i]; i++) {       if (i) putchar( + );       putchar(out[i]);     }     putchar(  n );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, c;     cin >> a >> b >> c;     long long Min = min(a, b);     Min = min(Min, c);     long long day = 0;     while (a + b + c >= 7 && a >= 3 && b >= 2 && c >= 2) {       a -= 3;       b -= 2;       c -= 2;       day += 7;     }     long long ans = 0;     long long _a = a, _b = b, _c = c;     for (long long i = 1; i <= 7; i++) {       long long tmp = 0;       a = _a;       b = _b;       c = _c;       for (long long j = i; j <= i + 7; j++) {         long long _j = j % 7;         if (_j == 1 || _j == 4 || _j == 0) {           if (!a) {             ans = max(ans, tmp);             break;           }           a--;           tmp++;         }         if (_j == 2 || _j == 6) {           if (!b) {             ans = max(ans, tmp);             break;           }           b--;           tmp++;         }         if (_j == 3 || _j == 5) {           if (!c) {             ans = max(ans, tmp);             break;           }           c--;           tmp++;         }       }       ans = max(ans, tmp);     }     cout << day + ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void ZZ(const char* name, Arg1&& arg1) {     std::cerr << name <<   =   << arg1 << endl;   }   template <typename Arg1, typename... Args>   void ZZ(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     std::cerr.write(names, comma - names) <<   =   << arg1;     ZZ(comma, args...);   }   int n, u, v;   int nxt[1000010], ans[1000010], cnt[1000010];   pair<int, int> a[1000000];   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;       cnt[a[i].second] = 1;       nxt[a[i].first] = a[i].second;       if (!a[i].first) u = a[i].second;     }     for (int i = 0; i < n; i++) {       if (!cnt[a[i].first]) {         v = a[i].first;         break;       }     }     for (int i = 0; i < n; i += 2) {       ans[i] = v;       v = nxt[v];     }     for (int i = 1; i < n; i += 2) {       ans[i] = u;       u = nxt[u];     }     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   #pragma GCC optimize(3)   using namespace std;   int a[200011], b[200011];   struct song {     int t;     int id;     bool operator<(song other) const {       if (t != other.t) return t < other.t;       return id < other.id;     }   };   struct oriSet {     int tol = 0;     int len = 0;     set<song> s;     bool empty() { return s.empty(); }     int size() { return s.size(); }     void insert(int id) {       s.insert(song{a[id], id});       tol += b[id];       len += a[id];     }     int extractmin() {       if (!s.empty()) {         return s.begin()->id;       }       return -1;     }     int extractmax() {       if (!s.empty()) {         return s.rbegin()->id;       }       return -1;     }     void del(int id) {       if (s.find(song{a[id], id}) != s.end()) {         s.erase(song{a[id], id});         tol -= b[id];         len -= a[id];       }     }   };   struct lessSet {     int tol = 0;     int len = 0;     set<song> s;     bool empty() { return s.empty(); }     int size() { return s.size(); }     void insert(int id) {       int ti = a[id] - a[id] / 2;       s.insert(song{a[id], id});       tol += b[id];       len += ti;     }     int extractmin() {       if (!s.empty()) {         return s.begin()->id;       }       return -1;     }     int extractmax() {       if (!s.empty()) {         return s.rbegin()->id;       }       return -1;     }     void del(int id) {       if (s.find(song{a[id], id}) != s.end()) {         s.erase(song{a[id], id});         tol -= b[id];         len -= a[id] - a[id] / 2;       }     }   };   oriSet s1;   lessSet s2;   int n, w, k;   void make_balance() {     while (s2.size() < w && !s1.empty()) {       int id = s1.extractmax();       s1.del(id);       s2.insert(id);     }     while (!s1.empty() && s2.size() == w) {       int id1 = s1.extractmax(), id2 = s2.extractmin();       if (a[id1] <= a[id2]) break;       if (a[id1] > a[id2]) {         s1.del(id1);         s2.del(id2);         s1.insert(id2);         s2.insert(id1);       }     }   }   int main() {     scanf( %d%d%d , &n, &w, &k);     int i, j, ans = 0;     for (i = 1; i <= n; i++) {       scanf( %d , &b[i]);     }     for (i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     for (i = 1, j = 1; i <= n; i++) {       s1.insert(i);       make_balance();       while (j <= i && s1.len + s2.len > k) {         s1.del(j);         s2.del(j);         make_balance();         j++;       }       ans = max(ans, s1.tol + s2.tol);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long n, k, ar[200000];   long long reach[777777];   long long q;   long long ans[777777];   multiset<long long> bst;   multiset<long long>::iterator it;   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> ar[i];     reach[0] = 1;     for (int i = 1; i <= 666666; i++) reach[i] = 0;     for (int i = 1; i <= n; i++)       for (int j = 555555; j + 1; --j)         if (reach[j]) reach[j + ar[i]] = 1;     ans[0] = 0;     bst.insert(0);     for (int i = 1; i <= 666666; i++) {       ans[i] = 1e18;       if (i > k && ans[i - k - 1] < 1e18) {         it = bst.find(ans[i - k - 1]);         bst.erase(it);       }       if (reach[i] == 0) continue;       if (bst.size() == 0) continue;       it = bst.begin();       q = (*it);       ans[i] = q + 1;       bst.insert(ans[i]);     }     q = 666666;     while (ans[q] > 1e17) --q;     cout << q <<     << ans[q] << endl;     cin.get();     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, C;   int A[1001];   vector<int> V;   int main() {     cin >> N;     for (int i = 0; i < N; ++i) cin >> A[i];     for (int i = 0; i < N; ++i) {       C = 1;       for (int j = i; j < N - 1; ++j) {         if (A[j] >= A[j + 1])           ++C;         else           break;       }       for (int j = i; j > 0; --j) {         if (A[j] >= A[j - 1])           ++C;         else           break;       }       V.push_back(C);     }     sort(V.begin(), V.end());     cout << V[V.size() - 1] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int nsz = 5e5;   int n, rt = 1, t, l[nsz + 5], r[nsz + 5];   vector<int> g[nsz + 5];   void dfs(int u = rt, int p = rt) {     for (int i = 0; i < g[u].size(); ++i) {       int v = g[u][i];       if (v == p) continue;       l[v] = ++t;     }     r[u] = ++t;     for (int i = g[u].size() - 1; i >= 0; --i) {       int v = g[u][i];       if (v == p) continue;       dfs(v, u);     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n - 1; ++i) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     l[rt] = ++t;     dfs();     for (int u = 1; u <= n; ++u) {       cout << l[u] <<     << r[u] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   inline long long Qmul(const long long &x, const long long &y,                         const long long &X) {     long long k = (long long)((1.0L * x * y) / (1.0L * X)), t = x * y - k * X;     t -= X;     while (t < 0) t += X;     return t;   }   class MillerRabin {    private:     const int P[12] = {2, 3, 5, 7, 11, 13, 17, 19, 61, 2333, 4567, 24251};     inline long long Qpow(long long x, long long y, long long X) {       long long t = 1;       while (y) y & 1 && (t = Qmul(t, x, X)), x = Qmul(x, x, X), y >>= 1;       return t;     }     inline bool Check(const long long &x, const int &p) {       if (!(x % p) || Qpow(p % x, x - 1, x) ^ 1) return false;       long long k = x - 1, t;       while (!(k & 1)) {         if ((t = Qpow(p % x, k >>= 1, x)) ^ 1 && t ^ (x - 1)) return false;         if (!(t ^ (x - 1))) return true;       }       return true;     }       public:     bool IsPrime(const long long &x) {       if (x < 2) return false;       for (int i = 0; i ^ 12; ++i) {         if (!(x ^ P[i])) return true;         if (!Check(x, P[i])) return false;       }       return true;     }   };   class PollardRho {    private:     long long ans;     MillerRabin MR;     inline long long gcd(const long long &x, const long long &y) {       return y ? gcd(y, x % y) : x;     }     inline long long Work(const long long &x, const int &y) {       int t = 0, k = 1;       long long v0 = (1LL * rand() * rand() * rand() * rand() % (x - 1) + 1),                 v = v0, d, s = 1;       while (1) {         if (v = (Qmul(v, v, x) + y) % x,             s = Qmul(s, ((v - v0) < 0 ? -(v - v0) : (v - v0)), x),             !(v ^ v0) || !s)           return x;         if (++t == k) {           if ((d = gcd(s, x)) ^ 1) return d;           v0 = v, k <<= 1;         }       }     }     inline void Resolve(long long x, int t) {       if (!(x ^ 1) || x <= ans) return;       if (MR.IsPrime(x)) return (void)(ans < (x) && (ans = (x)));       long long y = x;       while ((y = Work(x, t--)) == x)         ;       while (!(x % y)) x /= y;       Resolve(x, t), Resolve(y, t);     }       public:     inline PollardRho() { srand(20050521); }     inline long long GetMax(const long long &x) {       return ans = 0, Resolve(x, 302627441), ans;     }   } P;   int T, p, ans[10010];   long long pr[60], dis[100010], head[100010], o;   struct edge {     int to, link;     long long w;   } e[6000010];   struct node {     int id;     long long w;     bool operator<(const node &b) const { return w > b.w; }   };   priority_queue<node> Q;   struct ask {     long long n, k, id;     bool operator<(const ask &b) const { return k < b.k; }   } q[10010];   void add_edge(int u, int v, long long w) {     e[++o].to = v, e[o].link = head[u], head[u] = o, e[o].w = w;   }   long long exgcd(long long a, long long b, long long &x, long long &y) {     if (!b) return x = 1, y = 0, a;     long long t = exgcd(b, a % b, y, x);     return y -= a / b * x, t;   }   int main() {     scanf( %d , &T);     for (int i = 1; i <= T; i++) scanf( %lld%lld , &q[i].n, &q[i].k), q[i].id = i;     sort(q + 1, q + T + 1);     for (int l = 1, r = 0; l <= T; l = r + 1) {       while (r < T && q[r + 1].k == q[l].k) r++;       long long k = q[l].k;       if (k == 1) continue;       p = 0;       while (k > 1) {         pr[++p] = P.GetMax(k);         while (k % pr[p] == 0) k /= pr[p];       }       if (p == 1) {         for (int i = l; i <= r; i++) ans[q[i].id] = (q[i].n % pr[1] == 0);         continue;       }       if (p == 2) {         long long x, y;         exgcd(pr[1], pr[2], x, y);         x = (x % pr[2] + pr[2]) % pr[2];         for (int i = l; i <= r; i++) {           long long b = q[i].n - Qmul(x, q[i].n, pr[2]) * pr[1];           ans[q[i].id] = (b >= 0);         }         continue;       }       int L = pr[p];       o = 0;       for (int i = 1; i < L; i++) dis[i] = LLONG_MAX;       for (int i = 0; i < L; i++) {         head[i] = 0;         for (int j = 1; j < p; j++) add_edge(i, (i + pr[j]) % L, pr[j]);       }       Q.push((node){0, dis[0] = 0});       while (Q.size()) {         int u = Q.top().id;         Q.pop();         for (int i = head[u]; i; i = e[i].link)           if (e[i].w + dis[u] < dis[e[i].to]) {             dis[e[i].to] = dis[u] + e[i].w;             Q.push((node){e[i].to, dis[e[i].to]});           }       }       for (int i = l; i <= r; i++) ans[q[i].id] = (q[i].n >= dis[q[i].n % L]);     }     for (int i = 1; i <= T; i++) puts(ans[i] ?  YES  :  NO );   }


#include <bits/stdc++.h>   using namespace std;   const int N = int(5e5) + 3;   const int MOD = 998244353;   const double PI = 4 * atan(1);   const double eps = 1e-10;   const long long oo = 1e10;   const int K = 26;   int n, m;   int a[N], b[N], c[N], d[N], k[N];   vector<int> actors[N];   vector<int> pitch[N];   set<int> s;   unordered_map<int, int> maa;   unordered_map<int, set<int> > occ;   vector<int> songs[N];   int anss[N];   void prepAns() {     for (int i = 0; i < m; i++) {       for (auto j : songs[i]) {         anss[j] = i;       }     }   }   bool exists(int c, int j) {     set<int>::iterator it;     bool cond = false;     if (s.count(c - 1) == 0) {       cond = true;       s.insert(c - 1);     }     it = s.find(c - 1);     it++;     if (it == s.end()) {       if (cond) {         s.erase(c - 1);       }       return 0;     }     int x = *it;     int y = *occ[x].begin();     songs[j].push_back(y);     occ[x].erase(occ[x].begin());     if (occ[x].size() == 0) {       s.erase(x);     }     if (cond) {       s.erase(c - 1);     }     return 1;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i] >> b[i];       s.insert(a[i]);       s.insert(b[i]);     }     cin >> m;     s.insert(0);     for (int i = 0; i < m; i++) {       cin >> c[i] >> d[i] >> k[i];       s.insert(c[i]);       s.insert(d[i]);     }     int ind = 1;     for (auto t : s) {       maa[t] = ind++;     }     for (int i = 0; i < n; i++) {       a[i] = maa[a[i]];       b[i] = maa[b[i]];       pitch[b[i]].push_back(i);     }     for (int i = 0; i < m; i++) {       c[i] = maa[c[i]];       d[i] = maa[d[i]];       actors[d[i]].push_back(i);     }     maa.clear();     s.clear();     int ans = 0;     for (int i = 1; i < ind; i++) {       for (auto j : pitch[i]) {         s.insert(a[j]);         occ[a[j]].insert(j);       }       for (auto j : actors[i]) {         while (k[j] && exists(c[j], j)) {           k[j]--;           ans++;         }       }     }     if (ans == n) {       cout <<  YES n ;       prepAns();       for (int i = 0; i < n; i++) {         cout << anss[i] + 1 <<    ;       }     } else {       cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int TOUR = 1 << 18, INF = 0x3f3f3f3f;   struct strq {     int r1, r2, s1, s2, ind;     strq() {}   } q[TOUR];   struct event {     int st, red, stup;     strq qq;     event(int st, int red, int stup, strq qq)         : st(st), red(red), stup(stup), qq(qq) {}     event() {}   };   bool operator<(event a, event b) {     if (a.red != b.red) return a.red < b.red;     return a.st > b.st;   }   int t[2 * TOUR];   int red[TOUR], stup[TOUR];   int brred, brstup;   int rje[TOUR];   int indexxR[TOUR];   int n, brq;   vector<event> V;   vector<int> R[TOUR];   void Stavi(int pos, int val) {     for (t[pos + TOUR] = val, pos = (pos + TOUR) / 2; pos; pos /= 2)       t[pos] = max(t[2 * pos], t[2 * pos + 1]);   }   int Vrati(int pos, int lo, int hi, int begin, int end) {     if (lo >= end || hi <= begin) return 0;     if (lo >= begin && hi <= end) return t[pos];     return max(Vrati(2 * pos + 0, lo, (lo + hi) / 2, begin, end),                Vrati(2 * pos + 1, (lo + hi) / 2, hi, begin, end));   }   void Promjena() {     for (int i = 0; i < n; i++) swap(red[i], stup[i]);     for (int i = 0; i < brq; i++) {       swap(q[i].r1, q[i].s1);       swap(q[i].r2, q[i].s2);     }   }   void Init() {     V.clear();     for (int i = 0; i < TOUR; i++) R[i].clear();     for (int i = 0; i < n; i++) {       V.push_back(event(0, red[i], stup[i], strq()));       R[stup[i]].push_back(red[i]);     }     for (int i = 0; i < brq; i++) V.push_back(event(1, q[i].r1, q[i].s1, q[i]));     memset(t, INF, sizeof t);     memset(indexxR, 0, sizeof indexxR);     for (int i = 0; i < TOUR; i++) {       R[i].push_back(INF);       sort(R[i].begin(), R[i].end());       Stavi(i, R[i][0]);     }     sort(V.begin(), V.end());   }   int main() {     scanf( %d%d%d%d , &brred, &brstup, &n, &brq);     for (int i = 0; i < n; i++) scanf( %d%d , &red[i], &stup[i]);     for (int i = 0; i < brq; i++) {       scanf( %d%d%d%d , &q[i].r1, &q[i].s1, &q[i].r2, &q[i].s2);       q[i].ind = i;     }     for (int i = 0; i < 2; i++) {       Init();       for (int j = 0; j < (int)V.size(); j++) {         event tmp = V[j];         if (!tmp.st) {           indexxR[tmp.stup]++;           Stavi(tmp.stup, R[tmp.stup][indexxR[tmp.stup]]);         } else {           if (Vrati(1, 0, TOUR, tmp.qq.s1, tmp.qq.s2 + 1) <= tmp.qq.r2)             rje[tmp.qq.ind] = 1;         }       }       Promjena();     }     for (int i = 0; i < brq; i++) {       if (rje[i])         printf( YES n );       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int nextt[200007], c[200007], vis[200007];   long long ans, n;   template <class T>   void read(T &x) {     int f = 0;     x = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       f |= (ch ==  - );       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     x = f ? -x : x;   }   int MIN(int now) {     int min = c[now];     for (int i = nextt[now];; i = nextt[i]) {       if (i == now)         return min;       else if (c[i] < min)         min = c[i];     }   }   int main() {     read(n);     for (int i = 1; i <= n; i++) read(c[i]);     for (int i = 1; i <= n; i++) {       read(nextt[i]);       if (nextt[i] == i) vis[i] = -1, ans += c[i];     }     for (int now = 1; now <= n; now++) {       if (vis[now]) continue;       for (int i = now;; i = nextt[i]) {         if (vis[i]) {           if (vis[i] == now) ans += MIN(i);           break;         }         vis[i] = now;       }     }     printf( %I64d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[200015], i, n1;   bool doit() {     n1 = n - 1;     for (i = 0; n1; ++i) {       if (a[i] == a[i + 1]) {         printf( :( );         return 0;       }       a[i] += n1;       --n1;     }     for (i = 0; i < n; ++i) printf( %d  , a[i]);   }   int main() {     scanf( %d , &n);     i = n;     while (i) {       --i;       scanf( %d , a + i);       a[i] -= i;     }     sort(a, a + n);     doit();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a, b;     while (cin >> n >> m >> a >> b) {       a--;       b--;       int r1 = a / m, r2 = b / m;       int c1 = a % m, c2 = b % m;       if (r1 == r2)         cout << 1 << endl;       else if (c1 == 0 && c2 == m - 1)         cout << 1 << endl;       else if (c1 == 0 && c2 != m - 1) {         if (b == n - 1)           cout << 1 << endl;         else           cout << 2 << endl;       } else if (r1 + 1 == r2)         cout << 2 << endl;       else if (c1 != 0 && c2 == m - 1)         cout << 2 << endl;       else {         if (b == n - 1)           cout << 2 << endl;         else if (c2 + 1 == c1)           cout << 2 << endl;         else           cout << 3 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct IP {     string nm;     int a, b, c, d;   };   int n, m;   int test(IP a[], IP x) {     for (int i = 0; i < n; i++) {       if (a[i].a == x.a && a[i].b == x.b && a[i].c == x.c && a[i].d == x.d)         return i;     }     return -1;   }   IP a[1000], b[1000];   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> a[i].nm >> a[i].a;       getchar();       cin >> a[i].b;       getchar();       cin >> a[i].c;       getchar();       cin >> a[i].d;     }     for (int i = 0; i < m; i++) {       cin >> b[i].nm >> b[i].a;       getchar();       cin >> b[i].b;       getchar();       cin >> b[i].c;       getchar();       cin >> b[i].d;       getchar();     }     for (int i = 0; i < m; i++) {       cout << b[i].nm <<    ;       printf( %d.%d.%d.%d; , b[i].a, b[i].b, b[i].c, b[i].d);       int k = test(a, b[i]);       if (k != -1) cout <<   #  << a[k].nm;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   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;     }   };   map<string, long long> mp;   const long long N = 8;   long long a[N][N];   long long ans1 = (long long)1e18 + 5, ans2 = 0;   vector<long long> vv(3);   vector<long long> ini = {0, 1, 2, 3, 4, 5, 6};   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     mp[ Anka ] = 0, mp[ Chapay ] = 1, mp[ Cleo ] = 2, mp[ Troll ] = 3,     mp[ Dracul ] = 4, mp[ Snowy ] = 5, mp[ Hexadecimal ] = 6;     cin >> n;     string x, cc1, y;     for (long long i = 0; i < n; i++) {       cin >> x >> cc1 >> y;       a[mp[x]][mp[y]] = 1;     }     cin >> vv[0] >> vv[1] >> vv[2];     do {       for (long long i = 1; i < 5; i++) {         for (long long j = i + 1; j < 6; j++) {           vector<vector<long long> > aa(3);           for (long long k = 0; k < i; k++) aa[0].push_back(ini[k]);           for (long long k = i; k < j; k++) aa[1].push_back(ini[k]);           for (long long k = j; k < 7; k++) aa[2].push_back(ini[k]);           vector<vector<long long> > cur1 = aa;           long long lik = 0;           for (long long ii = 0; ii < 3; ii++) {             for (long long jj = 0; jj < (long long)cur1[ii].size(); jj++) {               for (long long k = jj + 1; k < (long long)cur1[ii].size(); k++) {                 if (a[cur1[ii][jj]][cur1[ii][k]]) {                   lik++;                 }                 if (a[cur1[ii][k]][cur1[ii][jj]]) lik++;               }             }           }           vector<long long> cur = {0, 1, 2};           vector<long long> arr(3, 0);           do {             arr[0] = (vv[cur[0]] / (long long)aa[0].size());             arr[1] = (vv[cur[1]] / (long long)aa[1].size());             arr[2] = (vv[cur[2]] / (long long)aa[2].size());             long long ans = 0;             for (long long ii = 0; ii < 3; ii++) {               for (long long jj = ii + 1; jj < 3; jj++) {                 ans = max(ans, abs(arr[ii] - arr[jj]));               }             }             if (ans <= ans1) {               if (ans == ans1) {                 if (lik > ans2) {                   ans2 = lik;                 }               } else {                 ans1 = ans;                 ans2 = lik;               }             }           } while (next_permutation((cur).begin(), (cur).end()));         }       }     } while (next_permutation((ini).begin(), (ini).end()));     cout << ans1 <<     << ans2 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     long long ans = 1;     for (int i = k + 1; i <= n; i++) ans = (ans * (n - k)) % 1000000007;     ans = (ans * (k)) % 1000000007;     if (k - 1 > 0) {       long long prod = 0;       vector<int> v(k + 2);       int loopval = 1;       for (int i = 1; i <= k - 1; i++) loopval *= k;       for (int i = 0; i < loopval; i++) {         int temp = i;         for (int j = 0; j < k - 1; j++) {           v[j + 2] = temp % k + 1;           temp /= k;         }         int j = 0;         for (; j < k - 1; j++) {           int steps = 0;           temp = v[j + 2];           while (steps <= 7 && temp > 1) {             temp = v[temp];             steps++;           }           if (temp > 1) break;         }         if (j == k - 1) prod++;       }       ans = (ans * (prod)) % 1000000007;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int w;     w = 1;     while (w--) {       int n, maxi = -1, b = 0;       vector<int> s, t[10];       bool f1 = true, f = false;       cin >> n;       while (n > 0) {         int x = n % 10;         s.push_back(x);         maxi = max(maxi, x);         n /= 10;       }       reverse(s.begin(), s.end());       while (f1) {         f1 = false;         f = false;         for (int i = 0; i < s.size(); i++) {           int y = s[i];           if (y > 0) {             f1 = true;             f = true;             t[b].push_back(1);             y--;             s[i] = y;           } else {             if (f) t[b].push_back(0);           }         }         b++;       }       string ans[maxi];       cout << maxi << endl;       for (int i = 0; i < maxi; i++) {         for (int j = 0; j < t[i].size(); j++) {           char c = (char)(t[i][j] +  0 );           ans[i].push_back(c);         }       }       for (int i = 0; i < maxi; i++) cout << ans[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500010;   long long x, y, m;   int main() {     ios_base::sync_with_stdio(false);     cin >> x >> y >> m;     if (x >= m || y >= m) {       cout <<  0 n ;       return 0;     }     if (x <= 0 && y <= 0) {       cout <<  -1 n ;       return 0;     }     if (x > y) swap(x, y);     long long res = 0;     if (x < 0) {       res = ceil(-x * 1.0 / y);       x += res * y;     }     while (x < m && y < m) {       if (x < y)         x = x + y;       else         y = x + y;       res++;     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Point {     double x, y;     Point() : x(0), y(0) {}     Point(double x, double y) : x(x), y(y) {}     Point(const Point &p) : x(p.x), y(p.y) {}     int operator<(const Point &rhs) const {       return make_pair(y, x) < make_pair(rhs.y, rhs.x);     }     int operator==(const Point &rhs) const {       return make_pair(y, x) == make_pair(rhs.y, rhs.x);     }     Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }     Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); }     Point operator*(double c) const { return Point(x * c, y * c); }     Point operator/(double c) const { return Point(x / c, y / c); }     double arg() const { return atan2(y, x); }   };   double cross(Point p, Point q) { return p.x * q.y - p.y * q.x; }   double area(Point a, Point b, Point c) {     return fabs(cross(a, b) + cross(b, c) + cross(c, a)) / 2;   }   double area2(Point a, Point b, Point c) {     return cross(a, b) + cross(b, c) + cross(c, a);   }   double dot(Point p, Point q) { return p.x * q.x + p.y * q.y; }   double dist(Point p, Point q) { return sqrt(dot(p - q, p - q)); }   double dist2(Point p, Point q) { return dot(p - q, p - q); }   ostream &operator<<(ostream &os, const Point &p) {     os <<  (  << p.x <<  ,  << p.y <<  ) ;     return os;   }   Point offset(vector<Point> &v) {     Point ans = v[0];     for (auto &p : v) {       ans.x = min(ans.x, p.x);       ans.y = min(ans.y, p.y);     }     return ans;   }   void diff(vector<Point> &v, vector<pair<double, Point>> &ret) {     for (int i = 0; i < v.size(); ++i) {       Point vec = v[(i + 1) % v.size()] - v[i];       ret.emplace_back(vec.arg(), vec);     }   }   vector<Point> minkowskisum(vector<Point> &a, vector<Point> &b) {     Point off = offset(a) + offset(b);     vector<pair<double, Point>> v;     vector<Point> ret;     diff(a, v);     diff(b, v);     sort(v.begin(), v.end(),          [&](const pair<double, Point> &x, const pair<double, Point> &y) {            return x.first < y.first;          });     vector<bool> mark(v.size(), false);     for (int i = v.size() - 1; i > 0; --i) {       if (abs(v[i].first - v[i - 1].first) < 1e-9) {         mark[i] = true;         v[i - 1].second = v[i - 1].second + v[i].second;         v[i].second = Point(0, 0);       }     }     for (int i = 0; i < v.size(); ++i)       if (!mark[i]) ret.emplace_back(v[i].second);     partial_sum(ret.begin(), ret.end(), ret.begin());     off = off - offset(ret);     for (auto &p : ret) p = p + off;     return ret;   }   double cross(Point a, Point b, Point c) {     return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);   }   bool PointInsidePolygon(vector<Point> &poly, Point q) {     int a = 1, b = poly.size() - 1;     swap(a, b);     if (cross(poly[0], poly[a], q) > 0 || cross(poly[0], poly[b], q) < 0)       return 0;     while (abs(a - b) > 1) {       int c = (a + b) >> 1;       if (cross(poly[0], poly[c], q) > 0) {         b = c;       } else         a = c;     }     return cross(poly[a], poly[b], q) <= 0;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     vector<Point> a[3];     for (int i = 0; i < 3; ++i) {       int m;       cin >> m;       while (m--) {         int x, y;         cin >> x >> y;         a[i].emplace_back(x, y);       }     }     vector<Point> poly = minkowskisum(a[0], a[1]);     poly = minkowskisum(poly, a[2]);     int q;     cin >> q;     while (q--) {       Point a;       cin >> a.x >> a.y;       a.x *= 3;       a.y *= 3;       if (PointInsidePolygon(poly, a))         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     int n, w;     cin >> n >> w;     vector<long long> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     long long tot = 0, al = 0;     long long minimum = 1e18, maximum = 0;     for (int i = 0; i < n; i++) {       tot += v[i];       minimum = min(minimum, tot);       maximum = max(maximum, tot);     }     long long left = w - maximum;     if (minimum < 0) al = abs(minimum);     if (left < 0 || left < al)       cout << 0;     else       cout << left - al + 1;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int n;   double res;   double x[60006], v[60006];   bool ok(double t) {     double l, r;     l = 1e-9;     r = 1e9;     for (int i = 1; i <= n; ++i) {       double m = t * v[i];       l = max(l, x[i] - m);       r = min(r, x[i] + m);     }     return (l <= r);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) cin >> x[i];     for (int i = 1; i <= n; ++i) cin >> v[i];     double l, r, mid;     l = 0.0, r = 1e9;     while (abs(l - r) > 1e-6) {       mid = (l + r) / 2;       if (ok(mid)) {         res = mid;         r = mid;       } else         l = mid;     }     cout << fixed << setprecision(20) << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 500004, inf = 0x3f3f3f3f;   int a[MAX];   int no[102];   int nex[MAX][102];   int f[102][102 * 102];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, k, p;     cin >> n >> k >> p;     for (int i = 1; i <= n; i++) {       cin >> a[i];       a[i] += a[i - 1];       a[i] %= p;     }     for (int i = 0; i < p; i++) no[i] = n + 1;     for (int i = n; i >= 0; i--) {       for (int j = 0; j < p; j++) {         nex[i][j] = no[j];       }       no[a[i]] = i;     }     int M = (p - 1) * k;     memset(f, 0x3f, sizeof f);     f[0][0] = 0;     for (int i = 1; i <= k; i++) {       for (int j = 0; j <= M; j++) {         for (int u = min(p - 1, j); u >= 0; u--) {           if (f[i - 1][j - u] <= n)             f[i][j] = min(nex[f[i - 1][j - u]][j % p], f[i][j]);         }       }     }     for (int j = a[n]; j <= M; j += p)       if (f[k][j] <= n) {         cout << j;         return 0;       }   }


#include <bits/stdc++.h>   using namespace std;   long double F(int A, int B) {     long double S = 1;     for (int i = 0; i < B; i++) S = S * (A - i) / (i + 1);     return S;   }   int main() {     int N;     scanf( %d , &N);     static int C[100000], Max[100000];     static long double P[100000][101];     memset(P, 0, sizeof(P));     for (int i = 0; i < N; i++) {       scanf( %d , &C[i]);       Max[i] = C[i];       P[i][C[i]] = 1;     }     int Query;     scanf( %d , &Query);     long double Ans = 0;     for (int i = 0; i < N; i++)       if (!C[i]) Ans++;     while (Query--) {       int a, b, K;       scanf( %d%d%d , &a, &b, &K);       a--;       b--;       static long double DP[101];       memset(DP, 0, sizeof(DP));       long double All = F(C[a], K);       for (int i = 0; i <= C[a] && i <= Max[a]; i++)         for (int j = max(0, K - C[a] + i); j <= K && j <= i; j++)           DP[i - j] += P[a][i] * F(i, j) * F(C[a] - i, K - j) / All;       Ans += DP[0] - P[a][0];       for (int i = 0; i <= Max[a]; i++) P[a][i] = DP[i];       C[a] -= K;       C[b] += K;       printf( %0.10lf n , (double)Ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int input[n];       for (int i = (0); i < (n); i++) cin >> input[i];       sort(input, input + n);       vector<int> ans(n);       for (int i = (0); i < (n / 2); i++) {         ans[i * 2] = i;         ans[i * 2 + 1] = n - 1 - i;       }       if (n & 1) ans[n - 1] = n / 2;       for (int i = (n)-1; i >= (0); --i) cout << input[ans[i]] <<    ;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char cell;     short r, c, rr, cc, e;     int res = 0, i, cat[100], cnt = 0;     cin >> r >> c;     rr = r;     cc = c;     for (i = 0; i < r; i++) {       e = 0;       for (int I = 0; I < c; I++) {         cin >> cell;         if (cell ==  S ) {           e++;           cat[cnt] = I;           cnt++;         }       }       if (e == 0) {         res += c;         rr--;       }     }     cc -= cnt;     sort(cat, cat + cnt);     for (i = 1; i < cnt; i++)       if (cat[i] == cat[i - 1]) cc++;     res += cc * rr;     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     int answer1, answer2;     answer1 = min(a, b);     a -= answer1;     b -= answer1;     answer2 = (max(a, b)) / 2;     cout << answer1 <<     << answer2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 3, M = 1e6 + 3, O = 2e6 + 3;   int c[N], a[N], b[N], id[O], tp;   bool f[N];   struct P {     int f, sz;   } d[M], st[O];   int gf(int x) { return d[x].f == x ? x : gf(d[x].f); }   void mg(int x, int y) {     int u = gf(x), v = gf(y);     if (d[u].sz > d[v].sz) swap(u, v);     st[++tp] = d[u], id[tp] = u, st[++tp] = d[v], id[tp] = v;     d[v].sz += d[u].sz, d[u].f = v;   }   map<pair<int, int>, int> mp;   vector<int> vc[N];   int main() {     int n, m, k, i, j, l, u, v, w, uu, vv, tt, uuu, vvv;     long long ans = 0;     scanf( %d%d%d , &n, &m, &k);     for (i = 1; i <= n; ++i) scanf( %d , c + i);     for (i = 1; i <= n * 2; ++i) d[i].f = i, d[i].sz = 1;     for (i = 1; i <= m; ++i) scanf( %d%d , a + i, b + i);     for (i = 1; i <= m; ++i)       if (c[a[i]] == c[b[i]]) {         u = gf(a[i]), v = gf(b[i]);         if (u == v)           f[c[a[i]]] = 1;         else           mg(a[i], b[i] + n), mg(b[i], a[i] + n);       }     for (i = 1, j = 0; i <= m; ++i)       if (c[a[i]] != c[b[i]] && !f[c[a[i]]] && !f[c[b[i]]]) {         u = c[a[i]], v = c[b[i]];         if (u > v) swap(u, v);         l = mp[{u, v}];         if (!l) l = mp[{u, v}] = ++j;         vc[l].push_back(i);       }     for (auto o : mp) {       u = o.first.first, v = o.first.second, w = o.second, tt = tp;       for (int o2 : vc[w]) {         uu = a[o2], vv = b[o2], uuu = gf(uu), vvv = gf(vv);         if (uuu == vvv) {           --ans;           break;         }         mg(uu, vv + n), mg(uu + n, vv);       }       while (tp != tt) d[id[tp]] = st[tp], --tp;     }     for (i = 1, j = 0; i <= k; ++i)       if (!f[i]) ++j;     ans += j * 1ll * (j - 1) / 2;     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, sum = 0;     string s;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> s;       if (s ==  Tetrahedron ) sum += 4;       if (s ==  Cube ) sum += 6;       if (s ==  Octahedron ) sum += 8;       if (s ==  Dodecahedron ) sum += 12;       if (s ==  Icosahedron ) sum += 20;     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int m, n;     cin >> m >> n;     char d[m][n];     int ub = m, lb = 0, lfb = n, rb = 0;     for (int r = 0; r < m; r++) {       for (int c = 0; c < n; c++) {         cin >> d[r][c];         if (d[r][c] ==  * ) {           if (ub > r) ub = r;           if (lb < r) lb = r;           if (rb < c) rb = c;           if (lfb > c) lfb = c;         }       }     }     for (int r = ub; r <= lb; r++) {       for (int c = lfb; c <= rb; c++) cout << d[r][c];       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     ;     long long t, i, j, r, k, x, y, n, m, l, a, b, c, d = 0;     cin >> t;     string s, cm =  abacaba ;     while (t--) {       cin >> l >> r;       if (2 * l <= r) {         cout << l <<     << 2 * l <<   n ;       } else         cout <<  -1 -1 n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<string> s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     s.resize(n);     for (int i = 0; i < n; i++) {       cin >> s[i];     }     int ans = 0;     for (int i = 0; i < 7; i++) {       int cnt = 0;       for (int j = 0; j < n; j++) {         if (s[j][i] ==  1 ) cnt++;       }       ans = max(ans, cnt);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     long long val;     cin >> val;     if (val & 1)       cout << 1 << endl;     else       cout << 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int arr[12], k, c = 0, a = 0;     cin >> k;     for (int i = 0; i < 12; i++) {       cin >> arr[i];     }     sort(arr, arr + 12);     for (int i = 11; i >= 0; i--) {       if (c < k) {         c += arr[i];         a++;       }     }     if (c < k)       cout <<  -1 ;     else       cout << a;     return 0;   }


#include <bits/stdc++.h>   const int maxn = 1e6 + 5;   const int maxm = 1e5 + 5;   using namespace std;   int rd() {     int x = 0;     char c = getchar();     while (!isdigit(c)) c = getchar();     while (isdigit(c)) x = (x << 3) + (x << 1) + c -  0 , c = getchar();     return x;   }   int n, head[maxn], tot, a, b, c, d, cnt, Node, rk[maxn], len, lst[maxn], top;   int lena, lenb, lenc, lend, mx;   vector<int> vec[maxn];   bitset<maxm> res;   struct node {     int v, nxt;   } e[maxn << 1];   void add(int u, int v) {     e[++tot].v = v;     e[tot].nxt = head[u];     head[u] = tot;   }   void dfs(int u, int y) {     bool flag = true;     for (int i = head[u]; i; i = e[i].nxt) {       int v = e[i].v;       if (v == y) continue;       if (u == 1) ++cnt;       flag = false;       dfs(v, u);     }     if (flag) vec[cnt].push_back(u), rk[u] = cnt;   }   bool check() {     lena = ((int)vec[rk[a]].size());     lenb = ((int)vec[rk[b]].size());     lenc = ((int)vec[rk[c]].size());     lend = ((int)vec[rk[d]].size());     for (int i = max(0, len - lena - lenb); i <= len - 2; i++) {       if (lenc <= i && res[i - lenc]) return true;       if (lend <= i && res[i - lend]) return true;     }     for (int i = max(0, len - lenc - lend); i <= len - 2; i++) {       if (lena <= i && res[i - lena]) return true;       if (lenb <= i && res[i - lenb]) return true;     }     return false;   }   int main() {     n = rd();     a = rd();     b = rd();     c = rd();     d = rd();     for (int i = 2, u; i <= n; i++) {       u = rd();       add(u, i);       add(i, u);     }     dfs(1, 0);     for (int i = 1; i <= cnt; i++) {       Node += ((int)vec[i].size());       if (i != rk[a] && i != rk[b] && i != rk[c] && i != rk[d])         lst[++top] = ((int)vec[i].size()), mx = max(mx, ((int)vec[i].size()));     }     if (Node & 1) {       puts( No );       return 0;     }     len = Node / 2 + 1;     if (rk[a] == rk[b]) {       puts( No );       return 0;     }     if (rk[c] == rk[d]) {       puts( No );       return 0;     }     if (((int)vec[rk[a]].size()) > Node - len ||         ((int)vec[rk[b]].size()) > Node - len ||         ((int)vec[rk[c]].size()) > Node - len ||         ((int)vec[rk[d]].size()) > Node - len || mx > Node - len) {       puts( No );       return 0;     }     res.set(0);     for (int i = 1; i <= top; i++) res |= res << lst[i];     puts(check() ?  Yes  :  No );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100005];   int main() {     int n, i;     long long sum = 0, x = 0;     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];       sum += a[i];     }     sort(a, a + n);     for (i = 0; i < (n / 2); i++) {       x += a[i];     }     cout << x * x + (sum - x) * (sum - x) << endl;     return 0;   }


#include <bits/stdc++.h>   const int inf = 2 * 1e9;   const int mod = 1e9 + 7;   const long long infll = (long long)1e18;   const long double eps = 1e-9;   const int dx[] = {1, 0, -1, 0};   const int dy[] = {0, -1, 0, 1};   const int MAXY = 1005000;   using namespace std;   int n, m;   vector<vector<pair<int, int>>> g;   vector<bool> used;   vector<int> topsort;   void dfs(int v, int time) {     used[v] = true;     for (int i = 0; i < g[v].size(); i++) {       int to = g[v][i].first;       int edgeTime = g[v][i].second;       if (edgeTime <= time) {         if (!used[to]) dfs(to, time);       }     }     topsort.push_back(v);   }   bool check(int time) {     used.assign(n, false);     topsort.clear();     for (int i = 0; i < n; i++) {       if (!used[i]) dfs(i, time);     }     reverse(topsort.begin(), topsort.end());     for (int i = 0; i < (n - 1); i++) {       int cur = topsort[i];       int nxt = topsort[i + 1];       bool ok = false;       for (int j = 0; j < g[cur].size(); j++) {         if (g[cur][j].second <= time) ok = ok || g[cur][j].first == nxt;       }       if (!ok) return false;     }     return true;   }   void solve() {     cin >> n >> m;     g.resize(n);     used.resize(n);     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       x--;       y--;       g[x].push_back(make_pair(y, i));     }     if (!check(m - 1)) {       cout << -1;       return;     }     int tl = 0;     int tr = m - 1;     while (tr - tl > 1) {       int tm = (tl + tr) / 2;       if (check(tm))         tr = tm;       else         tl = tm;     }     cout << tr + 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int tt;     cin >> tt;     while (tt--) {       int n, k;       cin >> n >> k;       vector<int> a(n);       for (int i = 0; i < n; i++) {         cin >> a[i];       }       int d = unique(a.begin(), a.end()) - a.begin();       if (k > d)         cout << 1 <<   n ;       else if (k == 1) {         if (d == 1)           cout << 1 <<   n ;         else           cout << -1 <<   n ;       } else {         int ans = 1;         d -= k;         ans += (d + k - 2) / (k - 1);         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using INT = long long;   int edge[2222][2222];   int deg[2222];   int n;   char s[2222];   int id[2222];   int ok() {     for (int i = 1; i <= n; i++) id[i] = i;     sort(id + 1, id + n + 1, [&](int u, int v) { return deg[u] < deg[v]; });     for (int i = 1, sum = 0; i < n; i++) {       sum += deg[id[i]];       if (sum == i * (i - 1) / 2) return 0;     }     return 1;   }   int small_solve() {     int mn = n + 1, cnt = 0;     for (int i = 0; i < (1 << n); i++) {       for (int j = 0; j < n; j++)         if ((i >> j) & 1) {           for (int k = 1; k <= n; k++)             if (k != j + 1) {               if (edge[k][j + 1])                 deg[k]--, deg[j + 1]++;               else                 deg[k]++, deg[j + 1]--;               edge[k][j + 1] ^= 1;               edge[j + 1][k] ^= 1;             }         }       if (ok()) {         if (mn > __builtin_popcount(i))           mn = __builtin_popcount(i), cnt = 1;         else if (mn == __builtin_popcount(i))           cnt++;       }       for (int j = 0; j < n; j++)         if ((i >> j) & 1) {           for (int k = 1; k <= n; k++)             if (k != j + 1) {               if (edge[k][j + 1])                 deg[k]--, deg[j + 1]++;               else                 deg[k]++, deg[j + 1]--;               edge[k][j + 1] ^= 1;               edge[j + 1][k] ^= 1;             }         }     }     for (int i = 1; i <= mn; i++) cnt *= i;     if (mn == n + 1)       cout << -1 << endl;     else       cout << mn <<     << cnt << endl;     exit(0);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       scanf( %s , s + 1);       for (int j = 1; j <= n; j++)         if (s[j] ==  1 ) {           edge[i][j] = 1;           deg[i]++;         }     }     if (ok()) {       cout << 0 <<     << 1 << endl;       return 0;     }     if (n <= 6) small_solve();     int cnt = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++)         if (j != i) {           if (edge[i][j])             deg[i]--, deg[j]++;           else             deg[i]++, deg[j]--;         }       if (ok()) cnt++;       for (int j = 1; j <= n; j++)         if (j != i) {           if (!edge[i][j])             deg[i]--, deg[j]++;           else             deg[i]++, deg[j]--;         }     }     cout << 1 <<     << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const long long linf = 1e18 + 5;   long long int gcd(long long int a, long long int b) {     return (b == 0 ? a : gcd(b, a % b));   }   long long int lcm(int a, int b) { return (a * (b / gcd(a, b))); }   long long int max(long long int a, long long int b, long long int c) {     return max(a, max(b, c));   }   long long int power(long long int x, long long int y) {     long long int ans = 1;     while (y > 0) {       if (y & 1) ans = (ans * x) % 1000000007;       x = (x * x) % 1000000007;       y /= 2;     }     return ans;   }   inline int read() {     int ret = 0, temp = 1;     int c = getchar();     while (c <  0  || c >  9 ) {       c = getchar();     }     while (c >=  0  && c <=  9 ) {       ret = (ret << 3) + (ret << 1) + c -  0 ;       c = getchar();     }     return ret;   }   string solve(int n) {     string s;     while (n > 0) {       s = char(n % 10 + 48) + s;       n /= 10;     }     return s;   }   double X[N], Y[N], A[N], B[N];   double dist(pair<long long int, long long int> a,               pair<long long int, long long int> b) {     return sqrt((a.first - b.first) * (a.first - b.first) +                 (a.second - b.second) * (a.second - b.second));   }   set<pair<double, int> > Adist, Bdist;   int main() {     int n;     double ax, ay, bx, by, tx, ty, x, y;     cin >> ax >> ay >> bx >> by >> tx >> ty;     cin >> n;     double add = 0.0;     for (int i = 0; i < n; i++) {       cin >> x >> y;       X[i] = x;       Y[i] = y;       add += 2 * dist({x, y}, {tx, ty});       A[i] = dist({x, y}, {ax, ay}) - dist({x, y}, {tx, ty});       B[i] = dist({x, y}, {bx, by}) - dist({x, y}, {tx, ty});       Adist.insert({A[i], i});       Bdist.insert({B[i], i});     }     double onlyA = 0.0, onlyB = 0.0, bothAB = 0.0;     onlyA = add + Adist.begin()->first;     onlyB = add + Bdist.begin()->first;     bothAB = add;     if (n == 1) {       printf( %.10lf n , min(onlyA, onlyB));     } else {       if (Adist.begin()->second == Bdist.begin()->second) {         auto it1 = Adist.begin();         it1++;         auto it2 = Bdist.begin();         it2++;         double ans = min(Adist.begin()->first + it2->first,                          Bdist.begin()->first + it1->first);         bothAB += ans;       } else         bothAB += (Adist.begin()->first + Bdist.begin()->first);       printf( %.10lf , min(min(onlyA, onlyB), bothAB));     }   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const long long MAX = 200005;   bool prime[MAX];   void sieve() {     memset(prime, true, sizeof(prime));     prime[0] = prime[1] = false;     for (long long p = 2; p * p <= MAX; p++) {       if (prime[p] == true) {         for (long long i = p * 2; i <= MAX; i += p) prime[i] = false;       }     }   }   long long power(long long a, long long b, long long m) {     if (b == 0) return 1;     if (b == 1) return a % m;     long long t = power(a, b / 2, m);     t = (t * t) % m;     if (b & 1) t = (t * a) % m;     return t;   }   long long modInverse(long long a, long long m) { return power(a, m - 2, m); }   long long __gcd(long long a, long long b) {     if (b == 0) return a;     return __gcd(b, a % b);   }   bool cmp(const pair<long long, long long> &l,            const pair<long long, long long> &r) {     return l.first < r.first;   }   long long n, q;   vector<vector<long long> > g;   long long visited[MAX];   long long num[MAX];   long long cnt[MAX];   long long k;   long long dfs(long long node) {     visited[node] = 1;     num[node] = k;     k++;     long long p = 0, flag = 0;     for (auto i : g[node]) {       if (!visited[i]) {         flag = 1;         p += dfs(i);       }     }     if (flag) {       cnt[node] = p;       return 1 + p;     } else {       cnt[node] = 0;       return 1;     }   }   void solve() {     cin >> n >> q;     g.resize(n + 1, vector<long long>());     long long x;     for (long long i = 2; i <= n; i++) {       cin >> x;       g[i].push_back(x);       g[x].push_back(i);     }     memset(visited, 0, sizeof(visited));     memset(num, 0, sizeof(num));     memset(cnt, 0, sizeof(cnt));     k = 1;     dfs(1);     map<long long, long long> m;     for (long long i = 0; i < MAX; i++) m[num[i]] = i;     while (q--) {       long long x, y;       cin >> x >> y;       if (cnt[x] < y - 1)         cout <<  -1               <<   n ;       else {         long long w = num[x];         cout << m[w + y - 1] <<   n ;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     ;     long long t;     t = 1;     while (t--) {       solve();       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> v;   int main() {     long long a, b, f, k;     cin >> a >> b >> f >> k;     if (b / a >= k) {       cout << 0 << endl;       return 0;     }     v.push_back(f);     for (long long i = 2;; i += 2) {       long long r1 = i * a - f;       long long r2 = i * a + f;       if (r1 > k * a) break;       if (r1 <= k * a) v.push_back(r1);       if (r2 <= k * a) v.push_back(r2);     }     v.push_back(k * a);     if (b < f) {       cout << -1;       return 0;     }     long long last = 0;     long long has = b;     int sum = 0;     for (int i = 0; i < v.size(); i++) {       long long dis = v[i] - last;       if (dis > has) {         has = b;         sum++;       }       if (has < dis) {         cout << -1 << endl;         return 0;       }       has -= dis;       last = v[i];     }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000005];   int main() {     int n;     long long sum = 0;     cin >> n;     memset(a, -1, sizeof(a));     for (int i = n; i >= 0; i--) {       if (a[i] != -1)         continue;       else {         for (int tt = 1;; tt *= 2) {           if (tt - 1 >= i) {             a[i] = tt - 1 - i;             a[tt - 1 - i] = i;             sum = sum + 2 * (tt - 1);             break;           }         }       }     }     cout << sum << endl;     for (int i = 0; i <= n; i++) {       if (i != 0) printf(   );       printf( %d , a[i]);     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a;     map<int, int> mp;     int mx = 0;     for (int i = 0; i < n; i++) {       cin >> a;       mp[a]++;     }     for (auto it : mp) {       mx = max(mx, it.second);     }     cout << n - mx;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   const int LIM = 15e5;   struct Vector {     int x, y;     Vector operator+(Vector v) { return {x + v.x, y + v.y}; }     Vector operator-(Vector v) { return {x - v.x, y - v.y}; }     long long operator%(Vector v) { return 1LL * x * v.y - 1LL * v.x * y; }     long long operator*(Vector v) { return 1LL * x * v.x + 1LL * y * v.y; }     friend double angle(Vector va, Vector vb) {       return abs(atan2(va % vb, va * vb) * 180 / PI);     }     double len() { return hypot(x, y); }     Vector operator*(int k) { return {k * x, k * y}; }   };   const int N = 1e5 + 1;   int n, sig[N];   Vector a[N];   vector<int> s[N];   void combine(int p) {     for (int i = p; i <= p + 2; ++i)       for (int j = i + 1; j <= p + 2; ++j) {         if (angle(a[i], a[j]) >= 120) {           int r = 3 * p + 3 - i - j;           if (s[i].size() > s[j].size()) s[i].swap(s[j]);           s[j].insert(s[j].end(), s[i].begin(), s[i].end());           a[j] = a[i] + a[j];           if (r == p) {             s[i].swap(s[r]);             a[i] = a[r];           }           return;         } else if (angle(a[i], a[j]) <= 60) {           int r = 3 * p + 3 - i - j;           for (int id : s[j]) sig[id] *= -1;           if (s[i].size() > s[j].size()) s[i].swap(s[j]);           s[j].insert(s[j].end(), s[i].begin(), s[i].end());           a[j] = a[i] - a[j];           if (r == p) {             s[i].swap(s[r]);             a[i] = a[r];           }           return;         }       }   }   bool toolong(Vector v) { return v.len() > LIM; }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i].x >> a[i].y;       s[i].push_back(i);       sig[i] = 1;     }     if (n == 1) return cout << 1, 0;     for (int i = 1; i <= n - 2; ++i) {       combine(i);     }     if (angle(a[n - 1], a[n]) <= 90) {       for (int id : s[n]) sig[id] *= -1;     }     for (int i = 1; i <= n; ++i) {       cout << sig[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[200005];   int func(long a, long long x, long long f) {     if (a - x > 0) {       int tem;       tem = (a - x) / (f + x);       if (tem * (f + x) == (a - x))         return (a - x) / (f + x);       else         return (a - x) / (f + x) + 1;     } else       return 0;   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     long long x, f;     cin >> x >> f;     long long ans = 0;     for (int i = 0; i < n; i++) {       int tem = func(a[i], x, f);       ans += tem;     }     cout << ans * f << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   void rset();   void init_test();   void solve();   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout << fixed;     cout.precision(20);     init_test();     return 0;   }   template <typename T>   void chmin(T& a, T b) {     if (a > b) a = b;   }   template <typename T>   void chmax(T& a, T b) {     if (a < b) a = b;   }   template <typename T>   void MACRO_rdv2_Init(long long n, T& t) {     t.resize(n);   }   template <typename First, typename... Rest>   void MACRO_rdv2_Init(long long n, First& first, Rest&... rest) {     first.resize(n);     MACRO_rdv2_Init(n, rest...);   }   template <typename T>   void MACRO_rdv2_Scan(long long p, T& t) {     std::cin >> t[p];   }   template <typename First, typename... Rest>   void MACRO_rdv2_Scan(long long p, First& first, Rest&... rest) {     std::cin >> first[p];     MACRO_rdv2_Scan(p, rest...);   }   template <typename T>   void wrv(const vector<T>& v) {     for (long long(__ii) = (0); (__ii) < (((long long)v.size())); ++(__ii)) {       if (__ii) cout <<    ;       cout << v[__ii];     }     cout <<   n ;   }   template <typename T>   void wrm(const vector<vector<T>>& v) {     for (long long(__ii) = (0); (__ii) < (((long long)v.size())); ++(__ii)) {       for (long long(__jj) = (0); (__jj) < (v[__ii].size()); ++(__jj)) {         if (__jj) cout <<    ;         cout << v[__ii][__jj];       }       cout <<   n ;     }   }   template <typename T>   void sc(T& x) {     cin >> x;   }   template <typename Head, typename... Tail>   void sc(Head& head, Tail&... tail) {     cin >> head;     sc(tail...);   }   template <typename T>   void wr(const T& x) {     cout << x <<   n ;   }   template <typename Head, typename... Tail>   void wr(const Head& head, const Tail&... tail) {     cout << head <<    ;     wr(tail...);   }   template <typename T>   void wrf(const T& x) {     cout << x << endl;   }   template <typename Head, typename... Tail>   void wrf(const Head& head, const Tail&... tail) {     cout << head <<    ;     wrf(tail...);   }   template <typename T>   void debug_out(const T& x) {     cerr << x <<   n ;   }   template <typename Head, typename... Tail>   void debug_out(const Head& head, const Tail&... tail) {     cerr << head <<    ;     debug_out(tail...);   }   template <typename... T>   void err(const T&... cod) {     wr(cod...);     exit(0);   }   void solve() {     long long n;     sc(n);     vector<long long> a(n);     for (long long(__ii) = (0); (__ii) < (n); ++(__ii)) cin >> a[__ii];     if (accumulate(a.begin(), a.end(), 0LL) % n != 0) {       wr(-1);       return;     };     long long s = accumulate(a.begin(), a.end(), 0LL) / n;     vector<pair<pair<long long, long long>, long long>> ans;     for (long long(i) = (1); (i) < (n); ++(i)) {       if (a[i] % (i + 1)) {         long long k = (i + 1) - (a[i] % (i + 1));         ans.push_back(make_pair(make_pair(1, i + 1), k));         a[i] += k;         a[0] -= k;       }       long long k = a[i] / (i + 1);       ans.push_back(make_pair(make_pair(i + 1, 1), k));       a[0] += k * (i + 1);       a[i] -= k * (i + 1);     }     for (long long(i) = (1); (i) < (n); ++(i)) {       ans.push_back(make_pair(make_pair(1, i + 1), s));       a[0] -= s;       a[i] += s;     }     wr(((long long)ans.size()));     for (long long(i) = (0); (i) < (((long long)ans.size())); ++(i)) {       wr(ans[i].first.first, ans[i].first.second, ans[i].second);     }   }   void init_test() {     long long qq = 1;     cin >> qq;     while (qq--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 500100;   long long n, m, de, dt, h;   int main() {     int a, b, c;     int x, y, z;     cin >> a >> b >> c;     cin >> x >> y >> z;     int m1 = -(x - a);     int m2 = -(y - b);     int m3 = -(z - c);     int neg = 0, pos = 0;     if (m1 < 0)       neg += m1;     else       pos += m1 - m1 % 2;     if (m2 < 0)       neg += m2;     else       pos += m2 - m2 % 2;     if (m3 < 0)       neg += m3;     else       pos += m3 - m3 % 2;     if (-neg * 2 > pos)       printf( No n );     else       printf( Yes n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 10;   vector<int> g[N];   int c[N], val[N], ans[N];   void merge(vector<int> &vc, vector<int> &tmp) {     for (auto u : tmp) vc.push_back(u);   }   vector<int> dfs(int u) {     vector<int> vc, tmp;     for (auto v : g[u]) {       tmp = dfs(v);       merge(vc, tmp);     }     if (c[u] > vc.size()) {       printf( NO n );       exit(0);     }     vc.insert(vc.begin() + c[u], u);     return vc;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1, u; i <= n; i++) {       val[i] = 1;       scanf( %d%d , &u, &c[i]);       g[u].push_back(i);     }     vector<int> vc = dfs(0);     for (int i = 0; i < vc.size(); i++) {       ans[vc[i]] = i + 1;     }     printf( YES n );     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9, SIZE = 1e5 * 2;   vector<long long> vec;   long long dp[100][SIZE], tree[SIZE * 8], pushing[SIZE * 8], prevPos[SIZE],       last[SIZE];   void push(int v) {     tree[v] += pushing[v];     pushing[v * 2 + 1] += pushing[v];     pushing[v * 2 + 2] += pushing[v];     pushing[v] = 0;   }   void build(int v, int l, int r, int k) {     if (l == r - 1) {       tree[v] = dp[k][l];     } else {       int mid = (l + r) / 2;       build(v * 2 + 1, l, mid, k);       build(v * 2 + 2, mid, r, k);       tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]);     }   }   void upd(int v, int l, int r, int askl, int askr) {     push(v);     if (l >= askr || r <= askl) return;     if (l >= askl && r <= askr) {       pushing[v]++;       push(v);       return;     }     int mid = (l + r) / 2;     upd(v * 2 + 1, l, mid, askl, askr);     upd(v * 2 + 2, mid, r, askl, askr);     tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]);   }   long long get(int v, int l, int r, int askl, int askr) {     push(v);     if (l >= askr || r <= askl) return 0;     if (l >= askl && r <= askr) {       return tree[v];     }     int mid = (l + r) / 2;     return max(get(v * 2 + 1, l, mid, askl, askr),                get(v * 2 + 2, mid, r, askl, askr));   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     ;     long long n, k;     cin >> n >> k;     vec.resize(n);     for (int i = 0; i < n; i++) cin >> vec[i];     set<long long> vals;     long long ans = 0;     for (int i = 0; i <= n; i++) {       last[i] = 0;     }     for (int i = 0; i < n; i++) {       vals.insert(vec[i]);       dp[0][i] = vals.size();       prevPos[i] = last[vec[i]];       last[vec[i]] = i;       ans = max(ans, dp[0][i]);     }     for (int j = 1; j < k; j++) {       for (int i = 0; i < SIZE; i++) {         tree[i] = 0;         pushing[i] = 0;       }       build(0, 0, n, j - 1);       for (int i = 0; i < n; i++) {         upd(0, 0, n, prevPos[i], i);         dp[j][i] = max(dp[j - 1][i], get(0, 0, n, 0, i));         ans = max(ans, dp[j][i]);       }       cout <<   ;     }     cout << dp[k - 1][n - 1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long k;     string n;     cin >> k >> n;     long long sum = 0;     vector<long long> arr;     for (int(i) = 0; (i) < ((int)(n).size()); (i)++) {       sum += int(n[i] -  0 );       arr.push_back(int(n[i] -  0 ));     }     if (sum >= k) {       printf( 0 );       return 0;     } else {       sort((arr).begin(), (arr).end());       long long col = 0;       for (int(i) = 0; (i) < ((int)(arr).size()); (i)++) {         sum -= arr[i];         sum += 9;         col++;         if (sum >= k) {           printf( %lld , col);           return 0;         }       }     }     return 0;   }


#include <bits/stdc++.h>   const int INF = 0x3f3f3f3f;   const int maxn = 1e5 + 5;   const int mod = 1e9 + 7;   const double eps = 1e-7;   using namespace std;   long long ans, n, s, a[maxn], b[maxn], num1, num2;   priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,                  greater<pair<long long, long long> > >       to1, to2;   void init() {     ans = 0;     num1 = 0;     num2 = 0;     while (to1.size()) to1.pop();     while (to2.size()) to2.pop();     memset(a, 0, sizeof(a)), memset(b, 0, sizeof(b));   }   int main() {     while (~scanf( %lld%lld , &n, &s)) {       init();       for (int i = 0; i <= n - 1; ++i) {         long long s, a, b;         scanf( %lld%lld%lld , &s, &a, &b);         if (a > b) {           num1 += s;           ans += s * a;           to2.push(pair<long long, long long>(a - b, s));         } else {           num2 += s;           ans += s * b;           to1.push(pair<long long, long long>(b - a, s));         }       }       num1 %= s, num2 %= s;       if (num1 + num2 > s) {         printf( %lld n , ans);         continue;       } else {         long long tmp1 = num1, tmp2 = num2, cost1 = 0, cost2 = 0;         while (tmp1) {           pair<long long, long long> tmp = to2.top();           to2.pop();           long long num = min(tmp1, tmp.second);           tmp1 -= num;           cost1 += num * tmp.first;         }         while (tmp2) {           pair<long long, long long> tmp = to1.top();           to1.pop();           int num = min(tmp2, tmp.second);           tmp2 -= num;           cost2 += num * tmp.first;         }         printf( %lld n , max(ans - cost1, ans - cost2));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:102400000,102400000 )   long long n, k, m, d;   int main() {     cin >> n >> k >> m >> d;     long long ans = min(n, m);     for (long long i = 1; i <= d; i++) {       long long tmp = (i - 1) * k + 1;       if (tmp <= 0) continue;       long long x = n / tmp;       if (x > m) x = m;       if (x * (i - 1) > (n - x) / k) continue;       long long res = x * (i - 1) + x;       if (res > ans) {         ans = res;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long a[n];     for (int i = 0; i < n; ++i) {       cin >> a[i];       while (a[i] % 2 == 0) a[i] /= 2;       while (a[i] % 3 == 0) a[i] /= 3;     }     int flag = 0;     for (int i = 1; i < n; ++i) {       if (a[i] != a[0]) {         flag = 1;         break;       }     }     if (flag == 1)       cout <<  NO ;     else       cout <<  YES ;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 2e5 + 10;   int a[mx];   vector<int> g[mx];   vector<int> lyr[mx];   bool cmp(int i, int j) { return g[i].size() < g[j].size(); }   int main() {     int n, t, k;     scanf( %d %d %d , &n, &t, &k);     a[0] = 1;     for (int i = 1; i <= t; i++) scanf( %d , a + i);     int from = 0;     int leafs = a[t];     for (int i = 0; i < t; i++) {       int to = from + a[i] - 1;       int l = to + 1;       int r = l + a[i + 1] - 1;       for (int x = 0; x < min(a[i], a[i + 1]); x++) g[from + x].push_back(l + x);       if (a[i] < a[i + 1]) {         for (int x = 0; x < a[i + 1] - a[i]; x++) {           g[to].push_back(r - x);         }       } else         leafs += a[i] - a[i + 1];       for (int x = from; x <= to; x++) lyr[i].push_back(x);       sort(lyr[i].begin(), lyr[i].end(), cmp);       from = l;     }     for (int i = 1; i < t; i++) {       int to = lyr[i].back();       for (int u : lyr[i])         if (u != to && !g[u].empty() && leafs < k) {           for (int v : g[u]) g[to].push_back(v);           g[u].clear();           leafs++;         }     }     if (leafs != k) {       puts( -1 );       return 0;     }     printf( %d n , n);     for (int i = 0; i < n; i++)       for (int j : g[i]) printf( %d %d n , i + 1, j + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a >> b;     int res = -1, pos = -1, j = 0;     for (int i = 0; i < b.size(); i++) {       if (a[j] != b[i]) {         pos = j;         if (res != -1 || a[j + 1] != b[i]) {           res = 0;           break;         }         res = 1;         j++;       }       j++;     }     if (res == -1) res = 1, pos = a.size() - 1;     if (res) {       vector<int> v;       v.push_back(pos);       while (pos > 0 && a[--pos] == a[v[0]]) v.push_back(pos);       cout << v.size() << endl;       for (int i = v.size() - 1; i >= 0; i--) cout << v[i] + 1 <<    ;       cout << endl;     } else       cout << 0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> v;     int n, A, B;     cin >> n >> A >> B;     int msum = 0;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       v.push_back(x);       msum += x;     }     int one = v[0];     v[0] = INT_MAX;     sort(v.begin(), v.end());     int x = n;     int sum = msum;     int res = 0;     for (int i = n - 2; i >= 0; i--) {       if (((one * A) / sum) >= B) {         cout << res << endl;         return 0;       }       sum -= v[i];       res++;     }     cout << n - 1 << endl;   }