#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   struct line {     int sx, sy, ex, ey;     friend bool operator<(const line &A, const line &B) { return A.sx < B.sx; }   };   map<char, line> mp;   int n, m;   char s[2005][2005], p[2005][2005], mxx;   void init() {     for (char x =  a ; x <=  z ; ++x) {       mp[x].sx = 0, mp[x].sy = 0;       mp[x].ex = 0, mp[x].ey = 0;     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         p[i][j] =  . ;       }     }   }   bool check() {     for (char x =  a ; x <= mxx; ++x) {       if (mp[x].sx != mp[x].ex and mp[x].sy != mp[x].ey) {         return false;       }     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (s[i][j] != p[i][j]) return false;       }     }     return true;   }   void slove() {     cin >> n >> m;     mxx = 0;     init();     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         cin >> s[i][j];         mxx = max(mxx, s[i][j]);         if (mp[s[i][j]].sx == 0) {           mp[s[i][j]].sx = i;           mp[s[i][j]].sy = j;           mp[s[i][j]].ex = i;           mp[s[i][j]].ey = j;         } else {           mp[s[i][j]].ex = i;           mp[s[i][j]].ey = j;         }       }     }     for (char x =  a ; x <= mxx; ++x) {       if (mp[x].sx == 0) {         mp[x] = mp[mxx];       }       for (int xx = mp[x].sx; xx <= mp[x].ex; ++xx) {         for (int yy = mp[x].sy; yy <= mp[x].ey; ++yy) {           p[xx][yy] = x;         }       }     }     if (!check()) {       cout <<  NO  << endl;       return;     }     cout <<  YES  << endl;     cout << max(0, mxx -  a  + 1) << endl;     for (char x =  a ; x <= mxx; ++x) {       cout << mp[x].sx <<     << mp[x].sy <<    ;       cout << mp[x].ex <<     << mp[x].ey << endl;     }     return;   }   signed main() {     ios ::sync_with_stdio(false);     int T;     cin >> T;     while (T--) {       slove();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct disjoint_set {     vector<int> p;     disjoint_set(int n) : p(n, -1) {}     bool share(int a, int b) { return root(a) == root(b); }     int sz(int u) { return -p[root(u)]; }     int root(int u) { return p[u] < 0 ? u : p[u] = root(p[u]); }     bool merge(int u, int v) {       u = root(u), v = root(v);       if (u == v) return false;       p[u] += p[v], p[v] = u;       return true;     }   };   int main() {     cin.tie(0)->sync_with_stdio(0);     int n;     string s;     cin >> n >> s;     vector<array<int, 2>> dp(n + 1);     vector<int> reach(n);     for (auto i = n - 1; i >= 0; --i) {       if (s[i] !=  1 ) {         dp[i][0] = dp[i + 1][0] + 1;       }       if (s[i] !=  0 ) {         dp[i][1] = dp[i + 1][1] + 1;       }       reach[i] = max(dp[i][0], dp[i][1]);     }     disjoint_set dsu(n + 1);     for (auto l = 1; l <= n; ++l) {       int res = 0;       for (auto i = 0; i < n;) {         if (reach[i] < l) {           dsu.merge(i + 1, i);           i = dsu.root(i);         } else {           res += reach[i] / l;           i += reach[i] / l * l;         }       }       cout << res <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[1001][7];   int n, a[30], p, m, ch, j, mn, mx, val, o;   long long sum;   struct chestie {     int c, fr, ok, nr;   } v[30];   int pow(int N, int E);   bool comp(chestie X, chestie Y);   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin.get();       cin >> s[i];       m = strlen(s[i]);       for (int j = 0; j < m; j++) {         ch = s[i][j] -  a ;         if (ch > mx) mx = ch;         if (v[ch].fr == 0) o++;         v[ch].fr += (pow(10, (m - j - 1)));         v[ch].c = ch;         if (j == 0 && m >= 1) v[ch].ok = 1;       }     }     sort(v + 0, v + mx + 1, comp);     val = 1;     j = 1;     for (int i = 0; i < o; i++) {       if (v[i].ok || j == 0) {         v[i].nr = val;         sum += (v[i].fr * val);         val++;       } else if (j)         v[i].nr = 0, j = 0, a[v[i].c] = 0;     }     cout << sum;     return 0;   }   int pow(int N, int E) {     int R = 1;     while (E) R = R * N, E--;     return R;   }   bool comp(chestie X, chestie Y) {     if (X.fr > Y.fr) return 1;     if (X.fr < Y.fr) return 0;     if (X.c < Y.c) return 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long read() {     long long f = 1, x = 0;     char s = getchar();     while (s <  0  || s >  9 ) {       if (s ==  - ) f = -f;       s = getchar();     }     while (s >=  0  && s <=  9 ) {       x = x * 10 + (s -  0 );       s = getchar();     }     return f * x;   }   int n, m, i, j, s, o, k;   vector<int> g[500005];   int cnt;   vector<int> bu[500005];   int bl[500005], rk[500005];   int vv[500005];   int d[500005], dpd[500005];   void dfs0(int x, int ff) {     d[x] = d[ff] + 1;     dpd[x] = 0x3f3f3f3f;     for (int i = 0; i < (int)g[x].size(); i++) {       int y = g[x][i];       if (y != ff) {         if (!d[y]) {           dfs0(y, x);           dpd[x] = min(dpd[x], dpd[y]);         } else {           dpd[x] = min(dpd[x], d[y]);         }       }     }     if (dpd[x] > d[x])       vv[x] = -1;     else       vv[x] = 1;     return;   }   void dfs00(int x, int ff) {     if (dpd[x] == d[x]) {       bu[++cnt].push_back(x);       rk[x] = 0;       bl[x] = cnt;     } else if (dpd[x] < d[x]) {       bl[x] = bl[ff];       bu[bl[x]].push_back(x);       rk[x] = (int)bu[bl[x]].size() - 1;     }     for (int i = 0; i < (int)g[x].size(); i++) {       int y = g[x][i];       if (y != ff && d[y] == d[x] + 1) {         dfs00(y, x);       }     }     return;   }   int dp1[500005], dp2[500005], dp0[500005];   vector<int> sm[500005];   void dfs(int x, int ff, int op) {     if (!op) {       dp1[x] = 0;       int nw = 0;       for (int i = 0; i < (int)g[x].size(); i++) {         int y = g[x][i];         sm[x].push_back(nw);         if (y != ff) {           dfs(y, x, vv[y] == 1 ? 1 : 0);           dp1[x] = max(dp1[x], dp1[y] + 1);           nw = max(nw, dp1[y] + 1);         }       }       return;     }     int id = bl[x], le = (int)bu[id].size();     int md = rk[x], ll = (md + le - (le / 2)) % le;     for (int ii = 0; ii < le; ii++) {       int t = bu[id][ii], nw = 0;       for (int i = 0; i < (int)g[t].size(); i++) {         int y = g[t][i];         sm[t].push_back(nw);         if (y != ff && bl[y] != id) {           dfs(y, t, vv[y] == 1 ? 1 : 0);           dp0[t] = max(dp0[t], dp1[y] + 1);           nw = max(nw, dp1[y] + 1);         }       }       dp1[t] = dp0[t];     }     for (int ii = (ll + 1) % le; ii != (md + 1) % le; ii = (ii + 1) % le) {       int pr = bu[id][(ii + le - 1) % le], t = bu[id][ii];       dp1[t] = max(dp1[t], dp1[pr] + 1);     }     for (int ii = (ll + le - 1) % le; ii != (md + le - 1) % le;          ii = (ii + le - 1) % le) {       int pr = bu[id][(ii + 1) % le], t = bu[id][ii];       if (ii != (ll + le - 1) % le) {         dp1[t] = max(dp1[t], dp1[pr] + 1);       }     }     return;   }   int as[500005];   int q[500005], hd, tl;   int ds(int x, int y, int le) { return (y + le - x) % le; }   void dfs2(int x, int ff, int op, int fdp) {     if (!op) {       dp2[x] = fdp + 1;       as[x] = max(dp2[x], dp1[x]);       int nw = 0;       for (int i = (int)g[x].size() - 1; i >= 0; i--) {         int y = g[x][i];         if (y != ff) {           dfs2(y, x, vv[y] == 1 ? 1 : 0, max(dp2[x], max(sm[x][i], nw)));           nw = max(nw, dp1[y] + 1);         }       }       return;     }     dp0[x] = max(dp0[x], fdp + 1);     int id = bl[x], le = (int)bu[id].size();     hd = 1;     tl = 0;     int ll2 = (le - (le / 2)) % le;     for (int i = ll2; i != 0; i = (i + 1) % le) {       int t = bu[id][i];       while (tl >= hd && dp0[bu[id][q[tl]]] + ds(q[tl], i, le) < dp0[t]) tl--;       q[++tl] = i;     }     for (int i = 0; i < le; i++, ll2 = (ll2 + 1) % le) {       int t = bu[id][i];       if (tl >= hd) dp2[t] = max(dp2[t], dp0[bu[id][q[hd]]] + ds(q[hd], i, le));       while (tl >= hd && dp0[bu[id][q[tl]]] + ds(q[tl], i, le) < dp0[t]) tl--;       q[++tl] = i;       if (tl >= hd && q[hd] == ll2) hd++;     }     ll2 = (le - 1 + le / 2) % le;     hd = 1;     tl = 0;     for (int i = ll2; i != le - 1; i = (i + le - 1) % le) {       int t = bu[id][i];       while (tl >= hd && dp0[bu[id][q[tl]]] + ds(i, q[tl], le) < dp0[t]) tl--;       q[++tl] = i;     }     for (int i = le - 1; i >= 0; i--, ll2 = (ll2 + le - 1) % le) {       int t = bu[id][i];       if (tl >= hd) dp2[t] = max(dp2[t], dp0[bu[id][q[hd]]] + ds(i, q[hd], le));       while (tl >= hd && dp0[bu[id][q[tl]]] + ds(i, q[tl], le) < dp0[t]) tl--;       q[++tl] = i;       if (tl >= hd && q[hd] == ll2) hd++;     }     for (int ii = 0; ii < le; ii++) {       int t = bu[id][ii], nw = 0;       as[t] = max(dp2[t], dp0[t]);       for (int i = (int)g[t].size() - 1; i >= 0; i--) {         int y = g[t][i], dpp = dp2[t];         if (t == x) dpp = max(dpp, fdp + 1);         if (y != ff && bl[y] != id) {           dfs2(y, t, vv[y] == 1 ? 1 : 0, max(dpp, max(nw, sm[t][i])));           nw = max(nw, dp1[y] + 1);         }       }     }     return;   }   int main() {     n = read();     m = read();     for (int i = 1; i <= m; i++) {       s = read();       o = read();       g[s].push_back(o);       g[o].push_back(s);     }     dfs0(1, 0);     cnt = 0;     dfs00(1, 0);     dfs(1, 0, vv[1] == 1 ? 1 : 0);     dfs2(1, 0, vv[1] == 1 ? 1 : 0, -1);     for (int i = 1; i <= n; i++) {       printf( %d  , as[i]);     }     putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   long long gMin = 1e12, gMax = -1e12, rMin = 1e12, rMax = -1e12, bMin = 1e12,             bMax = -1e12, lasty = -1e12, ret = 0;   vector<long long> notGreen[2];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout << fixed << setprecision(18);     cin >> N;     for (int i = 0; i <= N; i++) {       long long x = 1e12;       char c =  G ;       if (i < N) cin >> x >> c;       if (c ==  G ) {         if (i < N) {           gMin = min(gMin, x);           gMax = max(gMax, x);         }         long long add1 = x - lasty, add2 = add1 * 2;         if (add1 >= 1e10) add1 = 0;         for (int j = 0; j < 2; j++) {           if (notGreen[j].size() == 0) continue;           add1 += x - lasty;           long long maxi =               max(notGreen[j].front() - lasty, x - notGreen[j].back());           for (int k = 0; k < notGreen[j].size() - 1; k++)             maxi = max(maxi, notGreen[j][k + 1] - notGreen[j][k]);           add1 -= maxi;           notGreen[j] = vector<long long>();         }         ret += min(add1, add2);         lasty = x;       } else if (c ==  R ) {         notGreen[0].push_back(x);         rMin = min(rMin, x);         rMax = max(rMax, x);       } else {         notGreen[1].push_back(x);         bMin = min(bMin, x);         bMax = max(bMax, x);       }     }     if (gMin > gMax) ret = max(0ll, rMax - rMin) + max(0ll, bMax - bMin);     cout << ret <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y;   };   int n, m, d[5005];   node a[5005];   bool cmp(const node &x, const node &y) {     return x.x == y.x ? x.y < y.y : x.x < y.x;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d%d , &a[i].x, &a[i].y);     sort(a + 1, a + 1 + n, cmp);     memset(d, 0, sizeof(d));     int ans = min(a[1].x, a[1].y);     for (int i = 2; i <= n; i++)       if (ans > a[i].y)         ans = a[i].x;       else         ans = a[i].y;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   template <typename T>   void Print(T a[], T n) {     for (int i = 0; i < n; i++) {       cout << a[i] <<    ;     }     cout << endl;   }   void solve() {     int n;     cin >> n;     string s, t;     cin >> s >> t;     if (s == t) {       cout << 0 <<   n ;       return;     }     int same1 = 0, diff1 = 0, same = 0;     for (int i = 0; i < n; i++) {       if (s[i] == t[i]) {         same++;         if (s[i] ==  1 ) same1++;       } else {         if (s[i] ==  1 ) diff1++;       }     }     int diff = n - same;     if (same == n) {       cout << 0 <<   n ;       return;     }     int ans = INT_MAX;     if (same1 - 1 == same - same1) {       ans = min(ans, same);     }     if (diff1 == diff - diff1) {       ans = min(ans, diff);     }     if (ans != INT_MAX)       cout << ans <<   n ;     else       cout << -1 <<   n ;   }   void solve1() {     int n;     cin >> n;     string s, t;     cin >> s >> t;     if (s == t) {       cout << 0 <<   n ;       return;     }     int same1 = 0, diff1 = 0, same = 0;     for (int i = 0; i < n; i++) {       if (s[i] == t[i]) {         same++;         if (s[i] ==  1 ) same1++;       } else {         if (s[i] ==  1 ) diff1++;       }     }     int diff = n - same;     int ans = INT_MAX;     if (diff % 2 == 0) {       int one = (diff + 1) / 2;       if (diff1 == one) {         ans = min(ans, diff);       }     }     if (same % 2 != 0) {       int one = (same + 1) / 2;       if (same1 == one) {         ans = min(ans, same);       }     }     if (ans == INT_MAX)       cout << -1 <<   n ;     else       cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     int t;     cin >> t;     while (t--) solve1();   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   double sqrDist(double x1, double y1, double x2, double y2) {     return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);   }   double dist(double x1, double y1, double x2, double y2) {     return sqrt(sqrDist(x1, y1, x2, y2));   }   const double PI = acos(-1.0);   int main() {     int x0, y0, v, t;     scanf( %d%d%d%d , &x0, &y0, &v, &t);     double r0 = 1.0 * v * t;     int n;     scanf( %d , &n);     vector<pair<double, int> > a;     for (int i = 0; i < n; i++) {       int x, y, r;       scanf( %d%d%d , &x, &y, &r);       double d = sqrDist(x, y, x0, y0);       if (d < 1.0 * r * r + eps) {         printf( %.11f , 1.0);         return 0;       }       d = sqrt(d);       if (r + r0 < d - eps) {         continue;       }       double angL, angR, ang;       double angM = atan2(y - y0, x - x0);       if (angM < 0) {         angM += 2 * PI;       }       double tLen = sqrt(d * d - 1.0 * r * r);       if (tLen < r0 + eps) {         ang = asin(r / d);       } else {         ang = acos((d * d + r0 * r0 - 1.0 * r * r) / (2 * d * r0));       }       angL = angM - ang;       angR = angM + ang;       if (angL < 0) {         a.push_back(make_pair(angL + 2 * PI, 1));         a.push_back(make_pair(2 * PI, -1));         a.push_back(make_pair(0.0, 1));         a.push_back(make_pair(angR, -1));       } else if (angR > 2 * PI) {         a.push_back(make_pair(angL, 1));         a.push_back(make_pair(2 * PI, -1));         a.push_back(make_pair(0.0, 1));         a.push_back(make_pair(angR - 2 * PI, -1));       } else {         a.push_back(make_pair(angL, 1));         a.push_back(make_pair(angR, -1));       }     }     sort(a.begin(), a.end());     double last = 0;     int c = 0;     double ans = 0;     for (auto& p : a) {       if (c > 0) {         ans += p.first - last;       }       c += p.second;       last = p.first;     }     ans /= 2 * PI;     printf( %.11f , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> r1, r2;   vector<bool> pd;   vector<int> out;   vector<pair<int, int>> mp;   int le(int i, int cur) {     if (cur == mp[i].first) return mp[i].second;     return mp[i].first;   }   bool mak(int i, int p) {     if (mp[i].first == -1)       mp[i].first = p;     else if (mp[i].second == -1)       mp[i].second = p;     else       return true;     return false;   }   class proc {     struct Dt {       int pos;       bool fl;     };     int p;     int c;     vector<Dt> out_c;       public:     proc(int i) {       p = i;       c = 0;       re();       if (c > out_c.size() / 2) {         for (auto& i : out_c)           if (!i.fl) out.push_back(i.pos);       } else         for (auto& i : out_c)           if (i.fl) out.push_back(i.pos);     }       private:     void re() {       if (pd[p]) return;       pd[p] = 1;       if (r1[p] == r2[p]) return;       int x = le(r1[p], p);       if (r1[p] == r1[x]) {         ++c;         swap(r1[x], r2[x]);         out_c.push_back({x, 1});       } else         out_c.push_back({x, 0});       p = x;       re();     }   };   int main() {     ios_base::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       r1.resize(n);       r2.resize(n);       mp = vector<pair<int, int>>(n, {-1, -1});       pd = vector<bool>(n, 0);       out = vector<int>(0);       for (int i = 0; i < n; ++i) {         cin >> r1[i];         --r1[i];       }       for (int i = 0; i < n; ++i) {         cin >> r2[i];         --r2[i];       }       for (int i = 0; i < n; ++i)         if (mak(r1[i], i) || mak(r2[i], i)) goto badcase;       for (int i = 0; i < n; ++i) proc lp(i);       cout << out.size() << endl;       for (auto i : out) cout << i + 1 <<    ;       cout << endl;       continue;     badcase:       cout << -1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> readvec(long long length, long long off = 0);   template <typename T>   string ts(T v);   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long n;     long long z;     cin >> n >> z;     vector<long long> v = readvec(n);     sort(v.begin(), v.end());     long long left = 0;     long long right = v.size() / 2;     long long ans = 0;     while (true) {       if (left >= v.size() / 2 || right >= v.size()) break;       if (v[right] - v[left] >= z) {         ans++;         left++;         right++;       } else {         right++;       }     }     cout << ans << endl;   }   template <typename T>   string ts(T v) {     stringstream ss;     ss <<  [ ;     for (auto it = v.begin(); it != v.end(); it++) {       if (it != v.begin()) ss <<  ,  ;       ss << *it;     }     ss <<  ] ;     return ss.str();   }   vector<long long> readvec(long long length, long long off) {     vector<long long> v;     for (long long i = 0; i < length; i++) {       long long x;       cin >> x;       v.push_back(x + off);     }     return v;   }


#include <bits/stdc++.h>   using namespace std;   int arr[2505][2505], sum[2505][2505], n, m;   int get_sum(int x1, int y1, int x2, int y2) {     x2 = min(x2, n);     y2 = min(y2, m);     assert(x1 <= x2);     assert(y1 <= y2);     return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];   }   int main() {     ios::sync_with_stdio(false);     ;     cin.tie(0);     ;     int i, j, k, ans = 100000000, ones, zeroes;     string s;     cin >> n >> m;     for (i = 1; i <= n; i++) {       cin >> s;       s =     + s;       for (j = 1; j <= m; j++) arr[i][j] = s[j] -  0 ;     }     for (i = 1; i <= n; i++) {       for (j = 1; j <= m; j++)         sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + arr[i][j];     }     for (k = 2; k <= max(n, m); k++) {       int got = 0;       for (i = 1; i <= n; i = i + k) {         for (j = 1; j <= m; j = j + k) {           ones = get_sum(i, j, i + k - 1, j + k - 1);           zeroes = k * k - ones;           got += min(ones, zeroes);         }       }       ans = min(ans, got);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   int main() {     int n;     string s, ss;     cin >> s >> n;     vector<pair<int, string> > v(n + 1);     v[0] = make_pair(0, s);     char next;     while (cin.get(next))       if (next ==   n ) break;     for (int i = 1; i <= n; ++i) {       getline(cin, ss);       if (ss.length() > 3)         v[i] = make_pair(ss[0] -  0 , ss.substr(3));       else         v[i] = make_pair(ss[0] -  0 ,   );     }     long long val[10], len[10];     for (int i = 0; i < 10; ++i) val[i] = i, len[i] = 10;     for (int i = n; i >= 0; --i) {       int cu = v[i].first;       string s = v[i].second;       if (s.empty()) {         val[cu] = 0;         len[cu] = 1;         continue;       }       long long pow = 1;       long long va = 0;       for (int j = s.length() - 1; j >= 0; --j) {         va += ((val[(s[j] -  0 )]) * pow) % MOD;         va %= MOD;         pow = (pow * len[(s[j] -  0 )]) % MOD;       }       len[cu] = pow;       val[cu] = va;     }     cout << val[0] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)2e5 + 5;   int n, a[maxn], vis[2 * maxn], mx[maxn];   long long ans;   vector<int> slist[maxn];   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   void doit(int d) {     for (int i = 0; i < d; i++) mx[i] = 0;     for (int i = 0; i < n; i++) vis[i] = 0;     for (int i = 0; i < n; i++) mx[i % d] = max(mx[i % d], a[i]);     for (int i = 0; i < n; i++)       if (a[i] == mx[i % d]) vis[i] = 1;     for (int i = n; i < 2 * n; i++) vis[i] = vis[i - n];     int lst = -1;     for (int i = 2 * n - 1; i >= 0; i--) {       if (vis[i] && lst == -1)         lst = i;       else if (!vis[i])         lst = -1;       if (lst == -1 || i >= n) continue;       int len = lst - i + 1;       int idx =           upper_bound(slist[d].begin(), slist[d].end(), len) - slist[d].begin();       ans += idx;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.clear();     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 1; i < n; i++) slist[gcd(i, n)].push_back(i);     for (int i = 1; i * i <= n; i++) {       if (n % i) continue;       doit(i);       if (n / i != i) doit(n / i);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> G[200005];   bool V[200006] = {0};   int S[200006] = {0};   int n;   long long d = 0, mx = 0;   void dfs(int p) {     V[p] = 1;     d++;     int u = 0;     for (int i = 0; i < G[p].size(); i++) {       int cp = G[p][i];       if (!V[cp]) {         dfs(cp);         u++;       }     }     if (!u) mx = max(mx, d);     d--;   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       S[i] = x;       if (x == -1) continue;       G[x].push_back(i);       G[i].push_back(x);     }     for (int i = 1; i <= n; i++) {       if (!V[i] && S[i] == -1) {         d = 0;         dfs(i);       }     }     cout << mx;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int a[1000000];   struct cr {     int l;     int r;     int d;   } op[1000000];   long long int kt[1000000];   long long int ktt[1000000];   long long int fiop[1000000];   long long int vp[1000000];   long long int vpp[1000000];   long long int mba[1000000];   int main() {     int n, m, k, b, c, i, j;     long long int uv;     cin >> n >> m >> k;     for (i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     for (i = 1; i <= m; i++) {       cin >> op[i].l >> op[i].r >> op[i].d;     }     for (i = 0; i < k; i++) {       cin >> b >> c;       kt[b]++;       ktt[c]--;     }     b = 0;     for (i = 1; i <= m; i++) {       b += kt[i];       fiop[i] += b;       b += ktt[i];     }     uv = 0;     for (i = 1; i <= m; i++) {       uv = (op[i].d * fiop[i]);       vp[op[i].l] += uv;       vpp[op[i].r] -= uv;     }     uv = 0;     for (i = 0; i <= 100010; i++) {       uv += vp[i];       mba[i] += uv;       uv += vpp[i];     }     for (i = 1; i <= n; i++) {       a[i] += mba[i];       cout << a[i] <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using db = double;   const int mxn = 100006;   int n, m;   int a[mxn];   vi e[mxn];   bool v[mxn];   vi V;   void dfsv(int x, int p) {     v[x] = true;     V.push_back(x);     for (auto i : e[x])       if (i != p && !v[i]) dfsv(i, x);   }   bool v1[mxn];   void dfs1(int x, int p) {     v1[x] = true;     for (auto i : e[x])       if (i != p)         if (v1[i])           throw i;         else           dfs1(i, x);   }   bool v2[mxn];   bool dfs2(int x, int p, int o) {     v2[x] = true;     for (auto i : e[x])       if (i != p)         if (i == o) {           a[x] = 1;           return true;         } else if (!v2[i]) {           if (dfs2(i, x, o)) {             a[x] = 1;             return true;           }         }     return false;   }   vector<vi> c;   void dfs3(int x, int p) {     c.back().push_back(x);     for (auto i : e[x])       if (i != p) dfs3(i, x);   }   void solve(int x) {     V = {};     dfsv(x, 0);     int o = 0;     try {       dfs1(x, 0);     } catch (int x) {       o = x;     }     if (o) {       dfs2(o, 0, o);     } else {       int leaf = 0;       for (auto i : V)         if (e[i].size() == 1) ++leaf;       if (leaf <= 2) return;       if (leaf >= 4) {         for (auto i : V)           if (e[i].size() == 1)             a[i] = 1;           else             a[i] = 2;       } else {         int r = 0;         for (auto i : V)           if (e[i].size() == 3) r = i;         assert(r);         c = {};         for (auto i : e[r]) {           c.push_back({r});           dfs3(i, r);         }         vi k;         for (auto i : c) k.push_back(min(6, (int)i.size()));         if (k[0] * k[1] + k[0] * k[2] + k[1] * k[2] <= k[0] * k[1] * k[2]) {           const int s = 60;           a[r] = s;           for (int i = 0; i < 3; ++i)             for (int j = 0; j < k[i]; ++j) a[c[i][j]] = s / k[i] * (k[i] - j);         }       }     }   }   int main() {     int t;     cin >> t;     while (t--) {       scanf( %d%d , &n, &m);       for (int i = 1; i <= n; ++i) e[i] = {};       for (int i = 0; i < m; ++i) {         int x, y;         scanf( %d%d , &x, &y);         e[x].push_back(y);         e[y].push_back(x);       }       fill(v, v + n + 1, false);       fill(v1, v1 + n + 1, false);       fill(v2, v2 + n + 1, false);       fill(a, a + n + 1, 0);       for (int i = 1; i <= n; ++i)         if (!v[i]) solve(i);       if (count(a + 1, a + n + 1, 0) == n) {         puts( NO );       } else {         puts( YES );         for (int i = 1; i <= n; ++i) printf( %d  , a[i]);         puts(  );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string rat[100], cw[100], man[100], c;     int a = 0, b = 0, d = 0;     for (int i = 0; i < n; i++) {       string name, s;       cin >> name >> s;       if (s ==  captain )         c = name;       else if (s ==  woman  || s ==  child ) {         cw[a] = name;         a++;       } else if (s ==  rat ) {         rat[b] = name;         b++;       } else if (s ==  man ) {         man[d] = name;         d++;       } else if (s ==  captain )         c = name;     }     for (int i = 0; i <= b; i++) {       if (i == b)         cout << rat[i];       else         cout << rat[i] << endl;     }     for (int i = 0; i <= a; i++) {       if (i == a)         cout << cw[i];       else         cout << cw[i] << endl;     }     for (int i = 0; i <= d; i++) {       if (i == d)         cout << man[i];       else         cout << man[i] << endl;     }     cout << c << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   int p[2010] = {0};   int cnt = 0;   int sum[2010];   int dp[2010];   int a[200010];   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     n = min(n, k);     for (int i = 1, x, y; i <= m; ++i) {       scanf( %d%d , &x, &y);       if (x > k) continue;       p[x] = max(p[x], y);     }     sum[0] = 0;     for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];     memset(dp, 0x3f3f3f3f, sizeof dp);     dp[0] = 0;     for (int i = 1; i <= k; ++i) {       for (int j = 0; j < i; ++j) {         dp[i] = min(dp[i], dp[j] + sum[i] - sum[j + p[i - j]]);       }     }     printf( %d n , dp[k]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, min = INT_MAX, count = 0;     int arr[1000001];     int j;     cin >> n;     for (j = 0; j < n; j++) cin >> arr[j];     sort(arr, arr + n);     int i, diff;     for (i = 0; i < n - 1; i++) {       j = i + 1;       diff = arr[j] - arr[i];       if (diff < 0) diff = -diff;       if (diff < min) {         min = diff;         count = 1;       } else if (diff == min)         count++;     }     cout << min <<     << count;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   using namespace std;   int num[1001];   long long n;   long long check(long long k) {     long long p = 0, v = 0;     long long t = n;     while (t) {       if (t >= k) {         p += k;         t -= k;       } else {         p += t;         t = 0;       }       v += t / 10;       t -= t / 10;     }     if (v <= p)       return 1;     else       return 0;   }   int main() {     cin >> n;     long long l = 1;     long long r = 1e18;     long long ans = 0;     while (l <= r) {       long long mid = (l + r) / 2;       if (check(mid)) {         ans = mid;         r = mid - 1;       } else {         l = mid + 1;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   const int MOD = 1e9 + 7;   int add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }   int mul(int a, int b) { return 1ll * a * b % MOD; }   int n, x[N], y[N];   vector<int> g[N];   map<int, int> px, py;   int far[N], cnt, cir;   void dfs(int u, int fa) {     if (!px.count(x[u])) cnt++;     px[x[u]] = u;     if (!py.count(y[u])) cnt++;     py[y[u]] = u;     for (auto v : g[u]) {       if (v == fa) continue;       if (~far[v]) {         cir = 1;         continue;       }       far[v] = u;       dfs(v, u);     }   }   int pw2[N * 2];   void solve() {     for (int i = (0); i < (n); ++i) g[i].clear();     px.clear();     py.clear();     for (int i = (0); i < (n); ++i) {       int &x = ::x[i], &y = ::y[i];       scanf( %d%d , &x, &y);       if (px.count(x)) {         int j = px[x];         g[i].push_back(j);         g[j].push_back(i);       }       px[x] = i;       if (py.count(y)) {         int j = py[y];         g[i].push_back(j);         g[j].push_back(i);       }       py[y] = i;     }     int ans = 1;     fill_n(far, n, -1);     pw2[0] = 1;     for (int i = (1); i < (2 * n + 1); ++i) pw2[i] = add(pw2[i - 1], pw2[i - 1]);     for (int i = (0); i < (n); ++i)       if (!~far[i]) {         px.clear();         py.clear();         far[i] = cnt = cir = 0;         dfs(i, -1);         ans = mul(ans, pw2[cnt] - !cir);       }     printf( %d n , ans);   }   int main() {     while (~scanf( %d , &n)) solve();     return 0;   }


#include <bits/stdc++.h>   int a, b, yue[100000], l[10100], h[10100], p[10100], ans[10100], gs, c, n;   void qsort(int x, int y) {     int i, j, t, mid;     i = x;     j = y;     mid = yue[(i + j) / 2];     do {       while (yue[i] < mid) i++;       while (yue[j] > mid) j--;       if (i <= j) {         t = yue[i];         yue[i] = yue[j];         yue[j] = t;         i++;         j--;       }     } while (i <= j);     if (i < y) qsort(i, y);     if (x < j) qsort(x, j);   }   void qsort1(int x, int y) {     int i, j, t, mid;     i = x;     j = y;     mid = h[(i + j) / 2];     do {       while (h[i] > mid) i++;       while (h[j] < mid) j--;       if (i <= j) {         t = h[i];         h[i] = h[j];         h[j] = t;         t = l[i];         l[i] = l[j];         l[j] = t;         t = p[i];         p[i] = p[j];         p[j] = t;         i++;         j--;       }     } while (i <= j);     if (i < y) qsort1(i, y);     if (x < j) qsort1(x, j);   }   int gcd(int a, int b) {     if (a > b) {       int t;       t = a;       a = b;       b = t;     }     if (a == 0)       return b;     else       return gcd(b % a, a);   }   int main() {     int i, j;     while (scanf( %d %d , &a, &b) != EOF) {       c = gcd(a, b);       for (i = 1; i * i <= c; i++)         if (c % i == 0) {           gs++;           yue[gs] = i;           gs++;           yue[gs] = c / i;         }       qsort(1, gs);       scanf( %d , &n);       for (i = 0; i < n; i++) {         p[i] = i;         scanf( %d %d , &l[i], &h[i]);       }       qsort1(0, n - 1);       j = gs;       for (i = 0; i < n; i++) {         while (yue[j] > h[i]) j--;         if (yue[j] < l[i])           ans[p[i]] = -1;         else           ans[p[i]] = yue[j];       }       for (i = 0; i < n; i++) printf( %d n , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       string s;       cin >> s;       map<char, int> mp;       for (auto it : s) {         mp[it]++;       }       int ans = 1;       for (int i = 1; i * i <= k; i++) {         if (k % i == 0) {           int fact1 = i, fact2 = k / i;           for (int j = fact1; j <= n; j += fact1) {             int x = 0;             for (auto it : mp) x += (it.second / (j / fact1));             if (x >= fact1) ans = max(ans, j);           }           for (int j = fact2; j <= n; j += fact2) {             int x = 0;             for (auto it : mp) x += (it.second / (j / fact2));             if (x >= fact2) ans = max(ans, j);           }         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long nxt[45][3], lnk[45], pre[45][3];   string s;   long long n;   pair<long long, long long> sh[45][45][2];   long long dp[41][41][41][41][2];   long long sol(long long i, long long pre, long long l, long long r,                 long long f = 0) {     if (pre == s.size()) f = 1;     if (r - l + 1 == s.size()) f = 1;     if (i == n) {       if (f) return 1;       string final;       if (pre) final = s.substr(0, pre);       if (r - l + 1) final += s.substr(l, r - l + 1);       if (final.find(s) != string::npos) {         return 1;       }       return 0;     }     if (~dp[i][pre][l][r][f]) return dp[i][pre][l][r][f];     if (r - l + 1 == i)       dp[i][pre][l][r][f] =           sol(i + 1, nxt[pre][0], sh[l][r][0].first, sh[l][r][0].second, f) +           sol(i + 1, nxt[pre][1], sh[l][r][1].first, sh[l][r][1].second, f);     else       dp[i][pre][l][r][f] =           sol(i + 1, nxt[pre][0], l, r, f) + sol(i + 1, nxt[pre][1], l, r, f);     return dp[i][pre][l][r][f];   }   void solve() {     cin >> n;     cin >> s;     for (long long i = 0; i < s.size(); i++) {       if (i > 1) {         lnk[i] = nxt[lnk[i - 1]][s[i - 1] -  0 ];       }       for (long long j = 0; j < 2; j++) {         if (s[i] == j +  0 )           nxt[i][j] = i + 1;         else if (i > 0) {           nxt[i][j] = nxt[lnk[i]][j];         }       }     }     for (long long i = 0; i < s.size(); i++) {       for (long long j = 0; j < s.size(); j++) {         for (long long k = 0; k < 2; k++) {           string pt;           if (j >= i) pt += s.substr(i, j - i + 1);           pt.push_back(k +  0 );           long long x = s.find(pt);           if (x != string::npos)             sh[i][j][k] = {x, x + j - i + 1};           else             sh[i][j][k] = {i, j};         }       }     }     if (s.size() == 1) {       long long x = s.back() -  0 ;       sh[1][0][x] = {0, 0};       sh[1][0][!x] = {1, 0};     }     memset(dp, -1, sizeof dp);     cout << sol(0, 0, 1, 0, 0) << endl;   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1010];   int main() {     int n, m;     cin >> n >> m;     int tmp;     int ans = 0;     memset(a, 0, sizeof(a));     for (int i = 1; i <= m; i++) {       cin >> tmp;       a[tmp]++;       int b = 1;       for (int i = 1; i <= n; i++)         if (!a[i]) b = 0;       if (b) {         ans++;         for (int i = 1; i <= m; i++) a[i]--;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int n, q, x, y, cnt = 0;     cin >> n >> q;     vector<vector<long long int>> dp(3, vector<long long int>(n + 2, 0));     while (q--) {       cin >> x >> y;       if (dp[x][y] == 0) {         cnt += (dp[3 - x][y] == 1);         cnt += (dp[3 - x][y - 1] == 1);         cnt += (dp[3 - x][y + 1] == 1);         dp[x][y] = 1;       } else {         cnt -= (dp[3 - x][y] == 1);         cnt -= (dp[3 - x][y - 1] == 1);         cnt -= (dp[3 - x][y + 1] == 1);         dp[x][y] = 0;       }       if (cnt == 0)         cout <<  Yes  <<   n ;       else         cout <<  No  <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     long long int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     long long ans = 0;     priority_queue<int, vector<int>, greater<int>> pq;     while (n--) {       int x;       cin >> x;       if (pq.size() && x > pq.top()) {         ans += x - pq.top();         pq.pop();         pq.push(x);       }       pq.push(x);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, k;     int h[1001] = {0};     scanf( %d%d , &n, &k);     int i, j;     for (i = 1; i <= n; i++) scanf( %d , &h[i]);     int min = 9999999, nn = 1;     for (i = 1; i <= n; i++) {       if (h[i] < 1 + k * (i - 1)) continue;       int ans = 0;       for (j = 1; j <= n; j++)         if (h[j] != h[i] + k * (j - i)) ans++;       if (ans < min) {         min = ans;         nn = i;       }       if (min == 0) break;     }     printf( %d n , min);     for (i = 1; i <= n; i++) {       if (h[i] > h[nn] + k * (i - nn))         printf( - %d %d n , i, h[i] - h[nn] - k * (i - nn));       else if (h[i] < h[nn] + k * (i - nn))         printf( + %d %d n , i, h[nn] + k * (i - nn) - h[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     vector<int> v(2 * n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 0; i < 2 * n; i++) {       if (i >= n) {         v[i] = a[i - n];       } else         v[i] = a[i];     }     int ans = 0, cnt = 0;     for (int i = 0; i < 2 * n; i++)       if (v[i] == 1) {         cnt++;         ans = max(ans, cnt);       } else         cnt = 0;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c;     scanf( %d%d%d , &a, &b, &c);     int m;     scanf( %d , &m);     string s;     int val, ans = 0;     vector<int> u, p;     for (int i = 0; i < int(m); i++) {       cin >> val >> s;       if (s[0] ==  U )         u.push_back(val);       else         p.push_back(val);     }     long long sum = 0;     sort(u.begin(), u.end());     sort(p.begin(), p.end());     int i = 0;     while (i < int(u.size()) && a) {       sum += u[i];       i++;       a--;       ans++;     }     int j = 0;     while (j < int(p.size()) && b) {       sum += p[j];       j++;       b--;       ans++;     }     while (i < int(u.size()) || j < int(p.size())) {       if (!c) break;       c--;       ans++;       if (i == int(u.size()))         sum += p[j++];       else if (j == int(p.size()))         sum += u[i++];       else {         if (u[i] < p[j])           sum += u[i++];         else           sum += p[j++];       }     }     cout << ans <<     << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5, maxm = 1e5 + 5;   int first[maxn], n, k;   struct edge {     int to;     int next;   } E[maxm];   int vis[maxn];   int order[maxn], p;   int a[maxn];   bool dfs(int u) {     vis[u] = -1;     for (int e = first[u]; e != -1; e = E[e].next) {       int v = E[e].to;       if (vis[v] == -1)         return false;       else if (!vis[v] && !dfs(v))         return false;     }     vis[u] = 1;     order[p++] = u;     return true;   }   bool topo() {     p = 0;     memset(vis, 0, sizeof(vis));     for (int i = 0; i < k; i++)       if (!vis[a[i]] && !dfs(a[i])) return false;     return true;   }   int main() {     while (scanf( %d%d , &n, &k) != EOF) {       memset(first, -1, sizeof(first));       for (int i = 0; i < k; i++) scanf( %d , &a[i]);       int t, x, ecnt = 0;       for (int i = 1; i <= n; i++) {         scanf( %d , &t);         for (int j = 0; j < t; j++) {           scanf( %d , &x);           E[ecnt].to = x;           E[ecnt].next = first[i];           first[i] = ecnt++;         }       }       if (!topo())         cout <<  -1 n ;       else {         cout << p <<   n ;         for (int i = 0; i < p; i++) cout << order[i] <<    n [i == p - 1];       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, q, cnt, f[400005];   int fnd(int x) { return f[x] == 0 ? x : f[x] = fnd(f[x]); }   int main() {     scanf( %d%d%d , &n, &m, &q);     cnt = n + m - 1;     for (int i = 1, x, y; i <= q; i++) {       scanf( %d%d , &x, &y);       y += n;       x = fnd(x);       y = fnd(y);       if (x != y) {         cnt--;         f[x] = y;       }     }     printf( %d n , cnt);     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, m, a;     scanf( %d%d%d , &n, &m, &a);     printf( %I64d n , ((n - 1LL) / a + 1) * ((m - 1) / a + 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 2e5 + 10;   const int INF = 0x3f3f3f3f;   const int mod = 998244353;   struct node {     int dif;     long long need;     bool operator<(const node &f) const {       if (f.need != need)         return f.need > need;       else         return f.dif > dif;     }   } pro[M];   long long ans[M];   int main() {     int t;     scanf( %d , &t);     while (t--) {       memset(ans, 0, sizeof(ans));       long long n, t, a, b;       scanf( %lld%lld%lld%lld , &n, &t, &a, &b);       long long simple = 0;       for (int i = 1; i <= n; i++) {         scanf( %d , &pro[i].dif);         if (!pro[i].dif) simple++;       }       for (int i = 1; i <= n; i++) scanf( %lld , &pro[i].need);       sort(pro + 1, pro + n + 1);       long long time = 0;       if (pro[1].need) ans[0] = min(simple, (pro[1].need - 1) / a);       for (int i = 1; i <= n; i++) {         if (pro[i].dif)           time += b;         else           simple--, time += a;         if (simple == 0 || i == n) break;         ans[i] = min(simple, (pro[i + 1].need - time - 1) / a);       }       time = 0;       long long cnt = ans[0];       for (int i = 1; i <= n; i++) {         time += pro[i].dif == 0 ? a : b;         if (time > t)           break;         else {           if (time < pro[i + 1].need || i == n) {             cnt = max(cnt, i + ans[i]);           }         }       }       printf( %lld n , cnt);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m, k;     cin >> n >> m >> k;     map<int, bool> mp;     for (int i = 0; i < m; i++) {       int x;       cin >> x;       mp[x] = 1;     }     int pos = 1;     int ans = -1;     if (mp[pos]) {       cout << 1;       return 0;     }     for (int i = 0; i < k; i++) {       int x, y;       cin >> x >> y;       if (pos == x or pos == y) {         if (pos != x)           pos = x;         else           pos = y;         if (mp[pos]) {           cout << pos;           return 0;         }       }     }     cout << pos;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010, maxm = maxn * 2;   int h[maxn], ne[maxm], e[maxm], cnt;   int d[maxn], dp[maxn][3], w[maxn], root;   int a[maxn];   int vis[maxn];   void solve() {     int T;     scanf( %d , &T);     while (T--) {       int n;       scanf( %d , &n);       for (int i = 1; i <= n; i++) {         scanf( %d , &a[i]);       }       for (int i = 1; i < n; i += 2) {         cout << -a[i + 1] <<     << a[i] <<    ;       }       cout << endl;     }     return;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long INFLL = 1e18;   const int MOD = 1e9 + 7;   const int MAXN = 2e5 + 5;   vector<int> G[MAXN];   int n;   long long s[MAXN];   long long a[MAXN];   void dfs(int v, int p) {     if (s[v] == -1) {       long long aux = INF;       for (auto u : G[v])         if (u != p) {           aux = min(aux, s[u]);         }       if (aux == -1 || aux - s[p] < 0) {         a[v] = -1;         return;       }       if (aux == INF) {         a[v] = 0;         return;       }       a[v] = aux - s[p];       s[v] = a[v] + s[p];     } else {       if (p == v)         a[v] = s[v];       else         a[v] = s[v] - s[p];     }     for (auto u : G[v])       if (u != p) dfs(u, v);   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int x;       scanf( %d , &x);       x--;       G[x].push_back(i);       G[i].push_back(x);     }     for (int i = 0; i < n; i++) scanf( %lld , &s[i]);     dfs(0, 0);     long long ans = 0;     for (int i = 0; i < n; i++) {       if (a[i] < 0) {         printf( -1 n );         return 0;       }       ans += a[i];     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T sqr(T x) {     return x * x;   }   const double EPS = 1e-6;   const int INF = 0x3fffffff;   const long long LINF = INF * 1ll * INF;   const double PI = acos(-1.0);   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long d[400005];   long long a, b, k, lcm;   long long bfs(long long a, long long target) {     queue<int> q;     memset(d, -1, sizeof(d));     q.push(a);     d[a] = 0;     while (!q.empty()) {       int st = q.front();       q.pop();       for (int i = 2; i <= k; i++) {         if (st % i == 0) continue;         int tmp = st - st % i;         if (tmp < 0) tmp += lcm;         if (d[tmp] <= 0) {           d[tmp] = d[st] + 1;           q.push(tmp);         }       }       int tmp = st - 1;       if (tmp < 0) tmp += lcm;       if (d[tmp] <= 0) {         d[tmp] = d[st] + 1;         q.push(tmp);       }     }     return d[target];   }   int main(void) {     cin >> a >> b >> k;     lcm = 1;     for (int i = 2; i <= k; i++) lcm = lcm / gcd(lcm, i) * i;     long long ans = 0;     if (a / lcm == b / lcm) {       if (a % lcm != b % lcm) ans = bfs(a % lcm, b % lcm);     } else {       if (a % lcm) ans += bfs(a % lcm, 0);       if (b % lcm) ans += bfs(0, b % lcm);       ans += (a / lcm - b / lcm - 1) * bfs(0, 0);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t;     t = 1;     while (t--) {       int n, k, mx = 0, mn = 101, j;       cin >> n >> k;       int a[n];       for (int i = 0; i < n; i += 1) {         cin >> a[i];         mx = max(mx, a[i]);         mn = min(mn, a[i]);       }       if (mx - mn > k)         cout <<  NO ;       else {         cout <<  YES  <<   n ;         for (int i = 0; i < n; i += 1) {           for (j = 0; j < a[i] - 1; j++) {             cout << (j % k) + 1 <<    ;           }           cout << (j % k) + 1 <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   string a[100];   int m[100][26];   bool s1[100];   string makestr(string s1, string s2) {     char w =  * ;     string make =   ;     int s[26];     for (int i = 0; i < 26; ++i) s[i] = 0;     for (int i = 0; i < s1.length(); ++i) s[int(s1[i]) - 97] = 1;     for (int i = 0; i < s2.length(); ++i) {       if (s[int(s2[i]) - 97] == 1) {         w = s2[i];         break;       }     }     if (w ==  * )       return s1 + s2;     else {       int pref1 = 0;       int pref2 = 0;       while (s1[pref1] != w) ++pref1;       while (s2[pref2] != w) ++pref2;       if (pref1 < pref2) {         swap(s1, s2);         swap(pref1, pref2);       }       for (int i = 0; i < s1.length(); ++i) {         make += s1[i];       }       if (s2.length() - pref2 > s1.length() - pref1) {         for (int i = pref2 + s1.length() - pref1; i < s2.length(); ++i)           make += s2[i];       }       return make;     }   }   string dfs(string st) {     int u[26];     for (int i = 0; i < 26; ++i) u[i] = 0;     for (int i = 0; i < st.length(); ++i) u[int(st[i]) - 97] = 1;     for (int i = 1; i < n; ++i) {       if (s1[i]) {         for (int j = 0; j < 26; ++j)           if (m[i][j] && u[j]) {             st = makestr(st, a[i]);             s1[i] = false;             st = dfs(st);           }       }     }     return st;   }   int main() {     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> a[i];       s1[i] = true;     }     for (int i = 0; i < n; ++i) {       string st = a[i];       for (int j = 0; j < st.length(); ++j) {         m[i][int(st[j] - 97)] = 1;       }     }     string st = dfs(a[0]);     for (int i = 1; i < n; ++i) {       if (s1[i]) {         st = makestr(st, dfs(a[i]));       }     }     cout << st;   }


#include <bits/stdc++.h>   using namespace std;   int A[1 << 18], vis[1 << 20], n, k, d, t;   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d%d%d , &n, &k, &d);       int ans = k, cnt = 0;       for (int i = 1; i <= n; i++) {         scanf( %d , &A[i]);         if (!vis[A[i]]) cnt++;         vis[A[i]]++;         if (i >= d) {           if (i > d) {             vis[A[i - d]]--;             if (!vis[A[i - d]]) cnt--;           }           ans = min(ans, cnt);         }       }       printf( %d n , ans);       for (int i = 1; i <= n; i++) vis[A[i]] = 0;     }     return 0;   }


#include <bits/stdc++.h>   const int N = 1123;   const double eps = 1e-6;   const int inf = (int)1e9 + 123;   using namespace std;   int n, m;   char a[N][N];   int mini = inf;   int d[N][N];   int sum(int x, int y, int x2, int y2) {     return d[x2][y2] - d[x - 1][y2] - d[x2][y - 1] + d[x - 1][y - 1];   }   bool check(int x, int y, int sx, int sy) {     if (sum(sx, sy, sx + x - 1, sy + y - 1) != x * y) return 0;     int sz = sum(sx, sy, sx + x - 1, sy + y - 1);     while (1) {       if (sx + x <= n && sum(sx + x, sy, sx + x, sy + y - 1) == y) {         sx++;         sz += y;         continue;       }       if (sy + y <= m && sum(sx, sy + y, sx + x - 1, sy + y) == x) {         if (sum(sx, sy + y, sx + x - 1, sy + y) != x) return 0;         sy++;         sz += x;         continue;       }       break;     }     if (d[n][m] != sz) return 0;     return 1;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         cin >> a[i][j];         d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1] +                   (a[i][j] ==  X  ? 1 : 0);       }     int x = -1, y = -1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++)         if (a[i][j] ==  X ) {           x = i;           y = j;           break;         }       if (x != -1) break;     }     if (x == -1) {       cout << -1;       return 0;     }     int cnt = 0;     for (int i = x; i <= n; i++) {       if (a[i][y] ==  X )         cnt++;       else         break;     }     for (int i = 1; i <= m; i++)       if (y + i - 1 <= m && check(cnt, i, x, y) && mini > cnt * i) mini = cnt * i;     cnt = 0;     for (int i = y; i <= m; i++) {       if (a[x][i] ==  X )         cnt++;       else         break;     }     for (int i = 1; i <= n; i++)       if (x + i - 1 <= m && check(i, cnt, x, y) && mini > i * cnt) mini = i * cnt;     if (mini == inf) mini = -1;     cout << mini;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   ostream& operator<<(ostream& os, const pair<T1, T2>& p) {     os <<  (  << p.first <<  ,   << p.second <<  ) ;     return os;   }   template <typename T1, typename T2, typename T3>   ostream& operator<<(ostream& os, const tuple<T1, T2, T3>& t) {     os <<  (  << get<0>(t) <<  ,   << get<1>(t) <<  ,   << get<2>(t) <<  ) ;     return os;   }   template <typename T1, typename T2, typename T3, typename T4>   ostream& operator<<(ostream& os, const tuple<T1, T2, T3, T4>& t) {     os <<  (  << get<0>(t) <<  ,   << get<1>(t) <<  ,   << get<2>(t) <<  ,          << get<3>(t) <<  ) ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, const vector<T>& v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); it++) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     os <<  ] ;     return os;   }   template <typename T, typename C>   ostream& operator<<(ostream& os, const set<T, C>& v) {     os <<  { ;     for (auto it = v.begin(); it != v.end(); it++) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     os <<  } ;     return os;   }   template <typename T, typename C>   ostream& operator<<(ostream& os, const unordered_set<T, C>& v) {     os <<  { ;     for (auto it = v.begin(); it != v.end(); it++) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     os <<  } ;     return os;   }   template <typename T, typename C>   ostream& operator<<(ostream& os, const multiset<T, C>& v) {     os <<  { ;     for (auto it = v.begin(); it != v.end(); it++) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     os <<  } ;     return os;   }   template <typename T1, typename T2, typename C>   ostream& operator<<(ostream& os, const map<T1, T2, C>& mp) {     os <<  [ ;     for (auto it = mp.begin(); it != mp.end(); it++) {       if (it != mp.begin()) os <<  ,  ;       os << it->first <<  :   << it->second;     }     os <<  ] ;     return os;   }   template <typename T1, typename T2, typename C>   ostream& operator<<(ostream& os, const unordered_map<T1, T2, C>& mp) {     os <<  [ ;     for (auto it = mp.begin(); it != mp.end(); it++) {       if (it != mp.begin()) os <<  ,  ;       os << it->first <<  :   << it->second;     }     os <<  ] ;     return os;   }   template <typename T, typename T2>   ostream& operator<<(ostream& os, const queue<T, T2>& orig) {     queue<T, T2> que(orig);     bool first = true;     os <<  [ ;     while (!que.empty()) {       T x = que.front();       que.pop();       if (!first) os <<  ,  ;       os << x;       first = false;     }     return os <<  ] ;   }   template <typename T, typename T2>   ostream& operator<<(ostream& os, const deque<T, T2>& orig) {     deque<T, T2> que(orig);     bool first = true;     os <<  [ ;     while (!que.empty()) {       T x = que.front();       que.pop_front();       if (!first) os <<  ,  ;       os << x;       first = false;     }     return os <<  ] ;   }   template <typename T, typename T2, typename T3>   ostream& operator<<(ostream& os, const priority_queue<T, T2, T3>& orig) {     priority_queue<T, T2, T3> pq(orig);     bool first = true;     os <<  [ ;     while (!pq.empty()) {       T x = pq.top();       pq.pop();       if (!first) os <<  ,  ;       os << x;       first = false;     }     return os <<  ] ;   }   template <typename T>   ostream& operator<<(ostream& os, const stack<T>& st) {     stack<T> tmp(st);     os <<  [ ;     bool first = true;     while (!tmp.empty()) {       T& t = tmp.top();       if (first)         first = false;       else         os <<  ,  ;       os << t;       tmp.pop();     }     os <<  ] ;     return os;   }   ostream& operator<<(ostream& os, int8_t x) {     os << (int32_t)x;     return os;   }   template <class... Args>   string dbgFormat(const char* fmt, Args... args) {     size_t len = snprintf(nullptr, 0, fmt, args...);     char buf[len + 1];     snprintf(buf, len + 1, fmt, args...);     return string(buf);   }   template <class Head>   void dbgLog(Head&& head) {     cerr << head << endl;   }   template <class Head, class... Tail>   void dbgLog(Head&& head, Tail&&... tail) {     cerr << head <<    ;     dbgLog(forward<Tail>(tail)...);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout << setprecision(20);     long long int n;     cin >> n;     const long long int big = 1e10;     vector<long long int> ans(n + 1, big);     long long int lim = 10;     for (long long int t = 0; t < lim; t++) {       ;       auto doit = [&](long long int bit) -> void {         vector<long long int> vec;         for (long long int i = 1; i <= n; i++) {           if (((i >> t) & 1) == bit) vec.push_back(i);         };         long long int sz = vec.size();         if (sz == 0) return;         cout << sz << endl;         for (long long int i = 0; i < sz; i++) {           if (i > 0) cout <<    ;           cout << vec[i];         }         cout << endl;         for (long long int i = 1; i <= n; i++) {           long long int v;           cin >> v;           if (((i >> t) & 1) != bit) {             ans[i] = min(ans[i], v);             ;           }         }       };       doit(0);       doit(1);     }     cout << -1 << endl;     for (long long int i = 1; i <= n; i++) {       if (i > 0) cout <<    ;       cout << ans[i];     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 7;   const int inf = INT_MAX;   const long long inff = 1e18;   const int mod = 1e9 + 7;   long long a[50];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     a[0] = 0;     for (long long i = 1; i <= 31; i++) {       a[i] = 4 * a[i - 1] + 1;     }     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       if (n <= 30 && k > a[n])         cout <<  NO  << endl;       else if (k < 5) {         if (k == 3 && n == 2)           cout <<  NO  << endl;         else if (n == 2) {           if (k == 1 || k == 2)             cout <<  YES 1  << endl;           else             cout <<  YES 0  << endl;         } else {           cout <<  YES   << n - 1 << endl;         }       } else {         cout <<  YES  ;         long long now;         for (long long i = 1; i <= 31; i++) {           if (a[i] >= k) {             now = i;             break;           }         }         now--;         if (a[now + 1] - k <= (((1ll << now) - 1) * ((1ll << now) - 1)))           cout << n - now - 1 << endl;         else           cout << n - now << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long int computeXOR(long long int n) {     if (n % 4 == 0) return n;     if (n % 4 == 1) return 1;     if (n % 4 == 2) return n + 1;     return 0;   }   void solve() {     vector<long long int> v;     map<long long int, long long int> mp;     long long int e, i, j, l, m, x, y, z, r, k, n;     cin >> n >> m;     x = computeXOR(n - 1);     r = x ^ n;     if (x == m)       cout << n << endl;     else if (r == m)       cout << n + 2 << endl;     else       cout << n + 1 << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int T = 1;     cin >> T;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   void putin(char s[]);   int main() {     int i, total = 0;     char c, ori1[120], ori2[120], ori3[120], a[6][400], temp[700];     putin(ori1);     putin(ori2);     putin(ori3);     strcpy(a[0], ori1);     strcat(a[0], ori2);     strcat(a[0], ori3);     strcpy(a[1], ori2);     strcat(a[1], ori1);     strcat(a[1], ori3);     strcpy(a[2], ori3);     strcat(a[2], ori2);     strcat(a[2], ori1);     strcpy(a[3], ori1);     strcat(a[3], ori3);     strcat(a[3], ori2);     strcpy(a[4], ori2);     strcat(a[4], ori3);     strcat(a[4], ori1);     strcpy(a[5], ori3);     strcat(a[5], ori1);     strcat(a[5], ori2);     scanf( %d , &total);     getchar();     while (total) {       --total;       putin(temp);       for (i = 0; i < 6; ++i)         if (strcmp(temp, a[i]) == 0) {           printf( ACC );           break;         }       if (i == 6) printf( WA );       printf(  n );     }     return 0;   }   void putin(char s[]) {     char c;     int i = 0;     c = getchar();     while (c !=   n ) {       if (c >=  A  && c <=  Z )         s[i] = c + 32;       else if (c >=  a  && c <=  z )         s[i] = c;       else         --i;       ++i;       c = getchar();     }     s[i] =   0 ;     return;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const double pi = acos(-1.0);   const int INF = 0x3f3f3f3f;   const long long inf = (((long long)1) << 61) + 5;   const int N = 200005;   int a[N];   int b[N];   int f1[N];   int f2[N];   int bit[N];   int n;   int sum(int i) {     int s = 0;     while (i > 0) {       s += bit[i];       i -= i & -i;     }     return s;   }   void add(int i, int x) {     while (i <= n + 1) {       bit[i] += x;       i += i & -i;     }   }   int get(int x) {     int ans = n, l = 1, r = n;     while (l <= r) {       int mid = (l + r) >> 1;       if (sum(mid) >= x) {         ans = mid;         r = mid - 1;       } else         l = mid + 1;     }     return ans;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       int x = sum(a[i]);       int tmp = a[i] - x;       f1[n - i - 1] = tmp;       add(a[i] + 1, 1);     }     memset(bit, 0, sizeof bit);     for (int i = 0; i < n; i++) {       scanf( %d , &b[i]);       int x = sum(b[i]);       int tmp = b[i] - x;       f2[n - i - 1] = tmp;       add(b[i] + 1, 1);     }     for (int i = 0; i < n; i++) {       f1[i] += f2[i];       f1[i + 1] += f1[i] / (i + 1);       f1[i] %= (i + 1);     }     memset(bit, 0, sizeof bit);     for (int i = 1; i <= n; i++) add(i, 1);     for (int i = n - 1; i >= 0; i--) {       int ans = get(f1[i] + 1);       printf( %d  , ans - 1);       add(ans, -1);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 1e5 + 5;   vector<int> g[max_n], path;   bool vis[max_n], x[max_n];   int s;   void dfs(int u) {     vis[u] = 1;     x[u] ^= 1;     path.push_back(u);     bool leaf = 1;     for (int v : g[u]) {       if (!vis[v]) {         if (!leaf) leaf = 0;         dfs(v);         x[u] ^= 1;         path.push_back(u);         if (x[v]) {           x[v] ^= 1;           path.push_back(v);           x[u] ^= 1;           path.push_back(u);         }       }     }     if (u == s && x[u]) {       x[u] ^= 1;       path.pop_back();     }   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     while (m--) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     s = -1;     for (int i = 1; i <= n; i++) {       scanf( %d , &x[i]);       if (s == -1 && x[i]) s = i;     }     if (s != -1) dfs(s);     bool ok = 1;     for (int i = 1; i <= n; i++) {       if (x[i]) {         ok = 0;         break;       }     }     if (!ok) {       puts( -1 );       exit(0);     }     int sz = path.size();     printf( %d n , sz);     for (int i = 0; i < sz; i++) {       if (i != sz - 1)         printf( %d  , path[i]);       else         printf( %d n , path[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int OO = 0x3f3f3f3f, N = 1e2 + 5, mod = 1e9 + 7;   int ans, lst = OO, gt[26];   string s1, s2;   vector<int> idx[26];   int main() {     cin >> s1 >> s2;     for (int i = 0; i < s1.size(); ++i) idx[s1[i] -  a ].push_back(i);     for (int i = 0; i < s2.size(); ++i) {       int ch = s2[i] -  a ;       if (!idx[ch].size()) exit(puts( -1 ));       if (idx[ch].back() > lst && idx[ch][gt[ch]] <= lst)         gt[ch] =             lower_bound(idx[ch].begin(), idx[ch].end(), lst) - idx[ch].begin();       if (idx[ch].size() <= gt[ch] || idx[ch].back() <= lst) {         ++ans, memset(gt, 0, sizeof gt);         gt[ch] = 1, lst = idx[ch][0];       } else         lst = (idx[ch][gt[ch]++]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dfs(long long int n, int i, long long int b[]) {     int ans = i * (n / b[i]);     n = n % b[i];     if (n == 0)       return ans;     else       return (ans + min(i + dfs(b[i] - n, i - 1, b), dfs(n, i - 1, b)));   }   int main() {     long long int a[17], i, n;     a[1] = 1;     for (i = 2; i < 17; i++) a[i] = 10 * a[i - 1] + 1;     cin >> n;     int j = dfs(n, 16, a);     cout << j;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const double eps = 1e-6;   int modpow(int x, int y, int md) {     if (y == 0) return 1;     int ret = modpow(x, y >> 1, md);     ret = (long long)ret * ret % md;     if (y & 1) ret = (long long)ret * x % md;     return ret;   }   int n, q, mod;   int pws[10][2000001];   long long euclid(long long x, long long y, long long &k, long long &l) {     if (y == 0) {       k = 1;       l = 0;       return x;     }     long long g = euclid(y, x % y, l, k);     l -= k * (x / y);     return g;   }   long long rev(long long x) {     long long k, l;     if (euclid(x, mod, k, l) != 1) throw;     k %= mod;     if (k < 0) k += mod;     return k;   }   int pr[10], cnt = 0;   struct Num {     int a[10] = {};     int val = 0;     Num operator*(Num b) {       Num ret;       ret.val = (long long)val * b.val % mod;       for (int i = 0; i < cnt; i++) ret.a[i] = a[i] + b.a[i];       return ret;     }     Num operator+(Num b) {       if (val == -1) return b;       if (b.val == -1) return *this;       Num ret;       ret.val = 0;       int v2 = val, v1 = b.val;       for (int i = 0; i < cnt; i++) {         ret.a[i] = min(a[i], b.a[i]);         v2 = (long long)v2 * pws[i][a[i] - ret.a[i]] % mod;         v1 = (long long)v1 * pws[i][b.a[i] - ret.a[i]] % mod;       }       ret.val = (v1 + v2) % mod;       return ret;     }     Num inv() {       Num ret;       ret.val = rev(val);       for (int i = 0; i < cnt; i++) {         ret.a[i] = -a[i];       }       return ret;     }     int getreallyvalue() {       int ret = val;       for (int i = 0; i < cnt; i++) ret = (long long)ret * pws[i][a[i]] % mod;       return ret;     }   };   Num sjctxdy(int x) {     Num ret;     if (x == 0) {       ret.val = 0;       return ret;     }     for (int i = 0; i < 10; i++) ret.a[i] = 0;     for (int i = 0; i < cnt; i++) {       if (x % pr[i] == 0) {         while (x % pr[i] == 0) {           ret.a[i]++;           x /= pr[i];         }       }     }     ret.val = x;     return ret;   }   struct mod_sjc {     Num v, mul;     void print() {       printf( v: %d , v.val);       printf(  array: );       for (int i = 0; i < cnt; i++)         printf( %d%c , v.a[i], i == cnt - 1 ?   n  :    );       printf( mul: %d , mul.val);       printf(  array: );       for (int i = 0; i < cnt; i++)         printf( %d%c , mul.a[i], i == cnt - 1 ?   n  :    );     }   } node[131072 * 2];   int sjctql[131072];   struct sjcakioi {     void build() {       for (int i = 131072; i < 131072 + 131072; i++)         node[i] = (mod_sjc){sjctxdy(sjctql[i - 131072]), sjctxdy(1)};       for (int i = 131072 - 1; i >= 1; i--)         node[i].v = node[i + i + 1].v + node[i + i].v, node[i].mul = sjctxdy(1);     }     void pushup(int v) { node[v].v = node[v + v].v + node[v + v + 1].v; }     void pushdown(int v) {       if (node[v + v + 1].v.val != -1) {         node[v + v + 1].mul = node[v + v + 1].mul * node[v].mul;         node[v + v + 1].v = node[v + v + 1].v * node[v].mul;       }       if (node[v + v].v.val != -1) {         node[v + v].mul = node[v + v].mul * node[v].mul;         node[v + v].v = node[v + v].v * node[v].mul;       }       node[v].mul = sjctxdy(1);     }     void AK_init(int k, int l, int r, Num v, int a = 0, int b = 131072 - 1) {       if (r < a || b < l) return;       if (l <= a && b <= r) {         node[k].v = node[k].v * v;         node[k].mul = node[k].mul * v;         return;       }       pushdown(k);       AK_init(k + k, l, r, v, a, (a + b) >> 1);       AK_init(k + k + 1, l, r, v, ((a + b) >> 1) + 1, b);       pushup(k);     }     Num AK(int k, int l, int r, int a = 0, int b = 131072 - 1) {       if (r < a || b < l) return sjctxdy(-1);       if (l <= a && b <= r) {         return node[k].v;       }       pushdown(k);       Num ret = AK(k + k, l, r, a, a + b >> 1) +                 AK(k + k + 1, l, r, (a + b >> 1) + 1, b);       return ret;     }   } orzsjc;   int main() {     scanf( %d%d , &n, &mod);     int t = mod;     for (int i = 2; i * i <= mod; i++) {       if (t % i == 0) {         pr[cnt++] = i;         while (t % i == 0) t /= i;       }     }     if (t != 1) pr[cnt++] = t;     for (int i = 0; i < cnt; i++)       for (int j = 0; j < 2000000; j++) {         pws[i][j] = (j == 0 ? 1 : (long long)pws[i][j - 1] * pr[i] % mod);       }     for (int i = 0; i < n; i++) scanf( %d , sjctql + i);     orzsjc.build();     scanf( %d , &q);     while (q--) {       int type;       scanf( %d , &type);       if (type == 1) {         int l, r, x;         scanf( %d%d%d , &l, &r, &x);         orzsjc.AK_init(1, l - 1, r - 1, sjctxdy(x));       } else if (type == 2) {         int p, x;         scanf( %d%d , &p, &x);         orzsjc.AK_init(1, p - 1, p - 1, sjctxdy(x).inv());       } else if (type == 3) {         int l, r;         scanf( %d%d , &l, &r);         printf( %d n , orzsjc.AK(1, l - 1, r - 1).getreallyvalue());       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 5;   int st[maxn << 2];   int a[maxn], b[maxn];   vector<int> pos[maxn];   void build(int id, int l, int r) {     if (l == r) {       st[id] = a[l];       return;     }     int mid = (l + r) >> 1;     build(id << 1, l, mid);     build(id << 1 | 1, mid + 1, r);     st[id] = min(st[id << 1], st[id << 1 | 1]);   }   void update(int id, int l, int r, int u, int val) {     if (l > u || r < u) return;     if (l == r) {       st[id] = val;       return;     }     int mid = (l + r) >> 1;     update(id << 1, l, mid, u, val);     update(id << 1 | 1, mid + 1, r, u, val);     st[id] = min(st[id << 1], st[id << 1 | 1]);   }   int GetMin(int id, int l, int r, int u, int v) {     if (l > v || r < u) return 1e9;     if (u <= l && r <= v) return st[id];     int mid = (l + r) >> 1;     return min(GetMin(id << 1, l, mid, u, v),                GetMin(id << 1 | 1, mid + 1, r, u, v));   }   string Solve() {     int n;     cin >> n;     for (int i = (1), _b = (n); i <= _b; ++i) pos[i].clear();     for (int i = (1), _b = (n); i <= _b; ++i) cin >> a[i];     for (int i = (n), _b = (1); i >= _b; --i) pos[a[i]].push_back(i);     for (int i = (1), _b = (n); i <= _b; ++i) cin >> b[i];     build(1, 1, n);     int l = 1;     for (int i = (1), _b = (n); i <= _b; ++i) {       if (pos[b[i]].empty()) return  NO ;       int nxt = pos[b[i]].back();       pos[b[i]].pop_back();       if (GetMin(1, 1, n, l, nxt) != b[i]) return  NO ;       update(1, 1, n, nxt, 1e9);     }     return  YES ;   }   int main() {     iostream::sync_with_stdio(false);     cin.tie(0);     int q;     cin >> q;     for (int i = (1), _b = (q); i <= _b; ++i) cout << Solve() <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int q;     cin >> q;     while (q--) {       long long int n;       cin >> n;       long long int x, y;       int l, u, r, s;       long long int nl = -100000, nu = 100000, nr = 100000, ns = -100000;       for (long long int i = 0; i < n; i++) {         cin >> x >> y >> l >> u >> r >> s;         if (l == 0 && nl <= x) {           nl = x;         }         if (u == 0 && nu >= y) {           nu = y;         }         if (r == 0 && nr >= x) {           nr = x;         }         if (s == 0 && ns <= y) {           ns = y;         }       }       if (ns <= nu && nl <= nr) {         std::cout << 1 <<     << nl <<     << ns << std::endl;       } else {         std::cout << 0 << std::endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long A = 31, mod = 1000000033LL;   vector<string> read(int n) {     vector<string> ans(n);     for (auto &x : ans) cin >> x;     return ans;   }   vector<long long> hsh(vector<string> vec, long long a) {     vector<long long> ans;     for (auto x : vec) {       long long h = 0;       for (auto y : x) h = (h * a + (y -  a )) % mod;       ans.push_back(h);     }     return ans;   }   long long hsh(vector<long long> &vec, int beg, int end, long long a) {     long long h = 0;     for (int i = beg; i < end; i++) h = (h * a + vec[i]) % mod;     return h;   }   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     long long Am = 1;     for (int i = 0; i < m; i++) Am = Am * A % mod;     auto m1 = read(n), tmp2 = read(m);     vector<string> m2(n, string(m,    ));     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) m2[i][j] = tmp2[j][i];     auto h1 = hsh(m1, A), h2 = hsh(m2, Am);     vector<pair<long long, int>> vec;     for (int i = 0; i + m <= n; i++) vec.emplace_back(hsh(h1, i, i + m, Am), i);     sort(vec.begin(), vec.end());     for (int i = 0; i + m <= n; i++) {       auto h = hsh(h2, i, i + m, A);       auto p = lower_bound(vec.begin(), vec.end(), make_pair(h, 0));       if (p != vec.end() && p->first == h) {         cout << p->second + 1 <<     << i + 1 << endl;         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   vector<string> split(const string& s, const string& delim =    ) {     vector<string> res;     string t;     for (int i = 0; i != s.size(); i++) {       if (delim.find(s[i]) != string::npos) {         if (!t.empty()) {           res.push_back(t);           t =   ;         }       } else {         t += s[i];       }     }     if (!t.empty()) {       res.push_back(t);     }     return res;   }   vector<int> splitInt(const string& s, const string& delim =    ) {     vector<string> tok = split(s, delim);     vector<int> res;     for (int i = 0; i != tok.size(); i++) res.push_back(atoi(tok[i].c_str()));     return res;   }   template <typename T>   void print(T a) {     cerr << a;   }   static void print(long long a) { cerr << a <<  L ; }   static void print(string a) { cerr <<     << a <<    ; }   template <typename T>   void print(vector<T> a) {     cerr <<  { ;     for (int i = 0; i != a.size(); i++) {       if (i != 0) cerr <<  ,  ;       print(a[i]);     }     cerr <<  }  << endl;   }   char flag[1000005];   int main() {     string s;     while (cin >> s) {       memset(flag, 0, sizeof(flag));       reverse(s.begin(), s.end());       for (int i = 0, _n = (s.size()); i < _n; i++) flag[i] = s[i] -  0 ;       int i = 0, n = s.size();       while (i < n) {         int count = 0;         while (i < n && flag[i + count] == 1) count++;         if (count > 1) {           for (int j = (i), _n = (i + count - 1); j <= _n; j++) flag[j] = 0;           flag[i] = -1;           flag[i + count] = 1;           i = i + count - 1;         }         i++;       }       int count = 0;       for (int i = 0, _n = (n + 3); i < _n; i++) count += (flag[i] != 0);       printf( %d n , count);       for (int i = (0), _n = (n + 3); i <= _n; i++)         if (flag[i] != 0) {           if (flag[i] < 0)             printf( - );           else             printf( + );           printf( 2^%d n , i);         }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Node {     long long val, pos;   };   deque<long long> dq;   bool flag = false;   void go(long long a, long long b) {     if (a > b) return;     if (a == b) {       cout <<  YES  << endl;       dq.push_back(b);       cout << dq.size() << endl;       flag = true;       auto it = dq.begin();       while (it != dq.end()) {         cout << *it <<    ;         it++;       }       cout << endl;     }     dq.push_back(a);     go(a * 2, b);     go(a * 10 + 1, b);     dq.pop_back();   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long a, b;     cin >> a >> b;     go(a, b);     if (flag == false) cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   const long double EPSILON = 0.000000001;   const long long int mod = 1000000007;   const long long int AM = 2e5 + 5;   const long double pi = 3.14159265358979323846;   long long int max(long long int a, long long int b) {     if (a > b)       return a;     else       return b;   }   using namespace std;   long long int fact(long long int n) {     long long int ans = 1;     for (long long int i = 1; i <= n; i++) ans *= i;     return ans;   }   long long int powm(long long int a, long long int b, long long int n) {     long long int x = 1, y = a;     while (b > 0) {       if (b % 2 == 1) {         x = (x * y) % n;       }       y = (y * y) % n;       b /= 2;     }     return x % n;   }   long long int pow(long long int x, long long int y) {     long long int res = 1;     while (y > 0) {       if (y & 1) res = ((res) * (x));       y = y >> 1;       x = ((x) * (x));     }     return res;   }   long long int min(long long int a, long long int b) {     if (a > b)       return b;     else       return a;   }   bool cmp(long double x, long double y) {     if (abs(x - y) <= EPSILON) return 1;     return 0;   }   long long int nCrModp(long long int n, long long int r, long long int p) {     if (r > n - r) r = n - r;     long long int C[r + 1];     memset(C, 0, sizeof(C));     C[0] = 1;     for (long long int i = 1; i <= n; i++) {       for (long long int j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p;     }     return C[r];   }   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int NcR(long long int n, long long int r) {     long long p = 1, k = 1;     if (n - r < r) r = n - r;     if (r != 0) {       while (r) {         p *= n;         k *= r;         long long m = gcd(p, k);         p /= m;         p %= mod;         k /= m;         n--;         r--;       }     } else       p = 1;     return p;   }   int main() {     long long int n;     cin >> n;     vector<long long int> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     sort((v).begin(), (v).end());     long long int cnt = 0;     long long int sum = 0;     for (int i = 0; i < n; i++) {       if (sum <= v[i]) cnt++;       if (sum <= v[i]) sum += v[i];     }     cout << cnt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   struct zt {     int ss, ii;   } frm[1 << 22], ans[25];   struct node {     int l, r, t, id;   } a[25], b[25];   bool cmp1(node aa, node bb) { return aa.l < bb.l; }   bool cmp2(node aa, node bb) { return aa.t < bb.t; }   int N, P, S, f[1 << 22];   void dfs(int s, int ii) {     if (!s) return;     ans[frm[s].ii] = (zt){frm[s].ss, ii};     dfs(s ^ (1 << (frm[s].ii - 1)), ii);   }   void SLV1() {     if (f[S] == inf) {       puts( NO );       exit(0);     }     puts( YES );     dfs(S, 1);     for (int i = 1; i <= N; i++) {       printf( %d %d n , ans[i].ii, ans[i].ss);     }   }   void SLV2() {     int s1 = 0;     int s2 = -233;     for (; s1 <= S; s1++) {       if (f[s1] < inf && f[S ^ s1] < inf) {         s2 = (S ^ s1);         break;       }     }     if (s2 == -233) {       puts( NO );       exit(0);     }     puts( YES );     dfs(s1, 1);     dfs(s2, 2);     for (int i = 1; i <= N; i++) {       printf( %d %d n , ans[i].ii, ans[i].ss);     }   }   int main() {     scanf( %d%d , &N, &P);     for (int i = 1; i <= N; i++) {       scanf( %d%d%d , &a[i].l, &a[i].r, &a[i].t);       a[i].id = i;       a[i].r += a[i].l - 1;       b[i] = a[i];     }     sort(a + 1, a + 1 + N, cmp1);     sort(b + 1, b + 1 + N, cmp2);     S = (1 << N) - 1;     memset(f, 0x3f, sizeof f);     f[0] = 1;     for (int s = 0; s < S; s++) {       if (f[s] != inf) {         int tim = f[s];         int pit = 1;         for (int i = 1; i <= N; i++) {           if ((s >> (b[i].id - 1)) & 1) continue;           for (; pit <= N && tim + b[i].t >= a[pit].l; pit++) {             if (tim >= a[pit].l || ((s >> (a[pit].id - 1)) & 1))               tim = max(tim, a[pit].r + 1);           }           if (tim + b[i].t < b[i].l) {             if (f[s | (1 << (b[i].id - 1))] > tim + b[i].t) {               f[s | (1 << (b[i].id - 1))] = tim + b[i].t;               frm[s | (1 << (b[i].id - 1))] = (zt){tim, b[i].id};             }           }         }       }     }     if (P == 1)       SLV1();     else       SLV2();   }


#include <bits/stdc++.h>   using namespace std;   int b[1111][1111], n, m, k, q1, q2, xs, ys, step = 0, x, y, c;   vector<char> v;   char a[1111][1111];   queue<pair<int, int> > q;   void add(int x, int y, int c) {     if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] ==  *  || b[x][y] <= c)       return;     b[x][y] = c;     q.push(make_pair(x, y));   }   bool check(int x, int y) {     if (step + b[x][y] > k) return false;     if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] ==  * ) return false;     return true;   }   int main() {     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] ==  X ) {           xs = i;           ys = j;         }       }     for (int i = 0; i <= n; i++)       for (int j = 0; j <= m; j++) b[i][j] = 1e9;     b[xs][ys] = 0;     q.push(make_pair(xs, ys));     while (!q.empty()) {       x = q.front().first;       y = q.front().second;       q.pop();       c = b[x][y] + 1;       add(x + 1, y, c);       add(x - 1, y, c);       add(x, y + 1, c);       add(x, y - 1, c);     }     int x = xs;     int y = ys;     while (81 == 81) {       if (v.size() == k && x == xs && y == ys) break;       step = v.size();       if (check(x + 1, y)) {         v.push_back( D );         x++;         continue;       }       if (check(x, y - 1)) {         v.push_back( L );         y--;         continue;       }       if (check(x, y + 1)) {         v.push_back( R );         y++;         continue;       }       if (check(x - 1, y)) {         v.push_back( U );         x--;         continue;       }       cout <<  IMPOSSIBLE ;       return 0;     }     for (int i = 0; i < v.size(); i++) cout << v[i];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, in;   long long shift(long long i) { return (1 << i); }   void getx(long long x, long long y) {     cout <<  ?   << x <<     << y << endl;     cin >> in;     return;   }   int main() {     ios_base::sync_with_stdio(false);     long long curbit = 29;     getx(0, 0);     long long start = in;     while (curbit >= 0) {       if (start == 0) {         getx(a ^ shift(curbit), b);         if (in == -1) {           a |= shift(curbit);           b |= shift(curbit);           getx(a, b);           start = in;         }         curbit--;         continue;       }       getx(a ^ shift(curbit), b ^ shift(curbit));       while (in == start) {         if (start == 1) {           getx(a ^ shift(curbit), b);           if (in == -1) {             a |= shift(curbit);             b |= shift(curbit);           }         } else {           getx(a, b ^ shift(curbit));           if (in == 1) {             a |= shift(curbit);             b |= shift(curbit);           }         }         if (curbit == 0) break;         curbit--;         getx(a ^ shift(curbit), b ^ shift(curbit));       }       if (in != start) {         if (start == 1)           a |= shift(curbit);         else if (start == 0) {           a |= shift(curbit);           b |= shift(curbit);         } else           b |= shift(curbit);         getx(a, b);         start = in;       }       curbit--;     }     cout <<  !   << a <<     << b << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   char arr[1002][7];   bool sit = false;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %s , arr[i]);       if (!sit) {         if (arr[i][0] ==  O  && arr[i][1] ==  O ) {           sit = true;           arr[i][0] = arr[i][1] =  + ;         } else if (arr[i][3] ==  O  && arr[i][4] ==  O ) {           sit = true;           arr[i][3] = arr[i][4] =  + ;         }       }     }     if (sit) {       printf( YES n );       for (int i = 0; i < n; i++) printf( %s n , arr[i]);     } else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, x, y;   int main() {     cin >> x >> y >> m;     if (x >= m || y >= m) {       cout << 0 << endl;       return 0;     }     if ((x <= 0 && y <= 0)) {       cout <<  -1  << endl;       return 0;     }     long long ans = 0;     while (x < m && y < m) {       if (x < 0) {         ans += ((-x) / y);         x = x + ((-x) / y) * y;       } else if (y < 0) {         ans += ((-y) / x);         y = y + ((-y) / x) * x;       }       ans++;       if (x < y)         x = x + y;       else         y = y + x;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int md = 1000000007;   const int maxn = 30303;   const int maxk = 220;   const int maxn4 = 4 * maxn;   const long long inf = 2020202020202020202LL;   int n, k, a[maxn];   int ans[maxn][maxk], ansp[maxn][maxk], ansm[maxn][maxk], t1, t2, t3, t4, t5, t6;   int main() {     ios::ios_base::sync_with_stdio(false);     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i + 1];     int mx = -md, mn = -md;     for (int i = 0; i < maxn; i++)       for (int j = 0; j < maxk; j++) {         ansp[i][j] = -md;         ansm[i][j] = -md;         ans[i][j] = -md;       }     for (int i = 1; i <= n; i++) {       mx = max(mx + a[i], a[i]);       ansp[i][1] = mx;       mn = max(mn - a[i], -a[i]);       ansm[i][1] = mn;     }     for (int w = 2; w <= k; w++) {       t1 = ansp[w - 1][w - 1];       t2 = ansm[w - 1][w - 1];       t3 = ansm[w - 1][w - 1];       t4 = ansp[w - 1][w - 1];       t5 = ansm[w - 1][w - 1];       t6 = ansp[w - 1][w - 1];       for (int i = w; i <= n; i++) {         ans[i][w] = max(t5 + a[i], t6 - a[i]);         ansp[i][w] = max(t1, t3 + 2 * a[i]);         ansm[i][w] = max(t2, t4 - 2 * a[i]);         t1 = max(t1, ansp[i][w - 1]);         t2 = max(t2, ansm[i][w - 1]);         t3 = max(t3 + 2 * a[i], ansm[i][w - 1]);         t4 = max(t4 - 2 * a[i], ansp[i][w - 1]);         t5 = max(t5 + a[i], ansm[i][w - 1]);         t6 = max(t6 - a[i], ansp[i][w - 1]);       }     }     int an = -md;     for (int i = 1; i <= n; i++) an = max(an, ans[i][k]);     cout << an;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 5;   const long long inf = 1e18;   int f[maxn][32], rt;   long long ans, a[maxn];   vector<int> G[maxn];   void dfs(int u, int p) {     long long d = 0;     f[u][0] = p;     for (d = 1; d <= 20; d++) {       if (f[u][d - 1] != -1) f[u][d] = f[f[u][d - 1]][d - 1];     }     long long tmp = inf;     for (d = 0; d <= 20; d++) {       if (f[u][d] != -1)         tmp = min(tmp, (d + 1) * a[f[u][d]] + a[u]);       else         break;     }     tmp = min(tmp, (d + 1) * a[rt] + a[u]);     if (p != -1) ans += tmp;     for (int v : G[u])       if (v != p) dfs(v, u);   }   int main() {     ios::sync_with_stdio(false);     memset(f, -1, sizeof(f));     int n;     cin >> n;     a[rt] = inf;     for (int i = 1; i <= n; i++) {       cin >> a[i];       if (a[i] < a[rt]) rt = i;     }     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     dfs(rt, -1);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const long long INF = 2e18;   const int N = 1231234;   int n, m;   int a[N], b[N];   bool ok(long long x) {     int i, j;     for (i = 0, j = 0; i < m; ++i) {       while (j < n && abs(b[i] - a[j]) <= x) j++;     }     return j == n;   }   int main() {     while (cin >> n >> m) {       int i, j;       for (i = 0; i < n; ++i) scanf( %d , &a[i]);       for (i = 0; i < m; ++i) scanf( %d , &b[i]);       long long l = 0, r = INF;       while (l <= r) {         long long mid = (l + r) >> 1;         if (ok(mid))           r = mid - 1;         else           l = mid + 1;       }       printf( %I64d n , l);     }   }


#include <bits/stdc++.h>   using namespace std;   int n, a[3][55], b[55];   int sum(int row, int f, int e) {     int res = 0;     for (int i = f; i <= e; ++i) res += a[row][i];     return res;   }   int main() {     cin >> n;     for (int x = 1; x <= 2; ++x)       for (int i = 1; i < n; ++i) cin >> a[x][i];     for (int i = 1; i <= n; ++i) cin >> b[i];     int ans = (1 << 30);     for (int i = 1; i < n; ++i)       for (int j = i + 1; j <= n; ++j)         ans = min(ans, sum(1, 1, i - 1) + sum(2, i, n - 1) + b[i] +                            sum(1, 1, j - 1) + sum(2, j, n - 1) + b[j]);     cout << ans;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   int n, m;   string s[100000];   int a[100000];   int b[100000];   string name;   int id;   vector<pair<int, string> > All;   bool canMaxi(int k) {     pair<int, string> He = make_pair(-a[id], name);     He.first -= b[n - 1];     for (int i = k - 1; i >= 0; i--) {       pair<int, string> I = All[i];       I.first -= b[k - 1 - i];       if (He < I) return false;     }     return true;   }   int findMaxi() {     int l = n - 1, r = n - 1;     for (int i = 0; i < (int)((All).size()); i++)       if (make_pair(-a[id], name) < All[i]) {         l = i;         break;       }     while (r - l > 1) {       int k = (l + r) / 2;       if (canMaxi(k))         l = k;       else         r = k - 1;     }     if (l != r && canMaxi(r)) l++;     return l;   }   bool canMini(int k) {     pair<int, string> He = make_pair(-a[id], name);     He.first -= b[0];     for (int i = k; i < n - 1; i++) {       pair<int, string> I = All[i];       I.first -= b[(n - 1) - (i - k)];       if (!(He < I)) return false;     }     return true;   }   int findMini() {     int l = 0, r = n - 1;     for (int i = (int)((All).size()) - 1; i >= 0; i--)       if (make_pair(-a[id], name) < All[i]) {         r = i;         break;       }     while (r - l > 1) {       int k = (l + r) / 2;       if (canMini(k))         r = k;       else         l = k + 1;     }     if (l != r && canMini(l)) r--;     return r;   }   void solution() {     scanf( %d n , &n);     for (int i = 0, _n = (n); i < _n; ++i) {       char t[64];       scanf( %s %d n , t, a + i);       s[i] = string(t);     }     scanf( %d , &m);     for (int i = 0, _n = (m); i < _n; ++i) scanf( %d n , b + i);     sort(b, b + n, greater<int>());     cin >> name;     for (int i = 0, _n = (n); i < _n; ++i)       if (s[i] == name) {         id = i;         break;       }     for (int i = 0, _n = (n); i < _n; ++i)       if (i != id) All.push_back(make_pair(-a[i], s[i]));     sort((All).begin(), (All).end());     int mini = findMini();     int maxi = findMaxi();     printf( %d %d n , mini + 1, maxi + 1);   }   int main() {     solution();     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:20000000 )   using namespace std;   template <typename T1, typename T2, typename T3>   struct triple {     T1 a;     T2 b;     T3 c;     triple(){};     triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}   };   template <typename T1, typename T2, typename T3>   bool operator<(const triple<T1, T2, T3> &t1, const triple<T1, T2, T3> &t2) {     if (t1.a != t2.a)       return t1.a < t2.a;     else       return t1.b < t2.b;   }   template <typename T1, typename T2>   inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T>   inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {     bool first = true;     os <<  [ ;     for (unsigned int i = 0; i < v.size(); i++) {       if (!first) os <<  ,  ;       os << v[i];       first = false;     }     return os <<  ] ;   }   template <typename T>   inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) {     bool first = true;     os <<  [ ;     for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end();          ++ii) {       if (!first) os <<  ,  ;       os << *ii;       first = false;     }     return os <<  ] ;   }   template <typename T1, typename T2>   inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) {     bool first = true;     os <<  [ ;     for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end();          ++ii) {       if (!first) os <<  ,  ;       os << *ii;       first = false;     }     return os <<  ] ;   }   template <typename T, typename T2>   void printarray(T a[], T2 sz, T2 beg = 0) {     for (T2 i = beg; i < sz; i++) cout << a[i] <<    ;   }   inline long long binpow(long long x, long long n) {     long long res = 1;     while (n) {       if (n & 1) res *= x;       x *= x;       n >>= 1;     }     return res;   }   inline long long powmod(long long x, long long n, long long _mod) {     long long res = 1;     while (n) {       if (n & 1) res = (res * x) % _mod;       x = (x * x) % _mod;       n >>= 1;     }     return res;   }   inline long long mulmod(long long x, long long n, long long _mod) {     long long res = 0;     while (n) {       if (n & 1) res = (res + x) % _mod;       x = (x + x) % _mod;       n >>= 1;     }     return res;   }   inline long long gcd(long long a, long long b) {     long long t;     while (b) {       a = a % b;       t = a;       a = b;       b = t;     }     return a;   }   inline int gcd(int a, int b) {     int t;     while (b) {       a = a % b;       t = a;       a = b;       b = t;     }     return a;   }   inline long long lcm(int a, int b) { return a / gcd(a, b) * (long long)b; }   inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   inline long long gcd(long long a, long long b, long long c) {     return gcd(gcd(a, b), c);   }   inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }   inline long long lcm(long long a, long long b, long long c) {     return lcm(lcm(a, b), c);   }   inline long long lcm(int a, int b, int c) {     return lcm(lcm(a, b), (long long)c);   }   inline long long max(long long a, long long b) { return (a > b) ? a : b; }   inline int max(int a, int b) { return (a > b) ? a : b; }   inline double max(double a, double b) { return (a > b) ? a : b; }   inline long long max(long long a, long long b, long long c) {     return max(a, max(b, c));   }   inline int max(int a, int b, int c) { return max(a, max(b, c)); }   inline double max(double a, double b, double c) { return max(a, max(b, c)); }   inline long long min(long long a, long long b) { return (a < b) ? a : b; }   inline int min(int a, int b) { return (a < b) ? a : b; }   inline double min(double a, double b) { return (a < b) ? a : b; }   inline long long min(long long a, long long b, long long c) {     return min(a, min(b, c));   }   inline int min(int a, int b, int c) { return min(a, min(b, c)); }   inline double min(double a, double b, double c) { return min(a, min(b, c)); }   inline void getar(int *a, int n) {     for (int ii = 0; ii < n; ii++) {       scanf( %d , a + ii);     }   }   inline void getar(long long *a, int n) {     for (int ii = 0; ii < n; ii++) {       scanf( %I64 , a + ii);     }   }   bool a[1621][1621];   bool used[1621][1621];   bool used2[1621][1621];   bool used3[1621][1621];   const int maxl = 15;   const int maxs = 10;   int w, h;   int k = 0;   int cur = 0;   int basei, basej;   triple<int, int, int> q[10000];   int qs = 0;   void go2(int i, int j) {     if (((abs(i - basei) + abs(j - basej)) >= maxl) || (!a[i][j]) || used2[i][j])       return;     used2[i][j] = true;     qs = 0;     q[qs].a = i;     q[qs].b = j;     q[qs++].c = 0;     int qc = 0;     while (qc < qs) {       int curi = q[qc].a;       int curj = q[qc].b;       int curl = q[qc++].c;       if (curl == maxl) break;       for (int t = -1; t <= 1; t++)         for (int w = -1; w <= 1; w++) {           if (!used2[curi + t][curj + w] && a[curi + t][curj + w]) {             used2[curi + t][curj + w] = true;             q[qs].a = curi + t;             q[qs].b = curj + w;             q[qs++].c = curl + 1;           }         }     }   }   vector<pair<int, int>> v3;   bool check(int i, int j) {     int s = 0;     qs = 0;     q[qs].a = i;     q[qs].b = j;     q[qs++].c = 0;     used3[i][j] = true;     int qc = 0;     v3.clear();     while (qc < qs) {       int curi = q[qc].a;       int curj = q[qc].b;       int curl = q[qc++].c;       if (max(abs(basei - curi), abs(basej - curj)) > 2) continue;       if (a[curi][curj]) s++;       for (int t = -1; t <= 1; t++)         for (int w = -1; w <= 1; w++) {           if (!used3[curi + t][curj + w]) {             used3[curi + t][curj + w] = true;             v3.push_back(make_pair(curi + t, curj + w));             q[qs].a = curi + t;             q[qs].b = curj + w;             q[qs++].c = curl + 1;           }         }     }     for (int i = 0; i < v3.size(); i++) {       used3[v3[i].first][v3[i].second] = false;     }     return (s < maxs);   }   void go(int i, int j) {     if ((!a[i][j]) || used[i][j]) return;     used[i][j] = true;     go(i - 1, j);     go(i + 1, j);     go(i, j - 1);     go(i, j + 1);     if (used2[i][j]) return;     int t = 0;     if (a[i - 1][j] == 0) t++;     if (a[i][j - 1] == 0) t++;     if (a[i + 1][j] == 0) t++;     if (a[i][j + 1] == 0) t++;     if (t >= 2) {       basei = i;       basej = j;       if (check(i, j)) {         cur++;         go2(i, j);       }     }   }   vector<int> ans;   int main() {     scanf( %d , &w);     scanf( %d , &h);     int x;     for (int i = 5; i < w + 5; i++) {       for (int j = 5; j < h + 5; j++) {         scanf( %d , &x);         a[i][j] = x;         used[i][j] = used2[i][j] = false;       }     }     for (int i = 5; i < w + 5; i++) {       for (int j = 5; j < h + 5; j++) {         if ((!used[i][j]) && a[i][j]) {           k++;           cur = 0;           go(i, j);           ans.push_back(cur);         }       }     }     cout << k << endl;     sort(ans.begin(), ans.end());     for (int i = 0; i < k; i++) {       cout << ans[i] <<    ;     }     cout << endl;     return 0;   }