#include <bits/stdc++.h>   using namespace std;   const int N = 25;   const int mo = 1000000007;   const int inf = (int)1e9;   inline int IN() {     char ch;     (ch = getchar());     int f = 0, x = 0;     for (; ((ch) ==   n  || (ch) ==   r  || (ch) ==   t  || (ch) ==    );          (ch = getchar()))       ;     if (ch ==  - ) f = 1, (ch = getchar());     for (; !((ch) ==   n  || (ch) ==   r  || (ch) ==   t  || (ch) ==    );          (ch = getchar()))       x = x * 10 + ch -  0 ;     return (f) ? (-x) : (x);   }   int Pow(int x, int y, int p) {     int A = 1;     for (; y; y >>= 1, x = (long long)x * x % p)       if (y & 1) A = (long long)A * x % p;     return A;   }   int n, m, laba[N], labb[N], visa[N], visb[N], tim, mat[N], ans;   int w[N][N], a[N][N], b[N][N], c[N][N], tot_done;   bool extend(int x) {     if (!x) return 1;     visa[x] = tim;     for (int i = (int)1; i <= (int)n; i++) {       if (visb[i] != tim && laba[x] + labb[i] == w[x][i]) {         visb[i] = tim;         if (extend(mat[i])) {           mat[i] = x;           return 1;         }       }     }     return 0;   }   void change() {     int delta = inf;     for (int i = (int)1; i <= (int)n; i++)       if (visa[i] == tim)         for (int j = (int)1; j <= (int)n; j++)           if (visb[j] != tim) {             delta = min(delta, laba[i] + labb[j] - w[i][j]);           }     for (int i = (int)1; i <= (int)n; i++)       if (visa[i] == tim) laba[i] -= delta;     for (int i = (int)1; i <= (int)n; i++)       if (visb[i] == tim) labb[i] += delta;   }   int KM() {     for (int i = (int)1; i <= (int)n; i++) visa[i] = visb[i] = mat[i] = 0;     for (int i = (int)1; i <= (int)n; i++) laba[i] = w[i][0], labb[i] = 0;     tim = 0;     for (int i = (int)1; i <= (int)n; i++) {       for (;;) {         ++tim;         if (extend(i)) break;         change();       }     }     int re = 0;     for (int i = (int)1; i <= (int)n; i++) re += w[mat[i]][i];     return re;   }   void dfs(int x, int cnt) {     int now = KM();     if (now <= ans) return;     if (x == n) {       ans = now;       return;     }     ++tot_done;     if (tot_done >= 1e5) return;     ++x;     if (m - cnt < n - x + 1) {       for (int i = (int)0; i <= (int)n; i++) w[x][i] = a[x][i];       dfs(x, cnt);     }     if (cnt < m) {       for (int i = (int)0; i <= (int)n; i++) w[x][i] = b[x][i];       dfs(x, cnt + 1);     }     for (int i = (int)0; i <= (int)n; i++) w[x][i] = c[x][i];   }   int main() {     scanf( %d , &n);     m = n >> 1;     for (int i = (int)1; i <= (int)n; i++)       for (int j = (int)1; j <= (int)n; j++) {         a[i][j] = IN();         a[i][0] = max(a[i][0], a[i][j]);       }     for (int i = (int)1; i <= (int)n; i++)       for (int j = (int)1; j <= (int)n; j++) {         b[i][j] = IN();         b[i][0] = max(b[i][0], b[i][j]);       }     for (int i = (int)1; i <= (int)n; i++)       for (int j = (int)1; j <= (int)n; j++) {         w[i][j] = c[i][j] = max(a[i][j], b[i][j]);         w[i][0] = c[i][0] = max(w[i][0], c[i][j]);       }     dfs(0, 0);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e3 + 10, big = 1e18, mod = 1e9 + 7;   vector<long long> a[N], now, ans;   long long n, x, y, m, maxi;   bool vis[N], mp[N][N];   void dfs(long long i) {     vis[i] = 1, now.push_back(i);     for (int j = 0; j < a[i].size(); j++)       if (!vis[a[i][j]]) dfs(a[i][j]);   }   int main() {     cin >> n;     cin >> m;     while (m--) {       cin >> x >> y;       a[x].push_back(y);       a[y].push_back(x);     }     cin >> m;     while (m--) {       cin >> x >> y;       mp[x][y] = mp[y][x] = 1;     }     for (int i = 1; i <= n; i++)       if (!vis[i]) {         now.clear();         dfs(i);         bool check = 1;         if (now.size() == 1)           maxi = max(1LL, maxi);         else {           for (int j = 0; j < now.size() - 1; j++)             for (int k = j + 1; k < now.size(); k++)               if (mp[now[j]][now[k]]) check = 0;           if (check) maxi = max((long long)now.size(), maxi);         }       }     cout << maxi;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> V;   void initSet(int _size) {     V.resize(_size);     for (int i = int(0); i <= int(_size - 1); i++) V[i] = i;   }   int findSet(int i) { return (V[i] == i) ? i : (V[i] = findSet(V[i])); }   void unionSet(int i, int j) { V[findSet(i)] = findSet(j); }   bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }   long long a, b, s, N, M, A, B, r[123450];   int find(int node) {     if (r[node]) return r[node] = find(r[node]);     return node;   }   int main() {     int N = 0;     cin >> N >> M;     s = 1;     for (int i = 1; i <= M; ++i) {       cin >> a >> b;       A = find(a);       B = find(b);       if (A == B) {         s = s * 2 % 1000000009;       } else {         r[B] = A;       }       if (s > 0)         cout << s - 1 << endl;       else         cout << 1000000009 - 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 3e5 + 5;   const long long INF = 1e18;   const long long mod = 1e9 + 7;   void solve() {     long long n, m, k;     string s;     cin >> n;     map<int, int> F;     while (true) {       F[n] = 1;       n = n + 1;       while (n % 10 == 0) n /= 10;       if (F.find(n) != F.end()) break;     }     cout << F.size();   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> s;   int a[200005];   int b[200005], u[200005];   int n, k;   int main() {     int T;     scanf( %d , &T);     for (; T--;) {       scanf( %d%d , &n, &k);       for (int i = 1; i <= n; i++) scanf( %d , &a[i]);       for (int i = 1; i <= k; i++) scanf( %d , &b[i]);       for (int i = 1; i <= n; i++) u[a[i]] = i;       s.clear();       for (int i = 1; i <= k; i++) s.insert(b[i]);       long long ans = 1;       for (int i = 1; i <= k; i++) {         int x = u[b[i]];         int now = 0;         int y;         if (x > 1) {           y = x - 1;           if (s.find(a[y]) == s.end()) now++;         }         if (x < n) {           y = x + 1;           if (s.find(a[y]) == s.end()) now++;         }         ans = ans * now % 998244353;         s.erase(b[i]);       }       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   const string alf =  abcdefghijklmnopqrstuvwxyz ;   const int hashP = 239017;   const int N = 1e5 + 10;   const int MOD = 1e9 + 7;   const int MOD2 = 998244353;   const int INF = 1e9;   const long long oo = 9e18;   template <typename T>   bool umn(T &a, T b) {     return (a > b ? (a = b, 1) : 0);   }   template <typename T>   bool umx(T &a, T b) {     return (a < b ? (a = b, 1) : 0);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long tt;     cin >> tt;     while (tt--) {       long long n;       cin >> n;       long long k = 2, sum = 1;       while (1) {         sum += k;         k *= 2;         if (!(n % sum)) {           cout << n / sum <<   n ;           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long ans = 0;     long long n;     cin >> n;     vector<long long> a(n), b(n), c(n);     string s1, s2;     cin >> s1 >> s2;     for (long long i = 0; i < n; i++) {       a[i] = (s1[i] ==  0  ? 0 : 1);     }     for (long long i = 0; i < n; i++) {       b[i] = (s2[i] ==  0  ? 0 : 1);       c[i] = a[i] ^ b[i];     }     for (long long i = 1; i < n; i++) {       if (c[i] == 1 && c[i - 1] == 1 && (a[i] != a[i - 1])) {         ans += 1;         c[i] = 0;         c[i - 1] = 0;       }     }     for (long long i = 0; i < n; i++) {       if (c[i] == 1) {         ans += 1;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     while (n <= 70000) {       ++n;       int x = n;       int a[10] = {0};       for (int i = 0; i < 4; i++) {         int m = x % 10;         ++a[m];         x = x / 10;       }       int count = 0;       for (int i = 0; i < 10; i++) {         if (a[i] == 1) ++count;       }       if (count == 4) {         cout << n;         break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     char ch;     bool flag = false;     for (ch = getchar(); !isdigit(ch); ch = getchar())       if (ch ==  - ) flag = true;     for (x = 0; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar())       ;     x = flag ? -x : x;   }   const int INF = 0x3f3f3f3f;   const int N = 1e6 + 7;   int a[N], l[N], r[N];   int ll[N], rr[N];   int main() {     int n, x;     cin >> n >> x;     memset(l, 0x3f3f3f, sizeof(l));     memset(ll, 0x3f3f3f, sizeof(ll));     for (int i = 1; i <= n; i++) {       cin >> a[i];       l[a[i]] = min(i, l[a[i]]);       r[a[i]] = i;     }     long long ans = 0;     for (int i = 1; i <= x; i++) rr[i] = max(r[i], rr[i - 1]);     for (int i = x; i >= 1; i--) ll[i] = min(l[i], ll[i + 1]);     int d = x;     while (ll[d] >= r[d - 1] && d >= 1) d--;     for (int i = 0; i < x; i++) {       if (i != 0 && l[i] < rr[i - 1]) break;       while (d <= i + 1 || rr[i] > ll[d]) {         d++;       }       ans += x - d + 2;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 10, maxk = 1e6 + 10;   int n, k, A[maxn], cnt[maxk], sum[maxk];   int32_t main() {     scanf( %d%d , &n, &k);     for (int i = int(0); i < int(n); i++) {       scanf( %d , A + i);       cnt[A[i]]++;     }     sort(A, A + n);     for (int i = int(1); i < int(maxk); i++) {       sum[i] = sum[i - 1] + cnt[i - 1];     }     int g, c = n, ans = 0;     for (g = 1; g < maxk; g++) {       c = 0;       for (int j = g; j < maxk; j += g) {         c += sum[min(maxk - 1, j + min(k + 1, g))] - sum[j];       }       if (c >= n) ans = g;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, f = 0, d = 0;     cin >> a >> b;     for (int i = 1; i <= 6; i++) {       int d1 = abs(a - i), d2 = abs(b - i);       if (d1 < d2)         f++;       else if (d1 == d2)         d++;     }     cout << f <<     << d <<     << 6 - f - d << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     long long maxi;     maxi = -9999999999;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       int a, b;       long long tmp;       cin >> a >> b;       if (b > k) {         tmp = a - (b - k);       } else {         tmp = a;       }       maxi = max(tmp, maxi);     }     cout << maxi;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 5, maxlen = 5e5 + 5;   const int inf = 3e6 + 5;   int n, x[maxn], y[maxn];   char s[maxlen];   int ansb, ansn;   bool check(int mid, int ty) {     int xmin = 0, xmax = inf, ymin = 0, ymax = inf, dmin = -inf, dmax = inf;     for (int i = 1; i <= n; i++) {       xmin = max(xmin, x[i] - mid);       xmax = min(xmax, x[i] + mid);       ymin = max(ymin, y[i] - mid);       ymax = min(ymax, y[i] + mid);       dmin = max(dmin, x[i] - y[i] - mid);       dmax = min(dmax, x[i] - y[i] + mid);     }     if (xmin > xmax || ymin > ymax || dmin > dmax) return 0;     ymin = max(ymin, xmin - dmax);     ymax = min(ymax, xmax - dmin);     if (ymin > ymax) return 0;     if (ty) {       for (int x = xmin; x <= xmax; x++) {         int rymin = max(ymin, x - dmax), rymax = min(ymax, x - dmin);         if (rymin <= rymax && (x > 0 || rymax > 0)) {           ansb = x, ansn = (x == 0) ? rymax : rymin;           break;         }       }     }     return 1;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %s , s + 1);       int len = strlen(s + 1);       for (int j = 1; j <= len; j++)         if (s[j] ==  B )           x[i]++;         else           y[i]++;     }     int l = 0, r = 2e6;     while (l <= r) {       int mid = (l + r) >> 1;       if (check(mid, 0))         r = mid - 1;       else         l = mid + 1;     }     printf( %d n , ++r);     check(r, 1);     for (int i = 1; i <= ansb; i++) putchar( B );     for (int i = 1; i <= ansn; i++) putchar( N );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int>> g;   vector<int> c;   int root, n;   vector<int> dfs(int u) {     vector<int> combine;     for (int v : g[u]) {       vector<int> child = dfs(v);       for (int z : child) combine.emplace_back(z);     }     if (c[u] > combine.size()) {       cout <<  NO ;       exit(0);     }     combine.insert(combine.begin() + c[u], u);     return combine;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n;     g.resize(n + 1);     c.resize(n + 1);     for (int i = 1; i <= n; i++) {       int p;       cin >> p >> c[i];       if (!p)         root = i;       else         g[p].emplace_back(i);     }     vector<int> order = dfs(root);     vector<int> ans(n + 1);     for (int i = 0; i < order.size(); i++) ans[order[i]] = i + 1;     cout <<  YES n ;     for (int i = 1; i < ans.size(); i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long solve() {     long long n;     cin >> n;     vector<long long> arr(n);     map<long long, long long> counts;     map<long long, long long> checks;     for (auto &x : arr) {       cin >> x;       counts[x]++;     }     for (auto x : counts) {       checks[x.second]++;     }     while (n--) {       if (checks.size() == 1) {         auto x = checks.begin();         if (x->first == 1 or x->second == 1) {           cout << n + 1 << endl;           return 0;         }       }       if (checks.size() == 2) {         auto it1 = checks.begin();         auto it2 = checks.begin();         if (it1->first == 1 and it1->second == 1) {           cout << n + 1 << endl;           return 0;         }         it2++;         if (it2->first - it1->first == 1 and it2->second == 1) {           cout << n + 1 << endl;           return 0;         }       }       long long x = counts[arr[n]];       if (x) {         checks[x]--;         counts[arr[n]]--;         if (x - 1) checks[x - 1]++;         if (!checks[x]) checks.erase(x);       }     }   }   signed main() {     long long t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long int n, a[100008], cnt = 0;   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) {       if (a[i] == 1) {         if (!cnt) cnt++;         int j = i + 1;         while (a[j] != 1 && j <= n) j++;         if (a[j] == 1) {           cnt += min(j - i, 2);           i = j - 1;         }       }     }     cout << cnt << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline void fast_and_furious() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);   }   int n;   long long a[1005], b[1005], u, l, ans;   int main() {     fast_and_furious();     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     ans = 0;     for (int i = 0; i < n; i++) {       u = l = 0;       for (int j = i; j < n; j++) {         u = u | a[j];         l = l | b[j];         ans = max(u + l, ans);       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX_I = 1000;   long long a[MAX_I + 1];   long long b[MAX_I + 1];   int main() {     long long n, m;     long long c[5];     long long tmp;     long long asum = 0;     long long bsum = 0;     cin >> c[1] >> c[2] >> c[3] >> c[4];     cin >> n >> m;     for (long long i = 0; i < n; ++i) {       cin >> tmp;       a[i] = min(c[2], c[1] * tmp);       asum += a[i];     }     for (long long i = 0; i < m; ++i) {       cin >> tmp;       b[i] = min(c[2], c[1] * tmp);       bsum += b[i];     }     asum = min(asum, c[3]);     bsum = min(bsum, c[3]);     cout << min(asum + bsum, c[4]) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int pos[n + 1];     for (int i = 0; i < n; i++) {       int x;       cin >> x;       pos[x] = i;     }     int curIndex = 0;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       if (pos[x] < curIndex) {         cout <<  0  ;       } else {         cout << pos[x] - curIndex + 1 <<    ;         curIndex = pos[x] + 1;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void go() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout << fixed;     cout << setprecision(6);   }   void solve() {     string s;     cin >> s;     long long n = s.size();     vector<long long> arr(n);     long long cur = 0;     for (long long i = 0; i < n; i++) {       if (s[i] ==  + )         cur++;       else         cur--;       arr[i] = cur;     }     map<long long, long long> mp;     for (long long i = n - 1; i >= 0; i--) {       if (arr[i] < 0) mp[arr[i]] = i + 1;     }     long long ans = n;     for (auto x : mp) {       ans += x.second;     }     cout << ans <<   n ;   }   signed main() {     go();     long long t = 1;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e3 + 10;   int T, n, m, a[N], b[N], l, r, ins[N], las[N];   vector<int> to[N];   struct node {     int u;     long long k;     bool f;   };   node q[N];   inline bool check(long long now) {     vector<int> s;     for (int i = 1; i <= n; ++i) ins[i] = las[i] = 0;     s.push_back(1);     ins[1] = 1;     while (s.size() != n) {       memset(las + 1, 0, sizeof(int) * (n));       l = 1;       r = 0;       for (int i : s) q[++r] = (node){i, now, 0};       int x = -1, y = -1;       while (l <= r) {         node p = q[l++];         int u = p.u;         for (int v : to[u]) {           if (v == las[u]) continue;           if (p.k <= a[v]) continue;           if (las[v] || (ins[v] && p.f)) {             x = u;             y = v;             break;           }           if (ins[v]) continue;           las[v] = u;           q[++r] = (node){v, p.k + b[v], 1};         }         if (x != -1) break;       }       if (x == -1) return false;       while (x) {         if (ins[x]) {           x = las[x];           continue;         }         ins[x] = 1;         s.push_back(x);         now += b[x];         x = las[x];       }       while (y) {         if (ins[y]) {           y = las[y];           continue;         }         ins[y] = 1;         s.push_back(y);         now += b[y];         y = las[y];       }     }     return true;   }   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d%d , &n, &m);       for (int i = 2; i <= n; ++i) scanf( %d , &a[i]);       for (int i = 2; i <= n; ++i) scanf( %d , &b[i]);       for (int i = 1, u, v; i <= m; ++i) {         scanf( %d%d , &u, &v);         to[u].push_back(v), to[v].push_back(u);       }       int l = 0, r = 1e9 + 1, ans = 1e9 + 1;       while (l <= r) {         int mid = (l + r) >> 1;         if (check(mid))           r = mid - 1, ans = mid;         else           l = mid + 1;       }       printf( %d n , ans);       for (int i = 1; i <= n; ++i) to[i].clear();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 50;   int n;   int a[5][NMAX + 5];   int b[NMAX + 5];   vector<int> ans;   int main() {     cin >> n;     for (int i = 1; i <= 2; i++)       for (int j = 1; j < n; j++) {         cin >> a[i][j];         a[i][j] += a[i][j - 1];       }     a[1][n] = a[1][n - 1];     a[2][n] = a[2][n - 1];     for (int i = 1; i <= n; i++) cin >> b[i];     for (int i = 1; i <= n; i++) {       int cost = b[i];       cost += a[1][i - 1];       cost += a[2][n - 1] - a[2][i - 1];       ans.push_back(cost);     }     sort(ans.begin(), ans.end());     cout << ans[0] + ans[1] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NM = 1e5 + 10;   int color[NM];   vector<pair<int, char> > adj[NM];   bool dfs(int u, bool c, char type, vector<int> &all) {     if (color[u] != -1) return (color[u] == c);     all.push_back(u);     color[u] = c;     for (int i = 0; i < adj[u].size(); i++) {       int v = adj[u][i].first;       char edge = adj[u][i].second;       if (type == edge) {         if (!dfs(v, c, type, all)) return false;       } else {         if (!dfs(v, !c, type, all)) return false;       }     }     return true;   }   vector<int> solve(int n, char type) {     vector<int> ret;     memset(color, -1, sizeof(color));     for (int i = 1; i <= n; i++) {       if (color[i] == -1) {         vector<int> all;         if (!dfs(i, 0, type, all)) {           for (int j = 0; j <= n; j++) {             ret.push_back(-1);           }           return ret;         } else {           int cnt[2] = {0, 0};           for (int j = 0; j < all.size(); j++) {             cnt[color[all[j]]]++;           }           if (cnt[0] > cnt[1]) {             for (int j = 0; j < all.size(); j++) {               color[all[j]] ^= 1;             }           }         }       }     }     for (int i = 1; i <= n; i++) {       if (color[i] == 0) {         ret.push_back(i);       }     }     return ret;   }   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       int u, v;       char c;       scanf( %d %d %c , &u, &v, &c);       adj[u].push_back(make_pair(v, c));       adj[v].push_back(make_pair(u, c));     }     vector<int> a = solve(n,  R ), b = solve(n,  B );     if (a.size() > n && b.size() > n) {       printf( -1 n );     } else {       if (a.size() > b.size()) {         a.swap(b);       }       printf( %d n , a.size());       for (int i = 0; i < a.size(); i++) {         printf( %d  , a[i]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   int main() {     long long b;     scanf( %lld , &b);     long long flag = 0;     for (long long i = 1; i < sqrt(b); i++)       if (b % i == 0) flag = flag + 1;     flag = 2 * flag;     long long a = sqrt(b);     if (a * a == b) flag = flag + 1;     printf( %lld , flag);   }


#include <bits/stdc++.h>   using namespace std;   long long int n;   vector<long long int> BIT((int)1e6 + 5, 0);   void _update(int x) {     while (x <= n) {       BIT[x]++;       x += x & -x;     }   }   int query(int x) {     long long int sum = 0;     while (x > 0) {       sum += BIT[x];       x -= x & -x;     }     return sum;   }   int main() {     long long int k;     cin >> n >> k;     long long int res = 1;     k = min(n - k, k);     int pos = 1;     for (int i = 0; i < n; i++) {       res++;       int fi = pos + k;       if (fi > n) {         fi -= n;       }       if (fi > pos) {         res += query(fi - 1) - query(pos);       } else {         res += query(fi - 1) + query(n) - query(pos);       }       _update(pos);       _update(fi);       pos = fi;       cout << res <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2 * 1000 + 17;   int n, init[MAXN][MAXN], hope[MAXN][MAXN], c[MAXN][MAXN], r[MAXN],       cnt = 1, comp[MAXN << 2];   vector<int> out[MAXN << 2], in[MAXN << 2], order, trash;   vector<pair<string, int>> ans;   void add(int i, bool f1, int j, bool f2) {     int v = (i << 1) | f1, u = (j << 1) | f2;     out[v].push_back(u);     in[u].push_back(v);     out[u ^ 1].push_back(v ^ 1);     in[v ^ 1].push_back(u ^ 1);   }   void scc(int v, vector<int> g[], vector<int> &vec) {     comp[v] = cnt;     for (auto i : g[v])       if (!comp[i]) scc(i, g, vec);     vec.push_back(v);   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     char C;     cin >> n;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) cin >> C, init[i][j] = int(C -  0 );     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         cin >> C, hope[i][j] = int(C -  0 ), c[i][j] = init[i][j] ^ hope[i][j];     for (int i = 1; i <= n; i++) cin >> C, r[i] = int(C -  0 );     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (c[i][j] && r[i] && r[j])           add(i, true, n + j, false), add(i, false, n + j, true);         else if (c[i][j] && r[i] && !r[j])           add(i, true, n + j, true), add(i, false, n + j, true);         else if (c[i][j] && !r[i] && r[j])           add(n + j, true, i, true), add(n + j, false, i, true);         else if (c[i][j] && !r[i] && !r[j])           return cout << -1 << endl, 0;         else if (!c[i][j] && r[i] && r[j])           add(i, true, n + j, true), add(i, false, n + j, false);         else if (!c[i][j] && r[i] && !r[j])           add(i, true, n + j, false), add(i, false, n + j, false);         else if (!c[i][j] && !r[i] && r[j])           add(n + j, true, i, false), add(n + j, false, i, false);         else           ;     for (int i = 1; i <= ((n << 2) | 1); i++)       if (!comp[i]) scc(i, out, order);     cnt = 0;     memset(comp, 0, sizeof(comp));     for (int i = (n << 1); ~i; i--) {       int v = order[i];       if (!comp[v]) cnt++, scc(v, in, trash);     }     for (int i = 1; i <= (n << 1); i++) {       if (comp[i << 1] == comp[i << 1 | 1]) return cout << -1 << endl, 0;       if (comp[i << 1 | 1] > comp[i << 1]) {         if (i > n)           ans.push_back({ col , i - n - 1});         else           ans.push_back({ row , i - 1});       }     }     cout << ans.size() << endl;     for (auto i : ans) cout << i.first <<     << i.second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2e9;   long long int n, h;   bool check(long long int maxh) {     long long int k = min(maxh, h);     long long int count = maxh * maxh - k * (k - 1) / 2;     return (n >= count);   }   long long int get(long long int maxh) {     long long int k = min(h, maxh);     long long int count = maxh * maxh - k * (k - 1) / 2;     long long int length = (2 * maxh - 1) - (k - 1);     n -= count;     return length + (n + maxh - 1) / maxh;   }   int main() {     cin >> n >> h;     long long int l = 1, r = INF;     while (l < r - 1) {       long long int m = l + (r - l) / 2;       if (check(m))         l = m;       else         r = m;     }     cout << (check(r) ? get(r) : get(l)) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int it[200041], n, io;   int down(int x) {     int s = 0, rs = x;     while (x <= n * 2) {       s = max(it[x], s);       x += x & (-x);     }     return s;   }   void up(int x, int u) {     while (x > 0) {       it[x] = max(it[x], u);       x -= x & (-x);     }   }   int main() {     int i, a[100005], o = 0, co;     cin >> n;     for (i = 0; i < n; i++) cin >> a[i];     for (i = n - 1; i >= 0; i--) {       co = down(a[i]);       up(a[i], co + 1);       o = max(co + 1, o);     }     cout << o << endl;   }


#include <bits/stdc++.h>   int count[5001] = {0}, i = 0, n, a, c = 0;   int main() {     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d , &a);       count[a - 1]++;     }     for (i = 0; i < n; i++)       if (count[i] == 0) c++;     printf( %d n , c);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   const int N = 1e5 + 7;   pair<int, int> q[N];   int n, m, fa[N << 1], a[N];   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   int main() {     n = read(), m = read();     for (int i = 1; i <= n; i++) a[i] = !read();     for (int i = 1; i <= m; i++) fa[i] = i, fa[i + m] = i + m;     for (int i = 1; i <= m; i++) {       int opt = read();       for (int j = 1; j <= opt; j++) {         int v = read();         if (!q[v].first)           q[v].first = i;         else           q[v].second = i;       }     }     for (int i = 1; i <= n; i++) {       int x1 = find(q[i].first), x2 = find(q[i].first + m);       int y1 = find(q[i].second), y2 = find(q[i].second + m);       if (a[i]) {         fa[x1] = y2;         x2 = find(x2), y1 = find(y1);         fa[x2] = y1;       } else {         fa[x1] = y1;         x2 = find(x2), y2 = find(y2);         fa[x2] = y2;       }     }     for (int i = 1; i <= m; i++) {       if (find(i) == find(i + m)) {         printf( NO );         return 0;       }     }     printf( YES );   }


#include <bits/stdc++.h>   using namespace std;   int m1[502][502], m2[502][502];   int num[502], lie[502];   vector<int> v;   const int INF = 0x3f3f3f3f;   int main(void) {     int a, b, i, j, flag = 1, sum = 0;     scanf( %d %d , &a, &b);     for (i = 0; i < a; i++) {       for (j = 0; j < b; j++) {         scanf( %d , &m1[i][j]);       }     }     for (i = 0; i < a; i++) {       for (j = 0; j < b; j++) {         scanf( %d , &m2[i][j]);       }     }     for (i = 0; i < b; i++) {       num[i] = lie[i] = 0;     }     for (i = 0; i < a; i++) {       for (j = 0; j < b; j++) {         num[j] += m1[i][j] ^ m2[i][j];         lie[i] += m1[i][j] ^ m2[i][j];       }     }     for (i = 0; i < b; i++) {       if (num[i] % 2) {         flag = 0;         break;       }     }     for (i = 0; i < a; i++) {       if (lie[i] % 2) {         flag = 0;         break;       }     }     if (flag) {       printf( Yes n );     } else {       printf( No n );     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, n, i, t, r = 0;     while (cin >> t) {       for (i = 0; i < t; i++) {         cin >> a >> b >> n;         if (n % 3 == 1)           cout << b << endl;         else if (n % 3 == 0)           cout << a << endl;         else {           r = a ^ b;           cout << r << endl;           r = 0;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500009;   const int mod = 1000000007;   int n, m, x, y;   vector<int> v[N];   vector<bool> vis;   bool is;   void pre() { vis.assign(N, 0); }   void dfs(int ver, int par) {     vis[ver] = 1;     for (auto &it : v[ver]) {       if (it == par) continue;       if (vis[it] == 1) {         is = 1;         continue;       }       dfs(it, ver);     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin.exceptions(cin.failbit);     ;     pre();     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     int ans = 0;     for (int i = 1; i <= n; i++) {       if (vis[i]) continue;       is = 0;       dfs(i, -1);       ans++;       if (is) ans--;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   double pi = 3.1415926535;   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 power(long long int a, long long int n) {     long long int res = 1;     while (n) {       if (n % 2) {         res = res * a;       }       n = n / 2;       a = a * a;     }     return res;   }   bool checkprime(long long int n) {     for (long long int i = 2; i * i <= n; i++) {       if (n % i == 0) {         return false;       }     }     return true;   }   long long int mpow(long long int a, long long int n) {     long long int res = 1;     while (n) {       if (n % 2) {         res = (res * a) % mod;       }       a = (a * a) % mod;       n = n / 2;     }     return res;   }   void solve() {     string s;     cin >> s;     map<int, int> mp;     int n = s.size();     int cnt = 0;     vector<int> v;     for (int i = 0; i < n; i++) {       cnt = 0;       while (i < n && s[i] ==  1 ) {         i++;         cnt++;       }       v.push_back(cnt);     }     int a[101] = {0}, ans = 0;     bool f = true;     sort(v.begin(), v.end(), greater<int>());     for (auto c : v) {       if (f) {         ans += c;         f = false;       } else {         f = true;       }     }     cout << ans << endl;   }   int main() {     long long int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T read() {     char c = getchar();     T x = 0, f = 1;     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     return x * f;   }   template <typename T>   inline void write(T x) {     if (x < 0) x = (~x) + 1, putchar( - );     if (x / 10) write(x / 10);     putchar(x % 10 | 48);   }   const int INF = 0x3f3f3f3f;   long long int qz[200005][30];   long long int ans[30];   int main() {     int T = read<int>();     while (T--) {       memset(ans, 0, sizeof(ans));       int n = read<int>(), m = read<int>();       for (int i = 1; i <= n; i++) {         char c;         cin >> c;         int a = c -  a ;         ans[a]++;         for (int j = 0; j < 26; j++) qz[i][j] = qz[i - 1][j];         qz[i][a] = qz[i - 1][a] + 1;       }       for (int i = 0; i < m; i++) {         int a = read<int>();         for (int j = 0; j < 26; j++) {           ans[j] += qz[a][j];         }       }       for (int i = 0; i < 26; i++) {         cout << ans[i] <<    ;       }       cout << endl;     }   }


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


#include <bits/stdc++.h>   using namespace std;   const int MN = 1000 * 100 + 10;   const int inf = (1 << 29);   string s, t, ans, tmp;   int cnt[30], curr[30];   int main() {     cin >> s >> t;     sort(s.rbegin(), s.rend());     for (int i = 0; i < s.size(); i++) cnt[s[i] -  a ]++;     if (s <= t)       cout <<  -1 n ;     else {       for (int i = t.size() - (t.size() == s.size()); i >= 0; i--) {         bool good = true;         for (int j = 0; j < 26; j++) curr[j] = cnt[j];         for (int j = 0; j < i; j++) {           curr[t[j] -  a ]--;           if (curr[t[j] -  a ] < 0) good = false;         }         if (!good) continue;         char c =  z  + 1;         if (i < t.size()) {           for (int j = 0; j < 26; j++)             if (curr[j] && j > t[i] -  a ) c = min(c, (char)(j +  a ));           if (c <=  z )             curr[c -  a ]--;           else             continue;         }         for (int j = 0; j < i; j++) ans.push_back(t[j]);         if (i < t.size()) ans.push_back(c);         for (int j = 0; j < 26; j++) {           while (curr[j]) {             ans.push_back((char)(j +  a ));             curr[j]--;           }         }         cout << ans << endl;         break;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     scanf( %d %d , &n, &m);     map<unsigned long long, vector<int>> pow;     vector<int> a(m + 1), s(m + 1), u(m + 1), b(n + 1);     for (int i = 1; i <= n; ++i) {       unsigned long long p;       scanf( %llu , &p);       pow[p].push_back(i);     }     for (int i = 1; i <= m; ++i) scanf( %llu , &s[i]);     int c = 0, us = 0;     for (int i = 0; i < 32; ++i) {       for (int j = 1; j <= m; ++j) {         if (!u[j] && pow.count(s[j]) && !pow[s[j]].empty()) {           u[j] = 1;           us += i;           c++;           int cp = pow[s[j]].back();           pow[s[j]].pop_back();           b[cp] = j;           a[j] = i;         } else           s[j] = (s[j] + 1) / 2;       }     }     printf( %d %d n , c, us);     for (int i = 1; i <= m; ++i) {       printf( %d  , a[i]);     }     printf(  n );     for (int i = 1; i <= n; ++i) {       printf( %d  , b[i]);     }     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   struct Point {     double x, y;     Point(double x = 0, double y = 0) : x(x), y(y) {}     Point operator+(Point b) { return Point(x + b.x, y + b.y); }     Point operator-(Point b) { return Point(x - b.x, y - b.y); }     double operator^(Point b) { return x * b.y - y * b.x; }     Point operator*(double b) { return Point(x * b, y * b); }     double operator*(Point b) { return x * b.x + y * b.y; }     inline double dis() { return sqrt(*this * *this); }   };   struct tcurts {     double r;     int type;     bool operator<(const tcurts &b) const { return r < b.r; }   } q[100010 * 2];   int main() {     Point A, B, C;     scanf( %lf%lf%lf%lf , &A.x, &A.y, &B.x, &B.y), C = (A + B) * 0.5;     Point normal = (B - A) * (1 / (B - A).dis());     normal = Point(-normal.y, normal.x);     int n, top = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       Point p;       double R;       scanf( %lf%lf%lf , &p.x, &p.y, &R);       int flag = ((p - A) ^ (B - A)) > 0;       double l = -1e12, r = 1e12;       for (int times = 1; times <= 80; times++) {         double mid = (l + r) / 2;         Point O = C + normal * mid;         if ((O - A).dis() + R < (p - O).dis())           (flag ? r : l) = mid;         else           (flag ? l : r) = mid;       }       q[++top] = (tcurts){l, flag ? -1 : 1}, l = -1e12, r = 1e12;       for (int times = 1; times <= 80; times++) {         double mid = (l + r) / 2;         Point O = C + normal * mid;         if ((O - A).dis() - R > (p - O).dis())           (flag ? l : r) = mid;         else           (flag ? r : l) = mid;       }       q[++top] = (tcurts){l, flag ? 1 : -1};     }     q[++top] = (tcurts){-1e12, 0}, q[++top] = (tcurts){1e12, 0},     q[++top] = (tcurts){0, 0}, sort(q + 1, q + top + 1);     double res = 1e12;     bool flag = false;     for (int i = 1, s = 0; i <= top; i++) {       if (!s) res = min(res, fabs(q[i].r)), flag = true;       s += q[i].type;       if (!s) res = min(res, fabs(q[i].r)), flag = true;     }     if (!flag)       puts( -1 );     else       printf( %.10lf n , (C + normal * res - A).dis());   }


#include <bits/stdc++.h>   using namespace std;   int const N = 400400;   int const mod = 1000000007;   void add(int &a, int b) {     if ((a += b) >= mod) a -= mod;   }   int prod(int a, int b) {     auto x = (unsigned long long)a * b;     unsigned int xh = x >> 32, xl = x, q, r;     asm( divl %4;  n t  :  =a (q),  =d (r) :  d (xh),  a (xl),  r (mod));     return r;   }   int sum[N], sq[N], down[N];   void apply(int v, int l, int r, int val) {     add(sq[v], prod(2, prod(sum[v], val)));     add(sq[v], prod(val, prod(val, r - l)));     add(sum[v], prod(val, r - l));     add(down[v], val);   }   void push(int v, int l, int r) {     int m = (l + r) / 2;     apply(2 * v + 1, l, m, down[v]);     apply(2 * v + 2, m, r, down[v]);     down[v] = 0;   }   void add(int v, int l, int r, int from, int to, int val) {     if (r <= from || to <= l) return;     if (from <= l && r <= to) {       apply(v, l, r, val);       return;     }     push(v, l, r);     int m = (l + r) / 2;     add(2 * v + 1, l, m, from, to, val);     add(2 * v + 2, m, r, from, to, val);     sum[v] = sum[2 * v + 1] + sum[2 * v + 2];     add(sum[v], 0);     sq[v] = sq[2 * v + 1] + sq[2 * v + 2];     add(sq[v], 0);   }   int get_sq(int v, int l, int r, int from, int to) {     if (r <= from || to <= l) return 0;     if (from <= l && r <= to) return sq[v];     push(v, l, r);     int m = (l + r) / 2;     int ans =         get_sq(2 * v + 1, l, m, from, to) + get_sq(2 * v + 2, m, r, from, to);     add(ans, 0);     return ans;   }   int n;   vector<pair<int, int>> g[N];   int tin[N], tout[N];   void dfs(int v, int par) {     static int timer = 0;     tin[v] = timer++;     for (auto e : g[v]) {       if (e.first != par) {         dfs(e.first, v);         add(0, 0, n, tin[e.first], tout[e.first], e.second);       }     }     tout[v] = timer;   }   int ans[N];   vector<pair<int, int>> qs[N];   void calc(int v, int par) {     for (auto q : qs[v]) {       int cur = get_sq(0, 0, n, tin[q.first], tout[q.first]);       int all = get_sq(0, 0, n, 0, n);       ans[q.second] = prod(2, cur);       add(ans[q.second], mod - all);     }     for (auto e : g[v]) {       if (e.first != par) {         add(0, 0, n, 0, n, e.second);         add(0, 0, n, tin[e.first], tout[e.first], mod - prod(2, e.second));         calc(e.first, v);         add(0, 0, n, 0, n, mod - e.second);         add(0, 0, n, tin[e.first], tout[e.first], prod(2, e.second));       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; ++i) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       --a, --b;       g[a].emplace_back(b, c);       g[b].emplace_back(a, c);     }     dfs(0, -1);     int q;     scanf( %d , &q);     for (int i = 0; i < q; ++i) {       int u, v;       scanf( %d%d , &u, &v);       --u, --v;       qs[u].emplace_back(v, i);     }     calc(0, -1);     for (int i = 0; i < q; ++i) cout << ans[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int x[2010], y[2010];   long long nc2(long long n) { return (n * n - n) / 2LL; }   int main() {     int T, n, caseNo = 0;     map<pair<int, int>, long long> cnt;     scanf( %d , &n);     for (int i = 0; i <= n - 1; i++) {       scanf( %d %d , &x[i], &y[i]);       x[i] *= 2;       y[i] *= 2;     }     for (int i = 0; i <= n - 1; i++) {       for (int j = i + 1; j <= n - 1; j++) {         int midX = x[i] + x[j];         int midY = y[i] + y[j];         cnt[{midX, midY}]++;       }     }     long long ans = 0LL;     map<pair<int, int>, long long>::iterator it;     for (it = cnt.begin(); it != cnt.end(); it++) {       long long Cnt = it->second;       ans += nc2(Cnt);     }     printf( %lld n , ans);     cnt.clear();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class Node {     set<int> adj;       public:     bool isLeave() const { return adj.size() == 1; }     void add(int v) { adj.insert(v); }     void remove(int v) { adj.erase(v); }     int front() const { return *adj.begin(); }   };   const int N = 100001;   Node neighbors[N];   bool marked[N] = {};   int level[N] = {};   int pred[N] = {};   list<int>::iterator sortedPos[N];   int main() {     int n;     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       neighbors[u].add(v);       neighbors[v].add(u);     }     cout << 1;     if (n == 1) {       cout << endl;     } else if (n == 2) {       cout <<     << 2 << endl;     } else {       list<int> leaves, sorted;       for (int i = 1; i <= n; i++) {         if (neighbors[i].isLeave()) leaves.push_back(i);         sortedPos[i] = sorted.end();       }       {         while (!leaves.empty()) {           const int u = leaves.front();           leaves.pop_front();           if (neighbors[u].isLeave()) {             const int v = neighbors[u].front();             if (level[u] + 1 > level[v]) {               level[v] = level[u] + 1;               pred[v] = u;               if (sortedPos[v] != sorted.end()) sorted.erase(sortedPos[v]);               assert(sorted.empty() || level[v] >= level[sorted.front()]);               sorted.push_front(v);               sortedPos[v] = sorted.begin();             }             neighbors[u].remove(v);             neighbors[v].remove(u);             if (neighbors[v].isLeave()) leaves.push_back(v);           }         }         int k = 2;         int controlled = 0;         int v = sorted.front();         sorted.pop_front();         controlled += level[v] + 1;         assert(!marked[v]);         marked[v] = true;         v = pred[v];         while (v) {           assert(!marked[v]);           marked[v] = true;           v = pred[v];         }         while (!sorted.empty() && controlled < n - 1) {           v = sorted.front();           sorted.pop_front();           if (!marked[v]) {             controlled += level[v] + 1;             marked[v] = true;             v = pred[v];             while (v) {               assert(!marked[v]);               marked[v] = true;               v = pred[v];             }             cout <<     << controlled;             k++;           }         }         for (; k <= n; k++) {           if (controlled < n) controlled++;           cout <<     << controlled;         }         cout << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 111;   const int LIMIT = 10000;   const double eps = 1e-6;   int a[N], b[N];   int n, m;   int isSq(long long t) {     long long tt = (long long)sqrt(t + eps);     return tt * tt == t;   }   void gen(int n, int a[]) {     bool flag = false;     while (!flag) {       long long total = 0;       for (int i = 1; i < n; i++) {         a[i] = rand() % LIMIT + 1;         total += 1LL * a[i] * a[i];       }       for (int i = 1; i <= LIMIT; i++) {         long long t = total + i * i;         a[n] = i;         if (isSq(t)) {           flag = true;           break;         }       }     }   }   int main() {     scanf( %d %d , &n, &m);     gen(n, a);     gen(m, b);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         printf( %d%c , a[i] * b[j], j == m ?   n  :    );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool cycle[3001];   int vis[3001];   vector<vector<int> > adj;   bool f;   int node;   bool getC(int ind, int prev) {     if (cycle[ind]) {       node = ind;       return 1;     }     cycle[ind] = 1;     for (int i = 0; i < (int)adj[ind].size(); i++)       if (prev != adj[ind][i] && getC(adj[ind][i], ind)) {         if (!f)           cycle[ind] = 0;         else           vis[ind] = 0;         if (ind == node) f = 0;         return 1;       }     cycle[ind] = 0;     return 0;   }   void dfs(int ind, int l) {     vis[ind] = l;     for (int i = 0; i < (int)adj[ind].size(); i++) {       if (vis[adj[ind][i]] == -1) dfs(adj[ind][i], l + 1);     }   }   int main() {     memset(vis, -1, sizeof vis);     int n;     f = 1;     scanf( %d , &n);     adj = vector<vector<int> >(n + 1);     int a, b;     for (int i = 0; i < (int)n; i++) {       scanf( %d%d , &a, &b);       adj[a].push_back(b);       adj[b].push_back(a);     }     getC(1, -1);     for (int i = 1; i <= n; i++)       if (cycle[i]) {         dfs(i, 0);       }     for (int i = 1; i <= n; i++) cout << vis[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long perf[100];   struct BalancedBinaryForest {     int n;     vector<bool> isblack;     vector<int> blackheight;     vector<int> lft, rgt, par;     void init(int _n) {       n = _n;       isblack = vector<bool>(n, true);       blackheight = vector<int>(n, 1);       lft = rgt = par = vector<int>(n, -1);     }     pair<int, int> split(int a) {       ++perf[0];       int l = lft[a], r = rgt[a];       lft[a] = rgt[a] = -1;       if (l != -1) par[l] = -1;       if (r != -1) par[r] = -1;       if (l != -1 && !isblack[l]) isblack[l] = true, ++blackheight[l];       if (r != -1 && !isblack[r]) isblack[r] = true, ++blackheight[r];       while (par[a] != -1) {         ++perf[1];         int p = par[a], pl = lft[p], pr = rgt[p];         lft[p] = rgt[p] = -1;         par[a] = par[p];         par[p] = -1;         if (par[a] != -1) (lft[par[a]] == p ? lft[par[a]] : rgt[par[a]]) = a;         if (pl != a && pl != -1) par[pl] = -1;         if (pr != a && pr != -1) par[pr] = -1;         if (pl != a && pl != -1 && !isblack[pl])           isblack[pl] = true, ++blackheight[pl];         if (pr != a && pr != -1 && !isblack[pr])           isblack[pr] = true, ++blackheight[pr];         if (a == pl) r = join3(r, p, pr);         if (a == pr) l = join3(pl, p, l);       }       isblack[a] = true;       blackheight[a] = 1;       return make_pair(l, r);     }     int rotatelft(int a) {       ++perf[2];       assert(par[a] == -1);       int c = rgt[a], d = lft[c];       rgt[a] = d;       if (d != -1) par[d] = a;       lft[c] = a;       par[a] = c;       par[c] = -1;       return c;     }     int rotatergt(int c) {       ++perf[3];       assert(par[c] == -1);       int a = lft[c], d = rgt[a];       lft[c] = d;       if (d != -1) par[d] = c;       rgt[a] = c;       par[c] = a;       par[a] = -1;       return a;     }     int height(int x) { return x == -1 ? 0 : blackheight[x]; }     int joinlft(int a, int x, int b) {       ++perf[4];       if (height(a) == height(b) && (a == -1 || isblack[a])) {         isblack[x] = false;         blackheight[x] = height(a);         lft[x] = a;         rgt[x] = b;         if (a != -1) par[a] = x;         if (b != -1) par[b] = x;         return x;       }       int r = rgt[a];       if (r != -1) par[r] = -1;       rgt[a] = joinlft(r, x, b);       par[rgt[a]] = a;       if (!isblack[a] && !isblack[rgt[a]]) {         isblack[a] = true;         ++blackheight[a];         return a;       }       if (isblack[a] && isblack[rgt[a]] &&           blackheight[a] == blackheight[rgt[a]]) {         assert(!isblack[rgt[rgt[a]]]);         a = rotatelft(a);         isblack[a] = false;         isblack[rgt[a]] = true;         ++blackheight[rgt[a]];         return a;       }       return a;     }     int joinrgt(int a, int x, int b) {       ++perf[5];       if (height(a) == height(b) && (b == -1 || isblack[b])) {         isblack[x] = false;         blackheight[x] = height(a);         lft[x] = a;         rgt[x] = b;         if (a != -1) par[a] = x;         if (b != -1) par[b] = x;         return x;       }       int l = lft[b];       if (l != -1) par[l] = -1;       lft[b] = joinrgt(a, x, l);       par[lft[b]] = b;       if (!isblack[b] && !isblack[lft[b]]) {         isblack[b] = true;         ++blackheight[b];         return b;       }       if (isblack[b] && isblack[lft[b]] &&           blackheight[b] == blackheight[lft[b]]) {         assert(!isblack[lft[lft[b]]]);         b = rotatergt(b);         isblack[b] = false;         isblack[lft[b]] = true;         ++blackheight[lft[b]];         return b;       }       return b;     }     int join3(int a, int x, int b) {       ++perf[6];       int ret = height(a) >= height(b) ? joinlft(a, x, b) : joinrgt(a, x, b);       if (!isblack[ret]) isblack[ret] = true, ++blackheight[ret];       return ret;     }     int join2(int a, int b) {       assert(a == -1 && b == -1 || a != b);       ++perf[7];       if (a == -1)         return b;       else if (b == -1)         return a;       int c = lft[b], d = rgt[b];       lft[b] = rgt[b] = -1;       if (c != -1) par[c] = -1;       if (d != -1) par[d] = -1;       if (c != -1 && !isblack[c]) isblack[c] = true, ++blackheight[c];       if (d != -1 && !isblack[d]) isblack[d] = true, ++blackheight[d];       return join3(join2(a, c), b, d);     }     int merge(int a, int b) {       ++perf[8];       int ret = join2(getroot(a), getroot(b));       perf[11] = max(perf[11], (long long)height(ret));       return ret;     }     int getroot(int a) {       ++perf[9];       while (a != -1 && par[a] != -1) ++perf[10], a = par[a];       return a;     }     void myassert(bool cond, const char *s, int who) {       if (cond) return;       printf( fail: %s for %d n , s, who + 1);       assert(cond);     }     void verify() {       for (int i = (0); i < (n); ++i)         if (par[i] != -1)           myassert(lft[par[i]] == i || rgt[par[i]] == i,                     parent with incorrect child , i);       for (int i = (0); i < (n); ++i)         if (lft[i] != -1) myassert(par[lft[i]] == i,  lft incorrect parent , i);       for (int i = (0); i < (n); ++i)         if (rgt[i] != -1) myassert(par[rgt[i]] == i,  rgt incorrect parent , i);       for (int i = (0); i < (n); ++i)         if (par[i] != -1 && !isblack[i])           myassert(isblack[par[i]],  red with red parent , i);       for (int i = (0); i < (n); ++i)         myassert(height(lft[i]) == height(rgt[i]),  diff heights , i);       for (int i = (0); i < (n); ++i)         myassert(height(i) == height(lft[i]) + (isblack[i] ? 1 : 0),                   incorrect height , i);     }   };   struct ForestDynamicConnectivity {     int n;     BalancedBinaryForest bbf;     queue<int> pool;     void init(int _n) {       n = _n;       bbf.init(n + 2 * (n - 1));       for (int i = (n); i < (bbf.n); ++i) pool.push(i);     }     int getnode() {       assert(!pool.empty());       int ret = pool.front();       pool.pop();       return ret;     }     void releasenode(int x) { pool.push(x); }     void reroot(int a) {       int b, c;       tie(b, c) = bbf.split(a);       bbf.merge(a, bbf.merge(c, b));     }     pair<int, int> addedge(int a, int b) {       reroot(a);       reroot(b);       int ab = getnode(), ba = getnode();       bbf.merge(bbf.merge(a, ab), bbf.merge(b, ba));       return make_pair(ab, ba);     }     void remedge(pair<int, int> e) {       int a, b, c;       tie(a, b) = bbf.split(e.first);       if (bbf.getroot(a) == bbf.getroot(e.second)) {         c = b;         tie(a, b) = bbf.split(e.second);       } else {         tie(b, c) = bbf.split(e.second);       }       releasenode(e.first), releasenode(e.second);       bbf.merge(a, c);     }     int getrepresentative(int a) { return bbf.getroot(a); }   };   struct SegTreeMinCounter {     int n;     vector<int> smn, smncnt, slazy;     void sapply(int x, int by) {       smn[x] += by;       slazy[x] += by;     }     void spush(int x) {       if (slazy[x] != 0)         sapply(2 * x + 1, slazy[x]), sapply(2 * x + 2, slazy[x]), slazy[x] = 0;     }     void spull(int x) {       smn[x] = min(smn[2 * x + 1], smn[2 * x + 2]);       smncnt[x] = (smn[2 * x + 1] == smn[x] ? smncnt[2 * x + 1] : 0) +                   (smn[2 * x + 2] == smn[x] ? smncnt[2 * x + 2] : 0);     }     void sinit(int x, int l, int r) {       slazy[x] = 0;       if (l == r) {         smn[x] = 0, smncnt[x] = 1;       } else {         int m = l + (r - l) / 2;         sinit(2 * x + 1, l, m);         sinit(2 * x + 2, m + 1, r);         spull(x);       }     }     void smod(int x, int l, int r, int L, int R, int BY) {       if (L <= l && r <= R) {         sapply(x, BY);       } else {         int m = l + (r - l) / 2;         spush(x);         if (L <= m) smod(2 * x + 1, l, m, L, R, BY);         if (m + 1 <= R) smod(2 * x + 2, m + 1, r, L, R, BY);         spull(x);       }     }     int sget(int x, int l, int r, int L, int R, int VAL) {       if (L <= l && r <= R) {         assert(smn[x] >= VAL);         return smn[x] == VAL ? smncnt[x] : 0;       } else {         int m = l + (r - l) / 2;         spush(x);         int ret = 0;         if (L <= m) ret += sget(2 * x + 1, l, m, L, R, VAL);         if (m + 1 <= R) ret += sget(2 * x + 2, m + 1, r, L, R, VAL);         return ret;       }     }     void init(int _n) {       n = _n;       smn = smncnt = slazy = vector<int>(4 * n);       sinit(0, 0, n - 1);     }     void mod(int l, int r, int by) { smod(0, 0, n - 1, l, r, by); }     int get(int l, int r, int val) { return sget(0, 0, n - 1, l, r, val); }   };   const int MAXH = 1000;   const int MAXW = 1000;   const int MAXN = 200000;   const int DX[] = {-1, 0, +1, 0}, DY[] = {0, +1, 0, -1};   int h, w, n;   int g[MAXH][MAXW];   pair<int, int> pos[MAXN];   ForestDynamicConnectivity conn;   vector<pair<int, int>> connedges[MAXN];   SegTreeMinCounter segtree;   long long solve() {     for (int x = (0); x < (h); ++x)       for (int y = (0); y < (w); ++y) pos[g[x][y]] = make_pair(x, y);     conn.init(n);     segtree.init(n);     long long ret = 0;     int l = 0;     for (int r = (0); r < (n); ++r) {       int rx = pos[r].first, ry = pos[r].second;       while (true) {         vector<int> adj;         for (int k = (0); k < (4); ++k) {           int nx = rx + DX[k], ny = ry + DY[k];           if (nx < 0 || nx >= h || ny < 0 || ny >= w || g[nx][ny] < l ||               g[nx][ny] > r)             continue;           adj.push_back(conn.getrepresentative(g[nx][ny]));         }         sort(adj.begin(), adj.end());         bool duplicate = false;         for (int i = (1); i < (((int)(adj).size())); ++i)           if (adj[i - 1] == adj[i]) duplicate = true;         if (!duplicate) break;         for (int i = (0); i < (((int)(connedges[l]).size())); ++i)           conn.remedge(connedges[l][i]);         ++l;       }       segtree.mod(0, r, +1);       for (int k = (0); k < (4); ++k) {         int nx = rx + DX[k], ny = ry + DY[k];         if (nx < 0 || nx >= h || ny < 0 || ny >= w || g[nx][ny] < l ||             g[nx][ny] > r)           continue;         connedges[g[nx][ny]].push_back(conn.addedge(g[nx][ny], r));         segtree.mod(0, g[nx][ny], -1);       }       int cur = segtree.get(l, r, 1);       ret += cur;     }     return ret;   }   void run() {     scanf( %d%d , &h, &w);     n = h * w;     for (int x = (0); x < (h); ++x)       for (int y = (0); y < (w); ++y) scanf( %d , &g[x][y]), --g[x][y];     printf( %lld n , solve());   }   void stress() {     std::mt19937 rnd(1237891);     for (int rep = (0); rep < (1); ++rep) {       h = 992, w = 191, n = h * w;       memset(g, -1, sizeof(g));       int at = 0;       for (int xx = (0); xx < (h); ++xx) {         int x = (2 * xx) % h + (2 * xx >= h ? 1 : 0);         for (int y = (0); y < (w); ++y) g[x][y] = at++;       }       for (int x = (0); x < (h); ++x)         for (int y = (0); y < (w); ++y) assert(g[x][y] != -1);       long long have = solve();       printf( %lld n , have);     }     for (int i = (0); i <= (11); ++i) printf( perf[%d]=%lld n , i, perf[i]);   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long a, b;     cin >> a >> b;     if (b < a || (b - a) % 2)       cout << -1 << endl;     else if (b == a && a == 0)       cout << 0 << endl;     else if (b == a)       cout << 1 << endl << a;     else {       long long x = 0, cnt = 1, pp = b - a;       while (cnt <= pp) cnt *= 2;       cnt /= 2;       while (true) {         if (cnt * 2 <= pp) {           x += cnt;           pp -= cnt * 2;         }         cnt /= 2;         if (pp == 0) break;       }       long long tt = a ^ x;       if (tt == a + x)         cout << 2 << endl << a + x <<     << x << endl;       else         cout << 3 << endl << a <<     << x <<     << x << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double PI = acos(-1.0);   const double eps = 1e-6;   const int MAX = 1e5 + 10;   const long long mod = 1e9 + 7;   struct node {     long long l, r, mid, sum, tag;     node() {}     node(long long a, long long b) {       l = a;       r = b;       sum = 0;       tag = 0;       mid = (l + r) >> 1;     }   } tree[4 * MAX][15];   long long c[MAX][15];   void change(long long id, long long son) {     long long i, j, s[12], tag[12];     memset((s), (0), sizeof(s));     for (i = 0; i <= 9; i++) {       tag[i] = tree[son][i].tag;       s[i] = tree[son][i].sum;     }     for (i = 0; i <= 9; i++) {       if (tree[id][i].tag != i) {         for (j = 0; j <= 9; j++) {           if (tree[son][j].tag == i) tag[j] = tree[id][i].tag;         }         s[tree[id][i].tag] += tree[son][i].sum;         s[i] -= tree[son][i].sum;       }     }     for (i = 0; i <= 9; i++) {       tree[son][i].tag = tag[i];       tree[son][i].sum = s[i];     }   }   void pushdown(long long id) {     long long i;     change(id, id << 1);     change(id, id << 1 | 1);     for (i = 0; i <= 9; i++) {       tree[id][i].tag = i;     }   }   void pushup(long long id) {     long long i;     for (i = 0; i <= 9; i++) {       tree[id][i].sum = tree[id << 1][i].sum + tree[id << 1 | 1][i].sum;     }   }   void build(long long l, long long r, long long id) {     long long mid, i;     for (i = 0; i <= 9; i++) {       tree[id][i] = node(l, r);       tree[id][i].tag = i;       if (l == r) tree[id][i].sum = c[l][i];     }     if (l == r) return;     mid = (l + r) >> 1;     build(l, mid, id << 1);     build(mid + 1, r, id << 1 | 1);     pushup(id);   }   void update(long long l, long long r, long long id, long long x, long long y) {     long long mid, i, s = 0;     if (l <= tree[id][x].l && r >= tree[id][x].r) {       for (i = 0; i <= 9; i++) {         if (tree[id][i].tag == x) {           tree[id][i].tag = y;         }       }       tree[id][y].sum += tree[id][x].sum;       tree[id][x].sum = 0;       return;     }     pushdown(id);     mid = tree[id][x].mid;     if (r <= mid)       update(l, r, id << 1, x, y);     else if (l >= mid + 1)       update(l, r, id << 1 | 1, x, y);     else {       update(l, mid, id << 1, x, y);       update(mid + 1, r, id << 1 | 1, x, y);     }     pushup(id);   }   long long query(long long l, long long r, long long id, long long x) {     long long mid, s = 0, i;     if (l == tree[id][x].l && r == tree[id][x].r) {       for (i = 0; i <= 9; i++) {         s += tree[id][i].sum * i;       }       return s;     }     pushdown(id);     mid = tree[id][x].mid;     if (r <= mid)       return query(l, r, id << 1, x);     else if (l >= mid + 1)       return query(l, r, id << 1 | 1, x);     else       return query(l, mid, id << 1, x) + query(mid + 1, r, id << 1 | 1, x);     pushup(id);   }   int main() {     long long i, n, q, x, op, l, r, a, b, temp;     while (~scanf( %lld%lld , &n, &q)) {       memset((c), (0), sizeof(c));       for (i = 1; i <= n; i++) {         scanf( %lld , &x);         temp = 1;         while (x) {           long long now = x % 10;           c[i][now] += temp;           x /= 10;           temp *= 10;         }       }       build(1, n, 1);       while (q--) {         scanf( %lld%lld%lld , &op, &l, &r);         if (op == 1) {           scanf( %lld%lld , &a, &b);           if (a != b) update(l, r, 1, a, b);         } else {           long long ans = 0;           ans = query(l, r, 1, 1);           printf( %lld n , ans);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = (int)1e9 + 7;   const int MOD2 = 1007681537;   const int INF = (int)1e9;   const long long LINF = (long long)1e18;   const long double PI = acos((long double)-1);   const long double EPS = 1e-9;   inline long long gcd(long long a, long long b) {     long long r;     while (b) {       r = a % b;       a = b;       b = r;     }     return a;   }   inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   inline long long fpow(long long n, long long k, int p = MOD) {     long long r = 1;     for (; k; k >>= 1) {       if (k & 1) r = r * n % p;       n = n * n % p;     }     return r;   }   template <class T>   inline int chkmin(T& a, const T& val) {     return val < a ? a = val, 1 : 0;   }   template <class T>   inline int chkmax(T& a, const T& val) {     return a < val ? a = val, 1 : 0;   }   inline long long isqrt(long long k) {     long long r = sqrt(k) + 1;     while (r * r > k) r--;     return r;   }   inline long long icbrt(long long k) {     long long r = cbrt(k) + 1;     while (r * r * r > k) r--;     return r;   }   inline void addmod(int& a, int val, int p = MOD) {     if ((a = (a + val)) >= p) a -= p;   }   inline void submod(int& a, int val, int p = MOD) {     if ((a = (a - val)) < 0) a += p;   }   inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }   inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }   inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }   inline int sign(long double x, long double y) { return sign(x - y); }   long long ff(long long n, long long k) {     return min(k + k, k + (n - k) * (n - k - 1) / 2);   }   void solve() {     int test;     cin >> test;     while (test--) {       long long n;       cin >> n;       if (n <= 4) {         cout << n - 1 <<   n ;         continue;       }       long long lo = 1, hi = n - 3;       while (lo < hi) {         long long mi = lo + hi >> 1;         if (ff(n, mi) > ff(n, mi + 1)) {           hi = mi;         } else {           lo = mi + 1;         }       }       cout << max(n - 1, ff(n, lo + hi >> 1)) <<   n ;     }   }   int main() {     int JUDGE_ONLINE = 1;     if (fopen( in.txt ,  r )) {       JUDGE_ONLINE = 0;       assert(freopen( in.txt ,  r , stdin));     } else {       ios_base::sync_with_stdio(0), cin.tie(0);     }     solve();     if (!JUDGE_ONLINE) {     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t[3000000];   long long t1[3000000];   long long dp[10 * 10 + 5][10 + 5];   vector<long long> v;   long long n, m, k, l, r, nb, q, p;   map<long long, int> mapp;   void debug(long long* tab, int len) {     cout <<  debug ::::  n ;     for (int i = 0; i < len; i++) cout << tab[i] <<    ;     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> nb;     while (nb--) {       cin >> n;       for (int i = 0; i < 4 * n; i++) cin >> t[i];       sort(t, t + 4 * n);       bool test = true;       for (int i = 0; i < 2 * n; i++) {         if (t[2 * i + 1] != t[2 * i]) test = false;       }       for (int i = 0; i < 2 * n; i++) t1[i] = t[2 * i];       long long k = t1[0] * t1[2 * n - 1];       for (int i = 0; i < n; i++) {         if (t1[i] * t1[2 * n - 1 - i] != k) test = false;       }       if (test)         cout <<  YES ;       else         cout <<  NO ;       if (nb) cout <<   n ;     }   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   template <typename T>   T min(T x, T y) {     return x < y ? x : y;   }   template <typename T>   T max(T x, T y) {     return x > y ? x : y;   };   const int INF = 2000000005;   const long long mod = 1000000007;   const long long MAXN = 1000005;   const double eps = 1e-6;   int sgn(double x) { return x < -eps ? -1 : x > eps; }   double SQ(double x) { return x * x; }   struct point {     double x;     double y;     point(double X = 0, double Y = 0) { x = X, y = Y; }     point operator-(point t) { return {x - t.x, y - t.y}; }     double operator*(point t) { return x * t.y - y * t.x; }     double operator%(point t) { return x * t.x + y * t.y; }     double norm() { return sqrt(SQ(x) + SQ(y)); }   };   struct polygon {     int n;     std::vector<point> V;     double S;     void read() {       double x, y;       V.clear();       scanf( %d , &n);       for (int i = 0; i < n; i++) {         scanf( %lf %lf , &x, &y);         V.push_back({x, y});       }       S = 0;       for (int i = 0; i < n - 1; i++) S += V[i] * V[i + 1];       S += V[n - 1] * V[0];       S = 0.5 * fabs(S);     }     bool in(point p) {       int cnt = 0;       for (int i = 0; i < n; i++) {         int j = (i + 1) % n;         point u = V[i], v = V[j];         if (sgn(p.y - u.y) == 0 || sgn(p.y - v.y) == 0)           return in({p.x, p.y + 3 * eps});         if (sgn(u.y - v.y) <= 0) std::swap(u, v);         if (sgn(p.y - u.y) > 0 || sgn(p.y - v.y) <= 0) continue;         cnt += sgn((v - p) * (u - p)) > 0;       }       return cnt & 1;     }     double dis(point a, point b, point p) {       point ab = b - a, pa = a - p, pb = b - p;       if ((ab % pb) * (ab % pa) < 0) return fabs(pa * ab) / ab.norm();       return min(pa.norm(), pb.norm());     }     double dis(double x, double y) {       double t = 1e15;       for (int i = 0; i < n; i++) {         int j = (i + 1) % n;         point u = V[i], v = V[j];         t = min(t, dis(u, v, {x, y}));       }       return t;     }     bool judge(double x, double y, double r) {       if (dis(x, y) < r + eps) return true;       return in({x, y});     }   };   double W, H;   int N, Q;   polygon P[MAXN];   int main() {     scanf( %lf %lf %d , &W, &H, &N);     for (int i = 1; i <= N; i++) P[i].read();     double x, y, r;     scanf( %d , &Q);     while (Q--) {       scanf( %lf %lf %lf , &x, &y, &r);       std::vector<int> q;       double t = 0;       for (int i = 1; i <= N; i++)         if (P[i].judge(x, y, r)) q.push_back(i - 1), t += P[i].S;       printf( %lf %d  , t, q.size());       for (int i = 0; i < q.size(); i++) printf( %d  , q[i]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using D = double;   using uint = unsigned int;   const int maxn = 1005;   int n;   int a[maxn];   int cnt[maxn];   int have[maxn];   int was[maxn];   int ct = 0;   inline bool canset(int x) {     int wh = lower_bound(a, a + n, x) - a;     if (wh >= n || a[wh] != x) return false;     if (was[wh] == ct) {       if (!have[wh]) return false;       have[wh]--;       return true;     } else {       have[wh] = cnt[wh] - 1;       was[wh] = ct;       return true;     }   }   inline int getcnt(int x) {     int wh = lower_bound(a, a + n, x) - a;     return have[wh];   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     sort(a, a + n);     int last = 0;     cnt[0] = 1;     for (int i = 1; i < n; i++) {       if (a[i] == a[i - 1])         cnt[last]++;       else {         last++;         a[last] = a[i];         cnt[last] = 1;       }     }     n = last + 1;     int answer = 2;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         ct++;         int f = a[i];         int s = a[j];         canset(f);         if (!canset(s)) continue;         int curans = 2;         while (true) {           if (!canset(f + s)) break;           curans++;           int t = f + s;           f = s;           s = t;           if (f == 0 && s == 0) {             curans += getcnt(0);             break;           }         }         answer = max(answer, curans);       }     }     cout << answer << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s[1005];   long long a[1005];   long long ans;   long long b[1005][5];   long long ma;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     for (long long i = 1; i <= n; i++) {       cin >> s[i];     }     for (long long i = 0; i < m; i++) {       cin >> a[i];     }     for (long long i = 0; i < m; i++) {       ma = 0;       for (long long j = 1; j <= n; j++) {         b[i][s[j][i] -  A ]++;       }       for (long long j = 0; j < 5; j++) {         ma = max(ma, b[i][j]);       }       ans += ma * a[i];     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     unsigned long long sum{};     int cur{1};     for (int i{}; i < m; i++) {       int x;       cin >> x;       if (cur < x) {         sum += x - cur;         cur = x;       } else if (cur > x) {         sum += n - cur + x;         cur = x;       } else         continue;     }     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long ab[11], y[11];   long long val(string abv) {     long long res = 0;     for (int i = 0; i < (int)abv.size(); i++)       res = 10 * res + ((long long)(abv[i] -  0 ));     return res;   }   int main() {     cin.sync_with_stdio(false);     long long p10 = 10;     long long nb = 1989;     for (int i = 0; i < (int)10; i++) {       ab[i] = nb % p10;       y[i] = nb;       nb += p10;       p10 *= ((long long)10);     }     cin >> n;     while (n--) {       char c;       string abv;       for (int i = 0; i < (int)4; i++) cin >> c;       cin >> abv;       long long k = val(abv);       int l = abv.size() - 1;       if (ab[l] <= k)         printf( %I64d n , y[l] + k - ab[l]);       else         printf( %I64d n , y[l + 1] - (ab[l] - k));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const int N = 100010;   int a[N];   vector<int> f[N];   void go() {     long long n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 2; i < N; i++) {       for (int j = i; j < N; j += i) f[j].push_back(i);     }     int freq[N] = {0};     for (int i = 0; i < n; i++) {       int m = 0;       for (auto x : f[a[i]]) {         m = max(m, freq[x] + 1);       }       freq[a[i]] = m;       for (auto x : f[a[i]]) {         freq[x] = m;       }     }     int ans = 1;     for (int i = 0; i < n; i++) {       ans = max(freq[a[i]], ans);     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int t = 1;     cout << fixed << setprecision(9);     while (t--) {       go();     }   }


#include <bits/stdc++.h>   using namespace std;   int l1, r1, l2, r2, ans;   int min(int a, int b) { return a < b ? a : b; }   int max(int a, int b) { return a > b ? a : b; }   int check(int a) {     if (a > ans) ans = a;     return a;   }   int solve(int l1, int r1, int l2, int r2, int k) {     if (l1 == l2 && r1 == r2) {       if (r1 - l1 + 1 > ans) ans = r1 - l1 + 1;       return r1 - l1 + 1;     }     if (r1 - l1 + 1 <= ans || r2 - l2 + 1 <= ans) return 0;     if (k == 0) return 0;     int mid = (1 << k);     if (r1 < mid && r2 < mid) return check(solve(l1, r1, l2, r2, k - 1));     if (l1 > mid && l2 > mid)       return check(solve(l1 - mid, r1 - mid, l2 - mid, r2 - mid, k - 1));     if (r1 < mid && l2 > mid)       return check(solve(l1, r1, l2 - mid, r2 - mid, k - 1));     if (r2 < mid && l1 > mid)       return check(solve(l1 - mid, r1 - mid, l2, r2, k - 1));     if (l1 <= mid && r1 >= mid) {       if (r2 < mid)         return check(max(solve(l1, mid - 1, l2, r2, k - 1),                          solve(1, r1 - mid, l2, r2, k - 1)));       if (l2 > mid)         return check(max(solve(l1, mid - 1, l2 - mid, r2 - mid, k - 1),                          solve(1, r1 - mid, l2 - mid, r2 - mid, k - 1)));       int ret = min(r1, r2) - max(l1, l2) + 1;       ret = max(ret, solve(l1, mid - 1, 1, r2 - mid, k - 1));       ret = max(ret, solve(1, r1 - mid, l2, mid - 1, k - 1));       return check(ret);     }     if (l2 <= mid && r2 >= mid) {       if (r1 < mid)         return check(max(solve(l1, r1, l2, mid - 1, k - 1),                          solve(l1, r1, 1, r2 - mid, k - 1)));       if (l1 > mid)         return check(max(solve(l1 - mid, r1 - mid, l2, mid - 1, k - 1),                          solve(l1 - mid, r1 - mid, 1, r2 - mid, k - 1)));       int ret = min(r1, r2) - max(l1, l2) + 1;       ret = max(ret, solve(l1, mid - 1, 1, r2 - mid, k - 1));       ret = max(ret, solve(1, r1 - mid, l2, mid - 1, k - 1));       return check(ret);     }   }   int main() {     cin >> l1 >> r1 >> l2 >> r2;     cout << solve(l1, r1, l2, r2, 29) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char temp[11];     int n, m, k;     cin >> n >> m >> k;     int a[10][100], b[10][100], c[10][100];     for (int i = 0; i < n; i++) {       cin >> temp;       for (int j = 0; j < m; j++) cin >> a[i][j] >> b[i][j] >> c[i][j];     }     int maxi = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) {         if (i == j) continue;         vector<pair<int, int> > diff(m);         for (int t = 0; t < m; t++) diff[t] = make_pair(b[j][t] - a[i][t], t);         sort(diff.rbegin(), diff.rend());         int kc = k, cur = 0;         for (int t = 0; kc > 0 && t < m && diff[t].first > 0; t++) {           cur += min(kc, c[i][diff[t].second]) * diff[t].first;           kc -= min(kc, c[i][diff[t].second]);         }         maxi = max(maxi, cur);       }     cout << maxi << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int maxn = 100010;   int x0[maxn], x1[maxn], d[maxn], c[maxn];   int main() {     int n, m, y0, y1;     cin >> n >> m >> y0 >> y1;     for (int i = 0; i < n; ++i) {       cin >> x0[i];     }     memset(d, -1, sizeof(d));     for (int i = 0; i < m; ++i) {       cin >> x1[i];     }     int p = 0, k = 0, last = -1;     for (int i = 0; i < n; ++i) {       while (p < m && abs(x0[i] - x1[p]) > abs(x0[i] - x1[p + 1])) ++p;       if (last < p || abs(x0[i - 1] - x1[p]) == abs(x0[i] - x1[p]))         ++k, last = p;       else if (p < m && abs(x0[i] - x1[p]) == abs(x0[i] - x1[p + 1]))         ++k, last = ++p;       else         last = p;     }     cout << n - k << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve();   void precalc();   clock_t start;   bool doing = true;   int main() {     int t = 1;     cout.sync_with_stdio(0);     cin.tie(0);     precalc();     cout.precision(10);     cout << fixed;     start = clock();     int testNum = 1;     while (t--) {       solve();     }     return 0;   }   void precalc() {}   int binpow(int q, int w, int mod) {     if (!w) return 1;     if (w & 1) return q * binpow(q, w - 1, mod) % mod;     return binpow(q * q % mod, w / 2, mod);   }   int mod = 1000000007;   int gcd(int q, int w) {     while (w) {       q %= w;       swap(q, w);     }     return q;   }   int n, m;   string s[1005];   void solve() {     cin >> n >> m;     for (int i = 0; i < n; ++i) {       cin >> s[i];     }     vector<char> good(n - 1, false);     int ans = 0;     for (int i = 0; i < m; ++i) {       bool need = false;       for (int j = 0; j + 1 < n; ++j) {         if (good[j]) {           continue;         }         if (s[j][i] > s[j + 1][i]) {           need = true;         }       }       if (need) {         ++ans;       } else {         for (int j = 0; j + 1 < n; ++j) {           if (s[j][i] < s[j + 1][i]) {             good[j] = true;           }         }       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     double x = 0, ans = 0, p[100005] = {0};     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> p[i];     for (int i = 1; i <= n; i++) {       x = x * p[i] + p[i];       ans += 2 * x;       ans -= p[i];     }     cout << fixed << setprecision(10);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string char_to_str(char c) {     string tem(1, c);     return tem;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long n, k;     cin >> n >> k;     string s;     cin >> s;     string ans = s;     for (long long i = 0; i < n; ++i) {       long long d1 =  z  - s[i];       long long d2 = s[i] -  a ;       if (k >= d1 && k >= d2) {         if (d1 > d2) {           ans[i] =  z ;           k -= d1;         } else {           ans[i] =  a ;           k -= d2;         }       } else {         if (s[i] + k <=  z ) {           ans[i] = s[i] + k;           k = 0;         } else {           ans[i] = s[i] - k;           k = 0;         }       }     }     if (k != 0) {       cout <<  -1 ;     } else {       cout << ans;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5;   long long n, u, v, w, res = 0, par[N];   vector<pair<long long, long long> > a[N];   long long root(long long x) {     if (par[x] < 0) return x;     return (par[x] = root(par[x]));   }   void Merge(long long u, long long v) {     long long a = root(u), b = root(v);     if (a == b) return;     if (par[a] > par[b]) swap(a, b);     par[a] += par[b], par[b] = a;   }   bool check(long long n) {     while (n > 0) {       long long a = n % 10;       if (a != 4 && a != 7) return false;       n /= 10;     }     return true;   }   void in() {     cin >> n;     for (long long i = 1; i <= n; i++) par[i] = -1;     for (long long i = 1; i <= n - 1; i++) {       cin >> u >> v >> w;       if (!check(w)) Merge(u, v);       a[u].push_back({v, w}), a[v].push_back({u, w});     }   }   void process() {     for (long long i = 1; i <= n; i++) {       if (par[i] < 0) {         long long tmp = -par[i];         res += (tmp * (n - tmp - 1) * (n - tmp));       }     }     cout << res;   }   int main() {     if (fopen( trau.inp ,  r ))       freopen( trau.inp ,  r , stdin), freopen( trau.out ,  w , stdout);     in();     process();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       long long int i, j, s = 0, p = 0, u = 0;       for (i = 1; i <= n / 2; i++) {         s = s + i * i;       }       cout << s * 8 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   constexpr int inf32 = 0x3f3f3f3f;   constexpr long long inf64 = 0x3f3f3f3f3f3f3f3f;   int main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     int n, m;     cin >> n >> m;     vector<long long> a(n);     for (int u = 0; u < (n); ++u) cin >> a[u];     vector<vector<int> > adj(n);     for (int i = 0; i < (m); ++i) {       int u, v;       cin >> u >> v, --u, --v;       adj[u].push_back(v);     }     int k = 1;     vector<int> g(n, 0), vis(n, 0);     function<void(int)> dfs = [&](int u) {       if (vis[u]) return;       vis[u] = true;       for (const int &v : adj[u]) dfs(v);       vector<int> use(k, 0);       for (const int &v : adj[u]) use[g[v]] = true;       while (g[u] < k && use[g[u]]) ++g[u];       if (g[u] >= k) ++k;     };     for (int u = 0; u < (n); ++u) dfs(u);     vector<long long> f(k, 0);     for (int u = 0; u < (n); ++u) f[g[u]] ^= a[u];     int mx = -1;     for (int i = 0; i < (k); ++i)       if (f[i]) mx = i;     if (!~mx) return !(cout <<  LOSE n );     cout <<  WIN n ;     for (int u = 0; u < (n); ++u)       if (g[u] == mx && (a[u] ^ f[mx]) < a[u]) {         a[u] ^= f[mx], f[mx] = 0;         for (const int &v : adj[u]) a[v] ^= f[g[v]], f[g[v]] = 0;         break;       }     for (int u = 0; u < (n); ++u) cout << a[u] <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[100005], b;   int n, k;   pair<long long, int> pvec[100005];   int solve() {     for (int i = 0; i < n - 1; i++) pvec[i] = make_pair(a[i], i + 1);     sort(pvec, pvec + n);     reverse(pvec, pvec + n);     long long total = 0;     int ans = n;     for (int i = 0; i < k; i++) {       total += pvec[i].first;       ans = min(ans, pvec[i].second);     }     if (total > b) {       if (k > 1) b = b - (total - pvec[k - 1].first);       for (int j = 0; j < n; j++) {         if (a[j] > b) return j + 1;       }     } else       return n;     return 0;   }   int main() {     cin >> n >> k;     cin >> b;     for (int i = 0; i < n; i++) cin >> a[i];     cout << solve() << endl;     return 0;   }


#include <bits/stdc++.h>   const int B = 500;   using namespace std;   const long long inf = 1e9 + 12;   long long mod = 1e9 + 7;   const long long mxn = 4e5 + 5;   const int N = 10000005;   const double pi = 3.14159;   int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};   int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};   long long m, n, k, y, z;   vector<int> g[mxn];   int timer = 0;   int st[mxn], ed[mxn], d[mxn], tree[mxn << 4], lazy[mxn << 4];   bool lit[mxn];   void dfs(int v, int p) {     timer++;     st[v] = timer;     for (auto u : g[v]) {       if (u == p) continue;       dfs(u, v);     }     timer++;     ed[v] = timer;   }   void push_up(int node, int l, int r) {     int mid = (l + r) >> 1;     tree[node] = (lazy[2 * node] % 2 == 0 ? (tree[2 * node])                                           : (mid - l + 1 - tree[2 * node])) +                  (lazy[2 * node + 1] % 2 == 0 ? (tree[2 * node + 1])                                               : (r - mid - tree[2 * node + 1]));   }   void upd(int node, int l, int r, int ql, int qr, int t) {     if (ql > r || qr < l) return;     if (l >= ql && r <= qr) {       if (t)         lazy[node]++;       else         tree[node] = r - l + 1;       return;     }     int mid = (l + r) >> 1;     upd(2 * node, l, mid, ql, qr, t), upd(2 * node + 1, mid + 1, r, ql, qr, t);     push_up(node, l, r);   }   int qry(int node, int l, int r, int ql, int qr, int val) {     if (ql > r || qr < l) return 0;     if (l >= ql && r <= qr) {       val += lazy[node];       if (val % 2 == 0)         return tree[node];       else         return r - l + 1 - tree[node];     }     int mid = (l + r) >> 1;     return qry(2 * node, l, mid, ql, qr, val + lazy[node]) +            qry(2 * node + 1, mid + 1, r, ql, qr, val + lazy[node]);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 2; i <= n; i++) {       int x;       cin >> x;       g[x].push_back(i), g[i].push_back(x);     }     dfs(1, 0);     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       lit[i] = x;       if (x) upd(1, 1, 2 * n, st[i], st[i], 0), upd(1, 1, 2 * n, ed[i], ed[i], 0);     }     cin >> k;     int t;     while (k--) {       string s;       cin >> s >> t;       if (s[0] ==  g ) {         cout << qry(1, 1, 2 * n, st[t], ed[t], 0) / 2 << endl;       } else {         upd(1, 1, 2 * n, st[t], ed[t], 1);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char block[][3][4] = {{ ..# ,  ### ,  ..# },                         { .#. ,  .#. ,  ### },                         { #.. ,  ### ,  #.. },                         { ### ,  .#. ,  .#. }};   int cell[10][10], anscell[10][10];   int bcnt, ans;   bool isPlacable(int i, int j, int k) {     for (int p = 0; p < 3; ++p)       for (int q = 0; q < 3; ++q) {         if (cell[i + p][j + q] != -1 && block[k][p][q] ==  # ) return false;       }     return true;   }   void setBlock(int i, int j, int k) {     for (int p = 0; p < 3; ++p)       for (int q = 0; q < 3; ++q) {         if (block[k][p][q] ==  # ) cell[i + p][j + q] = bcnt;       }     ++bcnt;   }   void removeBlock(int i, int j, int k) {     for (int p = 0; p < 3; ++p)       for (int q = 0; q < 3; ++q) {         if (block[k][p][q] ==  # ) cell[i + p][j + q] = -1;       }     --bcnt;   }   void go(int i, int j) {     if (j >= m - 2) {       go(i + 1, 0);       return;     }     if (i == n - 2) {       if (ans < bcnt) {         ans = bcnt;         memcpy(anscell, cell, sizeof(cell));       }       return;     }     if (bcnt + (int)((n * m - (i * m + j)) / 6.2) <= ans) {       return;     }     for (int k = 0; k < 4; ++k) {       if (isPlacable(i, j, k)) {         setBlock(i, j, k);         go(i, j + 1);         removeBlock(i, j, k);       }     }     go(i, j + 1);   }   int main() {     memset(cell, -1, sizeof(cell));     memset(anscell, -1, sizeof(anscell));     scanf( %d %d , &n, &m);     if (n >= 3 && m >= 3) go(0, 0);     printf( %d n , ans);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         putchar(anscell[i][j] == -1 ?  .  : ( A  + anscell[i][j]));       }       putchar(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using uint = unsigned int;   using ll = long long;   using ull = unsigned long long;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   template <typename T1, typename T2>   ostream &operator<<(ostream &out, const pair<T1, T2> &item) {     out <<  (  << item.first <<  ,   << item.second <<  ) ;     return out;   }   template <typename T>   ostream &operator<<(ostream &out, const vector<T> &v) {     for (const auto &item : v) out << item <<    ;     return out;   }   const int NMAX = 200010;   struct lol {     int lv;     int v0;     int v1;     int v2;     int v3;     int v4;     lol(int v = 0) {       lv = v;       v0 = v;       v1 = v;       v2 = -v;       v3 = -v;       v4 = 0;     }     friend ostream &operator<<(ostream &out, const lol &a) {       return out <<  (  << a.lv <<     << a.v0 <<     << a.v1 <<     << a.v2                  <<     << a.v3 <<     << a.v4 <<  ) ;     }   };   int m;   string s;   lol st[NMAX << 2];   int val(char ch) { return (ch ==  (  ? 1 : -1); }   lol combine(const lol &a, const lol &b) {     lol ret;     ret.lv = a.lv + b.lv;     ret.v0 = min(a.v0, a.lv + b.v0);     ret.v1 = max(a.v1, a.lv + b.v1);     ret.v2 = max(max(a.v2, -a.lv + b.v2), a.v1 - 2 * (a.lv + b.v0));     ret.v3 = max(max(a.v3, -a.lv + b.v3), -2 * a.v0 + (a.lv + b.v1));     ret.v4 = max(max(a.v4, b.v4), max(a.v1 - a.lv + b.v3, a.v2 + a.lv + b.v1));     return ret;   }   void build(int node, int l, int r) {     if (l == r) return void(st[node] = lol(val(s[l])));     int mid = (l + r) / 2;     build(2 * node, l, mid);     build(2 * node + 1, mid + 1, r);     st[node] = combine(st[2 * node], st[2 * node + 1]);   }   void update(int node, int l, int r, int p) {     if (l == r) return void(st[node] = lol(val(s[p])));     int mid = (l + r) / 2;     if (p <= mid)       update(2 * node, l, mid, p);     else       update(2 * node + 1, mid + 1, r, p);     st[node] = combine(st[2 * node], st[2 * node + 1]);   }   int main() {     ios_base::sync_with_stdio(false);     int n, q;     cin >> n >> q;     m = 2 * n - 2;     cin >> s;     build(1, 0, m - 1);     cout << st[1].v4 <<   n ;     while (q--) {       int a, b;       cin >> a >> b;       --a;       --b;       swap(s[a], s[b]);       update(1, 0, m - 1, a);       update(1, 0, m - 1, b);       cout << st[1].v4 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       string a, b, c;       cin >> a >> b >> c;       int n = a.length();       int count = 0;       for (int i = 0; i < n; i++) {         if (c[i] == a[i] || c[i] == b[i])           continue;         else {           count++;           break;         }       }       if (count)         cout <<  NO  << endl;       else         cout <<  YES  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, n[3], t[3];     while (scanf( %d , &k) == 1) {       for (int i = 0; i < 3; i++) scanf( %d , &n[i]);       for (int i = 0; i < 3; i++) scanf( %d , &t[i]);       deque<int> q[3];       for (int i = 0; i < 3; i++) q[i].clear();       int p = 0;       while (k--) {         for (int i = 0; i < 3; i++)           if (q[i].size() == n[i]) {             p = q[i].front() + t[i];             for (int j = 0; j < 3; j++) {               while (q[j].size() && q[j].front() + t[j] <= p) q[j].pop_front();             }             i = -1;           }         for (int i = 0; i < 3; i++) q[i].push_back(p);       }       printf( %d n , p + t[0] + t[1] + t[2]);     }     return 0;   }


#include<bits/stdc++.h>       using namespace std;       typedef long long ll;   typedef pair<int, int> pii;   typedef long double ld;       #define F first   #define S second       const int MOD = 998244353;// XXX   const int C = 26;// XXX       void add(int &x, int y){    x += y;    while (x >= MOD) x -= MOD;    while (x < 0) x += MOD;   }       int fix(int x){    while (x >= MOD) x -= MOD;    while (x < 0) x += MOD;    return x;   }       int pw(int a, int b){    int ret = 1;    while (b){     if (b & 1)      ret = 1ll*ret*a%MOD;     b >>= 1;     a = 1ll*a*a%MOD;    }    return ret;   }      int mul(int a, int b) {    return (ll) a * b % MOD;   }      const int MAXN = 405;   const int MAXE = 605;      int n, m, d[MAXN][MAXN], ans[MAXN][MAXN];   pii edges[MAXE];      int onPath[MAXN], foundLen[MAXN], numPar[MAXN];      void solve() {    cin >> n >> m;    memset(d, 63, sizeof(d));       for(int i = 0; i < m; i++) {     int a, b; cin >> a >> b; --a; --b;     edges[i] = {a, b};     d[a][b] = d[b][a] = 1;    }    for(int i = 0; i < n; i++)     d[i][i] = 0;        for(int k = 0; k < n; k++)     for(int i = 0; i < n; i++)      for(int j = 0; j < n; j++)       d[i][j] = min(d[i][j], d[i][k] + d[k][j]);        for(int u = 0; u < n; u++) {     for(int v = 0; v < n; v++) {      bool ok = 1;      int cnt = 1;      memset(onPath, 0, sizeof(onPath));      memset(numPar, 0, sizeof(numPar));      memset(foundLen, 0, sizeof(foundLen));         for(int w = 0; w < n; w++) {       if(d[u][w] + d[v][w] == d[u][v]) {        onPath[w] = 1;        if(foundLen[d[u][w]])          ok = 0;        foundLen[d[u][w]] = 1;       }      }      for(int len = 0; len <= d[u][v]; len++)        if(!foundLen[len])         ok = 0;         for(int e = 0; e < m; e++) {       for(int rot = 0; rot < 2; rot++) {        auto &[a, b] = edges[e];        if(!onPath[a]) {         if(d[u][a] == d[u][b] + 1 && d[v][a] == d[v][b] + 1) {          ++numPar[a];         }        }        swap(a, b);       }      }      for(int w = 0; w < n; w++)        if(!onPath[w])        cnt = mul(cnt, numPar[w]);            if(ok)        ans[u][v] = cnt;     }    }        for(int u = 0; u < n; u++) {     for(int v = 0; v < n; v++) {      cout << ans[u][v] <<    ;     }     cout <<   n ;    }   }      int main(){    ios::sync_with_stdio(false);    cin.tie(0);    cout.tie(0);    // cout << fixed << setprecision(6);        int te = 1;     // cin >> te;    for (int w = 1; w <= te; w++){     // cout <<  Case #  << w <<  :  ;     solve();    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());   mt19937 rnf(2106);   const int N = 100005;   int n, m, k;   vector<int> a[N];   vector<int> v[2];   void dfs0(int x, int p, int z) {     v[z].push_back(x);     for (int i = 0; i < a[x].size(); ++i) {       int h = a[x][i];       if (h == p) continue;       dfs0(h, x, (z ^ 1));     }   }   bool c[N];   int dfs(int x, int p) {     c[x] = true;     for (int i = 0; i < a[x].size(); ++i) {       int h = a[x][i];       if (h == p) continue;       if (!c[h]) {         int u = dfs(h, x);         if (u) return u;       } else         return x;     }     return 0;   }   int p[N];   int d[N];   int f[N];   vector<int> bfs(int s) {     memset(c, false, sizeof c);     queue<int> q;     q.push(s);     c[s] = true;     int minu = N;     int xx, yy;     while (!q.empty()) {       int x = q.front();       q.pop();       for (int i = 0; i < a[x].size(); ++i) {         int h = a[x][i];         if (!c[h]) {           c[h] = true;           p[h] = x;           d[h] = d[x] + 1;           if (x != s)             f[h] = f[x];           else             f[h] = h;           q.push(h);         } else {           if (x != s && h != s) {             if (f[h] != f[x]) {               if (d[x] + d[h] < minu) {                 minu = d[x] + d[h];                 xx = x;                 yy = h;               }             }           }         }       }     }     vector<int> v;     for (int i = xx; i != s; i = p[i]) v.push_back(i);     vector<int> v1;     for (int i = yy; i != s; i = p[i]) v1.push_back(i);     reverse((v1).begin(), (v1).end());     v.push_back(s);     for (int i = 0; i < v1.size(); ++i) v.push_back(v1[i]);     return v;   }   void solv() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < m; ++i) {       int x, y;       scanf( %d%d , &x, &y);       a[x].push_back(y);       a[y].push_back(x);     }     if (m == n - 1) {       dfs0(1, 1, 0);       if (v[1].size() > v[0].size()) swap(v[0], v[1]);       printf( 1 n );       for (int i = 0; i < k / 2 + k % 2; ++i) printf( %d  , v[0][i]);       printf(  n );       return;     }     int s = dfs(1, -1);     vector<int> v = bfs(s);     if (v.size() <= k) {       printf( 2 n );       printf( %d n , v.size());       for (int i = 0; i < v.size(); ++i) printf( %d  , v[i]);       printf(  n );       return;     }     vector<int> vv;     for (int i = 0; i < v.size(); i += 2) vv.push_back(v[i]);     v = vv;     printf( 1 n );     for (int i = 0; i < k / 2 + k % 2; ++i) printf( %d  , v[i]);     printf(  n );   }   int main() {     solv();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 2e5 + 9, LG = 35;   long long sum, b[MAX], c[MAX], a[MAX], n, k[LG];   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> b[i], sum += b[i];     for (int i = 0; i < n; i++) cin >> c[i], sum += c[i];     if (sum % (2 * n) != 0) return cout << -1, 0;     sum /= 2 * n;     for (int i = 0; i < n; i++) {       a[i] = b[i] + c[i] - sum;       if (a[i] % n != 0 || a[i] < 0) return cout << -1, 0;       a[i] /= n;       long long x = a[i];       for (int j = 0; j < LG; j++) k[j] += x >> j & 1;     }     for (int i = 0; i < n; i++) {       long long sum1 = 0, sum2 = 0;       for (int j = 0; j < LG; j++)         if (a[i] >> j & 1)           sum1 += k[j] * (1 << j), sum2 += n * (1 << j);         else           sum2 += k[j] * (1 << j);       if (sum1 != b[i] || sum2 != c[i]) return cout << -1, 0;     }     for (int i = 0; i < n; i++) cout << a[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int mx1, my1, mx2, my2;   string in;   int main() {     ios::sync_with_stdio(0);     cin >> in;     mx1 = in[0] -  a  + 1;     my1 = in[1] -  0 ;     cin >> in;     mx2 = in[0] -  a  + 1;     my2 = in[1] -  0 ;     cout << max(abs(mx1 - mx2), abs(my1 - my2)) << endl;     while (1) {       if (mx1 == mx2 && my1 == my2) break;       if (mx1 > mx2) {         cout <<  L ;         mx1 -= 1;       } else if (mx1 < mx2) {         cout <<  R ;         mx1 += 1;       }       if (my1 < my2) {         cout <<  U ;         my1 += 1;       } else if (my1 > my2) {         cout <<  D ;         my1 -= 1;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void load() {}   const int N = 5e5 + 123;   const int inf = 1e9;   int a[N], v[N];   int dp[200][200];   void trungtt() {     int n, k, p;     scanf( %d%d%d , &n, &k, &p);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]), a[i] += a[i - 1], a[i] %= p;     if (k * p > n) {       for (int i = 0; i <= p; ++i)         for (int j = 0; j <= k; ++j) dp[i][j] = inf;       dp[0][0] = 0;       for (int i = 1; i <= n; ++i)         for (int j = k; j >= 1; --j)           for (int t = 0; t < p; ++t)             dp[a[i]][j] = min(dp[a[i]][j], dp[t][j - 1] + (a[i] - t + p) % p);       printf( %d , dp[a[n]][k]);     } else {       for (int i = 0; i <= n; ++i) v[i] = inf;       for (int i = 1; i < n; ++i) *upper_bound(v + 1, v + n, a[i]) = a[i];       if (v[k - 1] <= a[n])         printf( %d , a[n]);       else         printf( %d , a[n] + p);     }   }   int main() {     load();     trungtt();     return 0;   }