#include <bits/stdc++.h>   using namespace std;   long long a[18] = {0};   long long int get(long long int n, int l) {     if (!n) return 0;     if (l == 1) return n;     long long int t = a[l];     if (n == t) return l;     if (n < t)       return min(get(n, l - 1), l + get(t - n, l - 1));     else       return min((n / t) * l + get(n % t, l - 1),                  ((1 + (n / t)) * l) + get(t - n % t, l - 1));   }   int main() {     long long int n, i, ans;     scanf( %I64d , &n);     for (int i = 1; i < 18; i++) a[i] = a[i - 1] * 10 + 1;     for (i = 1; n > a[i]; i++)       ;     ans = get(n, i);     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, n, p, c;   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d%d , &n, &p), c = 2 * n + p;       for (int i = 1; i <= n; ++i)         for (int j = i + 1; j <= n; ++j)           if (c) printf( %d %d n , i, j), --c;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > g[500100];   long long f[500100][2];   int n, k;   void dfs(int u, int fa) {     vector<long long> d;     d.clear();     f[u][0] = f[u][1] = 0;     for (int i = 0; i < g[u].size(); i++) {       int v = g[u][i].first, w = g[u][i].second;       if (v == fa) continue;       dfs(v, u);       f[u][0] += f[v][1];       f[u][1] += f[v][1];       d.push_back(-(w + f[v][0] - f[v][1]));     }     sort(d.begin(), d.end());     for (int i = 0; i < d.size() && i < k; i++) {       long long D = -d[i];       if (D <= 0) break;       if (i < k - 1) f[u][0] += D;       f[u][1] += D;     }   }   void ae(int x, int y, int z) { g[x].push_back(make_pair(y, z)); }   void solve() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) g[i].clear();     int x, y, z;     for (int i = 1; i < n; i++) {       scanf( %d%d%d , &x, &y, &z);       ae(x, y, z);       ae(y, x, z);     }     dfs(1, 0);     printf( %lld n , f[1][1]);   }   int main() {     int tc;     scanf( %d , &tc);     while (tc--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 10;   int a[MAXN], b[MAXN], f[260], n, k;   int main(void) {     ios::sync_with_stdio(false);     cin >> n >> k;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       b[i] = a[i];     }     memset(f, -1, sizeof f);     for (int i = 1; i <= n; ++i) {       if (f[a[i]] == -1) {         int low = -1;         for (int j = a[i]; j >= a[i] - k + 1 && j >= 0; --j) {           if (f[j] != -1) {             if (f[j] >= a[i] - k + 1) low = f[j];             break;           }           low = j;         }         for (int j = low; j <= a[i]; ++j) {           f[j] = low;         }       }       b[i] = f[a[i]];     }     for (int i = 1; i <= n; ++i) {       cout << b[i] <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ben[11];   int n, k;   int mem[505][505 * 11];   int arr[505 * 505];   int freqFav[100005];   int freqCards[100005];   int solve(int num, int rem) {     if (rem == 0 || num == 0) return 0;     int &ret = mem[num][rem];     if (~ret) return ret;     for (int i = 0; i < min(k, rem); i++) {       ret = max(ret, ben[i] + solve(num - 1, rem - i - 1));     }     return ret;   }   int main() {     memset(mem, -1, sizeof(mem));     scanf( %d %d , &n, &k);     for (int i = 0; i < k * n; i++) {       scanf( %d , arr + i);       freqCards[arr[i]]++;     }     for (int i = 0; i < n; i++) {       int fav;       scanf( %d , &fav);       freqFav[fav]++;     }     for (int i = 0; i < k; i++) {       scanf( %d , ben + i);     }     int res = 0;     for (int i = 0; i < 100005; i++) {       res += solve(freqFav[i], freqCards[i]);     }     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sz = 4e6 + 50;   int n, m;   string str;   int s[sz], e[sz], four[sz], seven[sz], non_dec[sz], non_inc[sz];   vector<bool> lazy(sz, 0);   void calculate(int node) {     int lc = node * 2;     int rc = lc + 1;     int f1 = four[lc], s1 = seven[lc], nd1 = non_dec[lc], ni1 = non_inc[lc];     int f2 = four[rc], s2 = seven[rc], nd2 = non_dec[rc], ni2 = non_inc[rc];     if (lazy[lc]) {       swap(f1, s1);       swap(nd1, ni1);     }     if (lazy[rc]) {       swap(f2, s2);       swap(nd2, ni2);     }     four[node] = f1 + f2;     seven[node] = s1 + s2;     non_dec[node] = max(f1 + nd2, nd1 + s2);     non_inc[node] = max(s1 + ni2, ni1 + f2);   }   void modify(int node) {     int lc = node * 2;     int rc = lc + 1;     four[node] = four[lc] + four[rc];     seven[node] = seven[lc] + seven[rc];     non_dec[node] = max(four[lc] + non_dec[rc], non_dec[lc] + seven[rc]);     non_inc[node] = max(seven[lc] + non_inc[rc], non_inc[lc] + four[rc]);   }   void lazyPropagation(int node) {     if (lazy[node]) {       lazy[2 * node] = !lazy[2 * node];       lazy[2 * node + 1] = !lazy[2 * node + 1];     }     lazy[node] = 0;   }   void build(int node, int l, int r) {     s[node] = l;     e[node] = r;     if (l == r) {       if (str[l] ==  4 ) {         four[node] = 1;         seven[node] = 0;         non_dec[node] = 1;         non_inc[node] = 1;       } else {         four[node] = 0;         seven[node] = 1;         non_dec[node] = 1;         non_inc[node] = 1;       }       return;     }     int mid = (l + r) / 2;     build(2 * node, l, mid);     build(2 * node + 1, mid + 1, r);     modify(node);   }   void update(int node, int l, int r) {     if (s[node] > r || e[node] < l) return;     if (l <= s[node] && e[node] <= r) {       lazy[node] = !lazy[node];       return;     }     lazyPropagation(node);     update(2 * node, l, r);     update(2 * node + 1, l, r);     calculate(node);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string tt;     int x, y;     cin >> n >> m;     cin >> str;     str =  0  + str;     build(1, 1, n);     while (m--) {       cin >> tt;       if (tt[0] ==  c ) {         if (!lazy[1])           cout << non_dec[1] <<   n ;         else           cout << non_inc[1] <<   n ;       } else {         cin >> x >> y;         update(1, x, y);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int rnd(int l, int r) { return l + rng() % (r - l + 1); }   const int N = 200010;   int n;   long long ret;   int a[N], lz[N], b[N];   int f[N][25];   int que(int i, int j) {     int t = lz[j - i + 1];     return max(f[i][t], f[j - (1 << t) + 1][t]);   }   void dnc(int l, int r) {     if (l >= r) return;     int x = que(l, r);     int p = b[x];     if (p - l < r - p)       for (int i = l, _c = p - 1; i <= _c; ++i)         ret += r >= b[x - a[i]] && b[x - a[i]] > p;     else       for (int i = p + 1, _c = r; i <= _c; ++i)         ret += l <= b[x - a[i]] && b[x - a[i]] < p;     dnc(l, p - 1);     dnc(p + 1, r);   }   int main() {     n = ({       int x = 0;       int c = getchar(), n = 0;       for (; !isdigit(c); c = getchar()) n = (c ==  - );       for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;       n ? -x : x;     });     for (int i = 1, _c = n; i <= _c; ++i)       a[i] = f[i][0] = ({         int x = 0;         int c = getchar(), n = 0;         for (; !isdigit(c); c = getchar()) n = (c ==  - );         for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;         n ? -x : x;       }),       b[a[i]] = i, lz[i] = log2(i);     for (int j = 1, _c = 20; j <= _c; ++j)       for (int i = 1, _c = n; i <= _c; ++i) {         if (i + (1 << j - 1) > n) break;         f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);       }     dnc(1, n);     cout << ret;   }


#include <bits/stdc++.h>   using namespace std;   int arr[3010][3010], height[3010][3010];   deque<pair<int, int> > mdq;   void push(int val, int pos) {     while (!mdq.empty() && mdq.back().first > val) mdq.pop_back();     mdq.push_back(make_pair(val, pos));   }   int front(int lim) {     while (!mdq.empty() && mdq.front().second < lim) mdq.pop_front();     return mdq.front().first;   }   void clear() { mdq.clear(); }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int row, column, r1, c1;     long long int g0, x, y, z;     cin >> row >> column >> r1 >> c1;     cin >> g0 >> x >> y >> z;     long long int g = g0;     for (int i = 0; i < row; i++) {       for (int j = 0; j < column; j++) {         arr[i][j] = g;         g = (g * x + y) % z;       }     }     for (int i = 0; i < row; i++) {       clear();       for (int j = 0; j < c1; j++) push(arr[i][j], j);       for (int j = 0; j + c1 <= column; j++) {         height[i][j] = front(j);         if (j + c1 < column) push(arr[i][j + c1], j + c1);       }     }     for (int j = 0; j + c1 <= column; j++) {       clear();       for (int i = 0; i < r1; i++) push(height[i][j], i);       for (int i = 0; i + r1 <= row; i++) {         height[i][j] = front(i);         if (i + r1 < row) push(height[i + r1][j], i + r1);       }     }     long long int ans = 0;     for (int i = 0; i + r1 <= row; i++)       for (int j = 0; j + c1 <= column; j++) ans += height[i][j];     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   double ans;   int n, cnt, C;   int head, tail;   int q[3005], d[3005], d2[3005];   bool vis[3005];   vector<int> e[3005];   void dfs(int x) {     vis[x] = 1;     for (int i = 0; i < e[x].size(); i++)       if (!vis[e[x][i]]) {         d2[e[x][i]] = d2[x] + 1;         if (!d[e[x][i]]) {           d[e[x][i]] = d[x] + 1;           ans += 1.0 / d[e[x][i]];         } else           ans =               ans + 1.0 / d2[e[x][i]] - 2.0 / (d[e[x][i]] + d2[e[x][i]] + C - 2);         dfs(e[x][i]);       }     vis[x] = 0;   }   int main() {     n = read();     for (int i = 1; i <= n; i++) {       int u = read(), v = read();       u++;       v++;       e[u].push_back(v);       e[v].push_back(u);       d[u]++;       d[v]++;     }     for (int i = 1; i <= n; i++)       if (d[i] == 1) q[tail++] = i;     while (head != tail) {       int now = q[head];       head++;       for (int i = 0; i < e[now].size(); i++) {         d[e[now][i]]--;         if (d[e[now][i]] == 1) q[tail++] = e[now][i];       }     }     C = n - tail;     for (int i = 1; i <= n; i++) {       memset(d, 0, sizeof(d));       memset(d2, 0, sizeof(d2));       d[i] = d2[i] = 1;       dfs(i);     }     printf( %.10lf n , ans + n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void read(T& num) {     bool start = false, neg = false;     char c;     num = 0;     while ((c = getchar()) != EOF) {       if (c ==  - )         start = neg = true;       else if (c >=  0  && c <=  9 ) {         start = true;         num = num * 10 + c -  0 ;       } else if (start)         break;     }     if (neg) num = -num;   }   inline int Q(int x, int y) {     printf( 0 %d %d n , x, y);     fflush(stdout);     scanf( %d , &x);     return x;   }   set<int> c;   void dfs(int l, int r) {     if (l > r) return;     int mid = l + (r - l) / 2;     int d = Q(mid, mid);     if (!d) c.insert(mid), d++;     dfs(l, mid - d);     dfs(mid + d, r);   }   set<int> V, H;   int main() {     int n = 0, m = 0;     int mx = (int)(1e8);     dfs(-mx, mx);     int s = mx;     for (; c.count(s); s--)       ;     for (set<int>::iterator it = c.begin(); it != c.end(); it++) {       int x = (*it);       if (!Q(x, s)) V.insert(x), ++n;       if (!Q(s, x)) H.insert(x), ++m;     }     printf( 1 %d %d n , n, m);     for (set<int>::iterator it = V.begin(); it != V.end(); it++)       printf( %d  , (*it));     putchar(  n );     for (set<int>::iterator it = H.begin(); it != H.end(); it++)       printf( %d  , (*it));     putchar(  n );     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000000 + 5];   int b[1000000 + 5];   int main() {     ios_base::sync_with_stdio(false);     int i, j, n, k;     queue<pair<int, int> > q;     cin >> n;     for (i = 1; i <= n; i++) cin >> a[i];     for (i = 1; i <= n; i++) cin >> b[i];     for (i = 1; i <= n; i++) {       if (a[i] != b[i]) {         for (j = i + 1; j <= n; j++) {           if (a[i] == b[j]) {             k = j;             break;           }         }         for (j = k; j > i; j--) {           q.push(make_pair(j - 1, j));           int t = b[j];           b[j] = b[j - 1];           b[j - 1] = t;         }       }     }     cout << q.size() << endl;     while (q.empty() != 1) {       cout << q.front().first <<     << q.front().second << endl;       q.pop();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int bignumlen = 2200;   const int Blen = 8;   const long long base = 100000000;   struct bignum {     int len;     long long data[bignumlen];     long long &operator[](int x) { return (data[x]); }     const long long &operator[](int x) const { return (data[x]); }     bignum() {       memset(data, 0, sizeof(data));       len = 0;     }     void clear() {       for (int i = len; i >= 1; --i) data[i] = 0;       len = 0;     }     int check(const bignum &a, const bignum &b) {       if (a.len > b.len) return (0);       if (b.len > a.len) return (1);       for (int i = a.len; i >= 1; --i) {         if (a.data[i] < b.data[i]) return (1);         if (b.data[i] < a.data[i]) return (0);       }       return 2;     }     bool operator<(const bignum &b) { return (check(*this, b) == 1); }     bool operator>(const bignum &b) { return (check(*this, b) == 0); }     bool operator<=(const bignum &b) { return (check(*this, b) >= 1); }     bool operator>=(const bignum &b) { return (check(*this, b) % 2 == 0); }     bool operator!=(const bignum &b) { return (check(*this, b) != 2); }     bool operator==(const bignum &b) { return (check(*this, b) == 2); }     bignum operator=(const bignum &x) {       for (int i = x.len + 1; i <= len; ++i) data[i] = 0;       for (int i = 1; i <= x.len; ++i) data[i] = x.data[i];       len = x.len;       return *this;     }     bignum operator=(long long x) {       for (int i = len; i >= 0; --i) data[i] = 0;       len = 0;       while (x) {         data[++len] = x % base;         x /= base;       }       return *this;     }     bignum(long long x) {       memset(data, 0, sizeof(data));       len = 0;       (*this) = x;     }     bignum operator*(const bignum &b) {       int i, j;       bignum tmp;       for (i = 1; i <= len; ++i)         if (data[i] != 0)           for (j = 1; j <= b.len; ++j)             if (b.data[j] != 0) {               tmp.data[i + j - 1] += data[i] * b.data[j];               tmp.data[i + j] += tmp.data[i + j - 1] / base;               tmp.data[i + j - 1] %= base;             }       tmp.len = len + b.len - 1;       while (tmp.data[tmp.len + 1]) tmp.len++;       return tmp;     }     bignum operator*(long long x) {       int i;       bignum tmp;       for (i = 1; i <= len; ++i) tmp[i] = data[i] * x;       tmp.len = len;       for (i = 1; i <= len; ++i) {         tmp[i + 1] += tmp[i] / base, tmp[i] %= base;         if (tmp[i + 1] && i + 1 > tmp.len) tmp.len++;       }       return tmp;     }     bignum operator/(long long x) {       int i;       bignum tmp;       long long y = 0;       for (i = len; i >= 1; --i) {         y = y * base + data[i];         tmp[i] = y / x;         y %= x;       }       tmp.len = len;       while (tmp[tmp.len] == 0 && tmp.len > 1) tmp.len--;       return tmp;     }     bignum operator/(const bignum &b) {       if (b.len <= 1 && b[1] == 0) {         printf( error! ?0?! );         for (;;)           ;       }       int i, l1 = (len - 1) * Blen, l2 = (b.len - 1) * Blen;       long long x = data[len], y = b[b.len];       while (x) x /= 10, l1++;       while (y) y /= 10, l2++;       bignum tmp, chu, B;       chu = *this;       B = b;       for (i = 1; i * Blen <= l1 - l2; ++i) B *= base;       for (i = 1; i <= (l1 - l2) % Blen; ++i) B *= 10;       for (i = l1 - l2; i >= 0; --i) {         x = 0;         while (chu >= B) chu -= B, x++;         tmp[i / Blen + 1] = tmp[i / Blen + 1] * 10 + x;         B /= 10;       }       tmp.len = (l1 - l2) / Blen + 1;       while (tmp.len >= 1 && !tmp[tmp.len]) tmp.len--;       return tmp;     }     bignum operator+(const bignum &b) {       bignum tmp;       int i, l = max(len, b.len);       for (i = 1; i <= l; ++i) tmp[i] = data[i] + b[i];       for (i = 1; i <= l; ++i) tmp[i + 1] += tmp[i] / base, tmp[i] %= base;       tmp.len = l;       if (tmp[tmp.len + 1]) tmp.len++;       return tmp;     }     bignum operator+(long long x) {       bignum tmp;       tmp = *this;       tmp[1] += x;       for (int i = 1; i <= len && tmp[i] >= base; ++i)         tmp[i + 1] += tmp[i] / base, tmp[i] %= base;       while (tmp[tmp.len + 1]) tmp.len++;       return tmp;     }     bignum operator-(const bignum &b) {       int i;       bignum tmp;       for (i = 1; i <= len; ++i) tmp.data[i] = data[i] - b.data[i];       for (i = 1; i <= len; ++i) {         if (tmp[i] < 0) tmp.data[i] += base, tmp.data[i + 1]--;       }       tmp.len = len;       while (tmp[tmp.len] == 0 && tmp.len > 1) tmp.len--;       return tmp;     }     bignum operator-(long long x) {       bignum tmp;       tmp = *this;       tmp[1] -= x;       for (int i = 1; i <= len && tmp[i] < 0; ++i) {         tmp[i + 1] += (tmp[i] + 1) / base - 1;         tmp[i] = (tmp[i] + 1) % base + base - 1;       }       while (!tmp[tmp.len] && tmp.len > 1) tmp.len--;       return tmp;     }     long long operator%(long long x) {       int i;       long long y = 0;       for (i = len; i >= 1; --i) y = (y * base + data[i]) % x;       return y;     }     bignum operator%(const bignum &b) {       if (b.len <= 1 && b[1] == 0) {         printf( error! ?0 mod! );         for (;;)           ;       }       int i, l1 = (len - 1) * Blen, l2 = (b.len - 1) * Blen;       long long x = data[len], y = b[b.len];       while (x) x /= 10, l1++;       while (y) y /= 10, l2++;       bignum chu, B;       chu = *this;       B = b;       for (i = 1; i * Blen <= l1 - l2; ++i) B *= base;       for (i = 1; i <= (l1 - l2) % Blen; ++i) B *= 10;       for (i = l1 - l2; i >= 0; --i) {         while (chu >= B) chu -= B;         B /= 10;       }       return chu;     }     bignum operator+=(const bignum &b) { return *this = (*this + b); }     bignum operator*=(const bignum &b) { return *this = (*this * b); }     bignum operator-=(const bignum &b) { return *this = (*this - b); }     bignum operator/=(const bignum &b) { return *this = (*this / b); }     bignum operator%=(const bignum &b) { return *this = (*this % b); }     bignum operator*=(long long x) { return (*this = (*this * x)); }     bignum operator+=(long long x) { return (*this = (*this + x)); }     bignum operator-=(long long x) { return (*this = (*this - x)); }     bignum operator/=(long long x) { return (*this = (*this / x)); }     void read() {       char c[bignumlen * Blen + 10];       scanf( %s , c + 1);       int l = strlen(c + 1);       (*this).clear();       long long x;       for (int i = 1; i <= (l - 1) / Blen + 1; ++i) {         x = 0;         for (int j = l - Blen * i + 1; j <= l - Blen * i + Blen; ++j)           if (j >= 1) x = x * 10 + c[j] - 48;         data[++len] = x;       }     }     void write() {       printf( %lld , data[len]);       for (int i = len - 1; i >= 1; --i) printf( %0*I64d , Blen, data[i]);     }   } p, q, pp, qq;   bignum gcd(const bignum &A, const bignum &B) {     bignum a = A, b = B, res = 1;     while (!(a[1] & 1) && !(b[1] & 1)) a /= 2, b /= 2, res *= 2;     for (;;) {       if (a.len == 1 && a[1] == 0) return b * res;       if (b.len == 1 && b[1] == 0) return a * res;       while (!(a[1] & 1)) a /= 2;       while (!(b[1] & 1)) b /= 2;       if (a > b)         a -= b;       else         b -= a;     }   }   void YF(bignum &A, bignum &B) {     bignum tmp = gcd(A, B);     A /= tmp;     B /= tmp;   }   int n, i, x, num;   int main() {     scanf( %d , &n);     while (n % 2 == 0) {       n /= 2;       num++;     }     if (n == 1) {       printf( %d/%d n , num, 1);       return 0;     }     p = 0;     q = 1;     pp = 1;     qq = 1;     for (i = n + 1;;) {       if (i > n) {         if (q % (i - n) == 0)           q /= i - n;         else           p *= i - n;         if (qq % (i - n) == 0)           qq /= i - n;         else           pp *= i - n;         if (p % i == 0)           p /= i;         else           q *= i;         if (pp % i == 0)           pp /= i;         else           qq *= i;       } else         p += q;       if (i == 1) break;       if (i % 2 == 0)         i /= 2;       else         i += n;     }     YF(p, q);     p = p * qq;     q = q * (qq - pp);     p += q * num;     bignum tmp = gcd(p, q);     (p / tmp).write();     printf( / );     (q / tmp).write();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10, MSQ = 1010, SQ = 10, inf = 2e9;   pair<pair<int, int>, int> p[maxn];   map<int, vector<int> > mp;   vector<int> vec, vec2;   int n, k, sm[maxn], l[maxn], r[maxn];   long long ans;   void add(int ind, int val) {     while (ind < maxn) {       sm[ind] += val;       ind += (ind & -ind);     }   }   int ask(int ind) {     int ans = 0;     while (ind > 0) {       ans += sm[ind];       ind -= (ind & -ind);     }     return ans;   }   void Add(int id, int val) {     if (val == 1) ans += ask(r[id]) - ask(l[id]);     add(id + 1, val);   }   bool CMP(int a, int b) { return p[a].first.second > p[b].first.second; }   void Add(vector<int>& vec) {     sort(vec.begin(), vec.end(), CMP);     for (int id : vec) Add(id, 1);     for (int id : vec) Add(id, -1);   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d%d%d , &p[i].first.first, &p[i].first.second, &p[i].second);       vec.push_back(p[i].second);     }     sort(p, p + n);     for (int i = 0; i < n; i++) {       pair<pair<int, int>, int> PP1 = {{p[i].first.first - p[i].first.second, -1},                                        -1},                                 PP2 = {                                     {p[i].first.first + p[i].first.second, inf},                                     inf};       l[i] = lower_bound(p, p + n, PP1) - p;       r[i] = upper_bound(p, p + n, PP2) - p;     }     for (int i = 0; i < n; i++) {       mp[p[i].second].push_back(i);     }     sort(vec.begin(), vec.end());     vec.resize(unique(vec.begin(), vec.end()) - vec.begin());     for (int i = 0; i < int((vec).size()); i++) {       long long bf = ans;       Add(mp[vec[i]]);       vec2.push_back(ans - bf);     }     for (int i = 0; i < int((vec).size()); i++) {       for (int j = i + 1; j < int((vec).size()) && vec[j] - vec[i] <= k; j++) {         vector<int> vv(int((mp[vec[i]]).size()) + int((mp[vec[j]]).size())),             v1 = mp[vec[i]], v2 = mp[vec[j]];         merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vv.begin());         Add(vv);         ans -= vec2[i] + vec2[j];       }     }     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long x, y, p, q;       cin >> x >> y >> p >> q;       if (p == q && x == y) {         printf( 0 n );         continue;       }       if (p == 0 && x == 0) {         printf( 0 n );         continue;       }       if (p == q || p == 0 || q == 0) {         printf( -1 n );         continue;       }       long long i = max(           (x - p * ((y + q - 1) / q) + p - 1) / p,           (p * ((y + q - 1) / q) - (x + ((y + q - 1) / q) * q - y) + q - p - 1) /               (q - p));       printf( %lld n , i * q + ((y + q - 1) / q) * q - y);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   int main() {     fast();     long long int t = 1;     while (t--) {       long long int n, m;       cin >> n >> m;       long long int a[n][m], b[n][m];       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cin >> a[i][j];         }       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cin >> b[i][j];         }       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           if (a[i][j] > b[i][j]) {             a[i][j] = b[i][j] + a[i][j];             b[i][j] = a[i][j] - b[i][j];             a[i][j] = a[i][j] - b[i][j];           }         }       }       bool ans = 1;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m - 1; j++) {           if (a[i][j] >= a[i][j + 1]) {             ans = 0;             break;           }           if (b[i][j] >= b[i][j + 1]) {             ans = 0;             break;           }         }       }       for (int i = 0; i < n - 1; i++) {         for (int j = 0; j < m; j++) {           if (a[i][j] >= a[i + 1][j]) {             ans = 0;             break;           }           if (b[i][j] >= b[i + 1][j]) {             ans = 0;             break;           }         }       }       if (!ans)         cout <<  Impossible ;       else         cout <<  Possible ;     }   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const int inf = 0x3f3f3f3f;   const double eps = 1e-10;   const long long INF = (1LL << 60);   int main() {     int a, b;     scanf( %d%d , &a, &b);     if (a == b) {       printf( %d n , a);       return 0;     }     double ans = -1;     if (a >= b) {       int k = (int)floor((a * 1.0 / b - 1) / 2);       ans = (a - b) * 1.0 / (2 * k);       ans = min(ans, (a + b) * 1.0 / (2 * k + 2));     }     printf( %0.12f n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100000 + 10];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       long long k;       cin >> k;       for (int i = 1; i <= k; i++) cin >> a[i];       sort(a + 1, a + k + 1);       int ans = 1;       for (int i = 1; i < k; i++) {         if (a[i] != a[i + 1]) {           ans++;           continue;         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     ios::sync_with_stdio(false);     long long n, m, k;     cin >> n >> m >> k;     long long x, s;     cin >> x >> s;     pair<long long, long long> a[m];     for (int i = 0; i < m; i++) cin >> a[i].second;     for (int i = 0; i < m; i++) cin >> a[i].first;     sort(a, a + m);     pair<long long, long long> c[k];     for (int i = 0; i < k; i++) cin >> c[i].first;     for (int i = 0; i < k; i++) cin >> c[i].second;     long long mn[m];     mn[0] = a[0].second;     for (int i = 1; i < m; i++) {       mn[i] = min(mn[i - 1], a[i].second);     }     long long ans = n * x;     for (int i = 0; i < m; i++) {       if (a[i].first <= s) ans = min(ans, n * a[i].second);     }     for (int i = 0; i < k; i++) {       if (c[i].second > s) break;       int t = c[i].first;       int k =           upper_bound(a, a + m,                       pair<long long, long long>(s - c[i].second, 1LL << 60)) -           a;       k--;       if (k >= 0) {         ans = min(ans, (n - t) * min(x, mn[k]));       }       ans = min(ans, (n - t) * x);     }     ans = max(ans, 0LL);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   const int MAXN = 2e5 + 10;   using namespace std;   int T, N, cmp;   int p[MAXN], c[MAXN];   vector<int> comp[MAXN];   bool vis[MAXN];   void dfs(int x) {     vis[x] = true;     comp[cmp].push_back(c[x]);     if (!vis[p[x]]) dfs(p[x]);   }   int min_ans(int idx_comp) {     int n_comp = (int)(comp[idx_comp].size());     vector<int> div;     for (long long i = 1; i * i <= 1LL * n_comp; i++)       if (n_comp % i == 0) {         div.push_back((int)i);         if (i * i != 1LL * n_comp) div.push_back((int)(n_comp / i));       }     sort(div.begin(), div.end());     for (int d : div)       for (int i = 0; i < d; i++) {         int g = (i + d) % n_comp;         bool found = true;         while (g != i) {           if (comp[idx_comp][g] != comp[idx_comp][i]) found = false;           g = (g + d) % n_comp;         }         if (found) return d;       }   }   inline void init() {     cmp = 0;     for (int i = 1; i < N + 1; i++) {       vis[i] = false;       comp[i].clear();     }   }   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d , &N);       init();       for (int i = 1; i < N + 1; i++) scanf( %d , &p[i]);       for (int i = 1; i < N + 1; i++) scanf( %d , &c[i]);       for (int i = 1; i < N + 1; i++)         if (!vis[i]) {           cmp++;           dfs(i);         }       int mn = N;       for (int i = 1; i < cmp + 1; i++) mn = min(mn, min_ans(i));       printf( %d n , mn);     }   }


#include <bits/stdc++.h>   using namespace std;   bool st[1000000];   int main() {     int N, K;     cin >> N >> K;     for (int i = 0; i < 1000000; i++) st[i] = false;     int cnt = 0;     for (int i = 0; i < N; i++) {       int te1, te2;       cin >> te1 >> te2;       te1 += 100000, te2 += 100000;       for (int j = te1; j <= te2; j++) {         if (!st[j]) {           st[j] = true;           cnt++;         }       }     }     cout << ((cnt + K - 1) / K) * K - cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxint = -1u >> 1;   template <class T>   bool get_max(T& a, const T& b) {     return b > a ? a = b, 1 : 0;   }   template <class T>   bool get_min(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   int main() {     int f[21];     int a, b, c;     cin >> a >> b >> c;     f[0] = a;     f[1] = b;     for (int i = 2; i < 21; i++) f[i] = f[i - 1] + f[i - 2];     cout << f[c] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int u;     int v;     long long cost;     edge() {}     edge(int u, int v, long long c) : u(u), v(v), cost(c) {}   };   long long comb2(long long n) { return (n * (n - 1)) / 2; }   const int MAXN = 100100;   int n;   vector<int> G[MAXN];   edge es[MAXN];   int childNum[MAXN];   long long usedNum[MAXN];   int dfs(int v, int par) {     if (childNum[v] >= 0) return childNum[v];     int ret = 1;     for (int i = 0; i < (int)G[v].size(); i++) {       int next = G[v][i];       if (next != par) ret += dfs(next, v);     }     return childNum[v] = ret;   }   int main(void) {     cin >> n;     for (int i = 0; i < n - 1; i++) {       cin >> es[i].u >> es[i].v >> es[i].cost;       G[es[i].u].push_back(es[i].v);       G[es[i].v].push_back(es[i].u);     }     for (int i = 1; i <= n; i++) childNum[i] = -1;     dfs(1, 0);     double sum = 0;     for (int i = 0; i < n - 1; i++) {       int x = min(childNum[es[i].u], childNum[es[i].v]);       usedNum[i] = 2 * (x * comb2(n - x) + (n - x) * comb2(x));       sum += (double)usedNum[i] * (double)es[i].cost;     }     int q;     long long div = (long long)n * (long long)(n - 1) * (long long)(n - 2) / 6;     cin >> q;     while (q--) {       int id;       long long cost;       cin >> id >> cost;       id--;       long long diff = es[id].cost - cost;       es[id].cost = cost;       sum -= (double)diff * (double)usedNum[id];       printf( %.10lf n , sum / (double)div);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int t = n % 4;     int a = 0;     for (int i = 0; i < 4; i++) {       if ((i * 7) % 4 == t) a = i;     }     int b = -1, c;     for (int j = (n - a) / 4 * 4 + a; j >= a; j -= 4) {       if ((n - 7 * j) % 4 == 0 && (n - 7 * j >= 0)) {         c = j;         b = (n - 7 * j) / 4;         break;       }     }     if (b == -1) {       cout << -1;       return 0;     }     for (int i = 0; i < b; i++) cout << 4;     for (int i = 0; i < c; i++) cout << 7;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class Solution {    public:     void solve(vector<pair<int, int>>& edges, vector<int>& xs, vector<int>& ys,                int n) {       vector<vector<int>> graph(n + 1);       for (auto& p : edges) {         int u = p.first, v = p.second;         graph[u].push_back(v);         graph[v].push_back(u);       }       cout <<  B   << ys[0] << endl;       fflush(stdout);       int root = -1;       cin >> root;       vector<int> dis(n + 1, INT_MAX);       dis[root] = 0;       queue<int> q;       q.push(root);       while (!q.empty()) {         auto u = q.front();         q.pop();         int d = dis[u];         for (auto v : graph[u]) {           if (dis[v] > d + 1) {             dis[v] = d + 1;             q.push(v);           }         }       }       int x = -1;       for (auto v : xs) {         if (x == -1 || dis[x] > dis[v]) {           x = v;         }       }       cout <<  A   << x << endl;       fflush(stdout);       int y = -1;       cin >> y;       bool found = false;       for (auto v : ys) {         if (v == y) {           found = true;           break;         }       }       if (found) {         cout <<  C   << x << endl;       } else {         cout <<  C   << -1 << endl;       }       fflush(stdout);       return;     }   };   int main(int argc, char** argv) {     ios::sync_with_stdio(false);     cin.tie(0);     Solution sol;     int t;     cin >> t;     while (t-- > 0) {       int n;       cin >> n;       vector<pair<int, int>> edges;       edges.reserve(n - 1);       for (int i = 0, u = 0, v = 0; i < n - 1; ++i) {         cin >> u >> v;         edges.emplace_back(u, v);       }       int k;       cin >> k;       vector<int> xs(k);       for (int i = 0; i < k; ++i) {         cin >> xs[i];       }       cin >> k;       vector<int> ys(k);       for (int i = 0; i < k; ++i) {         cin >> ys[i];       }       sol.solve(edges, xs, ys, n);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000 * 1000 * 1000 + 9;   const int MAX_N = 1000 * 1000 + 10;   long long dp1[MAX_N];   int main() {     int n;     scanf( %d , &n);     n /= 2;     if (n == 1) {       printf( %d n , 10);       return 0;     }     dp1[1] = 5;     for (int i = 2; i <= n - 2; i++) dp1[i] = (3 + 2 * dp1[i - 1]) % MOD;     long long dp2 = 1;     for (int i = 0; i < (n - 2); i++)       dp2 = (1 + dp1[n - 2 - i] * (3 + dp2)) % MOD;     dp2 += 5;     dp2 = 1 + dp2 * dp2;     dp2 *= 2;     dp2 %= MOD;     printf( %d n , (int)dp2);     return 0;   }


#include <bits/stdc++.h>   int a[100100];   int f[100100][3];   int s[100100];   char tmp[100100];   int n;   void maximize(int &x, const int &y) {     if (x < y) x = y;   }   void swap(int &a, int &b) {     int sw;     sw = a;     a = b;     b = sw;   }   void init(void) {     scanf( %d , &n);     int i;     for (i = 1; i <= n; i = i + 1) scanf( %d , &a[i]);     scanf( %s , tmp);     for (i = 0; i < n; i = i + 1) s[i + 1] = tmp[i] - 48;     for (i = 1; i <= n; i = i + 1) {       if (i >= n + 1 - i) break;       swap(a[i], a[n + 1 - i]);       swap(s[i], s[n + 1 - i]);     }   }   void optimize(void) {     int i, j, k;     memset(f, -1, sizeof f);     f[0][0] = 0;     for (i = 0; i < n; i = i + 1)       for (j = 0; j < 2; j = j + 1)         if (f[i][j] >= 0)           for (k = 0; k < 2; k = k + 1)             if (j || (k <= s[i + 1]))               maximize(f[i + 1][j || (k < s[i + 1])], f[i][j] + a[i + 1] * k);     if (f[n][0] > f[n][1])       printf( %d , f[n][0]);     else       printf( %d , f[n][1]);   }   int main(void) {     init();     optimize();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long INF = 1e9 + 9;   const long long N = 500500;   char s[4][4];   long long dot, cross, nought, first_wins, second_wins;   long long first_win_rows, first_win_col, second_win_row, second_win_col;   long long total;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     for (long long i = 1; i <= 3; i++) {       for (long long j = 1; j <= 3; j++) {         cin >> s[i][j];         if (s[i][j] ==  . ) dot++;         if (s[i][j] ==  X ) cross++;         if (s[i][j] ==  0 ) nought++;       }     }     total += cross + nought;     if (!(cross == nought || cross == nought + 1)) {       cout <<  illegal ;       return 0;     }     for (long long i = 1; i <= 3; i++) {       set<char> st;       for (long long j = 1; j <= 3; j++) {         st.insert(s[i][j]);       }       if (st.size() == 1 && *st.begin() ==  X ) {         first_wins++;       }       if (st.size() == 1 && *st.begin() ==  0 ) {         second_wins++;       }     }     for (long long j = 1; j <= 3; j++) {       set<char> st;       for (long long i = 1; i <= 3; i++) {         st.insert(s[i][j]);       }       if (st.size() == 1 && *st.begin() ==  X ) {         first_wins++;       }       if (st.size() == 1 && *st.begin() ==  0 ) {         second_wins++;       }     }     set<char> st;     st.insert(s[1][1]);     st.insert(s[2][2]);     st.insert(s[3][3]);     if (st.size() == 1 && *st.begin() ==  X ) {       first_wins++;     }     if (st.size() == 1 && *st.begin() ==  0 ) {       second_wins++;     }     st.clear();     st.insert(s[1][3]);     st.insert(s[2][2]);     st.insert(s[3][1]);     if (st.size() == 1 && *st.begin() ==  X ) {       first_wins++;     }     if (st.size() == 1 && *st.begin() ==  0 ) {       second_wins++;     }     if (first_wins > 0 && second_wins > 0) {       cout <<  illegal ;       return 0;     }     if (first_wins > 0 || second_wins > 0) {       if (first_wins == 1 && total & 1) {         cout <<  the first player won ;       } else if (second_wins == 1 && total % 2 == 0) {         cout <<  the second player won ;       } else if (first_wins == 2 && total & 1) {         if (first_win_col == 2 || first_win_rows == 2) {           cout <<  illegal ;         } else {           cout <<  the first player won ;         }       } else if (second_wins == 2 && total % 2 == 0) {         if (second_win_col == 2 || second_win_row == 2) {           cout <<  illegal ;         } else {           cout <<  the second player won ;         }       } else {         cout <<  illegal ;       }       return 0;     }     if (dot > 0) {       if (cross == nought)         cout <<  first ;       else         cout <<  second ;     } else {       cout <<  draw ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int mx = 0, sum = 0;   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       int a;       cin >> a;       mx = max(mx, a);       sum += a;     }     sum *= 2;     sum += n;     sum /= n;     cout << max(sum, mx) << endl;     return 0;   }


/*    Author : Prakhar Rai    */   #include<bits/stdc++.h>   #define ll long long   #define ld long double   #define LB(x,num) lower_bound(x.begin(),x.end(),num) - x.begin()   #define UB(x,num) upper_bound(x.begin(),x.end(),num) - x.begin()   #define BS(x,num) binary_search(x.begin(),x.end(),num)   #define pb push_back   #define mp make_pair   #define fs first   #define sc second   #define vci vector<int>   #define vcll vector<ll>   #define vcd vector<long double>   #define line(x) sort(x.begin(),x.end())   #define all(x) x.begin(),x.end()   #define newl   n    #define vc vector   #define loop(i,a,b) for(int i = a; i < b; i++)   #define lol(i,a,b) for(ll i = a; i < b; i++)   #define lod(i,a,b) for(ld i = 0; i < b; i++)   #define mod 1000000007   #define read(v,n) lol(i,0,n) {ll x; cin >> x; v.pb(x);}   #define run(a,x) for(auto x : a)   #define yes  YES    #define no  NO    ll cnt;   using namespace std;   // ll dp[10000000];   // memset(dp, -1, sizeof(dp));      void solve() {    ll n; cin >> n;    vcll v;    read(v, n + 2);    sort(all(v));    ll sum = 0;       lol(i, 0, n) {     sum += v[i];    }       if (sum == v[n]) {     lol(i, 0, n) {      cout << v[i] <<    ;     }     cout << newl;     return;    }       sum += v[n];       lol(i, 0, n + 1) {     if (sum - v[i] == v[n + 1]) {      lol(j, 0, n + 1) {       if (i == j) {        continue;       } else {        cout << v[j] <<    ;       }      }      cout << newl;      return;     }    }       cout << -1 << newl;    return;   }      // FILE WITH TEST CASE      void init() {   #ifndef ONLINE_JUDGGE    freopen( input.txt ,  r , stdin);    freopen( output.txt ,  w , stdout);   #endif   }      int main() {   //  init();    ios_base::sync_with_stdio(false); cin.tie(0);    int t; cin >> t;    while (t--)     solve();   }   /* This is my journey and i shall endure the work to reach the top .*/


#include <bits/stdc++.h>   using namespace std;   int play(int n) {     int x = 1;     for (;;) {       if (x * 2 > n)         break;       else         x *= 2;     }     return x / 2;   }   int main() {     int n, b, p, k, x = 1, btl = 0, twl = 0;     scanf( %d %d %d , &n, &b, &p);     twl = n * p;     while (1) {       int pl = play(n);       btl += (2 * b + 1) * pl;       n = pl + n - pl * 2;       if (n == 1) break;     }     cout << btl <<     << twl << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int h, w;     cin >> h >> w;     string line;     char table[h + 2][w + 2];     memset(table,  0 , sizeof table);     for (int i = 1; i <= h; i++) {       cin >> line;       line =     + line;       for (int j = 1; j <= w; j++) {         table[i][j] = line[j];       }     }     int dph[h + 3][w + 3];     int dpv[h + 3][w + 3];     int dp[h + 3][w + 3];     int c = 0;     memset(dp, 0, sizeof dp);     memset(dpv, 0, sizeof dpv);     memset(dph, 0, sizeof dph);     for (int y = 1; y <= h; y++) {       for (int x = 1; x <= w; x++) {         c = 0;         if (table[y][x] ==  . ) {           if (table[y + 1][x] ==  .  && y != h) {             c += 1;             dpv[y][x] = 1;           }           if (table[y][x + 1] ==  .  && x != w) {             dph[y][x] = 1;             c += 1;           }         }         dp[y][x] = dp[y][x - 1] + c;       }     }     int tt;     cin >> tt;     while (tt--) {       int y1, x1, y2, x2, x, y, ans = 0;       cin >> y1 >> x1 >> y2 >> x2;       for (int y = y1; y < y2; y++) {         ans += dp[y][x2 - 1] - dp[y][x1 - 1];       }       for (int x = x1; x < x2; x++) {         ans += dph[y2][x];       }       for (int y = y1; y < y2; y++) {         ans += dpv[y][x2];       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename _T, typename _T2>   string to_string(pair<_T, _T2> x) {     return  <  + to_string(x.first) +  ,   + to_string(x.second) +  > ;   }   template <typename _T>   string to_string(vector<_T> vec) {     string s =  [  ;     for (auto el : vec) s += to_string(el) +  ,  ;     s[s.size() - 1] =  ] ;     s[s.size() - 2] =    ;     return s;   }   template <typename _T>   string to_string(set<_T> vec) {     string s =  {  ;     for (auto el : vec) s += to_string(el) +  ,  ;     s[s.size() - 1] =  } ;     s[s.size() - 2] =    ;     return s;   }   template <typename _T, typename _T2>   string to_string(map<_T, _T2> vec) {     string s =  {  ;     for (auto el : vec)       s +=  mp[  + to_string(el.first) +  ]=   + to_string(el.second) +  ,  ;     s[s.size() - 1] =  } ;     s[s.size() - 2] =    ;     return s;   }   template <typename _T>   inline void _DBG(const char *s, _T x) {     cerr << s <<   =   << to_string(x) <<   n ;   }   template <typename _T, typename... args>   void _DBG(const char *s, _T x, args... a) {     while (*s !=  , ) cerr << *s++;     cerr <<   =   << to_string(x) <<   n ;     _DBG(s + 1, a...);   }   mt19937 rng___(chrono::steady_clock::now().time_since_epoch().count());   const long long MOD = 998244353;   const long long LLINF = 1e18 + 7;   long long d, m;   long long powe(long long n, long long k) {     if (k == 0) return 1;     long long pom = powe(n, k / 2);     pom = (pom * pom) % m;     if (k & 1) pom = (pom * n) % m;     return pom;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     ;     int t;     cin >> t;     while (t--) {       cin >> d >> m;       long long dp[32][32];       for (int i = 0; i < 32; i++)         for (int j = 0; j < 32; j++) dp[i][j] = 0;       dp[0][0] = 1;       long long ans = 0;       for (int i = 1; i < 32; i++) {         for (int j = 1; j < 32; j++) {           int b = j - 1;           if ((1 << b) > d) break;           long long summ = 0;           for (int k = 0; k < j; k++) {             summ += dp[i - 1][k];           }           summ = summ % m;           dp[i][j] = (summ * 1LL * min(d - (1 << b) + 1, 1LL << b)) % m;           ans = (ans + dp[i][j]) % m;         }       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     long long s[100005]{0};     pair<long long, long long> p[100005];     for (int i = 0; i < m; i++) {       int u, v;       long long d;       cin >> u >> v >> d;       u--;       v--;       s[u] -= d;       s[v] += d;     }     for (int i = 0; i < n; i++) p[i] = pair<long long, long long>(s[i], i);     sort(p, p + n);     vector<int> u, v;     vector<long long> d;     int i = 0;     int j = n - 1;     while (i < j) {       if (p[i].first + p[j].first < 0) {         u.push_back(p[i].second);         v.push_back(p[j].second);         d.push_back(p[j].first);         p[i].first += p[j].first;         p[j].first = 0;         j--;       } else if (p[i].first + p[j].first > 0) {         u.push_back(p[i].second);         v.push_back(p[j].second);         d.push_back(-p[i].first);         p[j].first += p[i].first;         p[i].first = 0;         i++;       } else {         if (p[i].first == 0) {           i++;           j--;           continue;         }         u.push_back(p[i].second);         v.push_back(p[j].second);         d.push_back(p[j].first);         p[i].first = 0;         p[j].first = 0;         i++;         j--;       }     }     int l = u.size();     cout << l << endl;     for (int i = 0; i < l; i++)       cout << u[i] + 1 <<     << v[i] + 1 <<     << d[i] << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &res) {     register char ch = getchar();     res = 0;     register int g = 1;     while (!isdigit(ch)) {       if (ch ==  - ) g = -1;       ch = getchar();     }     while (isdigit(ch)) {       res = (res << 3) + (res << 1) + (ch ^ 48);       ch = getchar();     }     res *= g;   }   priority_queue<int> q;   int n, m, a[202000], ans[202000], sum = 0, t = 0;   int main() {     read(n);     read(m);     for (register int i = 1; i <= n; i++) {       read(a[i]);     }     for (register int i = 1; i <= n; i++) {       t = 0;       sum = 0;       while (!q.empty()) q.pop();       for (register int j = 1; j <= i - 1; j++) {         t += a[j];         q.push(a[j]);       }       while (t + a[i] > m) {         t -= q.top();         q.pop();         sum++;       }       printf( %d  , sum);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, i;   long long r, c = 1000000000000000000LL;   void solve(int n, long long x) {     for (int i = 1; i * i <= n; i++)       if (!(n % i)) {         r = max(r, (i + 2LL) * (n / i + 2LL) * x);         c = min(c, (i + 2LL) * (n / i + 2LL) * x);       }   }   int main() {     scanf( %d , &n);     for (int i = 1; i * i <= n; i++)       if ((n % i) == 0) {         solve(i, n / i + 1LL);         solve(n / i, i + 1LL);       }     printf( %I64d %I64d n , c - n, r - n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, string> p;   int ans = 0;   string tmp =  abcdef ;   void doit(int n, string s) {     if (n == 0) {       int f = 0;       while (true) {         if (s.length() == 2) {           if (p.find(s) == p.end()) {             f = 1;             break;           }           if (p[s] !=  a ) {             f = 1;           }           break;         }         string s1 = s.substr(0, 2);         string s2 =   ;         if (s.length() > 2) {           s2 += s.substr(2, s.length() - 2);         }         if (p.find(s1) == p.end()) {           f = 1;           break;         } else {           s = p[s1] + s2;         }       }       if (f == 0) ans++;       return;     }     for (int i = 0; i < 6; i++) {       string tmp1 = s;       tmp1.push_back(tmp[i]);       doit(n - 1, tmp1);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, q;     cin >> n >> q;     for (int i = 0; i < q; i++) {       string s1, s2;       cin >> s1 >> s2;       p.insert(make_pair(s1, s2));     }     for (int i = 0; i < 6; i++) {       string s;       s.push_back(tmp[i]);       doit(n - 1, s);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long const Mod = 1000000007;   unsigned long long sq_sum(unsigned long long n) {     unsigned long long ret = n;     ret = (n * (n + 1));     ret /= 2;     if (ret % 3 == 0) {       return ((ret / 3) % Mod * (2 * n + 1)) % Mod;     } else {       return ((ret % Mod) * ((2 * n + 1) / 3)) % Mod;     }   }   unsigned long long check(unsigned long long a, unsigned long long b) {     unsigned long long ret = 0;     for (unsigned long long x = b; x < 100000; ++x) {       if (x % b != 0) {         unsigned long long t1 = x / b;         unsigned long long t2 = x % b;         if (t1 % t2 == 0 && t1 / t2 <= a) ret += x;       }     }     return ret;   }   int main() {     unsigned long long a, b;     while (cin >> a >> b) {       cout << (((a * (a + 1) / 2) % Mod * b + a) % Mod *                ((b * (b - 1) / 2) % Mod)) %                   Mod            << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long segs[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};   int n, m, k;   long long a[200005];   vector<pair<int, int> > q;   int res[200005];   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 0; i < m; i++) {       int r, t;       cin >> r >> t;       if (q.empty()) {         q.push_back(make_pair(r, t - 1));       } else {         while (!q.empty() && t - 1 >= q.back().second) {           q.pop_back();         }         q.push_back(make_pair(r, t - 1));       }     }     q.push_back(make_pair(1, 0));     int l = 0, r = n - 1, ind = n - 1;     for (int i = 0; i < q.size(); i++) {       if (i == q.size() - 1) {         res[ind] = a[l];         break;       }       if (!i) {         while (r > q[0].second) {           res[r] = a[r];           r--;           ind--;         }         sort(a, a + r + 1);       }       if (q[i].first == 1) {         while (ind > q[i + 1].second) {           res[ind] = a[r];           ind--;           r--;         }       } else {         while (ind > q[i + 1].second) {           res[ind] = a[l];           ind--;           l++;         }       }     }     for (int i = 0; i < n; i++) {       if (i) cout <<    ;       cout << res[i];     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, c;   long long exgcd(long long a, long long b, long long& x, long long& y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     long long d = exgcd(b, a % b, x, y);     long long z = x;     x = y;     y = z - y * (a / b);     return d;   }   int main(int argc, char const* argv[]) {     cin >> a >> b >> c;     long long x, y;     long long d = exgcd(a, b, x, y);     if (c % d) {       cout << -1 << endl;     } else {       x = -c / d * x;       y = -c / d * y;       cout << x <<     << y << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t, cn, n, nr, p10;   int main() {     cin >> t;     while (t--) {       cin >> n;       cn = n;       nr = 0;       p10 = 1;       while (cn) {         if (cn % 10) nr++;         cn /= 10;       }       cout << nr <<   n ;       while (n) {         if (n % 10) cout << n % 10 * p10 <<    ;         n /= 10;         p10 *= 10;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, amazing = 0, maxx, minn, k;     cin >> n;     cin >> k;     maxx = k;     minn = k;     for (int i = 1; i < n; i++) {       cin >> k;       if (k > maxx) {         maxx = k;         amazing++;       } else if (k < minn) {         minn = k;         amazing++;       }     }     cout << amazing << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = INT_MAX;   const int N = 2 * 1e3 + 10;   const long double eps = 1e-9;   const int mod = 998244353;   char a[N][N];   int d[N][N], v[N][N], n, m;   bool used[N][N];   void nxt(char c, int &x, int &y) {     if (c ==  U )       --x;     else if (c ==  D )       ++x;     else if (c ==  R )       ++y;     else       --y;     return;   }   int cx, cy;   void fillcycle() {     int x = cx, y = cy, cnt = 1;     nxt(a[x][y], x, y);     while (!(x == cx && y == cy)) {       ++cnt;       nxt(a[x][y], x, y);     }     x = cx, y = cy;     d[x][y] = cnt;     v[x][y] = 2;     nxt(a[x][y], x, y);     while (!(x == cx && y == cy)) {       v[x][y] = 2;       d[x][y] = cnt;       nxt(a[x][y], x, y);     }     return;   }   void cycle(int x, int y) {     while (!(x <= 0 || y <= 0 || x > n || y > m) && !used[x][y]) {       v[x][y] = 0;       used[x][y] = true;       nxt(a[x][y], x, y);     }     if (x <= 0 || y <= 0 || x > n || y > m) return;     if (v[x][y] != 0) return;     cx = x;     cy = y;     fillcycle();     return;   }   void gg(int x, int y) {     while (x > 0 && y > 0 && x <= n && y <= m && v[x][y] == 0) {       v[x][y] = 1;       nxt(a[x][y], x, y);     }     return;   }   void dfs(int x, int y) {     int tx = x, ty = y;     nxt(a[x][y], tx, ty);     stack<pair<int, int> > vc;     vc.push({x, y});     int c = 1;     while (!(tx <= 0 || ty <= 0 || x > n || y > m) && d[tx][ty] == -1) {       vc.push({tx, ty});       nxt(a[tx][ty], tx, ty);     }     if (tx <= 0 || ty <= 0 || x > n || y > m)       c = 1;     else       c = d[tx][ty] + 1;     while (!vc.empty()) {       tx = vc.top().first;       ty = vc.top().second;       vc.pop();       d[tx][ty] = c;       ++c;     }     return;   }   void solve() {     cin >> n >> m;     for (int i = 0; i <= n + 1; ++i) {       fill(d[i], d[i] + m + 1, -1);       fill(used[i], used[i] + m + 1, false);       fill(v[i], v[i] + m + 1, -1);       if (i > 0 && i <= n) {         for (int j = 1; j <= m; ++j) cin >> a[i][j];       }     }     for (int i = 0; i <= n + 1; ++i) d[i][0] = d[i][m + 1] = 0;     for (int i = 0; i <= m + 1; ++i) d[0][i] = d[n + 1][i] = 0;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (!used[i][j]) {           cycle(i, j);           gg(i, j);         }       }     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (d[i][j] == -1) dfs(i, j);       }     }     int x = 1, y = 1, ans = -2;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (ans < d[i][j]) ans = d[i][j], x = i, y = j;       }     }     cout << x <<     << y <<     << ans <<   n ;     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[300005];   string s, p;   vector<long long> v;   int main() {     ios_base::sync_with_stdio(false);     long long i, j, k, l, m, x, y, r, n, t;     cin >> n >> m;     for (i = 1; i <= n; i++) {       cin >> x;       if (x < 300005) a[x] = 1;     }     long long msd = 0;     long long cnt = 0;     for (i = 1; i <= 300000; i++) {       if (msd >= m) {         break;       }       if (a[i] == 0) {         if ((msd + i) <= m) {           cnt++;           v.push_back(i);           msd += i;         }       }     }     cout << cnt << endl;     for (i = 0; i < v.size(); i++) {       cout << v[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0;     char ch = getchar();     while (!isdigit(ch)) ch = getchar();     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return x;   }   const int maxn = 100005;   int n;   long long zero = 0, one = 0, z1 = 0, o1 = 0;   char s[maxn], t[maxn];   int main() {     n = read();     scanf( %s%s , s, t);     for (int i = 0; i < n; ++i) {       if (s[i] ==  0 ) {         zero++;         if (t[i] ==  0 ) z1++;       } else {         one++;         if (t[i] ==  0 ) o1++;       }     }     printf( %lld n , z1 * one + o1 * zero - z1 * o1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};   int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};   char ch[] = { N ,  S ,  E ,  W };   template <class T>   inline T biton(T n, T pos) {     return n | ((T)1 << pos);   }   template <class T>   inline T bitoff(T n, T pos) {     return n & ~((T)1 << pos);   }   template <class T>   inline T ison(T n, T pos) {     return (bool)(n & ((T)1 << pos));   }   template <class T>   inline T gcd(T a, T b) {     while (b) {       a = a % b;       swap(a, b);     }     return a;   }   template <class T>   inline T bigmod(T p, T e, T m) {     T ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p) % m;       p = (p * p) % m;     }     return (T)ret;   };   vector<int> primes;   void sieve(int n) {     vector<int> vec(n + 1);     vec[0] = vec[1] = 1;     for (int i = 2; i <= n; i++) {       if (vec[i] == 0) {         for (int j = i * 2; j <= n; j = j + i) {           vec[j] = 1;         }       }     }     for (int i = 0; i <= n; i++) {       if (vec[i] == 0) {         primes.push_back(i);       }     }   }   void solve() {     int n;     cin >> n;     vector<int> items(n);     for (int i = 0; i < n; i++) {       cin >> items[i];     }     map<int, vector<int>> mp;     for (int i = 0; i < n; i++) {       int it = items[i];       for (int j = 0; j < primes.size() && primes[j] * primes[j] <= it; j++) {         int cnt = 0;         while (it % primes[j] == 0) {           cnt++;           it = it / primes[j];         }         if (cnt > 0) {           mp[primes[j]].push_back(cnt);         }       }       if (it > 1) {         mp[it].push_back(1);       }     }     long long res = 1;     for (auto m : mp) {       sort(m.second.begin(), m.second.end());       if (m.second.size() == n - 1) {         res = res * pow(m.first, m.second[0]);       } else if (m.second.size() == n) {         res = res * pow(m.first, m.second[1]);       }     }     cout << res <<   n ;     ;   }   int main() {     ios_base::sync_with_stdio(false);     cout.setf(ios::fixed);     cout.precision(10);     cout.tie(nullptr);     cin.tie(nullptr);     sieve(2e5);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 105;   vector<long long> a, b, v;   string s;   long long n, m, x;   long long killAll() {     vector<long long> hlp;     long long i = 0;     for (long long x : b) {       while (i < v.size() and v[i] <= x) {         hlp.push_back(v[i]);         i++;       }       if (i == v.size())         return 0;       else         i++;     }     while (i < v.size()) {       hlp.push_back(v[i]);       i++;     }     long long ans = 0;     long long j = hlp.size() - 1;     for (__typeof(((long long)a.size() - 1)) i = ((long long)a.size() - 1);          i >= (0); i--) {       long long x = a[i];       if (j < i) return 0;       if (hlp[j] >= x)         ans += (hlp[j] - x);       else         return 0;       j--;     }     while (j >= 0) {       ans += hlp[j];       j--;     }     return ans;   }   long long killExact(long long k) {     if (k > a.size()) return 0;     long long i = k - 1;     long long j = v.size() - 1;     long long ans = 0;     while (i >= 0 and j >= 0) {       if (v[j] >= a[i])         ans += (v[j] - a[i]);       else         return 0;       j--;       i--;     }     return ans;   }   long long killFew() {     long long ans = 0;     for (long long i = 1; i <= m; i++) ans = max(ans, killExact(i));     return ans;   }   long long solve() {     cin >> n >> m;     for (__typeof((n)) i = (1); i <= (n); i++) {       cin >> s >> x;       if (s ==  ATK )         a.push_back(x);       else         b.push_back(x);     }     for (__typeof((m)) i = (1); i <= (m); i++) {       cin >> x;       v.push_back(x);     }     sort((v).begin(), (v).end());     sort((a).begin(), (a).end());     sort((b).begin(), (b).end());     cout << max(killAll(), killFew()) <<   n ;     return 0;   }   signed main() {     long long t;     ios_base::sync_with_stdio(false);     t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int a[n];       for (int i = 0; i < n; ++i) {         cin >> a[i];       }       for (int i = n - 1; i >= 0; --i) {         int temp = i % 2 != 0 ? -a[i] : a[i];         cout << temp <<    ;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool ckmin(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   template <class T>   bool ckmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     stack<char> st;     string s;     cin >> s;     long long cnt = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  ( ) {         cnt++;       } else {         cnt--;       }     }     if (cnt != 0) {       cout <<  -1 ;       return 0;     }     long long ans = 0, len = 0, len1 = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  ( ) {         if (len == 0) {           st.push( ( );         } else {           len1++;         }       } else {         if (st.size() > 0) {           st.pop();         } else {           len++;         }       }       if (len == len1) {         ans += len + len1;         len = 0;         len1 = 0;       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)1e5 + 100;   const int mod = (int)1e9 + 7;   int n, k, a[maxn], c[maxn];   bool was[maxn];   vector<int> g[maxn];   bool check(int pos) {     memset(was, 0, sizeof(was));     int used = 0, unused = pos;     for (int i = pos; i >= 1; i--) {       if (was[a[i]] || a[i] == 0) continue;       if (unused > c[a[i]]) {         was[a[i]] = 1;         unused -= 1 + c[a[i]];       }     }     for (int i = (1); i <= (k); ++i)       if (!was[i]) return 0;     return 1;   }   int main() {     scanf( %d%d , &n, &k);     vector<int> st;     for (int i = (1); i <= (n); ++i) {       scanf( %d , a + i);       if (a[i] > 0) {         g[a[i]].push_back(i);         st.push_back(i);       }     }     for (int i = (1); i <= (k); ++i) scanf( %d , c + i);     int l = 0, r = (int)st.size() - 1, ans = -1;     while (l <= r) {       int m = (l + r) >> 1;       if (check(st[m]))         ans = m, r = m - 1;       else         l = m + 1;     }     if (ans == -1)       puts( -1 );     else       printf( %d , st[ans]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     string s;     int first;     cin >> s >> first;     string w = string(s.length(),  w );     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  0 ) {         if (i - first >= 0) w[i - first] =  0 ;         if (i + first < s.length()) w[i + first] =  0 ;       }     }     for (int i = 0; i < s.length(); i++) {       if (w[i] ==  w ) w[i] =  1 ;     }     string newS =   ;     for (int i = 0; i < s.length(); i++) {       bool one = false;       if (i - first >= 0 && w[i - first] ==  1 ) one = true;       if (i + first < s.length() && w[i + first] ==  1 ) one = true;       if (one) {         newS +=  1 ;       } else         newS +=  0 ;     }     if (s == newS) {       cout << w <<   n ;     } else {       cout <<  -1 n ;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[10][10];   int t, i, j, o, x[2], y[2], ok;   int main() {     scanf( %d , &t);     while (t--) {       for (i = o = 0; i < 8; i++) {         scanf( %s , s + i);         for (j = 0; j < 8; j++)           if (s[i][j] ==  K ) x[o] = i, y[o++] = j;       }       if (abs(x[0] - x[1]) % 4 || abs(y[0] - y[1]) % 4)         puts( NO );       else         puts( YES );     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4 )   #pragma GCC optimize( unroll-loops )   using namespace std;   using i64 = int64_t;   using ui64 = uint64_t;   template <class A, class B>   bool smin(A &x, B &&y) {     if (y < x) {       x = y;       return true;     }     return false;   }   template <class A, class B>   bool smax(A &x, B &&y) {     if (x < y) {       x = y;       return true;     }     return false;   }   int const N = int(1e5 + 10);   int const INF = int(1.2e9);   struct node {     int min_pos = INF;     int min_neg = INF;   };   node a[N];   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int q;     cin >> q;     while (q--) {       int t, l, r;       cin >> t >> l >> r;       --l, --r;       if (t == 1) {         int k;         cin >> k;         if (k > 0) {           for (int i = l; i < r; ++i) {             smin(a[i].min_pos, k);           }         } else {           k = -k;           for (int i = l; i < r; ++i) {             smin(a[i].min_neg, k);           }         }       } else {         i64 ans = 0;         for (int i = l; i < r; ++i) {           int const x = a[i].min_pos;           int const y = a[i].min_neg;           if (x != INF && y != INF) {             ans += x + y;           }         }         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, ans = 1, bell[1005], c[1005][1005];   char s[55][1005];   map<long long, int> w;   long long get(int x) {     long long res = 0;     for (int i = 1; i <= n; i++) res = (res << 1) + s[i][x] - 48;     return res;   }   int main() {     scanf( %d%d , &m, &n);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) scanf(  %c , &s[i][j]);     for (int i = 1; i <= m; i++) w[get(i)]++;     c[0][0] = 1;     bell[0] = 1;     for (int i = 1; i <= m; i++)       for (int j = 0; j <= i; j++) {         c[i][j] = (c[i - 1][j - 1] + (long long)j * c[i - 1][j]) % 1000000007;         bell[i] = (bell[i] + c[i][j]) % 1000000007;       }     for (int i = 1; i <= m; i++) {       long long num = get(i);       ans = (long long)ans * bell[w[num]] % 1000000007;       w[num] = 0;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   int N;   int Weight[300005];   long long SubWeight[300005];   std::vector<int> ADC[300005];   inline void AddEdge(int x, int y) {     ADC[x].push_back(y);     ADC[y].push_back(x);   }   long long Max;   int Count;   void DFS(bool Step, int Node = 1, int Parent = 0) {     SubWeight[Node] = Weight[Node];     for (auto Vecin : ADC[Node])       if (Vecin != Parent) {         DFS(Step, Vecin, Node);         SubWeight[Node] += std::max(SubWeight[Vecin], 0LL);       }     if (Step)       Max = std::max(Max, SubWeight[Node]);     else if (SubWeight[Node] == Max)       ++Count, SubWeight[Node] = 0;   }   void Citire() {     scanf( %d , &N);     for (int i = 1; i <= N; ++i) scanf( %d , &Weight[i]);     for (int i = 1, x, y; i < N; ++i) scanf( %d%d , &x, &y), AddEdge(x, y);   }   void Rezolvare() {     Max = -1000000005;     DFS(1);     DFS(0);     printf( %I64d %d , Count * Max, Count);   }   int main() {     Citire();     Rezolvare();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int id[n];     vector<int> ans(k, -1);     for (int i = 0; i < n; i++) {       cin >> id[i];       if (find(ans.begin(), ans.end(), id[i]) == ans.end()) {         ans.insert(ans.begin(), id[i]);         ans.resize(k);       }     }     int count = 0;     for (int i = 0; i < k; i++) {       if (ans[i] >= 0) {         count++;       }     }     cout << count << endl;     for (int i = 0; i < count; i++) {       cout << ans[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int tt;   string input;   int main() {     cin >> tt;     for (int qq = 1; qq <= tt; qq++) {       cin >> input;       int count[250] = {};       for (char c : input) count[c -  a ]++;       int diff = 0;       for (int i = 0; i < 250; i++) {         if (count[i] > 0) diff++;       }       if (diff == 1)         cout <<  -1  << endl;       else {         sort(input.begin(), input.end());         cout << input << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   const int inf = 0x3f3f3f3f;   const long long inff = 0x3f3f3f3f3f3f3f3f;   int n;   int a[200010];   int dp[200010];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       a[i] += a[i - 1];     }     dp[n] = a[n];     int mx = dp[n];     for (int i = n - 1; i >= 2; i--) {       dp[i] = max(a[i] - mx, mx);       mx = max(a[i] - mx, mx);     }     cout << mx << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T abs(T t) {     return t < 0 ? -t : t;   }   const long long modn = 1000000007;   inline long long mod(long long x) { return x % modn; }   const int MAXN = 212345;   int n, m, k;   int s[MAXN];   int main() {     scanf( %d , &n);     for (int a = 0; a < n; a++) {       scanf( %d , s + a);     }     int mx = 0;     for (int i = 0; i <= n; i++) {       int cnt = 0;       for (int a = 0; a < n; a++) {         if (a < i)           cnt += (s[a] == 0);         else           cnt += (s[a] == 1);       }       mx = max(mx, cnt);     }     printf( %d n , mx);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2600;   int n, m;   char s[60][60];   int addedge[MAXN][MAXN];   int blocks[60][60];   int cnt;   int dis[MAXN];   bool inside(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }   vector<int> B;   vector<int> e[MAXN];   int dx[] = {-1, 0, 1, 0};   int dy[] = {0, -1, 0, 1};   int MAXLEN = 0;   int color[MAXN];   void bfs(int x, int y, char st) {     ++cnt;     if (st ==  W )       color[cnt] = 1;     else       color[cnt] = 0;     if (st ==  B ) B.push_back(cnt);     queue<pair<int, int> > q;     blocks[x][y] = cnt;     q.push(make_pair(x, y));     while (!q.empty()) {       int lx = q.front().first, ly = q.front().second;       q.pop();       for (int i = 0; i < 4; i++) {         int nx = lx + dx[i], ny = ly + dy[i];         if (inside(nx, ny) && s[nx][ny] == st && !blocks[nx][ny]) {           blocks[nx][ny] = cnt;           q.push(make_pair(nx, ny));         }       }     }   }   void spfa(int st) {     for (int i = 1; i <= cnt; i++) dis[i] = 0x3F3F3F3F;     dis[st] = 0;     queue<int> q;     q.push(st);     while (!q.empty()) {       int u = q.front();       q.pop();       for (int i = 0; i < e[u].size(); i++) {         int v = e[u][i];         if (dis[v] > dis[u] + 1) {           dis[v] = dis[u] + 1;           q.push(v);         }       }     }     int MX = 0;     for (int i = 0; i < B.size(); i++) {       if (dis[B[i]] > MX) MX = dis[B[i]];     }     MAXLEN = min(MAXLEN, MX + 1);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %s , s[i]);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (!blocks[i][j]) {           bfs(i, j, s[i][j]);         }     for (int x = 0; x < n; x++)       for (int y = 0; y < m; y++) {         for (int i = 0; i < 4; i++) {           int nx = x + dx[i], ny = y + dy[i];           if (inside(nx, ny) && blocks[nx][ny] != blocks[x][y]) {             {               int u = blocks[x][y], v = blocks[nx][ny];               if (addedge[u][v]) continue;               e[u].push_back(v);               addedge[u][v] = true;             }           }         }       }     if (B.size() == 0)       puts( 0 );     else {       MAXLEN = 0x3F3F3F3F;       for (int i = 1; i <= cnt; i++) spfa(i);       printf( %d n , MAXLEN);     }     return 0;   }


#include <bits/stdc++.h>   int S(int x) { return x & 1 ? -(x + 1) / 2 : x / 2; }   int main() {     int q;     scanf( %d , &q);     while (q--) {       int l, r;       scanf( %d%d , &l, &r);       printf( %d n , S(r) - S(l - 1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int m;     cin >> n >> m;     int a[6];     int b[6];     for (int i = 1; i < 6; i++) {       if (n % 5 >= i) {         a[i] = (n / 5) + 1;       } else {         a[i] = n / 5;       }     }     for (int i = 1; i < 6; i++) {       if (m % 5 >= i) {         b[i] = (m / 5) + 1;       } else {         b[i] = m / 5;       }     }     long long int y = 0;     y += a[5];     y *= b[5];     long long int t = 0;     t += a[4];     t *= b[1];     long long int s = 0;     s += a[1];     s *= b[4];     long long int c = 0;     c += a[2];     c *= b[3];     long long int f = 0;     f += a[3];     f *= b[2];     long long int k = y + t + s + c + f;     cout << k;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (a < 0) a = -a;     if (b < 0) b = -b;     return b == 0 ? a : gcd(b, a % b);   }   template <typename T>   void read(T &res) {     bool flag = false;     char ch;     while (!isdigit(ch = getchar())) (ch ==  - ) && (flag = true);     for (res = ch - 48; isdigit(ch = getchar());          res = (res << 1) + (res << 3) + ch - 48)       ;     flag && (res = -res);   }   long long lcm(long long a, long long b) { return a * b / gcd(a, b); }   long long qp(long long a, long long b, long long mod) {     long long ans = 1;     if (b == 0) {       return ans % mod;     }     while (b) {       if (b % 2 == 1) {         b--;         ans = ans * a % mod;       }       a = a * a % mod;       b = b / 2;     }     return ans % mod;   }   long long qpn(long long a, long long b, long long p) {     long long ans = 1;     a %= p;     while (b) {       if (b & 1) {         ans = (ans * a) % p;         --b;       }       a = (a * a) % p;       b >>= 1;     }     return ans % p;   }   char s[100006];   signed main() {     long long n, k;     read(n);     read(k);     scanf( %s , s + 1);     for (long long i = 1; i <= n - 1; i++) {       if (k == 0) {         break;       }       if (s[i] ==  4  && s[i + 1] ==  7 ) {         if (i % 2 == 1) {           s[i + 1] =  4 ;         } else {           if (s[i - 1] ==  4 ) {             if (k % 2 == 1) {               s[i] =  7 ;             } else {               s[i] =  4 ;             }             break;           } else {             s[i] =  7 ;           }         }         k--;       }     }     printf( %s , s + 1);   }


#include <bits/stdc++.h>   using namespace std;   void _read();   const int nax = 2001;   const int inf = 1e9 + 11;   void NO() {     cout <<  NO n ;     exit(0);   }   int mat[nax][nax];   using tp = tuple<int, int, int>;   struct Solution {     int n;     bool in_a[nax];     vector<vector<pair<int, int>>> adj;     void inp() {       cin >> n;       adj.resize(n + 1);       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           cin >> mat[i][j];           if (i == j && mat[i][j]) NO();           if (i != j && !mat[i][j]) NO();         }       }     }     void rec() {       vector<tuple<int, int, int>> vec;       for (int i = 2; i <= n; i++) {         vec.emplace_back(mat[i][1], 1, i);       }       sort((vec).begin(), (vec).end(), greater<tp>());       while (!vec.empty()) {         auto &[d, u, v] = vec.back();         adj[u].emplace_back(v, d);         adj[v].emplace_back(u, d);         vec.pop_back();         for (int i = 0; i < ((int)(vec).size()); i++) {           int a, b, c;           tie(a, b, c) = vec[i];           if (mat[v][c] < a) {             b = v;             a = mat[v][c];             vec[i] = {a, b, c};           }         }         sort((vec).begin(), (vec).end(), greater<tp>());       }     }     void bfs(int src) {       vector<int> dis(n + 1);       vector<int> q = {src};       vector<int> vis(n + 1);       vis[src] = 1;       dis[src] = 0;       for (int i = 0; i < ((int)(q).size()); i++) {         int u = q[i];         for (auto &[v, w] : adj[u]) {           if (vis[v]) continue;           vis[v] = 1;           dis[v] = dis[u] + w;           q.push_back(v);         }       }       for (int i = 1; i <= n; i++) {         if (dis[i] != mat[src][i]) NO();         if (dis[i] != mat[i][src]) NO();       }     }     void check() {       for (int i = 1; i <= n; i++) {         bfs(i);       }     }     void test() {       inp();       in_a[1] = true;       rec();       check();       cout <<  YES n ;     }   };   int main() {     _read();     Solution().test();     return 0;   };   void _read() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);   }


#include <bits/stdc++.h>   using namespace std;   string tsize[] = { S ,  M ,  L ,  XL ,  XXL };   int a[6];   int main() {     int n = 5;     for (int i = 0; i < 5; i++) {       cin >> a[i];     }     int m;     cin >> m;     for (int k = 0; k < m; k++) {       string tshirt;       cin >> tshirt;       int tcode = 0;       for (int i = 0; i < n; i++)         if (tsize[i] == tshirt) tcode = i;       for (int i = 0; i < n; i++) {         if (tcode + i < n && a[tcode + i] > 0) {           cout << tsize[tcode + i] << endl;           a[tcode + i]--;           break;         }         if (tcode - i >= 0 && a[tcode - i] > 0) {           cout << tsize[tcode - i] << endl;           a[tcode - i]--;           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300500;   int n, a[N];   vector<int> v;   int cnt[N];   vector<int> all;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       v.push_back(a[i]);     }     sort(v.begin(), v.end());     v.resize(unique(v.begin(), v.end()) - v.begin());     if (n == (int)v.size()) {       cout << -1;       return 0;     }     for (int i = 1; i <= n; ++i) {       a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;     }     vector<pair<int, int> > ans;     for (int i = 1; i <= n; ++i) {       int id = -1;       for (int j = i; j <= n; ++j) {         if (cnt[a[j]]) {           id = j;           break;         }         ++cnt[a[j]];       }       if (id == -1) {         ans[(int)ans.size() - 1].second = n;         break;       } else {         ans.push_back(make_pair(i, id));         for (int j = i; j <= id; ++j) cnt[a[j]] = 0;         i = id;       }     }     printf( %d n , (int)ans.size());     for (int i = 0; i < (int)ans.size(); ++i)       printf( %d %d n , ans[i].first, ans[i].second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mod;   unsigned long long work(vector<int> &x) {     sort(x.begin(), x.end());     int n = x.size();     int m = x.end() - unique(x.begin(), x.end());     unsigned long long ret = 1;     for (int i = 2; i <= n; i++) {       unsigned long long tmp = i;       while (m > 0 && tmp % 2 == 0) tmp /= 2, m--;       ret = ret * tmp % mod;     }     return ret;   }   int main() {     vector<pair<int, int> > vec;     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int a;       scanf( %d , &a);       vec.push_back(pair<int, int>(a, i));     }     for (int i = 0; i < n; i++) {       int b;       scanf( %d , &b);       vec.push_back(pair<int, int>(b, i));     }     scanf( %d , &mod);     sort(vec.begin(), vec.end());     unsigned long long ans = 1;     for (int i = 0; i < n + n;) {       int nexti = i;       vector<int> tmp;       while (nexti < n + n && vec[i].first == vec[nexti].first) {         tmp.push_back(vec[nexti].second);         nexti++;       }       ans = ans * work(tmp) % mod;       i = nexti;     }     printf( %d n , (int)ans);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = 5009;   struct edge {     int to, nxt;   } e[N * 2];   int hd[N], tot;   void add(int u, int v) {     e[++tot] = (edge){v, hd[u]};     hd[u] = tot;   }   int n, leaf, lcnt[N], l[N];   void dfs1(int u, int fa) {     for (int i = hd[u], v; i; i = e[i].nxt) {       l[u]++;       if ((v = e[i].to) == fa) continue;       dfs1(v, u);     }     leaf += (l[u] == 1);   }   int f[N][N][2], g[N][2];   void dfs2(int u, int fa) {     if (l[u] == 1) {       f[u][0][0] = f[u][1][1] = 0;       lcnt[u] = 1;       return;     }     bool vst = 0;     for (int i = hd[u], v; i; i = e[i].nxt) {       if ((v = e[i].to) == fa) continue;       dfs2(v, u);       if (!vst) {         for (int k = 0; k <= lcnt[v]; k++)           f[u][k][0] = min(f[v][k][0], f[v][k][1] + 1),           f[u][k][1] = min(f[v][k][0] + 1, f[v][k][1]);       } else {         for (int j = 0; j <= min(lcnt[u], leaf / 2); j++)           for (int k = 0; k < 2; k++)             g[j][k] = f[u][j][k], f[u][j][k] = 0x3f3f3f3f;         for (int j = 0; j <= min(lcnt[u], leaf / 2); j++) {           for (int k = 0; k <= min(lcnt[v], leaf / 2 - j); k++) {             f[u][j + k][0] =                 min(f[u][j + k][0], g[j][0] + min(f[v][k][0], f[v][k][1] + 1));             f[u][j + k][1] =                 min(f[u][j + k][1], g[j][1] + min(f[v][k][1], f[v][k][0] + 1));           }         }       }       vst = 1;       lcnt[u] += lcnt[v];     }   }   int main() {     scanf( %d , &n);     if (n == 2) return puts( 2 ), 0;     for (int i = 1, u, v; i < n; i++) scanf( %d%d , &u, &v), add(u, v), add(v, u);     dfs1(1, 0);     memset(f, 0x3f, sizeof(f));     int root = 1;     while (l[root] == 1) root++;     dfs2(root, 0);     printf( %d , min(f[root][leaf / 2][0], f[root][leaf / 2][1]));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3e5 + 5;   const int MAXM = 2e6 + 5;   char s[MAXN];   vector<int> v[MAXN];   multiset<int> ss[MAXN];   int trie[MAXN][26], fail[MAXN];   int pos[MAXN], dat[MAXN], siz[MAXN];   int fa[MAXN], dep[MAXN], top[MAXN], dfn[MAXN];   int rev[MAXN], son[MAXN], dfn_cnt, root, tree_cnt, tot;   int ls[MAXM], rs[MAXM], val[MAXM];   inline int read() {     int x = 0;     char ch = getchar();     while (!isdigit(ch)) ch = getchar();     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x;   }   inline void Max(int &x, int y) {     if (x < y) x = y;   }   inline void insert(char *s, int id) {     int len = strlen(s + 1), cur = 0;     for (int i = 1; i <= len; i++) {       int ch = s[i] -  a ;       if (!trie[cur][ch]) trie[cur][ch] = ++tot;       cur = trie[cur][ch];     }     pos[id] = cur, ss[cur].insert(dat[id]);   }   inline void build(int x = 0) {     queue<int> q;     for (int i = 0; i < 26; i++)       if (trie[0][i]) q.push(trie[0][i]), v[0].push_back(trie[0][i]);     while (!q.empty()) {       x = q.front(), q.pop();       for (int i = 0; i < 26; i++) {         int y = trie[x][i];         if (y) {           fail[y] = trie[fail[x]][i];           v[fail[y]].push_back(y), q.push(y);         } else           trie[x][i] = trie[fail[x]][i];       }     }   }   void dfs_son(int x, int fath) {     siz[x] = 1;     fa[x] = fath;     dep[x] = dep[fath] + 1;     for (auto y : v[x]) {       dfs_son(y, x), siz[x] += siz[y];       if (son[x] || siz[y] > siz[son[x]]) son[x] = y;     }   }   void dfs_chain(int x, int tp) {     top[x] = tp;     rev[dfn[x] = ++dfn_cnt] = x;     if (son[x]) dfs_chain(son[x], tp);     for (auto y : v[x]) {       if (y == son[x]) continue;       dfs_chain(y, y);     }   }   inline void pushup(int cur) { val[cur] = max(val[ls[cur]], val[rs[cur]]); }   void build(int l, int r, int &cur) {     cur = ++tree_cnt;     if (l == r) {       val[cur] = *--ss[rev[l]].end();       return;     }     int mid = (l + r) >> 1;     build(l, mid, ls[cur]);     build(mid + 1, r, rs[cur]);     pushup(cur);   }   void modify(int l, int r, int pos, int to, int cur) {     if (l == r) return val[cur] = to, void();     int mid = (l + r) >> 1;     if (pos <= mid)       modify(l, mid, pos, to, ls[cur]);     else       modify(mid + 1, r, pos, to, rs[cur]);     pushup(cur);   }   int query(int l, int r, int ql, int qr, int cur) {     if (ql <= l && qr >= r) return val[cur];     int mid = (l + r) >> 1;     if (ql > mid) return query(mid + 1, r, ql, qr, rs[cur]);     if (qr <= mid) return query(l, mid, ql, qr, ls[cur]);     return max(query(l, mid, ql, qr, ls[cur]),                query(mid + 1, r, ql, qr, rs[cur]));   }   inline int query(int x) {     int ans = -1;     while (top[x]) {       Max(ans, query(1, dfn_cnt, dfn[top[x]], dfn[x], root));       x = fa[top[x]];     }     return max(ans, query(1, dfn_cnt, 1, dfn[x], root));   }   inline int query(char *s) {     int len = strlen(s + 1), cur = 0, ans = -1;     for (int i = 1; i <= len; i++) {       int ch = s[i] -  a ;       cur = trie[cur][ch];       Max(ans, query(cur));     }     return ans;   }   int main() {     int n = read(), m = read();     for (int i = 1; i <= n; i++) {       scanf( %s , s + 1);       insert(s, i);     }     for (int i = 0; i <= tot; i++) ss[i].insert(-1);     build();     dfs_son(0, 0);     dfs_chain(0, 0);     build(1, dfn_cnt, root);     while (m--) {       int opt = read();       if (opt == 1) {         int id = read(), x = read();         ss[pos[id]].erase(ss[pos[id]].find(dat[id]));         ss[pos[id]].insert(dat[id] = x);         modify(1, dfn_cnt, dfn[pos[id]], *--ss[pos[id]].end(), root);       } else {         scanf( %s , s + 1);         printf( %d n , query(s));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char a[60][60], b[60][60];   int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};   struct node {     int x, y, sum;   };   node ans[2600];   bool cmp(node a, node b) { return a.sum < b.sum; }   int bfs(int x, int y) {     queue<node> q;     node t, next;     t.x = x;     t.y = y;     q.push(t);     int cnt = 1;     a[x][y] =  * ;     while (!q.empty()) {       t = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         int tx = t.x + dir[i][0];         int ty = t.y + dir[i][1];         if (tx < 0 || ty < 0 || tx >= n || ty >= m) {           cnt = -1e9;           continue;         }         if (a[tx][ty] ==  * ) continue;         a[tx][ty] =  * ;         next.x = tx;         next.y = ty;         cnt++;         q.push(next);       }     }     return cnt;   }   void bfs2(int x, int y) {     queue<node> q;     node t, next;     t.x = x;     t.y = y;     q.push(t);     b[x][y] =  * ;     while (!q.empty()) {       t = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         int tx = t.x + dir[i][0];         int ty = t.y + dir[i][1];         if (b[tx][ty] ==  * ) continue;         b[tx][ty] =  * ;         next.x = tx;         next.y = ty;         q.push(next);       }     }   }   int main(void) {     int i, j, k;     while (scanf( %d%d%d , &n, &m, &k) == 3) {       for (i = 0; i < n; i++) {         scanf( %s , a[i]);         strcpy(b[i], a[i]);       }       int c = 0;       for (i = 0; i < n; i++)         for (j = 0; j < m; j++)           if (a[i][j] ==  . ) {             int t = bfs(i, j);             if (t > 0) {               ans[c].x = i;               ans[c].y = j;               ans[c].sum = t;               c++;             }           }       sort(ans, ans + c, cmp);       int sum = 0;       for (i = 0; i < c - k; i++) {         sum += ans[i].sum;         bfs2(ans[i].x, ans[i].y);       }       printf( %d n , sum);       for (i = 0; i < n; i++) printf( %s n , b[i]);     }   }


#include <bits/stdc++.h>   using namespace std;   struct cell {     int x, y, cost;     cell(int i, int j, int k) {       x = i;       y = j;       cost = k;     }   };   int main() {     int n, m, s;     scanf( %d%d%d , &n, &m, &s);     s--;     int t1, t2, t3;     vector<vector<pair<int, int> > > g(n);     vector<int> qq(m), w(m), e(m);     for (int i = 0; i < m; ++i) {       scanf( %d%d%d , &t1, &t2, &t3);       t1--;       t2--;       qq[i] = t1;       w[i] = t2;       e[i] = t3;       g[t1].push_back(make_pair(t2, t3));       g[t2].push_back(make_pair(t1, t3));     }     int l;     scanf( %d , &l);     vector<int> d(n, 1000000000);     d[s] = 0;     queue<int> q;     q.push(s);     d[s] = 0;     while (!q.empty()) {       int v = q.front();       q.pop();       for (unsigned int i = 0; i < g[v].size(); i++) {         int to = g[v][i].first, len = g[v][i].second;         if (d[v] + len < d[to]) {           d[to] = d[v] + len;           q.push(to);         }       }     }     int ans = 0;     for (int i = 0; i < n; ++i)       if (d[i] == l) ans++;     for (int i = 0; i < m; ++i) {       int x = qq[i], y = w[i], z = e[i];       int k1 = l - d[x];       if (k1 > 0 && k1 != z) {         int res = min(d[x] + k1, z - k1 + d[y]);         if (res == l) ans++;       }       int k2 = l - d[y];       if (k2 > 0 && k2 != z - k1 && k2 != z) {         int res = min(d[y] + k2, z - k2 + d[x]);         if (res == l) ans++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long cs = 1, t = 15, ar[100], n;     while (t--) {       long long ans = 0;       n = cs;       map<long long, long long> mp;       for (long long i = 0; i <= n; i++) {         for (long long j = 0; j <= n; j++) {           for (long long k = 0; k <= n; k++) {             for (long long l = 0; l <= n; l++) {               if (i + j + k + l == n) {                 long long val = 1 * i + 5 * j + 10 * k + 50 * l;                 if (mp[val] == 0) {                   mp[val]++;                   ans++;                   ar[n] = ans;                 }               }             }           }         }       }       cs++;     }     cin >> n;     if (n <= 14)       cout << ar[n] << endl;     else {       long long an = ar[14] + ((n - 14) * 49);       cout << an << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     vector<string> a(n);     for (long long i = 0; i < n; ++i) {       cin >> a[i];     }     unordered_set<char> diagonal, non_diagonal;     for (long long i = 0; i < n; ++i) {       for (long long j = 0; j < n; ++j) {         if (i == j) {           diagonal.insert(a[i][j]);         } else if (j == n - i - 1) {           diagonal.insert(a[i][j]);         } else {           non_diagonal.insert(a[i][j]);         }       }     }     if (diagonal.size() == 1 && non_diagonal.size() == 1) {       for (char a : diagonal) {         for (char b : non_diagonal) {           if (a != b) {             cout <<  YES ;           } else {             cout <<  NO ;           }         }       }     } else {       cout <<  NO ;     }   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int a[n];       for (int i = 0; i < n; i++) cin >> a[i];       int dif[n];       memset(dif, 0, sizeof(dif));       int ans = 0;       for (int i = 1; i < n; i++) {         int cnt = 1;         int temp = a[i];         while (a[i - 1] > a[i]) {           dif[i]++;           a[i] = temp + cnt;           cnt *= 2;         }         if (a[i] > a[i - 1] && dif[i]) {           dif[i]--;           a[i] = a[i - 1];         }         ans = max(ans, dif[i]);       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     int ans = 0;     for (int i = 0; i < s.size(); i++) {       if (((s[i] -  0 ) % 2) == 0) {         ans = ans + i + 1;       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v[200005];   vector<int> par(200005, -1);   vector<int> ran(200005, 1);   int fi(int c) {     while (1) {       if (par[c] == -1)         return c;       else         c = par[c];     }   }   int main() {     int n, m;     cin >> n >> m;     long long int a[n];     long long int val = 10000000000000000;     int in;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] < val) {         val = a[i];         in = i;       }     }     vector<pair<long long int, pair<int, int> > > v;     for (int i = 0; i < n; i++) {       if (i != in) {         v.push_back({a[i] + val, {i, in}});       }     }     long long int sum = 0;     while (m--) {       int x, y;       cin >> x >> y;       long long int z;       cin >> z;       x--, y--;       v.push_back({z, {x, y}});     }     sort(v.begin(), v.end());     int s = v.size();     for (int i = 0; i < s; i++) {       int t1 = v[i].second.first;       int t2 = v[i].second.second;       int t3 = fi(t1);       int t4 = fi(t2);       if (t3 != t4) {         if (ran[t3] > ran[t4]) {           par[t4] = t3;           ran[t3] += ran[t4];         } else {           par[t3] = t4;           ran[t4] += ran[t3];         }         sum += v[i].first;       }     }     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N, n, m, min, max;     while (cin >> N) {       n = N % 7;       if (n <= 2) {         max = N / 7 * 2 + n;         min = N / 7 * 2;       } else if (n == 6) {         min = N / 7 * 2 + 1;         max = N / 7 * 2 + 2;       } else {         min = N / 7 * 2;         max = N / 7 * 2 + 2;       }       cout << min <<     << max << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, pos, kol;   long long a[1000];   int main() {     cin >> n;     for (long long i = 1; i <= 2 * n; ++i) cin >> a[i];     for (long long i = 1; i < 2 * n; ++i) {       for (long long j = i + 1; j <= 2 * n; ++j) {         if (a[i] == a[j]) {           pos = j;           break;         }       }       while (pos > i + 1) {         swap(a[pos - 1], a[pos]);         --pos;         ++kol;       }     }     cout << kol << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   const long long MAXN = 100005;   long long n, m, o, a[MAXN], b[MAXN], f[2][205][5005];   int main() {     n = read();     m = read();     memset(a, 0, sizeof(a));     memset(b, 0, sizeof(b));     for (long long i = (1); i <= (n); ++i) {       long long x = read();       while (x % 2 == 0) a[i]++, x /= 2;       while (x % 5 == 0) b[i]++, x /= 5;     }     memset(f, 0, sizeof(f));     f[0][0][0] = 1;     o = 0;     for (long long i = (1); i <= (n); ++i) {       o = 1 - o;       memset(f[o], 0, sizeof(f[o]));       for (long long j = (0); j <= (i); ++j)         for (long long k = (0); k <= (j * 25); ++k) {           f[o][j][k] = max(f[o][j][k], f[1 - o][j][k]);           if (k >= b[i] && j > 0 && f[1 - o][j - 1][k - b[i]] > 0)             f[o][j][k] = max(f[o][j][k], f[1 - o][j - 1][k - b[i]] + a[i]);         }     }     long long ans = 0;     for (long long i = (0); i <= (m * 25); ++i)       ans = max(ans, min(f[o][m][i] - 1, i));     cout << ans << endl;     return 0;   }


#include<iostream>   #include<cstring>   #include<cstdio>   #include<climits>   #include<algorithm>   #include<queue>   #include<vector>   #define int long long   #define pii pair<int,int>   #define mp make_pair   #define pb push_back   #define fi first   #define se second   using namespace std;   inline int read(){    int f=1,ans=0; char c=getchar();    while(c< 0 ||c> 9 ){if(c== - )f=-1;c=getchar();}    while(c>= 0 &&c<= 9 ){ans=ans*10+c- 0 ;c=getchar();}    return f*ans;   }   const int MAXN=15;   const int MAXM=101;   struct node{int x,y,w;}tp[MAXM]; bool cmp(node x1,node x2){return x1.w<x2.w;}   int X[MAXN],Y[MAXN],N,M,f[1<<MAXN][MAXM],INF,g[1<<MAXN][MAXM],Dis[1<<MAXN][MAXN],DIS[1<<MAXN][MAXM],Maxn;   signed main(){    //freopen( 1.in , r ,stdin);    N=read(),M=read(); for(int i=0;i<N;i++) X[i]=read(),Y[i]=read(); for(int i=1;i<=M;i++) tp[i].x=read(),tp[i].y=read(),tp[i].w=read();    sort(tp+1,tp+M+1,cmp);    memset(f,127/3,sizeof(f)); memset(g,-127/3,sizeof(g)); memset(Dis,127/3,sizeof(Dis)); memset(DIS,127/3,sizeof(DIS)); INF=f[0][0];    for(int S=1;S<(1<<N);S++) for(int u=0;u<N;u++){int Minn=INF; for(int v=0;v<N;v++) if(S&(1<<v)) Minn=min(Minn,abs(X[u]-X[v])+abs(Y[u]-Y[v])); Dis[S][u]=Minn;}    for(int S=1;S<(1<<N);S++) for(int u=1;u<=M;u++){int Minn=INF; for(int v=0;v<N;v++) if(S&(1<<v)) Minn=min(Minn,abs(X[v]-tp[u].x)+abs(Y[v]-tp[u].y)); DIS[S][u]=Minn;}    for(int i=0;i<N;i++) f[1<<i][0]=0; for(int i=1;i<=M;i++) g[0][i]=1;    for(int S=0;S<(1<<N);S++){     // f update f     for(int i=0;i<=M;i++) if(f[S][i]!=INF) for(int j=0;j<N;j++) if(!(S&(1<<j)))       f[S|(1<<j)][i]=min(f[S|(1<<j)][i],f[S][i]+Dis[S][j]);     // f update g     for(int i=0;i<=M;i++) if(f[S][i]!=INF) for(int j=1;j<=M;j++) if(f[S][i]+DIS[S][j]<=tp[j].w) g[S][j]=max(g[S][j],i+1);     //g update g     for(int i=1;i<=M;i++) if(g[S][i]>=0){      for(int j=i+1;j<=M;j++) if(tp[i].w+min(abs(tp[i].x-tp[j].x)+abs(tp[i].y-tp[j].y),DIS[S][j])<=tp[j].w)        g[S][j]=max(g[S][j],g[S][i]+1);      Maxn=max(Maxn,g[S][i]);     }     //g update f     for(int i=1;i<=M;i++) if(g[S][i]>=0) for(int j=0;j<N;j++) if(!(S&(1<<j)))       f[S|(1<<j)][g[S][i]]=min(f[S|(1<<j)][g[S][i]],tp[i].w+(min(abs(tp[i].x-X[j])+abs(tp[i].y-Y[j]),Dis[S][j])));    }    printf( %lld n ,Maxn); return 0;    }/*   3 4   1 1   2 3   5 2   2 2 12   5 1 4   6 2 11   3 5 10   */


#include <bits/stdc++.h>   using namespace std;   int a[51];   long long c[51][51];   double dp[51][51][51];   bool state[51][51][51];   double cal(int stu, int room, int mval) {     if (state[stu][room][mval]) return dp[stu][room][mval];     state[stu][room][mval] = true;     double &ret = dp[stu][room][mval];     if (!stu) return ret = mval;     if (!room) return ret = 0.0;     for (int i = 0; i <= stu; i++) {       int rval = i / a[room] + (i % a[room] != 0);       ret += 1.0 * c[i][stu] * pow(1.0 / room, i) *              pow(1.0 - 1.0 / room, stu - i) *              cal(stu - i, room - 1, max(mval, rval));     }     return ret;   }   int main() {     int n, m, i;     for (i = 1; i <= 50; i++) c[0][i] = c[i][i] = 1;     for (i = 2; i <= 50; i++)       for (n = 1; n < i; n++) c[n][i] = c[n - 1][i - 1] + c[n][i - 1];     scanf( %d %d , &n, &m);     for (i = 1; i <= m; i++) scanf( %d , a + i);     printf( %.20lf , cal(n, m, 0));     return 0;   }