#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9;   const int mod = inf + 7;   const double eps = 1e-9;   const double pi = acos(-1.0);   int n, cnt, m;   string second, t;   vector<string> g[100];   map<string, int> id;   map<int, string> st;   bool check(string second, string t) {     string cur;     if ((int)second.size() > (int)t.size()) return 1;     for (int i = (int)t.size() - (int)second.size(); i < (int)t.size(); ++i) {       cur += t[i];     }     if (cur == second) return 0;     return 1;   }   void make(int pos) {     vector<string> v;     map<string, bool> x;     for (int i = 0; i < (int)g[pos].size(); ++i) {       if (x.count(g[pos][i]) == 0) {         v.push_back(g[pos][i]);         x[g[pos][i]] = 1;       }     }     g[pos] = v;     v.clear();     for (int i = 0; i < (int)g[pos].size(); ++i) {       bool bad = 0;       for (int j = 0; j < (int)g[pos].size(); ++j) {         if (i == j) continue;         if (!check(g[pos][i], g[pos][j])) {           bad = 1;         }       }       if (!bad) {         v.push_back(g[pos][i]);       }     }     g[pos] = v;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> second;       if (id.count(second) == 0) {         id[second] = cnt;         st[cnt] = second;         cnt++;       }       int v = id[second];       cin >> m;       for (int i = 0; i < m; ++i) {         cin >> t;         g[v].push_back(t);       }     }     for (int i = 0; i < cnt; ++i) {       make(i);     }     cout << cnt << endl;     for (int i = 0; i < cnt; ++i) {       cout << st[i] <<     << (int)g[i].size() <<    ;       for (int j = 0; j < (int)g[i].size(); ++j) {         cout << g[i][j] <<    ;       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     cin >> n >> a >> b;     int ans = a;     if (b < 0) {       int t = abs(b);       while (t--) {         --a;         if (a == 0) a = n;       }     } else {       while (b--) {         ++a;         if (a == n + 1) a = 1;       }     }     cout << a << endl;   }


#include <bits/stdc++.h>   using namespace std;   map<char, int> U1;   map<char, int> U2;   map<char, int> U3;   int n, len;   int panduan(int a) {     if (a != len)       a = min(a + n, len);     else if (a == len && n == 1)       a = a - 1;     return a;   }   int main() {     cin >> n;     string a, b, c;     cin >> a;     cin >> b;     cin >> c;     int Kuro = 0, Shiro = 0, Katie = 0;     len = a.size();     for (int i = 0; i < len; i++) {       U1[a[i]]++;       if (U1[a[i]] > Kuro) Kuro = U1[a[i]];     }     for (int i = 0; i < len; i++) {       U2[b[i]]++;       if (U2[b[i]] > Shiro) Shiro = U2[b[i]];     }     for (int i = 0; i < len; i++) {       U3[c[i]]++;       if (U3[c[i]] > Katie) Katie = U3[c[i]];     }     Kuro = panduan(Kuro);     Shiro = panduan(Shiro);     Katie = panduan(Katie);     if (Kuro > Shiro && Kuro > Katie)       printf( Kuro n );     else if (Shiro > Kuro && Shiro > Katie)       printf( Shiro n );     else if (Katie > Kuro && Katie > Shiro)       printf( Katie n );     else       printf( Draw n );   }


#include <bits/stdc++.h>   using namespace std;   struct trip {     int a, b, c;   };   const int MAXINT = 100;   int n, temp;   vector<int> pos, neg;   bool zeros = false;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> temp;       if (temp > 0)         pos.push_back(temp);       else if (temp < 0)         neg.push_back(temp);       else         zeros = true;     }     if (!pos.size() && !neg.size()) cout << 0 << endl;     if (pos.size() == 0 && neg.size() == 1) {       if (zeros)         cout << 0 << endl;       else         cout << neg[0] << endl;     }     for (int e : pos) cout << e <<    ;     if (neg.size() % 2 == 0)       for (int e : neg) cout << e <<    ;     else {       sort(neg.begin(), neg.end());       int N = neg.size();       for (int i = 0; i < N - 1; i++) cout << neg[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long int, long long int> > v;   long long int indegree[300005];   long long int vis[300005];   long long int parent[300005];   long long int getpar(long long int child) {     if (parent[child] == child)       return child;     else       return parent[child] = getpar(parent[child]);   }   void merge(long long int v1, long long int v2) {     parent[getpar(v1)] = getpar(v2);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int TESTS = 1;     while (TESTS--) {       long long int n, m;       cin >> n >> m;       v.resize(m);       memset(indegree, 0, sizeof(indegree));       memset(vis, 0, sizeof(vis));       for (long long int i = 1; i <= n; i++) {         parent[i] = i;       }       for (long long int i = 0; i < m; i++) {         cin >> v[i].first >> v[i].second;         indegree[v[i].first]++;         indegree[v[i].second]++;       }       long long int maxi = 0, idx = -1;       for (long long int i = 1; i <= n; i++) {         if (indegree[i] >= maxi) {           maxi = indegree[i];           idx = i;         }       }       for (long long int i = 0; i < m; i++) {         if (v[i].first == idx || v[i].second == idx) {           vis[i] = 1;           merge(v[i].first, v[i].second);         }       }       for (long long int i = 0; i < m; i++) {         if (vis[i] == 0) {           if (getpar(v[i].first) != getpar(v[i].second)) {             merge(v[i].first, v[i].second);             vis[i] = 1;           }         }       }       for (long long int i = 0; i < m; i++) {         if (vis[i] == 1) {           cout << v[i].first <<     << v[i].second <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int k;     cin >> k;     string str, s;     cin >> str;     s = str;     for (long long int i = 1; i < k; i++) str = str + s;     map<char, vector<long long int>> pos;     long long int i = 0;     for (auto c : str) {       pos[c].push_back(i);       i++;     }     long long int n;     cin >> n;     char c;     long long int p;     while (n--) {       cin >> p >> c;       str[pos[c][p - 1]] = 0;       pos[c].erase(pos[c].begin() + (p - 1));     }     for (auto c : str)       if (c) cout << c;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 100100 * 5;   const long long N = 50005;   long long m, b, k;   map<long long, long long> fre;   int v1[M], mmax;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> m;     for (int i = 0; i < m; i++) {       cin >> b;       fre[b]++;     }     for (int i = 0; i < N; i++) {       if (fre[i]) v1[k] = i, k++, fre[i]--, mmax = max(mmax, i);     }     for (int i = mmax - 1; i >= 0; i--) {       if (fre[i]) v1[k] = i, k++;     }     cout << k << endl;     for (int i = 0; i < k; i++) cout << v1[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> arr, hgt, res;   int n;   int func(int i, int j) {}   int main() {     int a, b, c, d, e, f, g;     cin >> a;     while (a--) {       cin >> b;       vector<int> num;       for (int i = 0; i < b; i++) {         cin >> c;         num.push_back(c);       }       if (num[b - 1] < num[0]) {         cout <<  NO  << endl;       } else {         cout <<  YES  << endl;       }       num.clear();     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:36777216 )   using namespace std;   const int inf = 1 << 30;   const long long Inf = 1LL << 62;   const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1, 0};   const int dy[] = {0, 1, 0, -1, -1, 1, 1, -1, 0};   const double eps = 1e-9;   const double pi = 3.1415926535897932384626433832795;   template <class T>   inline bool eq(const T &a, const T &b) {     return fabs(a - b) < eps;   }   template <class T>   inline bool deg2(const T &n) {     return (n & (n - 1)) == 0;   }   template <class T>   inline void chmax(T &a, const T &b) {     a = max(a, b);   }   inline int intRand(int l, int r) {     return ((rand() << 16) | rand()) % (r - l + 1) + l;   }   inline long long llRand(long long l, long long r) {     return (((1ll * rand()) << 48) | ((1ll * rand()) << 32) | (rand() << 16) |             rand()) %                (r - l + 1) +            l;   }   bool is[100500], dd;   int s[1000], a[100500], n, m, i, ii, cnt, j, pr[100000], pp;   char q;   bool prime(int n) {     for (int i = 2; i * i <= n; i++)       if (n % i == 0) return 0;     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     srand(time(NULL));     for (i = 2; i < 100000; i++)       if (prime(i)) pr[pp++] = i;     scanf( %d%d n , &n, &m);     while (m--) {       scanf( %c%d n , &q, &i);       if (q ==  + ) {         if (is[i]) {           puts( Already on );           continue;         }         ii = i;         cnt = 0;         for (j = 0; 1ll * pr[j] * pr[j] <= 1ll * i; j++) {           dd = 0;           while (i % pr[j] == 0) {             i /= pr[j];             dd = 1;           }           if (dd) s[cnt++] = pr[j];         }         if (i > 1) s[cnt++] = i;         dd = 0;         for (j = 0; j < cnt; j++)           if (a[s[j]]) {             printf( Conflict with %d n , a[s[j]]);             dd = 1;             break;           }         if (dd) continue;         puts( Success );         for (j = 0; j < cnt; j++) a[s[j]] = ii;         is[ii] = 1;       } else {         if (!is[i]) {           printf( Already off n );           continue;         }         is[i] = 0;         for (j = 0; 1ll * pr[j] * pr[j] <= 1ll * i; j++) {           dd = 0;           while (i % pr[j] == 0) {             i /= pr[j];             a[pr[j]] = 0;           }         }         if (i > 1) a[i] = 0;         puts( Success );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 10, LOG = 23, MAX = 1e6 + 10, M = 1e9 + 7;   long long n;   long long mark[N], level[N], par[N][LOG + 10], arr[MAX], cnt[N], tavan[MAX];   vector<long long> g[N];   map<pair<long long, long long>, long long> mp;   void DFS(long long v) {     mark[v] = 1;     for (long long u : g[v]) {       if (!mark[u]) {         level[u] = level[v] + 1;         par[u][0] = v;         DFS(u);       }     }   }   long long DFS2(long long v) {     mark[v] = 2;     for (long long u : g[v]) {       if (mark[u] != 2) cnt[v] += DFS2(u);     }     return cnt[v];   }   long long getpar(long long v, long long dis) {     long long cnt = 0;     while (dis) {       if (dis % 2) v = par[v][cnt];       cnt++;       dis /= 2;     }     return v;   }   long long MOD(long long a) { return (((a % M) + M) % M); }   void UPDATE() {     for (long long i = 1; i <= LOG; i++) {       for (long long node = 1; node <= n; node++)         par[node][i] = par[par[node][i - 1]][i - 1];     }     tavan[0] = 1;     for (long long i = 1; i < MAX; i++) {       tavan[i] = MOD(tavan[i - 1] * 2);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long u, v, x, q, ans = 0;     cin >> n;     for (long long i = 0; i < n - 1; i++) {       cin >> u >> v >> x;       mp[{u, v}] = x;       g[u].push_back(v);       g[v].push_back(u);     }     DFS(1);     UPDATE();     cin >> q;     arr[0] = 1;     for (long long i = 1; i <= q; i++) {       cin >> arr[i];       long long lca;       v = arr[i];       u = arr[i - 1];       cnt[u]++;       cnt[v]++;       if (level[v] < level[u]) swap(v, u);       v = getpar(v, level[v] - level[u]);       if (v == u)         lca = v;       else {         for (long long j = LOG; j >= 0; j--) {           if (par[v][j] != par[u][j]) {             v = par[v][j];             u = par[u][j];           }         }         lca = par[v][0];       }       cnt[lca] -= 2;     }     DFS2(1);     for (long long i = 1; i <= n; i++) {       v = i;       u = par[i][0];       if ((mp.count({v, u}) && mp[{v, u}]) || (mp.count({u, v}) && mp[{u, v}])) {         if (cnt[v] % 2 == 0)           ans += tavan[cnt[v] / 2] - 1;         else {           if (mp.count({v, u}))             ans += tavan[(cnt[v] + 1) / 2] - 1;           else             ans += tavan[cnt[v] / 2] - 1;         }         ans = MOD(ans);       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T MIN_3(T a, T b, T c) {     return min(min(a, b), c);   }   template <class T>   inline T MAX_3(T a, T b, T c) {     return max(max(a, b), c);   }   template <class T>   inline T BIGMOD(T n, T m, T mod) {     long long int ans = 1;     long long int k = n;     while (m) {       if (m & 1) {         ans *= k;         if (ans > mod) ans %= mod;       }       k *= k;       if (k > mod) k %= mod;       m >>= 1;     }     return ans;   }   template <class T>   string toString(T n) {     ostringstream ost;     ost << n;     ost.flush();     return ost.str();   }   template <class T>   string toBinary(T n) {     string ret =   ;     while (n) {       if (n % 2 == 1)         ret +=  1 ;       else         ret +=  0 ;       n >>= 1;     }     reverse(ret.begin(), ret.end());     return ret;   }   void combination(int n, vector<vector<int> > &ret) {     ret.resize(n + 1, vector<int>(n + 1, 0));     for (int i = 1; i <= n; i++) {       ret[i][0] = ret[i][i] = 1;       for (int j = 1; j < i; j++) {         ret[i][j] = ret[i - 1][j] + ret[i - 1][j - 1];       }     }   }   int toInt(string s) {     int r = 0;     istringstream sin(s);     sin >> r;     return r;   }   long long int toLInt(string s) {     long long int r = 0;     istringstream sin(s);     sin >> r;     return r;   }   double toDouble(string s) {     double r = 0;     istringstream sin(s);     sin >> r;     return r;   }   vector<string> parse(string temp) {     vector<string> ans;     ans.clear();     string s;     istringstream iss(temp);     while (iss >> s) ans.push_back(s);     return ans;   }   template <class T>   inline T gcd(T a, T b) {     if (a < 0) return gcd(-a, b);     if (b < 0) return gcd(a, -b);     return (b == 0) ? a : gcd(b, a % b);   }   template <class T>   inline T lcm(T a, T b) {     if (a < 0) return lcm(-a, b);     if (b < 0) return lcm(a, -b);     return a * (b / gcd(a, b));   }   template <class T>   inline T power(T b, T p) {     if (p < 0) return -1;     if (b <= 0) return -2;     if (!p) return 1;     return b * power(b, p - 1);   }   template <class T>   inline int asd(T &ret) {     char r;     bool start = false, neg = false;     ret = 0;     bool isaNumber = false;     while (true) {       r = getchar();       if (r == EOF) {         return 0;       }       if ((r -  0  < 0 || r -  0  > 9) && r !=  -  && !start) {         continue;       }       if ((r -  0  < 0 || r -  0  > 9) && r !=  -  && start) {         break;       }       if (start) ret *= 10;       start = true;       if (r ==  - )         neg = true;       else         ret += r -  0 ;     }     if (neg) ret *= -1;     return 1;   }   template <class T>   inline void asdasd(T x, char y) {     if (x < 0) {       putchar( - );       x = -x;     }     char buf[21], *p = buf;     do {       *p++ =  0  + x % 10;       x /= 10;     } while (x);     do {       putchar(*--p);     } while (p > buf);     if (y == 0)       putchar(  n );     else if (y == 1)       putchar(   );   }   vector<int> pr;   int inp[101];   int dp[101][1 << 16];   int go[101][1 << 16];   int n;   int bp[61];   void make(int &ret, int n) {     for (int i = 0; i <= pr.size() - 1; i++) {       if (n % pr[i] == 0) ret |= (1 << i);     }   }   int inf = 100000000, sat = (1 << 16) - 1;   int rec(int pos, int mask) {     if (pos == n) return 0;     int &ret = dp[pos][mask];     if (ret != -1) return ret;     ret = inf;     int now;     for (int i = 1; i <= 58; i++) {       if ((bp[i] & mask) == 0) {         now = abs(inp[pos] - i) + rec(pos + 1, mask | bp[i]);         if (ret > now) {           ret = now;           go[pos][mask] = i;         }       }     }     return ret;   }   vector<int> ans;   void path(int pos, int mask) {     if (pos == n) return;     ans.push_back(go[pos][mask]);     path(pos + 1, mask | bp[go[pos][mask]]);   }   int main() {     for (int i = 2; i <= 60; i++) {       bool ok = true;       for (int j = 2; j < i; j++) {         if (i % j == 0) {           ok = false;           break;         }       }       if (ok) pr.push_back(i);     }     for (int i = 2; i <= 60; i++) {       make(bp[i], i);     }     asd(n);     for (int i = 0; i <= n - 1; i++) {       asd(inp[i]);     }     memset(dp, -1, sizeof(dp));     rec(0, 0);     path(0, 0);     for (int i = 0; i <= ans.size() - 1; i++) asdasd(ans[i], i != ans.size() - 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 5;   int n;   char a[MAX];   pair<char, int> b[MAX];   int main() {     cin >> (a + 1);     n = strlen(a + 1);     for (int i = 1; i <= n; ++i) {       b[i].first = a[i];       b[i].second = i;     }     sort(b + 1, b + n + 1,          [&](const pair<char, int> &a, const pair<char, int> &b) {            if (a.first == b.first) {              return a.second < b.second;            }            return a.first > b.first;          });     for (int i = 1, j = 0; i <= n; ++i) {       if (j < b[i].second) {         cout << b[i].first;         j = b[i].second;       }     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   struct Rect {     int right, left, top, bottom;     int height() { return top - bottom; }     int width() { return right - left; }     int area() { return (top - bottom) * (right - left); }   };   void solve() {     int w, h;     cin >> w >> h;     Rect first, second;     second.bottom = 0, second.left = 0;     cin >> first.left >> first.bottom >> first.right >> first.top;     cin >> second.right >> second.top;     bool move_y = first.height() + second.height() <= h,          move_x = first.width() + second.width() <= w;     if (!move_y && !move_x) {       cout << -1 <<   n ;       return;     } else {       vector<int> moves;       if (move_x) {         moves.push_back(second.right - first.left);         moves.push_back(first.right - (w - second.width()));       }       if (move_y) {         moves.push_back(second.top - first.bottom);         moves.push_back(first.top - (h - second.height()));       }       int ans = INT_MAX;       for (auto i : moves) {         ans = min(ans, max(i, 0));       }       cout << ans;     }     cout <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     vector<long long> L(n), R(n), A(n);     for (int i = 0; i < n; ++i) {       cin >> L[i];     }     for (int i = 0; i < n; ++i) {       cin >> R[i];     }     for (int i = 0; i < n; ++i) {       A[i] = n - L[i] - R[i];     }     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j < n; ++j) {         if (A[j] > A[i]) {           R[i]--;         }       }       for (int j = 0; j < i; ++j) {         if (A[j] > A[i]) {           L[i]--;         }       }     }     for (int i = 0; i < n; ++i) {       if (L[i] || R[i]) {         cout <<  NO               <<   n ;         return 0;       }     }     cout <<  YES           <<   n ;     for (int i : A) {       cout << i <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void smin(T &a, U b) {     if (a > b) a = b;   }   template <typename T, typename U>   inline void smax(T &a, U b) {     if (a < b) a = b;   }   template <typename T>   inline void gn(T &first) {     char c, sg = 0;     while (c = getchar(), (c >  9  || c <  0 ) && c !=  - )       ;     for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ;          c = getchar())       first = (first << 1) + (first << 3) + c -  0 ;     if (sg) first = -first;   }   template <class T, class T1>   inline void gn(T &first, T1 &second) {     gn(first);     gn(second);   }   template <class T, class T1, class T2>   inline void gn(T &first, T1 &second, T2 &z) {     gn(first);     gn(second);     gn(z);   }   template <typename T>   inline void print(T first) {     if (first < 0) {       putchar( - );       return print(-first);     }     if (first < 10) {       putchar( 0  + first);       return;     }     print(first / 10);     putchar(first % 10 +  0 );   }   template <typename T>   inline void println(T first) {     print(first), putchar(  n );   }   template <typename T>   inline void printsp(T first) {     print(first), putchar(   );   }   template <class T, class T1>   inline void print(T first, T1 second) {     printsp(first), println(second);   }   template <class T, class T1, class T2>   inline void print(T first, T1 second, T2 z) {     printsp(first), printsp(second), println(z);   }   int power(int a, int b, int m, int ans = 1) {     for (; b; b >>= 1, a = 1LL * a * a % m)       if (b & 1) ans = 1LL * ans * a % m;     return ans;   }   int main() {     int a, b, p;     long long first;     gn(a, b, p);     gn(first);     long long ans = 0;     long long M = (long long)p * (p - 1);     int p1 = power(p - 1, p - 2, p);     int p2 = p == 2 ? 1 : power(p, p - 3, p - 1);     for (int r = 0; r < p - 1; r++) {       int second = (long long)b * power(a, p - 1 - r, p) % p;       long long rr =           ((long long)r * p % M * p2 + (long long)second * (p - 1) % M * p1) % M;       if (rr <= first) ans += (first - rr) / M + 1;     }     println(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 505;   int n, m, qc;   bool fd[N][N];   bitset<N> bs1[N][N];   bitset<N> bs2[N][N];   struct query {     int x1, y1, x2, y2;     int id;   };   vector<query> qs;   vector<bool> ans;   void solve(vector<int>& ids, int tl, int tr) {     if (ids.size() == 0) return;     if (tl > tr) return;     vector<int> l;     vector<int> r;     int tm = (tl + tr) / 2;     for (int i = n - 1; i >= 0; i--) {       bs1[i][tm].reset();       if (!fd[i][tm]) {         bs1[i][tm][i] = true;         if (i + 1 < n) bs1[i][tm] |= bs1[i + 1][tm];       }     }     for (int i = n - 1; i >= 0; i--) {       for (int j = tm - 1; j >= tl; j--) {         bs1[i][j].reset();         if (fd[i][j]) continue;         if (i + 1 < n) bs1[i][j] |= bs1[i + 1][j];         bs1[i][j] |= bs1[i][j + 1];       }     }     for (int i = 0; i < n; i++) {       bs2[i][tm].reset();       if (!fd[i][tm]) {         bs2[i][tm][i] = true;         if (i > 0) bs2[i][tm] |= bs2[i - 1][tm];       }     }     for (int i = 0; i < n; i++) {       for (int j = tm + 1; j <= tr; j++) {         bs2[i][j].reset();         if (fd[i][j]) continue;         if (i - 1 >= 0) bs2[i][j] |= bs2[i - 1][j];         bs2[i][j] |= bs2[i][j - 1];       }     }     for (int id : ids) {       query& q = qs[id];       if (q.y1 < tm && q.y2 < tm) {         l.push_back(id);       } else if (q.y1 > tm && q.y2 > tm) {         r.push_back(id);       } else {         bitset<N> bans = bs1[q.x1][q.y1] & bs2[q.x2][q.y2];         ans[id] = bans.any();       }     }     solve(l, tl, tm - 1);     solve(r, tm + 1, tr);   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     ;     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         char c;         cin >> c;         fd[i][j] = (c ==  # );       }     }     cin >> qc;     qs.resize(qc);     for (int i = 0; i < qc; i++) {       int x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       x1--;       y1--;       x2--;       y2--;       qs[i] = {x1, y1, x2, y2, i};     }     vector<int> ids;     ids.reserve(qc);     for (int i = 0; i < qc; i++) {       if (qs[i].x1 > qs[i].x2 || qs[i].y1 > qs[i].y2) continue;       ids.push_back(i);     }     ans.resize(qc);     solve(ids, 0, m - 1);     for (bool t : ans) cout << (t ?  Yes  :  No ) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2015;   const int inf = 1 << 29;   int n, h, a[maxn], l[maxn], r[maxn];   double p, dp[maxn][maxn][2][2];   int main() {     cin >> n >> h >> p;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     for (int i = 0; i < n; i++) {       int lb = i - 1;       for (; lb >= 0 && a[lb + 1] - a[lb] < h; lb--)         ;       l[i] = lb;       int rb = i + 1;       for (; rb < n && a[rb] - a[rb - 1] < h; rb++)         ;       r[i] = rb;     }     for (int len = 1; len <= n; len++) {       for (int leftFall = 0; leftFall < 2; leftFall++) {         for (int rightFall = 0; rightFall < 2; rightFall++) {           for (int start = 0; start + len - 1 < n; start++) {             int end = start + len - 1;             if (leftFall == 0) {               int tmp = -inf;               if (start > 0) {                 tmp = a[start - 1];               }               dp[start][end][leftFall][rightFall] +=                   0.5 * p *                   (min(h, a[start] - tmp) + dp[start + 1][end][0][rightFall]);             } else {               int tmp = -inf;               if (start > 0) {                 tmp = a[start - 1] + h;               }               dp[start][end][leftFall][rightFall] +=                   0.5 * p *                   (min(h, a[start] - tmp) + dp[start + 1][end][0][rightFall]);             }             if (rightFall == 1) {               int tmp = inf;               if (end < n - 1) {                 tmp = a[end + 1];               }               dp[start][end][leftFall][rightFall] +=                   0.5 * (1 - p) *                   (min(h, tmp - a[end]) + dp[start][end - 1][leftFall][1]);             } else {               int tmp = inf;               if (end < n - 1) {                 tmp = a[end + 1] - h;               }               dp[start][end][leftFall][rightFall] +=                   0.5 * (1 - p) *                   (min(h, tmp - a[end]) + dp[start][end - 1][leftFall][1]);             }             if (end < r[start]) {               dp[start][end][leftFall][rightFall] +=                   0.5 * (1 - p) * (a[end] - a[start]);               if (rightFall == 1) {                 int tmp = inf;                 if (end < n - 1) {                   tmp = a[end + 1];                 }                 dp[start][end][leftFall][rightFall] +=                     0.5 * (1 - p) * min(h, tmp - a[end]);               } else {                 int tmp = inf;                 if (end < n - 1) {                   tmp = a[end + 1] - h;                 }                 dp[start][end][leftFall][rightFall] +=                     0.5 * (1 - p) * min(h, tmp - a[end]);               }             } else {               dp[start][end][leftFall][rightFall] +=                   0.5 * (1 - p) *                   (a[r[start] - 1] - a[start] + h +                    dp[r[start]][end][1][rightFall]);             }             if (l[end] < start) {               dp[start][end][leftFall][rightFall] +=                   0.5 * p * (a[end] - a[start]);               if (leftFall == 0) {                 int tmp = -inf;                 if (start > 0) {                   tmp = a[start - 1];                 }                 dp[start][end][leftFall][rightFall] +=                     0.5 * p * min(h, a[start] - tmp);               } else {                 int tmp = -inf;                 if (start > 0) {                   tmp = a[start - 1] + h;                 }                 dp[start][end][leftFall][rightFall] +=                     0.5 * p * min(h, a[start] - tmp);               }             } else {               dp[start][end][leftFall][rightFall] +=                   0.5 * p *                   (a[end] - a[l[end] + 1] + h + dp[start][l[end]][leftFall][0]);             }           }         }       }     }     cout << setprecision(9) << fixed << dp[0][n - 1][0][0] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int getcom(pair<int, int> a, pair<int, int> b) {     vector<int> all;     all.push_back(a.first);     all.push_back(a.second);     all.push_back(b.first);     all.push_back(b.second);     sort(all.begin(), all.end());     int com = -1;     for (int k = 1; k < 4; k++) {       if (all[k] == all[k - 1]) {         com = all[k];       }     }     return com;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<pair<int, int> > a;     vector<pair<int, int> > b;     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       if (x > y) {         swap(x, y);       }       a.push_back(make_pair(x, y));     }     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       if (x > y) {         swap(x, y);       }       b.push_back(make_pair(x, y));     }     vector<int> possible;     bool theyknow = true;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (a[i] == b[j]) {           continue;         }         int com = getcom(a[i], b[j]);         if (com == -1) {           continue;         }         if (com != -1) {           possible.push_back(com);         }         for (int k = 0; k < m; k++) {           if (a[i] == b[k]) {             continue;           }           int com2 = getcom(a[i], b[k]);           if (com2 != -1 && com2 != com) {             theyknow = false;           }         }         for (int k = 0; k < n; k++) {           if (a[k] == b[j]) {             continue;           }           int com2 = getcom(a[k], b[j]);           if (com2 != -1 && com2 != com) {             theyknow = false;           }         }       }     }     sort(possible.begin(), possible.end());     if (possible[0] == possible[possible.size() - 1]) {       cout << possible[0] << endl;       return 0;     }     if (theyknow) {       cout << 0 << endl;     } else {       cout << -1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   bool solve(string temp, string p) {     int i = 0, j = 0;     while (i != temp.size()) {       if (temp[i] == p[j]) {         i++;         j++;       } else         i++;       if (j == p.size()) break;     }     if (i <= temp.size() && j >= p.size())       return true;     else       return false;   }   int main() {     string s, p;     cin >> s >> p;     int n = s.size();     int arr[n];     for (int i = 0; i < n; i++) cin >> arr[i];     int lo = 0, hi = n - 1, mid = (hi + lo) / 2;     while (lo <= hi) {       string temp = s;       for (int i = 0; i < mid; i++) temp[arr[i] - 1] =  0 ;       if (solve(temp, p))         lo = mid + 1;       else         hi = mid - 1;       mid = (hi + lo) / 2;     }     cout << mid;   }


#include <bits/stdc++.h>   using namespace std;   int total_operation = 0;   bool is_prime(long long n) {     if (n == 2) return true;     if (n % 2 == 0 || n < 2) return false;     for (int i = 3; i * i <= n; i += 2) {       if (n % i == 0) return false;     }     return true;   }   int countprime_in_range(int n) {     vector<bool> isprime(n + 1, true);     isprime[0] = isprime[1] = false;     for (int i = 2; i * i <= n; ++i) {       if (isprime[i]) {         for (int j = 2 * i; j <= n; j += i) {           isprime[j] = 0;         }       }     }     int cnt = 0;     for (int i = 0; i <= n; i++) {       if (isprime[i]) cnt++;     }     return cnt;   }   int main() {     int fact1[3], fact2[3];     fact1[0] = fact1[1] = fact1[2] = 0;     fact2[0] = fact2[1] = fact2[2] = 0;     int A, B;     cin >> A >> B;     if (A == B) {       cout <<  0 ;       return 0;     }     while (A) {       if (!(A % 2))         fact1[0]++, A /= 2;       else if (!(A % 3))         fact1[1]++, A /= 3;       else if (!(A % 5))         fact1[2]++, A /= 5;       else         break;     }     while (B) {       if (!(B % 2))         fact2[0]++, B /= 2;       else if (!(B % 3))         fact2[1]++, B /= 3;       else if (!(B % 5))         fact2[2]++, B /= 5;       else         break;     }     if (A != B)       cout <<  -1 ;     else       cout << abs(fact1[0] - fact2[0]) + abs(fact1[1] - fact2[1]) +                   abs(fact1[2] - fact2[2]);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n, a, b;       cin >> n >> a >> b;       string s =   ;       char x =  a ;       for (int i = 0; i < n; i++) {         if (i % b == 0) x =  a ;         s += x;         x++;       }       cout << s << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e5 + 3;   string s, p;   int F[MAX][2];   void solve() {     cin >> s >> p;     F[0][0] = F[0][1] = 0;     for (int i = 1; i <= p.size(); ++i) {       for (int j = 0; j < 2; ++j) {         F[i][j] = F[i - 1][j];       }       ++F[i][p[i - 1] -  0 ];     }     long long int ans = 0;     for (int i = 0; i < s.size(); ++i) {       int c = s[i] -  0 ;       for (int j = 0; j < 2; ++j) {         ans += abs(c - j) * (F[p.size() - s.size() + i + 1][j] - F[i][j]);       }     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     solve();     return 0;   }


#include <bits/stdc++.h>   std::string x;   long long getlen(long long r) {     long long now = 1, lst = 0, res = 0;     int dig = 0;     while (now <= r) {       res += (now - 1 - lst) * dig;       lst = now - 1;       now *= 10;       ++dig;     }     res += (r - lst) * dig;     return res;   }   int main() {     long long l = 1, r = 1000000000000ll, ans = 0;     long long t;     std::cin >> t;     while (l <= r) {       long long mid = l + r >> 1;       if (getlen(mid) >= t)         r = mid - 1;       else         l = mid + 1, ans = mid;     }     t -= getlen(ans);     std::string st;     for (long long i = ans + 1; i <= ans + 10; ++i) st += std::to_string(i);     printf( %c n , st[t - 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   vector<pair<int, int>> ans;   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     reverse(a.begin(), a.end());     vector<pair<int, int>> ans;     int m = a[0] + 1;     vector<int> cnt(m);     int l = 0, r = n;     int st = 0;     int en = m;     while (l < r) {       if (cnt[en - 1] == a[r - 1]) {         --en;         --r;         continue;       }       if (a[l] >= en) l++;       for (int i = 0; i < a[l] - st; i++) {         ans.emplace_back(st, st + i + 1);         cnt[st]++;         cnt[st + i + 1]++;       }       en = a[l] + 1;       st++;     }     cout << ans.size() << endl;     for (auto p : ans) cout << p.first + 1 <<     << p.second + 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[1000005], dp1[1000005], dp2[1000005], dp3[1000005];   long long ans;   int main(int argc, const char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 0; i <= m + 1; ++i) {       dp1[i] = 1e9;       dp2[i] = -1e9;     }     int x;     int mn = 1e9;     for (int i = n; i > 0; --i) {       x = a[i];       if (mn < x && mn < dp1[x]) dp1[x] = mn;       if (x < mn) mn = x;     }     int mx = -1e9;     for (int i = 1; i <= n; ++i) {       x = a[i];       if (mx > x && mx > dp2[x]) dp2[x] = mx;       if (x > mx) mx = x;     }     for (int i = m; i > 0; --i) {       dp3[i] = dp1[i];       if (dp1[i + 1] < dp1[i]) dp1[i] = dp1[i + 1];     }     for (int i = m; i > 0; --i) {       if (dp2[i + 1] > dp2[i]) dp2[i] = dp2[i + 1];     }     int l, r;     for (int i = 1; i <= m; ++i) {       l = i;       r = m;       while (l < r) {         int mid = (l + r) >> 1;         if (dp1[mid + 1] >= i && dp2[mid + 1] <= mid)           r = mid;         else           l = mid + 1;       }       if (dp1[r + 1] >= i && dp2[r + 1] <= r) ans += m - r + 1;       if (dp3[i] < i) break;     }     cout << ans <<   n ;     return 0;   }


#include<cstdio>   #include<cstring>   #include<algorithm>   using namespace std;   const int mod=1e9+7;   inline int addmod(int x)   {    return x>=mod?x-mod:x;   }   inline int submod(int x)   {    return x<0?x+mod:x;    }   int n,m,q,b[105],c[105],sum[105],f[105][10005],qans[1005],pn;   int solve(int x)   {    memset(f,0,sizeof(f));    f[0][0]=1;    int nw=0,ans=0;    for(int i=1;i<=n;i++)    {     for(int j=0;j<=nw;j++)     {      f[i][j]=addmod(f[i][j]+f[i-1][j]);      f[i][j+c[i]+1]=submod(f[i][j+c[i]+1]-f[i-1][j]);     }     nw+=c[i];     for(int j=1;j<=nw;j++)      f[i][j]=addmod(f[i][j]+f[i][j-1]);     for(int j=0;j<x*i+sum[i];j++)      f[i][j]=0;    }    for(int i=0;i<=nw;i++)     ans=addmod(ans+f[n][i]);    return ans;   }   int main()   {    scanf( %d ,&n);    int mul=1;    for(int i=1;i<=n;i++)    {     scanf( %d ,&c[i]);     m=max(m,c[i]);     mul=1ll*mul*(c[i]+1)%mod;    }    for(int i=1;i<n;i++)     scanf( %d ,&b[i]);    for(int i=1;i<=n;i++)     for(int j=1;j<i;j++)      sum[i]+=(i-j)*b[j];    pn=(-sum[n])/n;    for(int i=0;i<=m;i++)     qans[i]=solve(i+pn);    scanf( %d ,&q);    for(int i=1;i<=q;i++)    {     int x;     scanf( %d ,&x);     if(x<pn) printf( %d n ,mul);     else if(x>pn+m) printf( 0 n );     else printf( %d n ,qans[x-pn]);    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     int i, n, e;     double x1, y1, x2, y2, vmax, t, vx, vy, wx, wy;     cin >> x1 >> y1 >> x2 >> y2;     cin >> vmax >> t;     cin >> vx >> vy;     cin >> wx >> wy;     double sid;     double l = 0.0, r = (double)1e9;     double eps = 1e-7;     double x, y;     while (r - l > eps) {       sid = (r + l) / 2.0;       if (sid < t) {         x = x1 + sid * vx;         y = y1 + sid * vy;       } else {         x = x1 + t * vx + (sid - t) * wx;         y = y1 + t * vy + (sid - t) * wy;       }       double val;       val = (x2 - x) * (x2 - x) + (y2 - y) * (y2 - y);       val = sqrt(val);       if (val < vmax * sid) {         r = sid;       } else         l = sid;     }     cout << setprecision(8);     cout << fixed;     cout << r <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void reshenie() {     int n, m;     cin >> n >> m;     int a[n][n];     for (int i = 0; i < n; ++i) {       for (int j = i; j < n; ++j) {         a[i][j] = a[j][i] = 0;       }     }     while (m--) {       int u, v;       cin >> u >> v;       --u, --v;       a[u][v] = a[v][u] = 1;     }     if (n == 1) {       cout <<  Yes na  << endl;       return;     }     string s(n,  * );     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j < n; ++j) {         if (a[i][j]) {           s[i] = s[j] =  b ;         }       }     }     for (int i = 0; i < n; ++i) {       if (accumulate(a[i], a[i] + n, 0) != n - 1) {         if (s[i] !=  c ) {           s[i] =  a ;           for (int j = i + 1; j < n; ++j) {             if (!a[i][j]) {               s[j] =  c ;             }           }         } else {           s[i] =  c ;           for (int j = i + 1; j < n; ++j) {             if (!a[i][j]) {               s[j] =  a ;             }           }         }       }     }     for (int i = 0; i < n; ++i) {       if (s[i] ==  * ) {         cout <<  No  << endl;         return;       }       for (int j = i + 1; j < n; ++j) {         if (a[i][j] && abs(s[i] - s[j]) <= 1) continue;         if (!a[i][j] && abs(s[i] - s[j]) > 1) continue;         cout <<  No  << endl;         return;       }     }     cout <<  Yes n  << s << endl;   }   int main() {     reshenie();     return 1 - 1 + 2 - 2 + 3 - 3 + 4 - 4 + 5 - 5;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   const int N = 2e5 + 10;   const long long mod = 1e9 + 7;   const long long mod2 = 998244353;   const long long inf = 8e18;   const int LOG = 22;   long long pw(long long a, long long b, long long M) {     return (!b ? 1                : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M                         : pw(a * a % M, b / 2, M)));   }   int v[N], dp[N], cnt[N], cu[N], Fi[N], kol[N], L[N], A[N];   void solve() {     map<int, int> mp;     int n;     cin >> n;     A[0] = -1;     for (int i = 0; i <= n + 1; i++) {       if (i && i <= n) {         cin >> v[i];         A[i] = v[i];       }       dp[i] = cnt[i] = cu[i] = Fi[i] = L[i] = kol[i] = 0;     }     sort(A + 1, A + n + 1);     int ptr = 0;     for (int i = 1; i <= n; i++) {       if (A[i] != A[i - 1]) {         ptr++;       }       mp[A[i]] = ptr;     }     for (int i = n; ~(i - 1); i--) {       v[i] = mp[v[i]];       Fi[v[i]] = i;       cnt[v[i]]++;     }     int ans = 0;     for (int i = 1; i <= n; i++) {       dp[i] = max(dp[L[v[i]]], cu[v[i] - 1]) + 1;       if (cnt[v[i] - 1] == cu[v[i] - 1]) {         dp[i] = max(dp[i], kol[v[i] - 1] + 1);       }       ans = max(ans, dp[i]);       L[v[i]] = i;       ++cu[v[i]];       if (cu[v[i]] == cnt[v[i]]) {         kol[v[i]] = dp[Fi[v[i]]] + cnt[v[i]] - 1;       }     }     cout << n - ans <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int q;     cin >> q;     while (q--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mark[200002];   vector<int> pos[11];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int i, l, t, j, cnt, mx, last;     bool flag;     string s;     cin >> t;     while (t--) {       cin >> l >> s;       for (i = 0; i < 10; i++) {         pos[i].clear();       }       mx = -1;       for (i = 0; i < l; i++) {         pos[s[i] -  0 ].push_back(i);         mx = max(mx, (s[i] -  0 ));         mark[i] = 0;       }       cnt = 0;       for (i = pos[mx].size() - 1; i >= 0; i--) {         mark[pos[mx][i]] = 2;         ++cnt;         last = pos[mx][i];       }       flag = true;       for (i = mx - 1; i >= 0 && flag; i--) {         if (pos[i].size() > 0) {           for (j = pos[i].size() - 1; j >= 0; j--) {             if (last > pos[i][j]) {               mark[pos[i][j]] = 2;               last = pos[i][j];               ++cnt;             } else {               flag = false;             }           }         }       }       last = l;       for (i = mx - 1; i >= 0; i--) {         if (pos[i].size() > 0) {           for (j = pos[i].size() - 1; j >= 0; j--) {             if (mark[pos[i][j]] == 0 && last > pos[i][j]) {               mark[pos[i][j]] = 1;               last = pos[i][j];               ++cnt;             }           }         }       }       if (cnt == l) {         for (i = 0; i < l; i++) {           cout << mark[i];         }         cout <<   n ;       } else {         cout <<  - n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int testCases = 1;     cin >> testCases;     while (testCases--) {       long long int n, m;       cin >> n >> m;       vector<string> array(n);       vector<pair<long long int, long long int>> sol;       for (int i = 0; i < n; ++i) cin >> array[i];       for (int i = 0; i < n - 1; ++i) {         for (int j = 0; j < m - 1; ++j) {           if (array[i][j] ==  0  && array[i][j + 1] ==  0 ) continue;           if (array[i][j] ==  1  && array[i][j + 1] ==  1 ) {             sol.push_back({i + 1, j + 1});             sol.push_back({i + 1, j + 2});             sol.push_back({i + 2, j + 1});             array[i][j] =  0 ;             array[i][j + 1] =  0 ;             if (array[i + 1][j] ==  0 )               array[i + 1][j] =  1 ;             else               array[i + 1][j] =  0 ;           } else if (array[i][j] ==  0  && array[i][j + 1] ==  1 ) {             sol.push_back({i + 1, j + 2});             sol.push_back({i + 2, j + 1});             sol.push_back({i + 2, j + 2});             array[i][j + 1] =  0 ;             if (array[i + 1][j] ==  0 )               array[i + 1][j] =  1 ;             else               array[i + 1][j] =  0 ;             if (array[i + 1][j + 1] ==  0 )               array[i + 1][j + 1] =  1 ;             else               array[i + 1][j + 1] =  0 ;           } else {             sol.push_back({i + 1, j + 1});             sol.push_back({i + 2, j + 1});             sol.push_back({i + 2, j + 2});             array[i][j] =  0 ;             if (array[i + 1][j] ==  0 )               array[i + 1][j] =  1 ;             else               array[i + 1][j] =  0 ;             if (array[i + 1][j + 1] ==  0 )               array[i + 1][j + 1] =  1 ;             else               array[i + 1][j + 1] =  0 ;           }         }       }       for (int j = 0; j < m - 1; ++j) {         if (array[n - 1][j] ==  0  && array[n - 1][j + 1] ==  0 ) continue;         if (array[n - 1][j] ==  0  && array[n - 1][j + 1] ==  1 ) {           sol.push_back({n - 1, j + 1});           sol.push_back({n - 1, j + 2});           sol.push_back({n, j + 2});           sol.push_back({n - 1, j + 2});           sol.push_back({n, j + 1});           sol.push_back({n, j + 2});           sol.push_back({n - 1, j + 1});           sol.push_back({n, j + 1});           sol.push_back({n, j + 2});           array[n - 1][j + 1] =  0 ;         } else if (array[n - 1][j] ==  1  && array[n - 1][j + 1] ==  0 ) {           sol.push_back({n - 1, j + 1});           sol.push_back({n - 1, j + 2});           sol.push_back({n, j + 1});           sol.push_back({n - 1, j + 2});           sol.push_back({n, j + 1});           sol.push_back({n, j + 2});           sol.push_back({n - 1, j + 1});           sol.push_back({n, j + 1});           sol.push_back({n, j + 2});           array[n - 1][j] =  0 ;         } else {           sol.push_back({n - 1, j + 1});           sol.push_back({n - 1, j + 2});           sol.push_back({n, j + 1});           sol.push_back({n - 1, j + 1});           sol.push_back({n - 1, j + 2});           sol.push_back({n, j + 2});           array[n - 1][j] = array[n - 1][j + 1] =  0 ;         }       }       cout << sol.size() / 3 << endl;       for (int i = 0; i < sol.size(); i += 3) {         cout << sol[i].first <<     << sol[i].second <<    ;         cout << sol[i + 1].first <<     << sol[i + 1].second <<    ;         cout << sol[i + 2].first <<     << sol[i + 2].second << endl;         ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   struct M {     int x, y, z;   };   int dis(int x, int y) { return abs(x - y); }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     ;     int n;     cin >> n;     M note[5000];     vector<bool> vs(5000, 0);     for (int i = 1; i <= n; i++) {       cin >> note[i].x >> note[i].y >> note[i].z;     }     for (int i = 1; i <= n; i++) {       int X = INT_MAX, Y = INT_MAX, Z = INT_MAX, now;       if (!vs[i]) {         for (int j = 1; j <= n; j++) {           if (i != j && !vs[j]) {             int disX = dis(note[i].x, note[j].x);             int disY = dis(note[i].y, note[j].y);             int disZ = dis(note[i].z, note[j].z);             if (disX < X) {               now = j;             } else if (disX == X) {               if (disY < Y)                 now = j;               else if (disY == Y) {                 if (disZ < Z) now = j;               }             }             if (now == j) {               X = disX;               Y = disY;               Z = disZ;             }           }         }         cout << i <<     << now << endl;         vs[i] = 1;         vs[now] = 1;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 50;   struct node {     int v, c, l, r, id;     void read() { scanf( %d%d%d%d , &v, &c, &l, &r); }   } car[N];   map<int, int> mp, MP;   inline int clr(int x) { return car[x].c + car[x].l + car[x].r; }   bool cmp(node a, node b) {     int ta = a.c + a.l + a.r;     int tb = b.c + b.l + b.r;     if (ta == tb)       return a.id < b.id;     else       return ta < tb;   }   int n, f[N], pre[N], c[N];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) car[i].read(), car[i].id = i;     sort(car + 1, car + n + 1, cmp);     int ans = 0, num = 0;     for (int i = 1; i <= n;) {       int j = i;       while (j <= n && clr(j) == clr(i)) ++j;       mp.clear();       MP.clear();       mp[0] = 0;       MP[0] = 0;       for (int k = i; k <= j - 1; k++) {         if (mp.count(car[k].l)) {           f[k] = mp[car[k].l] + car[k].v;           pre[k] = MP[car[k].l];         }         if (car[k].r == 0 && f[k] > ans) {           ans = f[k];           num = k;         }         int tmp = car[k].c + car[k].l;         if (!mp.count(tmp) || mp[tmp] < f[k]) {           mp[tmp] = f[k];           MP[tmp] = k;         }       }       i = j;     }     int tmp = num;     ans = 0;     while (tmp) c[++ans] = car[tmp].id, tmp = pre[tmp];     printf( %d n , ans);     for (int i = ans; i >= 1; i--) printf( %d%c , c[i], i == 1 ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; ++i) cin >> a[i];     int m;     cin >> m;     int b[m];     for (int i = 0; i < m; ++i) cin >> b[i];     sort(a + 0, a + n);     sort(b + 0, b + m);     int ans = 0;     for (int i = 0, j = 0; i < n; i++) {       while (j < m) {         if (abs(a[i] - b[j]) <= 1) {           ans++;           j++;           break;         } else if (a[i] - b[j] < 0) {           break;         } else {           j++;         }       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T bigmod(T p, T e, T M) {     long long int 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>   inline T bpow(T p, T e) {     long long int ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p);       p = (p * p);     }     return (T)ret;   }   int toInt(string s) {     int sm;     stringstream ss(s);     ss >> sm;     return sm;   }   int toLlint(string s) {     long long int sm;     stringstream ss(s);     ss >> sm;     return sm;   }   const double eps = 1e-9;   int ts, kk = 1;   int n, b[500005];   pair<int, int> a[500005];   int lt[500005], rt[500005];   int main() {     int t, i, j, k;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d , &k);       b[i] = k;       a[i] = pair<int, int>(k, i);       lt[i] = i - 1;       rt[i] = i + 1;     }     sort(a, a + n);     int x, y;     long long int rs = 0;     int lo, hi;     lo = 0;     hi = n - 1;     for (i = 0; i < n - 2; i++) {       k = a[i].second;       if (k == lo) {         rs += a[i].first;         lo = rt[k];         continue;       }       if (k == hi) {         rs += a[i].first;         hi = lt[k];         continue;       }       x = lt[k];       y = rt[k];       rs += min(b[x], b[y]);       rt[x] = y;       lt[y] = x;     }     cout << rs <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-10;   const int maxn = (1 << 11) + 100;   int dp[maxn][maxn];   vector<int> g[15];   int n, m, k, u, v, mx, cc[maxn];   void init() {     for (int i = 0; i < maxn; ++i) cc[i] = __builtin_popcount(i);   }   int main() {     init();     cin >> n >> m >> k;     for (int i = 0; i < m; ++i) {       cin >> u >> v;       u--;       v--;       g[v].push_back(u);       g[u].push_back(v);     }     mx = (1 << n) - 1;     for (int i = 1; i <= mx; i <<= 1) dp[i][i] = 1;     for (int i = 1; i <= mx; ++i) {       for (int j = i; j; --j &= i) {         if (dp[i][j]) {           for (int k = 0; k < n; ++k) {             if (i & (1 << k)) {               for (int u : g[k]) {                 if (~i & (1 << u)) {                   if (cc[i] == 1)                     v = i | (1 << u);                   else                     v = j & ~(1 << k) | (1 << u);                   if (!(v >> u + 1)) dp[i | (1 << u)][v] += dp[i][j];                 }               }             }           }         }       }     }     long long ans = 0;     for (int i = 0; i < mx; ++i)       if (cc[i] == k) ans += dp[mx][i];     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100001;   const int MOD = 1e9 + 7;   mt19937 rnd(100);   int main(int argc, char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m, x;     vector<vector<int> > a;     cin >> n >> m;     a.assign(n, vector<int>(m));     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> a[i][j];     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> x;         a[i][j] = (int)(x != a[i][j]);       }     for (int i = 0; i < n; i++) {       int sum = 0;       for (int j = 0; j < m; j++) sum += a[i][j];       if (sum % 2) {         cout <<  No ;         return 0;       }     }     for (int i = 0; i < m; i++) {       int sum = 0;       for (int j = 0; j < n; j++) sum += a[j][i];       if (sum % 2) {         cout <<  No ;         return 0;       }     }     cout <<  Yes ;     return 0;   }


#include <bits/stdc++.h>      using namespace std;       typedef long long s64;       const int MaxN = 100000 + 5;       int a[MaxN];       void work() {    int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {     scanf( %d , a + i);     }       sort(a + 1, a + n + 1);        s64 ans = a[n];     for (int i = 2; i <= n; ++i)     ans -= 1LL * a[i] * ((i - 1) - (n - i));         printf( %lld n , ans);    }      int main() {    int T;     scanf( %d , &T);        while (T--) work();        return 0;    }


#include <bits/stdc++.h>   using namespace std;   int edge[300005][5];   int num[300005], res[300005];   void dfs(int x) {     int tmp = 0;     for (int i = 0; i < num[x]; i++)       if (res[x] == res[edge[x][i]]) tmp++;     if (tmp > 1) {       res[x] ^= 1;       for (int i = 0; i < num[x]; i++)         if (res[x] == res[edge[x][i]]) dfs(edge[x][i]);     }   }   int main(int argc, char* argv[]) {     std::ios::sync_with_stdio(false);     int n, m, a, b;     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> a >> b;       edge[a][num[a]++] = b;       edge[b][num[b]++] = a;     }     for (int i = 1; i < n + 1; i++) dfs(i);     for (int i = 1; i < n + 1; i++) cout << res[i];     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a(n), b(n), pa(n), pb(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];       --a[i];       pa[a[i]] = i;     }     for (int i = 0; i < n; ++i) {       cin >> b[i];       --b[i];       pb[b[i]] = i;     }     vector<int> ans(n, 999999);     vector<vector<int>> evi(n), evo(n);     multiset<int> segs;     for (int i = 0; i < n; ++i) {       int wh = (pb[i] - pa[i] + n) % n;       int tol = n - pa[i], tor = pa[i];       evi[wh].push_back(wh);       if ((wh + tor) % n < wh) {         segs.insert(wh - n);         evo[(wh + tor) % n].push_back(wh - n);       } else {         evo[(wh + tor) % n].push_back(wh);       }     }     for (int i = 0; i < n; ++i) {       for (int z : evi[i]) {         segs.insert(z);       }       if (segs.size()) {         auto it = segs.end();         --it;         int x = *it;         ans[i] = min(ans[i], (i - x) % n);       }       for (int z : evo[i]) {         segs.erase(segs.find(z));       }     }     segs.clear();     evi.assign(n, vector<int>());     evo.assign(n, vector<int>());     for (int i = 0; i < n; ++i) {       int wh = (pb[i] - pa[i] + n) % n;       int tol = n - pa[i] - 1, tor = pa[i];       evi[wh].push_back(wh);       if ((wh - tol + n) % n > wh) {         segs.insert(wh + n);         evo[(wh - tol + n) % n].push_back(wh + n);       } else {         evo[(wh - tol + n) % n].push_back(wh);       }     }     for (int i = n - 1; i >= 0; --i) {       for (int z : evi[i]) {         segs.insert(z);       }       if (segs.size()) {         int x = *segs.begin();         ans[i] = min(ans[i], (x - i) % n);       }       for (int z : evo[i]) {         segs.erase(segs.find(z));       }     }     for (int i = 0; i < n; ++i) {       cout << ans[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char st[100005];   int vis[100005];   int main() {     long l, cnt = 0;     scanf( %ld , &l);     scanf( %s , st);     if (l > 26) {       cout << -1;       return 0;     }     for (long i = 0; i < l + 10; i++) vis[i] = 0;     for (long i = 0; i < l; i++) {       if (vis[i] == 0) {         vis[i] = 1;         for (long j = i + 1; j < l; j++) {           if (st[i] == st[j] && vis[j] == 0) {             cnt++;             vis[j] = 1;           }         }       }     }     cout << cnt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<pair<int, int>, int> cntmp;   int n;   struct Point {     int x, y;   } pt[200005];   bool cpx(const Point &a, const Point &b) {     if (a.x != b.x) {       return a.x < b.x;     }     return a.y < b.y;   }   bool cpy(const Point &a, const Point &b) {     if (a.y != b.y) {       return a.y < b.y;     }     return a.x < b.x;   }   long long dcs(long long sx, long long mx) {     return (sx + mx) * (mx - sx + 1) / 2;   }   int main() {     scanf(  %d , &n);     cntmp.clear();     for (int i = 1; i <= n; i++) {       scanf(  %d %d , &pt[i].x, &pt[i].y);       cntmp[make_pair(pt[i].x, pt[i].y)]++;     }     sort(pt + 1, pt + 1 + n, cpx);     long long res = 0;     for (int i = 1; i <= n; i++) {       int j = i;       long long cnt = 0;       while (pt[j].x == pt[i].x && j <= n) {         cnt++;         j++;       }       j--;       if (cnt > 1) {         res += dcs(1, cnt - 1);       }       i = j;     }     sort(pt + 1, pt + 1 + n, cpy);     for (int i = 1; i <= n; i++) {       int j = i;       long long cnt = 0;       while (pt[j].y == pt[i].y && j <= n) {         cnt++;         j++;       }       j--;       if (cnt > 1) {         res += dcs(1, cnt - 1);       }       i = j;     }     for (map<pair<int, int>, int>::iterator it = cntmp.begin(); it != cntmp.end();          it++) {       if (it->second > 1) {         res -= dcs(1, it->second - 1);       }     }     cout << res << endl;   }


#include <bits/stdc++.h>   const int N = 1e6 + 10;   const int M = 9699690;   const int mod = 1e9 + 7, Gi = 332748118;   const int inf = 0x3f3f3f3f;   const long long INF = 0x3f3f3f3f3f3f3f3fll;   const int SIZE = 1 << 21;   char ibuf[SIZE], *iS, *iT;   using std::pair;   template <class T>   void read(T &x) {     x = 0;     int f = 0;     char c = getchar();     while (!isdigit(c)) f |= c ==  - , c = getchar();     while (isdigit(c)) x = x * 10 + c -  0 , c = getchar();     if (f) x = -x;   }   int random(int i) { return rand() % i + 1; }   template <class T>   void ckmax(T &x, T y) {     x = x > y ? x : y;   }   template <class T>   void ckmin(T &x, T y) {     x = x < y ? x : y;   }   template <class T>   T Abs(T x) {     return x > 0 ? x : -x;   }   inline int plus(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }   int qp(int d, int k) {     int f = 1;     while (k) {       if (k & 1) f = (1ll * (f) * (d) % mod);       d = (1ll * (d) * (d) % mod), k >>= 1;     }     return f;   }   int n, m, a[N], b[N], tmp[N];   pair<int, int> p[N];   long long ans;   void msort(int l, int r) {     if (l == r) return;     int mid = l + r >> 1;     msort(l, mid), msort(mid + 1, r);     int lp = l, rp = mid + 1, ct = 0;     while (lp <= mid && rp <= r) {       if (a[lp] <= a[rp])         tmp[++ct] = a[lp++], ans += rp - 1 - mid;       else         tmp[++ct] = a[rp++];     }     while (lp <= mid) tmp[++ct] = a[lp++], ans += rp - 1 - mid;     while (rp <= r) tmp[++ct] = a[rp++];     for (int i = (l); i <= (r); i++) a[i] = tmp[i + 1 - l];   }   int tag[N << 2], mi[N << 2];   void build(int id, int l, int r) {     tag[id] = 0;     if (l == r) {       mi[id] = l - 1;       return;     }     int mid = l + r >> 1;     build(id << 1, l, mid), build(id << 1 | 1, mid + 1, r);     mi[id] = std::min(mi[id << 1], mi[id << 1 | 1]);   }   void pushdown(int id) {     if (tag[id]) {       tag[id << 1] += tag[id];       tag[id << 1 | 1] += tag[id];       mi[id << 1] += tag[id];       mi[id << 1 | 1] += tag[id];       tag[id] = 0;     }   }   void upt(int id, int L, int R, int l, int r, int d) {     if (l <= L && R <= r) {       tag[id] += d;       mi[id] += d;       return;     }     if (l > R || r < L) return;     int Mid = L + R >> 1;     pushdown(id);     upt(id << 1, L, Mid, l, r, d);     upt(id << 1 | 1, Mid + 1, R, l, r, d);     mi[id] = std::min(mi[id << 1], mi[id << 1 | 1]);   }   void work() {     read(n), read(m);     for (int i = (1); i <= (n); i++) read(a[i]), p[i] = std::make_pair(a[i], i);     ans = 0;     msort(1, n);     for (int i = (1); i <= (m); i++) read(b[i]);     std::sort(p + 1, p + 1 + n);     std::sort(b + 1, b + 1 + m);     build(1, 1, n + 1);     int ld = 1, lu = 1;     for (int i = (1); i <= (m); i++) {       while (ld <= n && p[ld].first <= b[i])         upt(1, 1, n + 1, p[ld++].second + 1, n + 1, -1);       while (lu <= n && p[lu].first < b[i])         upt(1, 1, n + 1, 1, p[lu++].second, 1);       ans += mi[1];     }     printf( %lld n , ans);   }   int main() {     int T = 1;     read(T);     while (T--) work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, s, e, t, r;     cin >> n >> m;     vector<pair<int, int> > sections(n + 1);     for (int i = 1; i <= n; i++) sections[i].first = 0x3f3f3f3f;     while (m--) {       cin >> s >> e >> t >> r;       for (int i = s; i <= e; i++) {         if (t < sections[i].first) {           sections[i].first = t;           sections[i].second = r;         }       }     }     int ans = 0;     for (int i = 1; i <= n; i++) {       ans += sections[i].second;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   struct item {     long long val;     int ind;     bool operator<(const item& o) const { return val < o.val; }   };   long long n, m, k, s;   vector<long long> dollar, pound, dday, pday;   vector<item> ditems, pitems;   void readIt() {     cin >> n >> m >> k >> s;     long long minn = 1e7;     long long v, mday = 0;     dollar.push_back(1e16);     pound.push_back(1e16);     pday.push_back(-1);     dday.push_back(-1);     for (int i = 1; i <= n; ++i) {       cin >> v;       if (minn > v) {         minn = v;         mday = i;       }       dollar.push_back(minn);       dday.push_back(mday);     }     minn = 1e7;     mday = 0;     for (int i = 1; i <= n; ++i) {       cin >> v;       if (minn > v) {         mday = i;         minn = v;       }       pound.push_back(minn);       pday.push_back(mday);     }     int type;     for (int i = 0; i < m; ++i) {       cin >> type >> v;       if (type == 1) ditems.push_back({v, i + 1});       if (type == 2) pitems.push_back({v, i + 1});     }     sort(ditems.begin(), ditems.end());     sort(pitems.begin(), pitems.end());   }   bool isPossible(int days, bool verbose) {     long long d = dollar[days];     long long p = pound[days];     int dit = 0, pit = 0;     long long total = 0;     for (int i = 0; i < k; ++i) {       if (dit == ditems.size()) {         if (verbose) cout << pitems[pit].ind <<     << pday[days] <<   n ;         total += pitems[pit++].val * p;         continue;       } else if (pit == pitems.size()) {         if (verbose) cout << ditems[dit].ind <<     << dday[days] <<   n ;         total += ditems[dit++].val * d;         continue;       }       if (ditems[dit].val * d < pitems[pit].val * p) {         if (verbose) cout << ditems[dit].ind <<     << dday[days] <<   n ;         total += ditems[dit++].val * d;       } else {         if (verbose) cout << pitems[pit].ind <<     << pday[days] <<   n ;         total += pitems[pit++].val * p;       }     }     if (total <= s) return true;     return false;   }   int getDay() {     int l = 0, r = n;     while (l < r) {       int m = (l + r + 1) / 2;       if (isPossible(m, false)) {         r = m - 1;       } else {         l = m;       }     }     if (l == n) return -1;     return l + 1;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     readIt();     int day = getDay();     if (day == -1) {       cout << -1 << endl;     } else {       cout << day <<   n ;       isPossible(day, true);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 255;   int parent[N];   int ra[N];   void make_set(int v) {     parent[v] = v;     ra[v] = 0;   }   int find_set(int v) {     if (v == parent[v]) return v;     return parent[v] = find_set(parent[v]);   }   void union_sets(int a, int b) {     a = find_set(a);     b = find_set(b);     if (a != b) {       if (ra[a] < ra[b]) swap(a, b);       parent[b] = a;       if (ra[a] == ra[b]) ++ra[a];     }   }   string s[N];   int main() {     cout.sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> s[i];     }     for (int i = 0; i < n; i++) make_set(i);     for (int j = 0; j < m; j++) make_set(100 + j);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (s[i][j] ==  # ) {           union_sets(i, 100 + j);         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (s[i][j] ==  #  && find_set(i) != find_set(100 + j)) {           cout <<  No ;           return 0;         }         if (s[i][j] !=  #  && find_set(i) == find_set(100 + j)) {           cout <<  No ;           return 0;         }       }     }     cout <<  Yes ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9;   const int mod = inf + 7;   const double eps = 1e-9;   const double pi = acos(-1.0);   int n, a[300300];   int dp[33][300300];   int calc(int bon, int ind) {     if (ind == n) return 0;     int &res = dp[bon][ind];     if (res != -1) return res;     res = a[ind] + calc(min(bon + a[ind] / 10, 31), ind + 1);     for (int i = 1; i <= bon && i <= a[ind]; i++) {       res = min(res, a[ind] - i + calc(bon - i, ind + 1));     }     return res;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , a + i);       a[i] /= 100;     }     memset(dp, -1, sizeof dp);     printf( %d n , calc(0, 0) * 100);     return 0;   }


#include <bits/stdc++.h>   int n, sum;   int a[16];   int b[4][4];   int cmp(const void *a, const void *b) { return *((int *)a) - *((int *)b); }   int dfs(int x, int y, int z) {     int p, q, i, j;     if (x == n && y == 0) return 1;     p = x;     q = y + 1;     if (q == n) {       p = x + 1;       q = 0;     }     for (i = 0; i < n * n; i++) {       if ((z >> i) & 1) continue;       if (x == n - 1) {         int s = 0;         for (j = 0; j < n - 1; j++) s += b[j][y];         if (s + a[i] != sum) continue;       }       if (y == n - 1) {         int s = 0;         for (j = 0; j < n - 1; j++) s += b[x][j];         if (s + a[i] != sum) continue;       }       if (x == n - 1 && y == 0) {         int s = 0;         for (j = 0; j < n - 1; j++) s += b[j][n - j - 1];         if (s + a[i] != sum) continue;       }       if (x == n - 1 && y == n - 1) {         int s = 0;         for (j = 0; j < n - 1; j++) s += b[j][j];         if (s + a[i] != sum) continue;       }       b[x][y] = a[i];       if (dfs(p, q, z | (1 << i)) == 1) return 1;       for (j = i + 1; j < n * n; j++) {         if (a[i] != a[j]) break;       }       i = j - 1;     }     return -1;   }   int main() {     int i, j;     scanf( %d , &n);     for (i = 0; i < n * n; i++) {       scanf( %d , &a[i]);       sum += a[i];     }     sum /= n;     printf( %d n , sum);     qsort(a, n * n, sizeof(int), cmp);     dfs(0, 0, 0);     for (i = 0; i < n; i++) {       for (j = 0; j < n; j++) {         if (j > 0) putchar(   );         printf( %d , b[i][j]);       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long INFF = 1e18;   const double pi = acos(-1.0);   const double inf = 1e18;   const double eps = 1e-9;   const long long mod = 1e9 + 7;   const int maxmat = 10;   const unsigned long long BASE = 133333331;   inline void RI(int &x) {     char c;     while ((c = getchar()) <  0  || c >  9 )       ;     x = c -  0 ;     while ((c = getchar()) >=  0  && c <=  9 ) x = (x << 3) + (x << 1) + c -  0 ;   }   const int dy[3] = {-1, 0, 1};   char maze[3][105];   bool vis[3][105][105];   int sx, sy;   int N, k;   struct Node {     int x, y, t;   };   bool valid(int x, int y, int t) {     return (x >= 0 && x < 3 && y >= 0 && y < N &&             (maze[x][y + 2 * t] ==  .  || maze[x][y + 2 * t] == 0));   }   bool bfs(int x, int y) {     queue<Node> q;     q.push((Node){x, y, 0});     while (!q.empty()) {       Node tmp = q.front();       q.pop();       if (tmp.y == N - 1) return true;       if (maze[tmp.x][tmp.y + 1 + 2 * tmp.t] >=  A  &&           maze[tmp.x][tmp.y + 1 + 2 * tmp.t] <=  Z )         continue;       for (int k = 0; k < 3; k++) {         int tmpx = tmp.x + dy[k];         int tmpy = tmp.y + 1;         if (valid(tmpx, tmpy, tmp.t) && valid(tmpx, tmpy, tmp.t + 1) &&             !vis[tmpx][tmpy][tmp.t + 1]) {           vis[tmpx][tmpy][tmp.t + 1] = true;           q.push((Node){tmpx, tmpy, tmp.t + 1});         }       }     }     return false;   }   int main(int argc, char const *argv[]) {     int T;     cin >> T;     while (T--) {       scanf( %d%d , &N, &k);       memset(maze, 0, sizeof(maze));       memset(vis, false, sizeof(vis));       for (int i = 0; i < 3; i++) {         char s[105];         scanf( %s , s);         for (int j = 0; j < N; j++) {           maze[i][j] = s[j];           if (maze[i][j] ==  s ) sx = i, sy = j;         }       }       cout << (bfs(sx, sy) ?  YES  :  NO ) << endl;     }     return 0;   }


# include<bits/stdc++.h>   using namespace std;      int main()   {     int a,b,c=0;set<int>s;     cin>>a>>b;        while(a)     {       c++;       s.insert(a%b);       a/=b;     }     if(c==s.size())       cout<< YES <<endl;       else       cout<< NO <<endl;        return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double E = 2.71828182845904523536;   const long double pi = acos(-1);   const double eps = 1e-9;   const long long mod = 1e9 + 7;   const long long inf = 1LL << 30;   const int N = 500500;   vector<int> adj[N];   vector<int> v;   int dep[N];   void DFS(int u, int p) {     dep[u] = 1 + dep[p];     for (auto v : adj[u])       if (v != p) DFS(v, u);     if (adj[u].size() == 1) v.push_back(dep[u]);   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d%d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);     }     dep[1] = 0;     int ans = 0;     for (auto u : adj[1]) {       v.clear();       DFS(u, 1);       sort(v.begin(), v.end());       for (int i = 1; i < v.size(); i++)         if (v[i] <= v[i - 1]) v[i] = v[i - 1] + 1;       ans = max(ans, v.back());     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int c[500005], n, nm[500005], g[321];   vector<int> v[55];   char s[500005], p[500005];   inline int qr() {     int x = 0, w = 1;     char a = 0;     while (a <  0  || a >  9 ) {       if (a ==  - ) w = -1;       a = getchar();     }     while (a <=  9  && a >=  0 ) {       x = (x << 3) + (x << 1) + (a ^ 48);       a = getchar();     }     return x * w;   }   inline int lowbit(int x) { return x & -x; }   void add(int x, int d) {     while (x <= n) {       c[x] += d;       x += lowbit(x);     }   }   long long qu(int x) {     long long ans = 0;     while (x) {       ans += c[x];       x -= lowbit(x);     }     return ans;   }   int main() {     n = qr();     cin >> s + 1;     for (register int i = 1; i <= n; i++) p[i] = s[n - i + 1];     for (register int i = 1; i <= n; i++) v[p[i] -  a  + 1].push_back(i);     for (register int i = 1; i <= n; i++) {       int ch = s[i] -  a  + 1;       nm[i] = v[ch][g[ch]];       ++g[ch];     }     long long ans = 0;     for (register int i = 1; i <= n; i++) {       add(nm[i], 1);       ans += qu(n) - qu(nm[i]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   const int maxn = 500;   int v[maxn + 5];   long long vv[maxn + 5];   int ans[maxn + 5];   int r[maxn + 5];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &v[i]);     scanf( %d , &k);     for (int i = 0; i < k; i++) scanf( %I64d , &vv[i]);     memset(ans, -1, sizeof ans);     bool ok = true;     long long sum = 0;     int vvi = 0;     for (int i = 0; i < n; i++) {       sum += v[i];       if (sum == vv[vvi]) {         sum = 0;         r[vvi] = i;         vvi++;       } else if (sum > vv[vvi]) {         ok = false;         break;       }     }     ok = ok && (vvi == k);     if (!ok) {       printf( NO n );       return 0;     }     vvi = 0;     int prev = -1;     int maxval = -1;     for (int i = 0; i < n; i++) {       if (i > r[vvi]) {         if (r[vvi] - prev == 1) {           ans[vvi] = r[vvi];         }         if (ans[vvi] == -1) {           ok = false;           break;         }         prev = r[vvi];         maxval = -1;         vvi++;       }       if (i - 1 > prev && v[i] > v[i - 1] && v[i] > maxval) {         maxval = v[i];         ans[vvi] = i;       }       if (i + 1 <= r[vvi] && v[i] > v[i + 1] && v[i] > maxval) {         maxval = v[i];         ans[vvi] = i;       }     }     if (ans[vvi] == -1) {       if (r[vvi] - prev == 1) {         ans[vvi] = r[vvi];       } else {         ok = false;       }     }     ok = ok && (vvi == k - 1);     if (!ok) {       printf( NO n );       return 0;     }     printf( YES n );     prev = -1;     for (int i = 0; i < k; i++) {       int idx = ans[i];       int pidx = ans[i] - prev + i;       if (idx - 1 > prev && v[idx] > v[idx - 1]) {         for (int j = 0; j < ans[i] - prev - 1; j++) {           printf( %d L n , pidx--);         }         for (int j = 0; j < r[i] - ans[i]; j++) {           printf( %d R n , i + 1);         }       } else {         for (int j = 0; j < r[i] - ans[i]; j++) {           printf( %d R n , pidx);         }         for (int j = 0; j < ans[i] - prev - 1; j++) {           printf( %d L n , pidx--);         }       }       prev = r[i];     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   typedef long long int ll;   void solve(void);   int main(){   int t;   cin>>t;   while(t--){    solve();   }         return 0;}   void solve(){   ll n,i;   cin>>n;   ll a[n+1];   for(i=1;i<=n;i++)   {cin>>a[i];      }   cout<<3*n<<endl;   for(i=1;i<n;i++)   {      cout<< 1 <<   <<i<<   <<i+1<<endl;       cout<< 2 <<   <<i<<   <<i+1<<endl;       cout<< 1 <<   <<i<<   <<i+1<<endl;       cout<< 2 <<   <<i<<   <<i+1<<endl;       cout<< 1 <<   <<i<<   <<i+1<<endl;   cout<< 2 <<   <<i<<   <<i+1<<endl;   i++;      }      }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   enum { MOD0 = 1000000000, MOD7 = 1000000007, MOD9 = 1000000009 };   int a[200000];   long long ft[200000];   int t[800000];   int n;   void inc(int index, int val) {     while (index < n) {       ft[index] += val;       index |= index + 1;     }   }   long long sum(int r) {     long long ret = 0;     for (; r >= 0; r &= r + 1, --r) {       ret += ft[r];     }     return ret;   }   long long sum(int l, int r) { return sum(r) - (l ? sum(l - 1) : 0); }   void build(int x = 0, int L = 0, int R = n - 1) {     if (L == R) {       t[x] = a[L];       return;     }     int M = (L + R) / 2;     build(2 * x + 1, L, M);     build(2 * x + 2, M + 1, R);     t[x] = max(t[2 * x + 1], t[2 * x + 2]);   }   void upd(int index, int val, int x = 0, int L = 0, int R = n - 1) {     if (L == R) {       t[x] = val;       return;     }     int M = (L + R) / 2;     if (index <= M) {       upd(index, val, 2 * x + 1, L, M);     } else {       upd(index, val, 2 * x + 2, M + 1, R);     }     t[x] = max(t[2 * x + 1], t[2 * x + 2]);   }   int get(long long val, int from, int x = 0, int L = 0, int R = n - 1) {     if (t[x] < val || R < from) {       return n;     } else if (L == R) {       return L;     }     int M = (L + R) / 2;     if (M < from) {       return get(val, from, 2 * x + 2, M + 1, R);     } else if (t[2 * x + 1] >= val) {       int s = get(val, from, 2 * x + 1, L, M);       if (s < n) {         return s;       }     }     return get(val, from, 2 * x + 2, M + 1, R);   }   int get_ans() {     long long cur = 0;     int index = 0;     if (a[0] == 0) {       return 1;     }     while (index < n) {       cur += a[index];       index = get(cur, index + 1);       cur = sum(index - 1);       if (a[index] == cur) {         break;       }     }     return index >= n ? -1 : index + 1;   }   int main() {     int q;     cin >> n >> q;     for (int i = 0; i < n; ++i) {       cin >> a[i];       inc(i, a[i]);     }     build();     while (q--) {       int p, x;       cin >> p >> x;       --p;       inc(p, x - a[p]);       a[p] = x;       upd(p, x);       cout << get_ans() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, min, max;     std::cin >> n >> m >> min >> max;     int a[m];     int maxHit = 0;     int minHit = 0;     for (int i = 0; i < m; ++i) {       std::cin >> a[i];       if (a[i] == min) {         minHit = 1;       } else if (a[i] == max) {         maxHit = 1;       }       if (a[i] > max || a[i] < min) {         std::cout <<  Incorrect ;         return 0;       }     }     if (minHit != 1 && maxHit != 1) {       if (n - m < 2) {         std::cout <<  Incorrect ;       } else {         std::cout <<  Correct ;       }     } else if (minHit != 1 || maxHit != 1) {       if (n - m < 1) {         std::cout <<  Incorrect ;       } else {         std::cout <<  Correct ;       }     } else {       std::cout <<  Correct ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     int x = abs(a - b);     if (x % 2 == 0) {       x /= 2;       cout << x * (x + 1) << endl;     } else {       x /= 2;       cout << x * (x + 1) + x + 1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int Mx = 1e5 + 5;   const long long inf = 1e9 + 1;   struct Point {     long long x, y;   } point[Mx];   int n;   long long ans, hmax[Mx], emax[Mx], hmin[Mx], emin[Mx];   template <class T>   bool cmp(T a, T b) {     return a.x < b.x;   }   bool judge(long long mid) {     int l = 1, r = 1;     long long Max, Min;     while (r <= n) {       while (point[r].x - point[l].x <= mid && r <= n) r++;       Max = -inf;       Min = inf;       if (l != 1) {         Max = max(Max, hmax[l - 1]);         Min = min(Min, hmin[l - 1]);       }       if (r <= n) {         Max = max(Max, emax[r]);         Min = min(Min, emin[r]);       }       if (Max == -inf) return 1;       if (Max - Min <= mid) return 1;       l++;     }     return 0;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       long long x, y;       scanf( %lld%lld , &x, &y);       point[i].x = x + y;       point[i].y = x - y;     }     sort(point + 1, point + 1 + n, cmp<Point>);     hmin[0] = inf;     hmax[0] = -inf;     emin[n + 1] = inf;     emax[n + 1] = -inf;     for (int i = 1; i <= n; i++) {       hmax[i] = max(hmax[i - 1], point[i].y);       hmin[i] = min(hmin[i - 1], point[i].y);     }     for (int i = n; i >= 1; i--) {       emax[i] = max(emax[i + 1], point[i].y);       emin[i] = min(emin[i + 1], point[i].y);     }     long long l = 0, r = 2e9;     while (l <= r) {       long long mid = (l + r) >> 1;       if (judge(mid)) {         r = mid - 1;         ans = mid;       } else {         l = mid + 1;       }     }     printf( %lf , 1.0 * ans / 2);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     if (n == 1 or n > 26) {       cout <<  Yes n ;       return 0;     }     set<char> s;     char c;     for (int i = 0; i < n; i++) {       cin >> c;       if (s.find(c) != s.end()) {         cout <<  Yes n ;         return 0;       }       s.insert(c);     }     cout <<  No n ;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, x, y, k;     cin >> n >> m;     long long a[n], b[m];     cin >> x >> k >> y;     for (long long i = 0; i < n; i++) cin >> a[i];     for (long long i = 0; i < m; i++) cin >> b[i];     vector<long long> adj[n];     long long z[n];     long long ind = 0;     for (long long i = 0; i < n; i++) {       if (ind >= m) {         adj[ind].push_back(a[i]);         continue;       }       if (b[ind] == a[i]) {         if (ind - 1 >= 0)           z[ind] = max(b[ind], b[ind - 1]);         else           z[ind] = b[ind];         ind++;       } else         adj[ind].push_back(a[i]);     }     if (ind < m - 1) {       cout <<  -1 n ;       return 0;     }     z[ind] = b[m - 1];     ind = 0;     long long ans = 0;     int f = 0;     for (auto &it : adj) {       long long itl = it.size();       if (itl == 0) {         ind++;         continue;       }       long long mxv = -1;       for (auto &it2 : it) mxv = max(mxv, it2);       if (itl < k) {         if (mxv < z[ind])           ans += itl * y;         else {           f = 1;           break;         }       } else {         if (x >= y * k) {           if (mxv < z[ind])             ans += itl * y;           else {             ans = ans + (itl - k) * y + x;           }         } else {           ans += (itl / k) * x + (itl % k) * y;         }       }       ind++;     }     if (f == 1)       cout <<  -1 n ;     else       cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename S, typename T>   ostream& operator<<(ostream& out, pair<S, T> const& p) {     out <<  (  << p.first <<  ,   << p.second <<  ) ;     return out;   }   template <typename T>   ostream& operator<<(ostream& out, vector<T> const& v) {     long long l = v.size();     for (long long i = 0; i < l - 1; i++) out << v[i] <<    ;     if (l > 0) out << v[l - 1];     return out;   }   template <typename T>   void trace(const char* name, T&& arg1) {     cout << name <<   :   << arg1 <<   n ;   }   template <typename T, typename... Args>   void trace(const char* names, T&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     trace(comma + 1, args...);   }   const int N = 202;   const int K = 1010;   const int MOD = 1e9 + 7;   int dp[N][N][K];   int n, kk, arr[N];   int pre[N];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> kk;     for (int i = 1; i <= n; i++) {       cin >> arr[i];     }     sort(arr + 1, arr + 1 + n);     dp[0][0][0] = 1;     for (int i = 0; i < n; i++) {       for (int j = 0; j <= n; j++) {         for (int k = 0; k < K; k++) {           int nk = k + (j) * (arr[i + 1] - arr[i]);           if (nk <= kk) {             dp[i + 1][j][nk] =                 (0LL + (1LL * dp[i][j][k] * (j + 1)) % MOD + dp[i + 1][j][nk]) %                 MOD;             dp[i + 1][j + 1][nk] =                 (0LL + dp[i + 1][j + 1][nk] + dp[i][j][k]) % MOD;             if (j)               dp[i + 1][j - 1][nk] =                   (0LL + dp[i + 1][j - 1][nk] + 1LL * dp[i][j][k] * j) % MOD;           }         }       }     }     int ans = 0;     for (int i = 0; i <= kk; i++) {       ans = (ans + dp[n][0][i]) % MOD;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   long long c(int a, int b) {     long long res = 1;     for (int i = 1; i <= b; ++i) {       res = res * (a + 1 - i) / i;     }     return res;   }   int main() {     scanf( %d%d , &n, &k);     long long ans = 1;     for (int i = 1; i <= k; ++i) {       if (i == 2) {         ans += c(n, 2);       }       if (i == 3) {         ans += 2 * c(n, 3);       }       if (i == 4) {         ans += 9 * c(n, 4);       }     }     printf( %lld n , ans);     return 0;   }