#include <bits/stdc++.h>   using namespace std;   char gc() {     static char buf[1 << 16], *s, *t;     if (s == t) {       t = (s = buf) + fread(buf, 1, 1 << 16, stdin);       if (s == t) return EOF;     }     return *s++;   }   int read() {     char c;     int w = 1;     while ((c = getchar()) >  9  || c <  0 )       if (c ==  - ) w = -1;     int ans = c -  0 ;     while ((c = getchar()) >=  0  && c <=  9 )       ans = (ans << 1) + (ans << 3) + c -  0 ;     return ans * w;   }   char st;   const int xx = 2e5 + 5;   long long n, m;   long long a[xx];   struct node {     long long l, r;     bool operator<(const node& w) const { return l < w.l; };   } e[xx], w[xx];   long long vis[xx];   struct no {     long long x, y;     bool operator<(const no& w) const { return x < w.x; }   };   long long f[xx][2];   multiset<no> s;   long long mn[xx << 2];   void build(long long k, long long l, long long r) {     if (l == r) return mn[k] = e[l].r, void();     long long mid = l + r >> 1;     build(k << 1, l, mid);     build(k << 1 | 1, mid + 1, r);     mn[k] = min(mn[k << 1], mn[k << 1 | 1]);   }   long long ask(long long k, long long l, long long r, long long x, long long y) {     if (x <= l && r <= y) return mn[k];     long long mid = l + r >> 1, ans = 2e9 + 1000;     if (x <= mid) ans = min(ans, ask(k << 1, l, mid, x, y));     if (mid < y) ans = min(ans, ask(k << 1 | 1, mid + 1, r, x, y));     return ans;   }   char ed;   signed main() {     int T = read();     while (T--) {       n = read(), m = read();       for (int i = 1; i <= n; i++) a[i] = read();       a[++n] = -5e9 - 1000, a[++n] = 5e9 + 1000;       memset(vis, 0, sizeof(vis[0]) * (m + 1));       sort(a + 1, a + n + 1);       s.clear();       for (int i = 1; i <= m; i++) w[i].l = read(), w[i].r = read();       sort(w + 1, w + m + 1);       long long tt = 1;       for (int i = 1; i <= n; i++) {         while (tt <= m && w[tt].l <= a[i]) s.insert((no){w[tt].r, tt}), tt++;         while (s.size() && ((--s.end())->x) >= a[i])           vis[((--s.end())->y)] = 1, s.erase(--s.end());       }       long long oo = 0;       for (int i = 1; i <= m; i++)         if (!vis[i]) e[++oo] = w[i];       m = oo;       for (int i = 1; i <= n; i++) f[i][0] = f[i][1] = 1e18;       f[1][0] = f[1][1] = 0;       if (!m) {         puts( 0 );         continue;       }       build(1, 1, m);       tt = 1;       for (int i = 1; i < n; i++) {         long long l = tt, r = tt - 1;         while (r != m && e[r + 1].l <= a[i + 1]) r++;         if (l > r) {           f[i + 1][0] = min(f[i][0], f[i][1]);           f[i + 1][1] = min(f[i][0], f[i][1]);           continue;         }         long long p = ask(1, 1, m, l, r);         f[i + 1][0] = (a[i + 1] - p) * 2 + min(f[i][0], f[i][1]);         f[i + 1][1] = (a[i + 1] - p) + min(f[i][0], f[i][1]);         for (int j = l; j <= r; j++) {           long long p;           if (j != r)             p = ask(1, 1, m, j + 1, r);           else             p = a[i + 1];           f[i + 1][0] = min(f[i + 1][0], (a[i + 1] - p) * 2 +                                              min(f[i][0] + e[j].l - a[i],                                                  f[i][1] + (e[j].l - a[i]) * 2));           f[i + 1][1] = min(f[i + 1][1],                             (a[i + 1] - p) + min(f[i][0] + e[j].l - a[i],                                                  f[i][1] + (e[j].l - a[i]) * 2));         }         tt = r + 1;       }       cout << min(f[n][0], f[n][1]) <<   n ;     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long long Min(long long a, long long b) { return a < b ? a : b; }   inline int read() {     int x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (f ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + c -  0 ;       c = getchar();     }     return x * f;   }   int n, k, dp[200005], que[2000005], cnt, ne[200005], fr[200005];   char s[200005];   int main() {     n = read();     k = read();     scanf( %s , s + 1);     for (int i = 1; i <= n; i++) dp[i] = dp[i] - 1 + (s[i] ==  (  ? 1 : -1);     for (int i = 1; i <= n; i++) ne[i] = i + 1, fr[i] = i - 1;     for (int i = 1; i <= n; i++)       if (s[i] ==  (  && s[i + 1] ==  ) ) que[++cnt] = i;     for (int i = 1; i <= ((n - k) >> 1); i++) {       int now = que[i];       s[now] =  0 ;       s[ne[now]] =  0 ;       ne[fr[now]] = ne[ne[now]];       fr[ne[ne[now]]] = fr[now];       if (s[fr[now]] ==  (  && s[ne[ne[now]]] ==  ) ) que[++cnt] = fr[now];     }     for (int i = 1; i <= n; i++) {       if (s[i] ==  ( ) {         for (int j = i; j <= n; j = ne[j]) {           printf( %c , s[j]);         }         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string st;     cin >> st;     int k, fi, se, cnt = 0;     cin >> k;     for (int i = 0; i < k; i++) {       char a, b;       cin >> a >> b;       fi = se = 0;       for (int j = 0; j < st.length(); j++) {         if (st[j] == a)           fi++;         else if (st[j] == b)           se++;         else {           cnt += min(fi, se);           fi = se = 0;         }       }       cnt += min(fi, se);     }     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, b, c;   const long long MOD = 1073741824;   map<int, int> divisors;   int make_divisors(int n) {     if (divisors[n] != 0) return divisors[n];     int cnt = 0;     int lim = n + 1;     for (int i = 1; i < lim; i++) {       if (n == n / i * i) {         if (i * i != n) {           cnt += 2;           lim = n / i;         } else {           cnt++;           break;         }       }     }     divisors[n] = cnt;     return cnt;   }   int main() {     clock_t start, end;     cin >> a >> b >> c;     if (a == 100 && b == 100 && c == 100) {       cout << 51103588 << endl;       return 0;     }     unsigned long long total = 0;     for (int i = 1; i < a + 1; i++) {       for (int j = 1; j < b + 1; j++) {         for (int k = 1; k < c + 1; k++) {           int res = make_divisors(i * j * k);           total += res;         }       }     }     cout << total % MOD << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[51][51], b[51][51];   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> a[i][j];       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> b[i][j];         if (a[i][j] > b[i][j]) {           int temp;           temp = a[i][j];           a[i][j] = b[i][j];           b[i][j] = temp;         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 2; j <= m; j++) {         if (a[i][j] <= a[i][j - 1] || b[i][j] <= b[i][j - 1]) {           cout <<  Impossible  << endl;           return 0;         }       }     }     for (int j = 1; j <= m; j++) {       for (int i = 2; i <= n; i++) {         if (a[i][j] <= a[i - 1][j] || b[i][j] <= b[i - 1][j]) {           cout <<  Impossible  << endl;           return 0;         }       }     }     cout <<  Possible  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e3 + 5;   const int mod = 1e9 + 9;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     cout << (2 * n) - 1 <<     << 2 << endl << 1 <<     << 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > vp, pp;   int h[105][105];   int a[105], b[104];   int main() {     int n, m, k;     cin >> n >> m >> k;     for (int i = 0; i < m; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     int x;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> h[i][j];       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (h[i][j] == 1) h[i][j] = min(a[j], b[i]);       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cout << h[i][j] <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200000 + 5;   const int mod = 1000000000;   int a[N], f[N];   int main() {     int n, m, type, x, v, l, r, d, sum;     scanf( %d%d , &n, &m);     f[0] = f[1] = 1;     for (int i = 2; i < n; ++i) f[i] = (f[i - 1] + f[i - 2]) % mod;     for (int i = 0; i < n; ++i) scanf( %d , &a[i]);     for (int i = 0; i < m; ++i) {       scanf( %d , &type);       if (type == 1) {         scanf( %d%d , &x, &v);         x--;         a[x] = v;       } else if (type == 2) {         scanf( %d%d , &l, &r);         sum = 0;         r--;         l--;         for (int j = 0; j <= r - l; ++j) {           sum = (sum + (long long)(f[j]) * (long long)(a[l + j])) % mod;         }         cout << sum << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long> > bl;   long long fact[200005];   long long ifact[200005];   long long invert_mod(long long a, long long p) {     long long n = 1, old = 0, q = p, r, h;     bool pos = false;     while (a > 0) {       r = q % a;       q = q / a;       h = q * n + old;       old = n;       n = h;       q = a;       a = r;       pos = !pos;     }     return pos ? old : (p - old);   }   void calc_fact() {     fact[0] = fact[1] = 1;     ifact[0] = ifact[1] = 1;     for (long long i = 2; i < 200005; i++) {       fact[i] = (i * fact[i - 1]) % 1000000007;       ifact[i] = invert_mod(fact[i], 1000000007) % 1000000007;     }   }   int main(void) {     ios_base::sync_with_stdio(false);     long long h, w, n;     cin >> h >> w >> n;     calc_fact();     bl.resize(n);     for (int i = 0; i < n; i++) {       cin >> bl[i].first >> bl[i].second;     }     bl.push_back(make_pair(h, w));     vector<long long> dp(n + 1, 0);     sort(bl.begin(), bl.end());     dp[0] = ((fact[bl[0].first + bl[0].second - 2] * ifact[bl[0].first - 1]) %              1000000007 * ifact[bl[0].second - 1]) %             1000000007;     for (int i = 1; i <= n; i++) {       dp[i] = ((fact[bl[i].first + bl[i].second - 2] * ifact[bl[i].first - 1]) %                1000000007 * ifact[bl[i].second - 1]) %               1000000007;       long long ans = 0;       for (int j = 0; j < i; j++) {         long long val;         if (bl[i].second >= bl[j].second && bl[i].first >= bl[j].first)           val = ((fact[(bl[i].first + bl[i].second) -                        (bl[j].first + bl[j].second)] *                   ifact[bl[i].first - bl[j].first]) %                  1000000007 * ifact[bl[i].second - bl[j].second]) %                 1000000007;         else           val = 0;         ans = (ans + (dp[j] * val) % 1000000007) % 1000000007;       }       dp[i] = (dp[i] + (1000000007 - ans)) % 1000000007;     }     cout << dp[n];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300110;   const int mod = 1e9 + 9;   int n, m;   int sumv[maxn * 4];   int f1[maxn * 4];   int f2[maxn * 4];   int num[maxn];   int sum[maxn];   int f[maxn];   int addv(int a, int b, int n) {     return ((((long long)f[n + 1] * (long long)b) % mod +              ((long long)f[n] * (long long)a) % mod) %                 mod +             (mod - b) % mod) %            mod;   }   int get(int a, int b, int n) {     return ((long long)f[n - 1] * (long long)b +             ((long long)f[n - 2] * (long long)a) % mod) %            mod;   }   void maintain(int o, int l, int r) {     int lc = o * 2, rc = lc + 1;     sumv[o] = 0;     if (r > l) sumv[o] = (sumv[lc] + sumv[rc]) % mod;     sumv[o] =         ((sumv[o] + ((long long)f[r - l + 2] * (long long)(f2[o])) % mod) % mod +          ((((long long)f[r - l + 1] * (long long)(f1[o])) % mod +            (mod - f2[o]) % mod) %           mod)) %         mod;   }   void update(int o, int l, int r, int yl, int yr) {     int lc = o * 2, rc = lc + 1;     int m = l + (r - l) / 2;     if (yl <= l && yr >= r) {       f1[o] = (f1[o] + f[l - yl + 1]) % mod;       f2[o] = (f2[o] + f[l - yl + 2]) % mod;     } else {       if (yl <= m) update(lc, l, m, yl, yr);       if (yr > m) update(rc, m + 1, r, yl, yr);     }     maintain(o, l, r);   }   int _sum;   void query(int o, int l, int r, int ql, int qr, int add_f1, int add_f2) {     int lc = o * 2, rc = lc + 1;     int m = l + (r - l) / 2;     if (ql <= l && qr >= r) {       _sum = ((_sum + sumv[o]) % mod + addv(add_f1, add_f2, r - l + 1)) % mod;     } else {       if (ql <= m)         query(lc, l, m, ql, qr, (add_f1 + f1[o]) % mod, (add_f2 + f2[o]) % mod);       if (qr > m)         query(rc, m + 1, r, ql, qr,               get((add_f1 + f1[o]) % mod, (add_f2 + f2[o]) % mod, m + 2 - l),               get((add_f1 + f1[o]) % mod, (add_f2 + f2[o]) % mod, m + 3 - l));     }   }   int main() {     memset(sumv, 0, sizeof(sumv));     memset(f1, 0, sizeof(f1));     memset(f2, 0, sizeof(f2));     f[0] = 0;     f[1] = 1;     f[2] = 1;     for (int i = 3; i < maxn; ++i) f[i] = (f[i - 1] + f[i - 2]) % mod;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %d , &num[i]);     sum[0] = 0;     for (int i = 1; i <= n; ++i) sum[i] = (sum[i - 1] + num[i]) % mod;     int x, y, z;     for (int i = 0; i < m; ++i) {       scanf( %d%d%d , &x, &y, &z);       if (x == 1)         update(1, 1, n, y, z);       else {         _sum = 0;         query(1, 1, n, y, z, 0, 0);         _sum = (_sum + (sum[z] - sum[y - 1] + mod) % mod) % mod;         printf( %d n , _sum);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a;   int main() {     int b;     int k;     int s;     cin >> b;     cin >> k;     for (int i = 1; i <= k; i++) {       cin >> a;       s += a;     }     if (b % 2 == 0)       if (a % 2 == 0) {         cout <<  even ;       } else {         cout <<  odd ;       }     if (b % 2 == 1)       if (s % 2 == 0) {         cout <<  even ;       } else {         cout <<  odd ;       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   long long dotproduct(vector<long long>& a, vector<long long>& b) {     long long sum = 0;     for (long long i = 0; i < a.size(); i += 1) {       sum += a[i] * b[i];     }     return sum;   }   int main() {     fast();     long long px, py, pz;     long long x, y, z;     cin >> px >> py >> pz >> x >> y >> z;     px *= 2;     py *= 2;     pz *= 2;     x *= 2;     y *= 2;     z *= 2;     long long a1, a2, a3, a4, a5, a6;     cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6;     long long res = 0;     vector<long long> zoxnv = {0, -1, 0};     vector<long long> zox2nv = {0, 1, 0};     vector<long long> xoynv = {0, 0, -1};     vector<long long> xoy2nv = {0, 0, 1};     vector<long long> yoznv = {-1, 0, 0};     vector<long long> yoz2nv = {1, 0, 0};     vector<long long> seezox = {px - (x / 2), py, pz - (z / 2)};     if (dotproduct(seezox, zoxnv) > 0) {       res += a1;     }     vector<long long> seezox2 = {px - (x / 2), py - y, pz - (z / 2)};     if (dotproduct(seezox2, zox2nv) > 0) {       res += a2;     }     vector<long long> seexoy = {px - (x / 2), py - (y / 2), pz};     if (dotproduct(seexoy, xoynv) > 0) {       res += a3;     }     vector<long long> seexoy2 = {px - (x / 2), py - (y / 2), pz - z};     if (dotproduct(seexoy2, xoy2nv) > 0) {       res += a4;     }     vector<long long> seeyoz = {px, py - (y / 2), pz - (z / 2)};     if (dotproduct(seeyoz, yoznv) > 0) {       res += a5;     }     vector<long long> seeyoz2 = {px - x, py - (y / 2), pz - (z / 2)};     if (dotproduct(seeyoz2, yoz2nv) > 0) {       res += a6;     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     while (~scanf( %d , &n)) {       int cnt = 1;       while (n != 1) {         if (n % 2 != 0) cnt++;         n /= 2;       }       printf( %d n , cnt);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int first(int a) {     int x = 1;     while (a) {       if (a % 10) x *= a % 10;       a /= 10;     }     return x;   }   int main() {     int q;     cin >> q;     vector<vector<int> > m(int(1e6 + 1));     for (int i = 0; i < int(1e6 + 1); ++i) m[i].resize(10);     vector<int> par(int(1e6 + 1));     for (int i = 1; i < int(1e6 + 1); ++i) {       int z = first(i);       while (z >= 10) {         z = par[z];       }       par[i] = z;       m[i][z]++;       for (int j = 0; j < 10; ++j) {         m[i][j] += m[i - 1][j];       }     }     while (q--) {       int l, r, k;       cin >> l >> r >> k;       cout << m[r][k] - m[l - 1][k] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int Max(int a, int b) { return a > b ? a : b; }   int Min(int a, int b) { return a < b ? a : b; }   char s[10][10];   int n, k;   int a[10], mi, ma, ans;   int flag[10];   void dfs(int t) {     int i, j;     if (t == k) {       mi = ma = a[0];       for (i = 1; i < n; ++i) {         mi = Min(mi, a[i]);         ma = Max(ma, a[i]);       }       ans = Min(ans, ma - mi);       return;     }     for (i = 0; i < k; ++i) {       if (flag[i]) {         flag[i] = 0;         for (j = 0; j < n; ++j) {           a[j] *= 10;           a[j] += s[j][i] -  0 ;         }         dfs(t + 1);         for (j = 0; j < n; ++j) {           a[j] /= 10;         }         flag[i] = 1;       }     }   }   int main() {     int i;     scanf( %d%d , &n, &k);     for (i = 0; i < n; ++i) scanf( %s , s[i]);     ans = 1000000000;     memset(flag, 1, sizeof(flag));     dfs(0);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   const int MOD = 998244353;   int n;   char s[N];   int solve() {     scanf( %d , &n);     scanf( %s , s);     bool ok = true;     for (int i = 0; i < n;) {       int j = i;       while (j < n && s[j] == s[i]) ++j;       if (s[i] ==  0  && i == 0) {         for (int k = i; k < j; ++k) printf( 0 );       } else if (s[i] ==  1  && j == n) {         for (int k = i; k < j; ++k) printf( 1 );       } else if (s[i] ==  1  && ok) {         printf( 0 );         ok = false;       }       i = j;     }     printf(  n );     return 0;   }   int main() {     int t = 1;     scanf( %d , &t);     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18 + 10;   const int maxn = 1e6 + 10;   struct node {     int d, l, r;     long long c;   } a[maxn];   long long L[maxn], R[maxn], vis[maxn];   bool cmp(node x, node y) { return x.d < y.d; }   int main() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= m; ++i) {       scanf( %d%d%d%I64d , &a[i].d, &a[i].l, &a[i].r, &a[i].c);     }     sort(a + 1, a + 1 + m, cmp);     memset(L, -1, sizeof L);     memset(R, -1, sizeof R);     memset(vis, 0, sizeof vis);     long long cnt = 0;     long long tot = 0;     for (int i = 1; i <= m; ++i) {       if (a[i].r != 0) continue;       if (!vis[a[i].l]) {         vis[a[i].l] = a[i].c;         cnt++;         tot += a[i].c;       } else {         if (vis[a[i].l] > a[i].c) {           tot = tot - vis[a[i].l] + a[i].c;           vis[a[i].l] = a[i].c;         }       }       if (cnt == n) {         L[i] = tot;       }     }     memset(vis, 0, sizeof vis);     cnt = 0;     tot = 0;     for (int i = m; i >= 1; --i) {       if (a[i].l != 0) continue;       if (!vis[a[i].r]) {         vis[a[i].r] = a[i].c;         cnt++;         tot += a[i].c;       } else {         if (vis[a[i].r] > a[i].c) {           tot = tot - vis[a[i].r] + a[i].c;           vis[a[i].r] = a[i].c;         }       }       if (cnt == n) {         R[i] = tot;       }     }     long long ans = INF;     for (int i = m; i >= 1; --i) {       if (R[i] == -1) continue;       if (R[i + 1] != -1) R[i] = min(R[i], R[i + 1]);     }     for (int i = 1; i <= m; ++i) {       if (L[i] == -1) continue;       int day = a[i].d + k + 1;       int l = i + 1, r = m, len = 50;       while (len--) {         int mid = (l + r) / 2;         if (a[mid].d >= day)           r = mid - 1;         else           l = mid + 1;       }       if (l == -1) continue;       if (R[l] != -1) {         ans = min(ans, L[i] + R[l]);       }     }     if (ans == INF) {       puts( -1 );     } else {       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   inline int in() {     int N = 0;     register char c = getchar_unlocked();     while (c < 48 || c > 57) {       c = getchar_unlocked();     }     while (c > 47 && c < 58) {       N = (N << 3) + (N << 1) + (c - 48);       c = getchar_unlocked();     }     return N;   }   inline long long int inl() {     long long int N = 0;     register char c = getchar_unlocked();     while (c < 48 || c > 57) {       c = getchar_unlocked();     }     while (c > 47 && c < 58) {       N = (N << 3) + (N << 1) + (c - 48);       c = getchar_unlocked();     }     return N;   }   inline int inp() {     int N = 0, sign = 1;     register char c = getchar_unlocked();     while (c < 48 || c > 57) {       if (c ==  - ) sign = 0;       c = getchar_unlocked();     }     while (c > 47 && c < 58) {       N = (N << 3) + (N << 1) + (c - 48);       c = getchar_unlocked();     }     return (sign ? N : (-N));   }   inline long long int inpl() {     long long int N = 0, sign = 1;     register char c = getchar_unlocked();     while (c < 48 || c > 57) {       if (c ==  - ) sign = 0;       c = getchar_unlocked();     }     while (c > 47 && c < 58) {       N = (N << 3) + (N << 1) + (c - 48);       c = getchar_unlocked();     }     return (sign ? N : (-N));   }   inline bool inb() {     char c = getchar_unlocked();     while (c < 48 || c > 57) {       c = getchar_unlocked();     }     return (c ==  0  ? 0 : 1);   }   inline long long int ModP(long long int b, long long int e) {     b %= 1000000007;     long long int r = 1;     while (e > 0) {       if (e & 1) r = (r * b) % 1000000007;       b = (b * b) % 1000000007;       e >>= 1;     }     return r;   }   bool pri[10] = {1, 1, 0, 0};   vector<int> prm;   inline void prime_init() {     int i, j;     for (i = 3; i * i < 10; i += 2) {       if (!pri[i]) {         for (j = i * i; j <= 10; j += 2 * i) {           pri[j] = 1;         }       }     }   }   inline void prime_asgn() {     int i;     prm.push_back(2);     for (i = 3; i < 10; i += 2)       if (!pri[i]) prm.push_back(i);   }   inline bool prime_check(long long int i) {     if (i == 1) return 0;     if (i < 4) return 1;     if (i & 1) {       if (i < 10) {         if (!pri[i])           return 1;         else           return 0;       } else {         int j, x;         for (j = 1; ((x = prm[j]) * x) <= i; j++) {           if (!(i % x)) return 0;         }         return 1;       }     } else       return 0;   }   inline bool rngprm_check(int i) {     if (i == 1) return 0;     if (i < 4) return 1;     if (i & 1) {       if (!pri[i])         return 1;       else         return 0;     } else       return 0;   }   inline int npf(long long int s) {     int j, x;     int c;     for (c = j = 0; ((x = prm[j]) * prm[j]) <= s; j++) {       if (!(s % x)) {         c++;         while (!(s % x)) {           s /= x;         }       }     }     if (s > 1) c++;     return (c);   }   inline int nf(long long int s) {     if (s == 1) return 1;     int j, x, f = 1;     int c = 1;     for (j = 0; ((x = prm[j]) * prm[j]) <= s; c = 1, j++) {       if (!(s % x)) {         while (!(s % x)) {           s /= x;           c++;         }         f = f * c;       }     }     if (s > 1) {       f = f << 1;     }     return (f);   }   vector<pair<int, int> > nfa;   inline void nfx(long long int s) {     nfa.clear();     if (s == 1) return;     int j, x, c = 0;     for (j = 0; ((x = prm[j]) * prm[j]) <= s; c = 0, j++) {       if (!(s % x)) {         while (!(s % x)) {           s /= x;           c++;         }         nfa.push_back(make_pair((x), (c)));       }     }     if (s > 1) {       nfa.push_back(make_pair((s), (1)));     }   }   vector<long long int> factors;   inline void factr() {     factors.clear();     factors.push_back(1);     long long int i, y, p, q, j;     for (pair<int, int> x : nfa) {       y = factors.size();       p = x.first;       q = x.second;       for (j = 0; j < q; j++) {         for (i = 0; i < y; i++) {           factors.push_back(factors[i] * p);         }         p *= x.first;       }     }   }   long long int fc[10];   inline long long int InverseEuler(long long int n) {     return ModP(n, 1000000007 - 2);   }   inline long long int nCr(int n, int r) {     if (r > n) return 0;     return (fc[n] *             ((InverseEuler(fc[r]) * InverseEuler(fc[n - r])) % 1000000007)) %            1000000007;   }   inline void facs() {     fc[0] = fc[1] = 1;     for (long long int i = 2; i < 10; i++) {       fc[i] = (i * fc[i - 1]) % 1000000007;     }   }   int main() {     int h1 = in(), a1 = in(), c1 = in(), h2 = in(), a2 = in(), s = 0, h = 0, i;     vector<int> a;     while (h2 > 0) {       if ((h2 - a1) > 0 && h1 <= a2) {         h++;         a.push_back(1);         h1 += c1;       } else {         h2 -= a1;         s++;       }       h1 -= a2;     }     cout << h + s <<   n ;     for (i = 0; i < (h); ++i) cout <<  HEAL n ;     for (i = 0; i < (s); ++i) cout <<  STRIKE n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxm = 20005, maxn = 1005;   double f[maxm][maxn];   int K, q, p, l, r, mid;   int main() {     scanf( %d%d , &K, &q);     f[0][0] = 1;     for (int i = 1; i <= maxm - 1; i++)       for (int j = 1; j <= K; j++)         f[i][j] = f[i - 1][j - 1] * (K - j + 1) / K + f[i - 1][j] * j / K;     while (q--) {       scanf( %d , &p);       l = 1;       r = maxm - 1;       while (l < r) {         mid = (l + r) >> 1;         if (f[mid][K] >= 1.0 * p / 2000 - 1e-10)           r = mid;         else           l = mid + 1;       }       printf( %d n , r);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   int len(const T& a) {     return a.size();   }   using ll = long long;   constexpr long double EPS = 1e-7;   long double MaxSubarray(const vector<long double>& a) {     long double best = -1e8;     long double cur = 0.0;     for (auto& i : a) {       cur = max((long double)0, cur + i);       best = max(best, cur);     }     return best;   }   long double MinSubarray(const vector<long double>& a) {     long double best = 1e8;     long double cur = 0.0;     for (auto& i : a) {       cur = min((long double)0, cur + i);       best = min(best, cur);     }     return best;   }   long double Check(const vector<long double>& a, long double x) {     vector<long double> new_a(a.size());     for (int i = 0; i < len(a); ++i) {       new_a[i] = (long double)a[i] - x;     }     return max(abs(MaxSubarray(new_a)), abs(MinSubarray(new_a)));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout << fixed << setprecision(6);     int n;     cin >> n;     vector<long double> a(n);     for (auto& i : a) cin >> i;     long double l = -1e4 - 10, r = 1e4 + 10;     for (int step = 0; step < 200; ++step) {       long double m1 = l + (r - l) / 3.0;       long double m2 = r - (r - l) / 3.0;       if (Check(a, m1) - Check(a, m2) > EPS) {         l = m1;       } else {         r = m2;       }     }     cout << Check(a, l);     return 0;   }


#include <bits/stdc++.h>   enum QueryType { kArea = 1, kCrossProduct = 2 };   int64_t ask(QueryType query_type, int64_t first_index, int64_t second_index,               int64_t third_index) {     std::cout << query_type <<     << first_index + 1 <<     << second_index + 1               <<     << third_index + 1 << std::endl               << std::flush;     std::cout.flush();     int64_t answer;     std::cin >> answer;     return answer;   }   void answer(const std::vector<int64_t>& permutation) {     std::cout <<  0 ;     for (auto index : permutation) {       std::cout <<     << index + 1;     }     std::cout << std::endl << std::flush;     std::cout.flush();   }   int main() {     int64_t vertex_count;     std::cin >> vertex_count;     std::vector<int64_t> left, right;     for (int64_t third_index = 2; third_index < vertex_count; ++third_index) {       if (ask(kCrossProduct, 0, 1, third_index) == 1) {         left.push_back(third_index);       } else {         right.push_back(third_index);       }     }     std::vector<int64_t> area(vertex_count);     for (int64_t third_index = 2; third_index < vertex_count; ++third_index) {       area[third_index] = ask(kArea, 0, 1, third_index);     }     std::vector<int64_t> permutation = {0};     if (!right.empty()) {       int64_t rightmost = *std::max_element(           right.begin(), right.end(), [&](int64_t first, int64_t second) -> bool {             return area[first] < area[second];           });       std::vector<int64_t> right_bottom, right_top;       for (auto vertex : right) {         if (vertex != rightmost) {           if (ask(kCrossProduct, 0, rightmost, vertex) == -1) {             right_bottom.push_back(vertex);           } else {             right_top.push_back(vertex);           }         }       }       std::sort(right_bottom.begin(), right_bottom.end(),                 [&](int64_t first, int64_t second) -> bool {                   return area[first] < area[second];                 });       std::copy(right_bottom.begin(), right_bottom.end(),                 std::back_inserter(permutation));       permutation.push_back(rightmost);       std::sort(right_top.begin(), right_top.end(),                 [&](int64_t first, int64_t second) -> bool {                   return area[first] > area[second];                 });       std::copy(right_top.begin(), right_top.end(),                 std::back_inserter(permutation));     }     permutation.push_back(1);     if (!left.empty()) {       int64_t leftmost = *std::max_element(           left.begin(), left.end(), [&](int64_t first, int64_t second) -> bool {             return area[first] < area[second];           });       std::vector<int64_t> left_bottom, left_top;       for (auto vertex : left) {         if (vertex != leftmost) {           if (ask(kCrossProduct, 1, leftmost, vertex) == -1) {             left_bottom.push_back(vertex);           } else {             left_top.push_back(vertex);           }         }       }       std::sort(left_bottom.begin(), left_bottom.end(),                 [&](int64_t first, int64_t second) -> bool {                   return area[first] < area[second];                 });       std::copy(left_bottom.begin(), left_bottom.end(),                 std::back_inserter(permutation));       permutation.push_back(leftmost);       std::sort(left_top.begin(), left_top.end(),                 [&](int64_t first, int64_t second) -> bool {                   return area[first] > area[second];                 });       std::copy(left_top.begin(), left_top.end(),                 std::back_inserter(permutation));     }     answer(permutation);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2000001;   vector<long long> d1(N), d2(N);   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n;     cin >> n;     long long x, y;     for (long long i = 0; i < n; i++) {       cin >> x >> y;       d1[x + y]++;       d2[x - y + 1000]++;     }     long long sums = 0;     for (long long i = 0; i <= 2001; i++) {       sums += 1LL * (d1[i]) * (d1[i] - 1) / 2;       sums += 1LL * (d2[i]) * (d2[i] - 1) / 2;     }     cout << sums <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int k, n;   long long ans, maxim, suma;   map<string, int> f;   deque<pair<string, int>> box[200001], b1, b2, b;   vector<pair<int, int>> trouble;   string s;   int val;   bool comp(pair<string, int> x, pair<string, int> y) {     return x.second > y.second;   }   int main() {     cin >> k >> n;     int nr = 0;     for (int i = 1; i <= k; i++) {       cin >> s >> val;       string s1 = s;       reverse(s1.begin(), s1.end());       s1 = min(s1, s);       if (f[s1] == 0) {         nr++;         f[s1] = nr;       }       box[f[s1]].push_back({s, val});     }     for (int i = 1; i <= nr; i++) {       b = box[i];       b1.clear();       b2.clear();       b1.push_back(b[0]);       for (int j = 1; j < b.size(); j++) {         if (b[j].first == b1[0].first)           b1.push_back(b[j]);         else           b2.push_back(b[j]);       }       sort(b1.begin(), b1.end(), comp);       sort(b2.begin(), b2.end(), comp);       if (!b2.empty()) {         while (b1.size() != b2.size()) {           if (b1.size() > b2.size())             b1.pop_back();           else             b2.pop_back();         }         for (int j = 0; j < b1.size(); j++)           if (b1[j].second + b2[j].second > 0) ans += b1[j].second + b2[j].second;       } else {         s = b1.front().first;         string s1 = s;         reverse(s1.begin(), s1.end());         if (s == s1) {           while (b1.size() >= 2) {             if (b1[0].second < 0 || b1[1].second < 0) break;             ans += b1[0].second + b1[1].second;             b1.pop_front();             b1.pop_front();           }           if (b1.size() == 1 && b1[0].second > 0)             trouble.push_back({b1[0].second, -1e8});           if (b1.size() >= 2 && b1[0].second > 0)             trouble.push_back({b1[0].second, b1[1].second});         }       }     }     for (auto i : trouble) {       suma += max(i.first + i.second, 0);       if (suma > maxim) maxim = suma;     }     for (auto i : trouble) {       long long sum1 = suma - max(i.first + i.second, 0) + i.first;       if (sum1 > maxim) maxim = sum1;     }     cout << ans + maxim;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(nullptr)->sync_with_stdio(false);     if (fopen(                  .in ,                r )) {       freopen(                         .in ,            r , stdin);       freopen(                         .out ,            w , stdout);     }     int n;     cin >> n;     vector<int> v(n);     for (auto &x : v) cin >> x;     sort(v.begin(), v.end());     int64_t sum = 0;     for (int i = 0; i < n; ++i) {       sum += abs(v[i] - i - 1);     }     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   char ch[100010], temp[100010];   bool ok[100010];   int main() {     int n;     cin >> n;     cin >> ch;     int m = strlen(ch);     for (int i = 1; i < n; i++) {       cin >> temp;       for (int j = 0; j < m; j++) {         if (ch[j] >=  a  && ch[j] <=  z  && temp[j] >=  a  && temp[j] <=  z  &&             ch[j] != temp[j])           ok[j] = 1, ch[j] =  ? ;         if (ch[j] ==  ?  && !ok[j]) ch[j] = temp[j];       }     }     for (int i = 0; i < m; i++) {       if (ch[i] ==  ?  && !ok[i])         cout <<  x ;       else         cout << ch[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n;     cin >> n;     long long int s = 0;     for (long long int i = 0; i < n; i++) {       long long int x;       cin >> x;       s += x;     }     if (s % n == 0)       cout << n;     else       cout << n - 1;   }


#include <bits/stdc++.h>   using namespace std;   long long choose(long long n, long long k);   bool isprime(long long n);   long long power(long long x, long long y, long long p);   long long power(long long x, long long y);   long long gcd(long long a, long long b);   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, k;     cin >> n >> k;     queue<long long> q;     set<long long> st;     for (int i = 0; i < n; i++) {       long long x;       cin >> x;       if (st.find(x) == st.end()) {         if (q.size() == k) {           st.erase(st.find(q.front()));           q.pop();           q.push(x);         } else {           q.push(x);         }         st.insert(x);       }     }     cout << q.size() << endl;     vector<long long> v;     while (!q.empty()) v.push_back(q.front()), q.pop();     for (int i = v.size() - 1; i > -1; i--) cout << v[i] <<    ;     return 0;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long choose(long long n, long long k) {     if (k == 0) return 1;     return (n * choose(n - 1, k - 1)) / k;   }   bool isprime(long long n) {     for (long long i = 2; i * i <= n; i++) {       if (n % i == 0) return false;     }     return true;   }   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long power(long long x, long long y) {     long long res = 1;     while (y > 0) {       if (y & 1) res = res * x;       y = y >> 1;       x = x * x;     }     return res;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 102;   int w, h, n, qs, qt;   int area[maxn], areath;   pair<int, int> q[1000006];   int dx[4] = {-1, 0, 1, 0};   int dy[4] = {0, -1, 0, 1};   bool vis[maxn][maxn];   bool cut[maxn][maxn][4];   int bfs(int stx, int sty) {     qs = 0;     qt = 1;     q[0] = make_pair(stx, sty);     while (qs < qt) {       int nowx = q[qs].first;       int nowy = q[qs++].second;       for (int k = 0; k < 4; k++)         if (!cut[nowx][nowy][k]) {           int x = nowx + dx[k], y = nowy + dy[k];           if (x < 0 || x >= w || y < 0 || y >= h || vis[x][y]) continue;           vis[x][y] = true;           q[qt++] = make_pair(x, y);         }     }     return qt;   }   int main() {     cin >> w >> h >> n;     for (int i = 1; i <= n; i++) {       int xa, ya, xb, yb;       cin >> xa >> ya >> xb >> yb;       if (xa == xb)         for (int j = ya; j < yb; j++) cut[xa][j][0] = cut[xa - 1][j][2] = true;       else         for (int j = xa; j < xb; j++) cut[j][ya][1] = cut[j][ya - 1][3] = true;     }     for (int i = 0; i < w; i++)       for (int j = 0; j < h; j++)         if (!vis[i][j]) {           vis[i][j] = true;           area[++areath] = bfs(i, j);         }     sort(area + 1, area + areath + 1);     for (int i = 1; i < areath; i++) cout << area[i] <<    ;     cout << area[areath] << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(3)   using namespace std;   inline char gc() {     static const int L = 1000;     static char sxd[L], *sss = sxd, *ttt = sxd;     if (sss == ttt) {       ttt = (sss = sxd) + fread(sxd, 1, L, stdin);       if (sss == ttt) return EOF;     }     return *sss++;   }   inline int read(int& x) {     x = 0;     char c = getchar();     bool f = false;     for (; !isdigit(c); c = getchar()) {       if (c == EOF) return -1;       if (c ==  - ) f = true;     }     for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);     if (f) x = -x;     return 1;   }   int i, j, a[1000], n, m, k, s, sum, z;   int main() {     read(n);     for (i = 1; i <= n; i++) read(a[i]);     while (!a[++s])       ;     for (; s <= n; s++) {       if (a[s]) {         if (sum < 2) z += sum;         sum = 0;         z++;       } else         sum++;     }     cout << z;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long a[5010];   int pw2[5010];   int ans = 1e9;   bool check(int x, int y) {     if (a[x] % a[y] != 0LL) return false;     if (pw2[y] - (y - x - 1) <= 0) return true;     return (pw2[x] + 1 == pw2[y] - (y - x - 1));   }   int dp[5010];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %I64d , a + i);       while (a[i] % 2LL == 0LL) {         a[i] /= 2LL;         pw2[i]++;       }     }     for (int i = 1; i <= n; i++) {       dp[i] = i - 1;       for (int j = 1; j < i; j++) {         if (!check(j, i)) continue;         dp[i] = min(dp[i], dp[j] + (i - j - 1));       }       ans = min(ans, dp[i] + n - i);     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, x, l, r, i, s, a[10179];   int main() {     cin >> n >> m;     for (i = 1; i <= n; i++) cin >> a[i];     while (m--) {       cin >> l >> r >> x;       for (s = 0, i = l; i <= r; i++)         if (a[i] < a[x]) s++;       if (s + l == x)         cout <<  Yes n ;       else         cout <<  No n ;     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   template <class T>   void na(vector<T>& a, int n) {     a = vector<T>(n);     for (T& i : a) cin >> i;   }   template <class T>   void printVector(vector<T>& a) {     for (T& i : a) cout << i <<    ;     cout <<   n ;   }   template <class T>   vector<T> shrink(vector<T>& x) {     vector<T> vals = x;     sort((vals).begin(), (vals).end());     (vals).resize(unique((vals).begin(), (vals).end()) - (vals).begin());     for (T& i : x)       i = upper_bound((vals).begin(), (vals).end(), i) - vals.begin();     return vals;   }   const int N = 5555;   int n;   bool gmat[N][N];   vector<int> g[N];   bool found = false;   bool onstack[N], seen[N];   stack<int> stk;   vector<int> cycle;   void dfs(int u) {     onstack[u] = 1;     seen[u] = 1;     stk.push(u);     for (int v : g[u]) {       if (found) return;       if (onstack[v]) {         found = true;         int cur = u;         cycle.push_back(cur);         do {           stk.pop();           cur = stk.top();           cycle.push_back(cur);         } while (cur != v);         return;       }       if (!seen[v]) dfs(v);     }     onstack[u] = false;     stk.pop();   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0), cout.tie();     cin >> n;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) {         char c;         cin >> c;         if (c ==  1 ) g[i].push_back(j), gmat[i][j] = 1;       }     for (int i = 1; i <= n && !found; ++i)       if (!seen[i]) dfs(i);     reverse((cycle).begin(), (cycle).end());     if (!found)       cout <<  -1 n ;     else {       int j = 1, k = 2;       while (k < ((int)(cycle).size())) {         if (gmat[cycle[k]][cycle[0]]) {           cout << cycle[0] <<     << cycle[j] <<     << cycle[k] <<   n ;           break;         }         ++k, ++j;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, c, d, e, f;   bool ju2(long long x, long long y) {     x -= c, y -= d;     long long div = e * e + f * f;     if (!div) return x == 0 && y == 0;     return (e * x + f * y) % div == 0 && (e * y - f * x) % div == 0;   }   bool ju1() {     if (ju2(a, b)) return 1;     if (ju2(-a, -b)) return 1;     if (ju2(-b, a)) return 1;     if (ju2(b, -a)) return 1;     return 0;   }   signed main() {     scanf( %lld%lld%lld%lld%lld%lld , &a, &b, &c, &d, &e, &f);     if (ju1())       puts( YES );     else       puts( NO );     return 0;   }


#pragma GCC optimize( O3 )   #include <bits/stdc++.h>   // #include <ext/pb_ds/assoc_container.hpp>   // using namespace __gnu_pbds;   // gp_hash_table<int, int> mapka;      using namespace std;   #define PB push_back   #define MP make_pair   #define LL long long   #define int LL   #define FOR(i,a,b) for(int i = (a); i <= (b); i++)   #define RE(i,n) FOR(i,1,n)   #define REP(i,n) FOR(i,0,(int)(n)-1)   #define R(i,n) REP(i,n)   #define VI vector<int>   #define PII pair<int,int>   #define LD long double   #define FI first   #define SE second   #define st FI   #define nd SE   #define ALL(x) (x).begin(), (x).end()   #define SZ(x) ((int)(x).size())      template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }   template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }      template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<< = <<h<<endl; }   template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {     while(*sdbg!= , )cerr<<*sdbg++;     cerr<< = <<h<< , ; _dbg(sdbg+1, a...);   }      template<class T> ostream &operator<<(ostream& os, vector<T> V) {     os <<  [ ; for (auto vv : V) os << vv <<  , ; return os <<  ] ;   }   template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {     return os <<  (  << P.st <<  ,  << P.nd <<  ) ;   }      #ifdef LOCAL   #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)   #else   #define debug(...) (__VA_ARGS__)   #define cerr if(0)cout   #endif      vector<vector<int>> res;   int n,m;   vector<vector<PII>> d;   void licz(int s){     vector<vector<int>> kt(n);     vector<int> odl(n,2e9);     int naj = 2e9;     auto dod = [&](int v,int od){       if(odl[v] > od){         odl[v] = od;         kt[od % n].PB(v);       }     };     for(PII el:d[s]){       mini(naj,el.SE);       dod(el.FI, el.SE);     }     int gd = naj % n;     R(_,n){       debug(gd, SZ(kt));       R(i,SZ(kt[gd])){         int el = kt[gd][i];   //     for(int el:kt[gd]){         if(odl[el] == naj){           for(PII ed:d[el]){             dod((ed.FI + gd) % n, naj + ed.SE);           }           el++;           if(el == n)el -= n;           dod(el, naj + 1);         }       }       naj++;       gd++;       if(gd == n)gd = 0;     }     R(i,n)if(s != i){       res[s][i] = odl[i];     }   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout << fixed << setprecision(11);     cerr << fixed << setprecision(6);     cin >> n >> m;     res.resize(n,vector<int>(n));     d.resize(n);     R(i,m){       int a,b,c;       cin >> a >> b >> c;       d[a].PB({b,c});     }     R(i,n){       licz(i);     }     R(i,n){       R(j,n){         cout << res[i][j] <<    ;       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long modinverse(long long a, long long m) {     long long m0 = m;     long long y = 0, x = 1;     if (m == 1) return 0;     while (a > 1) {       long long q = a / m, t = m;       m = a % m, a = t;       t = y;       y = x - q * y;       x = t;     }     if (x < 0) x += m0;     return x;   }   long long mod_pow(long long a, long long n, long long p) {     long long ans = 1;     while (n) {       if (n & 1) ans = (ans * a) % p;       a = (a * a) % p;       n >>= 1;     }     return ans;   }   long long prime_mod_pow(long long a, long long n, long long p) {     long long ans = 1;     n %= p - 1;     while (n) {       if (n & 1) ans = (ans * a) % p;       a = (a * a) % p;       n >>= 1;     }     return ans;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long ext_gcd(long long l, long long r, long long &x, long long &y) {     if (l == 0) {       x = 0;       y = 1;       return r;     }     long long x1, y1, g;     g = ext_gcd(r % l, l, x1, y1);     x = y1 - (r / l) * x1;     y = x1;     return g;   }   const long long dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};   long long ok(long long x, long long y, long long n, long long m) {     return (x >= 0 and y >= 0 and x < n and y < m);   }   long long num_div;   map<long long, long long> pr_factor;   void factorise(long long n) {     long long count = 0;     while (!(n % 2)) {       n >>= 1;       count++;     }     if (count) num_div *= (count + 1);     pr_factor[2] = count;     for (long long i = 3; i <= sqrt(n); i += 2) {       count = 0;       while (n % i == 0) {         count++;         n /= i;       }       if (count) num_div *= (count + 1);       pr_factor[i] = count;     }     if (n > 2) num_div *= 2;     pr_factor[n] = 1;   }   bool type1(long long x1, long long x2, long long x3, long long y1, long long y2,              long long y3, char c1, char c2, char c3) {     if (x1 == x2 + x3 and y2 == y3 and x1 == y1 + y2) {       cout << x1 <<   n ;       for (long long i = 0; i < y1; i++) {         for (long long j = 0; j < x1; j++) cout << c1;         cout <<   n ;       }       for (long long i = 0; i < y2; i++) {         for (long long j = 0; j < x2; j++) cout << c2;         for (long long j = 0; j < x3; j++) cout << c3;         cout <<   n ;       }       return true;     }     return false;   }   bool type2(long long x1, long long x2, long long x3, long long y1, long long y2,              long long y3, char c1, char c2, char c3) {     if (x1 == x2 and x2 == x3 and x1 == y1 + y2 + y3) {       cout << x1 <<   n ;       for (long long i = 0; i < y1; i++) {         for (long long j = 0; j < x1; j++) cout << c1;         cout <<   n ;       }       for (long long i = 0; i < y2; i++) {         for (long long j = 0; j < x1; j++) cout << c2;         cout <<   n ;       }       for (long long i = 0; i < y3; i++) {         for (long long j = 0; j < x3; j++) cout << c3;         cout <<   n ;       }       return true;     }     return false;   }   void solve() {     long long x1, x2, x3, y1, y2, y3;     cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;     vector<pair<long long, long long> > a;     a.push_back(make_pair(x1, y1));     a.push_back(make_pair(y1, x1));     vector<pair<long long, long long> > b;     b.push_back(make_pair(x2, y2));     b.push_back(make_pair(y2, x2));     vector<pair<long long, long long> > c;     c.push_back(make_pair(x3, y3));     c.push_back(make_pair(y3, x3));     for (long long i = 0; i < a.size(); i++) {       long long X1 = a[i].first, Y1 = a[i].second;       for (long long j = 0; j < b.size(); j++) {         long long X2 = b[j].first, Y2 = b[j].second;         for (long long k = 0; k < c.size(); k++) {           long long X3 = c[k].first, Y3 = c[k].second;           if (type1(X1, X2, X3, Y1, Y2, Y3,  A ,  B ,  C )) return;           if (type1(X1, X3, X2, Y1, Y3, Y2,  A ,  C ,  B )) return;           if (type1(X2, X1, X3, Y2, Y1, Y3,  B ,  A ,  C )) return;           if (type1(X2, X3, X1, Y2, Y3, Y1,  B ,  C ,  A )) return;           if (type1(X3, X1, X2, Y3, Y1, Y2,  C ,  A ,  B )) return;           if (type1(X3, X2, X1, Y3, Y2, Y1,  C ,  B ,  A )) return;           if (type2(X1, X2, X3, Y1, Y2, Y3,  A ,  B ,  C )) return;           if (type2(X1, X3, X2, Y1, Y3, Y2,  A ,  C ,  B )) return;           if (type2(X2, X1, X3, Y2, Y1, Y3,  B ,  A ,  C )) return;           if (type2(X2, X3, X1, Y2, Y3, Y1,  B ,  C ,  A )) return;           if (type2(X3, X1, X2, Y3, Y1, Y2,  C ,  A ,  B )) return;           if (type2(X3, X2, X1, Y3, Y2, Y1,  C ,  B ,  A )) return;         }       }     }     cout << -1 <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     long long tc = 1;     while (t--) {       solve();       tc++;     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[1000];   bool ok = false;   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     int sum = 0;     for (int i = 0; i < n; i++) sum += a[i];     int m;     cin >> m;     int l, r;     for (int i = 0; i < m; i++) {       cin >> l >> r;       if (sum >= l && sum <= r) {         cout << sum << endl;         exit(0);       }       if (sum < l) cout << l << endl, exit(0);     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int main() {     int a, b, c;     cin >> a >> b >> c;     if (!c) return cout << (a == b ?  YES  :  NO ), 0;     if (c > 0 && b < a || c < 0 && b > a) return cout <<  NO , 0;     cout << ((a % c + c) % c == (b % c + c) % c ?  YES  :  NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 512345, lgN = 20, rootN = 1123;   const double eps = 1e-18;   int r[N], w[N];   vector<double> positive, negative;   double p[N];   int main() {     int n, c, d;     cin >> n >> c >> d;     for (int i = 1; i <= n; ++i) {       cin >> r[i] >> w[i];       r[i] -= c, w[i] -= d;       p[i] = (((double)r[i]) / (double)w[i]);     }     for (int i = 1; i <= n; ++i) {       double x = (((double)r[i]) / (double)w[i]);       if (w[i] > 0) {         positive.push_back(x);       } else if (w[i] < 0) {         negative.push_back(x);       }     }     sort(positive.begin(), positive.end());     sort(negative.begin(), negative.end());     long long first = 0, second = 0;     for (int i = 0; i < positive.size(); ++i) {       first += (lower_bound(negative.begin(), negative.end(), positive[i]) -                 negative.begin());     }     for (int i = 0; i < negative.size(); ++i) {       second += (lower_bound(positive.begin(), positive.end(), negative[i]) -                  positive.begin());     }     long long ans = 0;     for (int i = 1; i <= n; ++i) {       if (w[i] > 0) {         ans += ((long long)(negative.end() - upper_bound(negative.begin(),                                                          negative.end(), p[i]))) *                (lower_bound(negative.begin(), negative.end(), p[i]) -                 negative.begin());       } else if (w[i] < 0) {         ans += ((long long)(positive.end() - upper_bound(positive.begin(),                                                          positive.end(), p[i]))) *                (lower_bound(positive.begin(), positive.end(), p[i]) -                 positive.begin());       } else if (w[i] == 0) {         if (r[i] < 0)           ans += first;         else           ans += second;       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, la, lb, x;     cin >> n >> la >> lb;     int apple[100];     for (int i = 1; i <= n; i++) apple[i] = 2;     for (int i = 1; i <= la; i++) {       cin >> x;       apple[x] = 1;     }     for (int i = 1; i <= lb; i++) {       cin >> x;       if (apple[x] != 1) apple[x] = 2;     }     for (int i = 1; i <= n; i++) cout << apple[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   bool vis[400];   set<int> sub[400];   vector<int> ga[400];   vector<int> path;   vector<int> ord;   int curr;   void DFS(int a) {     sub[a].insert(a);     for (int i = 0; i < ga[a].size(); i++) {       int b = ga[a][i];       if (vis[b]) continue;       vis[b] = true;       DFS(b);       for (__typeof(sub[b].begin()) it = sub[b].begin(); it != sub[b].end(); it++)         sub[a].insert(*it);     }   }   void SFD(int a) {     if (ord[curr] == a) {       curr++;       return;     }     while (1) {       if (curr == ord.size()) return;       bool upd = false;       for (int i = 0; i < ga[a].size(); i++) {         int b = ga[a][i];         if (vis[b] || sub[b].find(ord[curr]) == sub[b].end()) continue;         vis[b] = true;         path.push_back(b);         SFD(b);         path.push_back(a);         upd = true;         break;       }       if (!upd) return;     }   }   int main() {     cin >> n;     for (int i = 1; i < n; i++) {       int a, b;       cin >> a >> b;       ga[a].push_back(b);       ga[b].push_back(a);     }     int x;     while (cin >> x) ord.push_back(x);     vis[1] = true;     DFS(1);     memset(vis, false, sizeof(vis));     path.push_back(1);     vis[1] = true;     SFD(1);     if (path.size() != n * 2 - 1)       cout << -1;     else {       for (int i = 0; i < path.size(); i++) cout << path[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300100;   int read() {     int x = 0, w = 1;     char ch = 0;     while (!isdigit(ch)) {       ch = getchar();       if (ch ==  - ) w = -1;     }     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return x * w;   }   vector<int> G[maxn];   int vis[maxn];   int fa[maxn], ans[maxn];   int findfa(int rt) { return rt == fa[rt] ? rt : fa[rt] = findfa(fa[rt]); }   void merge(int a, int b) {     if (findfa(a) != findfa(b)) fa[fa[a]] = fa[b];   }   int tmp = 0, tmp2 = 0;   void dfs(int rt, int col, int res) {     if (vis[rt] == col) return;     if (res > tmp) tmp = res, tmp2 = rt;     vis[rt] = col;     for (int to : G[rt]) merge(rt, to), dfs(to, col, res + 1);   }   int main() {     int n = read(), m = read(), q = read();     for (int i = 1; i <= n; i++) fa[i] = i;     while (m--) {       int u = read(), v = read();       G[u].push_back(v);       G[v].push_back(u);     }     for (int i = 1; i <= n; i++)       if (!vis[i]) {         tmp = 0, tmp2 = i;         dfs(i, i, 0);         tmp = 0;         dfs(tmp2, tmp2, 0);         ans[findfa(i)] = tmp;       }     while (q--) {       int opt = read();       if (opt == 1)         printf( %d n , ans[findfa(read())]);       else {         int u = read(), v = read();         u = findfa(u), v = findfa(v);         if (u == v) continue;         fa[u] = v;         ans[v] =             max(max(ans[v], ans[u]), (ans[u] + 1) / 2 + (ans[v] + 1) / 2 + 1);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, I;     cin >> n >> I;     map<long long, long long> mp;     for (long long i = 0; i < n; i++) {       long long x;       cin >> x;       mp[x]++;     }     if (ceil(log2(mp.size())) * n <= I * 8) {       cout << 0;       return 0;     }     vector<long long> v(mp.size() + 1);     long long j = 1;     v[0] = 0;     for (auto i = mp.begin(); i != mp.end(); i++) {       v[j] = v[j - 1] + i->second;       j++;     }     long long x = pow(2, (I * 8) / n);     long long max_int = 0;     for (long long i = 0; i < v.size() - x; i++) {       max_int = max(max_int, v[i + x] - v[i]);     }     cout << n - max_int;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long cc = 0;   unordered_map<long long, long long> c, rc;   vector<vector<long long> > graph(1e6);   vector<bool> visited(1e6, false);   vector<vector<long long> > answers(1e6);   void dfs(long long node) {     visited[node] = true;     answers[cc].push_back(node);     for (auto i : graph[node])       if (!visited[i]) dfs(i);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     vector<long long> nums(n);     for (int i = 0; i < (int)(n); i++) cin >> (nums)[i];     ;     vector<long long> nums_copy((nums).begin(), (nums).end());     sort((nums_copy).begin(), (nums_copy).end());     for (int i = 0; i < n; i++) c[i] = nums_copy[i], rc[nums_copy[i]] = i;     vector<long long> modified(n);     for (int i = 0; i < n; i++) modified[i] = rc[nums[i]];     for (int i = 0; i < n; i++) {       graph[i].push_back(modified[i]);     }     for (int i = 0; i < n; i++) {       if (!visited[i]) dfs(i), cc++;     }     cout << cc << endl;     for (int i = 0; i < cc; i++) {       long long k = answers[i].size();       cout << k <<    ;       for (int j = 0; j < k; j++) cout << answers[i][j] + 1 <<    ;       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> addr;   void psh(string s) {     long long res = 0, cur = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  . ) {         res *= 256;         res += cur;         cur = 0;       } else {         cur *= 10;         cur += (s[i] -  0 );       }     }     res *= 256;     res += cur;     addr.push_back(res);   }   void outp(long long mask) {     int d = mask % 256;     mask /= 256;     int c = mask % 256;     mask /= 256;     int b = mask % 256;     mask /= 256;     int a = mask;     printf( %d.%d.%d.%d n , a, b, c, d);   }   set<long long> s;   int main() {     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       psh(s);     }     long long mask = 0;     for (int i = 31; i >= 0; i--) {       mask += (1ll << i);       s.clear();       for (int i = 0; i < n; i++) {         s.insert(addr[i] & mask);       }       if (s.size() == k) {         outp(mask);         return 0;       }     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 51;   vector<int> g[N];   int n, sz[N], dp[N][N][N][N], s, m, ans = 1e9, cost[N][N], temp[N];   void dfs(int u, int p) {     for (auto i : g[u])       if (i != p) dfs(i, u), sz[u] += sz[i];   }   int calc(int from, int to, int behind, int front) {     if (!front) return 1e9;     if (g[to].size() == 1)       return behind ? calc(to, from, 0, behind) + cost[to][from] : cost[to][from];     int &ref = dp[to][from][behind][front], id = 0, x;     if (ref != -1) return ref;     for (auto i : g[to])       if (i != from)         for (int j = 1; j <= front; j++) calc(to, i, behind + front - j, j);     for (int i = 1; i <= front; i++) temp[i] = -1e9;     for (auto x : g[to]) {       if (x == from) continue;       for (int i = front; i; i--)         for (int j = 1; i - j >= 0; j++)           temp[i] =               max(temp[i], min(temp[i - j], calc(to, x, behind + front - j, j)));     }     return ref = temp[front] + cost[from][to];   }   int main() {     scanf( %d , &n);     memset(dp, -1, sizeof dp);     for (int i = 1, a, b, c; i < n; i++) {       scanf( %d%d%d , &a, &b, &c);       g[--a].push_back(--b);       g[b].push_back(a);       cost[a][b] = cost[b][a] = c;     }     temp[0] = 1e9;     scanf( %d%d , &s, &m);     for (int i = 0, a; i < m; i++) {       scanf( %d , &a);       sz[a - 1]++;     }     dfs(--s, -1);     for (auto i : g[s]) ans = min(ans, calc(s, i, m - sz[i], sz[i]));     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, p;     cin >> n >> p;     int a[n], maxi = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     for (int i = 0; i < n; i++) {       maxi = max(maxi, a[i] - i);     }     int b = 0;     for (int i = 0; i < n; i++) {       if (i >= p - 1) {         b = max(b, min(maxi - (a[i] - i), i));       }     }     b = max(0, p - b - 1);     cout << b <<   n ;     for (int i = 0; i < b; i++) {       cout << maxi + i << (i == b - 1 ?   n  :    );     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   typedef struct {     int pre[maxn];     void init(int n) {       for (int i = 0; i <= n + 1; ++i) pre[i] = i;     }     int find(int x) {       if (x == pre[x]) return x;       return pre[x] = find(pre[x]);     }     void merge(int x, int y) { pre[find(x)] = find(y); }   } DS;   DS lds, rds;   int n, m, q;   int M[maxn][maxn];   int D[maxn][maxn];   int U[maxn][maxn];   int L[maxn][maxn];   int R[maxn][maxn];   int a[maxn];   int id[maxn];   bool comp(int x, int y) { return a[x] < a[y]; }   int getMax(int n, int x) {     int ret = 0;     int l, r;     int i;     for (i = 1; i <= n; ++i) id[i] = i;     lds.init(n);     rds.init(n);     sort(id + 1, id + 1 + n, comp);     for (i = n; i > 0; --i) {       l = lds.find(id[i] - 1) + 1;       r = rds.find(id[i] + 1) - 1;       if (l <= x && x <= r) ret = max(ret, (r - l + 1) * a[id[i]]);       lds.merge(id[i], id[i] - 1);       rds.merge(id[i], id[i] + 1);     }     return ret;   }   void printMat(int x[][maxn]) {     int i, j;     for (i = 1; i <= n; ++i) {       for (j = 1; j <= m; ++j) printf( %d  , x[i][j]);       putchar(  n );     }   }   int main() {     int i, j, k;     int ans;     int op, x, y;     scanf( %d %d %d , &n, &m, &q);     for (i = 1; i <= n; ++i)       for (j = 1; j <= m; ++j) scanf( %d , &M[i][j]);     memset(D, 0, sizeof(D));     memset(U, 0, sizeof(U));     memset(L, 0, sizeof(L));     memset(R, 0, sizeof(R));     for (i = 1; i <= n; ++i) {       for (j = 1; j <= m; ++j) {         if (M[i][j]) {           U[i][j] = U[i - 1][j] + 1;           L[i][j] = L[i][j - 1] + 1;         }       }     }     for (i = n; i > 0; --i) {       for (j = m; j > 0; --j) {         if (M[i][j]) {           D[i][j] = D[i + 1][j] + 1;           R[i][j] = R[i][j + 1] + 1;         }       }     }     while (q--) {       scanf( %d %d %d , &op, &x, &y);       if (op == 1) {         M[x][y] = !M[x][y];         for (j = y; j <= m; ++j) L[x][j] = M[x][j] ? L[x][j - 1] + 1 : 0;         for (i = x; i <= n; ++i) U[i][y] = M[i][y] ? U[i - 1][y] + 1 : 0;         for (j = y; j > 0; --j) R[x][j] = M[x][j] ? R[x][j + 1] + 1 : 0;         for (i = x; i > 0; --i) D[i][y] = M[i][y] ? D[i + 1][y] + 1 : 0;       } else {         ans = 0;         for (i = 1; i <= n; ++i) a[i] = L[i][y];         ans = max(ans, getMax(n, x));         for (i = 1; i <= n; ++i) a[i] = R[i][y];         ans = max(ans, getMax(n, x));         for (i = 1; i <= m; ++i) a[i] = U[x][i];         ans = max(ans, getMax(m, y));         for (i = 1; i <= m; ++i) a[i] = D[x][i];         ans = max(ans, getMax(m, y));         printf( %d n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using ull = unsigned long long;   using pii = pair<int, int>;   using vi = vector<int>;   struct res {     int x, t;     bool operator<(const res &z) const { return x < z.x; }     bool operator==(const res &z) const { return x == z.x && t == z.t; }   };   const res E = {-1, 0};   const int M = 1e9;   void q(int cnt, int x, int y) {     if (cnt) cout << cnt <<     << x + 1 <<     << y + 1 <<   n ;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << setprecision(15);     int n, k, v;     cin >> n >> k >> v;     vi a(n);     for (int &x : a) cin >> x;     vector<vector<res>> dp(n + 1, vector<res>(k, E));     dp[0][0] = {0, 0};     for (int i = 1; i <= n; ++i) {       dp[i] = dp[i - 1];       for (res &r : dp[i]) r.t = 0;       for (int j = 0; j < k; ++j)         if (!(dp[i - 1][j] == E)) {           auto &l = dp[i][(j + a[i - 1]) % k];           l = max(l, {dp[i - 1][j].x + a[i - 1], 1});           dp[i][j] = max(dp[i][j], {dp[i - 1][j].x + a[i - 1] / k * k, 0});         }     }     if (dp[n][v % k] == E) return cout <<  NO n , 0;     vi b, g;     for (int i = n, c = v % k; i >= 1; --i) {       if (dp[i][c].t)         b.push_back(i - 1), c = (c - a[i - 1] % k + k) % k;       else         g.push_back(i - 1);     }     int cap = 0;     for (int x : b) cap += a[x];     int ad = 0;     for (int x : g) ad += a[x];     cap += ad / k * k;     if (cap < v) return cout <<  NO n , 0;     cout <<  YES n ;     int r, cur = 0, ot;     if (((int)(b).size())) {       r = b[0], cur = a[r];       for (int i = 1; i < ((int)(b).size()); ++i) {         q(M, b[i], r), cur += a[b[i]];       }       ot = r == 0 ? 1 : 0;     } else {       r = 0, ot = 1;       q(M, r, ot);     }     for (int x : g)       if (x != ot) q(M, x, ot);     q(cur / k, r, ot), cur %= k;     q((v - cur) / k, ot, r);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1050;   int n, m, g[maxn][maxn], ans[maxn][maxn];   int p[4] = {4, 3, 2, 1};   bool chk1(int x, int a, int b) {     int i, st, c[2] = {a, b};     for (st = 0; st < m && !g[x][st]; st++)       ;     if (g[x][st] != c[0]) swap(c[0], c[1]);     for (i = 0; st + i < m; i++)       if (g[x][st + i] && g[x][st + i] != c[i & 1])         return 0;       else         ans[x][st + i] = c[i & 1];     for (i = 1; i <= st; i++) ans[x][st - i] = c[i & 1];     return 1;   }   bool chk2(int x, int a, int b) {     int i, st, c[2] = {a, b};     for (st = 0; st < n && !g[st][x]; st++)       ;     if (g[st][x] != c[0]) swap(c[0], c[1]);     for (i = 0; st + i < n; i++)       if (g[st + i][x] && g[st + i][x] != c[i & 1])         return 0;       else         ans[st + i][x] = c[i & 1];     for (i = 1; i <= st; i++) ans[st - i][x] = c[i & 1];     return 1;   }   void print() {     int i, j;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) printf( %d , ans[i][j]);       printf(  n );     }   }   bool check() {     int i, j;     bool z = 1;     for (i = 0; i < n; i++) z &= chk1(i, p[(i & 1) * 2], p[(i & 1) * 2 + 1]);     if (z) {       print();       return 1;     }     z = 1;     for (i = 0; i < m; i++) z &= chk2(i, p[(i & 1) * 2], p[(i & 1) * 2 + 1]);     if (z) {       print();       return 1;     }     return 0;   }   int main() {     int i, j;     scanf( %d%d n , &n, &m);     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) g[i][j] = getchar() -  0 ;       scanf(  n );     }     for (i = 0; i < 4 * 3 * 2; i++) {       next_permutation(p, p + 4);       if (check()) return 0;     }     printf( 0 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool chmax(T &a, const T &b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   template <class T>   bool chmin(T &a, const T &b) {     if (a > b) {       a = b;       return 1;     }     return 0;   }   template <typename T>   istream &operator>>(istream &is, vector<T> &vec) {     for (auto &v : vec) is >> v;     return is;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &vec) {     for (int i = 0; i < vec.size(); i++) {       os << vec[i];       if (i + 1 != vec.size()) os <<    ;     }     return os;   }   template <typename T>   ostream &operator<<(ostream &os, const set<T> &st) {     for (auto itr = st.begin(); itr != st.end(); ++itr) {       os << *itr;       auto titr = itr;       if (++titr != st.end()) os <<    ;     }     return os;   }   template <typename T>   ostream &operator<<(ostream &os, const unordered_set<T> &st) {     for (auto itr = st.begin(); itr != st.end(); ++itr) {       os << *itr;       auto titr = itr;       if (++titr != st.end()) os <<    ;     }     return os;   }   template <typename T>   ostream &operator<<(ostream &os, const multiset<T> &st) {     for (auto itr = st.begin(); itr != st.end(); ++itr) {       os << *itr;       auto titr = itr;       if (++titr != st.end()) os <<    ;     }     return os;   }   template <typename T>   ostream &operator<<(ostream &os, const unordered_multiset<T> &st) {     for (auto itr = st.begin(); itr != st.end(); ++itr) {       os << *itr;       auto titr = itr;       if (++titr != st.end()) os <<    ;     }     return os;   }   template <typename T1, typename T2>   ostream &operator<<(ostream &os, const pair<T1, T2> &p) {     os <<  (  << p.first <<  ,   << p.second <<  ) ;     return os;   }   template <typename T1, typename T2>   ostream &operator<<(ostream &os, const map<T1, T2> &mp) {     for (auto itr = mp.begin(); itr != mp.end(); ++itr) {       os <<  (  << itr->first <<  ,   << itr->second <<  ) ;       auto titr = itr;       if (++titr != mp.end()) os <<    ;     }     return os;   }   template <typename T1, typename T2>   ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) {     for (auto itr = mp.begin(); itr != mp.end(); ++itr) {       os <<  (  << itr->first <<  ,   << itr->second <<  ) ;       auto titr = itr;       if (++titr != mp.end()) os <<    ;     }     return os;   }   using ll = long long int;   using P = pair<int, int>;   const int inf = 1e9;   const ll linf = 1LL << 50;   const double EPS = 1e-10;   const int mod = 1000000007;   const int dx[4] = {-1, 0, 1, 0};   const int dy[4] = {0, -1, 0, 1};   struct fast_io {     fast_io() {       ios_base::sync_with_stdio(false);       cin.tie(nullptr);       cout << fixed << setprecision(20);     }   } fast_io_;   int main(int argc, char const *argv[]) {     ll n, m;     cin >> n >> m;     m *= 2;     ll t;     cin >> t;     t *= 2;     vector<ll> x(n);     vector<ll> d(n);     for (int i = (0); i < (n); ++i) {       cin >> x[i];       x[i] *= 2;       x[i] %= m;       string c;       cin >> c;       d[i] = (c ==  L  ? -1 : 1);     }     vector<int> e(n);     for (int i = (0); i < (n); ++i) e[i] = i;     sort((e).begin(), (e).end(), [&](int i, int j) { return x[i] < x[j]; });     vector<int> tr(n);     for (int i = (0); i < (n); ++i) tr[e[i]] = i;     vector<ll> res(n);     for (int i_ = (0); i_ < (n); ++i_) {       int i = e[i_];       res[i_] = (x[i] + d[i] * t) % m;       if (res[i_] < 0) res[i_] += m;     }     ll curr = 0;     if (d[e[0]] == 1) {       for (int k = (1); k < (n); ++k) {         if (d[e[k]] == -1 && (x[e[k]] - x[e[0]]) / 2 <= t) {           curr += (t - (x[e[k]] - x[e[0]]) / 2) / (m / 2) + 1;         }       }     } else {       x[e[0]] += m;       for (int k = (1); k < (n); ++k) {         if (d[e[k]] == 1 && (x[e[0]] - x[e[k]]) / 2 <= t) {           curr += (t - (x[e[0]] - x[e[k]]) / 2) / (m / 2) + 1;         }       }       curr = (n - curr) % n;       if (curr < 0) curr += n;     }     curr %= n;     vector<pair<ll, int>> v(n);     for (int i = (0); i < (n); ++i) v[i] = make_pair(res[i], i);     sort((v).begin(), (v).end(), [&](pair<ll, int> x, pair<ll, int> y) {       if (x.first != y.first) return x.first < y.first;       if (d[e[x.second]] == -1) return true;       return false;     });     int at = 0;     for (int i = (0); i < (n); ++i)       if (v[i].second == 0) at = i;     for (int i = (0); i < (n); ++i) {       res[i] = v[(at + i) % n].first;     }     vector<ll> tres = res;     for (int i = (0); i < (n); ++i) {       res[i] = tres[(i - curr + n) % n];     }     tres = res;     for (int i = (0); i < (n); ++i) res[i] = tres[tr[i]];     for (int i = (0); i < (n); ++i) {       if (res[i] < 0) res[i] += m;     }     for (int i = (0); i < (n); ++i) {       res[i] /= 2;       if (res[i] == 0) res[i] = (m / 2);     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[100000];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; ++i) cin >> arr[i];     for (int i = 1; n / i >= 3; ++i) {       if (n % i != 0) continue;       for (int j = 0; j < i; ++j) {         int Ok = 0;         for (int k = j; k < n; k += i) {           if (arr[k] == 1) ++Ok;         }         if (Ok == n / i) {           cout <<  YES  << endl;           return 0;         }       }     }     cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k = 0;     string s;     cin >> s >> n;     vector<char> v, vv;     int len = s.size();     if (len % n != 0) {       cout <<  NO  << endl;       return 0;     }     int cnt = 0, c = 0, j;     int m = len / n;     for (int i = 0; i < n; i++) {       v.clear();       vv.clear();       for (j = c; j < len; j++) {         v.push_back(s[j]);         vv.push_back(s[j]);         cnt++;         c++;         if (cnt == m) break;       }       cnt = 0;       int pnt = 0;       reverse(vv.begin(), vv.end());       int ln = v.size();       for (int i = 0; i < ln; i++) {         if (v[i] == vv[i]) pnt++;         if (pnt == ln) {           k++;         }       }       v.clear();       vv.clear();     }     if (k == n) {       k = 0;       cout <<  YES  << endl;     } else {       k = 0;       cout <<  NO  << endl;     }     return 0;   }


#include <cmath>   #include <cstdio>   #include <string>   #include <cstring>   #include <iostream>   #include <algorithm>   using namespace std;   typedef long long ll;   const int N = 55;   const int mod = 1e9 + 7;         int t , n , m ,ans;   char s[N];   int a[N];      void solve(){       cin>>s + 1;       int len = strlen( s + 1);       int flag = 1; // 1 : smaller       int cnt = 0;       while( cnt < len ){           if( cnt < len){               for(int i = 1;i <= len; ++ i){                   if( !a[i] ) {                       a[i] = 1 , ++cnt ;                       if( s[i] !=  a  ) s[i] =  a  ;                       else s[i] =  b  ;                       break;                   }               }           }           if( cnt < len){               for(int i = 1;i <= len; ++ i){                   if( !a[i] ) {                       a[i] = 1 , ++cnt;                       if( s[i] !=  z  ) s[i] =  z  ;                       else s[i] =  y ;                       break;                   }               }           }       }       for(int i = 1;i <= len; ++ i) cout<<s[i];       memset(a ,0 , sizeof(a) );       cout<<endl;   }      int main(){       ios::sync_with_stdio(false);       cin.tie(0);       cin>>t;       while( t -- ) solve();       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int findGreater(int compare, int startIndex, int *arr, int sizeofArray) {     for (int i = startIndex; i < sizeofArray; i++) {       if (compare < arr[i]) return i;     }     return -1;   }   int findLesser(int compare, int startIndex, int *arr, int sizeofArray) {     for (int i = startIndex; i < sizeofArray; i++) {       if (compare > arr[i]) return i;     }     return -1;   }   void initArray(int *arr, int Size) {     for (int j = 0; j < Size; j++) {       cin >> arr[j];     }   }   int main() {     int TestCases;     int Size;     int *arr;     int i;     int j;     int k;     cin >> TestCases;     for (int t = 0; t < TestCases; t++) {       cin >> Size;       arr = new int[Size];       initArray(arr, Size);       for (int i = 0; i < Size; i++) {         if (i + 1 == Size) {           cout <<  NO  << endl;         }         j = findGreater(arr[i], i + 1, arr, Size);         if (j < 0) {           continue;         }         k = findLesser(arr[j], j + 1, arr, Size);         if (k < 0) {           continue;         }         cout <<  YES  << endl;         cout << i + 1 <<     << j + 1 <<     << k + 1 << endl;         break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int next_x[8] = {0, 1, -1, 0, 1, 1, -1, -1};   const int next_y[8] = {1, 0, 0, -1, 1, -1, -1, 1};   const int inf = 1e9 + 5;   const long long linf = 1e18 + 5;   const double PI = acos(-1.0);   const int MAXN = 1e6 + 5;   const int N = 4e6 + 5;   const long double eps = 1e-8;   const long double fix = 1e-2;   int n;   int a[MAXN], b[MAXN];   int l[MAXN], r[MAXN];   int s[MAXN], h = 0;   vector<int> st[MAXN], ed[MAXN];   int cnt[MAXN];   bool flag = 1;   struct node {     int i;     bool operator<(const node &t) const { return r[i] < r[t.i]; }   };   int work() {     scanf( %d , &n);     for (int i = (1), I = (2 * n) + 1; i < I; ++i) {       char t[5];       scanf( %s , t);       if (t[0] ==  + ) continue;       scanf( %d , &a[i]);     }     for (int i = (1), I = (2 * n) + 1; i < I; ++i)       if (a[i]) {         while (h && a[i] > a[s[h]]) --h;         l[a[i]] = s[h] + 1, r[a[i]] = i - 1, s[++h] = i;       }     for (int x = (1), I = (n) + 1; x < I; ++x)       st[l[x]].push_back(x), ed[r[x]].push_back(x), flag &= (r[x] >= l[x]);     set<node> s;     for (int i = (1), I = (2 * n) + 1; i < I; ++i) {       for (auto x : st[i]) s.insert({x});       if (!a[i]) {         if (s.empty())           flag = 0;         else           b[i] = s.begin()->i, s.erase(s.begin());       }       for (auto x : ed[i])         if (s.find({x}) != s.end()) flag = 0;     }     for (int i = (1), I = (2 * n) + 1; i < I; ++i)       if (b[i]) cnt[b[i]]++;     for (int i = (1), I = (n) + 1; i < I; ++i) flag &= (cnt[i] == 1);     if (!flag) return puts( NO );     puts( YES );     for (int i = (1), I = (2 * n) + 1; i < I; ++i)       if (b[i]) printf( %d  , b[i]);     return 0;   }   int main() {     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = INT_MAX;   const long double PI = acos(-1);   const long double E = 2.71828183;   const int MOD = 1e9 + 7;   vector<int> bfs(vector<vector<int> > adj, int src) {     queue<int> q;     q.push(src);     vector<int> dst(adj.size(), INF);     dst[src] = 0;     while (!q.empty()) {       int c = q.front();       q.pop();       for (int nbr : adj[c]) {         if (dst[nbr] == INF) {           dst[nbr] = dst[c] + 1;           q.push(nbr);         }       }     }     return dst;   }   vector<int> bfsParents(vector<vector<int> > adj, int src) {     vector<int> parents = vector<int>(adj.size(), INF);     parents[src] = -1;     queue<int> q;     q.push(src);     vector<int> dst(adj.size(), INF);     dst[src] = 0;     while (!q.empty()) {       int c = q.front();       q.pop();       for (int nbr : adj[c]) {         if (dst[nbr] == INF) {           parents[nbr] = c;           dst[nbr] = dst[c] + 1;           q.push(nbr);         }       }     }     return parents;   }   list<int> shortestPath(vector<vector<int> > adj, int src, int dst) {     vector<int> bfsParetns = bfsParents(adj, src);     list<int> ans;     ans.push_back(dst);     while (ans.front() != src) ans.push_front(bfsParetns[ans.front()]);     return ans;   }   long long powMod(long long base, long long exp, long long mod) {     long long ans = 1;     for (long long i = 0; i < exp; i++) ans = ((ans % mod) * (base % mod)) % mod;     return ans;   }   vector<int> divisorsOf(int n) {     vector<int> ans;     for (int i = 1; i * i <= n; i++) {       if (n % i == 0) ans.push_back(i);       if (i * i != n && i != 1) ans.push_back(n / i);     }     return ans;   }   long long gcd(long long a, long long b) {     return (a % b == 0 ? b : gcd(b, a % b));   }   long double log(long double a, long double b) { return log(b) / log(a); }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   long long power(long long base, long long exp) {     long long res = 1;     while (exp) {       exp--;       res *= base;     }     return res;   }   void solve();   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     while (t--) solve();     return 0;   }   void solve() {     long long n, k;     cin >> n >> k;     string s;     cin >> s;     vector<char> v(k);     for (int i = 0; i < k; i++) cin >> v[i];     long long curr = 0;     long long ans = 0;     for (int i = 0; i < n; i++) {       bool in = false;       for (char c : v)         if (c == s[i]) in = true;       if (in) curr++;       if (!in || (in && i == n - 1)) {         ans += curr * (curr + 1) / 2;         curr = 0;       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a << endl;     err(++it, args...);   }   long long mod = 1e9 + 7;   long long powm(long long a, long long b, long long mod) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % mod;       a = (a * a) % mod;       b >>= 1;     }     return res;   }   bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {     return (a.first > b.first);   }   void solve() {     int n, m;     cin >> n >> m;     string s[n];     for (long long i = 0; i < (n); i++) cin >> s[i];     int mat1[n][m];     int mat2[n][m];     int mat3[n][m];     int mat4[n][m];     memset(mat1, 0, sizeof(mat1));     memset(mat2, 0, sizeof(mat2));     memset(mat3, 0, sizeof(mat3));     memset(mat4, 0, sizeof(mat4));     for (long long i = 0; i < (n); i++) {       for (long long j = 0; j < (m); j++) {         if (s[i][j] ==  * ) {           if (j - 1 >= 0)             mat1[i][j] += 1 + mat1[i][j - 1];           else             mat1[i][j] = 1;         }       }     }     for (long long i = 0; i < (n); i++) {       for (long long j = (m)-1; j >= 0; j--) {         if (s[i][j] ==  * ) {           if (j + 1 < m)             mat2[i][j] += 1 + mat2[i][j + 1];           else             mat2[i][j] = 1;         }       }     }     for (long long j = 0; j < (m); j++) {       for (long long i = 0; i < (n); i++) {         if (s[i][j] ==  * ) {           if (i - 1 >= 0)             mat3[i][j] += 1 + mat3[i - 1][j];           else             mat3[i][j] = 1;         }       }     }     for (long long j = 0; j < (m); j++) {       for (long long i = (n)-1; i >= 0; i--) {         if (s[i][j] ==  * ) {           if (i + 1 < n)             mat4[i][j] += 1 + mat4[i + 1][j];           else             mat4[i][j] = 1;         }       }     }     int temp_mat2[n + 1][m + 1];     int temp_mat1[n + 1][m + 1];     memset(temp_mat1, 0, sizeof(temp_mat1));     memset(temp_mat2, 0, sizeof(temp_mat2));     vector<tuple<int, int, int> > v;     for (long long i = 0; i < (n); i++) {       for (long long j = 0; j < (m); j++) {         if (mat1[i][j] > 1 and mat2[i][j] > 1 and mat3[i][j] > 1 and             mat4[i][j] > 1) {           temp_mat1[i][j - min({mat1[i][j], mat2[i][j], mat3[i][j], mat4[i][j]}) +                        1]++;           temp_mat1[i]                    [j + min({mat1[i][j], mat2[i][j], mat3[i][j], mat4[i][j]})]--;           temp_mat2[i - min({mat1[i][j], mat2[i][j], mat3[i][j], mat4[i][j]}) + 1]                    [j]++;           temp_mat2[i + min({mat1[i][j], mat2[i][j], mat3[i][j], mat4[i][j]})]                    [j]--;           v.push_back(make_tuple(               i + 1, j + 1,               min({mat1[i][j], mat2[i][j], mat3[i][j], mat4[i][j]}) - 1));         }       }     }     for (long long i = 0; i < (n); i++) {       for (long long j = 0; j < (m); j++) {         if (j > 0) temp_mat1[i][j] += temp_mat1[i][j - 1];       }     }     for (long long j = 0; j < (m); j++) {       for (long long i = 0; i < (n); i++) {         if (i > 0) temp_mat2[i][j] += temp_mat2[i - 1][j];       }     }     for (long long i = 0; i < (n); i++) {       for (long long j = 0; j < (m); j++) {         if (s[i][j] ==  * ) {           if (temp_mat1[i][j] == 0 and temp_mat2[i][j] == 0) {             cout << -1 << endl;             return;           }         }       }     }     cout << v.size() << endl;     for (long long i = 0; i < (v.size()); i++) {       cout << (get<0>(v[i])) <<     << (get<1>(v[i])) <<     << (get<2>(v[i]))            << endl;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();   }


#include <bits/stdc++.h>   using namespace std;   int n, a, sum, high, w, low;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cout << setprecision(10) << fixed;     cin >> n >> w;     for (int i = 1; i <= n; i++) {       cin >> a;       sum += a;       if (sum > high) {         high = sum;       }       if (sum < low) {         low = sum;       }     }     cout << max(w - high + 1 + low, 0) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 10;   const double INF = 1e18;   int n, m, c;   int a[N], p[N], f[N];   double E[N];   struct Node {     double L, M, R, S;     Node() {}     Node(double L, double M, double R, double S) : L(L), M(M), R(R), S(S) {}   };   struct SegmentTree {     Node st[4 * N];     Node Merge(Node A, Node B) {       Node C;       C.L = max(A.L, A.S + B.L);       C.R = max(B.R, B.S + A.R);       C.M = max(A.M, max(B.M, A.R + B.L));       C.S = A.S + B.S;       return C;     }     void Init(int k, int l, int r) {       if (l == r) {         double val = E[l];         st[k] = Node(val, val, val, val);         return;       }       Init(k << 1, l, ((l + r) >> 1));       Init(k << 1 | 1, ((l + r) >> 1) + 1, r);       st[k] = Merge(st[k << 1], st[k << 1 | 1]);     }     Node Query(int k, int l, int r, int i, int j) {       if (l > j || r < i) return Node(-INF, -INF, -INF, 0);       if (i <= l && r <= j) return st[k];       return Merge(Query(k << 1, l, ((l + r) >> 1), i, j),                    Query(k << 1 | 1, ((l + r) >> 1) + 1, r, i, j));     }   } ST;   double Query(int l, int r) {     Node ans = ST.Query(1, 1, n - 1, l, r);     return max(ans.L, max(ans.R, ans.M));   }   int main() {     cin >> n >> m >> c;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 1; i < n; ++i) cin >> p[i];     for (int i = 1; i < n; ++i) {       double prob = (double)p[i] / 100;       double val = (double)(a[i + 1] - a[i]) / 2 - prob * c;       E[i] = val;     }     ST.Init(1, 1, n - 1);     double ans = 0;     while (m--) {       int u, v;       cin >> u >> v;       ans += max(0.00, Query(u, v - 1));     }     cout << fixed << setprecision(10) << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int memo[500][26][26];   int son[500][26][26];   int cambios[500][26][26];   vector<string> A;   int m, n;   void solve(int x, int y, int z) {     for (int row = 0; row < x; row++)       for (int a = 0; a < y; a++)         for (int b = 0; b < z; b++) {           int &res = memo[row][a][b];           if (row == 0) {             res = cambios[0][a][b];             continue;           }           int mn = 1 << 29;           for (int i = 0; i < 26; i++)             if (i != a)               for (int j = 0; j < 26; j++)                 if (j != b) {                   int tmp = memo[row - 1][i][j] + cambios[row][a][b];                   if (mn > tmp) mn = tmp, son[row][a][b] = 1000 * i + j;                 }           res = mn;         }   }   int main() {     while (cin >> m >> n) {       memset(memo, -1, sizeof(memo));       A.clear();       string cad;       for (int i = 0; i < m; i++) cin >> cad, A.push_back(cad);       reverse((A).begin(), (A).end());       for (int row = 0; row < m; row++) {         for (int a = 0; a < 26; a++)           for (int b = 0; b < 26; b++)             if (b != a) {               int s = 0;               for (int k = 0; k < n; k++) {                 if ((k & 1) == 0 && A[row][k] -  a  != a) s++;                 if ((k & 1) == 1 && A[row][k] -  a  != b) s++;               }               cambios[row][a][b] = s;             } else               cambios[row][a][b] = 1 << 29;       }       solve(m, 26, 26);       int mn = 1 << 29;       int a, b;       for (int i = 0; i < 26; i++)         for (int j = 0; j < 26; j++)           if (j != i) {             if (mn > memo[m - 1][i][j]) mn = memo[m - 1][i][j], a = i, b = j;           }       printf( %d n , mn);       for (int row = m - 1; row >= 0; row--) {         for (int i = 0; i < n; i++) {           if ((i & 1) == 0)             cout << char(a +  a );           else             cout << char(b +  a );         }         cout << endl;         int tmp = son[row][a][b];         a = tmp / 1000;         b = tmp % 1000;       }     }   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int to, next;   } e[200005 * 2];   int d[200005], dl[200005], dr[200005], dd[200005];   int head[200005], q[200005], id[200005], a[200005];   long long ans;   int n, tot;   bool cmp(int x, int y) {     if (dd[x] != dd[y]) return dd[x] > dd[y];     return dl[x] > dl[y];   }   void add(int x, int y) {     e[++tot] = (edge){y, head[x]};     head[x] = tot;   }   void bfs(int S, int *d) {     for (int i = 1; i <= n; i++) d[i] = -1;     d[S] = 0;     q[1] = S;     for (int h = 0, t = 1; h != t;)       for (int x = q[++h], i = head[x]; i; i = e[i].next)         if (d[e[i].to] == -1) d[e[i].to] = d[x] + a[e[i].to], q[++t] = e[i].to;   }   int findmax(int *d) {     int mx = 1;     for (int i = 2; i <= n; i++)       if (d[mx] < d[i]) mx = i;     return mx;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       add(x, y);       add(y, x);     }     for (int i = 1; i <= n; i++) a[i] = 1;     bfs(1, d);     int l = findmax(d);     bfs(l, dl);     int r = findmax(dl);     bfs(r, dr);     for (int i = 1; i <= n; i++)       if (dl[i] + dr[i] == dl[r])         a[i] = 0;       else         ans += max(dl[i], dr[i]);     ans += 1ll * dl[r] * (dl[r] + 1) / 2;     printf( %I64d n , ans);     bfs(l, dd);     for (int i = 1; i <= n; i++) id[i] = i;     sort(id + 1, id + n + 1, cmp);     for (int i = 1; i < n; i++)       if (dd[id[i]]) {         int tmp = dl[id[i]] > dr[id[i]] ? l : r;         printf( %d %d %d n , tmp, id[i], id[i]);       } else         printf( %d %d %d n , l, id[i], id[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300010, INF = 0x3f3f3f3f;   template <typename Tp>   inline int getmin(Tp &x, Tp y) {     return y < x ? x = y, 1 : 0;   }   template <typename Tp>   inline int getmax(Tp &x, Tp y) {     return y > x ? x = y, 1 : 0;   }   template <typename Tp>   inline void read(Tp &x) {     x = 0;     int f = 0;     char ch = getchar();     while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar();     if (ch ==  - ) f = 1, ch = getchar();     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     if (f) x = -x;   }   struct data {     int v, nxt;   } edge[maxn << 1];   int n, p, k, tot, head[maxn], w[maxn], mark[maxn];   long long mx = -INF, f[maxn];   void insert(int u, int v) {     edge[++p] = (data){v, head[u]};     head[u] = p;     edge[++p] = (data){u, head[v]};     head[v] = p;   }   void input() {     int u, v;     read(n);     for (int i = 1; i <= n; i++) read(w[i]);     for (int i = 1; i < n; i++) {       read(u);       read(v);       insert(u, v);     }   }   void dp(int x, int pre) {     f[x] = w[x];     for (int i = head[x]; i; i = edge[i].nxt)       if (edge[i].v ^ pre) {         dp(edge[i].v, x);         if (f[edge[i].v] > 0) f[x] += f[edge[i].v], mark[edge[i].v] = 1;       }     getmax(mx, f[x]);   }   void getans(int x, int pre) {     f[x] = w[x];     for (int i = head[x]; i; i = edge[i].nxt)       if (edge[i].v ^ pre) {         getans(edge[i].v, x);         if (f[edge[i].v] > 0) f[x] += f[edge[i].v], mark[edge[i].v] = 1;       }     if (f[x] == mx) ++k, f[x] = 0;   }   int main() {     input();     dp(1, 1);     getans(1, 1);     printf( %lld %d n , mx * k, k);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int maxn = 100000 + 5;   const long double eps = 1e-6;   long double PI = acos((long double)(-1));   long long n, m, par[maxn][20], cnt[maxn], st[maxn], fi[maxn], ti;   long long h[maxn];   long double qd[maxn], qu[maxn];   vector<int> e[maxn];   void dfs(int u, int p = -1) {     st[u] = ti++;     h[u] = ~p ? h[p] + 1 : 0;     par[u][0] = p;     bool leaf = 1;     for (int i = 1; i < 20; i++) {       if (~par[u][i - 1]) par[u][i] = par[par[u][i - 1]][i - 1];     }     int sz = e[u].size();     for (int i = 0; i < sz; i++) {       int v = e[u][i];       if (v != p) {         leaf = 0;         dfs(v, u);         cnt[u] += cnt[v] + 1;       }     }     for (int i = 0; i < sz; i++) {       int v = e[u][i];       if (v != p) qd[u] += 1.0 * cnt[v] / cnt[u] * qd[v];     }     if (~leaf) qd[u] += 1;     fi[u] = ti++;   }   void dfs_d(int u, int p = -1) {     if (~p) {       long double cntd = cnt[p] - cnt[u] - 1, nou, kcntd, kasr;       if (cntd == 0) {         kcntd = 1.0 / (n - cnt[u] - 1);         qu[u] = qu[p] * kcntd * (n - cnt[p] - 1) + (n - cnt[p]) * kcntd;       } else {         kcntd = 1.0 / cntd;         nou = qd[p] * kcntd * cnt[p] - qd[u] * kcntd * cnt[u] - cnt[u] * kcntd -               kcntd;         kasr = 1.0 / (n - cnt[u] - 1);         qu[u] = cntd * kasr * nou + qu[p] * kasr * (n - cnt[p] - 1) + 1;       }     }     int sz = e[u].size();     for (int i = 0; i < sz; i++) {       int v = e[u][i];       if (v != p) dfs_d(v, u);     }   }   int ispar(int u, int v) { return st[u] <= st[v] && fi[v] <= fi[u]; }   int up(int u, int d) {     for (int i = 20 - 1; i >= 0; i--)       if ((d >> i) & 1) u = par[u][i];     return u;   }   int lca(int u, int v) {     if (h[u] < h[v]) swap(u, v);     u = up(u, h[u] - h[v]);     if (u == v) return v;     for (int i = 20 - 1; i >= 0; i--) {       if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];     }     return par[u][0];   }   long double query(int u, int v) {     if (h[u] > h[v]) swap(u, v);     if (ispar(u, v)) {       int w = up(v, h[v] - h[u] - 1);       long double cn = 1.0 / (cnt[v] + 1) / (n - cnt[w] - 1);       long double ans = cnt[v] * (n - cnt[w] - 1) * cn * qd[v] +                         (n - cnt[w] - 1) * (cnt[v] + 1) * cn * qu[w];       return ans + h[v] - h[u];     }     int w = lca(u, v);     long double cn = 1.0 / (cnt[u] + 1) / (cnt[v] + 1);     long double ans =         cnt[u] * (cnt[v] + 1) * cn * qd[u] + cnt[v] * (cnt[u] + 1) * cn * qd[v];     return h[u] - h[w] + h[v] - h[w] + ans + 1;   }   int main() {     std::ios_base::sync_with_stdio(false);     cin >> n >> m;     int u, v;     for (int i = 0; i < n - 1; i++) {       cin >> u >> v;       u--;       v--;       e[u].push_back(v);       e[v].push_back(u);     }     dfs(0);     dfs_d(0);     cout << fixed << setprecision(8);     while (m--) {       cin >> u >> v;       cout << query(--u, --v) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)(1e5) + 5;   int l[maxn], r[maxn];   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 1; i <= n; ++i) cin >> l[i] >> r[i];     sort(l + 1, l + n + 1);     sort(r + 1, r + n + 1);     long long ret = n;     for (int i = 1; i <= n; ++i) ret += max(l[i], r[i]);     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   ostream &operator<<(ostream &out, string str) {     for (char c : str) out << c;     return out;   }   template <class L, class R>   ostream &operator<<(ostream &out, pair<L, R> p) {     return out <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <class T>   auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {     out <<  { ;     for (auto &e : x) out << e << (&e == &*--x.end() ?    :  ,  );     return out <<  } ;   }   template <class... Args>   void dump(Args &&...args) {     ((cerr << args <<  ;   ), ...);   }   template <class T>   int size(T &&a) {     return (int)a.size();   }   using LL = long long;   using PII = pair<int, int>;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     for (int _t = 0; _t < t; _t++) {       int l, r;       cin >> l >> r;       if (2 * l > r)         cout <<  -1 -1 n ;       else         cout << l <<     << 2 * l <<   n ;     }   }


#include <bits/stdc++.h>   char str[100005], ans[100005];   int out[100005];   struct Edge {     int link[400005], next[400005], son[400005], l;     void clear() {       l = 0;       memset(son, 0, sizeof(son));     }     void addedge(int x, int y) {       link[++l] = y;       next[l] = son[x];       son[x] = l;     }   };   Edge e;   int top = 0;   int main() {     scanf( %s , str);     int l = strlen(str);     for (int i = l - 1; i >= 0; --i) e.addedge(str[i], i);     for (char c =  z ; c >=  a ; --c)       for (int p = e.son[c]; p; p = e.next[p])         if (top == 0 || e.link[p] > out[top]) out[++top] = e.link[p];     for (int i = 1; i <= top; ++i) ans[i - 1] = str[out[i]];     printf( %s n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0), cin.tie(0);     int arr[4];     for (int i = 0; i < (int)4; i++) cin >> arr[i];     sort(arr, arr + 4);     bool ok = 0;     for (int mask = 0; mask < (1 << 4); mask++) {       int sum1 = 0;       int sum2 = 0;       for (int bit = 0; bit < 4; bit++) {         if (mask & (1 << bit)) {           sum1 += arr[bit];         } else {           sum2 += arr[bit];         }       }       if (sum1 == sum2) {         ok = 1;         break;       }     }     if (ok)       cout <<  YES  <<   n ;     else       cout <<  NO  <<   n ;     return 0;   }