#include<bits/stdc++.h>   #define ll long long   #define ull unsigned long long   #define pii pair<int,int>   using namespace std;   long long mod=1000000007;   ll gcd(ll a, ll b)    {        if (b == 0)            return a;        return gcd(b, a % b);    }    void solve()   {       ll n;cin>>n;       ll a[n];       ll sum=0;       for(int i=0;i<n;i++) cin>>a[i],sum+=a[i];       sort(a,a+n);       ll req=(sum+n-2)/(n-1);      // cout<<req<<   ;       ll ans=req*(n-1);       if(req<a[n-1]) ans=a[n-1]*(n-1);       cout<<ans-sum<<endl;   }   int main()   {       ios_base::sync_with_stdio(false);       cin.tie(0);cout.tie(0);       int t;cin>>t;       while(t--)       {           solve();       }       return 0;   }


#include <bits/stdc++.h>   int inp() {     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     int sum = 0;     while (c >=  0  && c <=  9 ) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     return sum;   }   struct Node {     int l;     int r;     int id;   } s[200010];   int a[200010];   long long cnt[1000010], ans, res[200010];   bool cmp(Node a, Node b) {     if ((a.l / 450) == (b.l / 450)) {       if ((a.l / 450) & 1) return a.r < b.r;       return a.r > b.r;     }     return a.l < b.l;   }   void add(long long x) {     x = a[x];     ans -= (cnt[x] * cnt[x]) * x;     cnt[x]++;     ans += (cnt[x] * cnt[x]) * x;   }   void del(long long x) {     x = a[x];     ans -= (cnt[x] * cnt[x]) * x;     cnt[x]--;     ans += (cnt[x] * cnt[x]) * x;   }   int main() {     int n = inp();     int m = inp();     for (int i = 1; i <= n; i++) a[i] = inp();     for (int i = 1; i <= m; i++) {       s[i].l = inp();       s[i].r = inp();       s[i].id = i;     }     std::sort(s + 1, s + m + 1, cmp);     int l = 1;     int r = 0;     for (int i = 1; i <= m; i++) {       while (l > s[i].l) add(--l);       while (r < s[i].r) add(++r);       while (l < s[i].l) del(l++);       while (r > s[i].r) del(r--);       res[s[i].id] = ans;     }     for (int i = 1; i <= m; i++) printf( %lld n , res[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 300000;   const double eps = 1e-6;   double sy, a, b;   int n, q;   double L[NMAX], R[NMAX];   double sum[NMAX];   int dcmp(double x, double y) {     if (fabs(x - y) < eps) return 0;     if (x > y) return 1;     return -1;   }   int bin_search(double x) {     int l = 1, r = n, ret = 0;     while (l <= r) {       int mid = (l + r) / 2;       if (dcmp(x, R[mid]) >= 0) {         ret = mid;         l = mid + 1;       } else         r = mid - 1;     }     return ret;   }   int main() {     scanf( %lf%lf%lf , &sy, &a, &b);     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %lf%lf , &L[i], &R[i]);       sum[i] = sum[i - 1] + R[i] - L[i];     }     scanf( %d , &q);     while (q--) {       double x0, y0, x1, x2;       scanf( %lf%lf , &x0, &y0);       if (dcmp(x0, a) == 0)         x1 = a;       else {         double k = (y0 - sy) / (x0 - a);         x1 = a - sy / k;       }       if (dcmp(x0, b) == 0)         x2 = b;       else {         double k = (y0 - sy) / (x0 - b);         x2 = b - sy / k;       }       int r1 = bin_search(x1);       int r2 = bin_search(x2);       double ans = (sum[r2] - sum[r1]);       if (r1 + 1 <= n && dcmp(L[r1 + 1], x1) < 0) ans -= x1 - L[r1 + 1];       if (r2 + 1 <= n && dcmp(L[r2 + 1], x2) < 0) ans += x2 - L[r2 + 1];       printf( %.15lf n , (b - a) * ans / (x2 - x1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long int gcd(long long int a, long long int b) {     return b ? gcd(b, a % b) : a;   }   inline long long int fast_pow(long long int a, long long int b,                                 long long int mod) {     long long int res = 1;     a %= mod;     while (b > 0) {       if (b & 1) res = res * a % mod;       b >>= 1;       a = a * a % mod;     }     return res;   }   inline bool isPrime(long long int a) {     for (long long int i = 2; i * i <= a; i++)       if (a % i == 0) return 0;     return 1;   }   int n, a, b, c, ans, cnt, anss = INT_MAX;   string person1, likes, person2, temp;   pair<string, string> arr[42];   map<string, int> nametoidx;   vector<string> per;   int main() {     ios::sync_with_stdio(0);     cin.tie(NULL);     nametoidx[ Anka ] = 0;     nametoidx[ Chapay ] = 1;     nametoidx[ Cleo ] = 2;     nametoidx[ Troll ] = 3;     nametoidx[ Dracul ] = 4;     nametoidx[ Snowy ] = 5;     nametoidx[ Hexadecimal ] = 6;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> person1 >> likes >> person2;       arr[i] = make_pair(person1, person2);     }     cin >> a >> b >> c;     for (int i = 0; i < 5; i++) {       for (int j = 0; j < 5 - i; j++) {         int t = max(a / (i + 1), max(b / (j + 1), c / (5 - i - j))) -                 min(a / (i + 1), min(b / (j + 1), c / (5 - i - j)));         temp =   ;         if (anss > t) {           per.clear();           anss = t;           for (int k = 0; k < i + 1; k++) temp +=  0 ;           for (int k = 0; k < j + 1; k++) temp +=  1 ;           for (int k = 0; k < 5 - i - j; k++) temp +=  2 ;           per.push_back(temp);         } else if (anss == t) {           for (int k = 0; k < i + 1; k++) temp +=  0 ;           for (int k = 0; k < j + 1; k++) temp +=  1 ;           for (int k = 0; k < 5 - i - j; k++) temp +=  2 ;           per.push_back(temp);         }       }     }     cout << anss <<    ;     for (int j = 0; j < per.size(); j++) {       temp = per[j];       do {         cnt = 0;         for (int i = 0; i < n; i++) {           if (temp[nametoidx[arr[i].first]] == temp[nametoidx[arr[i].second]])             cnt++;         }         ans = max(ans, cnt);       } while (next_permutation(temp.begin(), temp.end()));     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5, MOD = 1e9 + 7;   const double EPS = 1e-9;   double PI = acos(-1);   double square(long long x) {     double ret = double(x) * double(x);     return ret;   }   vector<pair<double, int> > V;   double deg(double x) { return (x * 180) / PI; }   double mod(double x) {     while (x < 0) x += 2 * PI;     while (x >= 2 * PI) x -= 2 * PI;     return x;   }   int main() {     int n;     long long x0, y0, v, T, d, x, y, r;     cin >> x0 >> y0 >> v >> T;     double dist = v * T;     double start = 0, ans = 0;     int cntr = 0;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> x >> y >> r;       x -= x0, y -= y0;       double d = sqrt(square(x) + square(y));       if (d <= r) {         cout <<  1.000000000 n ;         return 0;       }       if (dist >= d - r) {         double angle;         if (square(x) + square(y) - square(r) <= square(dist))           angle = asin(r / d);         else           angle = acos((square(dist) + square(x) + square(y) - square(r)) /                        (2 * dist * d));         angle += EPS;         double slope = atan2(y, x);         double a = mod(slope - angle), b = mod(slope + angle);         V.push_back({a, 1});         V.push_back({b, -1});         if (a > b) cntr++;       }     }     sort(V.begin(), V.end());     for (auto v : V) {       if (v.second == 1) {         if (cntr == 0) start = v.first;         cntr++;       } else {         if (cntr == 1) ans += (v.first - start);         cntr--;       }     }     if (cntr > 0) ans += 2 * PI - start;     ans = ans / (2 * PI);     cout << setprecision(10) << fixed << ans << endl;   }


#include <bits/stdc++.h>   long long CMB[50][50];   void computeCMB() {     for (long long i = 0; i < 50; ++i) {       CMB[i][0] = 1;       for (long long j = 1; j <= i; ++j) {         CMB[i][j] = CMB[i - 1][j - 1] + CMB[i - 1][j];       }     }   }   long long n;   long long pathsFrom(long long i, long long j) {     long long steps = (n - i) + (n - j);     long long v = (n - i);     return CMB[steps][v];   }   long long V[25 + 1][25 + 1];   long long maxPath(long long x, long long y) {     long long sum = 0;     while (x < n) sum += V[x][y], ++x;     while (y < n) sum += V[x][y], ++y;     return sum;   }   long long minCost(long long X, long long Y) {     long long sum = 0;     long long x = 1, y = 1;     while (y < Y) sum += V[x][y], ++y;     while (x < X) sum += V[x][y], ++x;     return sum;   }   int32_t main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(NULL);     std::cout.tie(NULL);     computeCMB();     std::cin >> n;     for (long long j = n - 1, v = 0; j >= 1; --j) {       for (long long i = 2; i <= n; ++i) {         v += pathsFrom(i, j + 1);         V[i][j] = v - maxPath(i, j + 1) - minCost(i, j);       }     }     for (long long i = 1; i <= n; ++i) {       for (long long j = 1; j <= n; ++j) {         std::cout << V[i][j] <<    ;       }       std::cout << std::endl;       std::cout.flush();     }     long long q;     std::cin >> q;     for (long long i = 0, k; i < q; ++i) {       std::cin >> k;       long long x = 1, y = 1;       long long weight = 0;       for (long long ln = 0; ln < 2 * n - 1; ++ln) {         std::cout << x <<     << y << std::endl;         std::cout.flush();         long long v = maxPath(x, y + 1);         if (y == n || weight + v < k) {           ++x;         } else {           ++y;         }         weight += V[x][y];       }     }     return 0;   }


#include <bits/stdc++.h>   using vi = std::vector<long long int>;   using vvi = std::vector<vi>;   using pii = std::pair<long long int, long long int>;   using vpii = std::vector<pii>;   using vvpii = std::vector<vpii>;   using namespace std;   const long long int N = 2e6 + 10;   const long long int inf = 1e18 + 10;   const long long int M = 1e9 + 7;   const long double Pi = 3.14159265;   long long int n, b, k, x, a[N];   long long int mul(long long int a, long long int b) {     return ((a % M) * (b % M)) % M;   }   long long int add(long long int a, long long int b) {     return ((a % M) + (b % M)) % M;   }   vvi p(vvi a, vvi b) {     vvi x(n, vi(n, 0));     for (long long int i = 0; i <= n - 1; i++)       for (long long int j = 0; j <= n - 1; j++)         for (long long int k = 0; k <= n - 1; k++)           x[i][j] = add(x[i][j], mul(a[i][k], b[k][j]));     return x;   }   vvi matrix_power_final(vvi A, long long int x) {     vvi result(n, vi(n, 0));     for (long long int i = 0; i <= n - 1; i++) result[i][i] = 1;     while (x) {       if (x & 1) result = p(result, A);       A = p(A, A);       x = x / 2;     }     return result;   }   void solve() {     cin >> n >> b >> k >> x;     vvi v(x, vi(x, 0));     for (long long int i = 1; i <= n; i++) {       cin >> a[i];     }     for (long long int i = 1; i <= n; i++) {       for (long long int j = 0; j <= x - 1; j++) {         a[i] %= x;         v[j][(j * 10 + a[i]) % x]++;       }     }     n = x;     v = matrix_power_final(v, b);     cout << v[0][k];   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     solve();     long long int xx = 0;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( avx2 )   using namespace std;   const int MOD = 1000 * 1000 * 1000 + 7;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int a, b, x1, y1, x2, y2;     cin >> a >> b >> x1 >> y1 >> x2 >> y2;     int x1_ = x1 + y1;     int y1_ = x1 - y1;     int x2_ = x2 + y2;     int y2_ = x2 - y2;     x1 = x1_ / (2 * a);     if (x1_ > 0) x1++;     x2 = x2_ / (2 * a);     if (x2_ > 0) x2++;     y1 = y1_ / (2 * b);     if (y1_ > 0) y1++;     y2 = y2_ / (2 * b);     if (y2_ > 0) y2++;     cout << max(abs(y2 - y1), abs(x1 - x2)) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> a[200005], b;   set<int> c1;   int n, k, p, f[200005], x[200005], c[200005], c2[200005];   int getdeepth(int i) {     for (vector<int>::iterator j = a[i].begin(); j != a[i].end(); j++)       if ((*j) != f[i]) {         f[*j] = i;         int x = getdeepth(*j);         c[i] += c[*j];         if (c[*j] >= k) return x;       }     return i;   }   void getdeepth1(int i) {     for (vector<int>::iterator j = a[i].begin(); j != a[i].end(); j++)       if ((*j) != f[i]) getdeepth1(*j);     if (c2[i]) b.push_back(i), c1.erase(i);   }   int main() {     cin.sync_with_stdio(false);     cout.sync_with_stdio(false);     cin >> n >> k;     for (int i = 1, j, l; i < n; i++) {       cin >> j >> l;       a[j].push_back(l);       a[l].push_back(j);     }     for (int i = 1, j; i <= 2 * k; i++) {       cin >> j;       c[j] = c2[j] = 1;       c1.insert(j);     }     p = getdeepth(1);     getdeepth1(p);     for (set<int>::iterator i = c1.begin(); i != c1.end(); i++) b.push_back(*i);     cout << 1 << endl << p << endl;     for (int i = 0; i < k; i++)       cout << b[i] <<     << b[i + k] <<     << p << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, val[200100], m;   set<pair<int, int> > s[200100];   struct opt {     int tp, x, y;   } q[200100];   inline void read(int &x) {     x = 0;     register char c = getchar();     while (c >  9  || c <  0 ) c = getchar();     while (c >=  0  && c <=  9 )       x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();   }   inline void print(int x) {     if (x <= 9)       putchar( 0  + x);     else       print(x / 10), putchar( 0  + x % 10);   }   void ins(int x, pair<int, int> k) {     while (x) s[x].insert(k), x -= x & -x;   }   void del(int x, pair<int, int> k) {     while (x) s[x].erase(k), x -= x & -x;   }   pair<int, int> ask(int x, pair<int, int> k) {     pair<int, int> res = make_pair(0x3f3f3f3f, 0x3f3f3f3f);     while (x <= m) res = min(res, *s[x].lower_bound(k)), x += x & -x;     return res;   }   int main() {     read(n);     for (register int i = 1; i <= n; i++) {       char str[10];       scanf( %s , str), read(q[i].x), read(q[i].y);       if (str[0] ==  a ) q[i].tp = 1;       if (str[0] ==  r ) q[i].tp = 2;       if (str[0] ==  f ) q[i].tp = 3, q[i].x++, q[i].y++;       val[++m] = q[i].y;     }     sort(val + 1, val + m + 1), m = unique(val + 1, val + m + 1) - val + 1;     for (int i = 1; i <= m; i++) s[i].insert(make_pair(0x3f3f3f3f, 0x3f3f3f3f));     for (register int i = 1; i <= n; i++) {       if (q[i].tp == 1)         ins(lower_bound(val + 1, val + m, q[i].y) - val,             make_pair(q[i].x, q[i].y));       if (q[i].tp == 2)         del(lower_bound(val + 1, val + m, q[i].y) - val,             make_pair(q[i].x, q[i].y));       if (q[i].tp == 3) {         register pair<int, int> tmp =             ask(lower_bound(val + 1, val + m, q[i].y) - val,                 make_pair(q[i].x, q[i].y));         if (tmp == make_pair(0x3f3f3f3f, 0x3f3f3f3f))           putchar( - ), putchar( 1 ), putchar(  n );         else           print(tmp.first), putchar(   ), print(tmp.second), putchar(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   template <typename T, typename U>   inline void swap(T &a, U &b) {     a = a ^ b;     b = a ^ b;     a = a ^ b;   }   inline void swap(int &a, int &b) {     a = a ^ b;     b = a ^ b;     a = a ^ b;   }   inline void swap(long long &a, long long &b) {     a = a ^ b;     b = a ^ b;     a = a ^ b;   }   template <typename T, typename U>   inline void smax(T &a, U b) {     if (b > a) a = b;   }   template <typename T, typename U>   inline void smin(T &a, U b) {     if (b < a) a = b;   }   inline int gcd(int a, int b) {     if (a < b) swap(a, b);     while (b > 0) {       a %= b;       swap(a, b);     }     return a;   }   long long nchose(int a, int b) {     long long ans = 1;     smax(a, b - a);     int p = 2;     for (int i = (b - a + 1); i <= (b); i++) {       ans *= (long long)i;       while (p <= a && ans % p == 0) ans /= p++;     }     return ans;   }   using namespace std;   static long long MOD = 1000000009;   string s, t;   int main() {     cin >> s >> t;     sort(s.begin(), s.end());     for (char &i : s) {       if (i !=  0 ) {         swap(i, s[0]);         break;       }     }     if (s == t) {       cout <<  OK ;     } else {       cout <<  WRONG_ANSWER ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, i, j;     string s;     cin >> s;     n = s.size();     string cur;     if (s.size() <= 6) {       cout << 0;       return 0;     }     for (i = 5; i < s.size(); i++) {       cur += s[i];     }     if (cur.size() == 2) {       cout << 1 << endl << cur;       return 0;     }     reverse(cur.begin(), cur.end());     string inf =  --- ;     vector<string> dp2(10500), dp3(10500);     vector<string> ans;     dp2[0] = inf;     dp2[1] += cur[0];     dp2[1] += cur[1];     dp3[0] = inf;     dp3[1] = inf;     dp3[2] += cur[0];     dp3[2] += cur[1];     dp3[2] += cur[2];     dp2[2] = inf;     ans.push_back(dp2[1]);     ans.push_back(dp3[2]);     for (i = 3; i < cur.size(); i++) {       bool two = true;       if (dp2[i - 2] == inf && dp3[i - 2] == inf) {         dp2[i] = inf;         two = false;       }       {         if (dp2[i - 2] != inf) {           string ss;           ss += cur[i - 1];           ss += cur[i];           if (ss != dp2[i - 2]) {             dp2[i] = ss;             ans.push_back(ss);           } else             two = false;         }         {           if (dp3[i - 2] != inf) {             two = true;             string ss;             ss += cur[i - 1];             ss += cur[i];             dp2[i] = ss;             ans.push_back(ss);           }         }       }       if (!two) dp2[i] = inf;       bool three = true;       if (dp3[i - 3] == inf && dp2[i - 3] == inf) {         dp3[i] = inf;         three = false;       }       {         if (dp3[i - 3] != inf) {           string ss;           ss += cur[i - 2];           ss += cur[i - 1];           ss += cur[i];           if (ss != dp3[i - 3]) {             dp3[i] = ss;             ans.push_back(ss);           } else             three = false;         }         {           if (dp2[i - 3] != inf) {             string ss;             ss += cur[i - 2];             ss += cur[i - 1];             ss += cur[i];             ans.push_back(ss);             dp3[i] = ss;             three = true;           }         }       }       if (!three) dp3[i] = inf;     }     for (i = 0; i < ans.size(); i++) {       reverse(ans[i].begin(), ans[i].end());     }     sort(ans.begin(), ans.end());     set<string> h;     set<string>::iterator ii;     for (i = 0; i < ans.size(); i++) h.insert(ans[i]);     cout << h.size() << endl;     for (ii = h.begin(); ii != h.end(); ii++) cout << *ii << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, k1 = 0, k2 = 0;     cin >> a;     string h;     cin >> h;     string *s = new string[a];     for (int i = 0; i < a; i++) {       cin >> s[i];       if (s[i].substr(s[i].size() - 3, 3) == h)         k1++;       else         k2++;     }     if (k1 == k2)       cout <<  home ;     else       cout <<  contest ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long p, d, i, j, tmp, res;     cin >> p >> d;     res = p;     for (i = 999999999999999999ll; i; i /= 10) {       j = i + 1;       if (i > p) continue;       if (i == p) break;       if (p % j == i) {         tmp = (p / j) * j + i;         if (p - tmp <= d) {           res = tmp;           break;         }       } else {         tmp = (p / j - 1) * j + i;         if (p - tmp <= d) {           res = tmp;           break;         }       }     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6;   int n;   int l[maxn], c[maxn];   int gcd(int x, int y) {     if (x == 0)       return y;     else       return gcd(y % x, x);   }   map<int, int> mp;   int main() {     while (scanf( %d , &n) != EOF) {       for (int i = 1; i <= n; i++) scanf( %d , &l[i]);       for (int i = 1; i <= n; i++) scanf( %d , &c[i]);       mp.clear();       mp[0] = 0;       for (int i = 1; i <= n; i++) {         for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {           int v = gcd(it->first, l[i]);           int cc = it->second + c[i];           if (mp.count(v) > 0) {             mp[v] = min(mp[v], cc);           } else             mp[v] = cc;         }       }       if (mp.count(1) > 0)         printf( %d n , mp[1]);       else         printf( -1 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, mx[400000], mn[400000];   long long seg[400000 * 4];   vector<long long> compress(vector<long long> x) {     vector<long long> ans = x;     vector<pair<long long, long long>> p;     p.push_back({0, 0});     for (long long i = 1; i < x.size(); i++) {       p.push_back({x[i], i});     }     sort(p.begin(), p.end());     ans[p[1].second] = 1;     for (long long i = 2; i < p.size(); i++) {       if (p[i].first > p[i - 1].first)         ans[p[i].second] = ans[p[i - 1].second] + 1;       else         ans[p[i].second] = ans[p[i - 1].second];     }     return ans;   }   void update(long long pos, long long val, long long l1, long long r1,               long long node) {     if (l1 == r1) {       seg[node] = val;       return;     }     long long mid = (l1 + r1) / 2;     if (pos <= mid) {       update(pos, val, l1, mid, 2 * node);     } else       update(pos, val, mid + 1, r1, 2 * node + 1);     seg[node] = max(seg[2 * node], seg[2 * node + 1]);   }   long long queri(long long l, long long r, long long l1, long long r1,                   long long node) {     if (l1 > r or r1 < l) return 0;     if (l <= l1 and r1 <= r) return seg[node];     long long mid = (l1 + r1) / 2;     return max(queri(l, r, l1, mid, 2 * node),                queri(l, r, mid + 1, r1, 2 * node + 1));   }   void solv() {     cin >> n;     vector<long long> a(n + 1);     set<long long> s;     for (long long i = 1; i < n + 1; i++) {       cin >> a[i];       s.insert(a[i]);       mx[i] = 0;       mn[i] = 400000;     }     a = compress(a);     for (long long i = 1; i < n + 1; i++) {       mx[a[i]] = i;       mn[a[i]] = min(mn[a[i]], i);     }     long long l = 1;     long long len = 0;     for (long long i = 1; i < s.size() + 1; i++) {       update(i, mx[i], 1, n, 1);     }     for (long long i = 1; i < s.size() + 1; i++) {       while (queri(l, i - 1, 1, n, 1) > mn[i] and l < i) l++;       len = max(len, i - l + 1);     }     long long c = s.size();     cout << c - len <<   n ;   }   signed main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     long long q;     cin >> q;     while (q--) solv();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 25;   const int maxm = 3000;   char maze[maxn][maxn];   int g[maxn * maxn][maxn * maxn];   int n, m, N, M;   int src, sink;   long long ans;   struct Node {     int r, c, t;     Node() {}     Node(int r, int c, int t) : r(r), c(c), t(t) {}   };   vector<Node> males, fmales;   struct Edge {     int u, v, c;     Edge() {}     Edge(int u, int v, int c) : u(u), v(v), c(c) {}   };   struct MaxFlow {     int n, m;     int Now[maxm], Dfn[maxm];     vector<Edge> edges;     vector<int> G[maxm];     void init(int n) {       this->n = n;       for (auto &x : G) x.clear();       edges.clear();     }     void add(int u, int v, int c) {       edges.push_back(Edge(u, v, c));       edges.push_back(Edge(v, u, 0));       m = ((int)((edges).size()));       G[u].push_back(m - 2);       G[v].push_back(m - 1);     }     int ISAP(int s, int flow) {       if (s == sink) return flow;       int v, tab = n, now = 0, vary;       for (int i = 0; i < ((int)((G[s]).size())); i++) {         Edge &e = edges[G[s][i]];         if (e.c > 0) {           if (Dfn[s] == Dfn[v = e.v] + 1)             vary = ISAP(v, min(flow - now, e.c)), now += vary, e.c -= vary,             edges[G[s][i] ^ 1].c += vary;           if (Dfn[src] == n) return now;           if (e.c > 0) tab = min(tab, Dfn[v]);           if (now == flow) break;         }       }       if (now == 0) {         if (--Now[Dfn[s]] == 0) Dfn[src] = n;         ++Now[Dfn[s] = tab + 1];       }       return now;     }     int getAns() {       int flow = 0;       memset(Dfn, 0, sizeof Dfn);       memset(Now, 0, sizeof Now);       Now[0] = n;       while (Dfn[src] < n) flow += ISAP(src, 0x0f0f0f0f);       return flow;     }   } solver;   int idx(char x, int y) { return x * m + y; }   bool cango(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }   int dx[] = {0, 0, -1, 1};   int dy[] = {-1, 1, 0, 0};   void calDist() {     memset(g, 0x0f, sizeof g);     for (int i = 0; i < n * m; i++) g[i][i] = 0;     for (int x = 0; x < n; x++)       for (int y = 0; y < m; y++) {         if (maze[x][y] !=  . ) continue;         int u = idx(x, y);         for (int k = 0; k < 4; k++) {           int nx = x + dx[k];           int ny = y + dy[k];           if (!cango(nx, ny) || maze[nx][ny] ==  # ) continue;           int v = idx(nx, ny);           g[u][v] = 1;         }       }     for (int k = 0; k < n * m; k++)       for (int i = 0; i < n * m; i++)         for (int j = 0; j < n * m; j++) {           if (g[i][k] == 0x0f0f0f0f || g[k][j] == 0x0f0f0f0f) continue;           g[i][j] = min(g[i][j], g[i][k] + g[k][j]);         }   }   bool check(long long mid) {     src = 2 * N + 2 * n * m;     sink = src + 1;     solver.init(sink + 1);     for (int j = 0; j < n; j++)       for (int k = 0; k < m; k++) {         if (maze[j][k] ==  # ) continue;         int v = idx(j, k);         int id1 = 2 * N + v * 2;         int id2 = id1 + 1;         solver.add(id1, id2, 1);       }     for (int i = 0; i < N; i++) {       int x = males[i].r, y = males[i].c, t = males[i].t;       int u = idx(x, y);       solver.add(src, i, 1);       for (int j = 0; j < n; j++)         for (int k = 0; k < m; k++) {           if (maze[j][k] ==  # ) continue;           int v = idx(j, k);           int id1 = 2 * N + v * 2;           int id2 = id1 + 1;           if (g[u][v] != 0x0f0f0f0f && 1.0 * g[u][v] * t <= mid)             solver.add(i, id1, 1);         }       x = fmales[i].r, y = fmales[i].c, t = fmales[i].t;       u = idx(x, y);       solver.add(i + N, sink, 1);       for (int j = 0; j < n; j++)         for (int k = 0; k < m; k++) {           if (maze[j][k] ==  # ) continue;           int v = idx(j, k);           int id1 = 2 * N + v * 2;           int id2 = id1 + 1;           if (g[u][v] != 0x0f0f0f0f && 1.0 * g[u][v] * t <= mid)             solver.add(id2, i + N, 1);         }     }     return solver.getAns() == N;   }   int main() {     cin >> n >> m >> N >> M;     for (int i = 0; i < n; i++) scanf( %s , maze[i]);     int ok = 1;     int r, c, t;     scanf( %d%d%d , &r, &c, &t);     r--, c--;     if (N - M == 1) {       fmales.push_back(Node(r, c, t));     } else if (M - N == 1) {       males.push_back(Node(r, c, t));     } else {       ok = 0;     }     for (int i = 0; i < N; i++) {       scanf( %d%d%d , &r, &c, &t);       r--, c--;       males.push_back(Node(r, c, t));     }     for (int i = 0; i < M; i++) {       int r, c, t;       scanf( %d%d%d , &r, &c, &t);       r--, c--;       fmales.push_back(Node(r, c, t));     }     if (!ok) {       puts( -1 );       return 0;     }     N = max(N, M);     calDist();     long long L = 0, R = (long long)1e15;     while (R - L > 0) {       long long mid = (L + R) >> 1;       if (check(mid))         R = mid;       else         L = mid + 1;     }     if (R != (long long)1e15) {       printf( %lld n , R);     } else       puts( -1 );     return 0;   }


#include <bits/stdc++.h>   int n;   long long ans = 0x7fffffffffffffff, sum, p[1000002];   int a[1000002], b[1000002], c[1000002], pcnt;   inline int abs(int x) { return x > 0 ? x : -x; }   signed main() {     scanf( %d , &n);     for (register int i = 1; i <= n; i++) scanf( %d , &a[i]), sum += a[i];     register long long temp = sum;     for (register int i = 2; (long long)i * i <= temp;)       if (sum % i == 0)         sum /= i, p[++pcnt] = i;       else         i++;     if (sum > 1) p[++pcnt] = sum;     for (register long long _k = 1, k = p[_k]; _k <= pcnt; _k++, k = p[_k]) {       if (p[_k] == p[_k - 1]) continue;       long long res = 0;       for (register int i = 1; i <= n; i++) b[i] = a[i] % k;       for (register long long i = 1, last = 1, t = 0; i <= n;) {         t += b[i];         c[i] = b[i], b[i] = 0;         if (t >= k) {           c[i] -= t - k, b[i] = t - k;           t = k;           register int mid;           register long long s = 0;           for (mid = last; mid <= i; mid++) {             s += c[mid];             if (s >= (k + 1) / 2) break;           }           for (register int j = last; j <= i; j++)             res += (long long)c[j] * abs(j - mid);           t = 0, last = i;         } else           i++;       }       if (res < ans) ans = res;     }     printf( %I64d , ans == 0x7fffffffffffffff ? -1 : ans);     return 0;   }


#include <bits/stdc++.h>   int main() {     int a, b, y, ans;     scanf( %d%d , &a, &b);     y = a * b;     ans = y / 2;     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     long long res = 0;     cin >> n;     vector<bool> v(6000, false);     for (int i = 0; i < n; ++i) {       cin >> k;       if (!v[k - 1])         v[k - 1] = true;       else         while (v[k - 1]) {           ++res;           ++k;           v[k - 2] = true;         }       v[k - 1] = true;     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1000000007;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long int n, h;     cin >> n >> h;     long long int arr[n];     for (long long int i = 0; i < n; i++) cin >> arr[i];     long long int ans = 0;     for (long long int i = 0; i < n; i++) {       vector<long long int> op;       for (long long int j = 0; j <= i; j++) op.push_back(arr[j]);       sort(op.begin(), op.end());       if (op.size() == 1) {         if (op[0] <= h) ans = i + 1;         continue;       }       long long int sum = 0;       for (long long int j = op.size() - 1; j >= 0; j -= 2) sum += op[j];       if (sum <= h)         ans = i + 1;       else         break;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, p, q, a, c = 1, b;     cin >> n;     cin >> a >> b;     p = a;     q = b;     int num = n;     n--;     int max = 1;     while (n--) {       cin >> a >> b;       if (p == a && q == b) {         c++;       } else if (c > 1) {         if (max < c) max = c;         c = 1;       }       p = a;       q = b;     }     if (c > max) max = c;     cout << max;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 2e5;   int infty = 1e9;   int p[M], t[M];   int main() {     int n, d, h;     cin >> n >> d >> h;     if (2 * h < d || (n > 2 && d == 1)) {       cout << -1 <<   n ;     } else {       int h1 = h, h2 = d - h, cur = 2;       for (int i = 0; i < h; i++) {         cout << cur - 1 <<     << cur <<   n ;         cur++;       }       if (d == h) {         for (int i = cur; i <= n; i++) {           cout << 2 <<     << i <<   n ;         }       } else {         if (h2 > 0) {           cout <<  1   << cur <<   n ;           cur++;         }         for (int i = 1; i < h2; i++) {           cout << cur - 1 <<     << cur <<   n ;           cur++;         }         for (int i = cur; i <= n; i++) {           cout << 1 <<     << i <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   int a[(100000 + 10)], cur[(100000 + 10)];   double dp[(100000 + 10)][110];   int C[110][110];   void init() {     C[0][0] = 1;     int n = 110;     for (int i = 1; i < n; ++i) {       C[i][0] = C[i][i] = 1;       for (int j = 1; j < i; ++j) C[i][j] = C[i - 1][j] + C[i - 1][j - 1];     }   }   double f(int a, int b) {     double ret = 1;     for (int i = 1; i <= b; ++i) ret *= (a - i + 1.0) / i;     return ret;   }   double cal(int cur, int i, int j, int k) {     if (cur - i - j < 0 || k - j < 0 || k > cur) return 0;     if (k - j > cur - i - j) return 0;     return 1.0 * f(cur - i - j, k - j) * f(i + j, j) / f(cur, k);   }   int read() {     char c;     while ((c = getchar()) && !( 0  <= c && c <=  9 ))       ;     int ret = c -  0 ;     while ((c = getchar()) &&  0  <= c && c <=  9 ) ret = ret * 10 + c -  0 ;     return ret;   }   int main() {     int n;     init();     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       cur[i] = a[i];       dp[i][a[i]] = 1;     }     double ans = 0;     for (int i = 1; i <= n; ++i) ans += dp[i][0];     int q;     scanf( %d , &q);     while (q--) {       int u, v, k;       u = read();       v = read();       k = read();       ans -= dp[u][0];       for (int i = 0; i <= a[u]; ++i) {         dp[u][i] = dp[u][i] * cal(cur[u], i, 0, k);         for (int j = 1; i + j <= a[u] && j <= k; ++j) {           dp[u][i] += dp[u][i + j] * cal(cur[u], i, j, k);         }       }       ans += dp[u][0];       cur[u] -= k;       cur[v] += k;       printf( %.10lf n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e6 + 10, P = 1e9 + 7, U = 1e6 - 1;   long long f1[N], f2[N], f3[N], n, A[N], t[N], val[N], bit[N], ans;   int main() {     cin >> n;     for (long long i = bit[0] = 1; i <= 6; i++) bit[i] = 10 * bit[i - 1];     for (long long i = t[0] = 1; i <= n; i++) t[i] = 2ll * t[i - 1] % P;     for (long long i = 1, x; i <= n; i++)       scanf( %lld , &x), f1[x]++, (f2[x] += x) %= P,           (f3[x] += 1ll * x * x % P) %= P;     for (long long j = 0; j < 6; j++)       for (long long i = U; i >= 0; i--)         if (i / bit[j] % 10 < 9) {           f1[i] += f1[i + bit[j]];           (f2[i] += f2[i + bit[j]]) %= P;           (f3[i] += f3[i + bit[j]]) %= P;         }     for (long long i = 0; i <= U; i++) {       long long res = (f2[i] * f2[i] % P - f3[i] + P) % P * (P / 2 + 1) % P;       if (f1[i]) val[i] = (t[f1[i] - 1] * (f3[i] + res) % P + P) % P;     }     for (long long j = 0; j < 6; j++)       for (long long i = 0; i <= U; i++)         if (i / bit[j] % 10 < 9) (val[i] += P - val[i + bit[j]]) %= P;     for (long long i = 0; i <= U; i++) ans ^= val[i] * i;     cout << ans << endl;   }


#include <bits/stdc++.h>   int main() {     long long t;     scanf( %lld , &t);     long int n, x = 1;     for (int i = 0; i < t; i++) {       scanf( %ld , &n);       int k = 1, p = 1, x;       for (; p <= n;) {         k++;         p = p + pow(2, k - 1);         if (n % p == 0) {           x = n / p;           break;         }       }       printf( %ld n , x);     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC omptimize( unroll-loops )   #pragma optimize( SEX_ON_THE_BEACH )   #pragma GCC optimize( no-stack-protector )   #pragma comment(linker,  /STACK:1000000000 )   using namespace std;   using ll = long long int;   using ull = unsigned long long int;   using dd = long double;   using ldd = long double;   using si = short int;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   ll get_seed(string s) {     ll ans = 0;     for (int i = 0; i < s.size(); ++i) ans += s[i];     return ans;   }   int popcount(int x) { return __builtin_popcount(x); }   ll popcountll(ll x) { return __builtin_popcount(x); }   int way[10][10];   void build(int x, int y) {     for (int i = 0; i < 10; ++i)       for (int j = 0; j < 10; ++j) way[i][j] = 1e5;     for (int i = 0; i < 10; ++i) way[i][(i + x) % 10] = way[i][(i + y) % 10] = 1;     for (int k = 0; k < 10; ++k)       for (int i = 0; i < 10; ++i)         for (int j = 0; j < 10; ++j)           way[i][j] = min(way[i][j], way[i][k] + way[k][j]);     for (int i = 0; i < 10; ++i)       for (int j = 0; j < 10; ++j)         if (way[i][j] >= (int)(1e5)) way[i][j] = -1;   }   int get_ans(int x, int y, string& s) {     build(x, y);     ll ans = 0;     int curr = 0;     for (int i = 1; i < s.size(); ++i) {       if (way[curr][s[i] -  0 ] == -1) return -1;       ans += way[curr][s[i] -  0 ] - 1;       curr = s[i] -  0 ;     }     return ans;   }   void solve(int ii) {     string s;     cin >> s;     for (int x = 0; x < 10; ++x) {       for (int y = 0; y < 10; ++y) cout << get_ans(x, y, s) <<    ;       cout <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     int t;     t = 1;     for (int i = 0; i < t; ++i) solve(i);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       std::vector<pair<long long int, long long int> > ev;       std::vector<pair<long long int, long long int> > od;       for (int i = 1; i <= 2 * n; i++) {         int x;         cin >> x;         if (x % 2 == 0)           ev.push_back({x, i});         else           od.push_back({x, i});       }       if (ev.size() % 2 == 0) {         if (ev.size() == 0) {           od.pop_back();           od.pop_back();         } else {           ev.pop_back();           ev.pop_back();         }       } else {         ev.pop_back();         od.pop_back();       }       for (int i = 0; i < ev.size(); i += 2)         cout << ev[i].second <<     << ev[i + 1].second <<   n ;       for (int i = 0; i < od.size(); i += 2)         cout << od[i].second <<     << od[i + 1].second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void FAST();   long long int big_mod(long long int val, long long int pow, long long int mod);   long long int mod_inverse(long long int val, long long int mod);   int main() {     FAST();     int m, n;     scanf( %d %d , &n, &m);     if (m < 2) {       printf( 1 );     } else {       if (n & 1 == 0) {         if (m >= n / 2) {           printf( %d , n - m);         } else {           printf( %d , m);         }       } else {         if (m > n / 2) {           printf( %d , n - m);         } else {           printf( %d , m);         }       }     }     return 0;   }   void FAST() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   long long int big_mod(long long int val, long long int pow, long long int mod) {     long long int res = 1;     val = val % mod;     while (pow > 0) {       if (pow & 1) {         res = (res * val) % mod;       }       pow = pow >> 1;       val = (val * val) % mod;     }     return res;   }   long long int mod_inverse(long long int val, long long int mod) {     long long int mod1 = mod;     if (mod == 1) {       return 0;     }     long long int y = 0;     long long int x = 1;     long long int quotient, temp;     while (val > 1) {       quotient = val / mod;       temp = mod;       mod = val % mod;       val = temp;       temp = y;       y = x - quotient * y;       x = temp;     }     if (x < 0) {       x += mod1;     }     return x;   }


#include <bits/stdc++.h>   using namespace std;   int n, d;   long long h[100001];   struct A {     long long h;     int i;     bool operator<(const A &B) const { return h < B.h || h == B.h && i < B.i; }   } H[100001];   map<long long, int> MP;   map<long long, int> MPSORT;   int N, Kn;   int st[100001 << 2];   int sti[100001 << 2];   void init() {     N = 1;     while (N < Kn + 2) N <<= 1;     memset(st, 0, sizeof(st));     memset(sti, 0, sizeof(sti));     st[N] = st[2 * N - 1] = -1;   }   int query(int L, int R, int &I) {     int MAX = 0;     I = 0;     for (int s = N + L - 1, t = N + R + 1; s ^ t ^ 1; s >>= 1, t >>= 1) {       if (~s & 1)         if (MAX < st[s ^ 1]) MAX = st[s ^ 1], I = sti[s ^ 1];       if (t & 1)         if (MAX < st[t ^ 1]) MAX = st[t ^ 1], I = sti[t ^ 1];     }     return MAX;   }   void update(int X, int C, int Oldi) {     int s = X + N;     st[s] = C;     sti[s] = Oldi;     s >>= 1;     while (s > 0) {       if (st[s << 1] > st[s << 1 | 1])         st[s] = st[s << 1], sti[s] = sti[s << 1];       else         st[s] = st[s << 1 | 1], sti[s] = sti[s << 1 | 1];       s >>= 1;     }   }   int BS1(long long X) {     if (X < H[1].h) return 0;     int L, R, M;     L = 1, R = Kn + 1;   begin:     if (L + 1 == R) goto end;     M = (L + R) >> 1;     if (H[M].h <= X)       L = M;     else       R = M;     goto begin;   end:     return L;   }   int BS2(long long X) {     if (X > H[Kn].h) return 0;     int L, R, M;     L = 0, R = Kn;   begin:     if (L + 1 == R) goto end;     M = (L + R) >> 1;     if (H[M].h < X)       L = M;     else       R = M;     goto begin;   end:     return R;   }   int dp[100001];   int pre[100001];   int main(void) {     while (~scanf( %d%d , &n, &d)) {       MP.clear();       for (int i = 1; i <= n; ++i)         scanf( %I64d , &h[i]), H[i].h = h[i], H[i].i = i;       sort(H + 1, H + n + 1);       Kn = 0;       for (int i = 1; i <= n; ++i)         if (!MP.count(H[i].h))           MP[H[i].h] = ++Kn, H[Kn].h = H[i].h, H[Kn].i = H[i].i;       init();       for (int i = 1; i <= n; ++i) dp[i] = 1, pre[i] = i;       for (int i = 1; i <= n; ++i) {         int ANS, ANS1, ANS2, D;         int D1 = BS1(h[i] - d), D2 = BS2(h[i] + d);         if (D1)           ANS1 = query(1, D1, D1);         else           ANS1 = 0;         if (D2)           ANS2 = query(D2, Kn, D2);         else           ANS2 = 0;         if (ANS1 > ANS2)           ANS = ANS1, D = D1;         else           ANS = ANS2, D = D2;         if (ANS > 0) {           dp[i] = ANS + 1;           pre[i] = D;         }         update(MP[h[i]], dp[i], i);       }       int ANS = 0, ANSN;       for (int i = 1; i <= n; ++i)         if (dp[i] > ANS) ANS = dp[i], ANSN = i;       printf( %d n , ANS);       vector<int> V;       while (pre[ANSN] != ANSN) {         V.push_back(ANSN);         ANSN = pre[ANSN];       }       printf( %d , ANSN);       for (int i = V.size() - 1; i >= 0; --i) printf(  %d , V[i]);       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5007;   int a[maxn], b[maxn], n, m, g[maxn];   int gcd(int a, int b) {     if (a < b) return gcd(b, a);     if (b == 0) return a;     return gcd(b, a % b);   }   bool found(int v) {     int l = 1, r = m, mid;     while (l <= r) {       mid = (l + r) / 2;       if (b[mid] == v)         return 1;       else if (v < b[mid])         r = mid - 1;       else         l = mid + 1;     }     return 0;   }   int cal(int v) {     int res = 0, s;     for (int i = 2; i * i <= v; ++i)       if (v % i == 0) {         s = 0;         while (v % i == 0) {           v /= i;           ++s;         }         if (found(i)) s = -s;         res += s;       }     if (v > 1) {       s = 1;       if (found(v)) s = -s;       res += s;     }     return res;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1, _c = n; i <= _c; i++) scanf( %d , &a[i]);     for (int i = 1, _c = m; i <= _c; i++) scanf( %d , &b[i]);     g[0] = 0;     int mul = 1;     int res = 0;     for (int i = 1, _c = n; i <= _c; i++)       g[i] = gcd(g[i - 1], a[i]), res += cal(a[i]);     for (int i = n; i >= 1; i--) {       int gx = g[i] / mul;       int vx = cal(gx);       if (vx < 0) {         res -= vx * i;         mul *= gx;       }     }     printf( %d n , res);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y;     char sex;     int m[367], f[367];     memset(m, 0, sizeof(m));     memset(f, 0, sizeof(f));     cin >> n;     for (int i = 0; i < n; i++) {       cin >> sex >> x >> y;       if (sex ==  M ) {         for (int j = x; j <= y; j++) m[j]++;       } else {         for (int j = x; j <= y; j++) f[j]++;       }     }     int res = 0;     for (int i = 1; i <= 366; i++) {       res = max(res, min(f[i], m[i]));     }     cout << 2 * res;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   bool isPrime(int n) {     if (n <= 3) {       return true;     }     if (n % 2 == 0 || n % 3 == 0) return false;     for (int i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, k;     cin >> n >> k;     int i = 1;     int sum;     while (1) {       sum = n * i;       if (sum % 10 == k || sum % 10 == 0) {         break;       }       i++;     }     cout << i;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, r, i;     cin >> n >> m;     r = m % ((n * (n + 1) / 2));     for (i = 0; i < n; i++) {       if (i + 1 > r) {         cout << r;         break;       } else         r -= i + 1;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   const int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};   const int dy[8] = {1, -1, 0, 1, -1, -1, 1, 0};   template <class T>   T gcd(T a, T b) {     return ((b == 0) ? a : gcd(b, a % b));   }   int n, m;   vector<string> s;   vector<string> g;   vector<vector<int> > dist, dist1;   bool valid(int i, int j) { return (min(i, j) >= 0 && i < n && j < m); }   bool check(int t) {     queue<pair<int, int> > q;     for (int i = 0; i < (int)(n); i++)       for (int j = 0; j < (int)(m); j++) {         g[i][j] =  . ;         dist1[i][j] = 0;         if (dist[i][j] > t) {           g[i][j] =  X ;           dist1[i][j] = t + 1;           q.push(make_pair(i, j));         }       }     while (!q.empty()) {       pair<int, int> cur = q.front();       q.pop();       for (int i = 0; i < (int)(8); i++) {         int x = cur.first + dx[i], y = cur.second + dy[i];         if (valid(x, y) && dist1[x][y] < (dist1[cur.first][cur.second] - 1)) {           dist1[x][y] = dist1[cur.first][cur.second] - 1;           q.push(make_pair(x, y));         }       }     }     for (int i = 0; i < (int)(n); i++)       for (int j = 0; j < (int)(m); j++) {         if (s[i][j] ==  . ) {           if (dist1[i][j] > 0) return 0;         } else if (dist1[i][j] <= 0)           return 0;       }     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     s.resize(n);     for (int i = 0; i < (int)(n); i++) cin >> s[i];     string a =   ;     for (int j = 0; j < (int)(m); j++) a +=  . ;     g.resize(n);     for (int i = 0; i < (int)(n); i++) g[i] = a;     dist.resize(n);     dist1.resize(n);     vector<int> b(m, 0);     for (int i = 0; i < (int)(n); i++) dist[i] = dist1[i] = b;     queue<pair<int, int> > q;     for (int i = 0; i < (int)(n); i++)       for (int j = 0; j < (int)(m); j++) {         if (s[i][j] ==  . ) {           dist[i][j] = 0;           q.push(make_pair(i, j));         } else           dist[i][j] = 1e9;       }     while (!q.empty()) {       pair<int, int> cur = q.front();       q.pop();       for (int i = 0; i < (int)(8); i++) {         int x = cur.first + dx[i], y = cur.second + dy[i];         if (valid(x, y) && dist[x][y] == 1e9) {           dist[x][y] = dist[cur.first][cur.second] + 1;           q.push(make_pair(x, y));         }       }     }     for (int i = 0; i < (int)(n); i++)       for (int j = 0; j < (int)(m); j++) {         dist[i][j] = min({dist[i][j], i + 1, j + 1, (n - i), (m - j)});       }     int l = 0, r = min(n, m) + 1;     while (l < r) {       int mid = (l + r + 1) / 2;       if (check(mid))         l = mid;       else         r = mid - 1;     }     cout << l << endl;     assert(check(l));     for (int i = 0; i < (int)(n); i++) cout << g[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int inf = pow(10, 9);   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int n;     cin >> n;     vector<pair<long long int, string>> v(n);     for (long long int i = 0; i < n; ++i) cin >> v[i].first >> v[i].second;     map<string, long long int> ma;     for (long long int i = 0; i < n; ++i) {       sort(v[i].second.begin(), v[i].second.end());       if (ma.find(v[i].second) == ma.end())         ma[v[i].second] = v[i].first;       else         ma[v[i].second] = min(ma[v[i].second], v[i].first);     }     vector<long long int> a;     if (ma.find( A ) != ma.end() and ma.find( B ) != ma.end() and         ma.find( C ) != ma.end())       a.push_back(ma[ A ] + ma[ B ] + ma[ C ]);     if (ma.find( A ) != ma.end() and ma.find( BC ) != ma.end())       a.push_back(ma[ A ] + ma[ BC ]);     if (ma.find( B ) != ma.end() and ma.find( AC ) != ma.end())       a.push_back(ma[ B ] + ma[ AC ]);     if (ma.find( C ) != ma.end() and ma.find( AB ) != ma.end())       a.push_back(ma[ C ] + ma[ AB ]);     if (ma.find( AB ) != ma.end() and ma.find( BC ) != ma.end())       a.push_back(ma[ AB ] + ma[ BC ]);     if (ma.find( AB ) != ma.end() and ma.find( AC ) != ma.end())       a.push_back(ma[ AB ] + ma[ AC ]);     if (ma.find( AC ) != ma.end() and ma.find( BC ) != ma.end())       a.push_back(ma[ AC ] + ma[ BC ]);     if (ma.find( ABC ) != ma.end()) a.push_back(ma[ ABC ]);     if (a.size() == 0)       cout << -1;     else       cout << *min_element(a.begin(), a.end());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s, t;   int dp[1005][1005];   int solve(int i, int j) {     if (i == s.size()) return t.size() - j;     if (j == t.size()) return s.size() - i;     if (dp[i][j] != -1) return dp[i][j];     if (s[i] == t[j]) return solve(i + 1, j + 1);     int c1 = solve(i + 1, j + 1) + 1;     int c2 = solve(i, j + 1) + 1;     int c3 = solve(i + 1, j) + 1;     return dp[i][j] = (min(c1, min(c2, c3)));   }   int shft1 = 0;   void print(int i, int j) {     if (i == s.size()) {       for (int xx = j; xx < t.size(); xx++) {         cout <<  INSERT   << 1 + xx <<     << t[xx] << endl;       }       return;     }     if (j == t.size()) {       for (int xx = 0; xx < s.size() - i; xx++) {         cout <<  DELETE   << j + 1 << endl;       }       return;     }     int c = -2;     if (s[i] == t[j]) {       return print(i + 1, j + 1);     }     int c1 = solve(i + 1, j + 1) + 1;     int c2 = solve(i, j + 1) + 1;     int c3 = solve(i + 1, j) + 1;     int optimal = solve(i, j);     if (optimal == c1) {       cout <<  REPLACE   << j + 1 <<     << t[j] << endl;       print(i + 1, j + 1);     } else if (optimal == c2) {       cout <<  INSERT   << j + 1 <<     << t[j] << endl;       shft1++;       print(i, j + 1);     } else if (optimal == c3) {       cout <<  DELETE   << j + 1 <<     << endl;       shft1--;       print(i + 1, j);     }   }   int main() {     cin >> s >> t;     memset(dp, -1, sizeof dp);     cout << solve(0, 0) << endl;     print(0, 0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int a, b, c, d, e, f;     cin >> a >> b >> c >> d >> e >> f;     int suit1 = min(a, d);     int suit2 = min(b, min(c, d));     long long cost = 0;     for (int i = 0; i <= suit1; i++) {       long long temp = i * e;       temp += min(b, min(c, d - i)) * f;       cost = max(cost, temp);     }     for (int i = 0; i <= suit2; i++) {       long long temp = i * f;       temp += min(a, d - i) * e;       cost = max(cost, temp);     }     cout << cost << endl;   }   int main() {     int q = 1;     while (q--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int BITS = 20;   template <typename T>   struct xor_basis {     T basis[BITS];     int n = 0;     T min_value(T start = 0) const {       for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]);       return start;     }     T max_value(T start = 0) const {       for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]);       return start;     }     bool add(T x) {       if (n == BITS) return false;       x = min_value(x);       if (x == 0) return false;       basis[n++] = x;       for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--)         swap(basis[k], basis[k - 1]);       return true;     }     void merge(const xor_basis<T> &other) {       for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]);     }   };   struct segment {     xor_basis<int> basis;     int get_max() const { return basis.max_value(); }     void join(const segment &other) { basis.merge(other.basis); }     void join(const segment &a, const segment &b) {       *this = a;       join(b);     }   };   struct basic_seg_tree {     int tree_n = 0;     vector<segment> tree;     basic_seg_tree(int n = 0) {       if (n > 0) init(n);     }     void init(int n) {       tree_n = n;       tree.assign(2 * tree_n, segment());     }     void build(const vector<segment> &initial) {       int n = initial.size();       assert(n <= tree_n);       for (int i = 0; i < n; i++) tree[tree_n + i] = initial[i];       for (int position = tree_n - 1; position > 0; position--)         tree[position].join(tree[2 * position], tree[2 * position + 1]);     }     segment query(int a, int b) {       segment answer;       for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2) {         if (a & 1) answer.join(tree[a++]);         if (b & 1) answer.join(tree[--b]);       }       return answer;     }   };   int N, Q;   int main() {     scanf( %d , &N);     vector<segment> initial;     for (int i = 0; i < N; i++) {       int burger;       scanf( %d , &burger);       initial.emplace_back();       initial.back().basis.add(burger);     }     basic_seg_tree tree(N);     tree.build(initial);     scanf( %d , &Q);     for (int q = 0; q < Q; q++) {       int L, R;       scanf( %d %d , &L, &R);       L--;       printf( %d n , tree.query(L, R).get_max());     }   }


#include <bits/stdc++.h>   using namespace std;   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;   }   template <class T>   inline T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <class T>   inline T modinverse(T a, T M) {     return bigmod(a, M - 2, M);   }   template <class T, class X>   inline bool getbit(T a, X i) {     T t = 1;     return ((a & (t << i)) > 0);   }   template <class T, class X>   inline T setbit(T a, X i) {     T t = 1;     return (a | (t << i));   }   template <class T, class X>   inline T resetbit(T a, X i) {     T t = 1;     return (a & (~(t << i)));   }   int n, m;   pair<int, int> arr[100009];   int ob[200009];   pair<pair<int, int>, int> trans[100009];   int cnt[100009];   void solve() {     memset(ob, 0, sizeof(ob));     sort(trans + 1, trans + m + 1);     for (int i = (1); i <= (m); i++) {       pair<int, int> p = trans[i].first;       int ind = trans[i].second;       if (ob[p.second] != 0) {         cnt[ob[p.second]]++;         cnt[ind]++;       }       ob[p.second] = ind;     }   }   int ans[9];   int main() {     scanf( %d %d , &n, &m);     for (int i = (1); i <= (m); i++)       scanf( %d %d , &arr[i].first, &arr[i].second);     for (int i = (1); i <= (m); i++) {       trans[i].first = arr[i];       trans[i].second = i;     }     solve();     for (int i = (1); i <= (m); i++) {       trans[i].first = make_pair(arr[i].second, arr[i].first);       trans[i].second = i;     }     solve();     for (int i = (1); i <= (m); i++) {       trans[i].first = make_pair(arr[i].second + arr[i].first - 1,                                  arr[i].second - arr[i].first + n);       trans[i].second = i;     }     solve();     for (int i = (1); i <= (m); i++) {       trans[i].first = make_pair(arr[i].second - arr[i].first + n,                                  arr[i].second + arr[i].first - 1);       trans[i].second = i;     }     solve();     for (int i = (1); i <= (m); i++) ans[cnt[i]]++;     for (int i = (0); i <= (8); i++) printf( %d  , ans[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int knigths[300006];   set<int> s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL), cout.tie(NULL);     ios::sync_with_stdio(false), cin.tie(0);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n + 1; i++) s.insert(i);     for (int i = 0; i < m; i++) {       int a, b, c;       cin >> a >> b >> c;       set<int>::iterator itl = s.lower_bound(a);       set<int>::iterator itu = s.upper_bound(b);       while (*itl < *itu) {         if (*itl != c) {           knigths[*itl] = c;         }         s.erase(itl);         itl = s.lower_bound(a);         itu = s.upper_bound(b);       }       s.insert(c);     }     for (int i = 1; i <= n; i++) cout << knigths[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 4e5 + 5;   const string s[] = { NW ,  NE ,  SE ,  SW };   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;   }   struct Node {     int x, y, k;     inline bool operator==(const Node B) const {       return this->x == B.x && this->y == B.y && this->k == B.k;     }     inline bool operator!=(const Node B) const { return !(*this == B); }   };   int n, m, k;   long long ans;   set<int> s1[MAXN], s2[MAXN];   map<pair<int, int>, int> mp;   inline void add(int x, int y) {     s1[(1 == 1 ? x - y + m + 1 : x + y)].insert(x);     s2[(2 == 1 ? x - y + m + 1 : x + y)].insert(x);     mp[make_pair(x, y)] = 1;   }   inline void prepare() {     for (int i = 0; i <= n + 1; i++) add(i, 0), add(i, m + 1);     for (int i = 0; i <= m + 1; i++) add(0, i), add(n + 1, i);   }   inline Node change(Node x, int opt = 1) {     Node now = x;     int cur = 0;     if (x.k == 1) {       cur = *(--s1[(1 == 1 ? x.x - x.y + m + 1 : x.x + x.y)].lower_bound(x.x));       now.x -= (abs(cur - x.x) - 1), now.y -= (abs(cur - x.x) - 1);       if ((mp.find(make_pair(now.x - 1, now.y)) != mp.end()) &&           (mp.find(make_pair(now.x, now.y - 1)) != mp.end()))         now.k = 3;       else if ((mp.find(make_pair(now.x - 1, now.y)) != mp.end()))         now.y--, now.k = 4;       else if ((mp.find(make_pair(now.x, now.y - 1)) != mp.end()))         now.x--, now.k = 2;       else         now.k = 3;     }     if (x.k == 2) {       cur = *(--s2[(2 == 1 ? x.x - x.y + m + 1 : x.x + x.y)].lower_bound(x.x));       now.x -= (abs(cur - x.x) - 1), now.y += (abs(cur - x.x) - 1);       if ((mp.find(make_pair(now.x - 1, now.y)) != mp.end()) &&           (mp.find(make_pair(now.x, now.y + 1)) != mp.end()))         now.k = 4;       else if ((mp.find(make_pair(now.x - 1, now.y)) != mp.end()))         now.y++, now.k = 3;       else if ((mp.find(make_pair(now.x, now.y + 1)) != mp.end()))         now.x--, now.k = 1;       else         now.k = 4;     }     if (x.k == 3) {       cur = *(s1[(1 == 1 ? x.x - x.y + m + 1 : x.x + x.y)].lower_bound(x.x));       now.x += (abs(cur - x.x) - 1), now.y += (abs(cur - x.x) - 1);       if ((mp.find(make_pair(now.x + 1, now.y)) != mp.end()) &&           (mp.find(make_pair(now.x, now.y + 1)) != mp.end()))         now.k = 1;       else if ((mp.find(make_pair(now.x + 1, now.y)) != mp.end()))         now.y++, now.k = 2;       else if ((mp.find(make_pair(now.x, now.y + 1)) != mp.end()))         now.x++, now.k = 4;       else         now.k = 1;     }     if (x.k == 4) {       cur = *(s2[(2 == 1 ? x.x - x.y + m + 1 : x.x + x.y)].lower_bound(x.x));       now.x += (abs(cur - x.x) - 1), now.y -= (abs(cur - x.x) - 1);       if ((mp.find(make_pair(now.x + 1, now.y)) != mp.end()) &&           (mp.find(make_pair(now.x, now.y - 1)) != mp.end()))         now.k = 2;       else if ((mp.find(make_pair(now.x + 1, now.y)) != mp.end()))         now.y--, now.k = 1;       else if ((mp.find(make_pair(now.x, now.y - 1)) != mp.end()))         now.x++, now.k = 3;       else         now.k = 2;     }     opt && (ans += abs(cur - x.x));     return now;   }   inline bool check_it(Node x) {     Node tmp = x;     Node y = change(x);     x = y;     if (((tmp.k + 1) % 4 == y.k - 1)) return false;     while (tmp != x) {       y = change(y);       if (((x.k + 1) % 4 == y.k - 1)) return false;       x = y;     }     return 1;   }   int main() {     n = read(), m = read();     k = read(), prepare();     for (int i = 1; i <= k; i++) {       int x = read();       int y = read();       add(x, y);     }     int sx = read(), sy = read(), sd = -1;     string str;     cin >> str;     for (int i = 0; i < 4; i++)       if (str == s[i]) sd = i + 1;     assert(~sd);     Node st = Node{sx, sy, sd};     st = change(st, 0);     if (!check_it(st)) {       ans--;       st.k = (st.k + 1) % 4 + 1;       check_it(st);     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       vector<int> v(n);       for (int i = 0; i < (int)n; ++i) cin >> v[i];       sort((v).begin(), (v).end());       for (int i = (int)n - 1; i >= 0; --i) cout << v[i] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   map<string, vector<int> > m;   map<string, int> memoria;   string codigo(string s1, string s2) { return s1 +  #  + s2; }   int calculadicot(int la, vector<int> &va, int lb, vector<int> &vb) {     int sol = 1000000000;     for (int ia = 0; ia < int(va.size()); ia++) {       int inf = -1;       int sup = int(vb.size());       while (inf + 1 < sup) {         int med = (inf + sup) / 2;         if (vb[med] <= va[ia])           inf = med;         else           sup = med;       }       if (inf >= 0) sol = min(sol, max(va[ia] + la, vb[inf] + lb) - vb[inf]);       if (sup < int(vb.size()))         sol = min(sol, max(va[ia] + la, vb[sup] + lb) - va[ia]);     }     return sol;   }   int calculafusion(int la, vector<int> &va, int lb, vector<int> &vb) {     int ia = 0;     int ib = 0;     int sol = 1000000000;     while (ia < int(va.size()) and ib < int(vb.size())) {       if (va[ia] < vb[ib]) {         sol = min(sol, max(va[ia] + la, vb[ib] + lb) - va[ia]);         ia++;       } else {         sol = min(sol, max(va[ia] + la, vb[ib] + lb) - vb[ib]);         ib++;       }     }     return sol;   }   int calcula(int la, vector<int> &va, int lb, vector<int> &vb) {     if (int(va.size()) > int(vb.size())) return calcula(lb, vb, la, va);     if (int(va.size()) == 0) return -1;     if (int(va.size()) * 10 > int(va.size()) + int(vb.size()))       return calculafusion(la, va, lb, vb);     return calculadicot(la, va, lb, vb);   }   int main() {     cin >> s;     for (int i = 0; i < int(s.size()); i++)       for (int l = 1; l <= 4; l++) m[s.substr(i, l)].push_back(i);     int q;     cin >> q;     for (int cas = 0; cas < q; cas++) {       string a, b;       cin >> a >> b;       if (a > b) swap(a, b);       string c = codigo(a, b);       if (memoria.count(c))         cout << memoria[c] << endl;       else {         vector<int> &va = m[a];         vector<int> &vb = m[b];         memoria[c] = calcula(int(a.size()), va, int(b.size()), vb);         cout << memoria[c] << endl;       }     }   }


#include <queue>   #include <cstdio>   #include <cstring>   using namespace std;   const int Maxn=200000;   const int Inf=0x3f3f3f3f;   int n,k;   int head[Maxn+5],arrive[Maxn<<1|5],nxt[Maxn<<1|5],tot;   void add_edge(int from,int to){    arrive[++tot]=to;    nxt[tot]=head[from];    head[from]=tot;   }   queue<int> q;   int a[Maxn+5];   int dis[Maxn+5];   void init_bfs(){    memset(dis,0x3f,sizeof dis);    for(int i=1;i<=k;i++){     dis[a[i]]=0;     q.push(a[i]);    }    while(!q.empty()){     int u=q.front();     q.pop();     for(int i=head[u];i;i=nxt[i]){      int v=arrive[i];      if(dis[v]!=Inf){       continue;      }      dis[v]=dis[u]+1;      q.push(v);     }    }   }   int ans[Maxn+5];   bool del[Maxn+5];   int sz[Maxn+5];   int st[Maxn+5],top;   int dep[Maxn+5];   void size_dfs(int u,int fa){    sz[u]=1;    for(int i=head[u];i;i=nxt[i]){     int v=arrive[i];     if(v==fa||del[v]){      continue;     }     size_dfs(v,u);     sz[u]+=sz[v];    }   }   int get_weight(int x){    size_dfs(x,0);    int num=sz[x];    top=0;    st[++top]=x;    int maxn;    int root=x,root_mx=sz[x]-1;    while(top>0){     int u=st[top--];     maxn=num-sz[u];     for(int i=head[u];i;i=nxt[i]){      int v=arrive[i];      if(del[v]||sz[v]>sz[u]){       continue;      }      st[++top]=v;      maxn=max(maxn,sz[v]);     }     if(maxn<root_mx){      root=u;      root_mx=maxn;     }    }    return root;   }   void dep_sz_dfs(int u,int fa){    dep[u]=dep[fa]+1;    sz[u]=1;    for(int i=head[u];i;i=nxt[i]){     int v=arrive[i];     if(del[v]||v==fa){      continue;     }     dep_sz_dfs(v,u);     sz[u]+=sz[v];    }   }   int lis[Maxn+5],len;   int tot_sz;   void lis_dfs(int u,int fa){    if(dis[u]-1-dep[u]>=0){     lis[min(dis[u]-1-dep[u],tot_sz)]=max(lis[min(dis[u]-1-dep[u],tot_sz)],dis[u]);    }    for(int i=head[u];i;i=nxt[i]){     int v=arrive[i];     if(del[v]||v==fa){      continue;     }     lis_dfs(v,u);    }   }   void calc_dfs(int u,int fa){    ans[u]=max(ans[u],lis[dep[u]]);    for(int i=head[u];i;i=nxt[i]){     int v=arrive[i];     if(del[v]||v==fa){      continue;     }     calc_dfs(v,u);    }   }   void calc(int u){    u=get_weight(u);    dep_sz_dfs(u,0);    tot_sz=sz[u];    for(int i=0;i<=sz[u];i++){     lis[i]=-Inf;    }    lis_dfs(u,0);    for(int i=sz[u]-1;i>=0;i--){     lis[i]=max(lis[i],lis[i+1]);    }    calc_dfs(u,0);    del[u]=1;    for(int i=head[u];i;i=nxt[i]){     int v=arrive[i];     if(del[v]){      continue;     }     calc(v);    }   }   int main(){    scanf( %d ,&n);    for(int i=1;i<n;i++){     int u,v;     scanf( %d%d ,&u,&v);     add_edge(u,v);     add_edge(v,u);    }    scanf( %d ,&k);    for(int i=1;i<=k;i++){     scanf( %d ,&a[i]);    }    init_bfs();    dep[0]=-1;    calc(1);    for(int i=1;i<=n;i++){     printf( %d  ,ans[i]);    }    puts(  );    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000000 + 5;   int vis[N], n, m;   pair<int, int> Get(int x) {     int a, b;     if (x >= n)       a = x - n + 1;     else       a = n - x + 1;     x = m - x + 1;     if (x >= n)       b = m - (x - n + 1) + 1;     else       b = m - (n - x + 1) + 1;     return pair<int, int>(a, b);   }   void dfs(int x) {     pair<int, int> cur = Get(x);     pair<int, int> nxt = Get(cur.first);     if (!vis[nxt.first]) {       vis[nxt.first] = 1;       dfs(nxt.first);     }     if (!vis[nxt.second]) {       vis[nxt.second] = 1;       dfs(nxt.second);     }     nxt = Get(cur.second);     if (!vis[nxt.first]) {       vis[nxt.first] = 1;       dfs(nxt.first);     }     if (!vis[nxt.second]) {       vis[nxt.second] = 1;       dfs(nxt.second);     }   }   int main() {     scanf( %d%d , &n, &m);     if (n > m) swap(n, m);     int ans = 0;     for (int i = 1; i <= m; i++)       if (!vis[i]) {         ans++;         vis[i] = 1;         dfs(i);       }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (a == 0) return b;     if (b == 0) return a;     if (a == b) return a;     if (a > b) return gcd(a - b, b);     return gcd(a, b - a);   }   void sol() {     long long int L, v, l, r, count = 0;     cin >> L >> v >> l >> r;     long long int p = L / v;     long long int q = (r / v) - ((l - 1) / v);     cout << p - q << endl;   }   int main() {     int t;     cin >> t;     while (t--) sol();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     long long x, y, z;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       long long ans = 0;       scanf( %lld%lld , &x, &y);       if (x >= y) {         ans += y;         printf( %lld n , ans);         continue;       }       long long d = (y - 1) / x + 1;       z = x * d - y;       ans += z * (d - 1) * (d - 1) + (x - z) * d * d;       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     string s;     cin >> s;     int count = 0, check = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  1 ) {         check = 1;       }       if (check == 1 && s[i] ==  0 ) count++;     }     if (count >= 6)       cout <<  yes ;     else {       cout <<  no ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int value[100005];   int main() {     int n, k, m;     while (cin >> n >> k >> m) {       map<string, int> mymap;       for (int i = 1; i <= n; i++) {         string s;         cin >> s;         mymap[s] = i;       }       for (int i = 1; i <= n; i++) scanf( %d , &value[i]);       for (int i = 1; i <= k; i++) {         int cnt, minv = 1000000000;         int t[100005];         scanf( %d , &cnt);         for (int j = 0; j < cnt; j++) {           scanf( %d , &t[j]);           minv = min(minv, value[t[j]]);         }         for (int j = 0; j < cnt; j++) value[t[j]] = minv;       }       long long ans = 0;       for (int i = 0; i < m; i++) {         string s;         cin >> s;         ans += value[mymap[s]];       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char a[250];     int k;     cin >> a;     cin >> k;     int temp = k;     int curr = 0;     a[strlen(a)] =   0 ;     if (strlen(a) > 1) {       while (temp) {         int max_v = a[curr] - 48, max_p = curr;         for (int j = curr + 1; j <= curr + temp && j < strlen(a); j++) {           if (a[j] - 48 > max_v) {             max_v = a[j] - 48;             max_p = j;           }         }         if (max_p != curr) {           for (int j = max_p; j > curr; j--) {             if (temp) {               swap(a[j], a[j - 1]);               temp--;             } else               break;           }         }         curr++;         if (curr >= strlen(a)) {           break;         }         a[strlen(a)] =   0 ;       }     }     cout << a;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 105;   int n, m;   char g[maxn][maxn];   int vis[3];   map<char, int> mp;   int main() {     mp[ R ] = 0;     mp[ G ] = 1;     mp[ B ] = 2;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %s , g[i]);     bool ok = true;     if (g[0][0] == g[0][m - 1] && n % 3 == 0) {       int w = n / 3;       for (int i = 0; i < n && ok; i++) {         char x = g[i][0];         int but = i + w;         for (; i < but; i++)           for (int j = 0; j < m; j++)             if (g[i][j] != x) ok = false;         int id = mp[x];         if (vis[id]) {           ok = false;           break;         }         vis[id] = 1;         i--;       }     } else if (g[0][0] == g[n - 1][0] && m % 3 == 0) {       int w = m / 3;       for (int j = 0; j < m && ok; j++) {         char x = g[0][j];         int r = j + w;         for (; j < r; j++)           for (int i = 0; i < n; i++)             if (g[i][j] != x) ok = false;         int id = mp[x];         if (vis[id]) {           ok = false;           break;         }         vis[id] = 1;         j--;       }     } else       ok = false;     puts(ok ?  YES  :  NO );     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   const long long inf = 1e18;   const double eps = 1e-9;   const double pi = 4 * atan(1.0);   const int N = int(2e5) + 100;   const int ITERS = 80;   int m;   pair<int, int> a[N];   vector<pair<int, int> > group[N];   long long sum_t[N];   int p[N];   double fen_suff[N];   inline long long val(const pair<int, int>& a, const pair<int, int>& b) {     return 1LL * a.first * b.second - 1LL * b.first * a.second;   }   inline bool cmp(const pair<int, int>& a, const pair<int, int>& b) {     return val(a, b) > 0;   }   inline void add_suff(int cp, double val) {     int pos = lower_bound(p, p + m, cp) - p;     assert(pos < m && p[pos] == cp);     while (pos >= 0) {       if (val < fen_suff[pos]) {         fen_suff[pos] = val;       } else {         break;       }       pos = (pos & (pos + 1)) - 1;     }   }   inline double get_suff(int cp) {     int pos = lower_bound(p, p + m, cp) - p;     assert(pos < m && p[pos] == cp);     double res = 1e100;     ++pos;     while (pos < m) {       res = min(res, fen_suff[pos]);       pos = (pos | (pos + 1));     }     return res;   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i].first);     }     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i].second);     }     sort(a, a + n, cmp);     for (int i = 0; i < n; ++i) {       p[i] = a[i].first;     }     sort(p, p + n);     m = unique(p, p + n) - p;     int i = 0, sz = 0;     while (i < n) {       int j = i;       while (j + 1 < n && val(a[i], a[j + 1]) == 0) {         ++j;       }       for (int z = i; z <= j; ++z) {         group[sz].push_back(a[z]);       }       sort((group[sz]).begin(), (group[sz]).end());       ++sz;       i = j + 1;     }     for (int i = 0; i < sz; ++i) {       if (i > 0) {         sum_t[i] += sum_t[i - 1];       }       for (pair<int, int> p : group[i]) {         sum_t[i] += p.second;       }     }     double l = 0, r = 1;     for (int it = 0; it < ITERS; ++it) {       double mid = (l + r) / 2;       for (int i = 0; i < m; ++i) {         fen_suff[i] = 1e100;       }       bool fl = false;       for (int i = sz - 1; i >= 0 && !fl; --i) {         int ptr = ((int)(group[i]).size()) - 1;         for (int j = ((int)(group[i]).size()) - 1; j >= 0; --j) {           while (group[i][ptr].first > group[i][j].first) {             add_suff(                 group[i][ptr].first,                 group[i][ptr].first * (1.0 - mid * sum_t[i] / sum_t[sz - 1]));             --ptr;           }           if (group[i][j].first *                   (1.0 - mid * ((i > 0 ? sum_t[i - 1] : 0) + group[i][j].second) /                              sum_t[sz - 1]) >               get_suff(group[i][j].first)) {             fl = true;             break;           }         }         while (ptr >= 0) {           add_suff(group[i][ptr].first,                    group[i][ptr].first * (1.0 - mid * sum_t[i] / sum_t[sz - 1]));           --ptr;         }       }       if (fl) {         r = mid;         continue;       }       for (int i = 0; i < m; ++i) {         fen_suff[i] = 1e100;       }       for (int i = 0; i < sz && !fl; ++i) {         int ptr = ((int)(group[i]).size()) - 1;         for (int j = ((int)(group[i]).size()) - 1; j >= 0; --j) {           while (group[i][ptr].first > group[i][j].first) {             add_suff(                 group[i][ptr].first,                 group[i][ptr].first * (1.0 - mid * sum_t[i] / sum_t[sz - 1]));             --ptr;           }           if (group[i][j].first *                   (1.0 - mid * ((i > 0 ? sum_t[i - 1] : 0) + group[i][j].second) /                              sum_t[sz - 1]) >               get_suff(group[i][j].first)) {             fl = true;             break;           }         }         while (ptr >= 0) {           add_suff(group[i][ptr].first,                    group[i][ptr].first * (1.0 - mid * sum_t[i] / sum_t[sz - 1]));           --ptr;         }       }       if (fl) {         r = mid;       } else {         l = mid;       }     }     printf( %.18lf n , (l + r) / 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9;   int main() {     int n;     cin >> n;     long long int a;     cin >> a;     while (1) {       if (a % 2 == 0)         a /= 2;       else if (a % 3 == 0)         a /= 3;       else         break;     }     bool f = 1;     for (int i = 0; i < n; i++) {       long long int b;       cin >> b;       while (1) {         if (b % 2 == 0)           b /= 2;         else if (b % 3 == 0)           b /= 3;         else           break;       }       if (a != b) f = 0;       a = b;     }     if (f)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int nums[n];     for (int i = 0; i < n; i++) {       cin >> nums[i];     }     int ans = 0;     int maxi = 0;     int mini = 1e9;     for (int i = 0; i < n; i++) {       maxi = max(maxi, nums[i]);       mini = min(mini, nums[i]);     }     int f = -1, l = -1;     for (int i = 0; i < n; i++) {       if (nums[i] == maxi) {         f = i;         break;       }     }     for (int i = n - 1; i >= 0; i--) {       if (nums[i] == mini) {         l = i;         break;       }     }     ans += f + n - 1 - l;     if (l < f) ans--;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int ar[n];       for (int i = 0; i < n; i++) {         cin >> ar[i];         --ar[i];       }       for (int i = 0; i < n; i++) {         int pos = i, j = i, cnt = 1, kaku = ar[i];         while (kaku != pos) {           j = ar[j];           kaku = ar[j];           cnt++;         }         cout << cnt <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     string Fillipino =  po ;     string Japanese1 =  desu ;     string Japanese2 =  masu ;     string Korean =  mnida ;     string s;     cin >> s;     long long n = s.size();     if (s.substr(n - 2, 2) == Fillipino) {       cout <<  FILIPINO  << endl;       return;     }     if (s.substr(n - 4, 4) == Japanese1) {       cout <<  JAPANESE  << endl;       return;     }     if (s.substr(n - 4, 4) == Japanese2) {       cout <<  JAPANESE  << endl;       return;     }     if (s.substr(n - 5, 5) == Korean) {       cout <<  KOREAN  << endl;       return;     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> st;   int main() {     ios::sync_with_stdio(0);     int n, x;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x;       if (st.empty())         st.push_back(x);       else {         st.push_back(x);         while (st.size() > 1) {           if (st[int(st.size()) - 1] != st[int(st.size()) - 2]) break;           st.pop_back();           st[int(st.size()) - 1]++;         }       }     }     cout << int(st.size()) <<   n ;     for (auto v : st) cout << v <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double ovo = 0.001;   const double eps = 1e-14;   double x, y, k[50005], b[50005];   struct node {     double p, q;     int tp, tq, id;   } t[50005];   struct qwq {     int pos, t, td, del;   } c[100005];   double a[100005], d[100005];   int n, m, p[100005], sz;   inline void add(int a, int del) {     while (a <= sz) p[a] += del, a += (a & (-a));   }   inline int query(int a) {     int s = 0;     while (a) s += p[a], a -= (a & (-a));     return s;   }   inline int min(int a, int b) { return a < b ? a : b; }   inline int max(int a, int b) { return a > b ? a : b; }   inline bool cmp(qwq a, qwq b) { return a.t < b.t; }   inline double get(double x, double y, double r) {     return (y < 0 ? x - r : r - x);   }   inline long long cal(double r) {     int i, cnt = 0, num = 0, o = 0;     long long ans = 0;     for (i = 1; i <= n; i++) {       double c1 = k[i] * k[i] + 1.0, c2 = 2.0 * k[i] * b[i],              c3 = b[i] * b[i] - r * r;       if (c2 * c2 - 4.0 * c1 * c3 < eps) continue;       double del = sqrt(c2 * c2 - 4 * c1 * c3);       double px = (-c2 + del) / (2.0 * c1), py = k[i] * px + b[i],              p = get(px, py, r);       double qx = (-c2 - del) / (2.0 * c1), qy = k[i] * qx + b[i],              q = get(qx, qy, r);       t[++cnt].p = p;       t[cnt].q = q;       t[cnt].id = i;       a[++num] = p;       a[++num] = q;     }     sort(a + 1, a + num + 1);     d[++o] = a[1];     for (i = 2; i <= num; i++) {       if (fabs(a[i] - a[i - 1]) > eps) d[++o] = a[i];     }     for (i = 1; i <= cnt; i++) {       t[i].tp = lower_bound(d + 1, d + o + 1, t[i].p - eps) - d;       t[i].tq = lower_bound(d + 1, d + o + 1, t[i].q - eps) - d;       c[i * 2 - 1].pos = t[i].id;       c[i * 2 - 1].t = min(t[i].tp, t[i].tq);       c[i * 2 - 1].del = 1;       c[i * 2].pos = t[i].id;       c[i * 2].t = max(t[i].tp, t[i].tq);       c[i * 2].del = -1;       c[i * 2].td = c[i * 2 - 1].t;     }     sort(c + 1, c + 2 * cnt + 1, cmp);     sz = c[2 * cnt].t;     for (i = 1; i <= 2 * cnt; i++) {       if (c[i].del == 1) {         add(c[i].t, 1);       } else {         ans += query(sz) - query(c[i].td);         add(c[i].td, -1);       }     }     return ans;   }   inline double get(int p, int q) {     double x = (b[q] - b[p]) / (k[p] - k[q]), y = k[p] * x + b[p];     return sqrt(x * x + y * y);   }   set<pair<int, int> > ss;   set<pair<int, int> >::iterator it;   inline double _cal(double r) {     if (r < eps) return 0;     int i, j, cnt = 0, num = 0, o = 0;     double res = 0;     for (i = 1; i <= n; i++) {       double c1 = k[i] * k[i] + 1.0, c2 = 2.0 * k[i] * b[i],              c3 = b[i] * b[i] - r * r;       if (c2 * c2 - 4.0 * c1 * c3 < eps) continue;       double del = sqrt(c2 * c2 - 4 * c1 * c3);       double px = (-c2 + del) / (2.0 * c1), py = k[i] * px + b[i],              p = get(px, py, r);       double qx = (-c2 - del) / (2.0 * c1), qy = k[i] * qx + b[i],              q = get(qx, qy, r);       t[++cnt].p = p;       t[cnt].q = q;       t[cnt].id = i;       a[++num] = p;       a[++num] = q;     }     sort(a + 1, a + num + 1);     d[++o] = a[1];     for (i = 2; i <= num; i++) {       if (fabs(a[i] - a[i - 1]) > eps) d[++o] = a[i];     }     for (i = 1; i <= cnt; i++) {       t[i].tp = lower_bound(d + 1, d + o + 1, t[i].p - eps) - d;       t[i].tq = lower_bound(d + 1, d + o + 1, t[i].q - eps) - d;       c[i * 2 - 1].pos = t[i].id;       c[i * 2 - 1].t = min(t[i].tp, t[i].tq);       c[i * 2 - 1].del = 1;       c[i * 2].pos = t[i].id;       c[i * 2].t = max(t[i].tp, t[i].tq);       c[i * 2].del = -1;       c[i * 2].td = c[i * 2 - 1].t;     }     o = 0;     sort(c + 1, c + 2 * cnt + 1, cmp);     for (i = 1; i <= 2 * cnt; i++) {       if (c[i].del == -1) {         it = ss.lower_bound(make_pair(c[i].td, c[i].pos));         while (it != ss.end()) {           if ((*it).second == c[i].pos) {             it++;             continue;           }           double dis = get(c[i].pos, (*it).second);           if (fabs(dis - r) > eps) {             res += dis;             o++;           }           it++;         }         ss.erase(ss.lower_bound(make_pair(c[i].td, c[i].pos)));       } else {         ss.insert(make_pair(c[i].t, c[i].pos));       }     }     int rm = m - o;     res += 1.0 * rm * r;     return res;   }   int main() {     int i, j, p, q, qwq = 70;     scanf( %d%d%d%d , &n, &p, &q, &m);     x = 1.0 * p * ovo;     y = 1.0 * q * ovo;     for (i = 1; i <= n; i++) {       scanf( %d%d , &p, &q);       k[i] = 1.0 * p * ovo;       b[i] = 1.0 * q * ovo;       b[i] -= (y - k[i] * x);     }     double l = 0, r = 3e9;     while (qwq--) {       double mid = (l + r) / 2.0;       if (cal(mid) < m) {         l = mid;       } else {         r = mid;       }     }     double ans = _cal(l - (1e-7));     printf( %.12lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int xx = 0, ff = 1;     char ch = getchar();     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) ff = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       xx = xx * 10 + ch -  0 ;       ch = getchar();     }     return xx * ff;   }   long long READ() {     long long xx = 0, ff = 1;     char ch = getchar();     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) ff = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       xx = xx * 10 + ch -  0 ;       ch = getchar();     }     return xx * ff;   }   char one() {     char ch = getchar();     while (ch ==     || ch ==   n ) ch = getchar();     return ch;   }   const int maxn = 110;   int N, R, cnt;   struct proj {     int a, b;     bool friend operator<(const proj& A, const proj& B) {       return A.a + A.b > B.a + B.b;     }   } P[maxn], obj[maxn];   bool vis[maxn];   int f[maxn][maxn];   int main() {     N = read(), R = read();     for (int i = 1; i <= N; i++) P[i].a = read(), P[i].b = read();     int ans = 0;     while (1) {       bool flag = 0;       for (int i = 1; i <= N; i++)         if (!vis[i] && R >= P[i].a && P[i].b >= 0) {           flag = 1;           vis[i] = 1;           R += P[i].b;           ans++;         }       if (!flag) break;     }     for (int i = 1; i <= N; i++)       if (P[i].b < 0) obj[++cnt] = P[i];     memset(f, -1, sizeof(f));     sort(obj + 1, obj + 1 + cnt);     f[0][0] = R;     for (int i = 1; i <= cnt; i++) {       for (int j = 0; j <= i; j++) {         f[i][j] = f[i - 1][j];         if (j && f[i - 1][j - 1] >= obj[i].a)           f[i][j] = max(f[i][j], f[i - 1][j - 1] + obj[i].b);       }     }     int mx = 0;     for (int i = 1; i <= cnt; i++)       for (int j = 0; j <= cnt; j++)         if (f[i][j] != -1) mx = max(mx, j);     printf( %d n , mx + ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1000000007;   long long int store[2002][2002] = {0};   long long int ncrModp(long long int n, long long int r, long long int p) {     long long int value[9999] = {0};     value[0] = 1;     for (long long int e = 1; e <= n; e++) {       store[e][0] = 1;       for (long long int f = e; f > 0; f--) {         value[f] = (value[f] + value[f - 1]) % p;         store[e][f] = value[f];       }     }     return (value[r]);   }   int bin(long long int n) {     long long int r = n;     long long int one = 0;     while (r != 0) {       one += r % 2;       r = r / 2;     }     return (one);   }   int main() {     long long int dp[1111] = {0};     dp[1] = 0;     dp[2] = 1;     for (int e = 3; e <= 1001; e++) {       long long int k = bin(e);       dp[e] = 1 + dp[k];     }     string s;     long long int n;     cin >> s;     cin >> n;     ncrModp(s.length() + 1, s.length() + 1, mod);     long long int ones = 0;     for (int u = 0; u < s.length(); u++) {       if (s[u] ==  1 ) ++ones;     }     int q = s.length() - 1;     long long int qar[1111] = {0};     for (int u = 1; u <= q; u++) {       qar[u] = store[q][u];     }     long long int u = 0, ct = 0;     long long int h = 0;     long long int t = 0;     qar[ones] += 1;     for (int w = q; w > 0; w--) {       if (s[w] ==  1 ) {         ++t;         long long int u = ones - t;         if (w != q)           for (int h = 0; h <= ct; h++) {             qar[h + ones - t] += store[ct][h];           }         else {           qar[ones - t] += 1;         }       }       ++ct;     }     long long int ans = 0;     for (int p = 1; p < s.length(); p++)       if (dp[p] == n - 1) ans += qar[p] % mod;     if (n == 1 && s !=  1 ) ans -= 1;     if (n != 0)       cout << ans % mod;     else       cout <<  1 ;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void show(T a, int n) {     for (int i = 0; i < n; ++i) cout << a[i] <<    ;     cout << endl;   }   template <class T>   void show(T a, int r, int l) {     for (int i = 0; i < r; ++i) show(a[i], l);     cout << endl;   }   const int N = 20;   const int M = 5000;   const int oo = 10000 * 10000 * 10;   int n;   char s[N + N];   long long h[N];   long long dp[N][N];   char tag[N][N];   void print(int i, int j) {     if (!i && !j) return;     if (tag[i][j] ==  H )       print(i - 1, j);     else       print(i, j - 1);     putchar(tag[i][j]);   }   void solve() {     int i, j, k;     h[n] = 1;     for (i = n - 1; i >= 1; --i) h[i] = h[i + 1] * 10;     for (i = 1; i <= n; ++i) {       int g = s[i] -  0 ;       dp[i][0] = dp[i - 1][0] + g * h[i];       tag[i][0] =  H ;       dp[0][i] = dp[0][i - 1] + g * h[i];       tag[0][i] =  M ;     }     for (i = 1; i <= n; ++i) {       for (j = 1; j <= n; ++j) {         int d = i + j;         int g = s[d] -  0 ;         dp[i][j] = (dp[i - 1][j] + g * h[i] > dp[i][j - 1] + g * h[j]                         ? dp[i - 1][j] + g * h[i]                         : dp[i][j - 1] + g * h[j]);         if (dp[i - 1][j] + g * h[i] > dp[i][j - 1] + g * h[j])           tag[i][j] =  H ;         else           tag[i][j] =  M ;       }     }     print(n, n);     putchar(10);   }   int main() {     int i, j, cas = 0;     scanf( %d , &n);     while (getchar() != 10)       ;     scanf( %s , s + 1);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int n1, long long int n2) {     if (n2 != 0)       return gcd(n2, n1 % n2);     else       return n1;   }   long long int power(long long int a, long long int b) {     if (b == 0) return 1;     if (b == 1) return a;     long long int x = power(a, b / 2);     if (b & 1) {       return a * x * x;     }     return x * x;   }   int tree[4 * (1000005)], lazy[4 * (1000005)];   void push(int ind, int l, int r) {     if (lazy[ind] == 0) return;     tree[ind] += lazy[ind];     if (l != r) {       lazy[2 * ind] += lazy[ind];       lazy[2 * ind + 1] += lazy[ind];     }     lazy[ind] = 0;   }   void upd(int ind, int l, int r, int left, int right, int val) {     push(ind, l, r);     if (left > r || l > right) return;     if (left <= l && r <= right) {       lazy[ind] += val;       push(ind, l, r);       return;     }     int mid = (l + r) / 2;     upd(ind * 2, l, mid, left, right, val);     upd(ind * 2 + 1, mid + 1, r, left, right, val);     tree[ind] = min(tree[2 * ind], tree[2 * ind + 1]);   }   int que(int ind, int l, int r) {     push(ind, l, r);     if (tree[ind] >= 0) return -1;     if (l == r) return l;     int mid = (l + r) / 2;     int res = que(2 * ind + 1, mid + 1, r);     if (res != -1) return res;     que(2 * ind, l, mid);   }   int main() {     int n, m;     cin >> n >> m;     int a[300005], b[300005];     memset(tree, 0, sizeof(tree));     memset(lazy, 0, sizeof(lazy));     for (__typeof((n)) i = (1); i <= (n); i++) {       cin >> a[i];       upd(1, 1, (1000000 + 5) - 1, 1, a[i], -1);     }     for (__typeof((m)) i = (1); i <= (m); i++) {       cin >> b[i];       upd(1, 1, (1000000 + 5) - 1, 1, b[i], 1);     }     int t;     cin >> t;     while (t--) {       int i, j, k;       cin >> i >> j >> k;       if (i == 1) {         upd(1, 1, (1000000 + 5) - 1, 1, a[j], 1);         a[j] = k;         upd(1, 1, (1000000 + 5) - 1, 1, a[j], -1);       } else {         upd(1, 1, (1000000 + 5) - 1, 1, b[j], -1);         b[j] = k;         upd(1, 1, (1000000 + 5) - 1, 1, b[j], 1);       }       cout << que(1, 1, (1000000 + 5) - 1) <<   n ;     }   }


#include <iostream>   #include <string>   #include <string.h>   #include <cstring>   #include <math.h>   #include <iomanip>   #include <vector>   #include <cmath>   #include <algorithm>   #include <numeric>   #include <cctype>   #include <utility>   #include <set>   #include <map>   #include <sstream>   #include <iterator>   #include <limits>   #include <map>   #include <deque>   #include <stack>   #include <queue>   #include <climits>   #include <tuple>   using namespace std;   #define pb push_back   #define ff(i, a, b) for (int i = a; i < b; ++i)   #define ffr(i, b, a) for (int i = b; i >= a; --i)   #define fflr(i, b, a) for (long long i = b; i >= a; --i)   #define ffl(i, a, b) for (long long i = a; i < b; ++i)   #define all(v) v.begin(), v.end()   #define endl   n    #define Yes cout <<  Yes  << endl   #define No cout <<  No  << endl   #define YES cout <<  YES  << endl   #define NO cout <<  NO  << endl   #define trav(a, x) for (auto &a : x)   #define lb lower_bound   #define ub upper_bound   #define ins insert   #define collect(v, n)                 for (ll i = 0; i < n; i++)        {                                     cin >> v[i];                  }   #define X first   #define epsilon -0.0000001f   #define Y second   #define mp make_pair   #define mt make_tuple   typedef long long ll;   typedef long double ld;   typedef std::pair<int, int> pii;   typedef pair<ll, ll> pll;   typedef std::vector<int> vec;   typedef vector<pii> vii;   typedef vector<pll> vll;   typedef std::vector<long long> vecl;   typedef map<int, int> mii;   typedef map<ll, ll> mll;      void FAST()   { // FAST Input Output, DONT use both scnaf,printf / cin,cout , Use any ONE system       ios::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);   }      const ll md = 1000000007;   #define pi 3.141592653589793238462643383279   const ll inf = 1e18L + 5; // max 1000*inf   const char nl =   n ;   const int N = 200005;      // ll sod(ll x)   // {   //     ll sum = 0;   //     while (x > 0)   //     {   //         sum += x % 10;   //         x = x / 10;   //     }   //     return sum;   // }   // float round1(float var)   // {      //     char str[40];   //     sprintf(str,  %.2f , var);   //     sscanf(str,  %f , &var);      //     return var;   // }   // bool palindrome(string s)   // {   //     int count = 0;   //     int i = 0;   //     int l = s.length();   //     ff(i, 0, l / 2)   //     {   //         if (s[i] != s[l - i - 1])   //             return 0;   //     }   //     return 1;   // }      int main()   {       //     FAST();       //     int tt{}, T{};       //     tt = 1;       //     cin >> tt;       //     T = tt;       //     while (tt--)       //     {      ll n;      cin>>n;      n=n%9;      if(n==0)n=9;     cout<<n;          return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     float x, y, avg = 0, result = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x >> y;       avg += y;     }     avg = avg / n + 5;     printf( %3f , avg);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     int64_t a, b;     cin >> a;     a = a * (a + 1) / 2;     while (cin >> b) a -= b;     cout << a;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300005;   int k, u, v, a[N], n;   long long ans, dp[N];   vector<int> G[N];   void dfs(int u, int v, int flag) {     dp[u] = a[u];     for (int i : G[u]) {       if (i != v) {         dfs(i, u, flag);         dp[u] += max(dp[i], 0ll);       }     }     if (flag)       ans = max(ans, dp[u]);     else if (dp[u] == ans)       dp[u] = 0, k++;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];     }     for (int i = 1; i < n; i++) {       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     ans = -1e9;     dfs(1, 0, 1);     dfs(1, 0, 0);     cout << ans * k <<     << k;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1024;   double p[MAXN][MAXN];   int a[MAXN];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) {       scanf( %d , a + i);       for (int j = 0; j < i; ++j) p[i][j] = a[i] > a[j], p[j][i] = 1 - p[i][j];     }     for (int x, y; m--;) {       scanf( %d%d , &x, &y), --x, --y;       for (int i = 0; i < n; ++i)         if (i != x && i != y) {           p[i][x] = p[i][y] = (p[i][x] + p[i][y]) / 2;           p[x][i] = p[y][i] = 1 - p[i][x];         }       p[x][y] = (p[x][y] + p[y][x]) / 2, p[y][x] = 1 - p[x][y];     }     double e = 0;     for (int i = 0; i < n; ++i)       for (int j = i + 1; j < n; ++j) e += p[i][j];     printf( %.16lf n , e);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long N;   string S;   long long sol;   long long dim;   long long e;   long long last() {     long long d = 1ll;     long long n = 0ll;     long long sz = (long long)(S.size());     int zero = 0;     for (long long i = sz - 1; i >= sz - dim && i >= 0 && n < N; i--) {       if (d * (long long)(S[i] -  0 ) + n < N) {         if (S[i] ==  0 )           zero++;         else           zero = 0;         n += d * (long long)(S[i] -  0 ), d *= 10ll, S.pop_back();       }     }     if (n == 0) zero--;     while (zero > 0) S.push_back( 0 ), zero--;     return n;   }   int main() {     cin >> N >> S;     long long c = N;     while (c) c /= 10, dim++;     e = 1ll;     while (!S.empty()) {       sol += e * last();       e *= N;     }     cout << sol;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   long long dp[2005][2005];   int main() {     long long n, k, sum = 0;     cin >> n >> k;     for (long long i = 1; i <= n; i++) dp[1][i] = 1;     for (int i = 2; i <= k; i++) {       for (long long j = 1; j <= n; j++) {         for (int a = j; a <= n; a += j) {           dp[i][a] = (dp[i][a] + dp[i - 1][j]) % MOD;         }       }     }     for (long long i = 1; i <= n; i++) sum = (sum + dp[k][i]) % MOD;     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 1005;   int n, m, k, now;   char s[max_n], a[max_n][max_n], b[max_n][max_n];   int ans[max_n][max_n];   inline int gcd(int a, int b) {     if (!b)       return a;     else       return gcd(b, a % b);   }   int main() {     scanf( %d%d%d n , &n, &m, &k);     for (int i = 1; i <= 2 * n - 1; ++i) {       gets(s);       if (i % 2 != 0) {         now = i / 2 + 1;         for (int j = 1; j < m; ++j) a[now][j] = s[j - 1];       } else {         now = i / 2;         for (int j = 1; j <= m; ++j) b[now][j] = s[j - 1];       }     }     if (k == 1) {       int cnt = 0;       for (int i = 1; i <= n; ++i)         for (int j = 1; j < m; ++j)           if (a[i][j] ==  E ) cnt++;       for (int i = 1; i < n; ++i)         for (int j = 1; j <= m; ++j)           if (b[i][j] ==  E ) cnt++;       int t1 = 4;       int t2 = n * (m - 1) + m * (n - 1);       int t = gcd(t1, t2);       int tt = t1 * t2 / t;       int tmp1 = 3 * (tt / t1);       int tmp2 = cnt * (tt / t2);       if (tmp1 > tmp2)         printf( NO n );       else {         printf( YES n );         for (int i = 1; i <= n; ++i)           for (int j = 1; j <= m; ++j) printf( %d%c , 1,    n [j == m]);       }       return 0;     }     if (n > m) {       for (int i = 1; i <= m; ++i) {         ans[1][i] = 0;         for (int j = 1; j < n; ++j)           if (b[j][i] ==  E )             ans[j + 1][i] = ans[j][i];           else             ans[j + 1][i] = ans[j][i] ^ 1;       }       for (int i = 1; i < m; ++i) {         int cnt = 0;         for (int j = 1; j <= n; ++j)           if (a[j][i] ==  E ) {             if (ans[j][i] != ans[j][i + 1]) cnt++;           } else {             if (ans[j][i] == ans[j][i + 1]) cnt++;           }         if (cnt > n / 2)           for (int j = 1; j <= n; ++j) ans[j][i + 1] ^= 1;       }     } else {       for (int i = 1; i <= n; ++i) {         ans[i][1] = 0;         for (int j = 1; j < m; ++j)           if (a[i][j] ==  E )             ans[i][j + 1] = ans[i][j];           else             ans[i][j + 1] = ans[i][j] ^ 1;       }       for (int i = 1; i < n; ++i) {         int cnt = 0;         for (int j = 1; j <= m; ++j)           if (b[i][j] ==  E ) {             if (ans[i][j] != ans[i + 1][j]) cnt++;           } else {             if (ans[i][j] == ans[i + 1][j]) cnt++;           }         if (cnt > m / 2)           for (int j = 1; j <= m; ++j) ans[i + 1][j] ^= 1;       }     }     printf( YES n );     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) printf( %d%c , ans[i][j] + 1,    n [j == m]);   }