#include <bits/stdc++.h>   using namespace std;   const int32_t MOD = 1000000007;   struct WEdge {     int32_t u, v;     int w;     bool operator<(const WEdge& other) { return w < other.w; }     WEdge(int32_t u, int32_t v, int w) : u(u), v(v), w(w) {}     WEdge() {}     void in() { cin >> u >> v >> w; }     void trace() {       if (0) cout << u <<     << v <<     << w <<   n ;     }   };   struct Edge {     int32_t u, v;     Edge(int32_t u, int32_t v) : u(u), v(v) {}     void in() { cin >> u >> v; }   };   struct UnionSet {     map<int32_t, pair<int32_t, int32_t> > sets;     void makeSet(int32_t x) { sets.emplace(x, make_pair(x, 0)); }     void init(int32_t a, int32_t b) {       for (int x = (a); x < (b); ++x) makeSet(x);     }     int find(int32_t x) {       if (sets[x].first == x)         return x;       else         return sets[x].first = find(sets[x].first);     }     void union_both(int32_t x, int32_t y) {       int32_t parentX = find(x), parentY = find(y);       int32_t rankX = sets[parentX].second, rankY = sets[parentY].second;       if (parentX == parentY)         return;       else if (rankX < rankY)         sets[parentX].first = parentY;       else         sets[parentY].first = parentX;       if (rankX == rankY) sets[parentX].second++;     }   };   int32_t add(int32_t a, int32_t b) {     int32_t r = a + b;     if (r >= MOD) r -= MOD;     return r;   }   int32_t sub(int32_t a, int32_t b) {     int32_t r = a - b;     if (r < 0) r += MOD;     return r;   }   int32_t mul(int32_t a, int32_t b) { return (a * 1ll * b) % MOD; }   int32_t pow_mod(int32_t a, int64_t n) {     if (n == 0)       return 1;     else {       int32_t tmp = pow_mod(a, n / 2);       tmp = mul(tmp, tmp);       if (n % 2 == 0)         return tmp;       else         return mul(tmp, a);     }   }   int32_t fact_mod(int32_t n) {     int32_t r = 1;     for (int32_t k = 1; k <= n; k++) r = mul(r, k);     return r;   }   vector<int> children[200001];   int a[200001];   int64_t S[200001];   int64_t T[200001];   void dfs_init(int u, int pred) {     S[u] = a[u];     T[u] = 0;     for (auto v : children[u]) {       if (v != pred) {         dfs_init(v, u);         S[u] += S[v];         T[u] += T[v] + S[v];       }     }   }   int64_t M = -1;   void dfs_maxt(int u, int pred, int64_t sa, int64_t ta) {     M = max(M, ta + T[u]);     sa += a[u];     ta += sa;     int64_t ts = 0, tt = 0;     for (auto v : children[u]) {       if (v != pred) {         ts += S[v];         tt += 2ll * S[v] + T[v];       }     }     for (auto v : children[u]) {       if (v != pred) {         dfs_maxt(v, u, sa + ts - S[v], ta + tt - T[v] - 2ll * S[v]);       }     }   }   int32_t main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     for (int i = (0); i < (n); ++i) cin >> (a + 1)[i];     int u, v;     for (int i = (0); i < (n - 1); ++i) {       cin >> u >> v;       children[u].push_back(v);       children[v].push_back(u);     }     dfs_init(1, -1);     dfs_maxt(1, -1, 0, 0);     cout << M <<   n ;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   template <class T>   T gcd(T a, T b) {     return ((b == 0) ? a : gcd(b, a % b));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, x;     cin >> n >> x;     n %= 6;     for (int i = 0; i < (int)(3); i++) {       vector<int> a(3, 0);       a[i] = 1;       for (int j = 0; j < (int)(n); j++) {         if (j % 2 == 0)           swap(a[0], a[1]);         else           swap(a[1], a[2]);       }       if (a[x]) {         cout << i << endl;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int t, l, r, d, m, n, i;     scanf( %d , &t);     for (i = 0; i < t; i++) {       scanf( %d %d %d , &l, &r, &d);       if (l > d || r < d) {         printf( %d n , d);       } else {         m = (r / d) + 1;         n = d * m;         printf( %d n , n);       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast ,  unroll-loops ,  no-stack-protector )   using namespace std;   template <class T>   using minheap = priority_queue<T, vector<T>, greater<T>>;   template <typename T>   void setmax(T& a, T b) {     a = max(a, b);   };   template <typename T>   void setmin(T& a, T b) {     a = min(a, b);   };   const int N = 3010;   int n;   vector<int> g[N];   long long sz[N][N];   int par[N][N];   long long dp[N][N];   void dfs(int root, int x, int p = 0) {     sz[root][x]++;     par[root][x] = p;     for (int y : g[x]) {       if (y == p) continue;       dfs(root, y, x);       sz[root][x] += sz[root][y];     }   }   long long solve(int u, int v) {     if (dp[u][v] != -1) return dp[u][v];     if (u > v) return dp[u][v] = solve(v, u);     if (u == v) return dp[u][v] = 0;     return dp[u][v] = sz[u][v] * sz[v][u] +                       max(solve(par[v][u], v), solve(u, par[u][v]));   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     for (int i = 1; i <= n; i++) {       dfs(i, i);     }     memset(dp, -1, sizeof dp);     long long ans = 0;     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         setmax(ans, solve(i, j));       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int s, x, y, a, b;     cin >> s >> x >> y >> a >> b;     if ((x * s) + (2 * a) < (y * s) + (2 * b)) {       cout <<  First ;       return 0;     }     if ((x * s) + (2 * a) > (y * s) + (2 * b)) {       cout <<  Second ;       return 0;     }     if ((x * s) + (2 * a) == (y * s) + (2 * b)) {       cout <<  Friendship ;       return 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[1111];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long n, ans = 0;     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + 1 + n);     for (long long i = 1; i < n; i++) {       ans = ans + (a[i + 1] - a[i] - 1);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t, n;   string s;   int main() {     cin >> t;     for (int x = 1; x <= t; x++) {       cin >> n;       cin >> s;       string ans;       long long v = 0;       for (int i = 0; i < s.length(); i++) {         ans += s[i + (v++)];         if (i == n - 1) break;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace std::chrono;   bool prime[2000005];   vector<int> myarr;   void myprime(int n) {     for (long int i = 0; i < n; i++) prime[i] = true;     for (int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (int i = p * p; i <= n; i += p) prime[i] = false;       }     }     for (int p = 2; p <= n; p++)       if (prime[p]) myarr.push_back(p);   }   int gcd(int a, int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   void swap(int &a, int &b) {     int temp = a;     a = b;     b = temp;   }   int max(int a, int b) { return (a > b ? a : b); }   vector<long long> a(15);   void moj(long long n) {}   bool cmp(pair<string, int> p1, pair<string, int> p2) {     if (p2.second < p1.second) return true;     return false;   }   void solve() {     int n;     cin >> n;     map<string, int> ms;     vector<string> vs(n);     for (long int i = 0; i < n; i++) cin >> vs[i];     int rr = 1;     for (long int i = 0; i < n; i++) {       string s;       s = vs[i];       ms[s] = rr;       rr++;     }     vector<pair<string, int> > sss;     for (auto &i : ms) {       sss.push_back(i);     }     sort(sss.begin(), sss.end(), cmp);     for (auto i : sss) {       cout << i.first <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   struct ky {     int u, val;     bool operator<(const ky &p) const { return val < p.val; }   } a[N], b[N];   int n, ca, cb, ci, vali;   bool tg[N];   inline void fr(int &num) {     num = 0;     char c = getchar();     int p = 1;     while (c <  0  || c >  9 ) c ==  -  ? p = -1, c = getchar() : c = getchar();     while (c >=  0  && c <=  9 ) num = num * 10 + c -  0 , c = getchar();     num *= p;   }   int main() {     fr(n);     for (register int i = 1; i <= n; ++i)       fr(ci), fr(vali), ci ? a[++ca] = (ky){i, vali} : b[++cb] = (ky){i, vali};     sort(a + 1, a + 1 + ca), sort(b + 1, b + 1 + cb);     register int i = 1, j = 1, minn;     while (i <= ca && j <= cb)       minn = (a[i].val < b[j].val ? a[i].val : b[j].val),       printf( %d %d %d n , a[i].u, b[j].u, minn), a[i].val -= minn,       b[j].val -= minn, tg[a[i].u] = tg[b[j].u] = true, !a[i].val ? ++i : ++j;     while (i <= ca) {       if (!tg[a[i].u]) printf( %d %d 0 n , a[i].u, b[1].u), tg[a[i].u] = 1;       ++i;     }     while (j <= cb) {       if (!tg[b[j].u]) printf( %d %d 0 n , a[1].u, b[j].u), tg[b[j].u] = 1;       ++j;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     struct node* left;     struct node* right;     int vl;     node() {       vl = 0;       left = NULL;       right = NULL;     }   };   int a[300005];   int ans = 0;   void query(node* n, int v, int i, int j) {     if (n == NULL || j == 0) return;     int no = (1 << (j - 1));     node* l2 = n->left;     node* r2 = n->right;     int l1 = 0, r1 = 0;     if (l2 != NULL) l1 = l2->vl;     if (r2 != NULL) r1 = r2->vl;     j--;     if ((v & (1 << j)) == 0) {       if (l1 == no) {         ans += (1 << (j));         query(n->right, v, i / 2, j);         return;       }       query(n->left, v, i / 2, j);       return;     }     if (r1 == no) {       ans += (1 << (j));       query(n->left, v, i / 2, j);       return;     }     query(n->right, v, i / 2, j);     return;   }   void insert(node* n, int val, int i, int j) {     if (j == 0) return;     j--;     if ((val & (1 << j)) == 0) {       if (n->left == NULL) {         node* cur = new node();         n->left = cur;       }       insert(n->left, val, i / 2, j);       return;     }     if (n->right == NULL) {       node* cur = new node();       n->right = cur;     }     insert(n->right, val, i / 2, j);     return;   }   int dfs(node* n) {     if (n->left == NULL && n->right == NULL) {       n->vl = 1;       return 1;     }     int w = 0;     int u = 0;     if (n->left != NULL) {       w += dfs(n->left);       u++;     }     if (n->right != NULL) {       w += dfs(n->right);       u++;     }     n->vl = w;     return w;   }   int main() {     int n, m;     cin >> n >> m;     node* h = new node();     node* w1 = h;     for (int i = 1; i <= n; i++) {       cin >> a[i];       h = w1;       insert(h, a[i], (1 << 20) - 1, 19);     }     int x = 0;     h = w1;     dfs(h);     for (int i = 1; i <= m; i++) {       int x1;       cin >> x1;       x = x ^ x1;       ans = 0;       h = w1;       query(h, x, (1 << 20) - 1, 19);       cout << ans <<   n ;     }   }


#include <iostream>   #include <set>   #include <vector>   #include <algorithm>   using namespace std;   typedef long long ll;      vector<int> res;   vector<pair<int, int>> edges;   vector<vector<int>> v;   int n, k;      int buildTree(set<int> idx, int rootSalary) {    if (idx.size() == 1) { return *idx.begin(); }    int curRoot = k++; res.push_back(rootSalary);       while (!idx.empty()) {     int cur = *idx.begin(), nextRootSalary = 0;     set<int> newIDX; newIDX.insert(cur);        for (auto& x : idx) if(cur != x && v[cur][x] < rootSalary) {      nextRootSalary = max(nextRootSalary, v[cur][x]);      newIDX.insert(x);     }     for (auto& x : newIDX) { idx.erase(x); }        int child = buildTree(newIDX, nextRootSalary);     edges.push_back({ child, curRoot });    }       return curRoot;   }      int main() {    ios::sync_with_stdio(0);    cin.tie(0), cout.tie(0);       cin >> n; k = n; v.resize(n, vector<int>(n)); res.resize(n); int ma = 0;    for (auto& x : v) for (auto& y : x) { cin >> y; ma = max(ma, y); }    for (int i = 0; i < n; i++) res[i] = v[i][i];       set<int> idx; for (int i = 0; i < n; i++) idx.insert(i);    int head = buildTree(idx, ma);       cout << res.size() <<   n ; for (auto& x : res)      cout << x <<    ; cout <<   n ;    cout << head + 1 <<   n ; for (auto& e : edges)      cout << ++e.first <<     << ++e.second <<   n ;       cin.ignore(2); return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200200;   int n;   int a[N];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int cnt_0 = 0, cnt_neg = 0, pos = -1;     vector<int> used(n);     for (int i = 0; i < n; i++) {       if (a[i] == 0) {         used[i] = 1;         cnt_0++;       }       if (a[i] < 0) {         cnt_neg++;         if (pos == -1 || abs(a[pos]) > abs(a[i])) {           pos = i;         }       }     }     if (cnt_neg & 1) {       used[pos] = 1;     }     if (cnt_0 == n || (cnt_0 == n - 1 && cnt_neg == 1)) {       for (int i = 0; i < n - 1; i++) {         cout << 1 <<     << i + 1 <<     << i + 2 <<   n ;       }       return 0;     }     int lst = -1;     for (int i = 0; i < n; i++) {       if (!used[i]) continue;       if (lst != -1) {         cout << 1 <<     << lst + 1 <<     << i + 1 <<   n ;       }       lst = i;     }     if (lst != -1) cout << 2 <<     << lst + 1 <<   n ;     lst = -1;     for (int i = 0; i < n; i++) {       if (used[i]) continue;       if (lst != -1) {         cout << 1 <<     << lst + 1 <<     << i + 1 <<   n ;       }       lst = i;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     string s;     int n, x = 0, ans;     cin >> s;     n = s.length();     for (int i = 0; i < n; i++) {       x = x * 10 + (s[i] -  0 );       x = x % 4;     }     ans = (1 + (int)pow(2, x) + (int)pow(3, x) + (int)pow(4, x)) % 5;     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie();     int N, ans = 0;     string s;     cin >> N >> s;     for (int i = 0, j; i < N;) {       if (s[i] ==  ? ) {         for (j = i; j < N && s[j] ==  ? ; j++)           ;         if (j - i > 1 || !i || j == N || s[i - 1] == s[j]) ans = 1;         i = j;       } else {         if (i && s[i - 1] == s[i]) {           cout <<  No n ;           return 0;         }         i++;       }     }     if (ans)       cout <<  Yes n ;     else       cout <<  No n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using lint = long long int;   constexpr int MOD = 1000000007;   int main() {     int T;     cin >> T;     for (int t = (0), t_end_ = (T); t < t_end_; t++) {       int r, c, k;       cin >> r >> c >> k;       vector<string> map(r);       for (int i = (0), i_end_ = (r); i < i_end_; i++) {         cin >> map[i];       }       int rice = 0;       for (int i = (0), i_end_ = (r); i < i_end_; i++) {         for (int j = (0), j_end_ = (c); j < j_end_; j++) {           if (map[i][j] ==  R ) {             rice++;           }         }       }       int a = rice / k;       int b = rice % k;       vector<int> ricelist(k, a);       for (int i = (0), i_end_ = (b); i < i_end_; i++) {         ricelist[i]++;       }       vector<vector<char>> ans(r, vector<char>(c));       int nowk = 0;       for (int i = (0), i_end_ = (r); i < i_end_; i++) {         if (i % 2 == 0) {           for (int j = (0), j_end_ = (c); j < j_end_; j++) {             if (nowk >= 36) {               ans[i][j] =  a  + (nowk - 36);             } else if (nowk >= 10) {               ans[i][j] =  A  + (nowk - 10);             } else {               ans[i][j] =  0  + nowk;             }             if (map[i][j] ==  R ) {               ricelist[nowk]--;             }             if (ricelist[nowk] == 0 && nowk != k - 1) {               nowk++;             }           }         } else {           for (int j = (c)-1, j_begin_ = (0); j >= j_begin_; j--) {             if (nowk >= 36) {               ans[i][j] =  a  + (nowk - 36);             } else if (nowk >= 10) {               ans[i][j] =  A  + (nowk - 10);             } else {               ans[i][j] =  0  + nowk;             }             if (map[i][j] ==  R ) {               ricelist[nowk]--;             }             if (ricelist[nowk] == 0 && nowk != k - 1) {               nowk++;             }           }         }       }       for (int i = (0), i_end_ = (r); i < i_end_; i++) {         for (int j = (0), j_end_ = (c); j < j_end_; j++) {           cout << ans[i][j];         }         cout << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   inline void boost() {     ios_base::sync_with_stdio();     cin.tie(0);     cout.tie(0);   }   const long long maxn = 1e5 + 123;   const long long inf = 1e9 + 123;   const long long mod = 1e9 + 7;   const double infm = 1e-9;   const double pi = acos(-1);   int a[maxn];   bool used[maxn];   int main() {     boost();     int n, k;     cin >> n >> k;     int p = 0, m = 0;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     int mx = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (abs(j - i) % k == 0) {           used[j] = 1;         }       }       int sum = 0, sum1 = 0;       for (int j = 1; j <= n; j++) {         if (used[j] == 0) {           if (a[j] == 1) {             sum++;           } else {             sum1++;           }         }         used[j] = 0;       }       mx = max(mx, abs(sum - sum1));     }     cout << mx;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   double ans = 0;   map<double, double> F;   void insert(double dist, double s) {     double need = dist / (s + 1);     F[s] += need;     if (s > 1) F[s] += dist / (s - 1);     ans += need;     while (need > 1e-9) {       map<double, double>::iterator it = --F.end();       double y = it->first;       double dlt = min(need, F[y]);       F[y] -= dlt;       need -= dlt;       ans += dlt / y;       if (F[y] < 1e-9) {         F.erase(it);       }     }   }   int n, L, ls;   int main() {     scanf( %d%d , &n, &L);     int l, r;     double s;     while (n--) {       scanf( %d%d%lf , &l, &r, &s);       insert(l - ls, 1);       insert(r - l, s + 1);       ls = r;     }     insert(L - ls, 1);     printf( %.10lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> arr[100100];   int dist[100100];   map<int, int> m1;   void dfs(int curr, int par) {     dist[curr] = dist[par] + 1;     if (curr == par) {       dist[curr] = 0;     }     for (auto x : arr[curr]) {       if (x != par) {         dfs(x, curr);       }     }   }   int main() {     int n;     cin >> n;     for (int i = 2; i <= n; i++) {       int x;       cin >> x;       arr[i].push_back(x);       arr[x].push_back(i);     }     dfs(1, 1);     int ans = 0;     for (int i = 1; i <= n; i++) {       m1[dist[i]]++;     }     for (auto it = m1.begin(); it != m1.end(); it++) {       ans += (it->second) % 2;     }     cout << ans << endl;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6;   vector<int> gr[N];   long long int edges, z, o;   int col[N], vis[N];   void color(int n) {     for (int i = 0; i < gr[n].size(); i++) {       if (col[gr[n][i]] == -1) {         if (col[n] == 0) {           o++;           col[gr[n][i]] = 1;         } else {           z++;           col[gr[n][i]] = 0;         }         edges++;         color(gr[n][i]);       }     }   }   int main() {     int n, a, b;     cin >> n;     for (int i = 0; i < n - 1; i++) {       col[i] = -1;       cin >> a >> b;       gr[a].push_back(b);       gr[b].push_back(a);     }     col[n - 1] = -1;     col[n] = -1;     col[1] = 0;     z++;     color(1);     if (n == 1) {       cout << 0 << endl;     } else {       cout << z * o - edges << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y;     cin >> n >> x >> y;     int arr[n];     int countx = 0;     for (int i = 0; i < n; i++) {       cin >> arr[i];       if (arr[i] <= x) {         countx++;       }     }     if (y < x) {       cout << n << endl;       return 0;     }     if (y >= x) {       if (countx % 2 == 0) {         countx = countx / 2;       } else {         countx = countx / 2 + 1;       }       cout << countx << endl;       return 0;     }   }


#include <bits/stdc++.h>   inline char getThirdColor(char a, char b) {     char ret;     if (a ==  B  && b ==  G  || a ==  G  && b ==  B )       ret =  R ;     else if (a ==  B  && b ==  R  || a ==  R  && b ==  B )       ret =  G ;     else if (a ==  R  && b ==  G  || a ==  G  && b ==  R )       ret =  B ;     return ret;   }   std::unordered_map<std::string, bool> ump;   inline bool solve(std::string s, char c) {     if (s.size() == 1) {       return s[0] == c;     }     if (ump.find(s) != ump.end()) return ump[s];     bool ret = false;     for (int i = 0; i < s.size(); ++i) {       for (int j = i + 1; j < s.size(); ++j) {         if (s[i] == s[j]) {           std::string next_pass{s[i]};           for (int k = 0; k < s.size(); ++k) {             if (k != i && k != j) next_pass += s[k];           }           ret = solve(next_pass, c);         } else {           char thirdColor = getThirdColor(s[i], s[j]);           std::string next_pass{thirdColor};           for (int k = 0; k < s.size(); ++k) {             if (k != i && k != j) next_pass += s[k];           }           ret = solve(next_pass, c);         }         if (ret) break;       }       if (ret) break;     }     return ump[s] = ret;   }   int main() {     int n;     std::cin >> n;     std::string s;     std::cin >> s;     std::string modified;     modified.reserve(n);     std::unordered_map<char, int> cnt;     for (int i = 0; i < s.size(); ++i) {       if (modified.empty()) {         modified += s[i];         ++cnt[s[i]];       } else if (modified.back() == s[i] && cnt[s[i]] == 1) {         modified += s[i];         ++cnt[s[i]];       } else if (modified.back() != s[i]) {         modified += s[i];         ++cnt[s[i]];       }     }     std::vector<char> possible{ B ,  G ,  R };     for (int i = 0; i < possible.size(); ++i) {       ump.clear();       if (solve(modified, possible[i])) {         std::cout << possible[i];       }     }     std::cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t, n, k;     cin >> t;     while (t--) {       long long ats = 0;       cin >> n >> k;       while (n) {         ats += n % k;         n -= n % k;         while (n % k == 0 && n) n /= k, ats++;       }       cout << ats <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2000001;   int n, m, x;   pair<int, int> a[maxn];   int st[maxn], q[maxn], cnt, ans[maxn], tail, top;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i].first);     }     int x;     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       a[n + i].first = m - 1 - x;       a[n + i].second = 1;     }     sort(a + 1, a + 1 + 2 * n);     top = 0;     tail = 0;     for (int i = 1; i <= 2 * n; i++) {       if (a[i].second == 0)         st[++top] = a[i].first;       else {         if (top == 0)           q[++tail] = a[i].first;         else           ans[++cnt] = (m - 1 - a[i].first + st[top--]) % m;       }     }     for (int i = 1; i <= tail; i++)       ans[++cnt] = (st[i] + m - 1 - q[tail - i + 1]) % m;     sort(ans + 1, ans + 1 + n);     reverse(ans + 1, ans + 1 + n);     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     cout << endl;     fclose(stdin);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 15;   int n, a[MAXN], b[MAXN], inf = 1e9 + 19, pos, st[MAXN], en[MAXN], LIS,                            cnt[MAXN];   void fil() {     b[0] = -inf;     for (int i = 1; i < MAXN; i++) b[i] = inf;   }   int bs(int x) {     int s = 0, e = 1e5 + 10;     while (s + 1 < e) {       int mid = (s + e) / 2;       if (b[mid] < x)         s = mid;       else         e = mid;     }     if (b[s] >= x) {       b[s] = x;       return s;     } else {       b[e] = x;       return e;     }   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     fil();     for (int i = 0; i < n; i++) {       pos = bs(a[i]);       st[i] = pos;     }     for (int i = 0; i <= n; i++)       if (b[i] != inf) LIS = i;     fil();     for (int i = n - 1; i >= 0; i--) {       pos = bs(-a[i]);       en[i] = pos;     }     for (int i = 0; i < n; i++)       if (st[i] + en[i] - 1 == LIS) cnt[st[i]]++;     for (int i = 0; i < n; i++) {       if (st[i] + en[i] - 1 != LIS)         cout << 1;       else if (cnt[st[i]] == 1)         cout << 3;       else         cout << 2;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<char> s(n);     long long ans = 0;     for (int i = 0; i < n; ++i) {       cin >> s[i];       if (i > 0 and s[i] ==  .  and s[i - 1] ==  . ) ++ans;     }     for (int i = 0; i < m; ++i) {       int pos;       cin >> pos;       --pos;       char c;       cin >> c;       if (c ==  .  and s[pos] !=  . ) {         if (pos > 0 and s[pos - 1] ==  . ) ++ans;         if (pos < n and s[pos + 1] ==  . ) ++ans;       } else if (c !=  .  and s[pos] ==  . ) {         if (pos > 0 and s[pos - 1] ==  . ) --ans;         if (pos < n and s[pos + 1] ==  . ) --ans;       }       s[pos] = c;       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   template <class T>   using pq = priority_queue<T>;   template <class T>   using pqg = priority_queue<T, vector<T>, greater<T>>;   template <class T>   bool ckmin(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   template <class T>   bool ckmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char* x) { cerr <<     << x <<    ; }   void __print(const string& x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V>& x) {     cerr <<  { ;     __print(x.first);     cerr <<  ,  ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T& x) {     int first = 0;     cerr <<  { ;     for (auto& i : x) cerr << (first++ ?  ,   :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   const int MOD = 1000000007;   const char nl =   n ;   const int MX = 100001;   void solve() {     int N;     cin >> N;     int A[N];     for (int i = 0; i < (N); i++) cin >> A[i];     int sum = 0;     for (int i = 0; i < (N); i++) sum += A[i];     if (sum % 2) {       cout <<  NO  << nl;       return;     }     int cnt = 0;     vector<int> ops;     for (int i = 0; i < (N); i++) {       cnt += A[i];       if (A[i] == 0) {         if (cnt % 2) {           ops.push_back(i - 1);           int X = 0;           for (int j = 0; j < (3); j++) X ^= A[i - 1 + j];           for (int j = 0; j < (3); j++) A[i - 1 + j] = X;         }         if (A[i] == 0) {           cnt = 0;         } else           cnt++;       }     }     sum = 0;     for (int i = 0; i < (N); i++) sum += A[i];     if (sum == N) {       cout <<  NO  << nl;       return;     }     int fz = 0;     for (int i = (N)-1; i >= 0; i--)       if (A[i] == 0) fz = i;     while (fz > 0) {       ops.push_back(fz - 2);       A[fz - 2] = 0;       A[fz - 1] = 0;       A[fz] = 0;       fz -= 2;     }     for (int i = 0; i < (N - 1); i++) {       if (A[i] == 0 && A[i + 1] == 1) {         ops.push_back(i);         A[i] = 0;         A[i + 1] = 0;         A[i + 2] = 0;       }     }     cout <<  YES  << nl;     cout << (int)(ops).size() << nl;     for (auto& a : ops) {       cout << a + 1 <<    ;     }     cout << nl;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int T = 1;     cin >> T;     while (T--) {       solve();     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int MAX_L = 20;   const long long MOD = 1e9 + 7;   const long long INF = 1e9 + 7;   int p = 100000007;   template <typename T>   void scan(T &x) {     x = 0;     bool neg = 0;     register T c = getchar();     if (c ==  - ) neg = 1, c = getchar();     while ((c < 48) || (c > 57)) c = getchar();     for (; c < 48 || c > 57; c = getchar())       ;     for (; c > 47 && c < 58; c = getchar()) x = (x << 3) + (x << 1) + (c & 15);     if (neg) x *= -1;   }   template <typename T>   void print(T n) {     bool neg = 0;     if (n < 0) n *= -1, neg = 1;     char snum[65];     long long int i = 0;     do {       snum[i++] = n % 10 +  0 ;       n /= 10;     } while (n);     --i;     if (neg) putchar( - );     while (i >= 0) putchar(snum[i--]);     putchar(  n );   }   bool semiprime(long long int num) {     int cnt = 0;     for (int i = 2; cnt < 2 && i * i <= num; ++i)       while (num % i == 0) num /= i, ++cnt;     if (num > 1) ++cnt;     return cnt == 2;   }   bool prime(long long int n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (int i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   vector<pair<long long int, long long int>> g[100001];   long long int dfs(long long int nod, long long int y) {     long long int ret = 0;     for (auto x : g[nod]) {       if (x.first != y) {         ret = max(ret, x.second + dfs(x.first, nod));       }     }     return ret;   }   void overpower() {     long long int n, x, y, z;     long long int tot = 0;     cin >> n;     for (int i = 0; i < n - 1; i++) {       cin >> x >> y >> z;       g[x].push_back(make_pair(y, z));       g[y].push_back(make_pair(x, z));       tot += 2 * z;     }     cout << tot - dfs(1, 0);   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int t;     t = 1;     long long int i;     for (long long int i = i = 0; i < t; i++) {       overpower();     }   }


#include <bits/stdc++.h>   using namespace std;   int n, k, Ord[100000 + 5];   long long ans, Set[100000 + 5][5];   inline int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }   int main() {     scanf( %d%d , &n, &k);     printf( %d n , (n * 6 - 1) * k);     for (int i = 0; i < n; i++)       printf( %d %d %d %d n , (1 + i * 6) * k, (2 + i * 6) * k, (3 + i * 6) * k,              (5 + i * 6) * k);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int max(long long int a, long long int b) {     if (a > b)       return a;     else       return b;   }   long long int min(long long int a, long long int b) {     if (a < b)       return a;     else       return b;   }   const int dx[4] = {-1, 1, 0, 0};   const int dy[4] = {0, 0, -1, 1};   int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1};   int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1};   vector<pair<long long int, long long int> > edges;   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     long long int n, m, i, j, k, l, r, ind, c, d, x;     cin >> n >> k;     j = k + 1;     long long int z = n / k;     long long int count = 0, ans = 2 * z;     j = 2;     vector<long long int> vec, vec2;     for (i = 1; i <= k; i++) {       count = 0;       while (count < z && j <= n) {         if (count == 0)           edges.push_back(make_pair(1, j));         else           edges.push_back(make_pair(j - 1, j));         j++;         count++;       }       vec2.push_back(count);       vec.push_back(j - 1);     }     if (j == n + 1) {       sort(vec2.begin(), vec2.end());       long long int sum = vec2[vec2.size() - 1];       if (vec2.size() >= 2) sum += vec2[vec2.size() - 2];       cout << sum <<   n ;       for (i = 0; i < edges.size(); i++) {         cout << edges[i].first <<     << edges[i].second <<   n ;       }       return 0;     }     long long int flag = 0;     for (i = 0; i < vec.size(); i++) {       if (j == n + 1) break;       edges.push_back(make_pair(vec[i], j));       j++;       flag++;     }     if (flag == 1)       ans++;     else if (flag > 1)       ans += 2;     cout << ans <<   n ;     for (i = 0; i < edges.size(); i++) {       cout << edges[i].first <<     << edges[i].second <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   char arr[1000007];   void sieve() {     arr[0] = 1;     arr[1] = 1;     for (int i = 4; i <= 1000007; i += 2) arr[i] = 1;     for (int i = 3; i * i <= 1000007; i += 2) {       if (arr[i] == 0) {         for (int j = i; i * j <= 1000007; j++) {           arr[i * j] = 1;         }       }     }   }   int main() {     sieve();     int n;     cin >> n;     int p = n + 1;     if (n < 3) {       if (n == 1) {         cout <<  1  << endl;         cout <<  1  << endl;         return 0;       } else {         cout <<  1  << endl;         cout <<  1 1  << endl;         return 0;       }     }     cout <<  2  << endl;     for (int i = 2; i <= p; i++) {       if (arr[i] == 0)         cout <<  1  ;       else         cout <<  2  ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, vector<int> > m;   int main() {     int n, k, query;     scanf( %d %d , &n, &k);     for (int i = 0; i < (int)(k); i++) {       scanf( %d , &query);       m[query - 1].push_back(i);     }     int ans = 0;     std::vector<int>::iterator low;     for (int i = 0; i < (int)(n); i++) {       if (m[i].size() == 0) {         ans += 3;         if (i == 0 || i == n - 1) ans--;       } else {         if (i - 1 >= 0) {           low = lower_bound(m[i - 1].begin(), m[i - 1].end(), m[i][0]);           if (low == m[i - 1].end() || m[i - 1].size() == 0) {             ans++;           }         }         if (i + 1 < n) {           low = lower_bound(m[i + 1].begin(), m[i + 1].end(), m[i][0]);           if (low == m[i + 1].end() || m[i + 1].size() == 0) ans++;         }       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 1e6 + 1;   int n, m;   string s[3];   int dp[mx][8];   bool isok(int i, int j) {     int cnt = 0;     for (int k = 0; k < 2; k++) cnt += ((i >> k) & 1) + ((j >> k) & 1);     if (cnt % 2 == 0) return false;     if (n == 2) return true;     cnt = 0;     for (int k = 1; k < 3; k++) cnt += ((i >> k) & 1) + ((j >> k) & 1);     return cnt & 1;   }   int solve(int idx, int msk) {     if (idx == m) return 0;     int &ans = dp[idx][msk];     if (~ans) return ans;     ans = 1e9;     for (int i = 0; i < (1 << n); i++)       if (idx == 0 || isok(msk, i)) {         int cur = 0;         for (int j = 0; j < n; j++) cur += ((i >> j) & 1) != (s[j][idx] -  0 );         ans = min(ans, cur + solve(idx + 1, i));       }     return ans;   }   int main() {     cin.tie(nullptr);     ios_base::sync_with_stdio(0);     cin >> n >> m;     if (n == 1) return cout <<  0 , 0;     if (n >= 4) return cout <<  -1 , 0;     for (int i = 0; i < n; i++) cin >> s[i];     int ans = 1e9;     memset(dp, -1, sizeof dp);     for (int i = 0; i < (1 << n); i++) ans = min(ans, solve(0, i));     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long N, M;   long long Arb[800002], Value[100002], Lazy[800002], Colour[800002];   void Build(long long K, long long L, long long R) {     long long Mid = (L + R) / 2;     if (L == R) {       Colour[K] = L;       return;     }     Build(2 * K, L, Mid);     Build(2 * K + 1, Mid + 1, R);   }   void Read() { cin >> N >> M; }   void Relax(long long K, long long L, long long R) {     if (Lazy[K] == 0) return;     Colour[2 * K] = Colour[2 * K + 1] = Colour[K];     Arb[2 * K] += Lazy[K] * ((L + R) / 2 - L + 1);     Arb[2 * K + 1] += Lazy[K] * (R - (L + R) / 2);     if (L != R) {       Lazy[2 * K] += Lazy[K];       Lazy[2 * K + 1] += Lazy[K];     }     Lazy[K] = 0;   }   void Update(long long K, long long L, long long R, long long Poz1,               long long Poz2, long long Val) {     long long Mid = (L + R) / 2;     Relax(K, L, R);     if (L > Poz2 || R < Poz1 || K > 4 * N) return;     if (Poz1 <= L && R <= Poz2) {       if (Colour[K] > 0) {         Arb[K] += abs(Colour[K] - Val) * (R - L + 1);         Lazy[K] += abs(Colour[K] - Val);         Colour[K] = Val;       } else {         Update(K * 2, L, Mid, Poz1, Poz2, Val);         Update(K * 2 + 1, Mid + 1, R, Poz1, Poz2, Val);         Arb[K] = Arb[2 * K] + Arb[2 * K + 1];         Colour[K] = Val;       }       return;     }     Update(2 * K + 1, Mid + 1, R, Poz1, Poz2, Val);     Update(2 * K, L, Mid, Poz1, Poz2, Val);     Arb[K] = Arb[2 * K] + Arb[2 * K + 1];     Colour[K] = Colour[K * 2] == Colour[K * 2 + 1] ? Colour[K * 2] : 0;   }   long long Qwery(long long K, long long L, long long R, long long Poz1,                   long long Poz2) {     Relax(K, L, R);     long long Mid = (L + R) / 2;     if (L > Poz2 || R < Poz1) return 0;     if (Poz1 <= L && R <= Poz2) return Arb[K];     return Qwery(2 * K, L, Mid, Poz1, Poz2) +            Qwery(2 * K + 1, Mid + 1, R, Poz1, Poz2);   }   void Browse() {     long long i;     for (i = 1; i <= M; i++) {       long long type, a, b, c;       cin >> type >> a >> b;       if (type == 1) {         cin >> c;         Update(1, 1, N, a, b, c);       }       if (type == 2) cout << Qwery(1, 1, N, a, b) <<   n ;     }   }   int main() {     Read();     Build(1, 1, N);     Browse();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 15;   const int inf = 1e9;   const int md = 1e9 + 7;   const int shift = 1 << 17;   int n, m;   vector<vector<int> > g;   set<pair<int, int> > br;   int timer;   int tin[maxn], fup[maxn];   int used[maxn];   void dfs(int v, int p = -1) {     used[v] = 1;     tin[v] = fup[v] = timer++;     for (int i = 0; i < g[v].size(); ++i) {       int to = g[v][i];       if (to == p) continue;       if (used[to])         fup[v] = min(fup[v], tin[to]);       else {         dfs(to, v);         fup[v] = min(fup[v], fup[to]);         if (fup[to] > tin[v]) {           br.insert(make_pair(to, v));           br.insert(make_pair(v, to));         }       }     }   }   int color[maxn];   int first[maxn];   int d[maxn];   vector<int> eu;   int z[shift * 2];   int c;   void dfs2(int v, int tekc, int len, int p = -1) {     used[v] = 1;     color[v] = tekc;     d[tekc] = len;     for (int i = 0; i < g[v].size(); ++i) {       int to = g[v][i];       if (to == p) continue;       if (used[to]) continue;       if (br.find(make_pair(v, to)) != br.end()) {         eu.push_back(c + 1);         first[c + 1] = eu.size() - 1;         ++c;         dfs2(to, c, len + 1, v);         eu.push_back(tekc);       } else         dfs2(to, tekc, len, v);     }   }   void build() {     for (int i = 0; i < eu.size(); ++i) z[i + shift] = eu[i];     for (int i = eu.size(); i < shift; ++i) z[i + shift] = n + 1;     for (int i = shift - 1; i > 0; --i)       if (d[z[i * 2]] < d[z[i * 2 + 1]])         z[i] = z[i * 2];       else         z[i] = z[i * 2 + 1];   }   int get(int v, int tl, int tr, int l, int r) {     if (l > r) return n + 1;     if (l == tl && r == tr) return z[v];     int tm = (tl + tr) / 2;     int ans1 = get(v * 2, tl, tm, l, min(r, tm));     int ans2 = get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);     return d[ans1] < d[ans2] ? ans1 : ans2;   }   int main() {     cin >> n >> m;     g.resize(n);     for (int i = 0; i < m; ++i) {       int v, u;       cin >> v >> u;       --v, --u;       g[v].push_back(u);       g[u].push_back(v);     }     dfs(0);     memset(used, 0, sizeof(used));     memset(first, -1, sizeof(first));     eu.push_back(0);     first[0] = 0;     dfs2(0, 0, 0);     d[n + 1] = inf;     build();     int k;     cin >> k;     for (int i = 0; i < k; ++i) {       int s, l;       cin >> s >> l;       --s, --l;       int L = first[color[s]];       int R = first[color[l]];       if (L > R) swap(L, R);       int lca = get(1, 0, shift - 1, L, R);       cout << d[color[s]] + d[color[l]] - 2 * d[lca] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   int q, n, a[N], pos[N], dp[N], pre[N];   long long ans = 0;   int main() {     scanf( %d , &q);     while (q--) {       ans = 0;       scanf( %d , &n);       for (int i = 1; i <= n; i++) pre[i] = -1;       for (int i = 0; i < n; i++) {         scanf( %d , &a[i]);         dp[i] = 0;         pos[i] = N;         if (pre[a[i]] == -1) {           pre[a[i]] = i;         }       }       dp[N - 1] = 0;       for (int i = 0; i < n; i++) {         if (a[i + 1] == a[i])           pos[i + 1] = i;         else {           if (a[i + 1] == a[pos[i] - 1]) {             pos[i + 1] = pos[i] - 1;             continue;           }           int tmp = i;           while (a[i + 1] != a[pos[tmp] - 1]) {             tmp = pos[tmp] - 1;             if (a[i + 1] == a[pos[tmp] - 1]) {               pos[i + 1] = pos[tmp] - 1;               break;             }             if (tmp == N - 1 || tmp < pre[a[i + 1]]) {               pos[i + 1] = N;               break;             }           }         }       }       for (int i = 0; i < n; i++) {         if (pos[i] != N) dp[i] = 1;       }       for (int i = 0; i < n; i++) {         dp[i] += dp[pos[i] - 1];         ans += dp[i];       }       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e17;   const int MAXM = 1e5 + 5;   const int MAXN = 1e6 + 4;   long long fact[MAXM];   long long nChoosek(long long n, long long k) {     if (k > n) return 0;     if (k * 2 > n) k = n - k;     if (k == 0) return 1;     long long result = n;     for (long long i = 2; i <= k; ++i) {       result *= (n - i + 1);       result /= i;     }     return result;   }   int main() {     long long n;     cin >> n;     long long Ans = 0;     for (long long i = 5; i <= 7; i++) {       Ans += nChoosek(n, i);     }     cout << Ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void smin(T &a, U b) {     if (a > b) a = b;   }   template <typename T, typename U>   inline void smax(T &a, U b) {     if (a < b) a = b;   }   int val[501000], deg[501000];   vector<int> adj[501000];   int main() {     int n, m, u, v;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);       deg[u]++, deg[v]++;     }     set<pair<int, int> > st;     for (int i = 1; i <= n; i++) {       if (adj[i].size() > 1) st.insert(pair<int, int>(adj[i].size(), i));     }     while (!st.empty()) {       pair<int, int> a = *st.begin();       if (a.first < 2) break;       st.erase(a);       val[a.second] ^= 1;       u = a.second;       deg[u] = 0;       for (int i = 0; i < adj[u].size(); i++) {         int v = adj[u][i];         if (val[v] == val[u]) {           if (deg[v] > 1) st.erase(pair<int, int>(deg[v], v));           deg[v]++;           if (deg[v] > 1) st.insert(pair<int, int>(deg[v], v));           deg[u]++;         } else {           if (deg[v] > 1) st.erase(pair<int, int>(deg[v], v));           deg[v]--;           if (deg[v] > 1) st.insert(pair<int, int>(deg[v], v));         }       }     }     for (int i = 1; i <= n; i++) printf( %d , val[i]);     return 0;   }


#include <bits/stdc++.h>       using namespace std;               #define endl   n        #define ll long long       #define INF (ll)1e18               ll i, i1, j, k, k1, t, n, m, res, check1, a[200010], a1, b, d, v, dist[200010], dp[200010], s, s1, s2;       vector<ll> adj[200010];       vector<array<ll, 3>> dadj[200010];       bool visited[200010];               void dfs(ll s) {           for (auto u : adj[s]) {               if (!visited[u]) {                   visited[u] = true;                   dist[u] = dist[s] + 1;                   dadj[dist[u]].push_back({-1, s, u});                   d = max(d, dist[u]);                   dfs(u);               }           }       }               int main() {           ios::sync_with_stdio(0);           cin.tie(0);                   //ifstream cin( input.txt );           //ofstream cout( output.txt );                   cin >> t;           while (t--) {               cin >> n;               for (i = 0; i <= n; i++) {                   adj[i].clear(); dadj[i].clear(); visited[i] = false; dist[i] = 0; dp[i] = 0;               }               for (i = 2; i <= n; i++) {                   cin >> v;                   adj[v - 1].push_back(i - 1);                   adj[i - 1].push_back(v - 1);               }                       for (i = 1; i < n; i++) {                   cin >> a[i];               }                       d = 0;               visited[0] = true;               dfs(0);               dadj[0].push_back({0, -1, 0});                       for (i = 1; i <= d; i++) {                   for (auto& u : dadj[i]) {                       u[0] = a[u[2]];                       // cout << u[0] <<     << u[1] <<     << u[2] << endl;                   }                   // cout << endl;                   sort(dadj[i].begin(), dadj[i].end());                   s = dadj[i].size();                   s1 = dadj[i][0][0];                   s2 = dadj[i][dadj[i].size() - 1][0];                   // cout << s1 <<     << s2 << endl;                   for (auto& u : dadj[i]) {                       a1 = a[u[2]];                       dp[u[2]] = max({dp[u[2]], dp[u[1]] + a1 - s1, dp[u[1]] + s2 - a1});                   }                   k = 0;                   for (j = 0; j < s; j++) {                       if (dp[dadj[i][j][1]] - dadj[i][j][0] > dp[dadj[i][k][1]] - dadj[i][k][0]) {                           k = j;                       }                       dp[dadj[i][j][2]] = max(dp[dadj[i][j][2]], dadj[i][j][0] - dadj[i][k][0] + dp[dadj[i][k][1]]);                   }                   k = s - 1;                   for (j = s - 1; j >= 0; j--) {                       if (dp[dadj[i][j][1]] + dadj[i][j][0] > dp[dadj[i][k][1]] + dadj[i][k][0]) {                           k = j;                       }                       dp[dadj[i][j][2]] = max(dp[dadj[i][j][2]], dadj[i][k][0] - dadj[i][j][0] + dp[dadj[i][k][1]]);                   }               }                       res = 0;               for (i = 1; i < n; i++) {                   // cout << dp[i] <<    ;                   res = max(res, dp[i]);               }               // cout << endl;                       cout << res << endl;           }                   return 0;       }


#include <bits/stdc++.h>   using namespace std;   int n;   map<int, int> mpp;   map<int, int> tmp;   multiset<int> adj[200005];   multiset<pair<int, int> > pairs;   int cnt[200005];   int arra[100005];   int arrb[100005];   list<int> ans;   void addpair(int u, int v) {     pairs.insert(make_pair(u, v));     pairs.insert(make_pair(v, u));   }   void removepair(int u, int v) {     pairs.erase(pairs.find(make_pair(u, v)));     pairs.erase(pairs.find(make_pair(v, u)));   }   int ispair(int u, int v) {     if (pairs.find(make_pair(u, v)) == pairs.end()) return 0;     return 1;   }   void bit() {     printf( -1 n );     exit(0);   }   int main() {     clock_t start = clock();     scanf( %d , &n);     for (int i = 0; i < 2 * n; i++) cnt[i] = 0;     for (int i = 0; i < n - 1; i++) {       scanf( %d , arra + i);     }     for (int i = 0; i < n - 1; i++) {       scanf( %d , arrb + i);     }     for (int i = 0; i < n - 1; i++) {       int a = arra[i], b = arrb[i];       int ai = i * 2, bi = i * 2 + 1;       if (a > b) bit();       if (tmp.count(a))         ai = tmp[a];       else {         tmp[a] = ai;         mpp[ai] = a;       }       if (tmp.count(b))         bi = tmp[b];       else {         tmp[b] = bi;         mpp[bi] = b;       }       cnt[ai]++;       cnt[bi]++;       addpair(ai, bi);       adj[ai].insert(bi);       adj[bi].insert(ai);     }     int tek1 = -1;     int tek2 = -1;     for (int i = 0; i < 2 * n; i++) {       if (cnt[i] % 2 == 1) {         if (tek2 != -1)           bit();         else if (tek1 != -1)           tek2 = i;         else           tek1 = i;       }     }     if (tek1 != -1 && tek2 == -1) assert(0);     list<int> ans;     if (tek1 == -1) {       tek1 = 0;     }     stack<int> st;     st.push(tek1);     while (!st.empty()) {       int node = st.top();       if (adj[node].size() == 0) {         ans.push_back(mpp[node]);         st.pop();       } else {         st.push(*adj[node].begin());         adj[*adj[node].begin()].erase(adj[*adj[node].begin()].find(node));         adj[node].erase(adj[node].begin());       }     }     for (int i = 0; i < 2 * n; i++) {       if (adj[i].size() != 0) bit();     }     for (list<int>::iterator it = ans.begin(); it != ans.end(); ++it) {       printf( %d  , *it);     }   }


#include <bits/stdc++.h>   using namespace std;   int n, ans = 1, p, m;   string s;   set<int> buy, sell, extra;   int main() {     cin >> n;     while (n--) {       cin >> s >> p;       if (s ==  ACCEPT ) {         if (extra.find(p) != extra.end()) {           for (auto it : extra) {             if (it < p)               buy.insert(it);             else if (it > p)               sell.insert(it);           }           ans = (ans + ans) % 1000000007;           extra.clear();         } else if (buy.find(p) != buy.end()) {           auto it = buy.end();           it--;           if (*it != p) ans = 0;           it = buy.find(p);           buy.erase(it);           for (auto it : extra) {             sell.insert(it);           }           extra.clear();         } else {           auto it = sell.find(p);           assert(it != sell.end());           if (it != sell.begin()) ans = 0;           sell.erase(it);           for (auto it : extra) {             buy.insert(it);           }           extra.clear();         }       } else {         if (buy.empty()) {           if (sell.empty()) {             extra.insert(p);           } else {             auto it = sell.begin();             if (p > *it)               sell.insert(p);             else               extra.insert(p);           }         } else {           auto it = buy.end();           it--;           if (sell.empty()) {             if (p < *it)               buy.insert(p);             else               extra.insert(p);           } else {             auto jt = sell.begin();             if (p > *it && p < *jt)               extra.insert(p);             else if (p < *it)               buy.insert(p);             else               sell.insert(p);           }         }       }     }     long long int res = ((ans * 1LL) * (extra.size() + 1)) % 1000000007;     return cout << res <<   n , false;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   template <class T, class U>   inline void Max(T &a, U b) {     if (a < b) a = b;   }   template <class T, class U>   inline void Min(T &a, U b) {     if (a > b) a = b;   }   inline void add(int &a, int b) {     a += b;     while (a >= 1000000007) a -= 1000000007;   }   int pow(int a, int b) {     int ans = 1;     while (b) {       if (b & 1) ans = ans * (long long)a % 1000000007;       a = (long long)a * a % 1000000007;       b >>= 1;     }     return ans;   }   pair<int, pair<int, int> > b[200010];   int v[200010], f[200010], ans[200010];   int query(int first) {     int ans = 0;     while (first < 200010) ans += f[first], first += first & -first;     return ans;   }   void upd(int first) {     while (first > 0) f[first]++, first -= first & -first;   }   int main() {     int i, j, k, n, m = 0;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d%d , &j, &k);       v[m++] = j;       b[i].first = k;       b[i].second = make_pair(j, i);     }     sort(v, v + m);     m = unique(v, v + m) - v;     sort(b, b + n);     for (i = 0; i < n; i++) {       k = lower_bound(v, v + m, b[i].second.first) - v + 1;       ans[b[i].second.second] = query(k);       upd(k);     }     for (i = 0; i < n; i++) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int num[105];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       scanf( %1d , &num[i]);     }     int sum = 0;     int flag = 0;     for (int i = 1; i <= n; i++) {       sum += num[i];       int t = 0;       flag = 0;       for (int j = i + 1; j <= n; j++) {         if (num[j] == 0 && t == 0 && flag == 1) {           if (j == n) {             puts( YES );             return 0;           }           continue;         }         t += num[j];         if (t > sum) {           break;         } else if (t == sum) {           t = 0;           flag = 1;           if (j == n) {             puts( YES );             return 0;           }         }       }     }     puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct point {     int x, y, l, r;   };   bool f(point a, point b) { return a.r > b.r; }   point a[5000];   int par[5000];   void init(int n) {     for (int i = 1; i <= n; i++) par[i] = i;   }   int root(int n) {     if (par[n] == n) return n;     return par[n] = root(par[n]);   }   int main() {     int n, m, maxx = 0;     cin >> n >> m;     for (int i = 1; i <= m; i++) cin >> a[i].x >> a[i].y >> a[i].l >> a[i].r;     sort(a + 1, a + m + 1, f);     for (int i = 1; i <= m; i++) {       init(n);       for (int j = 1; j <= m; j++) {         if (a[j].l > a[i].l) continue;         int q1 = root(a[j].x), q2 = root(a[j].y);         if (q1 != q2) par[q1] = q2;         if (root(1) == root(n)) maxx = max(maxx, a[j].r - a[i].l + 1);       }     }     if (!maxx)       cout <<  Nice work, Dima!  << endl;     else       cout << maxx << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int na, ma, nb, mb;     cin >> na >> ma;     char ch1[na + 5][ma + 5];     for (int i = 1; i <= na; i++) {       for (int j = 1; j <= ma; j++) {         cin >> ch1[i][j];       }     }     cin >> nb >> mb;     char ch2[nb + 5][mb + 5];     for (int i = 1; i <= nb; i++) {       for (int j = 1; j <= mb; j++) {         cin >> ch2[i][j];       }     }     int x = -1, y = -1, mx = -1;     for (int xx = -50; xx <= 50; xx++) {       for (int yy = -50; yy <= 50; yy++) {         int sum = 0;         for (int i = 1; i <= na; i++) {           for (int j = 1; j <= ma; j++) {             if (((i + xx) >= 1 && (i + xx) <= nb) &&                 ((j + yy) >= 1 && (j + yy) <= mb)) {               sum += (ch1[i][j] -  0 ) * (ch2[i + xx][j + yy] -  0 );             }           }         }         if (sum > mx) {           mx = sum;           x = xx;           y = yy;         }       }     }     cout << x <<     << y <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, sum, s;     cin >> a >> b >> s;     sum = abs(a) + abs(b);     if (sum <= s && (s - sum) % 2 == 0) {       cout <<  YES  << endl;     } else {       cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const int MOD = 1e9 + 7;   const int MAXN = (1 << 9) + 3;   int k;   int a[MAXN][MAXN];   void dien(int x) {     if (x == 1) return;     dien(x / 2);     for (int i = 1; i <= x / 2; i++) {       for (int j = 1; j <= x / 2; j++) {         a[i + x / 2][j] = a[i][j + x / 2] = a[i][j];         a[i + x / 2][j + x / 2] = 1 - a[i][j];       }     }   }   int main() {     cin >> k;     dien(1 << k);     for (int i = 1; i <= 1 << k; i++) {       for (int j = 1; j <= 1 << k; j++) {         if (a[i][j] == 0)           cout <<  * ;         else           cout <<  + ;       }       cout << endl;     }     return 0;   }


//To debug :  g++ -g file.cpp -o code   //to flush output : fflush(stdout) or cout.flush()   //cout<<setprecision(p)<<fixed<<var   //use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll   //take care of precedence rule of operators    //do not forget to change the sizes of arrays and value of contants and other things after debugging         #include<bits/stdc++.h>   using namespace std;   #define ll long long   #define rep(i,a,n) for(i=a;i<n;++i)   #define irep(i,n,a) for(i=n;i>a;--i)   #define mod 1000000007   #define pb push_back   #define big 9223372036854775807   #define big1 LONG_MAX   #define big2 ll_MAX   #define big3 1000000000   #define sma1 LONG_MIN   #define sma2 ll_MIN   #define sma3 -1000000000   #define mp make_pair   #define dub double   #define ivec vector<ll>   #define lvec vector<long long>   #define cvec vector<char>   #define svec vector<string>   #define mt make_tuple   #define MOD 998244353   #define ld long double   #define pi acos(-1.0)       #define SZ(x)  (ll)(x.size())       //comment the below if not required       /*       #define ss second.second   #define ff first.first   #define f first   #define s second   #define sf second.first   #define fs first.second   */       #define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)       //cout<< Case # <<c<< :  <<ans<<  n  ;      const ll N = 150001;      ll a[5][N],n[5],x,y,temp,val,i,j,ans=LLONG_MAX;      set<pair<ll,ll>> m;      vector<ll> v[5][N];      int main()   {    IOS;       rep(i,1,5)     cin>>n[i];       rep(i,1,5)     rep(j,1,n[i]+1)      cin>>a[i][j];       rep(i,1,4)    {     cin>>val;        rep(j,1,val+1)     {      cin>>x>>y;      v[i+1][y].pb(x);     }       }       rep(i,2,5)    {     m.clear();        rep(j,1,n[i-1]+1)     {      if(a[i-1][j]!=-1)       m.insert({a[i-1][j],j});     }        rep(j,1,n[i]+1)     {      for(auto u : v[i][j])      {       if(a[i-1][u]!=-1)       {        auto it = m.find({a[i-1][u],u});        m.erase(it);       }      }         if(SZ(m))      {       auto it = m.begin();       a[i][j]+=(*it).first;      }      else       a[i][j]=-1;         for(auto u : v[i][j])      {       if(a[i-1][u]!=-1)        m.insert({a[i-1][u],u});      }        }       }          rep(i,1,n[4]+1){        if(a[4][i]==-1)      continue;        ans = min(ans,a[4][i]);    }       if(ans==LLONG_MAX)     ans = -1;       cout<<ans<<  n ;          return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {     return (a.first > b.first);   }   struct greaterr {     template <class T>     bool operator()(T const &a, T const &b) const {       return a > b;     }   };   int main() {     int n;     scanf( %d , &n);     char ans[n];     int x, y;     long long int suma = 0, sumb = 0;     for (int i = 0; i < n; i++) {       scanf( %d %d , &y, &x);       if (suma + y - sumb <= 500) {         suma += y;         printf( A );       } else {         sumb += x;         printf( G );       }     }     return 0;   }


#include <bits/stdc++.h>   long double pi = acos(-1);   long long z = 1000000007;   long long power(long long a, long long b) {     if (b == 0) return 1;     long long c = power(a, b / 2);     if (b % 2 == 0)       return c * c;     else       return c * c * a;   }   using namespace std;   const int N = 1e6 + 7;   void solve() {     long long n;     cin >> n;     long long k;     cin >> k;     map<long long, long long> m;     for (long long i = 0; i < n; i++) {       long long x;       cin >> x;       m[x]++;     }     if (m.size() <= k)       cout << 1 << endl;     else if (k == 1 && m.size() > 1)       cout << -1 << endl;     else {       long long ans = (m.size() - k) / (k - 1) + 1;       if ((m.size() - k) % (k - 1) != 0) ans++;       cout << ans << endl;     }   }   int main() {     long long t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int get(long long a) {     if (a < 4) return 0;     if (a < 16) return 1;     if (a < 82) return 2;     if (a < 6724) return 0;     if (a < 50626) return 3;     if (a < 2562991876) return 1;     return 2;   }   int main() {     int cur = 0;     int n;     cin >> n;     for (int i = 0; i < n; i++) {       long long t;       cin >> t;       cur ^= get(t);     }     if ((cur) != 0)       cout <<  Furlo ;     else       cout <<  Rublo ;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, l;   int pre[200010], other[200010], last[100010];   int flag[100010];   void connect(int x, int y) {     pre[++l] = last[x];     last[x] = l;     other[l] = y;   }   int dfs(int x, int fa) {     flag[x] = 1;     int cur(0);     for (int p = last[x]; p; p = pre[p]) {       if (other[p] == fa) continue;       if (flag[other[p]] == 2) continue;       int edge;       if (!flag[other[p]])         edge = dfs(other[p], x);       else         edge = 0;       if (edge)         printf( %d %d %d n , x, other[p], edge);       else if (cur)         printf( %d %d %d n , cur, x, other[p]), cur = 0;       else         cur = other[p];     }     flag[x] = 2;     return cur;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       connect(x, y);       connect(y, x);     }     if (m & 1)       printf( No solution n n );     else       dfs(1, -1);     return 0;   }


#include <bits/stdc++.h>   const int MAX_N = (int)1e5 + 123;   const double eps = 1e-6;   const int inf = (int)1e9 + 123;   using namespace std;   int n;   vector<int> ans[MAX_N];   int k;   int main() {     cin >> n;     ans[1].push_back(1), ans[2].push_back(1);     for (int i = 3, now = 2;; i++) {       if (n - now + 1 >= i - 1) {         k = i;         for (int j = 1; j < i; j++) {           ans[j].push_back(now);           ans[i].push_back(now);           now++;         }       } else         break;     }     cout << k << endl;     for (int i = 1; i <= k; i++) {       for (auto j : ans[i]) cout << j <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[1001][1001], ans[1001];   int main() {     ios_base::sync_with_stdio(NULL);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     int x = (n + 1) / 2 + 1 * ((n + 1) & 1);     vector<pair<int, int> > v;     for (int j = 1; j <= x; j++) {       if (v.size() == n) break;       v.push_back(make_pair(1, j));     }     for (int i = 2; i <= x; i++) {       if (v.size() == n) break;       v.push_back(make_pair(i, x));     }     cout << x <<   n ;     for (pair<int, int> x : v) {       cout << x.first <<     << x.second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long int, long long int> > adj[100005];   long long int sp = 0, mst = 0, p = 100003;   int n, m;   int main() {     cin >> n >> m;     long long int c = p - (n - 2);     adj[1].push_back({2, c});     m--;     for (int i = 2; i <= n - 2 + 1; i++, m--) adj[i].push_back({i + 1, 1});     if (m != 0) {       for (int i = 1; i < n && m != 0; i++) {         for (int j = i + 2; j <= n; j++) {           adj[i].push_back({j, 1e7});           m--;           if (m == 0) break;         }       }     }     cout << p <<     << p << endl;     for (int i = 1; i < n; i++) {       for (int j = 0; j < adj[i].size(); j++)         cout << i <<     << adj[i][j].first <<     << adj[i][j].second << endl;     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;      int main()   {      int t,n; cin >> t;            while(t--)      {          cin >> n; int ans=0,k=n,p=n,h=0;                              while(ceil(sqrt(k))>=2 && k>2)          {              ans++; k=ceil(sqrt(k));          }                    cout << ans+n-2 << endl; k=n; int i=k-1;          while(k>2)          {                 int p=ceil(sqrt(k));              while(i>p)              {                 cout << i <<    << k << endl; i--;               }                      cout<< k <<     << i << endl; cout << k <<     << i << endl;               k=ceil(sqrt(k)); i=k-1;          }      }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n;     long long int i;     cin >> n;     std::vector<long long int> v;     long long int s = 0;     for (long long int i = 0; i < n; i++) {       long long int val;       cin >> val;       s += val;       if (i != (n - 1)) v.push_back(s);     }     if (s % 2 == 1) {       cout << 0 << endl;       return 0;     }     long long int c = 0;     for (i = 0; i < v.size(); i++) {       if (v[i] == s - v[i]) c++;     }     cout << c << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 35, M = 100005;   int n, m, f, ans, e;   char a[N][M];   int len[N], z[M], b[N][M], p[N][M];   vector<int> v[M];   void dfs(int pos) {     if (f) return;     z[pos] = 2;     for (auto &i : v[pos])       if (!z[i])         dfs(i);       else if (z[i] == 2) {         f = 1;         return;       }     z[pos] = 1;   }   bool check(int s, int t) {     f = e = 0;     z[0] = 0;     v[0].clear();     for (int i = b[s][1]; i <= b[t][len[t]]; i++) v[i].clear(), z[i] = 0;     for (int i = s; i <= t; i++) {       v[0].push_back(b[i][1]);       for (int j = s; j <= t; j++) {         int l = 0;         for (int k = 1; k <= len[i]; k++) {           while (k + l <= len[i] && l + 1 <= len[j] && a[i][k + l] == a[j][l + 1])             ++l;           if (l) {             if (len[i] - k + 1 < len[j] && l == len[i] - k + 1)               v[b[i][k]].push_back(b[j][l + 1]);             if (len[j] < len[i] - k + 1 && l == len[j])               v[b[i][k]].push_back(b[i][k + l]);             if (k > 1 && len[i] - k + 1 == len[j] && l == len[j])               v[b[i][k]].push_back(0);             l = p[j][l];           }         }       }     }     if (!z[0]) dfs(0);     if (f) return 0;     for (int i = b[s][1]; i <= b[t][len[t]]; i++)       if (!z[i]) {         dfs(i);         if (f) return 0;       }     return 1;   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> (a[i] + 1);       len[i] = strlen(a[i] + 1);       for (int j = 1; j <= len[i]; j++) b[i][j] = ++m;     }     for (int i = 1; i <= n; i++) {       int k = 0;       for (int j = 2; j <= len[i]; j++) {         while (k && a[i][k + 1] != a[i][j]) k = p[i][k];         if (a[i][k + 1] == a[i][j]) ++k;         p[i][j] = k;       }     }     int j = 1;     for (int i = 1; i <= n; i++) {       if (i > j) j = i;       while (j <= n && check(i, j)) ++j;       ans += j - i;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T1, class T2>   ostream& operator<<(ostream& out, pair<T1, T2> p) {     out << p.first <<     << p.second;     return out;   }   template <class T1, class T2>   istream& operator>>(istream& in, pair<T1, T2>& p) {     in >> p.first >> p.second;     return in;   }   template <class T>   istream& operator>>(istream& in, vector<T>& v) {     for (auto& x : v) in >> x;     return in;   }   template <class T>   ostream& operator<<(ostream& out, vector<vector<T>>& v) {     for (auto& x : v) out << x <<   n ;     return out;   }   template <class T>   ostream& operator<<(ostream& out, vector<T>& v) {     for (auto x : v) out << x <<    ;     return out;   }   void solve() {     int n;     cin >> n;     vector<int> a(n), b(n);     cin >> a;     iota(b.begin(), b.end(), 0);     sort(b.begin(), b.end(), [&](int i, int j) {       if (a[i] != a[j]) {         return a[i] < a[j];       }       return i < j;     });     vector<pair<int, int>> ans;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n - 1; j++) {         if (b[j] > b[j + 1]) {           ans.push_back({b[j + 1], b[j]});           swap(b[j], b[j + 1]);         }       }     }     cout << ans.size() <<   n ;     for (auto p : ans) {       cout << p.first + 1 <<     << p.second + 1 <<   n ;     }   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cerr.tie(0);     ;     int t;     t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   FILE *in = stdin, *out = stdout;   using namespace std;   int n, ans[100005], N;   int alpha[30];   char a[2005], b[2005], c[2005];   void input() {     fscanf(in,  %d , &n);     fscanf(in,  %s , &a[1]);     fscanf(in,  %s , &b[1]);   }   int most_fit(int start) {     for (int i = (start); i <= (n); i++)       if (a[i] == b[start]) return i;     return 0;   }   void method1(int l, int r) {     ans[++N] = r;     ans[++N] = l;     ans[++N] = r;     for (int i = (1); i <= (r); i++) c[i] = a[l + i];     int t = l;     for (int i = (r + 1); i <= (n); i++) c[i] = a[t--];     for (int i = (1); i <= (n); i++) a[i] = c[i];   }   void method2(int A, int B, int C) {     ans[++N] = C;     ans[++N] = A + B;     ans[++N] = C;     ans[++N] = A;     for (int i = (A + 1); i <= (A + C); i++) c[i] = a[i + B];     int t = A + B;     for (int i = (A + C + 1); i <= (n); i++) c[i] = a[t--];     for (int i = (A + 1); i <= (n); i++) a[i] = c[i];   }   void pro() {     for (int i = (1); i <= (n); i++) alpha[a[i] -  a ]++, alpha[b[i] -  a ]--;     for (int i = (0); i <= (25); i++)       if (alpha[i] != 0) {         fprintf(out,  -1 );         return;       }     for (int i = (1); i <= (n); i++) {       for (int j = (i); j <= (n); j++) {         if (a[j] == b[n - i + 1]) {           ans[++N] = n;           ans[++N] = j - 1;           ans[++N] = 1;           c[1] = a[j];           for (int k = (2); k <= (j); k++) c[k] = a[k - 1];           int t = n;           for (int k = (j + 1); k <= (n); k++) c[k] = a[t--];           for (int k = (1); k <= (n); k++) a[k] = c[k];           break;         }       }     }     fprintf(out,  %d n , N);     for (int i = (1); i <= (N); i++) {       fprintf(out,  %d  , ans[i]);     }   }   int main() {     input();     pro();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const long long INF = 1e18;   int n;   int p[N];   long long dif[N];   long long ans = INF;   int id;   void add(int l, int r, int val) {     dif[l] += val;     dif[r + 1] -= val;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     long long res = 0;     for (int i = 1; i <= n; i++) {       cin >> p[i];       res += (1LL) * abs(p[i] - i);       if (p[i] >= i) {         add(2, i, 1);         add(i + 1, i + 1, (n - p[i]) - (p[i] - 1));         add(i + 2, i + 2 + (n - p[i]) - 1, -1);         add(i + 2 + (n - p[i]), n, 1);       } else {         add(2, i - p[i] + 1, -1);         add(i - p[i] + 2, i, 1);         add(i + 1, i + 1, (n - p[i]) - (p[i] - 1));         add(i + 2, n, -1);       }     }     for (int i = 1; i <= n; i++) {       dif[i] += dif[i - 1];       res += dif[i];       if (res < ans) {         ans = res, id = n - i + 1;         if (id == n) id = 0;       }     }     cout << ans <<     << id <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   queue<int> q1, q2, q3, q4, q5, q6;   int main() {     int n, x, ans = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       if (x == 4)         q1.push(i);       else if (x == 8)         q2.push(i);       else if (x == 15)         q3.push(i);       else if (x == 16)         q4.push(i);       else if (x == 23)         q5.push(i);       else         q6.push(i);     }     if (q1.empty() || q2.empty() || q3.empty() || q4.empty() || q5.empty() ||         q6.empty()) {       printf( %d n , n - ans);       return 0;     }     while (1) {       if (q2.front() > q1.front()) {         if (q3.front() > q2.front()) {           if (q4.front() > q3.front()) {             if (q5.front() > q4.front()) {               if (q6.front() > q5.front()) {                 ans += 6;                 q1.pop();                 q2.pop();                 q3.pop();                 q4.pop();                 q5.pop();                 q6.pop();               } else                 q6.pop();             } else               q5.pop();           } else             q4.pop();         } else           q3.pop();       } else         q2.pop();       if (q1.empty() || q2.empty() || q3.empty() || q4.empty() || q5.empty() ||           q6.empty())         break;     }     printf( %d n , n - ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct point {     int x, y;   };   point moves[2020];   queue<int> Q;   const int lin[] = {1, -1, 0, 0};   const int col[] = {0, 0, 1, -1};   int A[2020][2020];   int X[3 * 2020], Y[3 * 2020];   int n;   void new_point(int x, int y) {     X[++X[0]] = x;     Y[++Y[0]] = y;   }   void normalizare(int X[]) {     sort(X + 1, X + X[0] + 1);     int c = 1;     for (int i = 2; i <= X[0]; i++) {       if (X[c] != X[i]) {         X[++c] = X[i];       }     }     X[0] = c;   }   void precomp() {     normalizare(X);     normalizare(Y);   }   void find(int x, int X[], int &poz) {     int step = 0;     for (; (1 << step) <= X[0]; step++)       ;     step--;     poz = 0;     for (int i = step; i >= 0; i--) {       if (poz + (1 << i) <= X[0] && X[poz + (1 << i)] < x) {         poz += (1 << i);       }     }     poz++;   }   void vecini(int x, int y) {     for (int t = 0; t < 4; t++) {       int xx = x + lin[t], yy = y + col[t];       if (xx >= 0 && xx <= X[0] && yy >= 0 && yy <= Y[0] && A[xx][yy] == 0) {         A[xx][yy] = 2;         Q.push(xx + (yy << 13));       }     }   }   void bf() {     Q.push(0);     A[0][0] = 2;     while (!Q.empty()) {       int x = Q.front() & ((1 << 13) - 1), y = Q.front() >> 13;       Q.pop();       vecini(x, y);     }   }   void solve() {     precomp();     int x1, y1;     find(moves[0].x, X, x1);     find(moves[0].y, Y, y1);     for (int i = 1; i <= n; i++) {       int x2, y2, chg;       if (moves[i].x == moves[i - 1].x) {         x2 = x1;         chg = 2 * (moves[i].y > moves[i - 1].y) - 1;         find(moves[i].y, Y, y2);         for (int j = y1; j != y2; j += chg) {           A[x1][j] = 1;         }         A[x1][y2] = 1;       } else {         y2 = y1;         chg = 2 * (moves[i].x > moves[i - 1].x) - 1;         find(moves[i].x, X, x2);         for (int j = x1; j != x2; j += chg) {           A[j][y1] = 1;         }         A[x2][y1] = 1;       }       x1 = x2;       y1 = y2;     }     bf();     long long sol = 0;     for (int i = 1; i < X[0]; i++) {       for (int j = 1; j < Y[0]; j++) {         if (A[i][j] <= 1) {           sol += (long long)(X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);         }       }     }     cout << sol;   }   int main() {     scanf( %d n , &n);     int x = 0, y = 0;     X[++X[0]] = x;     Y[++Y[0]] = y;     new_point(1, 1);     for (int i = 1; i <= n; i++) {       char c;       int val;       scanf( %c %d n , &c, &val);       if (c ==  R ) {         x += val;       } else if (c ==  L ) {         x -= val;       } else if (c ==  D ) {         y += val;       } else {         y -= val;       }       moves[i].x = x;       moves[i].y = y;       new_point(x, y);       new_point(x + 1, y + 1);     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     n /= 1000;     if (n & 1)       cout << 2 << endl;     else       cout << 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     long long n, m;     cin >> n >> m;     vector<long long> v, h;     for (long long idx = 0; idx < n; ++idx) {       long long x;       cin >> x;       v.push_back(x);     }     for (long long idx = 0; idx < m; ++idx) {       long long x1, x2, y;       cin >> x1 >> x2 >> y;       if (x1 > 1) continue;       h.push_back(x2);     }     sort(v.begin(), v.end());     sort(h.begin(), h.end());     long long res = n + m;     for (long long idx = 0; idx <= v.size(); ++idx) {       long long lim = idx == v.size() ? 1000000000 : v[idx];       auto it = lower_bound(h.begin(), h.end(), lim);       res = min(res, distance(it, h.end()) + idx);     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     string s;     cin >> s;     long long mini = n - 1, maxi = -1;     for (long long i = 0; i < n; i++) {       if (s[i] ==  1 ) {         mini = i;         break;       }     }     for (long long i = n - 1; i >= 0; i--) {       if (s[i] ==  1 ) {         maxi = i;         break;       }     }     if (maxi == -1) {       cout << n << endl;       return;     }     cout << 2 * max(maxi + 1, n - mini) << endl;   }   int main() {     long long t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long x1, y1, x2, y2, x, y;     cin >> x1 >> y1 >> x2 >> y2;     cin >> x >> y;     x2 = x2 - x1;     y2 = y2 - y1;     if (x2 % x != 0 || y2 % y != 0)       cout <<  NO  << endl;     else {       if (((x2 / x) & 1) == ((y2 / y) & 1))         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1024000000,1024000000 )   using namespace std;   const int P = 1000000007;   int n, m;   const int V = 5010;   int main() {     while (~scanf( %d%d , &n, &m)) {       int mx = 0;       for (int i = 1; i <= n; ++i) mx += (i - 1) / 2;       if (mx < m) {         puts( -1 );         continue;       }       int pr = 0, pre = 0;       for (int i = 0; i <= n; ++i) {         if (pre + (i - 1) / 2 > m) break;         pre += (i - 1) / 2;         pr = i;       }       int need = m - pre;       for (int i = 1; i <= pr; ++i) printf( %d  , i);       int remain = n - pr;       if (need != 0) {         int np = -1;         for (int i = pr + 1;; ++i) {           int tc = 0;           for (int j = 1; j <= pr; ++j) {             if (i - j > j && i - j <= pr) tc++;           }           if (tc == need) {             np = i;             break;           }         }         --remain;         printf( %d  , np);       }       for (int i = remain; i > 0; --i) printf( %d  , 1000000000 - 30000 * i);       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 4e6 + 10;   const int p = 1009;   int raw[maxn], cnt[maxn];   complex<double> F1[maxn], F2[maxn];   const double PI = acos(-1.0);   int rev[maxn];   void init(vector<complex<double> >& A, int n) {     A.clear();     A.shrink_to_fit();     int size = 1;     while (size <= n) size *= 2;     A.assign(size * 2, 0);   }   void FFT_init(int n) {     int S = log2(n);     for (int i = 0; i < n; i++)       rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (S - 1));   }   void FFT(complex<double>* A, int siz, int type) {     int n = siz;     FFT_init(n);     for (int i = 0; i < n; i++)       if (i < rev[i]) swap(A[i], A[rev[i]]);     for (int i = 1; i < n; i *= 2) {       complex<double> Wn(cos(PI / i), type * sin(PI / i));       for (int j = 0; j < n; j += i * 2) {         complex<double> W(1, 0);         for (int k = 0; k < i; k++) {           complex<double> facx = A[j + k], facy = W * A[j + k + i];           A[j + k] = facx + facy;           A[j + k + i] = facx - facy;           W *= Wn;         }       }     }     if (type == -1)       for (int i = 0; i < n; i++) A[i].real((A[i].real() / n + 0.5));   }   vector<complex<double> > mul(vector<complex<double> > A,                                vector<complex<double> > B) {     int n = A.size(), m = B.size();     int siz = n + m - 1;     vector<complex<double> > C(siz);     int fsiz = 1;     while (fsiz <= siz) fsiz *= 2;     fsiz *= 2;     for (int i = 0; i < fsiz; i++) F1[i] = F2[i] = 0;     for (int i = 0; i < n; i++) F1[i] = A[i];     for (int i = 0; i < m; i++) F2[i] = B[i];     FFT(F1, fsiz, 1);     FFT(F2, fsiz, 1);     for (int i = 0; i < fsiz; i++) F1[i] *= F2[i];     FFT(F1, fsiz, -1);     for (int i = 0; i < siz; i++) {       C[i] = ((long long)F1[i].real()) % p;     }     return C;   }   vector<complex<double> > solve(int l, int r) {     if (l == r) {       return vector<complex<double> >(cnt[l] + 1, 1);     }     int m = (l + r) / 2;     return mul(solve(l, m), solve(m + 1, r));   }   struct Node {     int w;     vector<complex<double> > P;     Node(int w = 0, vector<complex<double> > P = {}) : w(w), P(P) {}     bool operator<(const Node& X) const { return w > X.w; }   };   priority_queue<Node> pq;   vector<complex<double> > ans;   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m, k;     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       cnt[x]++;     }     vector<complex<double> > ans = solve(1, m);     cout << ((int)ans[k].real() + p) % p <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:102400000,102400000 )   using namespace std;   const double eps = 1e-8;   const int INF = 1e9 + 7;   const long long inf = (1LL << 62);   const int MOD = 1e9 + 7;   const int M = 100010;   const int maxn = 1001;   const int lowbit(int x) { return x & -x; }   int read() {     int v = 0, f = 1;     char c = getchar();     while (c < 48 || 57 < c) {       if (c ==  - ) f = -1;       c = getchar();     }     while (48 <= c && c <= 57) v = v * 10 + c - 48, c = getchar();     return v * f;   }   struct timer {     double t;     timer() { t = clock(); }     ~timer() { printf(  nruntime %.3f s n , get_time()); }     double get_time() { return (clock() - t) / CLOCKS_PER_SEC; }   };   int N;   int A[1100010], B[1100010];   int inv2 = 500000004;   void fwt(int *a) {     int i, j, k, l, t, x, y;     for (i = 1; i < N; i <<= 1) {       for (j = 0, l = N / (i << 1); j < N; j += i << 1) {         for (k = 0, l = 0; k < i; ++k, l += t) {           x = a[j + k];           y = a[j + k + i];           a[j + k] = (x + y) % MOD;           a[j + k + i] = (x - y + MOD) % MOD;         }       }     }   }   void wfwt(int *a) {     int i, j, k, l, t, x, y;     for (i = 1; i < N; i <<= 1) {       for (j = 0, l = N / (i << 1); j < N; j += i << 1) {         for (k = 0, l = 0; k < i; ++k, l += t) {           x = a[j + k];           y = a[j + k + i];           a[j + k] = 1ll * (x + y) * inv2 % MOD;           a[j + k + i] = 1ll * (x - y + MOD) * inv2 % MOD;         }       }     }   }   int solve(int x) {     int res = 0;     while (x) {       res += (x & 1);       x >>= 1;     }     return res;   }   int qpower(int x, int y) {     int res = 1;     while (y) {       if (y & 1) res = 1LL * res * x % MOD;       x = 1LL * x * x % MOD;       y >>= 1;     }     return res;   }   int ans = 0;   char s[1100010];   int main() {     int n;     n = read();     scanf( %s , s);     for (int i = 0; i < (1 << n); i++) {       A[i] = s[i] -  0 ;     }     for (int i = 0; i < (1 << n); i++) {       B[i] = 1 - A[i];     }     N = 1 << n;     fwt(A);     fwt(B);     for (int i = 0; i < (1 << n); i++) {       A[i] = 1LL * A[i] * B[i] % MOD;     }     wfwt(A);     for (int i = 0; i < (1 << n); i++) {       ans += 3LL * A[i] * qpower(2, solve(i)) % MOD;       ans %= MOD;     }     printf( %d n , ans);     return 0;   }


//zxggtxdy!   #include<bits/stdc++.h>   using namespace std;   const int M=52,N=2e3+7,p=998244353;   int n,k,D,l[M],f1[N],f2[N],s[N],G[M][N],Q[M][N],dp[2][N];   inline long long pows(long long u,int v){    long long ans=1; while(v>0) {if(v&1) ans=ans*u%p; u=u*u%p,v=v>>1;} return ans;   }   inline int binom(int a,int b){    if(a<b||b<0) return 0; return 1ll*f1[a]*f2[a-b]%p*f2[b]%p;   }   inline int getval(int u,int q){    int A=0,B=0; if(u<0) return 1; if(u==0) return pows(1-q+p,p-2);    for(int i=1;i<=u+2;i++) s[i]=(s[i-1]+binom(i,u)*pows(q,i))%p; int v=pows(q,p-2);    for(int i=1;i<=u+2;i++){     int k=pows(v,i),b=s[i]*pows(v,i)%p,W=1ll*f1[u+2]*pows(i,p-2)%p;     W=1ll*W*f2[i-1]%p*f2[u+2-i]%p; if((i+u)&1) W=p-W;     A=(A+1ll*k*W)%p,B=(B+1ll*b*W)%p;    }    return pows(A-1+p,p-2)*B%p*pows(v,u)%p;   }   inline int getans(int u,int q){    return pows(pows(D,p-2)*(u-q*k)%p,q)*f2[q]%p;    }   signed main(){    cin>>n>>k,f1[0]=f2[0]=1,G[0][0]=1;    for(int i=1;i<=n;i++) cin>>l[i],D+=l[i];    for(int i=1;i<=2005;i++) f1[i]=1ll*f1[i-1]*i%p,f2[i]=pows(f1[i],p-2);    for(int i=1;i<=n;i++){     memset(dp,0,sizeof(dp)),dp[0][0]=1,dp[1][0]=(l[i]>k);     for(int j=1;j<=l[i]/k;j++) dp[0][j]=getans(l[i],j);     for(int j=1;j<l[i]/k;j++) dp[1][j]=getans(l[i]-k,j);     memset(Q,0,sizeof(Q));     for(int a=0;a<=i;a++)      for(int b=0;b<=D-l[i];b++)       for(int c=0;c<=1;c++)        for(int d=0;d<=l[i];d++)         Q[a+c][b+d]=(Q[a+c][b+d]+1ll*G[a][b]*dp[c][d])%p;     for(int a=0;a<=i;a++)      for(int b=0;b<=D;b++) G[a][b]=Q[a][b];    }    int ans=0;    for(int a=n;a>=0;a--)     for(int b=0;b<=D;b++){      if(a+b>0&&G[a][b]){       int Q=(D-(a+b)*k+p)%p*pows(D,p-2)%p,T=pows(1-Q+p,p-2-b)*f1[b]%p;       ans=(ans+pows(p-1,a+b+1)*G[a][b]%p*T%p)%p;      }     }    cout<<ans<<endl;    return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, bool> isMark;   int main() {     long long n, x, y, l;     cin >> n >> l >> x >> y;     bool b = 0, g = 0, d = 0, bd = 0;     long long diff = y - x, B = y + x;     long long tmp;     long long mark = -1, mark2 = -1;     bool n1 = 1, n2 = 1;     for (int i = 0; i < n; i++) {       cin >> tmp;       isMark[tmp] = 1;       b |= isMark.count(tmp - x);       g |= isMark.count(tmp - y);       d |= isMark.count(tmp - diff);       bd |= isMark.count(tmp - B);       if (b && g) {         cout <<  0 ;         return 0;       }       if (d && n1) {         long long idx = tmp + x;         if (idx > l) {           idx = tmp - y;           if (idx >= 0) {             mark = idx;           }         } else {           mark = idx;         }         if (mark == -1) {           d = 0;         } else {           n1 = 0;         }       }       if (bd && n2) {         mark2 = tmp - y;         n2 = 0;       }     }     if (b & !g) {       cout <<  1 n  << y;     } else if (g & !b) {       cout <<  1 n  << x;     } else if (d) {       cout <<  1 n ;       cout << mark;     } else if (bd) {       cout <<  1 n  << mark2 <<   n ;     } else {       cout <<  2 n ;       cout << x <<     << y;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   inline int read() {     char ch = getchar();     int x = 0, f = 1;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) -  0  + ch;       ch = getchar();     }     return x * f;   }   struct node {     int x, y, z;     bool operator<(const node W) const { return y < W.y; }   } a[N];   int n;   long long b[N];   int cnt;   pair<long long, int> tr[N << 2];   long long lazy[N << 2];   long long ans;   void pushup(int rt) {     if (tr[rt << 1].first > tr[rt << 1 | 1].first)       tr[rt] = tr[rt << 1];     else       tr[rt] = tr[rt << 1 | 1];     tr[rt].first += lazy[rt];   }   void build(int rt, int l, int r) {     if (l == r) {       tr[rt] = {b[l], l};       return;     }     int mid = l + r >> 1;     build(rt << 1, l, mid);     build(rt << 1 | 1, mid + 1, r);     pushup(rt);   }   void update(int rt, int L, int R, int C, int l, int r) {     if (L <= l && R >= r) {       tr[rt].first += C;       lazy[rt] += C;       return;     }     int mid = l + r >> 1;     if (L <= mid) update(rt << 1, L, R, C, l, mid);     if (R > mid) update(rt << 1 | 1, L, R, C, mid + 1, r);     pushup(rt);   }   pair<long long, int> query(int rt, int L, int R, long long tg, int l, int r) {     if (L <= l && R >= r) {       return {tr[rt].first + tg, tr[rt].second};     }     int mid = l + r >> 1;     if (L > mid) {       return query(rt << 1 | 1, L, R, tg + lazy[rt], mid + 1, r);     } else if (R <= mid) {       return query(rt << 1, L, R, tg + lazy[rt], l, mid);     } else {       return max(query(rt << 1 | 1, mid + 1, R, tg + lazy[rt], mid + 1, r),                  query(rt << 1, L, mid, tg + lazy[rt], l, mid));     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d%d , &a[i].x, &a[i].y, &a[i].z);       if (a[i].x > a[i].y) swap(a[i].x, a[i].y);       b[++cnt] = a[i].x;       b[++cnt] = a[i].y;     }     sort(b + 1, b + cnt + 1);     cnt = unique(b + 1, b + cnt + 1) - (b + 1);     for (int i = 1; i <= n; i++) {       a[i].x = lower_bound(b + 1, b + cnt + 1, a[i].x) - b;       a[i].y = lower_bound(b + 1, b + cnt + 1, a[i].y) - b;     }     build(1, 1, cnt);     sort(a + 1, a + n + 1);     long long tx = 2e9, ty = 2e9;     for (int i = 1; i <= n; i++) {       update(1, 1, a[i].x, a[i].z, 1, cnt);       if (i < n && a[i].y == a[i + 1].y) continue;       pair<long long, int> tmp = query(1, 1, a[i].y, 0, 1, cnt);       if (tmp.first - b[a[i].y] > ans) {         ans = tmp.first - b[a[i].y];         tx = b[tmp.second];         ty = b[a[i].y];       }     }     printf( %lld n , ans);     printf( %lld %lld %lld %lld n , tx, tx, ty, ty);   }


#include <bits/stdc++.h>   using namespace std;   long long n, l, r, k;   bool checks(long long s) {     long long x = (k - r + l - 1) / (n + s);     long long sp = k - r + l - 1 - x * (n + s);     if (sp < r - l + 1) sp++;     if (sp > r - l + 1) {       return false;     }     if (s - sp > n - r + l - 1) {       return false;     }     return true;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> l >> r >> k;     if (l > r) r += n;     if (k < r - l + 1) {       cout <<  -1 ;       return 0;     }     if (n < 1e6) {       long long res = -1;       long long s = 0, sp = 0;       for (s = 0; s <= n; s++) {         if (checks(s)) res = s;       }       cout << res <<   n ;     } else {       long long res = -1;       long long m = k - r + l - 1;       for (long long x = 1; x < 1e6; x++) {         long long a, b;         a = (m - x * n - r + l - 1) / x;         b = (m - x * n + n - r + l) / (x + 1);         b = min(b, n);         if (a <= b && checks(b)) res = max(res, b);       }       long long sp = k - r + l - 1;       if (sp < r - l + 1) sp++;       if (sp >= 0 && sp <= r - l + 1) {         res = max(res, n - r + l - 1 + sp);       }       cout << res <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     long long w, id;     bool friend operator<(node a, node b) {       if (a.w == b.w)         return a.id < b.id;       else         return a.w < b.w;     }   } a[1000004];   long long b[1000004][4];   long long l[1000004];   long long r[1000004];   int main() {     long long n, x;     cin >> n >> x;     for (int i = 1; i <= n; ++i) {       scanf( %lld , &a[i].w);       a[i].id = i;     }     sort(a + 1, a + 1 + n);     long long cnt = 1;     b[cnt][0] = b[cnt][1] = a[1].id;     b[cnt][3] = a[1].w;     for (int i = 2; i <= n; ++i) {       if (a[i].w == a[i - 1].w)         b[cnt][1] = a[i].id;       else {         cnt++;         b[cnt][0] = b[cnt][1] = a[i].id;         b[cnt][3] = a[i].w;       }     }     long long ans = 0;     long long base = 0;     long long lmax = 1;     long long rmin = cnt;     for (int i = 1; i <= cnt; ++i) {       if (b[i][0] > base) {         base = b[i][1];         l[i] = base;         lmax = i;       } else         break;     }     base = 1e9;     for (int i = cnt; i >= 1; --i) {       if (b[i][1] < base) {         base = b[i][0];         r[i] = base;         rmin = i;       } else         break;     }     if (lmax < rmin) {       for (int i = 1; i <= lmax; ++i) {         int pos = upper_bound(r + (int)rmin, r + (int)cnt + 1, l[i]) - r;         if (pos < cnt + 1)           ans += (b[cnt][3] - b[pos - 1][3]) * (b[i + 1][3] - b[i][3]);       }       cout << ans + (x - b[cnt][3] + 1) * b[lmax + 1][3] +                   (b[cnt][3] - b[rmin - 1][3]) * b[1][3];     } else {       cout << x * (x + 1) / 2 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   string s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int n, p, l = -1, r = 0, i, j, ans = 0;     cin >> n >> p;     cin >> s;     n--, p--;     if (p > n / 2) p = n - p;     for (i = 0; i <= n / 2; i++) {       if (s[i] != s[n - i]) {         long long int dif = abs(s[i] - s[n - i]);         ans += min(dif, 26 - dif);         if (l < 0)           l = i;         else           r = i;       }     }     ans += min(abs(p - l), abs(p - r));     ans += max((r - l), 0LL);     cout << (l < 0 ? 0 : ans) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 1e3 + 9, lg = 19, MOD = 1e9 + 7, maxV = 1e6 + 9;   pair<int, int> arr[3];   int dp[maxN][maxN];   pair<int, int> auxi[maxN][maxN];   bool vi[maxN][maxN];   int dst(pair<int, int> one, pair<int, int> two) {     return abs(one.first - two.first) + abs(two.second - one.second);   }   const int dr[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};   bool ins(int x, int y) { return x < maxN && y < maxN && y >= 0 && x >= 0; }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n = 3;     for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second;     pair<int, int> begi = pair<int, int>(min(arr[0].first, arr[1].first),                                          min(arr[0].second, arr[1].second));     pair<int, int> ende = pair<int, int>(max(arr[0].first, arr[1].first),                                          max(arr[0].second, arr[1].second));     pair<int, int> clos = begi;     for (int i = begi.first; i <= ende.first; i++) {       for (int j = begi.second; j <= ende.second; j++) {         if (dst(arr[2], clos) > dst(arr[2], pair<int, int>(i, j))) {           clos = pair<int, int>(i, j);         }       }     }     queue<pair<int, int> > q;     q.push(clos);     memset(dp, -1, sizeof dp);     dp[clos.first][clos.second] = 0;     auxi[clos.first][clos.second] = pair<int, int>(-1, -1);     while (q.size()) {       pair<int, int> cr = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         int x = cr.first + dr[i][0], y = cr.second + dr[i][1];         if (!ins(x, y) || dp[x][y] != -1) continue;         dp[x][y] = dp[cr.first][cr.second] + 1;         auxi[x][y] = cr;         q.push(pair<int, int>(x, y));       }     }     for (int i = 0; i < 3; i++) {       pair<int, int> strt = arr[i];       while (strt != pair<int, int>(-1, -1)) {         vi[strt.first][strt.second] = 1;         strt = auxi[strt.first][strt.second];       }     }     vector<pair<int, int> > ans;     for (int i = 0; i < maxN; i++)       for (int j = 0; j < maxN; j++)         if (vi[i][j]) ans.push_back(pair<int, int>(i, j));     cout << ans.size() <<   n ;     for (pair<int, int> el : ans) cout << el.first <<     << el.second <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2020;   struct Slope {     long long x, y;     bool up;     Slope() {}     Slope(long long x, long long y, bool up) : x(x), y(y), up(up) {}     bool operator<(const Slope& m) const {       pair<long long, long long> p1 = {y, x};       pair<long long, long long> p2 = {m.y, m.x};       return (1LL * p1.first * p2.second < 1LL * p2.first * p1.second);     }   };   long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); }   pair<long long, long long> getSlope(long long x, long long y) {     long long G = gcd(x, y);     x /= G;     y /= G;     if (x < 0) {       x = -x;       y = -y;     }     return {y, x};   }   long long C(long long n, long long k = 2) {     if (k > n) return 0;     return n * (n - 1) / 2;   }   vector<Slope> v;   vector<pair<long long, long long>> Point;   long long Sweep(int centerIdx) {     long long side1 = 0, side2 = 0, Ans = 0;     for (int i = 0; i < (int)Point.size(); i++)       if (i != centerIdx) {         long long x = Point[i].first - Point[centerIdx].first;         long long y = Point[i].second - Point[centerIdx].second;         bool up;         if (x > 0 || (x == 0 && y > 0))           up = true, side1++;         else           up = false, side2++;         pair<long long, long long> m = getSlope(x, y);         v.push_back(Slope(m.first, m.second, up));       }     sort(v.begin(), v.end());     for (int i = 0; i < v.size(); i++) {       if (v[i].up)         side1--;       else         side2--;       Ans += C(side1) * C(side2);       if (v[i].up)         side2++;       else         side1++;     }     v.clear();     return Ans;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       Point.push_back({x, y});     }     long long Ans = 0;     for (int i = 0; i < n; i++) Ans += Sweep(i);     printf( %I64d n , Ans / 2);     return 0;   }