#include <bits/stdc++.h>   using namespace std;   const int maxn = 300005;   const long long mod = 998244353;   long long fac[maxn], invfac[maxn];   int a[maxn];   long long qp(long long x, long long p, long long MOD) {     long long res = 1;     while (p) {       if (p & 1) res = res * x % MOD;       x = x * x % MOD;       p >>= 1;     }     return res;   }   void init() {     fac[0] = 1;     invfac[0] = 1;     for (int i = 1; i < maxn; i++) fac[i] = fac[i - 1] * 1ll * i % mod;     invfac[maxn - 1] = qp(fac[maxn - 1], mod - 2, mod);     for (int i = maxn - 2; i >= 1; i--)       invfac[i] = invfac[i + 1] * 1ll * (i + 1) % mod;   }   long long C(int n, int k) {     return fac[n] * invfac[n - k] % mod * invfac[k] % mod;   }   int main() {     init();     int n;     scanf( %d , &n);     for (int i = 1; i <= n + n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n * 2 + 1);     long long ans = 0;     for (int i = 1; i <= n * 2; i++) {       if (i <= n) {         ans = (ans - C(n * 2, n) * a[i] % mod) % mod;       } else {         ans = (ans + C(n * 2, n) * a[i] % mod + mod) % mod;       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int f3(int n) { return n * (n - 1) * (n - 2) / 6; }   int main() {     int K;     cin >> K;     int N = 3;     while (f3(N + 1) <= K) N++;     int MN = N * (N - 1) / 2;     int TN = 100;     vector<string> E(TN, string(TN,  0 ));     for (int i = 0; i < N; i++) {       for (int j = i + 1; j < N; j++) {         E[i][j] = E[j][i] =  1 ;       }     }     int GK = f3(N);     for (int i = N; i < TN; i++) {       for (int j = 0; j < N && GK + j <= K; j++) {         E[i][j] = E[j][i] =  1 ;         GK += j;       }     }     cout << TN << endl;     for (int i = 0; i < E.size(); i++) cout << E[i] << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int up = 1e6 + 6;   int n, m, k, pos;   int cost[up], p[up], last[up];   int main() {     scanf( %d , &n);     scanf( %d , &m);     scanf( %d , &k);     for (int i = 1; i <= m; i++) {       scanf( %d , &pos);       p[pos] = 1;     }     if (p[0]) {       printf( %d n , -1);       return 0;     }     for (int i = 0; i <= n; i++) {       if (!p[i])         last[i] = i;       else         last[i] = last[i - 1];     }     for (int i = 1; i <= k; i++) {       scanf( %d , &cost[i]);     }     long long res = 1ll * up * up;     for (int i = 1; i <= k; i++) {       int cnt = 1;       for (int j = i; j < n; j += i) {         if (j - last[j] >= i) {           cnt = n + 10;           break;         } else {           j = last[j];           cnt++;         }       }       if (cnt <= n) {         res = min(res, 1ll * cnt * cost[i]);       }     }     if (res == 1ll * up * up) {       res = -1ll;     }     printf( %I64d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937_64 rang(       chrono::high_resolution_clock::now().time_since_epoch().count());   int rng(int lim) {     uniform_int_distribution<int> uid(0, lim - 1);     return uid(rang);   }   void solution() {     int y;     cin >> y;     while (true) {       y = y + 1;       int a = y / 1000;       int b = y / 100 % 10;       int c = y / 10 % 10;       int d = y % 10;       if (a != b && a != c && a != d && b != c && b != d && c != d) {         break;       }     }     cout << y << endl;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     srand(chrono::high_resolution_clock::now().time_since_epoch().count());     int test = 1;     while (test--) {       solution();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<char, char> q;   int main() {     char a[30], b[30];     scanf( %s , a);     scanf( %s , b);     q.clear();     for (int i = 0; i < 26; i++) {       q[a[i]] = b[i];       q[a[i] - 32] = b[i] - 32;     }     char aim[2000];     scanf( %s , aim);     for (int i = 0; aim[i]; i++) {       if (q[aim[i]])         printf( %c , q[aim[i]]);       else         printf( %c , aim[i]);     }     printf(  n );   }


#include <bits/stdc++.h>   const double EPS = 1e-7;   const double PI = 3.1415926535897932384626;   const int MAXN = 100001;   int n, a[MAXN];   int main() {     std::cin >> n;     for (int i = 1; i <= n; i++) {       std::cin >> a[i];     }     long long answer = 0;     for (int i = 1; i <= n; i++) {       answer += a[i];     }     std::sort(a + 1, a + n + 1);     if (answer & 1) {       for (int i = 1; i <= n; i++)         if (a[i] & 1) {           answer -= a[i];           break;         }     }     std::cout << answer << std::endl;     return 0;   }


#include <bits/stdc++.h>   int n;   int degree[100010];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n - 1; i++) {       int r1, r2;       scanf( %d%d , &r1, &r2);       degree[r1]++;       degree[r2]++;     }     for (int i = 1; i <= n; i++) {       if (degree[i] == 2) {         printf( NO );         return 0;       }     }     printf( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long int cost = 0;   vector<int> a, b, c;   vector<vector<int>> g;   pair<int, int> dfs(int cur, int par, int mn_before) {     pair<int, int> ap = {0, 0};     int mn_cur = min(a[cur], mn_before);     if (b[cur] != c[cur]) {       if (b[cur] == 1) {         ap.first = 1;         ap.second = 0;       } else {         ap.first = 0;         ap.second = 1;       }     }     for (auto it : g[cur]) {       if (it != par) {         pair<int, int> pa = dfs(it, cur, mn_cur);         ap.first += pa.first;         ap.second += pa.second;       }     }     if (a[cur] < mn_before) {       int cnt = min(ap.first, ap.second);       cost += 2 * 1ll * cnt * 1ll * a[cur];       ap.first -= cnt;       ap.second -= cnt;     }     return ap;   }   long long int solve() {     cin >> n;     a.resize(n + 1);     b.resize(n + 1);     c.resize(n + 1);     for (int i = 1; i <= n; i++) {       cin >> a[i] >> b[i] >> c[i];     }     g.resize(n + 1);     int u, v;     for (int i = 0; i < n - 1; i++) {       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     pair<int, int> a = dfs(1, -1, 1000000000);     if (a.first != a.second) {       return -1;     }     return cost;   }   int main() {     int t = 1;     while (t--) {       cout << solve() <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int num, rt[200010], n;   struct data {     int l, r, v;   } tr[200010 * 20];   void insert(int p, int l, int r, int n, int &x) {     x = ++num;     tr[x] = tr[n];     if (l == r) {       tr[x].v++;       return;     }     int mid = l + r >> 1;     if (p <= mid)       insert(p, l, mid, tr[n].l, tr[x].l);     else       insert(p, mid + 1, r, tr[n].r, tr[x].r);     tr[x].v = tr[tr[x].l].v + tr[tr[x].r].v;   }   int ask(int al, int ar, int l, int r, int lx, int x) {     if (x == 0) return 0;     if (al <= l && r <= ar) return tr[x].v - tr[lx].v;     int mid = l + r >> 1, ans = 0;     if (al <= mid) ans = ask(al, ar, l, mid, tr[lx].l, tr[x].l);     if (ar > mid) ans += ask(al, ar, mid + 1, r, tr[lx].r, tr[x].r);     return ans;   }   int ask(int r1, int r2, int l1, int l2) {     if (r1 > r2 || l1 > l2) return 0;     return ask(r1, r2, 1, n, rt[l1 - 1], rt[l2]);   }   int main() {     int m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       insert(x, 1, n, rt[i - 1], rt[i]);     }     for (int i = 1; i <= m; i++) {       int l1, l2, r1, r2;       scanf( %d%d%d%d , &l1, &r1, &l2, &r2);       long long ans = 0;       ans += 1ll * ask(1, r1 - 1, 1, l1 - 1) * ask(r1, n, l1, n);       ans += 1ll * ask(1, r1 - 1, l1, l2) * ask(r1, n, 1, n);       ans += 1ll * ask(1, r1 - 1, l2 + 1, n) * ask(r1, n, 1, l2);       ans += 1ll * ask(r1, r2, 1, l1 - 1) * ask(r1, n, l1, n);       ans += 1ll * ask(r1, r2, l1, l2) *              (ask(r1, n, 1, n) - ask(r1, r2, 1, l1 - 1) - ask(r1, r2, l1, l2));       ans += 1ll * ask(r1, r2, l2 + 1, n) * ask(r2 + 1, n, 1, l2);       ans += 1ll * ask(r1, r2, l1, l2) * (ask(r1, r2, l1, l2) - 1) / 2;       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 10000;   const long long MAXN = 1000;   int n, a[MAXN], sum, cnt, ans;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       sum += a[i];     }     sort(a, a + n + 1);     for (int i = n; i >= 1; i--) {       cnt += a[i];       ans++;       if (cnt > sum - cnt) {         cout << ans;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int l = 0; l < t; l++) {       long long n, s, k;       cin >> n >> s >> k;       unordered_set<long long> tilos;       for (int i = 0; i < k; ++i) {         long long x;         cin >> x;         tilos.insert(x);       }       for (long long i = 0; i <= k; i++) {         if ((s + i <= n) && (tilos.find(s + i) == tilos.end())) {           cout << i << endl;           break;         }         if ((s - i >= 1) && (tilos.find(s - i) == tilos.end())) {           cout << i << endl;           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int d[200100], a[200100];   long long ans;   int main() {     int n, m, i, tmp, s, x;     bool f = false, flag = true;     scanf( %d , &n);     for (i = 1; i <= n; ++i) scanf( %d , d + i);     scanf( %d , &m);     if (m % ((n - 1) << 1) == 0) f = true;     s = (m / ((n - 1) << 1)) << 1;     ans = (long long)s * (d[n] - d[1]);     while (m--) {       scanf( %d , &x);       ++a[x];     }     a[1] <<= 1;     a[n] <<= 1;     tmp = d[2] - d[1];     for (i = 1; i < n; ++i) {       if (f && d[i + 1] - d[i] != tmp) flag = false;       ans += min(a[i] - s, a[i + 1] - s) * (d[i + 1] - d[i]);     }     if (flag)       printf( %I64d n , ans - f * tmp);     else       puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long abs2(long long a) { return a >= 0 ? a : -a; }   long long calc(long long k, long long a) {     if (k >= 0) return k / a;     return (k - (a - 1)) / a;   }   long long calc2(long long a1, long long b1, long long a2, long long b2) {     long long x = abs2(a1 - a2) + abs2(b2 - (a2 + (b1 - a1)));     long long y = abs2(a1 - a2) + abs2(b2 - (-a2 + a1 + b1));     return min(x, y);   }   int main() {     long long a, b, x1, x2, y1, y2;     while (cin >> a >> b >> x1 >> y1 >> x2 >> y2) {       long long a1, b1;       long long a2, b2;       a1 = calc(x1 + y1, 2 * a);       b1 = calc(x1 - y1, 2 * b);       a2 = calc(x2 + y2, 2 * a);       b2 = calc(x2 - y2, 2 * b);       long long x = calc2(a1, b1, a2, b2);       long long y = calc2(b1, a1, b2, a2);       cout << (int)min(x, y) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using lld = signed long long int;   lld solve() {     lld n, m, k;     cin >> n >> m >> k;     lld x = n / k;     if (m <= x) return m;     if (m <= x + (k - 1)) return x - 1;     return x - ceil((m - x) / (k - 1.0));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       cout << solve() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<  :   << arg1 << endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<  :   << arg1 <<   | ;     __f(comma + 1, args...);   }   const int INF = 1 << 29;   const int MOD = 1e9 + 7;   struct Node {     Node *pre, *nxt;     int val, pos, len;     bool operator<(const Node& rhs) const {       if (len != rhs.len) return len > rhs.len;       return pos < rhs.pos;     }   };   const int N = 2e5 + 10;   Node pool[N << 1], *last;   struct cmp {     bool operator()(int u, int v) const { return pool[u] < pool[v]; }   };   int main() {     int n;     scanf( %d , &n);     vector<int> a(n);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);     }     set<int, cmp> A;     last = pool;     for (int i = 0, j; i < n; i = j) {       for (j = i + 1; j < n && a[j] == a[i]; ++j)         ;       Node* cur = last++;       cur->val = a[i];       cur->pos = i;       cur->len = j - i;       cur->pre = cur->nxt = nullptr;       if (i > 0) {         auto pre = last - 2;         pre->nxt = cur;         cur->pre = pre;       }     }     for (auto it = pool; it != last; ++it) {       A.insert(it - pool);     }     int ret = 0;     while (A.size()) {       auto it = A.begin();       auto cur = &pool[*it];       auto pre = cur->pre;       auto nxt = cur->nxt;       if (pre && nxt && pre->val == nxt->val) {         cur = last++;         cur->val = pre->val;         cur->pos = pre->pos;         cur->len = pre->len + nxt->len;         cur->pre = pre->pre;         cur->nxt = nxt->nxt;         if (cur->pre) cur->pre->nxt = cur;         if (cur->nxt) cur->nxt->pre = cur;         A.erase(pre - pool);         A.erase(nxt - pool);         A.insert(cur - pool);       } else {         if (pre) pre->nxt = nxt;         if (nxt) nxt->pre = pre;       }       A.erase(it);       ++ret;     }     printf( %d n , ret);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 10;   int a[maxn], b[maxn];   int main() {     int n;     scanf( %d , &n);     long long ans = 0;     int cnt1 = 0, cnt2 = 0, cnt0 = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , a + i);       if (a[i] <= -1) {         ans += -1 - a[i];         cnt1++;       } else if (a[i] >= 1) {         ans += a[i] - 1;         cnt2++;       } else {         cnt0++;       }     }     if (cnt1 % 2) {       if (cnt0) {         ans += cnt0;       } else {         ans += 2;       }     } else {       ans += cnt0;     }     printf( %lld n , ans);     getchar();     getchar();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000005;   int T, n, m, las = 1, ndnum = 1, ans;   char s[maxn];   struct node {     int ch[26], len, fa;   } d[maxn << 1];   int siz[maxn << 1], vis[maxn << 1], timer;   int head[maxn << 1], tot;   struct edge {     int nxt, to;   } e[maxn << 1];   void Addedge(int x, int y) {     e[++tot] = (edge){head[x], y};     head[x] = tot;   }   void Insert(int x) {     int p = las, np = las = ++ndnum;     siz[np] = 1;     d[np].len = d[p].len + 1;     while (p && !d[p].ch[x]) {       d[p].ch[x] = np;       p = d[p].fa;     }     if (!p) {       d[np].fa = 1;     } else {       int q = d[p].ch[x];       if (d[q].len == d[p].len + 1) {         d[np].fa = q;       } else {         int nq = ++ndnum;         d[nq] = d[q];         d[nq].len = d[p].len + 1;         d[q].fa = d[np].fa = nq;         while (p && d[p].ch[x] == q) {           d[p].ch[x] = nq;           p = d[p].fa;         }       }     }   }   void dfs(int x) {     int id;     for (int i = head[x]; i; i = e[i].nxt) {       id = e[i].to;       dfs(id);       siz[x] += siz[id];     }   }   void go(int &x, int y, int &len) {     if (d[x].ch[y]) {       ++len;       x = d[x].ch[y];     } else {       while (x && !d[x].ch[y]) x = d[x].fa;       if (!x) {         x = 1;         len = 0;       } else {         len = d[x].len + 1;         x = d[x].ch[y];       }     }   }   void del(int &x, int &len) {     if (len != m) return;     --len;     if (len == d[d[x].fa].len) x = d[x].fa;   }   void work(int x, int y) {     if (y < m || vis[x] == timer) return;     ans += siz[x];     vis[x] = timer;   }   int main() {     scanf( %s , s + 1);     n = strlen(s + 1);     for (int i = 1; i <= n; ++i) Insert(s[i] -  a );     for (int i = 2; i <= ndnum; ++i) Addedge(d[i].fa, i);     dfs(1);     scanf( %d , &T);     int now, len;     while (T--) {       scanf( %s , s + 1);       m = strlen(s + 1);       now = 1;       len = 0;       for (int i = 1; i <= m; ++i) go(now, s[i] -  a , len);       ++timer;       ans = 0;       work(now, len);       for (int i = 1; i < m; ++i) {         del(now, len);         go(now, s[i] -  a , len);         work(now, len);       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int OO = (int)2e9;   const double PI = 2 * acos(0.0);   const double EPS = 1e-9;   int dcmp(double a, double b) { return fabs(a - b) <= EPS ? 0 : a > b ? 1 : 2; }   int DI[] = {-1, 0, 1, 0, 1, -1, -1, 1};   int DJ[] = {0, 1, 0, -1, 1, -1, 1, -1};   void clc1(string x) {     int idx = x.find( C );     string r = x.substr(1, idx - 1);     string c = x.substr(idx + 1);     int cc = atoi(c.c_str());     string ans =   ;     while (cc) {       char d = cc % 26 +  A  - 1;       cc /= 26;       if (d ==  @ ) {         d =  Z ;         cc--;       }       ans += d;     }     reverse(ans.begin(), ans.end());     cout << ans << r << endl;   }   void clc2(string x) {     string c =   ;     cout <<  R ;     for (int i = 0; i < (int)(int)x.size(); i++) {       if (isdigit(x[i]))         cout << x[i];       else         c += x[i];     }     cout <<  C ;     int ans = 0;     int p = 1;     for (int i = (int)c.size() - 1; i >= 0; i--) {       ans += (((c[i] -  A ) + 1) * p);       p *= 26;     }     cout << ans << endl;   }   bool f(string x) {     if (x[0] !=  R ) return 0;     int r = x.find( C );     if (r == -1) return 0;     for (int i = 1; i < r; i++)       if (isdigit(x[i])) return 1;     return 0;   }   int main() {     int n;     cin >> n;     for (int i = 0; i < (int)n; i++) {       string x;       cin >> x;       if (f(x))         clc1(x);       else         clc2(x);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 300;   const int mod = 1e9 + 7;   const int INF = 0x3f3f3f3f;   const double eps = 1e-6;   string s;   int gx[] = {0, 0, 1, -1};   int gy[] = {1, -1, 0, 0};   bool used[MAX_N][MAX_N];   bool check(int x, int y) {     int cnt = 0;     for (int i = 0; i < 4; ++i) {       int nx = x + gx[i];       int ny = y + gy[i];       if (used[nx][ny]) ++cnt;     }     return cnt == 1;   }   bool solve() {     cin >> s;     int x = 100, y = 100;     used[x][y] = true;     for (auto &ch : s) {       if (ch ==  U )         ch = ch -  U  +  3 ;       else if (ch ==  D )         ch = ch -  D  +  2 ;       else if (ch ==  L )         ch = ch -  L  +  1 ;       else         ch = ch -  R  +  0 ;     }     for (auto ch : s) {       x += gx[ch -  0 ];       y += gy[ch -  0 ];       if (used[x][y]) return false;       used[x][y] = true;       if (!check(x, y)) return false;     }     return true;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int t = 1;     while (t--) {       if (solve())         cout <<  OK  <<   n ;       else         cout <<  BUG  <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace kotespace {   template <class T>   class duplet {    private:    public:     T x, y;     duplet(){};     duplet(T a, T b) : x(a), y(b){};     bool operator<(const duplet P) const {       return (x < P.x || (x == P.x && y < P.y));     }     bool operator>(const duplet P) const {       return (x > P.x || (x == P.x && y > P.y));     }     bool operator==(const duplet P) const { return (x == P.x && y == P.y); }     bool operator!=(const duplet P) const { return (x != P.x || y != P.y); }     void reverse() { std::swap(x, y); }   };   template <class P>   istream &operator>>(istream &in, duplet<P> &T) {     return (in >> T.x >> T.y);   }   template <class P>   ostream &operator<<(ostream &out, duplet<P> T) {     return (out << T.x <<     << T.y);   }   template <class T>   class triplet {    private:    public:     T x, y, z;     triplet(){};     triplet(T a, T b, T c) : x(a), y(b), z(c){};     bool operator<(const triplet P) const {       return (x < P.x || (x == P.x && y < P.y) ||               (x == P.x && y == P.y && z < P.z));     }     bool operator>(const triplet P) const {       return (x > P.x || (x == P.x && y > P.y) ||               (x == P.x && y == P.y && z > P.z));     }     bool operator==(const triplet P) const {       return (x == P.x && y == P.y && z == P.z);     }     bool operator!=(const triplet P) const {       return (x != P.x || y != P.y || z != P.z);     }     void reverse() { std::swap(x, z); }     void cycle_right(int a) {       if (a == 1) {         std::swap(x, y);         std::swap(x, z);       }       if (a == 2) {         std::swap(x, z);         std::swap(y, x);       }     }     void cycle_left(int a) {       if (a == 1) {         std::swap(x, z);         std::swap(y, x);       }       if (a == 2) {         std::swap(x, y);         std::swap(x, z);       }     }   };   template <class P>   istream &operator>>(istream &in, triplet<P> &T) {     return (in >> T.x >> T.y >> T.z);   }   template <class P>   ostream &operator<<(ostream &out, triplet<P> &T) {     return (out << T.x <<     << T.y <<     << T.z);   }   }  // namespace kotespace   using namespace kotespace;   long long inf = 1000 * 1000 * 1000 + 7;   long long inf64 = inf * inf;   long long mod = 228228227;   float START_TIME;   namespace std {   template <>   struct hash<duplet<int>> {     size_t operator()(const duplet<int> &k) const {       return (size_t)(k.x) * 102931204 + (size_t)(k.y) * 434823123;     }   };   }  // namespace std   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cerr << fixed << setprecision(8);     cout << fixed << setprecision(8);     srand(time(0));     START_TIME = clock();     int t;     cin >> t;     while (t-- > 0) {       int n;       cin >> n;       string s;       cin >> s;       int balx = 0, baly = 0;       map<pair<int, int>, vector<int>> q;       long long l = -inf, r = inf;       q[make_pair(0, 0)].push_back(0);       for (int i = 0; i < n; ++i) {         if (s[i] ==  L ) ++balx;         if (s[i] ==  R ) --balx;         if (s[i] ==  U ) ++baly;         if (s[i] ==  D ) --baly;         q[make_pair(balx, baly)].push_back(i + 1);       }       for (auto &c : q) {         for (int i = 0; i + 1 < c.second.size(); ++i) {           if (c.second[i + 1] - c.second[i] < r - l) {             r = c.second[i + 1];             l = c.second[i];           }         }       }       if (r == inf)         cout << -1 << endl;       else         cout << l + 1 <<     << r << endl;     }     cerr << endl << (clock() - START_TIME) / CLOCKS_PER_SEC <<   sec.  << endl;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:102400000,102400000 )   template <class T>   void chkmax(T &a, T b) {     if (a < b) a = b;   }   template <class T>   void chkmin(T &a, T b) {     if (a > b) a = b;   }   const int inf = 0x3f3f3f3f;   const long long linf = 2e18 + 100;   const double dinf = 2e18 + 100;   const double eps = 1e-7;   const double pi = 3.14159265358979323846264338327950288L;   bool scanld(double &first) {     double xx;     int f = scanf( %lf , &xx);     first = xx;     return f > 0;   }   void printld(const double &first) {     printf( %.10lf , fabs(first) < eps ? 0 : (double)first);   }   inline char nc() {     static char buf[100000], *p1 = buf, *p2 = buf;     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)                ? EOF                : *p1++;   }   template <class T>   void read(T &first) {     char c = nc(), b = 1;     for (; !((c >=  0  && c <=  9 ) || c == EOF); c = nc())       if (c ==  - ) b = -b;     for (first = 0; c >=  0  && c <=  9 ; first = first * 10 + c -  0 , c = nc())       ;     first *= b;   }   inline void read(char *s) {     char c = nc();     int len = 0;     for (; c ==     || c ==   t  || c ==   n ; c = nc())       ;     for (; !(c ==     || c ==   t  || c ==   n  || c == EOF);          s[len++] = c, c = nc())       ;     s[len] = 0;   }   int n, P[100005], len;   vector<int> v[100005];   int ds[100005];   void dfs(int k, int pa) {     P[k] = pa;     for (auto first : v[k]) {       if (first == pa) continue;       dfs(first, k);     }     return;   }   int mx[100005][3], id[100005][3];   void dfs_2(int k, int pa) {     mx[k][0] = 1, mx[k][1] = 0, mx[k][2] = 0;     id[k][0] = k, id[k][1] = 0, id[k][2] = 0;     for (auto first : v[k]) {       if (first == pa) continue;       dfs_2(first, k);       int d = mx[first][0] + 1;       if (d > mx[k][2]) mx[k][2] = d, id[k][2] = first;       if (mx[k][2] > mx[k][1]) swap(mx[k][1], mx[k][2]), swap(id[k][1], id[k][2]);       if (mx[k][1] > mx[k][0]) swap(mx[k][1], mx[k][0]), swap(id[k][1], id[k][0]);     }     return;   }   bool ok[100005];   int pmx[100005];   void dfs_3(int k, int pa) {     if (!pa)       pmx[k] = 0;     else {       int d = id[pa][0] == k ? mx[pa][1] : mx[pa][0];       chkmax(d, pmx[pa]);       pmx[k] = d + 1;     }     if (mx[k][2] > len || mx[k][1] > len && pmx[k] > len)       ok[k] = 1;     else       ok[k] = 0;     for (auto first : v[k]) {       if (first == pa) continue;       dfs_3(first, k);     }     return;   }   int G, st[100005], fs[100005];   int p[100005][17], dp[100005];   void dfs_4(int k, int pa) {     dp[k] = dp[pa] + 1;     p[k][0] = pa;     st[k] = ++G;     for (int i = 1; i <= 16; i++) p[k][i] = p[p[k][i - 1]][i - 1];     ds[k] = k;     for (auto first : v[k]) {       if (first == pa) continue;       dfs_4(first, k);       if (dp[ds[first]] > dp[ds[k]]) ds[k] = ds[first];     }     fs[k] = ++G;     return;   }   int kth_father(int first, int k) {     for (int i = 16; i >= 0; i--)       if (k & (1 << i)) first = p[first][i];     return first;   }   bool is_anc(int p, int first) {     return st[p] <= st[first] && fs[first] <= fs[p];   }   bool chk[100005];   int head, tail;   int main() {     int T;     for (read(T); T--;) {       read(n);       read(head), read(tail);       int first, second;       for (int i = 1; i < n; i++) {         read(first), read(second);         v[first].push_back(second);         v[second].push_back(first);       }       dfs(head, 0);       len = 0;       int t = tail;       while (t != head) {         len++;         t = P[t];       }       dfs_2(head, 0);       dfs_3(head, 0);       int R = -1;       for (int i = 1; i <= n; i++)         if (ok[i]) R = i;       if (R < 0)         puts( NO );       else {         dfs_4(R, 0);         int dph = dp[head], dpt = dp[tail];         bool yes = is_anc(head, tail) || is_anc(tail, head);         if (!yes) {           int ch = head, ct = tail;           int lca = R;           for (int i = 1; i <= n; i++)             if (is_anc(i, ch) && is_anc(i, ct) && st[i] > st[lca]) lca = i;           int lh = dp[ch] - dp[lca], lt = dp[ct] - dp[lca];           bool fh = 0;           while (!yes) {             if (fh && chk[ch]) break;             if (fh) chk[ch] = 1;             if (fh) {               ch = ds[ch];               if (dp[ch] - dp[lca] >= len)                 yes = 1;               else {                 lh = dp[ch] - dp[lca];                 ct = kth_father(ct, lt - (len - lh));                 lt = dp[ct] - dp[lca];               }             } else {               ct = ds[ct];               if (dp[ct] - dp[lca] >= len)                 yes = 1;               else {                 lt = dp[ct] - dp[lca];                 ch = kth_father(ch, lh - (len - lt));                 lh = dp[ch] - dp[lca];               }             }             fh ^= 1;           }         }         puts(yes ?  YES  :  NO );       }       for (int i = 1; i <= n; i++) v[i].clear();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.);   const int MX = 500500;   int t, n, i, j, z[MX];   char s[MX];   vector<complex<double> > a, b;   vector<int> r, ans;   void fft(vector<complex<double> >& a, bool inv) {     int n = a.size();     if (n == 1) return;     for (int i = 1, rv = 0; i < n; i++) {       int j = n / 2;       for (; rv >= j; j /= 2) rv -= j;       rv += j;       if (i < rv) swap(a[i], a[rv]);     }     for (int l = 2; l <= n; l *= 2) {       double alpha = 2 * pi / l;       if (inv) alpha = -alpha;       for (int i = 0; i < n; i += l) {         complex<double> w(1), wn(cos(alpha), sin(alpha));         for (int j = 0; j < l / 2; j++) {           complex<double> x = a[i + j];           complex<double> y = w * a[i + j + l / 2];           a[i + j] = x + y;           a[i + j + l / 2] = x - y;           w *= wn;         }       }     }     if (inv)       for (int i = 0; i < n; i++) a[i] /= n;   }   void mul(vector<complex<double> >& a, vector<complex<double> >& b) {     int n;     for (n = 1; n < a.size() || n < b.size(); n *= 2)       ;     n *= 2;     a.resize(n);     b.resize(n);     r.resize(n);     fft(a, false);     fft(b, false);     for (int i = 0; i < n; i++) a[i] *= b[i];     fft(a, true);     for (int i = 0; i < n; i++) r[i] = int(a[i].real() + 0.5);   }   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       scanf( %s , s);       a.resize(n);       b.resize(n);       for (i = 0; i < n; i++) a[i] = int(s[i] ==  V );       for (i = 0; i < n; i++) b[i] = int(s[n - i - 1] ==  K );       mul(a, b);       for (i = 1; i < n; i++) z[i] = r[n - 1 - i] + r[n - 1 + i];       ans.clear();       for (i = 1; i < n; i++)         if (z[i] == 0) {           for (j = i + i; j < n; j += i)             if (z[j] > 0) break;           if (j >= n) ans.push_back(i);         }       printf( %d n , int(ans.size()) + 1);       for (i = 0; i < ans.size(); i++) printf( %d  , ans[i]);       printf( %d n , n);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   int a[N], L[N], tot, R[N], Stack[N], n, m;   long long Ans[N];   struct Ask {     int l, r, from;   } A[N];   inline int cmp1(Ask x, Ask y) { return x.l > y.l; }   inline int cmp2(Ask x, Ask y) { return x.r < y.r; }   struct segTree {     struct Tree {       int l, r;       long long num, flag;     } T[N * 4];     inline void build(int x, int l, int r) {       T[x].l = l;       T[x].r = r;       T[x].flag = T[x].num = 0;       if (l == r) return;       int mid = (l + r) / 2;       build(x * 2, l, mid);       build(x * 2 + 1, mid + 1, r);     }     inline void down(int x) {       if (!T[x].flag) return;       T[x * 2].num += T[x].flag * (T[x * 2].r - T[x * 2].l + 1);       T[x * 2 + 1].num += T[x].flag * (T[x * 2 + 1].r - T[x * 2 + 1].l + 1);       T[x * 2].flag += T[x].flag;       T[x * 2 + 1].flag += T[x].flag;       T[x].flag = 0;     }     inline void insert(int x, int l, int r, int z) {       if (T[x].l > r || l > T[x].r) return;       if (T[x].l >= l && T[x].r <= r) {         T[x].num += z * (T[x].r - T[x].l + 1);         T[x].flag += z;         return;       }       down(x);       insert(x * 2, l, r, z);       insert(x * 2 + 1, l, r, z);       T[x].num = T[x * 2].num + T[x * 2 + 1].num;     }     inline long long find(int x, int y) {       if (T[x].l == T[x].r) return T[x].num;       down(x);       int mid = (T[x].l + T[x].r) / 2;       if (y <= mid) return find(x * 2, y);       return find(x * 2 + 1, y);     }   } T1, T2;   signed main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= m; i++) A[i].from = i;     for (int i = 1; i <= m; i++) scanf( %d , &A[i].l);     for (int i = 1; i <= m; i++) scanf( %d , &A[i].r);     for (int i = 1; i <= m; i++) Ans[i] = A[i].r - A[i].l + 1;     a[0] = a[n + 1] = n + 1;     Stack[++tot] = 0;     for (int i = 1; i <= n; i++) {       while (a[i] > a[Stack[tot]]) tot--;       L[i] = Stack[tot] + 1;       Stack[++tot] = i;     }     Stack[tot = 1] = n + 1;     for (int i = n; i; i--) {       while (a[i] > a[Stack[tot]]) tot--;       R[i] = Stack[tot] - 1;       Stack[++tot] = i;     }     T1.build(1, 1, n);     T2.build(1, 1, n);     sort(A + 1, A + m + 1, cmp2);     for (int i = 1, j = 1; i <= n; i++) {       T1.insert(1, 1, L[i] - 1, -L[i]);       T2.insert(1, L[i], i, -1);       while (j <= m && A[j].r == i) {         Ans[A[j].from] +=             T1.find(1, A[j].l) + (long long)T2.find(1, A[j].l) * A[j].l;         j++;       }     }     T1.build(1, 1, n);     T2.build(1, 1, n);     sort(A + 1, A + m + 1, cmp1);     for (int i = n, j = 1; i; i--) {       T1.insert(1, R[i] + 1, n, R[i]);       T2.insert(1, i, R[i], 1);       while (j <= m && A[j].l == i) {         Ans[A[j].from] +=             T1.find(1, A[j].r) + (long long)T2.find(1, A[j].r) * A[j].r;         j++;       }     }     for (int i = 1; i <= m; i++) printf( %lld  , Ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int maxn = (1 << 18) - 1;   int main() {     int k;     scanf( %d , &k);     int b = maxn, cnt = 0;     for (int i = 0; i <= 20; ++i) {       if ((k >> i) & 1) {         b ^= (1 << i);       }     }     int cc = maxn >> 1;     printf( %d %d n , 3, 3);     printf( %d %d %d n , maxn, maxn, b);     printf( %d %d% d n , maxn, cc, maxn);     printf( %d %d %d n , b, maxn, cc);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n, q;     cin >> n >> q;     bool lava[2][n];     for (int i = 0; i < 2; i++) {       for (int j = 0; j < n; j++) lava[i][j] = false;     }     set<int> blocked1;     set<int> blocked2;     for (int i = 0; i < q; i++) {       int r, c;       cin >> r >> c;       r--;       c--;       if (lava[r][c]) {         blocked2.erase(c);         if (c < n - 1 && !lava[r][c + 1]) {           blocked1.erase(c);         }         if (c > 0 && !lava[r][c - 1]) {           blocked1.erase(c - 1);         }       } else {         if (lava[(r + 1) % 2][c]) {           blocked2.insert(c);         }         if (c < n - 1 && lava[(r + 1) % 2][c + 1]) {           blocked1.insert(c);         }         if (c > 0 && lava[(r + 1) % 2][c - 1]) {           blocked1.insert(c - 1);         }       }       lava[r][c] = !lava[r][c];       if (blocked1.empty() && blocked2.empty() && !lava[0][0] &&           !lava[1][n - 1]) {         cout <<  Yes  << endl;       } else {         cout <<  No  << endl;       }     }   }


#include <bits/stdc++.h>   const long long maxn = 8e9 + 5;   long long v[10000000];   long long k[10000000];   long long n, sum;   int judge(long long m) {     long long i, sum1 = sum;     for (i = 0; i < n; i++) {       if (m * v[i] > k[i]) {         if (k[i] + sum1 < m * v[i])           break;         else if (k[i] + sum1 >= m * v[i])           sum1 = sum1 + k[i] - m * v[i];       }     }     if (i < n)       return 0;     else       return 1;   }   int main() {     long long l, r, mid;     scanf( %I64d%I64d , &n, &sum);     for (long long i = 0; i < n; i++) {       scanf( %I64d , &v[i]);     }     for (long long i = 0; i < n; i++) {       scanf( %I64d , &k[i]);     }     l = 0;     r = maxn;     while (l + 1 < r) {       mid = l + (r - l) / 2;       if (judge(mid)) {         l = mid;       } else         r = mid;     }     if (judge(l + 1))       printf( %I64d , l + 1);     else       printf( %I64d , l);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> g(n + 1);     for (int i = 0; i < n; i++) {       int u;       cin >> u;       g[i + 1] = u;     }     for (int i = 1; i <= n; i++) {       if (g[g[g[i]]] == i) {         cout <<  YES ;         return 0;       }     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 0x3f3f3f3f;   const double eps = 1e-9;   const int maxn = 100010;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, k;     double l, v1, v2;     cin >> n >> l >> v1 >> v2 >> k;     int t = (n + k - 1) / k;     if (t == 1) {       cout.precision(17);       cout << fixed << l / v2 <<   n ;     } else {       double y = l * (1 + v2 / v1) / (2.0 * t - 1 + v2 / v1);       double x = l - y;       double a = y / v2 + x / v1;       cout.precision(17);       cout << fixed << a <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long MOD(string s, long long c) {     long long ans = 0, p = 1;     for (int i = (((int)s.size()) - 1); i >= (0); i--) {       ans = (ans + (s[i] -  0 ) * p) % c;       p *= 10;       p %= c;     }     return ans;   }   long long mod(long long a, long long b, long long c) {     long long ans = 1;     while (b) {       if (b % 2) ans = ans * a % c;       a = a * a % c;       b /= 2;     }     return ans;   }   long long solve(long long a, string b, long long c) {     long long ans = 1;     for (int i = (((int)b.size()) - 1); i >= (0); i--) {       ans = ans * mod(a, (b[i] -  0 ), c) % c;       a = mod(a, 10, c);     }     return ans;   }   string dec(string s) {     for (int i = (((int)s.size()) - 1); i >= (0); i--) {       if (s[i] !=  0 ) {         s[i]--;         break;       } else {         s[i] =  9 ;       }     }     return s;   }   int main() {     string bb, n;     long long c;     cin >> bb >> n >> c;     long long b = MOD(bb, c);     long long ans = (b - 1 + c) * solve(b, dec(n), c) % c;     if (ans == 0) ans = c;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const double INF = 1e18;   const int MAX_N = 100100;   const int IT = 100;   const double PI = acos(-1.0);   int x[MAX_N];   int y[MAX_N];   long long dist(int a, int b) {     long long dx = x[a] - x[b];     long long dy = y[a] - y[b];     return dx * dx + dy * dy;   }   double dist1(double xx, double yy) {     xx -= x[0];     yy -= y[0];     return xx * xx + yy * yy;   }   double fun(int a, int b) {     double l = 0.0, r = 1.0;     for (int i = 0; i < IT; i++) {       double ml = l + (r - l) / 3.0;       double mr = r - (r - l) / 3.0;       double x1 = x[a] * ml + (1.0 - ml) * x[b];       double y1 = y[a] * ml + (1.0 - ml) * y[b];       double x2 = x[a] * mr + (1.0 - mr) * x[b];       double y2 = y[a] * mr + (1.0 - mr) * y[b];       if (dist1(x1, y1) > dist1(x2, y2)) {         l = ml;       } else {         r = mr;       }     }     double xx = x[a] * l + (1.0 - l) * x[b];     double yy = y[a] * l + (1.0 - l) * y[b];     return dist1(xx, yy);   }   void solve() {     int n;     scanf( %d%d%d , &n, &x[0], &y[0]);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &x[i], &y[i]);     }     double mn = INF;     double mx = 0;     for (int i = 1; i <= n; i++) {       int j = (i == n ? 1 : i + 1);       mx = max(mx, (double)dist(0, i));       mx = max(mx, (double)dist(0, i));       mn = min(mn, fun(i, j));     }     double answer = PI * (mx - mn);     printf( %.18lf n , answer);   }   int main() {     int cases;     cases = 1;     for (int i = 0; i < cases; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     vector<int> a(n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     sort(a.begin(), a.end());     for (auto i : a) {       if (i != a[0]) {         printf( %d , i);         return 0;       }     }     printf( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   int dp[maxn], ans;   const int inf = 1e9;   int a1, a2, adfds, fd;   vector<int> edge[maxn];   int n, m, d, num[maxn], fuck;   void dfs1(int u, int fa) {     int i;     for (i = 0; i < (int)edge[u].size(); i++) {       int v = edge[u][i];       if (v == fa) continue;       dfs1(v, u);       dp[u] = max(dp[u], dp[v] + 1);     }     if (num[u]) dp[u] = max(dp[u], 0);   }   void dfs2(int u, int fa, int fv) {     int flag = 0, i, j, k;     if (fv + 1 > d) flag = 1;     int l1 = -inf, id1 = 0, l2 = -inf, id2 = 0;     for (i = 0; i < (int)edge[u].size(); i++) {       int v = edge[u][i];       if (v == fa) continue;       if (dp[v] + 1 > d) flag = 1;       if (dp[v] >= l1) {         l2 = l1, id2 = id1;         l1 = dp[v], id1 = v;       } else if (dp[v] > l2) {         l2 = dp[v], id2 = v;       }     }     if (!flag) ans++;     for (i = 0; i < (int)edge[u].size(); i++) {       int v = edge[u][i];       if (v == fa) continue;       int fvv = fv;       if (v == id1)         fvv = max(fvv, l2);       else         fvv = max(fvv, l1);       if (num[u]) fvv = max(fvv, -1);       dfs2(v, u, fvv + 1);     }   }   int main() {     int i, j, k, a, b, c;     ans = 0;     scanf( %d%d%d , &n, &m, &d);     for (i = 0; i <= n; i++) {       dp[i] = -inf;       edge[i].clear();     }     for (i = 1; i <= m; i++) {       scanf( %d , &a);       num[a] = 1;     }     for (i = 1; i < n; i++) {       scanf( %d%d , &a, &b);       edge[a].push_back(b);       edge[b].push_back(a);     }     dfs1(1, 0);     dfs2(1, 0, -inf);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   const double eps = 1e-9;   int n, t;   char A[N], B[N];   int main() {     scanf( %d %d n , &n, &t);     gets(A);     gets(B);     int sameCnt = 0;     for (int i = 0; i < n; ++i) {       if (A[i] == B[i]) ++sameCnt;     }     int dif = n - sameCnt;     if (dif / 2 + dif % 2 <= t) {       int wic = 0;       int have = 0;       t -= dif / 2 + dif % 2;       have = dif % 2;       if (t > sameCnt) {         have += 2 * (t - sameCnt);         t = sameCnt;       }       if (n == 1000) {       }       for (int i = 0; i < n; ++i) {         if (have && A[i] != B[i]) {           char tempval =  a ;           for (int j = 0; j < 26; ++j) {             if (tempval + j != A[i] && tempval + j != B[i]) {               printf( %c , tempval + j);               break;             }           }           have--;           continue;         }         if (A[i] != B[i]) {           if (wic)             printf( %c , A[i]);           else             printf( %c , B[i]);           wic = 1 - wic;           continue;         }         if (t > 0 && A[i] == B[i]) {           t--;           char temp = A[i] + 1;           if (A[i] ==  z ) temp =  y ;           printf( %c , temp);         } else {           printf( %c , A[i]);         }       }       printf(  n );     } else {       cout <<  -1  << endl;     }     return 0;   }


#include <bits/stdc++.h>      using namespace std;   const int c=200002, k=20;   int n, q, p[c], szint[c], fel[c][k], f[c], db[c];   long long aa[c], bb[c], dist[c], val[c], sum;   bool v[c];   set<int> s;   int lca(int a, int b) {       if (szint[a]<szint[b]) {           swap(a, b);       }       for (int i=k-1; i>=0; i--) {           if (szint[fel[a][i]]>=szint[b]) {               a=fel[a][i];           }       }       if (a==b) {           return a;       }       for (int i=k-1; i>=0; i--) {           if (fel[a][i]!=fel[b][i]) {               a=fel[a][i], b=fel[b][i];           }       }       return f[a];   }   long long tav(int a, int b) {       int koz=lca(a, b);       return dist[a]+dist[b]-2*dist[koz];   }   void keres(int a, int &l, int &r) {       auto it=s.find(a);       --it;       l=*it;       ++it, ++it;       if (it!=s.end()) {           r=*it;       }   }   void add(int a) {       s.insert(a);       int l=0, r=0;       keres(a, l, r);       sum+=tav(l, a)+tav(a, r)-tav(l, r);   }   void cl(int a) {       int l=0, r=0;       keres(a, l, r);       sum-=tav(l, a)+tav(a, r)-tav(l, r);       s.erase(a);   }   int main()   {       ios_base::sync_with_stdio(false);       cin >> n >> q;       p[0]=n+1;       for (int i=1; i<=n; i++) {           cin >> p[i];           f[i]=i-1;           while (p[i]>p[f[i]]) {               f[i]=f[f[i]];           }           szint[i]=szint[f[i]]+1;           fel[i][0]=f[i];           for (int j=1; j<k; j++) {               fel[i][j]=fel[fel[i][j-1]][j-1];           }       }       for (int i=1; i<=n; i++) {           cin >> aa[i];           val[i]+=aa[i];       }       for (int i=1; i<=n; i++) {           cin >> bb[i];           val[i]-=bb[i];           val[f[i]]+=bb[i];       }       for (int i=n; i>=1; i--) {           if (val[i]<0) {               val[f[i]]+=val[i];               val[i]=0;           }       }       for (int i=1; i<=n; i++) {           dist[i]=dist[f[i]]+val[i];       }       db[0]=1;       s.insert(0);       for (int i=1; i<=q; i++) {           int x; cin >> x;           if (!v[x]) {               v[x]=1;               if (++db[f[x]]==1) {                   add(f[x]);               }           } else {               v[x]=0;               if (--db[f[x]]==0) {                   cl(f[x]);               }           }           cout << sum/2+val[0] <<   n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 400005;   map<int, int> H;   int n, m, p, l, r, s, tot, T, a[N], b[N], c[N], ans[N];   int get() {     int v;     scanf( %d , &v);     if (!H[v]) H[v] = ++tot;     return H[v];   }   void add(int x) {     if (c[x]++ < 0)       --s;     else       ++s;   }   void del(int x) {     if (c[x]-- > 0)       --s;     else       ++s;   }   int main() {     scanf( %d%d%d , &n, &m, &p);     for (int i = 0; i < n; ++i) a[i] = get();     for (int i = 0; i < m; ++i) b[i] = get(), --c[b[i]];     for (int i = 0; i < p; ++i) {       l = i, r = i, s = m;       for (int j = 0; j < m - 1; ++j) {         add(a[r]), r += p;         if (r >= n) break;       }       while (r <= n) {         add(a[r]), r += p;         if (!s) ans[T++] = l;         del(a[l]), l += p;       }       while (l <= n) del(a[l]), l += p;     }     printf( %d n , T);     sort(ans, ans + T);     for (int i = 0; i < T; ++i) printf( %d  , ans[i] + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e9;   struct comparator {     bool operator()(const pair<int, int> &a, const pair<int, int> &b) {       return a.first > b.first;     }   } cmp;   int main() {     char in;     vector<pair<int, int> > white, black;     int board[8][8];     for (int i = 0; i < 8; i++) {       for (int j = 0; j < 8; j++) {         board[i][j] = 0;       }     }     for (int i = 0; i < 8; i++) {       for (int j = 0; j < 8; j++) {         cin >> in;         if (in ==  W ) {           white.push_back(pair<int, int>(i, j));           board[i][j] = 1;         }         if (in ==  B ) {           black.push_back(pair<int, int>(i, j));           board[i][j] = 2;         }       }     }     sort(white.begin(), white.end());     sort(black.begin(), black.end(), cmp);     int i = 0, j = 0, x;     if (white[i].first == 0) {       cout <<  A  << endl;       return 0;     }     int pos_A = white[i].first;     int pos_B = black[j].first;     if (pos_A - 1 == 0 && board[pos_A - 1][white[i].second] == 0) {       cout <<  A  << endl;       return 0;     }     while (pos_A != 0) {       pos_A--;       if (board[pos_A][white[i].second] != 0) {         i++;         pos_A = white[i].first;         continue;       }     }     while (pos_B != 7) {       pos_B++;       if (board[pos_B][black[j].second] != 0) {         j++;         pos_B = black[j].first;         continue;       }     }     pos_A = white[i].first;     pos_B = black[j].first;     if (pos_A - (7 - pos_B) <= 0) {       cout <<  A  << endl;     } else {       cout <<  B  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long i, j, k, n;     string s, s1, b;     cin >> n >> k >> s;     b = s;     for (i = k; i < n; i++) b[i] = b[i - k];     if (b >= s)       cout << n << endl << b << endl;     else {       for (i = k - 1; i >= 0; i--) {         if (b[i] ==  9 )           b[i] =  0 ;         else {           b[i]++;           break;         }       }       for (i = k; i < n; i++) b[i] = b[i - k];       cout << n << endl << b << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   bool cmp(int u, int y) { return u > y; }   int main() {     int i, j;     int n, m, x;     int a[100009], b[100009];     int cnt;     while (scanf( %d %d , &n, &x) != EOF) {       for (i = 0; i < n; i++) scanf( %d , &a[i]);       sort(a, a + n, cmp);       for (i = 0; i < n; i++) scanf( %d , &b[i]);       sort(b, b + n);       cnt = 0;       int k = 0;       for (i = 0; i < n; i++) {         for (j = k; j < n; j++) {           if (a[i] != -0x3fffffff && b[j] != -0x3fffffff) {             if (a[i] + b[j] >= x) {               a[i] = -0x3fffffff;               b[j] = -0x3fffffff;               cnt++;               k = j;               break;             }           }         }       }       printf( 1 %d n , cnt);     }   }


#include <bits/stdc++.h>   using namespace std;   int find(string s, string sub, int begin, int end) {     for (int i = begin; i < end - 1; i++)       if (s[i] == sub[0] && s[i + 1] == sub[1]) return i;     return -1;   }   int main() {     string s, sub;     cin >> s;     int size = s.size();     int index1, index2, index3, index4;     index1 = find(s,  AB , 0, size);     index2 = find(s,  BA , 0, size);     index3 = find(s,  BA , index1 + 2, size);     index4 = find(s,  AB , index2 + 2, size);     if ((index1 + 1 && index3 + 1) || (index2 + 1 && index4 + 1))       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     std::ios::sync_with_stdio(false);     string a;     cin >> a;     for (int i = 0; i < a.size(); i++) {       if (((int)(a[i]) >= 48 && (int)(a[i]) <= 57) ||           ((int)(a[i]) >= 97 && (int)(a[i]) <= 122) || (int)(a[i]) == 95 ||           (int)(a[i]) == 64 || (int)(a[i]) == 46 || (int)(a[i]) == 47 ||           ((int)(a[i]) >= 65 && (int)(a[i]) <= 90)) {         continue;       } else {         cout <<  NO ;         return 0;       }     }     string d =   ;     int ans1 = -1;     for (int i = 0; i < a.size(); i++) {       if (a[i] ==  @ ) {         ans1 = i;         break;       }       d += a[i];     }     if (ans1 == 0 || ans1 == -1) {       cout <<  NO ;       return 0;     }     for (int i = 0; i < d.size(); i++) {       if (d[i] ==  .  || d[i] ==  / ) {         cout <<  NO ;         return 0;       }     }     string e =   ;     int ans2 = -1;     for (int i = ans1 + 1; i < a.size(); i++) {       if (a[i] ==  / ) {         ans2 = i;         break;       }       e += a[i];     }     if (e.size() == 0) {       cout <<  NO ;       return 0;     }     if (ans2 - ans1 == 1) {       cout <<  NO ;       return 0;     }     int ans3 = 0;     for (int i = 0; i < e.size(); i++) {       if (e[i] ==  @ ) {         cout <<  NO ;         return 0;       }       if (e[i] ==  . ) {         ans3++;       }     }     if (e[e.size() - 1] ==  .  || e[0] ==  . ) {       cout <<  NO ;       return 0;     }     for (int i = 0; i < e.size() - 1; i++) {       if (e[i] ==  .  && e[i + 1] ==  . ) {         cout <<  NO ;         return 0;       }     }     if (ans2 == -1) {       cout <<  YES ;       return 0;     }     string r =   ;     for (int i = ans2 + 1; i < a.size(); i++) {       if (a[i] ==  /  || a[i] ==  .  || a[i] ==  @ ) {         cout <<  NO ;         return 0;       }     }     if (ans2 == a.size() - 1) {       cout <<  NO ;       return 0;     }     cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int M = 1e9 + 7;   void madd(int& a, int b) {     a += b;     if (a >= M) a -= M;   }   int mult(int a, int b) { return (1LL * a * b) % M; }   int modexp(int a, int b) {     int res = 1;     while (b > 0) {       if (b & 1) res = mult(res, a);       a = mult(a, a);       b >>= 1;     }     return res;   }   int inverse(int x) { return modexp(x, M - 2); }   const int maxn = 1e5 + 5;   int n, a[maxn];   int cnt[maxn];   int mu[maxn];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       cnt[a[i]]++;     }     mu[1] = 1;     for (int i = 1; i < maxn; i++) {       for (int j = 2 * i; j < maxn; j += i) {         mu[j] -= mu[i];         cnt[i] += cnt[j];       }     }     int ans = 0;     for (int d = 1; d <= 100000; d++) {       madd(ans, mult((mu[d] + M) % M, modexp(2, cnt[d]) - 1));     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int x, d, nxt = 1, arr[10001] = {0}, pos = 0;     cin >> x >> d;     for (int j = 30; j >= 0; j--)       if (x >= (((long long int)1 << j))) {         x -= ((long long int)1 << j);         for (int i = 1; i <= j; i++) arr[pos++] = nxt;         nxt += d;         arr[pos++] = nxt;         nxt += d;       }     if (x == 0) {       cout << pos << endl;       for (int j = 0; j < pos; j++) cout << arr[j] <<    ;       cout << endl;     } else       cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Edge {     int v, f;     Edge *next, *rest;   };   int n, m, b, x[100], k[100], p[100];   long long dp[1048576];   long long ans = 0x3FFFFFFFFFFFFFFFLL;   int main() {     scanf( %d%d%d , &n, &m, &b);     for (int i = 0, len; i < n; i++) {       scanf( %d%d%d , &x[i], &k[i], &len);       for (int j = 0, t; j < len; j++) scanf( %d , &t), p[i] |= 1 << (t - 1);       p[i] ^= (1 << m) - 1;     }     for (int i = 0; i < n; i++) {       for (int j = n - 1; j > i; j--) {         if (k[j - 1] > k[j])           swap(k[j - 1], k[j]), swap(x[j - 1], x[j]), swap(p[j - 1], p[j]);       }     }     for (int i = 1; i < 1 << m; i++) dp[i] = 0x3FFFFFFFFFFFFFFFLL;     for (int i = 0; i < n; i++) {       for (int j = 0; j < 1 << m; j++) dp[j] = min(dp[j], dp[j & p[i]] + x[i]);       if (dp[(1 << m) - 1] < 0x3FFFFFFFFFFFFFFFLL)         ans = min(ans, dp[(1 << m) - 1] + (long long)k[i] * b);     }     printf( %I64d , ans == 0x3FFFFFFFFFFFFFFFLL ? -1 : ans);     getchar();     getchar();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[300005] = {0};   int c1 = 0, c2 = 0, p1 = 0, p2 = 0;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     int maxn = -1;     for (int i = 1; i <= n; i++) {       if (c1 == 0) {         p1 = i;         c1 = a[i];       } else if (c2 == 0 && a[i] != c1) {         p2 = i;         c2 = a[i];         maxn = max(maxn, p2 - p1);       } else if (a[i] == c1 && p2 != 0) {         maxn = max(maxn, i - p2);       } else if (a[i] == c2) {         maxn = max(maxn, i - p1);       } else if (a[i] != c1 && a[i] != c2) {         maxn = max(maxn, i - p1);       }     }     cout << maxn << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T gcd(T a, T b) {     return (b != 0 ? gcd<T>(b, a % b) : a);   }   template <class T>   T lcm(T a, T b) {     return (a / gcd<T>(a, b) * b);   }   template <class T>   inline bool read(T &x) {     int c = getchar();     int sgn = 1;     while (~c && c <  0  || c >  9 ) {       if (c ==  - ) sgn = -1;       c = getchar();     }     for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ;     x *= sgn;     return ~c;   }   const int INF = 2000000009;   const int MX = 200005;   const double EPS = 1e-9;   const int MOD = 1000000007;   long long a[MX];   vector<long long> sq, nsq;   int n, p, q;   long long solve1() {     vector<long long> cost;     for (int i = 0; i < q; i++) {       long long t = sqrt(nsq[i]);       cost.push_back(min(nsq[i] - t * t, (t + 1) * (t + 1) - nsq[i]));     }     sort(cost.begin(), cost.end());     long long ret = 0;     int t = (n / 2) - p;     for (int i = 0; i < t; i++) ret += cost[i];     return ret;   }   long long solve2() {     long long ret = 0;     vector<long long> cost;     for (int i = 0; i < p; i++) {       if (sq[i] == 0)         cost.push_back(2LL);       else         cost.push_back(1LL);     }     sort(cost.begin(), cost.end());     int t = (n / 2) - q;     for (int i = 0; i < t; i++) ret += cost[i];     return ret;   }   int main() {     n = ({       int a;       read(a);       a;     });     for (int i = 0; i < n; i++) {       a[i] = ({         long long a;         read(a);         a;       });       long long t = sqrt(a[i]);       if (t * t == a[i])         sq.push_back(a[i]);       else         nsq.push_back(a[i]);     }     p = (int)sq.size();     q = (int)nsq.size();     if (p == q) {       printf( 0 n );       return 0;     }     if (q > p) {       printf( %lld n , solve1());     } else {       printf( %lld n , solve2());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   vector<int> E[200020], V;   int dis[2][200020];   void solve() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= k; i++) {       int x;       scanf( %d , &x);       V.push_back(x);     }     for (int i = 0; i < (m); i++) {       int x, y;       scanf( %d%d , &x, &y);       E[x].push_back(y);       E[y].push_back(x);     }     for (int v = 0; v < (2); v++) {       int st = (v == 0 ? 1 : n);       vector<int> q;       auto D = dis[v];       for (int i = 1; i <= n; i++) D[i] = -1;       D[st] = 0;       q.push_back(st);       for (int i = 0; i < ((int)(q).size()); i++) {         int t = q[i];         for (int e : E[t])           if (D[e] == -1) {             D[e] = D[t] + 1;             q.push_back(e);           }       }     }     sort((V).begin(), (V).end(),          [&](int x, int y) { return dis[0][x] > dis[0][y]; });     int mx = -1e9, ans = 0;     for (int i = 0; i + 1 < (int)(V).size(); i++) {       if (dis[0][V[i]] == dis[0][V[i + 1]]) {         printf( %d n , dis[0][n]);         return;       }     }     for (int i = 0; i < ((int)(V).size()); i++) {       int u = V[i];       int val = min({dis[0][n], dis[0][u] + dis[1][u], dis[0][u] + 1 + mx});       ans = max(ans, val);       mx = max(mx, dis[1][u]);     }     printf( %d n , ans);   }   int main() {     int T = 1;     for (int t = 1; t <= T; t++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); }   double pi = 3.141592653589793238462643;   int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};   int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};   inline int add(int a, int b, int m = 1000000007) {     a += b;     if (a >= m) a -= m;     return a;   }   inline int mul(int a, int b, int m = 1000000007) {     return (int)(((long long)a * (long long)b) % m);   }   inline bool ispalin(string &str) {     int n = str.length();     for (int i = 0; i < n / 2; i++)       if (str[i] != str[n - i - 1]) return false;     return true;   }   long long expo(long long base, long long pow) {     long long ans = 1;     while (pow != 0) {       if (pow & 1 == 1) {         ans = ans * base;         ans = ans % 1000000007;       }       base *= base;       base %= 1000000007;       pow /= 2;     }     return ans;   }   void swapp(int *a, int *b) {     int temp = *a;     *a = *b;     *b = temp;   }   long long invt(long long x) { return expo(x, 1000000007 - 2); }   int expFactor(int n, int p) {     int x = p;     int exponent = 0;     while ((n / x) > 0) {       exponent += n / x;       x *= p;     }     return exponent;   }   inline int countsetbit(int n) {     unsigned int count = 0;     while (n) {       n &= (n - 1);       count++;     }     return count;   }   inline int abs(int x) {     if (x < 0) return -x;     return x;   }   bool isPal(string ss) {     int len = ss.length();     for (int i = 0; i < len / 2; i++) {       int comp = len - i - 1;       if (ss[i] != ss[comp]) return false;     }     return true;   }   vector<long long> fact, inv;   void calfal(int maxn) {     fact.resize(maxn);     inv.resize(maxn);     fact[0] = inv[0] = inv[1] = 1;     for (int i = (1); i <= (maxn - 1); ++i)       fact[i] = i * fact[i - 1] % 1000000007;     for (int i = (2); i <= (maxn - 1); ++i)       inv[i] = (1000000007 - 1000000007 / i) * inv[1000000007 % i] % 1000000007;     for (int i = (1); i <= (maxn - 1); ++i)       inv[i] = inv[i] * inv[i - 1] % 1000000007;   }   long long ncr(int n, int r) {     if (r > n) return 0;     return (fact[n] * inv[r] % 1000000007) * inv[n - r] % 1000000007;   }   long long _sieve_size;   bitset<10000010> bs;   vector<int> primes;   void sieve(long long upperbound) {     _sieve_size = upperbound + 1;     bs.set();     bs[0] = bs[1] = 0;     for (long long i = 2; i <= _sieve_size; i++)       if (bs[i]) {         for (long long j = i * i; j <= _sieve_size; j += i) bs[j] = 0;         primes.push_back((int)i);       }   }   bool isPrime(long long N) {     if (N <= _sieve_size) return bs[N];     for (int i = 0; i < (int)primes.size(); i++)       if (N % primes[i] == 0) return false;     return true;   }   double dpw[1001][1001][2], dpl1[1001][1001][2], dp[1001][1001][2];   int w, b;   double fun(int turn, int ww, int bb) {     if (ww >= w) {       return 0;     }     if (bb > b) {       return 0;     }     if (turn == 1 && bb >= b) {       return 0;     }     if (dp[ww][bb][turn] != -1) {       return dp[ww][bb][turn];     }     if (turn) {       double x, y;       x = (w - ww) * 1.0 / (b - bb - 1 + w - ww);       y = ((b - bb - 1) * 1.0 / (b - bb - 1 + w - ww));       if (bb == b - 1) {         y = 0;       }       dp[ww][bb][turn] = (y * fun((turn + 1) % 2, ww, bb + 2) +                           x * fun((turn + 1) % 2, ww + 1, bb + 1)) *                          ((b - bb) * 1.0 / (b - bb + w - ww));     } else {       dp[ww][bb][turn] =           (w - ww) * 1.0 / (b - bb + w - ww) +           fun((turn + 1) % 2, ww, bb + 1) * ((b - bb) * 1.0 / (b - bb + w - ww));     }     return dp[ww][bb][turn];   }   int main() {     cin >> w >> b;     if (!w) {       cout << 0 << endl;       return 0;     }     for (int i = (0); i <= (1); ++i) {       for (int j = (0); j <= (w + 1); ++j) {         for (int k = (0); k <= (b + 1); ++k) {           dp[j][k][i] = -1;         }       }     }     printf( %.10f , fun(0, 0, 0));   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   int n;   int b[MAXN];   set<pair<int, int>, greater<pair<int, int> > > s;   int ans[MAXN];   int main() {     scanf( %d , &n);     for (auto i = 0; i != n; i++) scanf( %d , &b[i]);     for (auto i = 0; i != n; i++) {       int j = (i + 1) % n;       int delta = abs(n - b[i] - b[j]);       s.insert(make_pair(delta, i));     }     int l = 0, r = n - 1;     while (!s.empty()) {       int pos = s.begin()->second;       s.erase(s.begin());       int j = (pos + 1) % n;       if (n - b[pos] > b[j]) {         ans[pos] = l++;       } else {         ans[pos] = r--;       }     }     for (auto i = 0; i != n; i++) printf( %d  , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N = 0;     cin >> N;     for (int i = 0; i < N; i++) {       int length, height;       cin >> length >> height;       int pixL, pixH;       cin >> pixL >> pixH;       int ans = max(max(height - pixH - 1, pixH) * length,                     max(length - pixL - 1, pixL) * height);       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > p, pt;   int n, m;   void getanswer(int l, int r) {     if (l >= r) return;     getanswer(l, (l + r) / 2);     getanswer(1 + ((l + r) / 2), r);     for (int i = int(l); i <= int(r); ++i) {       m++;       pt.push_back(make_pair(p[(l + r) / 2].first, p[i].second));     }   }   int main() {     scanf( %I32d , &n);     p.resize(n);     pt.resize(n);     for (int i = 0; i < int(n); i++)       scanf( %I32d%I32d , &p[i].first, &p[i].second);     stable_sort(p.begin(), p.end());     for (int i = 0; i < int(n); i++) pt[i] = p[i];     m = n;     getanswer(0, n - 1);     stable_sort(pt.begin(), pt.end());     p.clear();     for (int i = 0; i < int(m); i++)       if ((i == 0) || (pt[i] != pt[i - 1])) p.push_back(pt[i]);     printf( %I32d n , (int)p.size());     for (int i = 0; i < int(p.size()); i++)       printf( %I32d %I32d n , p[i].first, p[i].second);     return 0;   }


#include <bits/stdc++.h>   const int maxn = 8005;   const int inf = 0x3f3f3f3f;   const long long mod = 998244353;   using namespace std;   long long vis[111];   int main() {     long long x = 1ll, y;     int n, q;     scanf( %d %d , &n, &q);     for (int i = 1; i <= n; ++i) {       scanf( %lld , &y);       for (int j = 0; j <= 33; ++j) {         if ((x << j) == y) {           ++vis[j];           break;         }       }     }     for (int i = 1; i <= q; ++i) {       scanf( %lld , &y);       long long cnt = 0;       for (int j = 33; j >= 0; --j) {         cnt += min(y / (x << j), vis[j]);         y -= (x << j) * min(y / (x << j), vis[j]);       }       if (y) cnt = -1;       printf( %lld n , cnt);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     char sym[] = { A ,  O ,  M ,  H ,  I ,  o ,  T ,  V ,                    W ,  U ,  X ,  Y ,  x ,  w ,  v };     map<char, char> mp;     mp[ b ] =  d ;     mp[ d ] =  b ;     mp[ p ] =  q ;     mp[ q ] =  p ;     string s;     cin >> s;     int n = s.length();     int a = n / 2 - 1, b;     if (n & 1)       b = n / 2 + 1;     else       b = n / 2;     if (n & 1 && count(sym, sym + 16, s[n / 2]) == 0) {       cout <<  NIE ;       return 0;     }     for (int i = 0, j = n - 1; i <= a && j >= b; i++, j--) {       if (s[i] == s[j] && count(sym, sym + 16, s[i]) == 1) {         continue;       }       if (mp[s[i]] == s[j] && mp[s[j]] == s[i]) {         continue;       }       cout <<  NIE ;       return 0;     }     cout <<  TAK ;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9 + 7;   const pair<int, int> pinf = pair<int, int>(inf, inf);   int up(int n) {     int ret = 1;     while (ret < n) ret <<= 1;     return ret;   }   template <class T>   class segtree {     vector<T> arr;     T min(int l, int r, int cl, int cr, int pos) {       if (cr <= l || r <= cl) return pinf;       if (l <= cl && cr <= r) return arr[pos];       int ct = (cl + cr) / 2;       return std::min(min(l, r, cl, ct, 2 * pos), min(l, r, ct, cr, 2 * pos + 1));     }     void pset(int pos, T x) {       arr[pos] = x;       while (pos /= 2) arr[pos] = std::min(arr[2 * pos], arr[2 * pos + 1]);     }       public:     segtree(int n) : arr(2 * up(n)) {}     T min(int l, int r) { return min(l, r, 0, arr.size() / 2, 1); }     void set(int pos, T x) { pset(pos + arr.size() / 2, x); }   };   class sumtree {     vector<int> arr;     int sum(int l, int r, int cl, int cr, int pos) {       if (cr <= l || r <= cl) return 0;       if (l <= cl && cr <= r) return arr[pos];       int ct = (cl + cr) / 2;       return sum(l, r, cl, ct, 2 * pos) + sum(l, r, ct, cr, 2 * pos + 1);     }     void pset(int pos, int x) {       arr[pos] = x;       while (pos /= 2) arr[pos] = arr[2 * pos] + arr[2 * pos + 1];     }       public:     sumtree(int n) : arr(2 * up(n)) {}     int sum(int l, int r) { return sum(l, r, 0, arr.size() / 2, 1); }     void set(int p, int x) { pset(p + arr.size() / 2, x); }   };   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     vector<int> arr(n);     for (auto &it : arr) cin >> it;     segtree<pair<int, int>> st(n);     sumtree cards(n);     for (int i = 0; i < arr.size(); i++) st.set(i, {arr[i], i});     long long cnt = 0;     int pos = 0;     sort(arr.begin(), arr.end());     for (int i = 0; i < n; i++) {       auto q = st.min(pos, n);       if (q.first == arr[i]) {         cnt += q.second - pos - cards.sum(pos, q.second);         pos = q.second;         st.set(pos, pinf);         cards.set(pos, 1);       } else {         cnt += n - pos - cards.sum(pos, n);         i--;         pos = 0;       }     }     cout << cnt + n << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int da[550];   int dp[550][1050];   int dp2[550][550];   struct kutija {     int in, out, w, s, v;   } K[505];   bool cmp(const kutija &a, const kutija &b) {     if (a.in != b.in) return a.in < b.in;     if (a.out != b.out) return a.out > b.out;     return a.s > b.s;   }   int rek(int x, int g) {     if (g < 0) return -1e9;     if (x == n + 1) return 0;     if (dp[x][g] != -1) return dp[x][g];     memset(dp2[x], 0, sizeof dp2[x]);     for (int i = x + 1; i <= n; i++) {       dp2[x][i + 1] = max(dp2[x][i + 1], dp2[x][i]);       if (K[i].in >= K[x].out || K[i].out > K[x].out) continue;       dp2[x][da[i]] = max(dp2[x][da[i]],                           dp2[x][i] + K[i].v + rek(i, min(K[i].s, g - K[i].w)));     }     dp[x][g] = dp2[x][n + 1];     return dp2[x][n + 1];   }   int main(void) {     scanf( %d%d , &n, &K[0].s);     K[0].in = 0;     K[0].out = 2 * n;     K[0].w = 0;     K[0].v = 0;     for (int i = 1; i <= n; i++)       scanf( %d%d%d%d%d , &K[i].in, &K[i].out, &K[i].w, &K[i].s, &K[i].v);     sort(K, K + n + 1, cmp);     memset(dp, -1, sizeof dp);     for (int i = 0; i <= n; i++) {       da[i] = n + 1;       for (int j = 0; j < n + 1; j++) {         if (K[i].out <= K[j].in) {           da[i] = j;           break;         }       }     }     printf( %d , rek(0, K[0].s));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[5][8];   int vis[123124];   struct Mp {     int a[30];     Mp() { memset(a, 0, sizeof(a)); }     bool operator<(const Mp &x) const {       for (int i = 0; i < 25; ++i)         if (x.a[i] != a[i]) return a[i] < x.a[i];       return 0;     }     void swap(int &a, int &b, int &c, int &d) {       int t = a;       a = b;       b = c;       c = d;       d = t;     }     Mp Rotate1() {       Mp res = *this;       swap(res.a[1], res.a[3], res.a[4], res.a[2]);       swap(res.a[13], res.a[5], res.a[17], res.a[21]);       swap(res.a[14], res.a[6], res.a[18], res.a[22]);       swap(res.a[15], res.a[7], res.a[19], res.a[23]);       swap(res.a[16], res.a[8], res.a[20], res.a[24]);       swap(res.a[9], res.a[10], res.a[12], res.a[11]);       return res;     }     Mp Rotate2() {       Mp res = *this;       swap(res.a[1], res.a[3], res.a[4], res.a[2]);       swap(res.a[13], res.a[5], res.a[17], res.a[21]);       swap(res.a[14], res.a[6], res.a[18], res.a[22]);       return res;     }     Mp Rotate3() {       Mp res = *this;       swap(res.a[2], res.a[23], res.a[10], res.a[6]);       swap(res.a[4], res.a[21], res.a[12], res.a[8]);       swap(res.a[17], res.a[18], res.a[20], res.a[19]);       return res;     }     bool check() {       for (int i = 1; i <= 24; i += 4) {         for (int j = i + 1; j <= i + 3; ++j)           if (a[j] != a[i]) return 0;       }       return 1;     }     void OUT() {       for (int i = 1; i <= 24; ++i) cout << a[i] <<    ;       cout << endl;     }   };   map<Mp, bool> hs;   int main() {     Mp init;     for (int i = 1; i <= 24; ++i) cin >> init.a[i];     queue<Mp> Q;     Q.push(init);     while (!Q.empty()) {       Mp now = Q.front();       Q.pop();       Mp news;       news = now.Rotate1();       if (!hs[news]) {         hs[news] = 1;         Q.push(news);       }       news = now.Rotate2();       if (news.check()) {         puts( yes );         return 0;       }       news = now.Rotate2().Rotate2().Rotate2();       if (news.check()) {         puts( yes );         return 0;       }       news = now.Rotate3();       if (news.check()) {         puts( yes );         return 0;       }       news = now.Rotate3().Rotate3().Rotate3();       if (news.check()) {         puts( yes );         return 0;       }     }     puts( no );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     long long a[n], b[n], c[n];     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     for (long long i = 0; i < n; i++) cin >> b[i];     for (long long i = 0; i < n; i++) {       c[i] = (a[i] - b[i]);     }     sort(c, c + n);     long long answer = 0, answer1 = 0;     for (long long i = 0; i < n; i++) {       long long ans = a[i] - b[i];       long long sum = 1 - ans;       if (ans > 0) answer1++;       long long ss = lower_bound(c, c + n, sum) - c;       answer += (n - ss);     }     answer -= answer1;     answer /= 2;     cout << answer << endl;     return 0;   }


#include <bits/stdc++.h>   const int oo = 2139063143;   const int N = 101000;   const int P = 1000000007;   using namespace std;   inline void sc(int &x) {     x = 0;     static int p;     p = 1;     static char c;     c = getchar();     while (!isdigit(c)) {       if (c ==  - ) p = -1;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + (c - 48);       c = getchar();     }     x *= p;   }   inline void print(int x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10) print(x / 10);     putchar(x % 10 +  0 );   }   inline void pr(int x) { print(x), putchar(  n ); }   int n, k;   int vis[N], cl = 1, cr = 0, ans = 0;   int a[N];   void add(int w, int d) {     if (!vis[w] && d == 1)       ++ans;     else if (vis[w] == 1 && d == -1)       --ans;     vis[w] += d;   }   int calc(int l, int r) {     while (cr < r) ++cr, add(a[cr], 1);     while (cl > l) --cl, add(a[cl], 1);     while (cr > r) add(a[cr], -1), --cr;     while (cl < l) add(a[cl], -1), ++cl;     return ans;   }   int f[N], g[N];   void solve(int wl, int wr, int l, int r) {     int mid = (l + r) >> 1, pos = 0;     f[mid] = 0;     for (int i = wl; i <= min(wr, mid - 1); i++) {       int sb = g[i] + calc(i + 1, mid);       if (sb > f[mid]) f[mid] = sb, pos = i;     }     if (l == r) return;     if (l < mid) solve(wl, pos, l, mid - 1);     if (r > mid) solve(pos, wr, mid + 1, r);   }   int las[N];   int main() {     sc(n), sc(k);     for (int i = 1; i <= n; i++) sc(a[i]);     for (int i = 1; i <= k; i++) {       solve(0, n, 1, n);       memcpy(g, f, sizeof(int) * (n + 1));     }     pr(f[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, ans;   int x[100010], y[100010];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &x[i]);     }     for (int i = 1; i <= m; i++) {       scanf( %d , &y[i]);     }     long long sum1 = 0, sum2 = 0;     for (int r1 = 0, r2 = 0; r1 <= n, r2 <= m;) {       while (sum1 < sum2 || r1 == 0) {         r1++;         sum1 += x[r1];       }       while (sum1 > sum2 || r2 == 0) {         r2++;         sum2 += y[r2];       }       if (sum1 == sum2) {         ans++;         r1++;         r2++;         sum1 = x[r1];         sum2 = y[r2];       }     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     char ch;     int arr[5001], brr[5001], mdays[5001], fdays[5001];     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> ch >> arr[i] >> brr[i];       if (ch ==  M ) {         for (int j = arr[i]; j <= brr[i]; j++) mdays[j]++;       } else {         for (int j = arr[i]; j <= brr[i]; j++) fdays[j]++;       }     }     int mx = 0;     for (int i = 1; i <= 366; i++) {       mx = max(mx, min(mdays[i], fdays[i]));     }     mx *= 2;     cout << mx << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sq = (int)sqrt((int)1e6);   const int maxnb = (int)1e9 + 7;   const double pi = acos(-1.0);   const int maxnl = 100500;   const double eps = 1e-9;   set<pair<long long, int> > se;   int n, pos[50500];   char c[50500];   long long price;   string s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> s;     int n = s.size(), bal = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  ( ) bal++;       if (s[i] ==  ) ) bal--;       if (s[i] ==  ? ) {         long long a, b;         cin >> a >> b;         se.insert(make_pair(a - b, i));         price += b;         s[i] =  ) ;         bal--;       }       while (bal < 0) {         if (!se.size()) {           cout << -1;           return 0;         }         pair<long long, int> p = *se.begin();         price += p.first;         s[p.second] =  ( ;         bal += 2;         se.erase(se.begin());       }     }     if (bal != 0) {       cout << -1;       return 0;     }     cout << price <<   n ;     for (int i = 0; i < n; i++) {       cout << s[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 10;   string s;   int n, len;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> s;     len = s.size();     for (int i = 0; i < len; i++)       if (s[i] ==  a ) n++;     if (len / 2 < n) {       cout << len;       return 0;     }     cout << 2 * n - 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k, ans(0);     bool flg = true;     cin >> n >> m >> k;     int ar[n * m];     for (int a = 0; a < n * m; a++) cin >> ar[a];     sort(ar, ar + n * m);     int median = (n * m % 2) ? ar[n * m / 2] : ar[(n * m + 1) / 2];     for (int a = 0; a < n * m; a++) {       if (abs(median - ar[a]) % k != 0) flg = false;       ans += abs(median - ar[a]) / k;     }     if (flg)       cout << ans;     else       cout << -1;   }


#include <bits/stdc++.h>   int main() {     int n;     scanf( %d , &n);     int a[5005], ans = 1, max = 0;     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 1; i < n; i++)       for (int j = 0; j < n - i; j++)         if (a[j] > a[j + 1]) {           int t = a[j];           a[j] = a[j + 1];           a[j + 1] = t;         }     for (int i = n - 1; i >= 0; i--) {       if (a[i] == a[i - 1])         ans++;       else {         if (ans > max) {           max = ans;         }         ans = 1;       }     }     if (max == 0)       printf( 1 n );     else       printf( %d n , max);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     vector<string> a(n + 1), b(n + 1);     vector<int> c(n + 1);     vector<long long> f1(n + 1), f2(n + 1);     for (int i = 1; i <= n; ++i) {       cin >> c[i];     }     for (int i = 1; i <= n; ++i) {       cin >> a[i];       b[i] = a[i];       reverse(b[i].begin(), b[i].end());     }     for (int i = 1; i <= n; ++i) {       f1[i] = f2[i] = LLONG_MAX;       if (f1[i - 1] != LLONG_MAX) {         if (a[i] >= a[i - 1]) {           f1[i] = min(f1[i], f1[i - 1]);         }         if (b[i] >= a[i - 1]) {           f2[i] = min(f2[i], f1[i - 1] + c[i]);         }       }       if (f2[i - 1] != LLONG_MAX) {         if (a[i] >= b[i - 1]) {           f1[i] = min(f1[i], f2[i - 1]);         }         if (b[i] >= b[i - 1]) {           f2[i] = min(f2[i], f2[i - 1] + c[i]);         }       }     }     if (f1[n] == LLONG_MAX && f2[n] == LLONG_MAX) {       cout << -1;     } else {       cout << min(f1[n], f2[n]);     }   }


#include <bits/stdc++.h>   using namespace std;   const int NN = 1010;   const int MM = 101010;   struct Node {     int id;     int x, y;   } Q[MM];   int a[NN][NN];   vector<int> adj[MM];   int ans[MM], f[MM];   int n, m, k;   void dfs(int u, int fa = 0, int crr = 0) {     if (Q[u].id == 1) {       int x = Q[u].x, y = Q[u].y;       if (!a[x][y]) {         a[x][y] = 1;         crr++;         f[u] = 1;       }     } else if (Q[u].id == 2) {       int x = Q[u].x, y = Q[u].y;       if (a[x][y]) {         a[x][y] = 0;         crr--;         f[u] = 1;       }     } else if (Q[u].id == 3) {       int x = Q[u].x;       for (int i = 1; i <= m; i++) {         if (a[x][i])           crr--;         else           crr++;         a[x][i] ^= 1;       }     }     ans[u] = crr;     for (int v : adj[u]) {       if (v == fa) continue;       dfs(v, u, ans[u]);     }     if (Q[u].id == 1) {       int x = Q[u].x, y = Q[u].y;       if (f[u]) a[x][y] = 0;     } else if (Q[u].id == 2) {       int x = Q[u].x, y = Q[u].y;       if (f[u]) a[x][y] = 1;     } else if (Q[u].id == 3) {       int x = Q[u].x;       for (int i = 1; i <= m; i++) a[x][i] ^= 1;     }   }   int main() {     cin >> n >> m >> k;     for (int i = 1; i <= k; i++) {       int id, x, y;       scanf( %d , &id);       if (id < 3) {         scanf( %d %d , &x, &y);         Q[i] = {id, x, y};         adj[i - 1].push_back(i);         adj[i].push_back(i - 1);         continue;       }       scanf( %d , &x);       if (id == 4) {         adj[x].push_back(i);         adj[i].push_back(x);       } else {         adj[i - 1].push_back(i);         adj[i].push_back(i - 1);       }       Q[i] = {id, x, 0};     }     dfs(0);     for (int i = 1; i <= k; i++) {       printf( %d n , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, q;   vector<int> gr[300001];   int par[300001], comp[300001];   int mx[300001][2];   int dfs(int s, int k) {     comp[s] = k;     mx[s][0] = mx[s][1] = 0;     for (int i = 0; i < gr[s].size(); i++) {       if (comp[gr[s][i]] == -1) {         int p = dfs(gr[s][i], k) + 1;         par[gr[s][i]] = s;         if (p > mx[s][0]) {           mx[s][1] = mx[s][0];           mx[s][0] = p;         } else if (p > mx[s][1])           mx[s][1] = p;       }     }     return mx[s][0];   }   int mark[300001];   void dfs2(int s, int d) {     mark[s] = 1;     if (d > mx[s][0]) {       mx[s][1] = mx[s][0];       mx[s][0] = d;     } else if (d > mx[s][1])       mx[s][1] = d;     for (int i = 0; i < gr[s].size(); i++) {       if (!mark[gr[s][i]]) {         if (mx[s][0] == (mx[gr[s][i]][0] + 1)) {           ;           dfs2(gr[s][i], mx[s][1] + 1);         } else           dfs2(gr[s][i], mx[s][0] + 1);       }     }   }   int mlen[300001];   int mpar[300001];   int get(int id) {     if (par[id] == id) return id;     return par[id] = get(par[id]);   }   void merge(int x, int y) { par[x] = get(par[y]); }   int main() {     cin >> n >> m >> q;     int a, b;     for (int i = 0; i < m; i++) {       scanf( %d%d , &a, &b);       gr[a - 1].push_back(b - 1);       gr[b - 1].push_back(a - 1);     }     memset(comp, -1, sizeof comp);     int c = 0;     for (int i = 0; i < n; i++) {       if (comp[i] == -1) {         dfs(i, i);       }     }     memset(mark, 0, sizeof mark);     for (int i = 0; i < n; i++)       if (!mark[i]) dfs2(i, 0);     memset(mlen, -1, sizeof mlen);     for (int i = 0; i < n; i++) {       mlen[i] = mx[i][0] + mx[i][1];       mlen[comp[i]] = max(mlen[i], mlen[comp[i]]);       mpar[i] = mpar[comp[i]];       if (i == comp[i])         mpar[i] = i;       else if (mx[i][0] < mx[mpar[i]][0])         mpar[comp[i]] = i;     }     for (int i = 0; i < n; i++) {       mlen[i] = mlen[comp[i]];       mpar[i] = mpar[comp[i]];       par[i] = mpar[i];     }     for (int i = 0; i < q; i++) {       scanf( %d , &c);       if (c == 1) {         scanf( %d , &a);         printf( %d n , mlen[get(a - 1)]);       } else {         scanf( %d%d , &a, &b);         a = get(a - 1);         b = get(b - 1);         if (a != b) {           if (mx[a][0] >= mx[b][0]) {             merge(b, a);             mlen[a] = max(mlen[a], mx[a][0] + mx[b][0] + 1);             mx[a][0] = max(mx[a][0], mx[b][0] + 1);           } else {             merge(a, b);             mlen[b] = max(mlen[b], mx[b][0] + mx[a][0] + 1);           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   const int p = 1000000007;   int c[12][15], e[12][15], g[12][5000], h[12][5000];   int edge1[3000], next1[3000], first1[3000], deg1[3000], sum_edge1;   int edge2[30], next2[30], first2[30], deg2[30], sum_edge2;   int i, n1, n2, r, s, t, x, y;   inline int power(int x, int y) {     int s = 1;     for (int i = 30; i >= 0; i--) {       s = 1LL * s * s % p;       if (y & (1 << i)) s = 1LL * s * x % p;     }     return s;   }   inline void addedge1(int x, int y) {     sum_edge1++, edge1[sum_edge1] = y, next1[sum_edge1] = first1[x],                  first1[x] = sum_edge1, deg1[x]++;     return;   }   inline void addedge2(int x, int y) {     sum_edge2++, edge2[sum_edge2] = y, next2[sum_edge2] = first2[x],                  first2[x] = sum_edge2, deg2[x]++;     return;   }   inline int dfs1(int d, int x, int fx, int y, int fy) {     if (deg1[x] < deg2[y]) return 0;     int l = 0;     for (int j = 1; j <= n2; j++) e[d][j] = 0;     for (int j = first2[y]; j != 0; j = next2[j])       if (edge2[j] != fy) l++, e[d][edge2[j]] = l;     for (int i = 0; i < (1 << l); i++) g[d][i] = 0;     g[d][0] = 1;     for (int i = first1[x]; i != 0; i = next1[i])       if (edge1[i] != fx) {         for (int j = 0; j < (1 << l); j++) h[d][j] = g[d][j];         for (int j = first2[y]; j != 0; j = next2[j])           if (edge2[j] != fy)             c[d][e[d][edge2[j]]] = dfs1(d + 1, edge1[i], x, edge2[j], y);         for (int k = 1; k <= l; k++)           if (c[d][k])             for (int j = 0; j < (1 << l); j++)               if (!(j & (1 << (k - 1))))                 g[d][j | (1 << (k - 1))] =                     (g[d][j | (1 << (k - 1))] + 1LL * h[d][j] * c[d][k]) % p;       }     return g[d][(1 << l) - 1];   }   inline int dfs2(int d, int x, int fx, int y, int fy) {     if (deg2[x] < deg2[y]) return 0;     int l = 0;     for (int j = 1; j <= n2; j++) e[d][j] = 0;     for (int j = first2[y]; j != 0; j = next2[j])       if (edge2[j] != fy) l++, e[d][edge2[j]] = l;     for (int i = 0; i < (1 << l); i++) g[d][i] = 0;     g[d][0] = 1;     for (int i = first2[x]; i != 0; i = next2[i])       if (edge2[i] != fx) {         for (int j = 0; j < (1 << l); j++) h[d][j] = g[d][j];         for (int j = first2[y]; j != 0; j = next2[j])           if (edge2[j] != fy)             c[d][e[d][edge2[j]]] = dfs2(d + 1, edge2[i], x, edge2[j], y);         for (int k = 1; k <= l; k++)           if (c[d][k])             for (int j = 0; j < (1 << l); j++)               if (!(j & (1 << (k - 1))))                 g[d][j | (1 << (k - 1))] =                     (g[d][j | (1 << (k - 1))] + 1LL * h[d][j] * c[d][k]) % p;       }     return g[d][(1 << l) - 1];   }   inline void input() {     scanf( %d , &n1);     for (i = 1; i < n1; i++) {       scanf( %d%d , &x, &y);       addedge1(x, y), addedge1(y, x);     }     scanf( %d , &n2);     for (i = 1; i < n2; i++) {       scanf( %d%d , &x, &y);       addedge2(x, y), addedge2(y, x);     }     for (i = 1; i <= n2; i++)       if (deg2[i] > deg2[r]) r = i;     return;   }   int main() {     input();     for (i = 1; i <= n1; i++) s = (s + dfs1(0, i, 0, r, 0)) % p;     for (i = 1; i <= n2; i++) t = t + dfs2(0, i, 0, r, 0);     printf( %d , 1LL * s * power(t, p - 2) % p);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int i, n, d, e;     cin >> n >> d >> e;     e *= 5;     int min = n % e;     for (i = 0; i <= n / d; i++) {       if ((n - (i * d)) % e < min) min = (n - (i * d)) % e;     }     if ((n % e) % d < min) min = (n % e) % d;     cout << min;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 30;   struct edge {     int to, next;   } G[maxn << 1];   int h[maxn], si;   void add(int u, int v) {     G[si].to = v;     G[si].next = h[u];     h[u] = si++;   }   vector<int> p;   void dfs(int u, int f, int d) {     int is = 0;     for (int i = h[u]; ~i; i = G[i].next) {       int v = G[i].to;       if (v ^ f) {         dfs(v, u, d + 1);         is = 1;       }     }     if (!is) p.push_back(d);   }   int solve(int u) {     p.clear();     dfs(u, 1, 1);     int cur = -1;     sort(p.begin(), p.end());     for (int i = 0; i < p.size(); i++) {       if (cur < p[i])         cur = p[i];       else         cur++;     }     return cur;   }   int main() {     int n;     while (~scanf( %d , &n)) {       memset(h, -1, sizeof h);       si = 0;       int u, v;       for (int i = 0; i < n - 1; i++) {         scanf( %d%d , &u, &v);         add(u, v);         add(v, u);       }       int ans = 0;       for (int i = h[1]; ~i; i = G[i].next) ans = max(ans, solve(G[i].to));       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int K[3];   long long T[3];   queue<long long> waiting[3];   queue<pair<long long, long long> > events[3];   long long res = 0;   void ServeSomeone(long long t) {     for (int i = 0; i < 3; i++)       if (K[i] && !waiting[i].empty()) {         K[i]--;         events[i].push(make_pair(waiting[i].front(), t + T[i]));         waiting[i].pop();       }   }   void ProcessEvents(long long max_time) {     while (true) {       int mini = -1;       for (int i = 0; i < 3; i++)         if (!events[i].empty() && (mini == -1 || events[i].front().second <                                                      events[mini].front().second))           mini = i;       if (mini == -1) break;       if (events[mini].front().second > max_time) break;       pair<long long, long long> m = events[mini].front();       events[mini].pop();       K[mini]++;       if (mini == 2) {         res = max(res, m.second - m.first);       } else {         waiting[mini + 1].push(m.first);       }       ServeSomeone(m.second);     }   }   int main() {     cin >> K[0] >> K[1] >> K[2];     cin >> T[0] >> T[1] >> T[2];     int N;     cin >> N;     for (int i = 0; i < N; i++) {       long long next_arrival;       cin >> next_arrival;       ProcessEvents(next_arrival);       waiting[0].push(next_arrival);       ServeSomeone(next_arrival);     }     ProcessEvents(1000000000000000000LL);     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   int trie[64 * 100005][2], uni;   inline long long int bin_to_int(vector<long long int> vt) {     long long int ans = 0, hudai = 1;     while (vt.size()) {       ans += (hudai * vt[vt.size() - 1]);       hudai *= 2;       vt.pop_back();     }     return ans;   }   inline vector<long long int> int_to_bin(long long int n) {     vector<long long int> ans(64), tem;     while (n > 0) {       tem.push_back(n % 2);       n /= 2;     }     for (int i = 0; i < (tem.size()); i++) {       ans[63 - i] = tem[i];     }     return ans;   }   inline long long int search(vector<long long int> vt) {     int cur = 1;     vector<long long int> ans;     for (int i = 0; i < (64); i++) {       int id = (vt[i] + 1) % 2;       if (trie[cur][id]) {         ans.push_back(1);         cur = trie[cur][id];       } else {         ans.push_back(0);         cur = trie[cur][vt[i]];       }     }     return bin_to_int(ans);   }   inline void insert(vector<long long int> vt) {     int cur = 1;     for (int i = 0; i < (64); i++) {       int id = vt[i];       if (!trie[cur][id]) trie[cur][id] = ++uni;       cur = trie[cur][id];     }     return;   }   int main() {     int n;     scanf( %d , &n);     vector<long long int> vt;     uni = 1;     for (int i = 0; i < (n); i++) {       long long int xx;       scanf( %lld , &xx);       vt.push_back(xx);     }     vector<long long int> hudai;     hudai.push_back(0);     vt.push_back(0);     for (int i = 0; i < (n); i++)       hudai.push_back(hudai[hudai.size() - 1] ^ vt[i]);     long long int maximum = 0;     long long int xr = 0;     for (int i = hudai.size() - 1; i >= 0; i--) {       long long int xx = hudai[i];       xr = xr ^ vt[i];       vector<long long int> tem = int_to_bin(xr);       insert(tem);       tem = int_to_bin(xx);       maximum = max(maximum, search(tem));     }     printf( %lld n , maximum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int rev(int a) {     bitset<8> bs(a);     string s = bs.to_string();     reverse((s).begin(), (s).end());     return bitset<32>(s).to_ulong();   }   int main() {     string line;     getline(cin, line);     for (int i = 0; i < (int)(line.length()); i++) {       int p = i == 0 ? 0 : line[i - 1];       int c = line[i];       int x = (rev(p) - rev(c) + 256) % 256;       ;       printf( %d n , x);     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx )   using namespace std;   template <class U, class V>   istream& operator>>(istream& is, pair<U, V>& p) {     is >> p.first >> p.second;     return is;   }   template <class T>   istream& operator>>(istream& is, vector<T>& v) {     for (auto& x : v) {       is >> x;     }     return is;   }   template <class T>   ostream& operator<<(ostream& os, vector<T>& v) {     for (auto& x : v) {       os << x <<    ;     }     return os;   }   const int N = 2e5 + 5;   vector<int> adj[N];   vector<int> parents[N];   int cnt_parents[N];   bool removed[N];   int n;   void init() {     for (int i = 1; i <= n; ++i) {       adj[i].clear();       parents[i].clear();       cnt_parents[i] = 0;       removed[i] = false;     }   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   vector<int> solve() {     static vector<int> res;     static vector<int> perm;     perm.clear();     res.clear();     for (int i = 1; i <= n; ++i) {       removed[i] = false;       perm.emplace_back(i);     }     shuffle(begin(perm), end(perm), rng);     for (int i : perm) {       if (removed[i]) continue;       for (int child : adj[i]) {         if (removed[child]) continue;         for (int grand : adj[child]) {           if (removed[grand]) continue;           res.emplace_back(grand);           removed[grand] = true;         }       }     }     return res;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t;     cin >> t;     vector<int> res;     while (t--) {       res.clear();       int m;       cin >> n >> m;       init();       while (m--) {         int u, v;         cin >> u >> v;         adj[u].emplace_back(v);         ++cnt_parents[v];         parents[v].emplace_back(u);       }       for (int i = 1; i <= n; ++i) {         bool to_remove = false;         for (int parent : parents[i]) {           if (cnt_parents[parent] == 0 || removed[parent]) continue;           to_remove = true;           break;         }         if (!to_remove) continue;         res.emplace_back(i);         removed[i] = true;         for (int v : adj[i]) {           --cnt_parents[v];         }       }       for (int i = 0; i < 30; ++i) {         auto tmp = solve();         if (tmp.size() < res.size()) {           swap(res, tmp);         }       }       cout << res.size() <<   n ;       cout << res <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int pr(int n) {     for (int i = 2; i * i <= n; i++)       if (n % i == 0) return 0;     return 1;   }   int near(int n) {     int i = 0;     for (i = n - 2; i >= 2; i--) {       if (pr(i)) break;     }     return i;   }   int main() {     int i, n, s1, j = 0, s2 = 0, counter = 0, x = 0;     int ara[10] = {0};     cin >> n;     if (pr(n)) {       cout << 1 << endl;       cout << n << endl;       return 0;     } else {       s1 = near(n);       ara[0] = s1;       j++;       s2 = n - s1;       if (pr(s2) == 1) {         printf( 2 n );         printf( %d %d , s1, s2);         return 0;       }       x = near(s2);       while (pr(s2 - x) == 0) {         x = near(x - 1);       }       printf( 3 n );       printf( %d %d %d , s1, x, n - s1 - x);       return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     unsigned long long n, ans = 0, a[30], cur, deg = 20, countOfPair = 1;     cin >> n;     memset(a, 0, sizeof(unsigned long long) * 30);     for (int i = 0; i < n; ++i) {       cin >> cur;       ++a[cur + 10];     }     for (int i = 0; i < 10; ++i) {       ans += a[i] * a[deg + i];       deg -= 2;     }     if (a[10] != 0 && a[10] != 1) {       if (a[10] == 2)         ans += 1;       else {         for (unsigned int i = 3; i <= a[10]; ++i) {           countOfPair = countOfPair + i - 1;         }         ans += countOfPair;       }     }     cout << ans;     cin >> n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int n;     cin >> n;     map<int, int> mp;     for (int i = 0; i < n; i++) {       int curr;       cin >> curr;       mp[curr]++;     }     vector<int> nums(mp.size());     int counter = 0;     vector<pair<int, int> > sorted(mp.size());     for (auto i : mp) {       sorted[counter].first = i.second;       sorted[counter].second = i.first;       nums[counter] = i.second;       counter++;     }     sort(sorted.begin(), sorted.end());     sort(nums.begin(), nums.end());     vector<int> sums(nums.size());     sums[nums.size() - 1] = nums.back();     for (int i = sums.size() - 2; i >= 0; i--) {       sums[i] = sums[i + 1] + nums[i];     }     int sqr = (int)sqrt(n);     int mx = 0;     pair<int, int> dimensions;     for (int i = sqr; i > 0; i--) {       int low = 0;       int high = nums.size() - 1;       while (low <= high) {         int mid = (low + high) >> 1;         if (nums[mid] <= i) {           low = mid + 1;         } else {           high = mid - 1;         }       }       int res = n;       if (i < nums.back()) {         res -= sums[low] - (nums.size() - low) * i;       }       low = i;       high = n;       if (res < i * i) {         continue;       }       while (low <= high) {         int mid = (low + high) >> 1;         if (mid * i < res) {           low = mid + 1;         } else if (mid * i > res) {           high = mid - 1;         } else {           high = mid;           break;         }       }       if (mx < i * high) {         mx = i * high;         dimensions.first = i;         dimensions.second = high;       }     }     cout << mx <<   n ;     cout << dimensions.first <<     << dimensions.second <<   n ;     int arr[dimensions.first][dimensions.second];     auto itr = sorted.rbegin();     counter = min((*itr).first, dimensions.first);     for (int i = 0; i < dimensions.second; i++) {       int start = i;       for (int a = 0; a < dimensions.first; a++) {         if (counter == 0) {           itr++;           counter = min((*itr).first, dimensions.first);         }         arr[a][start] = (*itr).second;         counter--;         start = (start + 1) % dimensions.second;       }     }     for (int i = 0; i < dimensions.first; i++) {       for (int a = 0; a < dimensions.second - 1; a++) {         cout << arr[i][a] <<    ;       }       cout << arr[i][dimensions.second - 1] <<   n ;     }   }