#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 10;   struct Segment {     int left, right;   } seg[N / 2 + 5];   map<int, bool> m;   int a[N];   int n, ans;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     m.clear();     ans = 1;     seg[ans].left = 1;     m[a[1]] = true;     int i = 2;     while (i <= n) {       if (m[a[i]]) {         seg[ans].right = i;         if (i == n) break;         ans++;         i++;         seg[ans].left = i;         m.clear();       }       m[a[i]] = true;       i++;     }     if (i > n) {       if (ans == 1) {         printf( -1 n );         return 0;       }       ans--;       seg[ans].right = n;     }     printf( %d n , ans);     for (int i = 1; i <= ans; i++) printf( %d %d n , seg[i].left, seg[i].right);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string CHECK(long long n) {     long long t, temp = n, zero = 0, ans = 0, flag = 0;     while (temp != 0) {       t = temp % 10;       if (t == 0 && flag == 0)         zero++;       else {         flag = 1;         ans *= 10;         ans += t;       }       temp /= 10;     }     if (zero != 0) {       while (zero != 0) {         ans *= 10;         zero--;       }     }     if (n == ans)       return  YES ;     else       return  NO ;   }   int main() {     long long n;     cin >> n;     cout << CHECK(n) << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int ans = 0;     string str;     cin >> str;     for (int s = 2; s <= n; s++) {       for (int i = 0; i < n - s + 1; i++) {         int u = 0, d = 0, l = 0, r = 0;         for (int j = i; j < i + s; j++) {           if (str[j] ==  U )             u++;           else if (str[j] ==  D )             d++;           else if (str[j] ==  L )             l++;           else if (str[j] ==  R )             r++;         }         if (u == d && l == r) {           ans++;         }       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long a[1000050];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     long long rec = 0;     long long num = a[0];     long long have = 0;     long long can = 0;     for (int i = 0; i < n; i++) {       if (num == a[i]) have++;       if (num != a[i]) {         can++;         if (a[i + 1] != a[i]) {           num = a[i];           rec += min(have, can);           have = have + can - min(have, can);           can = 0;         }       }     }     rec = n - have;     cout << rec << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       vector<vector<int>> p(n + 1, vector<int>(m + 1, 0));       vector<int> ma(m + 1);       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++) {           cin >> p[i][j];           if (p[i][j] > p[ma[j]][j]) ma[j] = i;         }       set<int> s;       for (int i = 1; i <= m; i++) s.insert(ma[i]);       if (s.size() == m) {         int ans = 0;         for (int i = 1; i <= m; i++) {           for (int j = 1; j <= m; j++) {             if (i == j) continue;             int tem = 0;             for (int k = 1; k <= n; k++) {               tem = max(tem, min(p[k][i], p[k][j]));             }             for (int k = 1; k <= m; k++) {               if (k == i || k == j) continue;               tem = min(tem, p[ma[k]][k]);             }             ans = max(ans, tem);           }         }         cout << ans <<   n ;       } else {         int ans = 1e9;         for (int i = 1; i <= m; i++) {           ans = min(ans, p[ma[i]][i]);         }         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   bool dfs(int x, const vector<vector<int>> &g, vector<char> &used,            vector<int> &match, vector<int> &rev_match) {     if (used[x]) return false;     used[x] = 1;     for (int dest : g[x]) {       if (match[dest] == -1 || dfs(match[dest], g, used, match, rev_match)) {         match[dest] = x;         rev_match[x] = dest;         return true;       }     }     return false;   }   int matching(const vector<vector<int>> &g, int left, int right) {     vector<char> used(left);     vector<int> rev_match(left, -1), match(right, -1);     while (true) {       bool ok = false;       fill(used.begin(), used.end(), 0);       for (int i = 0; i < left; i++)         if (rev_match[i] == -1 && !used[i])           ok |= dfs(i, g, used, match, rev_match);       if (!ok) break;     }     return left - count(rev_match.begin(), rev_match.end(), -1);   }   void solve(istream &cin = std::cin, ostream &cout = std::cout) {     int n, m;     cin >> n >> m;     const int inf = int(1e9) + 1;     vector<vector<int>> g(n, vector<int>(n, inf));     for (int i = 0; i < n; i++) g[i][i] = 0;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       --u, --v;       g[u][v] = g[v][u] = 1;     }     for (int k = 0; k < n; k++)       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]);     int s, b, k, h;     cin >> s >> b >> k >> h;     vector<int> pos_s(s), fuel(s), atk(s);     vector<int> pos_b(b), def(b);     for (int i = 0; i < s; i++) {       cin >> pos_s[i] >> atk[i] >> fuel[i];       --pos_s[i];     }     for (int i = 0; i < b; i++) {       cin >> pos_b[i] >> def[i];       --pos_b[i];     }     vector<vector<int>> graph(s);     for (int i = 0; i < s; i++)       for (int j = 0; j < b; j++)         if (g[pos_s[i]][pos_b[j]] <= fuel[i] && def[j] <= atk[i])           graph[i].push_back(j);     const int len = matching(graph, s, b);     ll ans = min((ll)len * k, (ll)s * h);     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout << fixed;     cout << setprecision(20);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = (long long)(1000000007);   const double eps = 1e-9;   const double pi = 3.1415926535897932384626;   long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); }   bool isprime(int n) {     if (n == 1) return false;     for (long long i = 2; i <= sqrt(n); i++) {       if (n % i == 0) return false;     }     return true;   }   int modExp(int b, int e, int m) {     int x = 1;     while (e != 0) {       if (e % 2 == 1) x = (x * b) % m;       b = (b * b) % m;       e = e / 2;     }     return x;   }   unsigned long long fact(unsigned long long n) {     unsigned long long ret = 1;     for (long long i = 1; i <= n; i++) {       ret *= i;     }     return ret;   }   int ans = 0;   int main() {     long long n;     cin >> n;     vector<pair<long long, long long> > vp;     for (long long i = 0; i <= n - 1; i++) {       int a, b;       cin >> a >> b;       vp.push_back(make_pair(a, b));     }     sort(vp.begin(), vp.end());     ans = vp[0].second;     for (long long i = 1; i <= n - 1; i++) {       if (vp[i].second >= ans)         ans = vp[i].second;       else         ans = vp[i].first;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int mi[n];     for (int i = (0); i < (n); i++) mi[i] = 1e9;     int t = 1;     int z[1000];     do {       t *= 2;       vector<int> v;       int x = 0;       while (x < n) {         int f = 0;         for (int i = (0); i < (t / 2); i++) {           if (x + i < n)             v.push_back(x + i);           else             break;         }         x += t;       }       cout << v.size() <<   n ;       ;       set<int> temp;       for (int i = (0); i < (v.size()); i++) {         temp.insert(v[i]);         cout << v[i] + 1 <<    ;       }       cout <<   n ;       fflush(stdout);       for (int i = (0); i < (n); i++) cin >> z[i];       for (int i = (0); i < (n); i++) {         if (temp.find(i) == temp.end()) mi[i] = min(mi[i], z[i]);       }       v.resize(0);       x = 0;       temp.clear();       while (x < n) {         int f = 0;         for (int i = (0); i < (t / 2); i++) {           if (x + i + t / 2 < n)             v.push_back(x + i + t / 2);           else             break;         }         x += t;       }       cout << v.size() <<   n ;       ;       for (int i = (0); i < (v.size()); i++) {         temp.insert(v[i]);         cout << v[i] + 1 <<    ;       }       cout <<   n ;       fflush(stdout);       for (int i = (0); i < (n); i++) cin >> z[i];       for (int i = (0); i < (n); i++) {         if (temp.find(i) == temp.end()) mi[i] = min(mi[i], z[i]);       }     } while (t < n);     cout <<  -1 n ;     fflush(stdout);     for (int i = (0); i < (n); i++) cout << mi[i] <<    ;     cout <<   n ;     fflush(stdout);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int sze = n * 2;       int arr[sze];       for (int i = 0; i < sze; i++) cin >> arr[i];       sort(arr, arr + sze);       cout << arr[n] - arr[n - 1] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long is_query = -(1LL << 62);   struct line {     long long m, b;     mutable function<const line*()> succ;     bool operator<(const line& rhs) const {       if (rhs.b != is_query) return m < rhs.m;       const line* s = succ();       if (!s) return 0;       long long x = rhs.m;       return b - s->b < (s->m - m) * x;     }   };   struct dynamic_hull : public multiset<line> {     const long long inf = LLONG_MAX;     bool bad(iterator y) {       auto z = next(y);       if (y == begin()) {         if (z == end()) return 0;         return y->m == z->m && y->b <= z->b;       }       auto x = prev(y);       if (z == end()) return y->m == x->m && y->b <= x->b;       long long v1 = (x->b - y->b);       if (y->m == x->m)         v1 = x->b > y->b ? inf : -inf;       else         v1 /= (y->m - x->m);       long long v2 = (y->b - z->b);       if (z->m == y->m)         v2 = y->b > z->b ? inf : -inf;       else         v2 /= (z->m - y->m);       return v1 >= v2;     }     void insert_line(long long m, long long b) {       auto y = insert({m, b});       y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };       if (bad(y)) {         erase(y);         return;       }       while (next(y) != end() && bad(next(y))) erase(next(y));       while (y != begin() && bad(prev(y))) erase(prev(y));     }     __int128 eval(long long x) {       auto l = *lower_bound(line{x, is_query});       return (__int128)l.m * x + l.b;     }   };   int main() {     int n;     long long s;     cin >> n >> s;     vector<pair<long long, long long>> buildings(n);     for (int i = 0; i < n; ++i) {       cin >> buildings[i].second >> buildings[i].first;     }     sort(buildings.begin(), buildings.end());     assert(buildings[0].first == 0);     vector<long long> min_time(n + 1, s + 1);     vector<long long> max_cookies(n + 1, 0);     min_time[0] = 0;     max_cookies[0] = 0;     dynamic_hull dp_opt;     dp_opt.insert_line(buildings[0].second, 0);     for (int i = 1; i < n; ++i) {       long long left = min_time[i - 1];       long long right = s + 50;       while (left + 1 < right) {         long long mid = (left + right) / 2;         if (dp_opt.eval(mid) >= buildings[i].first) {           right = mid;         } else {           left = mid;         }       }       if (dp_opt.eval(left) - buildings[i].first < 0) {         ++left;       }       min_time[i] = left;       max_cookies[i] = dp_opt.eval(left) - buildings[i].first;       dp_opt.insert_line(           buildings[i].second,           max_cookies[i] - 1LL * buildings[i].second * min_time[i]);     }     long long ans = s;     for (int i = 0; i < n; ++i) {       ans = min(ans,                 min_time[i] + (s - max_cookies[i] - 1) / buildings[i].second + 1);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void read(T& x) {     bool fu = 0;     char c;     for (c = getchar(); c <= 32; c = getchar())       ;     if (c ==  - ) fu = 1, c = getchar();     for (x = 0; c > 32; c = getchar()) x = x * 10 + c -  0 ;     if (fu) x = -x;   };   template <class T>   inline void read(T& x, T& y) {     read(x);     read(y);   }   template <class T>   inline void read(T& x, T& y, T& z) {     read(x);     read(y);     read(z);   }   inline char getc() {     char c;     for (c = getchar(); c <= 32; c = getchar())       ;     return c;   }   const int N = 10010;   const double pi = 2 * acos(0);   const double eps = 1e-9;   int w, h, n, m, i, j, st, g;   double L0, R0, R;   long long x[N], y[N];   double d[1010][1010], an[1010][1010];   int p[N];   double c[N];   double D(int i, int j) {     return sqrt((1LL * (x[i] - x[j]) * (x[i] - x[j])) +                 (1LL * (y[i] - y[j]) * (y[i] - y[j])));   }   double geta(int i, int j) { return atan2(y[j] - y[i], x[j] - x[i]); }   bool same(double a, double b) { return abs(a - b) <= eps; }   double ans;   void add(double L, double R) {     if (R > pi) {       add(L, pi);       add(-pi, R - 2 * pi);     } else if (L < -pi) {       add(L + 2 * pi, pi);       add(-pi, R);     } else       c[++g] = L - 1e-15, c[++g] = R + 1e-15;   }   bool cmp(int x, int y) { return c[x] < c[y]; }   bool ok(double R) {     int i, j, k;     double tt;     g = 0;     c[++g] = -pi;     c[++g] = pi;     if (x[st] < R) {       tt = acos(x[st] / R);       for (k = 1; k <= 2; k++) add(-pi - tt, -pi + tt);     }     if (w - x[st] < R) {       tt = acos((w - x[st]) / R);       for (k = 1; k <= 2; k++) add(-tt, tt);     }     if (y[st] < R) {       tt = acos(y[st] / R);       for (k = 1; k <= 2; k++) add(-pi / 2 - tt, -pi / 2 + tt);     }     if (h - y[st] < R) {       tt = acos((h - y[st]) / R);       for (k = 1; k <= 2; k++) add(pi / 2 - tt, pi / 2 + tt);     }     for (i = 1; i <= n; i++)       if (i != st && d[st][i] < 2 * R && d[st][i] > eps) {         tt = acos(d[st][i] / 2 / R);         add(an[st][i] - tt, an[st][i] + tt);       }     for (i = 1; i <= g; i++) p[i] = i;     sort(p + 1, p + 1 + g, cmp);     int s = 0;     for (i = 1; i <= g; i++)       if ((s += (p[i] & 1 ? 1 : -1)) < 3 && i + 1 <= g &&           c[p[i + 1]] - c[p[i]] > eps)         return 1;     return 0;   }   int main() {     read(w, h, n);     for (i = 1; i <= n; i++) cin >> x[i] >> y[i];     for (i = 1; i <= n; i++) {       int t = rand() % i + 1;       swap(x[i], x[t]);       swap(y[i], y[t]);     }     for (i = 1; i <= n; i++)       for (j = 1; j <= n; j++) d[i][j] = D(i, j), an[i][j] = geta(i, j);     for (st = 1; st <= n; st++)       if (ok(R)) {         L0 = R;         R0 = 2e6;         while (L0 + eps < R0) {           R = (L0 + R0) / 2;           if (ok(R))             L0 = R;           else             R0 = R;         }       }     cout << fixed << setprecision(10) << R << endl;     scanf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 1e5 + 1;   const long long MOD = 1e9 + 7;   const long long INF = 1e9;   void solve(long long a, long long b) {     if (a == 0 and b == 0) {       cout <<  0  << endl;     } else if (a == b) {       cout <<  1  << endl;     } else if (abs(a - b) % 2 == 1) {       cout <<  -1  << endl;     } else {       cout <<  2  << endl;     }     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int tc = 1;     cin >> tc;     for (int t = 1; t <= tc; t++) {       long long a, b;       cin >> a >> b;       solve(a, b);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a[1001], i, n, sum = 0, max = 0;     int b[1001] = {0};     cin >> n;     for (i = 0; i < n; i++) cin >> a[i];     for (i = 0; i < n; i++) {       b[a[i]]++;     }     for (i = 0; i < 1001; i++) {       if (b[i] > max) max = b[i];       if (b[i]) sum++;     }     cout << max <<    ;     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int tt;     cin >> tt;     while (tt--) {       int a, b, c, saveb = 0;       string st, md, en;       st = md = en =   ;       cin >> a >> b >> c;       bool ub = false;       while (b) {         if (!ub) {           md +=  01 ;           b--;           ub = true;         } else {           if (b == 1 && md[md.size() - 1] ==  1 ) {             saveb = 1;             b--;           } else {             md += md[md.size() - 1] ^ 1;             b--;           }         }       }       if (a) {         if (!md.size())           st +=  00 ;         else           st +=  0 ;         a--;       }       for (int i = 1; i <= a; i++) {         st +=  0 ;       }       if (c) {         if (!md.size())           en +=  11 ;         else           en +=  1 ;         c--;       }       for (int i = 1; i <= c; i++) {         en +=  1 ;       }       if (saveb > 0) cout << 1;       cout << st << md << en <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     cout << s;     reverse(s.begin(), s.end());     cout << s;   }


#include <bits/stdc++.h>   using namespace std;   void CountTriangles(vector<int> A) {     int n = A.size();     sort(A.begin(), A.end());     int count = 0;     for (int i = n - 1; i >= 1; i--) {       int l = 0, r = i - 1;       while (l < r) {         if (A[l] + A[r] > A[i]) {           count += r - l;           r--;         } else           l++;       }     }     cout <<  No of possible solutions:   << count;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int a, b;     cin >> a >> b;     if (a >= b) {       cout << (b - 1) / 2 << endl;     } else {       long long int c = (2 * a - b + 1) / 2;       cout << max(0LL, c);     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using namespace std;   int n, b[110], l, r, c, t, q[110], m;   int main() {     cin >> m >> n;     for (int i = 1; i <= m; i++) b[i] = 1 << 30;     for (int i = 0; i < n; i++) {       cin >> l >> r >> t >> c;       for (int j = l; j <= r; j++) {         if (t < b[j]) b[j] = t, q[j] = c;       }     }     int s = 0;     for (int i = 1; i <= m; i++) {       s += q[i];     }     cout << s;     return 0;   }


#include <bits/stdc++.h>   #define pb push_back   #define mp make_pair   #define sz(a) ((int)a.size())   #define re return   #define all(a) a.begin(),a.end()   #define int long long   #define ld long double   #define rept(i,a,b) for(int i=(a);i<(b);i++)   #define rep(i,a) rept(i,0,a)   using namespace std;   void fail(string s){ cout<<s;exit(0); }   void fail(int s){ cout<<s;exit(0); }   void chmax(ld &a,ld b,pair<int,int> &c,pair<int,int> d){ if (b>a) a=b,c=d; }   int n,d;   int a[100005];   ld dp[100005][10];   ld lg[100005];   pair<int,int>l[100005][10];   signed main(){    cin>>n>>d;    rept(i,1,n+1){     cin>>a[i];     lg[i]=log2(a[i]);    }    rep(i,n+1) rep(j,10) dp[i][j]=-1.0;    dp[0][1]=0.0;    rep(i,n){     rep(j,10){      if (dp[i][j]>=0.0){       chmax(dp[i+1][j],dp[i][j],l[i+1][j],mp(0,j));       chmax(dp[i+1][j*a[i+1]%10],dp[i][j]+lg[i+1],l[i+1][j*a[i+1]%10],mp(1,j));      }     }    }    int pos=-1;    ld ans=0.0;     rept(i,1,n+1){     if (dp[i][d]>=ans){      ans=dp[i][d];      pos=i;     }    }    vector<int>v;    int x=l[pos][d].first,y=l[pos][d].second;    for (int i=pos-1;i>=0;i--){     if (x)      v.pb(i+1);     x=l[i][y].first,y=l[i][y].second;    }    if (!sz(v))     fail(-1);    cout<<sz(v)<<  n ;    rep(i,sz(v))     cout<<a[v[i]]<<   ;    return 0;   }


#include <bits/stdc++.h>   using namespace std;   void read(int& x) {     bool fu = 0;     char c;     for (c = getchar(); c <= 32; c = getchar())       ;     if (c ==  - ) fu = 1, c = getchar();     for (x = 0; c > 32; c = getchar()) x = x * 10 + c -  0 ;     if (fu) x = -x;   };   char getc() {     char c;     for (c = getchar(); c <= 32; c = getchar())       ;     return c;   }   const int N = 100010;   int n, i, j, k, l, p, x, a[N], s0, s1;   int b[N];   int main() {     read(n);     for (i = 1; i <= n; i++) read(a[i]);     for (i = 1; i <= n && a[i] == 0; i++) printf( 0 n );     for (; i <= n; i = j + 1) {       for (j = i; j <= n && a[j] > 0; j++)         ;       j--;       if (j + 1 > n) {         for (k = i; k <= n; k++) printf( pushStack n );         break;       }       b[0] = 0;       for (k = i; k <= j; k++) b[++b[0]] = a[k];       sort(b + 1, b + 1 + b[0]);       if (b[0] == 0)         printf( 0 n );       else if (b[0] == 1)         printf( pushStack n1 popStack n );       else if (b[0] == 2)         printf( pushStack npushQueue n2 popStack popQueue n );       else {         p = b[b[0] - 2];         for (k = i; k <= j; k++)           if (a[k] >= p) break;         s0 = k;         for (k = j; k >= i; k--)           if (a[k] >= p) break;         s1 = k;         if (s1 <= s0) printf( - - n );         for (k = i; k <= j; k++)           if (k == s1)             printf( pushStack n );           else if (k == s0)             printf( pushQueue n );           else if (a[k] >= p)             printf( pushFront n );           else if (k < s1)             printf( pushStack n );           else             printf( pushQueue n );         printf( 3 popStack popQueue popFront n );       }       j++;     }     scanf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct com {     int l, r;     long long c;     friend bool operator<(const com &a, const com &b) { return a.r < b.r; }   } a[100010];   int n, m, K;   long long cost[310][310], dp[310][310];   const long long inf = 0x3F3F3F3F3F3F3F3FLL;   int main() {     scanf( %d%d%d , &n, &m, &K);     memset(cost, 0x3F, sizeof(cost));     for (int i = 0; i < m; i++)       scanf( %d%d%I64d , &a[i].l, &a[i].r, &a[i].c),           cost[a[i].l][a[i].r] = min(cost[a[i].l][a[i].r], a[i].c);     for (int l = n; l >= 1; l--) {       for (int i = 1; i + l - 1 <= n; i++) {         int st = i, ed = i + l - 1;         if (st > 1) cost[st][ed] = min(cost[st][ed], cost[st - 1][ed]);         if (ed < n) cost[st][ed] = min(cost[st][ed], cost[st][ed + 1]);       }     }     memset(dp, 0x3F, sizeof(dp));     dp[0][0] = 0;     for (int i = 1; i <= n; i++)       for (int j = 0; j <= i; j++) {         dp[i][j] = min(dp[i][j], dp[i - 1][j]);         for (int k = 0; k <= j; k++)           dp[i][j] = min(dp[i - k][j - k] + cost[i - k + 1][i], dp[i][j]);       }     if (dp[n][K] == inf)       cout << -1 << endl;     else       cout << dp[n][K] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[107], b[107];   int ans[10000007];   int n, nMax, nMaxid, times, nMin, aol, aMin, aMax, aL;   void init() {     memset(a, 0, sizeof(a));     memset(ans, 0, sizeof(ans));     memset(b, 0, sizeof(b));     aol = 0;     aL = 0;   }   int check(int x) {     nMax = 0;     times = 0;     for (int i = 1; i <= n; i++) {       if (a[i] > nMax) {         nMax = a[i];         nMaxid = i;         times = 0;       }       if (a[i] == nMax) times++;     }     if (times < x) return 1;     return 0;   }   void makeans(int x) {     times -= x;     for (int i = 1; i <= n; i++)       if (a[i] == nMax) {         x--;         a[i]--;         ans[++aol] = i;         if (x == 0) break;       }     ++aol;     aL++;   }   void printans(int x) {     if (ans[x] == 0) {       for (int i = 1; i <= n; i++) printf( %d , b[i]);       printf(  n );       memset(b, 0, sizeof(b));     } else       b[ans[x]] = 1;   }   int main() {     while (scanf( %d , &n) != EOF) {       init();       for (int i = 1; i <= n; i++) scanf( %d , &a[i]);       while (check(2)) {         int yMax = -1, yID = 0;         for (int i = 1; i <= n; i++)           if (a[i] > yMax) {             if (i == nMaxid) continue;             yMax = a[i];             yID = i;           }         ans[++aol] = yID;         ans[++aol] = nMaxid;         ++aol;         aL++;         if (a[yID]) a[yID]--;         a[nMaxid]--;       }       aMin = 107;       aMax = 0;       for (int i = 1; i <= n; i++)         if (a[i] < aMin) aMin = a[i];       while (check(n)) {         if (times % 2 == 1) makeans(3);         while (times) makeans(2);       }       printf( %d n%d n , aMin, aL);       for (int i = 1; i <= aol; i++) printans(i);     }     return 0;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   namespace Bigcat {   template <typename T>   inline void max_s(T& a, const T& b) {     a = max(a, b);   }   template <typename T>   inline void min_s(T& a, const T& b) {     a = min(a, b);   }   template <typename T>   void sort_v(T& x) {     sort(x.begin(), x.end());   }   template <typename T>   void erase_dup(T& x) {     x.erase(unique(x.begin(), x.end()), x.end());   }   template <typename T, typename V>   int pos_l(const T& x, const V& v) {     return lower_bound(x.begin(), x.end(), v) - x.begin();   }   template <typename T, typename V>   int pos_u(const T& x, const V& v) {     return upper_bound(x.begin(), x.end(), v) - x.begin();   }   inline unsigned rand_g() {     static mt19937 mt_rand((int)time(0));     return mt_rand();   }   using LL = long long;   using VI = vector<int>;   using VVI = vector<VI>;   using VL = vector<LL>;   using PII = pair<int, int>;   using VPII = vector<PII>;   }  // namespace Bigcat   using namespace Bigcat;   const LL base = LL(1e9 + 7);   const LL INFL = 1LL << 62;   const int INF = 1 << 30;   const int MAX = int(1e5 + 100);   struct Node {     int x, y, w;   };   bool operator<(const Node& n0, const Node& n1) { return n0.x < n1.y; }   LL le0[300], ri0[300], th[300];   string zs[MAX];   void solve1() {     LL result = 0;     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> zs[i];     }     string s = zs[n - 1];     char c0 =    ;     LL g = 0;     for (auto x : s) {       if (x == c0)         g++;       else {         c0 = x;         result = max(result, g);         g = 1;       }     }     result = max(result, g);     char lastone;     auto work = [&]() {       int bb = n;       for (int tt = n - 1; tt >= 0; --tt) {         string s = zs[tt];         LL g = 0;         for (auto x : s) {           if (x == lastone)             g++;           else {             break;           }         }         if (g == s.size()) {           continue;         }         bb = tt + 1;         break;       }       auto func = [&](const string& s) {         LL t = 0, a = 0;         for (auto c : s) {           if (c == lastone) {             t++;             a = max(a, t);           } else {             t = 0;           }         }         return a;       };       LL mc = 0;       for (int i = 0; i < bb; ++i) {         string s = zs[i];         if (mc == 0) {           mc = func(s);           continue;         }         if (i != bb - 1) continue;         LL t = 1;         for (int i = 0; i < s.size(); ++i) {           if (s[i] != lastone) break;           t++;         }         for (int i = s.size() - 1; i >= 0; --i) {           if (s[i] != lastone) break;           t++;         }         mc = t;         mc = max(mc, func(s));       }       for (int i = bb; i < n; ++i) {         mc = (mc + 1) * zs[i].size() + mc;       }       result = max(result, mc);     };     lastone = zs[n - 1][0];     work();     lastone = zs[n - 1].back();     work();     cout << result << endl;   }   void solve() {     int t = 1;     for (int i = 0; i < t; ++i) {       solve1();     }     int x = 1;   }   void io_init() {     const int inId = 0;     const int outId = 0;     if (inId > 0) {       cerr <<  redirect stdin to input   << inId << endl;       string s =  input ;       s += to_string(inId);       s +=  .txt ;       auto r = freopen(s.c_str(),  r , stdin);       if (r == nullptr) {         cerr <<  invalid input file   << s << endl;         ;         r = freopen( CON ,  r , stdin);       }     }     if (outId > 0) {       cerr <<  redirect stdout to stdout   << outId << endl;       string s =  stdout ;       s += to_string(outId);       s +=  .txt ;       auto r = freopen(s.c_str(),  w , stdout);       if (r == nullptr) {         cerr <<  invalid output file   << s << endl;         ;         r = freopen( CON ,  w , stdout);       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout.precision(11);     solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, s1;     int count1 = 0, count2 = 0;     cin >> s >> s1;     for (int i = 0; i < s.size(); i++) {       if (s[i] != s1[i]) {         if (s[i] ==  4 )           count1++;         else           count2++;       }     }     if (count1 <= count2)       cout << count2;     else       cout << count1;     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int n;       cin >> n;       map<int, int> m;       long long int a[n];       for (int j = 0; j < n; j++) {         cin >> a[j];         m[a[j] % 3]++;       }       long long int res = m[0];       if (m[1] >= m[2]) {         res += m[2];         m[1] = m[1] - m[2];         res += m[1] / 3;       } else {         res += m[1];         m[2] = m[2] - m[1];         res += m[2] / 3;       }       cout << res <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, ans, flag;   int a[100005], b[100005], xb[100005];   void solve() {     int i, j, ma;     ans = -1;     b[1] = xb[1] = 0;     for (i = 2; i <= n; i++) {       ma = 0;       flag = 0;       j = i - 1;       while (a[j] < a[i] && j) {         if (!b[j]) {           flag = 1;           break;         }         if (b[j] > ma) ma = b[j];         j = xb[j];       }       if (!flag)         b[i] = ma + 1;       else         b[i] = 0;       xb[i] = j;     }     for (i = 1; i <= n; i++) {       if (ans < b[i]) ans = b[i];     }   }   int main() {     int i, j;     while (~scanf( %d , &n)) {       for (i = 1; i <= n; i++) {         scanf( %d , &a[i]);       }       solve();       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int SIZE = 1 << 20;   int INF = 1000000001;   int a[SIZE], ops[SIZE];   int h, g;   int qq[24], qn;   int pull(int id) {     int tmp = a[id];     a[id] = 0;     qn = 0;     qq[qn++] = id;     while (id * 2 < (1 << h) && a[id] < max(a[id << 1], a[(id << 1) | 1])) {       if (a[id << 1] > a[(id << 1) | 1]) {         swap(a[id << 1], a[id]);         id <<= 1;       } else {         swap(a[(id << 1) | 1], a[id]);         id = (id << 1) | 1;       }       qq[qn++] = id;     }     if (id < (1 << g)) {       for (int i = qn - 1; i > 0; i--) {         a[qq[i]] = a[qq[i - 1]];       }       a[qq[0]] = tmp;       return 0;     }     return tmp;   }   void solve() {     scanf( %d%d , &h, &g);     long long an = 0;     for (int i = 1; i < (1 << h); i++) {       scanf( %d , &a[i]);       an += a[i];     }     int need = 0;     for (int i = 1; i < (1 << g); i++) {       while (1) {         int v = pull(i);         if (v) {           an -= v;           ops[need++] = i;         } else           break;       }     }     printf( %lld n , an);     for (int i = 0; i < need; i++) printf( %d%c , ops[i],    n [i == need - 1]);   }   int main() {     int T;     scanf( %d , &T);     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     cin >> a >> b;     cout << (a * b) / 2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mox = 1e6 + 9;   long long ha[mox];   long long mod = 1e9 + 7;   long long d[mox];   bool visit[mox];   long long ma[mox];   bool visit2[mox];   long long ta(long long a, long long b) {     long long ans = 1;     long long c = a;     while (b) {       if (b % 2) {         ans = (ans * c) % mod;       }       c = (c * c) % mod;       b /= 2;     }     return ans;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, m;     cin >> n >> m;     string t;     cin >> t;     d[0] = 1;     for (long long i = 0; i < t.size(); i++) {       long long x = (d[i] * (t[i] -  a  + 1)) % mod;       ha[i] = (ha[i - 1] + x) % mod;       d[i + 1] = (d[i] * 27) % mod;     }     long long ans = 0;     for (long long i = t.size() - 1; i >= 1; i--) {       long long x = (ha[t.size() - 1] - ha[i - 1] + mod) % mod;       long long y = ta(d[i], mod - 2);       x = (x * y) % mod;       if (x == ha[t.size() - 1 - i]) {         visit[i] = 1;       }     }     for (long long i = 0; i < m; i++) {       cin >> ma[i];       visit2[ma[i]] = 1;     }     for (long long i = 1; i < m; i++) {       if (ma[i] - ma[i - 1] < t.size()) {         if (visit[ma[i] - ma[i - 1]] == 0) {           cout << 0;           exit(0);         }       }     }     long long x = 0;     long long o = 0;     for (long long i = 1; i <= n; i++) {       if (visit2[i] == 1) {         x = i + t.size() - 1;       }       if (i > x) {         o += 1;       }     }     long long w = 1;     for (long long i = 0; i < o; i++) {       w = (w * 26) % mod;     }     cout << w;   }


#include <bits/stdc++.h>   using namespace std;   void solve_test() {     string s;     cin >> s;     int n = int(s.length());     int total = 0;     long long ans = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  + )         total++;       else         total--;       if (total < 0) {         total++;         ans += i + 1;       }     }     cout << ans + n <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int tests = 1;     cin >> tests;     while (tests--) solve_test();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 10;   int sum[maxn], op[maxn];   int main() {     int n, l = 0, r = 0, ans = 0, minn = 100;     string s;     cin >> n >> s;     for (int i = 0; i < n; i++) {       if (s[i] ==  ( ) {         op[i + 1] = 1;         sum[i + 1] = sum[i] + 1;         l++;       } else {         op[i + 1] = 0;         sum[i + 1] = sum[i] - 1;         r++;       }     }     if (n & 1 == 1 || (abs(l - r) > 2) || (sum[n] == 0) ||         (*min_element(sum + 1, sum + 1 + n) < -2) ||         (*min_element(sum + 1, sum + 1 + n) < 0 && sum[n] != -2)) {       printf( 0 n );       exit(0);     }     if (sum[n] == -2) {       for (int i = 1; i <= n; i++) {         if (op[i] == 0 && sum[i] < 0) {           ans++;           break;         } else if (op[i] == 0 && sum[i] >= 0)           ans++;       }     }     if (sum[n] == 2) {       for (int i = n; i >= 1; i--) {         if (sum[i] >= 2)           minn = i;         else           break;       }       minn = max(minn, 2);       for (int i = minn; i <= n; i++) {         if (op[i] == 1) ans++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   int fr[200002];   int arr[200002];   bool check(int cati) {     int qq = 0;     for (int i = 1; i <= 200000; ++i) qq += fr[i] / cati;     if (qq >= k) return 1;     return 0;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> k;     for (int i = 1; i <= n; ++i) cin >> arr[i], fr[arr[i]]++;     int b = 1;     int e = n;     int ans = 1;     while (b <= e) {       int mid = (b + e) / 2;       if (check(mid))         ans = mid, b = mid + 1;       else         e = mid - 1;     }     int qa = 0;     for (int i = 1; i <= 200000; ++i) {       if (qa == k) return 0;       while (fr[i] >= ans && qa < k) cout << i <<    , ++qa, fr[i] -= ans;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v;   map<int, long long> dp;   long long power(int a, long long b) {     long long res = 1, base = a;     while (b > 0) {       if (b % 2) {         res *= base;         res %= (1000 * 1000 * 1000 + 7);       }       base *= base;       base %= (1000 * 1000 * 1000 + 7);       b /= 2;     }     return res;   }   long long compute(int g) {     if (g == 1) {       return 1;     }     if (dp.find(g) != dp.end()) {       return dp[g];     }     long long val = power(2, g - 1);     int i;     for (i = 0; i < v.size(); i++) {       if (v[i] >= g) {         break;       }       if (g % v[i] == 0) val -= compute(v[i]);     }     dp[g] = val;     dp[g] %= (1000 * 1000 * 1000 + 7);     dp[g] += (1000 * 1000 * 1000 + 7);     dp[g] %= (1000 * 1000 * 1000 + 7);     return dp[g];   }   int main() {     std::ios::sync_with_stdio(false);     int x, y, val1, i;     cin >> x >> y;     if (y % x) {       cout << 0 << endl;     } else {       y /= x;       for (i = 1; i * i <= y; i++) {         if (y % i == 0) {           v.push_back(i);           if (y / i != i) v.push_back(y / i);         }       }       sort(v.begin(), v.end());       cout << compute(y);     }   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     long long r;     while (b != 0) {       r = a % b;       a = b;       b = r;     }     return a;   }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   int n, m;   vector<int> v[4000000];   char g[4000000];   int inline make(int i, int j) { return i * m + j; }   void solve() {     cin >> n >> m;     int cnt = 0;     for (int i = 0; i < n; i++) {       char buf[2001];       cin >> buf;       for (int j = 0; j < m; j++) {         g[make(i, j)] = buf[j];         if (g[make(i, j)] ==  . ) {           cnt++;           if (i > 0 && g[make(i - 1, j)] ==  . ) {             v[make(i, j)].push_back(make(i - 1, j));             v[make(i - 1, j)].push_back(make(i, j));           }           if (j > 0 && g[make(i, j - 1)] ==  . ) {             v[make(i, j)].push_back(make(i, j - 1));             v[make(i, j - 1)].push_back(make(i, j));           }         }       }     }     queue<int> q;     for (int i = 0; i < n * m; i++)       if (int((v[i]).size()) == 1) q.push(i);     while (!q.empty()) {       int i = q.front();       q.pop();       if (int((v[i]).size()) == 1) {         int j = v[i][0], x = ((i) > (j) ? (j) : (i)), y = ((i) > (j) ? (i) : (j));         if (x / m == y / m) {           g[x] =  < ;           g[y] =  > ;         } else {           g[x] =  ^ ;           g[y] =  v ;         }         for (int k = 0; k < int((v[j]).size()); k++) {           int l = v[j][k];           for (int h = 0; h < int((v[l]).size()); h++) {             if (v[l][h] == j) {               v[l].erase(v[l].begin() + h);               break;             }           }           if (int((v[l]).size()) == 1) q.push(l);         }         v[i].clear();         v[j].clear();         cnt -= 2;       }     }     if (cnt) {       cout <<  Not unique ;       return;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) cout << g[make(i, j)];       cout <<   n ;     }   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> v = {1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683};     vector<int> vis;     for (int i = 0; i < (1 << 10); i++) {       int sum = 0;       for (int j = 0; j < 10; j++) {         if (i & (1 << j)) {           sum += v[j];         }       }       vis.push_back(sum);     }     sort(vis.begin(), vis.end());     int q;     cin >> q;     while (q--) {       int x;       cin >> x;       cout << *lower_bound(vis.begin(), vis.end(), x) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 7;   int main() {     int T;     scanf( %d , &T);     while (T--) {       int l, r;       scanf( %d%d , &l, &r);       if (l * 2 > r) {         printf( YES n );       } else {         printf( NO n );       }     }     return 0;   }


#include<bits/stdc++.h>   #define LL long long   using namespace std;   int n,m,edge_t=0,sum=0,rt_t=0;      bool tyl_jyy=0;      LL t,ans=0;   int rt[200002]={};   vector<int> vec[200002];   set<int> s;   struct aaa   {    int x,y,val,u;   }e[200002];   template<class T>void read(T &x)   {    x=0;int f=0;char ch=getchar();    while(ch< 0  || ch> 9 )f|=(ch== - ),ch=getchar();    while(ch>= 0  && ch<= 9 )x=(x<<3)+(x<<1)+(ch^48),ch=getchar();    x=f? -x:x;return ;   }   inline bool cmp(aaa a,aaa b)   {    return a.val<b.val? 1:0;   }   inline void add_edge(int x,int y,int z,int i)   {    e[i]=(aaa){x,y,z,0},vec[x].push_back(y),vec[y].push_back(x);   }   inline void dfs(int x,int y)   {    int i,j;s.erase(x),rt[x]=y;    set<int>::iterator it;    for(int i=1;;++i)    {     it=s.lower_bound(i);     if(it==s.end())return ;     i=(*it),j=lower_bound(vec[x].begin(),vec[x].end(),i)-vec[x].begin();     if(j==vec[x].size() || vec[x][j]>i)dfs(i,y);    }    /*for(set<int>::iterator it=s.begin();;)    {     v=(*it),ok=((++it)==s.end()),i=lower_bound(vec[x].begin(),vec[x].end(),v)-vec[x].begin();     if(i==vec[x].size() || vec[x][i]>v)dfs(v,y);if(ok)return ;    }*/   }   inline void getroot(int x)   {    if(x==rt[x])return ;    getroot(rt[x]),rt[x]=rt[rt[x]];   }   int main()   {    read(n),read(m),t=(1LL*n*(n-1))/2-m;        //if(n==200000 && m==200000)tyl_jyy=1;        for(int i=1,x,y,z;i<=m;++i)read(x),read(y),read(z),sum^=z,add_edge(x,y,z,i);    for(int i=1;i<=n;++i)s.insert(i),sort(vec[i].begin(),vec[i].end());        //if(tyl_jyy)return 0&puts( ok );        for(int i=1;i<=n;++i)if(!rt[i])++rt_t,dfs(i,i);    sort(e+1,e+m+1,cmp);    for(int i=1,x,y;i<=m;++i)    {     x=e[i].x,y=e[i].y,getroot(x),getroot(y);     if(rt[x]==rt[y])continue;     rt[rt[x]]=rt[y],ans+=e[i].val,e[i].u=1;    }    if(n-rt_t==t)    {     for(int i=1;i<=n;++i)rt[i]=i;     for(int i=1,x,y;i<=m;++i)if(e[i].u)x=e[i].x,y=e[i].y,getroot(x),getroot(y),rt[rt[x]]=rt[y];     for(int i=1,x,y;i<=m;++i)      if(!e[i].u)      {       x=e[i].x,y=e[i].y,getroot(x),getroot(y);       if(rt[x]==rt[y])continue;       sum=min(sum,e[i].val);break;      }    }    else sum=0;    printf( %lld ,ans+sum);    return 0;   }


#include <bits/stdc++.h>   using namespace std;   long int a[5001], b[5001];   long long ans[5001];   int main(void) {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       a[i] -= i;       b[i] = a[i];     }     sort(b + 1, b + n + 1);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) {         ans[j] += abs(a[i] - b[j]);         if (j > 1) ans[j] = min(ans[j], ans[j - 1]);       }     cout << ans[n];   }


#include <bits/stdc++.h>   int main() {     long n, a, b;     scanf( %ld %ld %ld , &n, &a, &b);     std::vector<long> v(n, 1);     long current(1), sum(1);     for (long p = 1; p < n; p++) {       if (b > 0) {         current = sum + 1;         --b;       } else if ((p > 1) && (a > 0)) {         ++current, --a;       }       v[p] = current;       sum += current;     }     if ((a > 0) || (b > 0)) {       puts( -1 );     } else {       for (long p = 0; p < n; p++) {         printf( %ld  , v[p]);       };       puts(  );     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 100;   void ask(int r1, int c1, int r2, int c2) {     cout <<  ?   << r1 <<     << c1 <<     << r2 <<     << c2 <<   n ;     cout.flush();   }   bool ans() {     string ans;     cin >> ans;     return ans ==  YES ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int steps = 0;     int rx = 1, ry = 1;     string pre =   ;     while (steps < n - 1) {       ask(rx + 1, ry, n, n);       bool sol = ans();       if (sol) {         pre +=  D ;         rx++;       } else {         pre +=  R ;         ry++;       }       steps++;     }     string suf =   ;     steps = 0;     rx = n, ry = n;     while (steps < n - 1) {       ask(1, 1, rx, ry - 1);       bool sol = ans();       if (sol) {         suf +=  R ;         ry--;       } else {         suf +=  D ;         rx--;       }       steps++;     }     reverse(suf.begin(), suf.end());     cout <<  !   << (pre + suf) <<   n ;     cout.flush();   }


#include <bits/stdc++.h>   using namespace std;   template <class A>   void read(vector<A>& v);   template <class A, size_t S>   void read(array<A, S>& a);   template <class T>   void read(T& x) {     cin >> x;   }   void read(double& d) {     string t;     read(t);     d = stod(t);   }   void read(long double& d) {     string t;     read(t);     d = stold(t);   }   template <class H, class... T>   void read(H& h, T&... t) {     read(h);     read(t...);   }   template <class A>   void read(vector<A>& x) {     for (auto& a : x) read(a);   }   template <class A, size_t S>   void read(array<A, S>& x) {     for (auto& a : x) read(a);   }   const int mod = 1e9 + 7;   long long int primeLimit = 1e5;   const double PI = acos(-1);   bool prime[100000];   string LongestPalindromicPrefix(string str) {     string temp = str +  ? ;     reverse(str.begin(), str.end());     temp += str;     long long int n = temp.length();     long long int lps[n];     fill(lps, lps + n, 0);     for (long long int i = 1; i < n; i++) {       long long int len = lps[i - 1];       while (len > 0 && temp[len] != temp[i]) {         len = lps[len - 1];       }       if (temp[i] == temp[len]) {         len++;       }       lps[i] = len;     }     return temp.substr(0, lps[n - 1]);   }   bool binarySearchFloat(float lower, float higher, float maxErrorAllowed,                          long long int numberOfTimesWanttoRunCode) {     float l = lower, r = higher;     float eps = maxErrorAllowed;     long long int iteration_count = numberOfTimesWanttoRunCode;     for (long long int i = 0; i < iteration_count && l + eps < r; ++i) {     }   }   void primefinder() {     memset(prime, true, sizeof(prime));     prime[1] = false;     prime[0] = false;     for (long long int i = 2; i < primeLimit; i++) {       if (prime[i]) {         for (long long int j = i * i; j < primeLimit; j += i) {           prime[j] = false;         }       }     }   }   long long fast_pow(long long int x, long long int y) {     long long res = 1;     while (y > 0) {       if (y & 1) res = res * x;       y = y >> 1;       x = x * x;     }     return res;   }   long long Fast_pow(long long int x, long long int y, long long int p) {     long long res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long int bs(vector<long long int> arr, long long int low,                    long long int cur) {     long long int high = (long long int)arr.size() - 1;     long long int index = -1;     while (low <= high) {       long long int mid = (low + high) / 2;       if (arr[mid] <= cur) {         index = mid;         low = mid + 1;       } else {         high = mid - 1;       }     }     return index;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, k;     cin >> n >> k;     vector<long long int> arr(n);     read(arr);     sort((arr).begin(), (arr).end());     long long int ans = 0;     for (long long int i = 0; i < n; i++) {       long long int index = bs(arr, i, 2 * k);       if (index >= (n - 1)) break;       if (index == -1) {         while ((k * 2) < arr[i]) {           k *= 2;           ans++;         }         k = std::max(k, arr[i]);       } else {         k = std::max(k, arr[index]);       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   char x[15];   int main() {     scanf( %s , x);     int lenx = strlen(x);     for (int i = lenx - 1; i >= 0; i--) {       int dig = x[i] -  0 ;       if (dig == 0)         printf( O-|-OOOO n );       else if (dig == 1)         printf( O-|O-OOO n );       else if (dig == 2)         printf( O-|OO-OO n );       else if (dig == 3)         printf( O-|OOO-O n );       else if (dig == 4)         printf( O-|OOOO- n );       else if (dig == 5)         printf( -O|-OOOO n );       else if (dig == 6)         printf( -O|O-OOO n );       else if (dig == 7)         printf( -O|OO-OO n );       else if (dig == 8)         printf( -O|OOO-O n );       else         printf( -O|OOOO- n );     }   }


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


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-6, pi = acos(-1);   const int N = 1e2 + 5, M = 1e9 + 7, SEGM = 4 * N, OO = 0x3f3f3f3f;   int t, n, m, v1, v2;   int mem[105][2005];   int main() {     scanf( %d%d%d%d , &v1, &v2, &n, &m);     if (v1 > v2) swap(v1, v2);     memset(mem, -OO, sizeof mem);     mem[0][v1] = v1;     for (int i = 1; i < n; ++i) {       for (int cur = v1; cur < 2005; ++cur) {         for (int j = 0; j <= m; ++j) {           if (cur + j < 2005)             mem[i][cur + j] = max(mem[i][cur + j], cur + j + mem[i - 1][cur]);           if (cur - j > -1)             mem[i][cur - j] = max(mem[i][cur - j], cur - j + mem[i - 1][cur]);         }       }     }     cout << mem[n - 1][v2];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void flanther_fakadi() {     string s;     cin >> s;     long long n = s.length();     long long ct = 0;     for (long long i = 0; i < s.length(); i++) {       if (ct && s[i] ==  B ) {         n -= 2;         ct--;       } else {         ct++;       }     }     cout << n << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(NULL);     int t = 0;     cin >> t;     while (t--) {       flanther_fakadi();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int n, k;     cin >> n >> k;     string s;     cin >> s;     vector<pair<long long int, long long int>> ans;     string des =   ;     for (long long int i = 1; i < k; i++) {       des +=  () ;     }     long long int maxx = (n / 2);     for (long long int i = 0; i < (maxx - k + 1); i++) {       des +=  ( ;     }     for (long long int i = 0; i < (maxx - k + 1); i++) {       des +=  ) ;     }     for (long long int i = 0; i < n; i++) {       if (s[i] != des[i]) {         long long int j = i + 1;         while (s[j] != des[i]) j++;         string temp = s.substr(i, (j - i + 1));         reverse(temp.begin(), temp.end());         for (long long int k = i; k <= j; k++) {           s[k] = temp[k - i];         }         ans.push_back({i + 1, j + 1});       }     }     cout << ans.size() <<   n ;     for (long long int i = 0; i < ans.size(); i++) {       cout << ans[i].first <<     << ans[i].second <<   n ;     }   }   signed main() {     long long int t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxv = 1.5e7;   const int bit = 29;   int cnt[maxv] = {};   int go[maxv][2] = {};   int tot = 1;   void add(int v, int d, int val) {     cnt[v]++;     if (d == -1) return;     if (!go[v][(val >> d) & 1]) go[v][(val >> d) & 1] = ++tot;     add(go[v][(val >> d) & 1], d - 1, val);   }   void get(int v, int d, int val, int& t_a) {     assert(cnt[v]);     cnt[v]--;     if (d == -1) return;     for (int k = ((val >> d) & 1), l = 0; l < 2; k = 1 - k, ++l)       if (go[v][k]) {         get(go[v][k], d - 1, val, t_a);         if (!cnt[go[v][k]]) go[v][k] = 0;         t_a += (k << d);         return;       }   }   int main() {     int n;     cin >> n;     vector<int> msg(n);     for (int& i : msg) cin >> i;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       add(1, bit, x);     }     for (int i : msg) {       int out = 0;       get(1, bit, i, out);       assert((i ^ out) >= 0);       cout << (i ^ out) <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   int head[N], nxt[N << 1], ver[N << 1], tot;   int n, d[N], out[N], f[N], ji[N], ou[N];   vector<int> v[N];   bool flag;   void add(int x, int y) {     ver[++tot] = y;     nxt[tot] = head[x], head[x] = tot;     out[x]++;   }   void dfs(int x, int fa) {     d[x] = d[fa] + 1;     f[x] = fa;     for (int i = head[x]; i; i = nxt[i]) {       int y = ver[i];       if (y == fa) continue;       dfs(y, x);     }   }   void dfs2(int x, int fa) {     for (int i = head[x]; i; i = nxt[i]) {       int y = ver[i];       if (y == fa) continue;       dfs2(y, x);       if (ou[y]) ji[x] = 1;       if (ji[y]) ou[x] = 1;     }     if (ou[x] && ji[x]) flag = true;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       add(x, y), add(y, x);     }     int root = 0, minn = 0, maxx = n - 1;     for (int i = 1; i <= n; i++)       if (out[i] >= 2) {         root = i;         break;       }     for (int i = 1; i <= n; i++)       if (out[i] == 1) ou[i] = 1;     dfs2(root, 0);     minn = (flag ? 3 : 1);     dfs(root, 0);     for (int i = 1; i <= n; i++) v[f[i]].push_back(i);     for (int i = 1; i <= n; i++) {       int siz = v[i].size(), cnt = 0;       if (!siz) continue;       for (int j = 0; j < siz; j++) {         int tt = v[i][j];         if (out[tt] == 1) cnt++;       }       if (cnt) maxx -= cnt - 1;     }     printf( %d %d n , minn, maxx);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cnt[3], ans;   vector<int> c;   vector<vector<int>> sum;   vector<vector<int>> arr;   void dfs(int x, int p) {     for (auto i : arr[x]) {       if (i != p) {         dfs(i, x);         sum[x][0] += sum[i][0];         sum[x][1] += sum[i][1];         sum[x][2] += sum[i][2];       }     }     sum[x][c[x]]++;     if (sum[x][1] == 0 && sum[x][2] == cnt[2] ||         sum[x][1] == cnt[1] && sum[x][2] == 0)       ans++;   }   int main() {     cin.tie(0), ios::sync_with_stdio(0);     int n;     cin >> n;     c.resize(n);     arr.resize(n);     for (int i = 0; i < n; i++) cin >> c[i], cnt[c[i]]++;     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       arr[a - 1].push_back(b - 1);       arr[b - 1].push_back(a - 1);     }     sum.resize(n, vector<int>(3, 0));     dfs(0, 0);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1e3 + 5;   long long int dp[M][12][M], inv[M], n, d, mod;   long long int pw(long long int a, long long int b) {     if (b == 0) return 1ll;     long long int p = pw(a, b / 2ll);     if (b % 2ll)       return p * p % mod * a % mod;     else       return p * p % mod;   }   int main() {     cin >> n >> d >> mod;     for (long long int i = 1; i <= n; i++) inv[i] = pw(i, mod - 2ll);     dp[1][0][0] = 1;     for (int i = 2; i <= n; i++)       for (int j = 1; j <= d; j++)         for (int k = (i - 1) / j; k <= i - 1; k++) {           dp[i][j][k] = dp[i][j][k - 1];           long long int cur = 1, buf = dp[k][d - 1][k - 1];           for (int t = 1; t <= j && t * k <= i - 1; t++) {             cur = cur * buf++ % mod * inv[t] % mod;             (dp[i][j][k] += dp[i - t * k][j - t][min(k - 1, i - t * k - 1)] *                             (cur + (k == 1))) %= mod;           }         }     long long int ans = dp[n][d][(n - 1) / 2];     if (n % 2 == 0)       (ans += dp[n / 2][d - 1][n / 2 - 1] * (dp[n / 2][d - 1][n / 2 - 1] + 1) %               mod * inv[2]) %= mod;     cout << (n <= 2 ? 1 : ans) << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     long long n, m;     cin >> n >> m;     vector<vector<long long>> dist((size_t)n,                                    vector<long long>((size_t)n, (long long)1e16));     for (long long i = 0; i < n; i++) {       dist[i][i] = (long long)0;     }     for (long long i = 0; i < m; i++) {       long long u, v;       cin >> u >> v;       u--, v--;       dist[u][v] = dist[v][u] = 0;     }     for (long long k = 0; k < n; k++) {       for (long long i = 0; i < n; i++) {         for (long long j = 0; j < n; j++) {           dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);         }       }     }     long long s, b, k, h;     cin >> s >> b >> k >> h;     vector<pair<long long, pair<long long, long long>>> x((size_t)s);     for (long long i = 0; i < s; i++) {       cin >> x[i].first >> x[i].second.first >> x[i].second.second;       x[i].first--;     }     vector<pair<long long, long long>> y((size_t)b);     for (long long i = 0; i < b; i++) {       cin >> y[i].first >> y[i].second;       y[i].first--;     }     vector<vector<long long>> adj((size_t)s);     for (long long i = 0; i < s; i++) {       for (long long j = 0; j < b; j++) {         if (x[i].second.first >= y[j].second &&             x[i].second.second >= dist[x[i].first][y[j].first]) {           adj[i].push_back(j);         }       }     }     vector<bool> visited;     vector<long long> matching((size_t)b, -1);     function<bool(long long)> visit = [&](long long i) {       if (visited[i]) {         return false;       }       visited[i] = true;       for (long long j : adj[i]) {         if (matching[j] == -1 || visit(matching[j])) {           matching[j] = i;           return true;         }       }       return false;     };     long long f = 0;     for (long long i = 0; i < s; i++) {       visited.assign((size_t)s, false);       if (visit(i)) {         f++;       }     }     cout << min(f * k, h * s) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     int x, y;     cin >> x >> y;     string s;     cin >> s;     bool flag = false;     int cnt = 0;     for (int i = 0; i < n; ++i) {       if (s[i] ==  0 ) {         flag = true;       } else if (flag) {         ++cnt;         flag = false;       }     }     if (flag) ++cnt;     long long ans = 1LL * (cnt - 1) * min(x, y) + y;     if (cnt == 0) ans = 0;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string cad;     int cnt = 0, ans = 0;     cin >> cnt >> cad;     cnt = 0;     for (int i = 0; i < cad.size(); i++) {       if (cad[i] ==  x ) {         cnt++;       } else {         ans += max(0, cnt - 2);         cnt = 0;       }     }     ans += max(0, cnt - 2);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int x, y, a, b, c;     cin >> x >> y >> a >> b >> c;     long long int t = a * 2;     long long int r = c * 3;     cout << max(t + b - x, 0ll) + max(r + b - y, 0ll);     return 0;   }


#include <bits/stdc++.h>   const int kInf = 0x3f3f3f3f;   int len, n;   int msk[200010];   int mem[21][1 << 20];   int f[200010], s[200010];   int conv(char *s) {     int res = 0;     for (int i = 0; i < len; i++) res = (res << 1) + (s[i] -  0 );     return res;   }   int g(int i, int j) {     int p = msk[i], q = msk[j];     for (int i = len; i >= 0; i--)       if ((p & ((1 << i) - 1)) == (q >> (len - i))) return i;     assert(0);   }   inline void upd(int &x, int y) {     if (y < x) x = y;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       static char ts[22];       scanf( %s , ts);       len = strlen(ts);       msk[i] = conv(ts);     }     for (int i = 2; i <= n; i++) s[i] = s[i - 1] + g(i - 1, i);     memset(mem, 0x3f, sizeof(mem));     mem[0][0] = 0;     for (int i = 1; i <= n; i++) {       f[i] = kInf;       if (i < n) {         for (int l = 0; l <= len; l++) {           int tmp = mem[l][msk[i + 1] >> (len - l)];           if (tmp != kInf) upd(f[i], tmp + i * len - s[i] - l);         }         for (int l = 0; l <= len; l++)           upd(mem[l][msk[i] & ((1 << l) - 1)], f[i] + s[i + 1] - i * len);       } else {         f[i] = mem[0][0] + i * len - s[i];       }     }     printf( %d n , f[n]);   }


#include <bits/stdc++.h>   using namespace std;   long long int n;   long long int a[100001];   vector<pair<long long int, long long int> > g[100001];   long long int dis[100001];   long long int rem[100001];   long long int dp[100001];   long long int child[100001];   long long int ct = 1;   void simpledfs(long long int x) {     rem[x] = 1;     for (long long int i = 0; i < g[x].size(); i++) {       simpledfs(g[x][i].first);     }   }   void dfs1(long long int x, long long int distance) {     dis[x] = distance;     long long int temp = 0;     long long int mi = 1e9;     long long int pos = -1;     for (long long int i = 0; i < g[x].size(); i++) {       dfs1(g[x][i].first, distance + g[x][i].second);       temp = temp + child[g[x][i].first] + 1;       if (dis[g[x][i].first] < mi) {         mi = dis[g[x][i].first];         pos = g[x][i].first;       }     }     dp[x] = pos;     child[x] = temp;   }   void dfs2(long long int x, long long int minv) {     for (long long int i = 0; i < g[x].size(); i++) {       long long int cur = g[x][i].first;       if ((dis[cur] - minv) > a[cur])         continue;       else {         ct++;         dfs2(cur, min(minv, dis[cur]));       }     }   }   int main() {     cin >> n;     for (long long int i = 1; i <= n; i++) cin >> a[i];     for (long long int i = 2; i <= n; i++) {       long long int p, c;       cin >> p >> c;       g[p].push_back(make_pair(i, c));     }     dfs1(1, 0);     dfs2(1, 0);     cout << n - ct;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e4 + 5, mod = 1e9 + 9, bit = 61, sq = 1000;   const int T = (100001 + sq - 1) / sq;   vector<int> lucky;   bool isl[N] = {0};   int bid[100005], a[100005], beg[T], en[T], lazy[T] = {0};   int fq[T][N];   void create() {     for (int d = 1; d <= 4; d++) {       for (int mask = 0; mask < (1ll << d); mask++) {         int curr = 0;         for (int j = 0; j <= d - 1; j++) {           curr = (curr * 10);           if (mask & (1ll << j)) {             curr += 4;           } else {             curr += 7;           }         }         isl[curr] = 1;         lucky.push_back(curr);       }     }   }   signed main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int t = 1;     memset(fq, 0, sizeof(fq));     create();     while (t--) {       int n, q;       cin >> n >> q;       int j = -1;       for (int i = 1; i <= n; i++) {         cin >> a[i];         if (i % sq == 1) {           bid[i] = ++j;           beg[j] = i;           en[j] = min(i + sq - 1, n);         } else {           bid[i] = j;         }         fq[j][a[i]]++;       }       while (q--) {         string tp;         int l, r;         cin >> tp >> l >> r;         if (tp[0] ==  c ) {           int ans = 0;           if (bid[l] == bid[r]) {             for (int i = l; i <= r; i++) {               ans += isl[a[i] + lazy[bid[l]]];             }           } else {             for (int i = l; i <= en[bid[l]]; i++) {               ans += isl[a[i] + lazy[bid[l]]];             }             for (int i = bid[l] + 1; i <= bid[r] - 1; i++) {               for (auto x : lucky) {                 if (x >= lazy[i]) {                   ans += fq[i][x - lazy[i]];                 }               }             }             for (int i = beg[bid[r]]; i <= r; i++) {               ans += isl[a[i] + lazy[bid[r]]];             }           }           cout << ans <<   n ;         } else {           int d;           cin >> d;           if (bid[l] == bid[r]) {             for (int i = l; i <= r; i++) {               fq[bid[i]][a[i]]--;               fq[bid[i]][a[i] + d]++;               a[i] += d;             }           } else {             for (int i = l; i <= en[bid[l]]; i++) {               fq[bid[i]][a[i]]--;               fq[bid[i]][a[i] + d]++;               a[i] += d;             }             for (int i = bid[l] + 1; i <= bid[r] - 1; i++) {               lazy[i] += d;             }             for (int i = beg[bid[r]]; i <= r; i++) {               fq[bid[i]][a[i]]--;               fq[bid[i]][a[i] + d]++;               a[i] += d;             }           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int inf = 1e9 + 7;   int main() {     long long int n;     cin >> n;     vector<pair<long long int, long long int> > arr(n + 1);     for (long long int i = 1; i < n + 1; i++) {       long long int a, b;       cin >> a >> b;       arr[i].first = a;       arr[i].second = b;     }     vector<long long int> ans(n + 1);     ans[1] = 1;     long long int x, y;     x = arr[1].first;     y = arr[1].second;     if (arr[x].first == y or arr[x].second == y) {       ans[2] = x;       ans[3] = y;       long long int i = 3;       while (i != n) {         i++;         long long int a = arr[ans[i - 2]].first;         long long int b = arr[ans[i - 2]].second;         ans[i] = (a == ans[i - 1] ? b : a);       }       for (int i = 1; i < ans.size(); i++) {         cout << ans[i] <<    ;       }       return 0;     } else {       ans[2] = y;       ans[3] = x;       long long int i = 3;       while (i != n) {         i++;         long long int a = arr[ans[i - 2]].first;         long long int b = arr[ans[i - 2]].second;         ans[i] = (a == ans[i - 1] ? b : a);       }       for (int i = 1; i < ans.size(); i++) {         cout << ans[i] <<    ;       }       return 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[100020], b[100020];   int main() {     int m, n;     scanf( %d%d , &m, &n);     long long sum1 = 0, sum2 = 0, s1 = 0, s2 = 0, Max1 = 0, Max2 = 0;     for (int i = 1; i <= m; i++) {       scanf( %I64d , &a[i]);       sum1 += a[i];       if (a[i] > Max1) {         Max1 = a[i];         s1 = i;       }     }     for (int i = 1; i <= n; i++) {       scanf( %I64d , &b[i]);       sum2 += b[i];       if (b[i] > Max2) {         Max2 = b[i];         s2 = i;       }     }     long long ans1 = sum1 * n, ans2 = sum2 * m;     for (int i = 1; i <= m; i++) {       if (i != s1 && a[i] < sum2) ans2 = ans2 - sum2 + a[i];     }     for (int i = 1; i <= n; i++) {       if (i != s2 && b[i] < sum1) ans1 = ans1 - sum1 + b[i];     }     if (ans1 > ans2)       printf( %I64d n , ans2);     else       printf( %I64d n , ans1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2005;   char mpt[2005][2005];   int dp[maxn][maxn];   int col[maxn];   int main() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; i++) scanf( %s , mpt[i] + 1);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (mpt[i][j] ==  L ) {           int w = i - 1;           if (j - w >= 1) dp[i][j - w] += 1;         } else if (mpt[i][j] ==  R ) {           int w = i - 1;           if (j + w <= m) dp[i][j + w] += 1;         } else if (mpt[i][j] ==  U  && i % 2 == 1) {           col[j] += 1;         } else if (mpt[i][j] ==  D ) {           continue;         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         col[j] += dp[i][j];       }     }     for (int i = 1; i <= m; i++) printf( %d  , col[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k, c, x, y, rem;   char a[501][501];   int v[501][501];   void dfs(int x, int y) {     if (rem == 0 || a[x][y] ==  # ) return;     if (x < 0 || x >= n || y < 0 || y >= m || v[x][y] == 1) return;     v[x][y] = 1;     rem--;     dfs(x + 1, y);     dfs(x, y + 1);     dfs(x, y - 1);     dfs(x - 1, y);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> k;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> a[i][j];         if (a[i][j] ==  . ) {           c++;           if (c == 1) {             x = i;             y = j;           }         }       }     rem = c - k;     dfs(x, y);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (v[i][j] == 0 && a[i][j] ==  . )           cout <<  X ;         else           cout << a[i][j];       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> a[15];   vector<pair<int, int>> all;   int rp[75000];   long long sum[15];   vector<pair<int, int>> loops;   int path[16];   int plen = 0;   bool vis[1 << 15] = {0};   int finstate;   bool dfs(int state, int ps) {     if (state == finstate) return true;     if (vis[state]) return false;     vis[state] = true;     for (int i = ps; i < loops.size(); i++) {       int m = loops[i].first;       if ((state & m) == 0 && !vis[state | m] && dfs(state | m, i + 1)) {         path[plen++] = i;         return true;       }     }     return false;   }   int main() {     ios::sync_with_stdio(false);     int k;     long long total = 0;     cin >> k;     for (int i = 0; i < k; i++) {       int ni;       cin >> ni;       a[i].reserve(ni);       long long s = 0;       for (int j = 0; j < ni; ++j) {         int x;         cin >> x;         s += x;         a[i].push_back(x);         all.push_back(make_pair(x, i));       }       sum[i] = s;       total += s;     }     if (total % k != 0) {       cout <<  No  << endl;       return 0;     }     long long final = total / k;     sort(all.begin(), all.end());     int okmask = 0;     for (int i = 0; i < k; i++)       if (sum[i] == final) okmask |= 1 << i;     int n = all.size();     for (int i = 0; i < n; i++) {       long long v = all[i].first;       long long delta = final - sum[all[i].second];       long long rep = v + delta;       rp[i] = -1;       if (abs(rep) <= 1000000000 && delta != 0) {         auto pos =             lower_bound(all.begin(), all.end(), make_pair<int, int>((int)rep, 0));         if (pos != all.end() && pos->first == rep && pos->second != all[i].second)           rp[i] = pos - all.begin();       }     }     for (int i = 0; i < n; ++i) {       if (rp[i] != -1) {         int mask = (1 << all[i].second);         int p = rp[i];         while (p >= 0 && (mask & (1 << all[p].second)) == 0) {           mask |= (1 << all[p].second);           p = rp[p];         }         if (p == i) {           loops.push_back(make_pair(mask, i));         }       }     }     sort(loops.begin(), loops.end());     loops.erase(unique(loops.begin(), loops.end()), loops.end());     finstate = (1 << k) - 1;     if (dfs(okmask, 0)) {       int moveTo[15];       int val[15];       for (int i = 0; i < k; i++) {         moveTo[i] = i;         val[i] = a[i][0];       }       if (a[0][0] == -174529428) {         cout <<  okmask =   << okmask << endl;         for (int i = 0; i < plen; i++) {           cout <<  mask =   << loops[path[i]].first << endl;         }       }       for (int i = 0; i < plen; i++) {         int p0 = loops[path[i]].second;         int from = p0;         int p = rp[from];         while (p != p0) {           int grp = all[p].second;           moveTo[grp] = all[from].second;           val[grp] = all[p].first;           from = p;           p = rp[from];         }         int sg = all[p].second;         moveTo[sg] = all[from].second;         val[sg] = all[p].first;       }       cout <<  Yes  << endl;       for (int i = 0; i < k; i++) {         cout << val[i] <<     << (moveTo[i] + 1) << endl;       }     } else {       cout <<  No  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   const int mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     long long ans = 0;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       ans += ((i % 5) + m) / 5;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct opt {     vector<set<int>> s;     vector<int> l;     vector<int> r;     int n;     opt(string t) {       n = t.size();       t =  $  + t;       int c = 1;       s.resize(n + 1);       l.assign(n + 1, 1);       r.assign(n + 1, n);       s[1].insert(n);       for (int i = n - 1; i >= 1; i--) {         if (t[i] == t[i + 1])           c++;         else           c = 1;         s[c].insert(i);       }       char val = t[n];       for (int i = n - 1; i >= 1; i--) {         if (t[i] == val)           r[i] = r[i + 1];         else {           val = t[i];           r[i] = i;         }       }       val = t[1];       for (int i = 2; i <= n; i++) {         if (t[i] == val)           l[i] = l[i - 1];         else {           val = t[i];           l[i] = i;         }       }     }     int nxt(int pos, int sz) {       auto it = s[sz].lower_bound(pos);       if (it == s[sz].end()) return -1;       int x = *it;       int a = max(pos, l[x]);       return a + sz;     }   };   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     string s;     cin >> s;     string t = s;     for (int i = 0; i < n; i++)       if (t[i] ==  ? ) t[i] =  0 ;     opt a(t);     t = s;     for (int i = 0; i < n; i++)       if (t[i] ==  ? ) t[i] =  1 ;     opt b(t);     for (int i = 1; i <= n; i++) {       int now = 1;       int res = 0;       while (1) {         int one = a.nxt(now, i);         int two = b.nxt(now, i);         if (one == -1 && two == -1)           break;         else if (one == -1) {           res++;           now = two;         } else if (two == -1) {           res++;           now = one;         } else {           res++;           now = min(one, two);         }       }       cout << res <<    n [i == n];     }     return 0;   }


#include<cstdio>   #include<cstring>   #include<algorithm>   using namespace std;   #define N 100500   int n,is[N],mn=1e9,f1=1,f2=1,as[N],ct,s2[N];   char st[N];   int main()   {    scanf( %s ,st+1);n=strlen(st+1);    st[n+2]= # ;    int s1=0;    for(int i=0;i<=n+2;i++)if(st[i]== # )    {     if(!s1)continue;     mn=min(mn,s1),is[s1-1]=1;     if(s1&1)f2=0;     if(s1>1)f1=0;     s1=0;    }    else s1++;    for(int i=1;i<=mn;i++)    {     if(i==1&&!f1)continue;     if(i==2&&!f2)continue;     if(is[i])continue;     int ls=0;     for(int j=0;j<=n+2;j++)if(st[j]== # )     {      int l1=ls,r1=j-1;ls=j+1;      if(l1>=j)continue;      r1-=i-1;s2[r1]=1;      while(l1<r1)      {       s2[l1]=1;       int v1=l1+i;       if(v1>r1)v1=r1;       if(v1==r1-1)v1--;       l1=v1;      }     }     int s3=0;     for(int j=1;j<=n-i+1;j++)if(s2[j]){if(s3)as[++ct]=s3,s3=0;}else s3++;     if(s3)as[++ct]=s3;     printf( %d n ,ct);for(int i=1;i<=ct;i++)printf( %d  ,as[i]);     return 0;    }    printf( -1 n );   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   int a[10], n;   char s1, s2;   int main() {     a[0] = 118, a[1] = 60, a[2] = 94, a[3] = 62;     while (cin >> s1 >> s2) {       scanf( %d , &n);       n %= 4;       int k;       for (int i = 0; i < 4; i++)         if ((int)s1 == a[i]) {           k = i;           break;         }       int kk = (k + n) % 4, kkk = ((k - n) % 4 + 4) % 4;       if (a[kk] == (int)s2 && kkk == kk)         printf( undefined n );       else if (a[kk] == (int)s2)         printf( cw n );       else         printf( ccw n );     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:67108864 )   const int MOD = 1000000007;   const long long INF = 3000000000000000001;   const double EPS = 1e-6;   const int HASH_POW = 29;   const double PI = acos(-1.0);   using namespace std;   double workTime() { return double(clock()) / CLOCKS_PER_SEC; }   void my_return(int code) { exit(code); }   int cnt[1000000];   void add(string s) {     reverse(s.begin(), s.end());     int len = s.length();     int ptr = 0;     for (int i = 0; i < 18; ++i) {       int bit;       if (i < len)         bit = (s[i] -  0 ) % 2;       else         bit = 0;       ptr += bit * (1 << i);     }     ++cnt[ptr];   }   void destroy(string s) {     reverse(s.begin(), s.end());     int len = s.length();     int ptr = 0;     for (int i = 0; i < 18; ++i) {       int bit;       if (i < len)         bit = (s[i] -  0 ) % 2;       else         bit = 0;       ptr += bit * (1 << i);     }     --cnt[ptr];   }   int count(string s) {     reverse(s.begin(), s.end());     int len = s.length();     int ptr = 0;     for (int i = 0; i < 18; ++i) {       int bit;       if (i < len)         bit = (s[i] -  0 ) % 2;       else         bit = 0;       ptr += bit * (1 << i);     }     return cnt[ptr];   }   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     mt19937 mt_rand(time(0));     int q;     cin >> q;     while (q--) {       char c;       string v;       cin >> c >> v;       if (c ==  + )         add(v);       else if (c ==  - )         destroy(v);       else if (c ==  ? )         cout << count(v) << endl;       else         return 1;     }     my_return(0);   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, k, i, j, a[1000000], b[1000000], x, res = 1;   int main() {     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];     }     for (i = 0; i < n; i++) {       cin >> x;       b[x] = i;     }     while (b[a[res]] > b[a[res - 1]]) {       res++;     }     res = n - res;     cout << res;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,no-stack-protector )   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native )   using namespace std;   struct Dinic {     struct Edge {       long long flow;       int to, next;     };     vector<Edge> edges;     vector<int> adia, at, dist;     int S, D;     void add_Edge(int from, int to, long long cap) {       edges.push_back({cap, to, adia[from]});       adia[from] = edges.size() - 1;       edges.push_back({0, from, adia[to]});       adia[to] = edges.size() - 1;     }     bool bfs() {       queue<int> q;       fill(dist.begin(), dist.end(), 1e9);       dist[S] = 0;       q.push(S);       while (!q.empty()) {         int x = q.front();         q.pop();         for (int i = adia[x]; i != -1; i = edges[i].next) {           if (dist[edges[i].to] > dist[x] + 1 && edges[i].flow) {             dist[edges[i].to] = 1 + dist[x];             q.push(edges[i].to);           }         }       }       return dist[D] < 1e9;     }     long long dfs(int nod, long long fmax) {       if (nod == D) return fmax;       while (at[nod] != -1) {         Edge& e = edges[at[nod]];         long long f;         if (dist[e.to] == dist[nod] + 1 && e.flow &&             (f = dfs(e.to, min(fmax, e.flow)))) {           e.flow -= f;           edges[at[nod] ^ 1].flow += f;           return f;         }         at[nod] = edges[at[nod]].next;       }       return 0;     }     long long GetFlow() {       long long f = 0;       while (bfs()) {         at = adia;         while (long long x = dfs(S, 1e18)) f += x;       }       return f;     }     Dinic(int n = 0, int s = 0, int d = 0) {       S = s, D = d;       at = dist = adia = vector<int>(n + 1, -1);     }   };   const int NMAX = 110;   int dmin[NMAX][NMAX];   struct Object {     int cost, nod, power, id, fuel, atac;   };   vector<Object> ships, bases;   vector<pair<int, int>> dependencies;   int n;   int best_cost[NMAX];   void get_cost_nave() {     fill(best_cost, best_cost + n + 1, -1);     vector<int> nv(ships.size());     iota(nv.begin(), nv.end(), 0);     sort(nv.begin(), nv.end(),          [](int a, int b) { return ships[a].power < ships[b].power; });     sort(bases.begin(), bases.end(),          [](Object a, Object b) { return a.power < b.power; });     int it = 0;     for (auto i : nv) {       while (it != bases.size() && ships[i].power >= bases[it].power) {         best_cost[bases[it].nod] = max(best_cost[bases[it].nod], bases[it].cost);         it++;       }       int best = -1;       for (int nod = 1; nod <= n; nod++)         if (dmin[nod][ships[i].nod] <= ships[i].fuel)           best = max(best, best_cost[nod]);       ships[i].atac = best;     }   }   void read() {     int m, a, b;     scanf( %d%d , &n, &m);     fill(dmin[0], dmin[n + 1], 1e9);     for (int i = 1; i <= n; i++) dmin[i][i] = 0;     while (m--) {       scanf( %d%d , &a, &b);       dmin[a][b] = dmin[b][a] = 1;     }     for (int k = 1; k <= n; k++)       for (int i = 1; i <= n; i++)         for (int j = 1; j <= n; j++)           dmin[i][j] = min(dmin[i][j], dmin[i][k] + dmin[k][j]);     int s, k;     scanf( %d%d%d , &s, &b, &k);     ships.resize(s);     bases.resize(b);     dependencies.resize(k);     for (auto& i : ships) {       scanf( %d%d%d%d , &i.nod, &i.power, &i.fuel, &i.cost);       i.fuel = min(i.fuel, 1000000);     }     for (auto& i : bases) scanf( %d%d%d , &i.nod, &i.power, &i.cost);     for (auto& i : dependencies) scanf( %d%d , &i.first, &i.second);   }   int main() {     read();     get_cost_nave();     long long ans = 0;     int cnt = 2, S = 1, D = 2;     const long long inf = 1e18;     for (auto& i : dependencies) i.first--, i.second--;     for (int i = 0; i < ships.size(); i++) ships[i].id = ++cnt;     Dinic flow(cnt + 1, S, D);     for (auto i : ships) {       if (i.atac == -1)         flow.add_Edge(i.id, D, inf);       else {         int castig = i.atac - i.cost;         if (castig > 0) {           ans += castig;           flow.add_Edge(S, i.id, castig);         } else           flow.add_Edge(i.id, D, -castig);       }     }     for (auto i : dependencies)       flow.add_Edge(ships[i.first].id, ships[i.second].id, inf);     ans -= flow.GetFlow();     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int h, cen;     long long n, num;     while (scanf( %d%I64d , &h, &n) != EOF) {       num = (long long)pow(2.0, h) - 1 + n;       int flag;       long long ans = 0;       if (num % 2 == 0)         flag = 0;       else         flag = 1;       cen = h - 1;       while (1) {         ans++;         num /= (long long)2;         if (num % 2 == 0) {           if (flag == 0) ans += (long long)pow(2.0, h - cen) - (long long)1.0;           flag = 0;         } else {           if (flag == 1) ans += (long long)pow(2.0, h - cen) - (long long)1.0;           flag = 1;         }         if (num == 1) break;         cen--;       }       printf( %I64d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int used[110];   int v[110][110];   vector<int> ans;   int main() {     int n, x;     memset(used, 0, sizeof(used));     cin >> n;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cin >> x;         if (x == 1) {           used[i] = 1;         } else if (x == 2) {           used[j] = 1;         } else if (x == 3) {           used[i] = used[j] = 1;         }       }     }     for (int i = 1; i <= n; i++) {       if (!used[i]) {         ans.push_back(i);       }     }     cout << ans.size() << endl;     if (!ans.size()) return 0;     bool cond = true;     for (int i = 0; i < (int)ans.size(); i++) {       if (!cond) cout <<    ;       cout << ans[i];       cond = false;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long P = 998244353;   double PI = 3.141592653589793;   const int sqr = 500;   const int arrn = 2e5 + 5;   const int arrn2 = 1e3 + 5;   long long inv[arrn] = {};   long long fac[arrn] = {};   void prefinv(int n, int p) {     inv[0] = 1;     inv[1] = 1;     fac[1] = 1;     fac[0] = 1;     for (int i = 2; i <= n; i++) {       fac[i] = fac[i - 1] * i;       fac[i] %= p;       inv[i] = (p - (((p / i) * inv[p % i]) % p)) % p;     }     for (int i = 2; i <= n; i++) {       inv[i] = inv[i] * inv[i - 1];       inv[i] %= p;       if (inv[i] < 0) inv[i] += p;     }   }   long long comb(int n, int r, int mod) {     if (n < r)       return 0;     else {       long long temp = fac[n];       temp *= inv[r];       temp %= mod;       temp *= inv[n - r];       temp %= mod;       return temp;     }   }   long long binpow(long long a, long long n, long long p) {     long long ret = 1;     while (n) {       if (n & 1) ret *= a;       ret %= p;       a *= a;       a %= p;       n /= 2;     }     return ret;   }   void solve() {     prefinv(arrn - 1, P);     int n;     long long k;     cin >> n >> k;     if (k > n - 1) {       cout << 0;       return;     }     long long out = 0;     for (int i = k; i < n; i++) {       if ((i - k) & 1) {         out = (out - ((comb(n - k, i - k, P) * binpow(n - i, n, P)) % P)) % P;       } else {         out = (out + ((comb(n - k, i - k, P) * binpow(n - i, n, P)) % P)) % P;       }     }     out = (out * comb(n, k, P)) % P;     if (k > 0) out *= 2;     out %= P;     cout << (out + P) % P;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int T = 1;     for (int c = 0; c < T; c++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     int dif;     cin >> s >> dif;     set<char> st;     for (int i = (0); i < s.length(); i++) st.insert(s[i]);     if (s.length() < dif) {       cout <<  impossible  << endl;     } else if (st.size() >= dif)       cout << 0;     else       cout << dif - st.size() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int T;     cin >> T;     while (T--) {       int a, b;       cin >> a >> b;       int a1 = 2 * min(a, b);       int a2 = max(a, b);       cout << pow(max(a1, a2), 2) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int i, j, viz[1005], n;   int sol[100005], nrsol;   int main(void) {     cin >> n;     for (i = 2; i <= n; ++i)       if (viz[i] == 0) {         int nrp = i;         int put = nrp;         while (put <= n) {           sol[++nrsol] = put;           put *= nrp;         }         for (j = 2; j * i <= n; ++j) viz[i * j] = 1;       }     cout << nrsol <<   n ;     for (i = 1; i <= nrsol; ++i) cout << sol[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000005;   long long a00, a01, a10, a11;   long long newton(long long n) { return n * (n - 1) / 2; }   long long sqrNewton(long long x) {     long long n = 0;     while (newton(n) < x) n++;     return newton(n) == x ? n : -1;   }   int W[MAXN];   bool policz(long long zero, long long one) {     long long ile = zero * one;     long long n = zero + one;     if (zero < 0 || one < 0 || (newton(n) != a00 + a01 + a10 + a11) || ile < a01)       return false;     int suf = 0;     while (one > 0 && ile - one >= a01) {       suf++;       ile -= one;       if (suf > zero) return false;     }     for (int i = 1; i <= n; i++) W[i] = 1;     for (int i = 1; i <= zero - suf; i++) W[i] = 0;     for (int i = 1; i <= suf; i++) W[n - i + 1] = 0;     long long p = ile - a01;     if (p > 0) {       W[zero - suf + p] = 0;       W[zero - suf] = 1;     }     for (int i = 1; i <= n; i++) cout << W[i];     return true;   }   int main() {     cin >> a00 >> a01 >> a10 >> a11;     if (a00 == 0 && a01 == 0 && a10 == 0 && a11 == 0) {       cout <<  0 ;       return 0;     }     vector<long long> zero;     vector<long long> one;     zero.push_back(sqrNewton(a00));     one.push_back(sqrNewton(a11));     if (a00 == 0) zero.push_back(1);     if (a11 == 0) one.push_back(1);     int d1 = zero.size();     int d2 = one.size();     for (int i = 0; i < d1; i++)       for (int j = 0; j < d2; j++)         if (policz(zero[i], one[j])) return 0;     cout <<  Impossible ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2e6;   const long long LINF = 8e18;   const int MOD = 1e9 + 7;   const long double EPS = 1e-9;   int n;   vector<long long> a;   struct triplet {     int ti, i, j;   };   vector<struct triplet> s;   int cnt[INF];   int main() {     scanf( %d , &n);     ;     if (n == 1) {       printf( 0 );       return 0;     }     for (int i = 0; i < INF; i++) cnt[i] = 0;     for (int i = 0; i < n; i++) {       long long X;       scanf( %lld , &X);       a.push_back(X);       cnt[X]++;     }     int mx = -1, mx_i = 0;     for (int i = 0; i < n; i++) mx = max(mx, cnt[a[i]]);     for (int i = 0; i < n; i++)       if (mx == cnt[a[i]]) mx_i = i;     for (int i = mx_i + 1; i < n; i++) {       if (a[i] < a[mx_i]) s.push_back({1, i, i - 1});       if (a[i] > a[mx_i]) s.push_back({2, i, i - 1});     }     for (int i = mx_i - 1; i > -1; i = i - 1) {       if (a[i] < a[mx_i]) s.push_back({1, i, i + 1});       if (a[i] > a[mx_i]) s.push_back({2, i, i + 1});     }     printf( %ld , s.size());     for (struct triplet it : s) {       printf(  n );       printf( %d %d %d , it.ti, it.i + 1, it.j + 1);     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int n, b, i, rez, a[2013], minn[2013], maxx[2013];   int main() {     minn[0] = 123456890;     cin >> n >> b;     for (i = 1; i <= n; ++i) {       cin >> a[i];       minn[i] = min(minn[i - 1], a[i]);     }     for (i = n; i >= 1; --i) {       maxx[i] = max(maxx[i + 1], a[i]);     }     for (i = 1; i <= n; ++i) {       rez = max(rez, b / minn[i] * maxx[i] + b % minn[i]);     }     cout << rez;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     if (a.second < b.second) return true;     if (a.second == b.second) return a.first < b.first;     return false;   }   bool isprime(int n) {     if (n <= 1) return false;     for (int i = 2; i <= sqrt(n); i++)       if (n % i == 0) return false;     return true;   }   bool isPowerOfTwo(int n) {     if (n == 0) return false;     return (ceil(log2(n)) == floor(log2(n)));   }   int gcd(int a, int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long int lcm(long long int a, long long int b) {     return (a / gcd(a, b)) * b;   }   long long int ncr(long long int n, long long int r) {     if (r == 0) return 1;     r = min(r, n - r);     long long int ans = 1;     for (int i = 1; i <= r; i++) {       ans *= (n - i + 1);       ans /= i;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     while (t--) {       int n, m;       cin >> n >> m;       vector<pair<int, int>> v;       int x, y;       for (int i = 0; i < m; i++) {         cin >> x >> y;         v.push_back(make_pair(x, y));       }       int ans = 0;       if (v[0].first != 1) {         cout << 1 <<     << 0 <<   n ;       } else {         int dayno = -1;         for (int i = 1; i < v.size(); i++) {           if (v[i].first == v[i - 1].second) {             ans = 2;             dayno = v[i].first;             int j = i + 1;             while (j < v.size() && v[j].first == v[i].first) ans++, j++;             break;           } else if (v[i].first != v[i - 1].second + 1) {             dayno = v[i - 1].second + 1;             ans = 0;             break;           }         }         if (dayno != -1)           cout << dayno <<     << ans <<   n ;         else {           if (v[v.size() - 1].second != n)             cout << v[v.size() - 1].second + 1 <<     << 0 <<   n ;           else             cout <<  OK                   <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<array<int, 2>> a(n);     map<int, int> hhor;     map<int, int> hver;     set<array<int, 2>> hor[n];     set<array<int, 2>> ver[n];     int idhor = 0, idver = 0;     for (int i = 0; i < n; i++) {       auto &[x, y] = a[i];       cin >> x >> y;       if (hhor.find(y) == hhor.end()) hhor[y] = idhor++;       if (hver.find(x) == hver.end()) hver[x] = idver++;       hor[hhor[y]].insert({x, i});       ver[hver[x]].insert({y, i});     }     auto one = [&](int x, int y) -> array<int, 2> {       array<int, 2> tmp = {-1, -1};       int idx = 0;       if (hhor.find(y) == hhor.end()) return tmp;       int id = hhor[y];       auto it = hor[id].lower_bound({x, -1});       if (it != hor[id].end()) tmp[idx++] = ((*it)[1]);       if (it != hor[id].begin()) {         it--;         tmp[idx++] = ((*it)[1]);       }       return tmp;     };     auto two = [&](int x, int y) -> array<int, 2> {       array<int, 2> tmp = {-1, -1};       int idx = 0;       if (hver.find(x) == hver.end()) return tmp;       int id = hver[x];       auto it = ver[id].lower_bound({y, -1});       if (it != ver[id].end()) tmp[idx++] = ((*it)[1]);       if (it != ver[id].begin()) {         it--;         tmp[idx++] = ((*it)[1]);       }       return tmp;     };     vector<vector<vector<array<int, 4>>>> pre(         n, vector<vector<array<int, 4>>>(n, vector<array<int, 4>>(2)));     auto ini = [&](int x, int y, int i, int j, int e) {       array<int, 4> &now = pre[i][j][e];       array<int, 2> tmp = one(x, y);       now[0] = tmp[0];       now[1] = tmp[1];       tmp = two(x, y);       now[2] = tmp[0];       now[3] = tmp[1];     };     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         auto [x1, y1] = a[i];         auto [x2, y2] = a[j];         if (x1 == x2)           ini(x1, (y1 + y2) / 2, i, j, 0);         else if (y1 == y2)           ini((x1 + x2) / 2, y1, i, j, 0);         else {           ini(x1, y2, i, j, 0);           ini(x2, y1, i, j, 1);         }       }     }     const long long lim = (1ll << 31);     long long l = 0, r = lim;     while (l < r - 1) {       int m = (l + r) / 2;       vector<int> v[n];       for (int i = 0; i < n; i++) {         for (int j = i + 1; j < n; j++) {           auto [x1, y1] = a[i];           auto [x2, y2] = a[j];           if ((x1 == x2 && abs(y1 - y2) <= m) ||               (y1 == y2 && abs(x1 - x2) <= m)) {             v[i].push_back(j);             v[j].push_back(i);           }         }       }       vector<int> c(n, -1);       int now = 0;       for (int i = 0; i < n; i++) {         if (c[i] != -1) continue;         c[i] = now;         queue<int> q;         q.push(i);         while (!q.empty()) {           int e = q.front();           q.pop();           for (int to : v[e]) {             if (c[to] != -1) continue;             c[to] = now;             q.push(to);           }         }         now++;       }       bool ok = (now == 1);       if (now <= 4) {         auto go = [&](int x, int y, int i, int j, int e) {           vector<int> z(now, 0);           for (int b : pre[i][j][e])             if (b != -1 && abs(x - a[b][0]) + abs(y - a[b][1]) <= m) z[c[b]] = 1;           bool b = 1;           for (int k = 0; k < now; k++) b &= z[k];           if (b) ok = 1;         };         for (int i = 0; i < n; i++) {           for (int j = i + 1; j < n; j++) {             auto [x1, y1] = a[i];             auto [x2, y2] = a[j];             if (x1 == x2)               go(x1, (y1 + y2) / 2, i, j, 0);             else if (y1 == y2)               go((x1 + x2) / 2, y1, i, j, 0);             else {               go(x1, y2, i, j, 0);               go(x2, y1, i, j, 1);             }           }         }       }       if (ok)         r = m;       else         l = m;     }     if (r < lim)       cout << r <<   n ;     else       cout <<  -1 n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   int dp[100005], x[105], s[105];   bool vis[100005];   int main() {     int n, m, minv = INF;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &x[i], &s[i]);       minv = min(minv, x[i]);       for (int j = max(x[i] - s[i], 1); j <= min(x[i] + s[i], m); ++j) {         vis[j] = true;       }     }     memset(dp, INF, sizeof(dp));     dp[0] = 0;     for (int i = 1; i <= m; ++i) {       if (vis[i])         dp[i] = min(dp[i - 1], dp[i]);       else {         if (i >= minv) dp[i] = min(dp[i], dp[i - 1] + 1);         for (int j = 0; j < n; ++j) {           if (x[j] > i) {             int tem = x[j] - s[j] - i;             dp[min(m, x[j] + s[j] + tem)] =                 min(dp[min(x[j] + s[j] + tem, m)], dp[i - 1] + tem);           }         }       }     }     printf( %d n , dp[m]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:16777216 )   const int fin = 111110;   struct T {     int cnt, val, l, r, mx, y, ans;     T() : cnt(0), val(-1), l(fin), r(fin), mx(-1000000000), y(-1), ans(-1) {}   };   T a[111111];   void upd(int v) {     a[v].cnt = 1 + a[a[v].r].cnt + a[a[v].l].cnt;     a[v].mx = max(a[v].val, max(a[a[v].r].mx, a[a[v].l].mx));   }   void split(int v, int &l, int &r, int key) {     l = r = fin;     if (v == fin) return;     if (key && (a[v].l == -1 || a[a[v].l].cnt < key)) {       l = v;       split(a[v].r, a[v].r, r, key - a[a[v].l].cnt - 1);       upd(l);     } else {       r = v;       split(a[v].l, l, a[v].l, key);       upd(r);     }   }   int merge(int l, int r) {     if (l == fin) return r;     if (r == fin) return l;     if (a[l].y < a[r].y) {       a[l].r = merge(a[l].r, r);       upd(l);       return l;     } else {       a[r].l = merge(l, a[r].l);       upd(r);       return r;     }   }   int root, m;   void push(int val) {     a[m].y = (rand() << 16) + rand();     a[m].val = val;     a[m].cnt = 1;     a[m].mx = val;     a[m].ans = m + 1;     root = merge(root, m++);   }   int tot = 0;   void print(int v) {     if (v == fin) return;     if (a[v].l != fin) print(a[v].l);     {       if (tot)         printf(   );       else         tot = 1;       printf( %d , a[v].ans);     }     if (a[v].r != fin) print(a[v].r);   }   int fmin(int st, int fn) {     int r1, r2, r3, r4;     split(root, r1, r2, st);     split(r2, r3, r4, fn - st);     int rez = (r3 != -1) ? (a[r3].mx) : (-1000000000);     root = merge(r1, r3);     root = merge(root, r4);     return rez;   }   int n, A, C;   int find(int v, int maxim, int value, int lft) {     if (a[a[v].r].cnt + 1 > maxim)       return find(a[v].r, maxim, value, lft + 1 + a[a[v].l].cnt);     else if (a[a[v].r].mx > value)       return find(a[v].r, maxim, value, lft + 1 + a[a[v].l].cnt);     else if (a[v].val > value)       return lft + a[a[v].l].cnt;     else       return find(a[v].l, maxim - 1 - a[a[v].r].cnt, value, lft);   }   int main() {     a[fin].cnt = 0;     a[fin].mx = -1000000000;     scanf( %d , &n);     for (int(i) = 0; (i) < (n); ++(i)) {       scanf( %d%d , &A, &C);       if (i) {         int high = max(i - C, 0);         if (fmin(high, i) > A) high = find(root, C, A, 0) + 1;         int r1, r2;         split(root, r1, r2, high);         root = r1;         push(A);         root = merge(root, r2);       } else {         a[0].val = A;         a[0].ans = 1;         a[0].cnt = 1;         a[0].mx = A;         root = 0;         m = 1;       }     }     print(root);     cout << endl;   }