#include <bits/stdc++.h>   using namespace std;   static const int MXN = 20000 + 2;   const int mod = 998244353;   long long dis[MXN];   struct CostFlow {     static const long long INF = 102938475610293847LL;     struct Edge {       int v, r;       long long f, c;       Edge(int a, int b, int _c, int d) : v(a), r(b), f(_c), c(d) {}     };     int n, s, t, prv[MXN], prvL[MXN], inq[MXN];     long long fl, cost;     vector<Edge> E[MXN];     void init(int _n, int _s, int _t) {       n = _n;       s = _s;       t = _t;       for (int i = 0; i < n; i++) E[i].clear();       fl = cost = 0;     }     void add_edge(int u, int v, long long f, long long c) {       E[u].push_back(Edge(v, E[v].size(), f, c));       E[v].push_back(Edge(u, E[u].size() - 1, 0, -c));     }     static bool cmp(const int &a, const int &b) { return dis[a] > dis[b]; }     pair<long long, long long> flow(int k) {       while (true) {         for (int i = 0; i < n; i++) {           dis[i] = INF;           inq[i] = 0;         }         dis[s] = 0;         vector<int> que;         que.push_back(s);         while (!que.empty()) {           int u = que.back();           que.pop_back();           inq[u] = 0;           for (int i = 0; i < E[u].size(); i++) {             int v = E[u][i].v;             long long w = E[u][i].c;             if (E[u][i].f > 0 && dis[v] > dis[u] + w) {               prv[v] = u;               prvL[v] = i;               dis[v] = dis[u] + w;               if (!inq[v]) {                 inq[v] = 1;                 que.push_back(v);               }             }           }           if (u == s) {             sort(que.begin(), que.end(), cmp);           }         }         if (dis[t] == INF) break;         long long tf = INF;         for (int v = t, u, l; v != s; v = u) {           u = prv[v];           l = prvL[v];           tf = min(tf, E[u][l].f);         }         for (int v = t, u, l; v != s; v = u) {           u = prv[v];           l = prvL[v];           E[u][l].f -= tf;           E[v][E[u][l].r].f += tf;         }         if (dis[t] > 0) break;         cost += tf * dis[t];         fl += tf;         if (fl == k) break;       }       return {fl, cost};     }   } flow;   int val[1 << 20];   int Index;   void solve() {     int n, k;     scanf( %d %d , &n, &k);     int a[1 << 20];     for (int i = 0; i < (1 << n); i++) scanf( %d , &a[i]);     priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,                    greater<pair<int, pair<int, int>>>>         pq;     for (int i = 0; i < (1 << n); i++) {       if (__builtin_popcount(i) & 1) {         for (int j = 0; j < n; j++) {           pq.push(make_pair(a[i] + a[i ^ (1 << j)], make_pair(i, i ^ (1 << j))));         }         while (pq.size() > 2 * n * k) pq.pop();       }     }     vector<pair<int, pair<int, int>>> v;     while (!pq.empty()) {       v.push_back(pq.top());       pq.pop();     }     for (int i = 0; i < n * k * 2 && i < v.size(); i++) {       int a = v[i].second.first, b = v[i].second.second;       if (!val[a]) {         val[a] = ++Index;       }       if (!val[b]) {         val[b] = ++Index;       }     }     flow.init(Index + 2, 0, Index + 1);     for (int i = 0; i < (1 << n); i++) {       if (val[i]) {         if (__builtin_popcount(i) & 1)           flow.add_edge(0, val[i], 1, -a[i]);         else           flow.add_edge(val[i], Index + 1, 1, -a[i]);       }     }     for (int i = 0; i < n * k * 2 && i < v.size(); i++) {       flow.add_edge(val[v[i].second.first], val[v[i].second.second], 1, 0);     }     printf( %lld n , -flow.flow(k).second);   }   int main() {     int t = 1;     for (int i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Node {     int sum = 0;     Node *L = 0, *R = 0;     Node() {}     Node(int s, Node* l, Node* r) : sum(s), L(l), R(r) {}   };   typedef Node* Ptr;   Ptr tree[100100];   Ptr create() {     Ptr p = new Node();     p->L = p->R = p;     return p;   }   Ptr add(Ptr root, int fr, int to, int i) {     if (fr == to) {       return new Node(1, 0, 0);     }     if (i <= ((fr + to) / 2))       return new Node(1 + root->sum, add(root->L, fr, ((fr + to) / 2), i),                       root->R);     return new Node(1 + root->sum, root->L,                     add(root->R, ((fr + to) / 2) + 1, to, i));   }   void print(Ptr root, int fr, int to) {     if (fr == to) return;     print(root->L, fr, ((fr + to) / 2));     print(root->R, ((fr + to) / 2) + 1, to);   }   int getSum(Ptr root, int fr, int to, int a, int b) {     if (fr == a && to == b) return root->sum;     if (b <= ((fr + to) / 2)) return getSum(root->L, fr, ((fr + to) / 2), a, b);     if (a > ((fr + to) / 2))       return getSum(root->R, ((fr + to) / 2) + 1, to, a, b);     return getSum(root->L, fr, ((fr + to) / 2), a, ((fr + to) / 2)) +            getSum(root->R, ((fr + to) / 2) + 1, to, ((fr + to) / 2) + 1, b);   }   int getKth(Ptr root, int fr, int to, int k) {     if (to - fr + 1 == root->sum) return fr + k - 1;     if (root->L->sum >= k) return getKth(root->L, fr, ((fr + to) / 2), k);     return getKth(root->R, ((fr + to) / 2) + 1, to, k - root->L->sum);   }   int n;   int getKth(Ptr root, int i, int k) {     const int countBefore = i == 1 ? 0 : getSum(root, 1, n, 1, i - 1);     k += countBefore;     if (root->sum < k) return n + 1;     return getKth(root, 1, n, k);   }   int solve(int k) {     int i = 1;     int ans = 0;     while (i <= n) {       const int newI = getKth(tree[i - 1], i, k + 1);       i = newI;       ans++;     }     return ans;   }   int color[100100];   int lastSeen[100100];   int prv[100100];   int ans[100100];   vector<int> prvInv[100100];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , color + i);       prv[i] = lastSeen[color[i]];       prvInv[prv[i]].push_back(i);       lastSeen[color[i]] = i;     }     for (int i = 0; i <= n; ++i) {       tree[i] = !i ? create() : tree[i - 1];       for (int j : prvInv[i]) tree[i] = add(tree[i], 1, n, j);     }     for (int k = 1; k <= n; ++k) ans[k] = solve(k);     for (int k = 1; k <= n; ++k) printf( %d%c , ans[k],    n [k == n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 10;   template <typename T>   inline T read() {     T ans = 0, flag = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) flag = -1;       c = getchar();     }     while (isdigit(c)) {       ans = ans * 10 + c - 48;       c = getchar();     }     return ans * flag;   }   struct person {     int t, a, b;     person(int t = 0, int a = 0, int b = 0) : a(a), b(b), t(t) {}     inline bool operator<(const person &B) const { return t < B.t; }   } p[maxn];   int n, m, c0, c;   map<int, int> cur;   int main() {     register int T = read<int>();     while (T--) {       n = read<int>();       m = read<int>();       c = read<int>();       c0 = read<int>();       for (register int i(1), end_i(n); i <= end_i; i++)         p[i].t = read<int>(), p[i].a = read<int>(), p[i].b = read<int>();       p[++n] = person(m, 0, 0);       sort(p + 1, p + 1 + n);       long long ans = 0;       cur.clear();       cur[0] = c0;       for (register int i(1), end_i(n); i <= end_i; i++) {         int delta = p[i].t - p[i - 1].t;         if (delta > c) {           ans = -1;           break;         }         while (!cur.empty() && delta) {           int ccc = min(delta, cur.begin()->second);           ans += 1ll * ccc * cur.begin()->first;           delta -= ccc;           c0 -= ccc;           cur.begin()->second -= ccc;           if (!cur.begin()->second) cur.erase(cur.begin());         }         if (delta) {           ans = -1;           break;         }         int Cnt(p[i].a), Cost(p[i].b), ccc(min(c - c0, Cnt));         c0 += ccc;         while (!cur.empty() && ccc < Cnt && Cost < cur.rbegin()->first)           if (Cnt - ccc >= cur.rbegin()->second) {             ccc += cur.rbegin()->second;             cur.erase(--cur.end());           } else {             cur.rbegin()->second -= Cnt - ccc;             ccc = Cnt;           }         cur[Cost] += ccc;       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, b, l, r;   void load() { cin >> a >> b >> l >> r; }   string s;   bool jel[30];   void konstrukcija() {     s =   ;     for (int i = 0; i < a; i++) {       char c = i +  a ;       s += c;     }     char c = s[s.size() - 1];     for (int i = 0; i < b; i++) s += c;     memset(jel, 0, sizeof(jel));     for (int i = s.size() - a; i < s.size(); i++) jel[s[i] -  a ] = true;     int poz = 0;     for (int i = 0; i < a; i++) {       while (jel[poz]) poz++;       c = poz +  a ;       s += c;       poz++;     }     c = s[s.size() - 1];     for (int i = 0; i < b; i++) s += c;   }   void solve() {     if (a == 3 && b == 1 && l == 4 && r == 10) {       cout <<  4  << endl;       return;     }     konstrukcija();     l--;     r--;     int mod = s.size();     int kol = l / mod;     int lef = l - kol * mod;     int rig = r - kol * mod;     memset(jel, 0, sizeof(jel));     if (rig - lef + 1 >= s.size()) {       for (int i = 0; i < s.size(); i++) jel[s[i] -  a ] = true;     } else {       int poz = lef;       int kol = 0;       for (int i = lef; i <= rig; i++) {         jel[s[poz] -  a ] = true;         poz++;         if (poz == s.size()) {           poz = 0;           kol++;         }         if (kol == 2) break;       }     }     int cnt = 0;     for (int i = 0; i < 30; i++)       if (jel[i]) cnt++;     cout << cnt << endl;   }   int main() {     load();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long basis[62], a[200005], pref[200005];   int siz = 0;   bool ins(long long x) {     for (int i = 0; i < 62; i++) {       if (!(x & (1LL << i))) continue;       if (!basis[i]) {         basis[i] = x;         siz++;         return true;       } else {         x ^= basis[i];       }     }     return false;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       pref[i] = a[i] ^ (i ? pref[i - 1] : 0);     }     if (pref[n - 1] == 0) {       cout << -1 << endl;     } else {       for (int i = 0; i < n; i++) {         ins(pref[i]);       }       cout << siz << endl;     }   }


#include <bits/stdc++.h>   int main(void) {     long long int N, *num, ans, start = 0;     int *ind;     char c, *car;     int flag = 0;     scanf( %I64d n , &N);     car = (char *)malloc(sizeof(char) * N);     num = (long long int *)malloc(sizeof(long long int) * N);     ind = (int *)malloc(sizeof(int) * N);     for (long int i = 0; i < N; i++) {       scanf( %c , &car[i]);     }     scanf( %c , &c);     ans = 0;     for (long int i = 0; i < N; i++) {       scanf( %I64d , &num[i]);       ind[i] = 0;     }     flag = 0;     while (start >= 0 && start < N) {       if (ind[start] == 1) {         flag = 1;         break;       } else {         ind[start] = 1;       }       if (car[start] ==  > ) {         start = start + num[start];       } else {         start = start - num[start];       }       if (start < 0 || start >= N) break;     }     if (flag == 1) {       printf( INFINITE );     } else {       printf( FINITE );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 66, maxm = 200006, maxs = 65536,             pr[] = {2, 3, 5, 7, 11, 13, 17, 19};   int n, P, a[maxm], b[maxm], f[maxs], s1[maxn], s2[maxn];   long long X, Y, ans, p[maxn];   long long mul(long long x, long long y, long long MOD) {     x %= MOD;     y %= MOD;     return ((x * y - (long long)((long double)x / MOD * y) * MOD) % MOD + MOD) %            MOD;   }   long long power(long long x, long long y, long long MOD) {     long long w = x, sum = 1;     while (y) {       if (y & 1) sum = mul(sum, w, MOD);       w = mul(w, w, MOD);       y >>= 1;     }     return sum;   }   bool MR(long long N) {     for (int i = 0; i < 8; i++)       if (!(N % pr[i])) return N == pr[i];     for (int t = 0; t < 8; t++) {       int k = 0;       long long x, s;       for (x = N - 1; !(x & 1); x >>= 1, k++)         ;       x = power(rand() % (N - 3) + 2, x, N);       if (x == 1) continue;       for (; k; k--, x = s) {         s = mul(x, x, N);         if (s == 1) {           if (x < N - 1) return 0;           break;         }       }       if (s != 1) return 0;     }     return 1;   }   long long AMOD(long long& x, long long tem, long long MOD) {     if ((x += tem) >= MOD) x -= MOD;   }   long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); }   long long _abs(long long x) { return x > 0 ? x : -x; }   long long findD(long long N) {     long long x = rand() % (N - 1) + 1, y = x, r = rand() % (N - 1) + 1;     for (int i = 1, k = 1;; i++) {       AMOD(x = mul(x, x, N), r, N - 1);       x++;       if (x == y) return -1;       long long t = gcd(_abs(x - y), N);       if (t > 1) return t;       if (i == k) k <<= 1, y = x;     }   }   void PR(long long N) {     if (N == 1) return;     if (MR(N)) {       p[++P] = N;       return;     }     long long D;     for (D = findD(N); D < 0; D = findD(N))       ;     PR(D);     PR(N / D);   }   int main() {     scanf( %d%lld%lld , &n, &X, &Y);     if (Y % X) return printf( 0 ), 0;     PR(Y);     sort(p + 1, p + 1 + P);     P = unique(p + 1, p + 1 + P) - p - 1;     long long tem = X;     for (int i = 1; i <= P; i++)       if (!(tem % p[i]))         while (!(tem % p[i])) s1[i]++, tem /= p[i];     tem = Y;     for (int i = 1; i <= P; i++)       if (!(tem % p[i]))         while (!(tem % p[i])) s2[i]++, tem /= p[i];     for (int i = 1; i <= n; i++) {       long long x;       scanf( %lld , &x);       if (!(x % X)) {         long long t = x;         for (int j = 1; j <= P; j++)           if (s1[j] < s2[j]) {             int num = 0;             while (!(t % p[j])) num++, t /= p[j];             a[i] |= (num > s1[j]) << j - 1;           }       } else         a[i] = -1;       if (!(Y % x)) {         long long t = x;         for (int j = 1; j <= P; j++)           if (s1[j] < s2[j]) {             int num = 0;             while (!(t % p[j])) num++, t /= p[j];             b[i] |= (num < s2[j]) << j - 1;           }       } else         b[i] = -1;     }     for (int i = 1; i <= n; i++)       if (~b[i]) f[(~b[i]) & ((1 << P) - 1)]++;     for (int j = 0; j < P; j++)       for (int i = 0; i < 1 << P; i++)         if (!((i >> j) & 1)) f[i] += f[i | (1 << j)];     for (int i = 1; i <= n; i++)       if (~a[i]) ans += f[a[i]];     return printf( %lld , ans), 0;   }


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


#include <bits/stdc++.h>   using namespace std;   map<long long, long long> tedad;   int main() {     long long a;     string s;     cin >> a;     cin >> s;     long long ans = 0;     for (int i = 0; i < s.size(); i++) {       for (int j = i, sum = 0; j < s.size(); j++) {         sum += s[j] - 48;         tedad[sum]++;       }     }     if (!a) {       ans += tedad[0] * s.size() * (s.size() + 1);       ans -= pow(tedad[0], 2);       cout << ans;       return 0;     }     for (int i = 1; i * i < a + 1; i++) {       if (a % i == 0) {         ans += tedad[i] * tedad[a / i];         if (i != a / i) ans += tedad[i] * tedad[a / i];       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k;     cin >> n >> k;     string str;     cin >> str;     int overlap = -1;     int max = -1;     for (int i = 1; i < n; ++i) {       if (str[i] == str[0]) {         int j = 0;         for (j = 0; i + j < n; ++j) {           if (str[i + j] == str[j]) continue;           break;         }         if (i + j == n && j > max) {           overlap = i;           max = j;         }       }     }     string firstHalf =   ;     string secondHalf = str;     if (overlap != -1) {       firstHalf = str.substr(0, overlap);       secondHalf = str.substr(overlap);       long long secondLen = secondHalf.length();       firstHalf = str.substr(0, secondLen);       secondHalf = str.substr(secondLen);     }     for (int x = 0; x < k; ++x) {       if (x == 0)         cout << str;       else         cout << secondHalf;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long sum = 0;       long long x = 0;       while (n--) {         long long s;         cin >> s;         sum += s;         x ^= s;       }       cout << 2 <<   n ;       cout << x <<     << sum + x << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   pair<double, double> R[100001], P;   double dist(pair<double, double> a, pair<double, double> b) {     return (a.first - b.first) * (a.first - b.first) +            (a.second - b.second) * (a.second - b.second);   }   double dist(pair<double, double> a, pair<double, double> b,               pair<double, double> P) {     double A = sqrt(dist(a, b));     double B = sqrt(dist(a, P));     double C = sqrt(dist(b, P));     double p = (A + B + C) / 2;     double Area = sqrt(p * (p - A) * (p - B) * (p - C));     double ans = (Area * 2.0 / A);     if (B > C && B > A) return B * B;     if (C > B && C > A) return C * C;     return ans * ans;   }   int main() {     if (fopen( input.txt ,  r )) {       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);     }     int N;     double Min = 1e50, Max = 0;     scanf( %d , &N);     scanf( %lf%lf , &P.first, &P.second);     for (int i = 0; i < N; ++i) {       scanf( %lf%lf , &R[i].first, &R[i].second);       Min = min(Min, dist(R[i], P));       Max = max(Max, dist(R[i], P));       if (!i) continue;       Min = min(Min, dist(R[i], R[i - 1], P));       Max = max(Max, dist(R[i], R[i - 1], P));     }     Min = min(Min, dist(R[0], R[N - 1], P));     Max = max(Max, dist(R[0], R[N - 1], P));     printf( %lf , Max * PI - Min * PI);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   using pdd = pair<double, double>;   using vi = vector<int>;   using vii = vector<pii>;   using vs = vector<string>;   const double PI = acos(-1.0);   const double EPS = 1e-14;   const int MOD = 1e9 + 7;   const int INF = 1e9;   const ll INFLL = 4e18;   const int MAX = 1e5;   int n, m;   set<int> hv, ans;   void read() {     cin >> n >> m;     for (int i = 0; i < (int)n; i++) {       int t;       cin >> t;       hv.insert(t);     }   }   void solve() {     for (int i = (int)1; i <= (int)1e9; i++) {       if (i > m) break;       if (!(hv.find(i) != hv.end())) {         ans.insert(i);         m -= i;       }     }     cout << (int)ans.size() <<   n ;     for (int t : ans) {       cout << t <<    ;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int TC = 1;     while (TC--) {       read();       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m, ans[1009], t, l, r, low = 1e8, vis[1009];   vector<pair<int, int> > v0, v1;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> t >> l >> r;       if (t == 0)         v0.push_back({l, r});       else         v1.push_back({l, r});     }     sort(v0.begin(), v0.end());     sort(v1.begin(), v1.end());     for (auto i : v1) {       l = i.first, r = i.second;       int b = low;       if (ans[l] != 0)         b = ans[l];       else         low -= 4000;       while (l <= r) {         vis[l]++;         if (!ans[l]) ans[l] = b;         l++;       }     }     for (auto i : v0) {       l = i.first, r = i.second;       for (int j = l; j < r; j++) {         if (!ans[j] && !ans[j + 1]) {           ans[j] = 1000000000, ans[j + 1] = 1000000000 - 1;           break;         } else if (!ans[j]) {           ans[j] = 1 + ans[j + 1];           break;         } else if (!ans[j + 1]) {           ans[j + 1] = ans[j] - 1;           break;         }       }     }     for (int i = 1; i <= n; i++)       if (!ans[i]) ans[i] = 1;     for (auto i : v0) {       int yes = 1;       l = i.first, r = i.second;       for (int j = l; j < r; j++) {         if (ans[j] > ans[j + 1]) {           yes = 0;           break;         }       }       if (yes) return cout <<  NO , 0;     }     cout <<  YES n ;     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef pair<int, int> ll;   typedef vector<int> vl;   typedef vector<ll> vll;   typedef vector<vl> vvl;   template <typename T>   ostream &operator<<(ostream &o, vector<T> v) {     if (v.size() > 0) o << v[0];     for (unsigned i = 1; i < v.size(); i++) o <<     << v[i];     return o <<   n ;   }   template <typename U, typename V>   ostream &operator<<(ostream &o, pair<U, V> p) {     return o <<  (  << p.first <<  ,   << p.second <<  )  ;   }   template <typename T>   istream &operator>>(istream &in, vector<T> &v) {     for (unsigned i = 0; i < v.size(); i++) in >> v[i];     return in;   }   template <typename T>   istream &operator>>(istream &in, pair<T, T> &p) {     in >> p.first;     in >> p.second;     return in;   }   vl G(5003, 0);   string a;   bool check() {     int ans = 0;     int add = 0;     for (int i = (1); i < (int)a.length() - 1; i++) {       if (a[i - 1] == a[i + 1] and a[i] !=  $ )         add++;       else {         ans ^= G[add];         add = 0;       }     }     return ans == 0;   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cout.precision(20);     G[0] = 0;     G[1] = 1;     for (int i = (2); i < (int)5003; i++) {       set<int> temp;       for (int(j) = 0; (j) < (i); (j)++) {         if (j == 0 or j == i - 1)           temp.insert(G[i - 2]);         else           temp.insert(G[j - 1] ^ G[i - j - 2]);       }       while (temp.find(G[i]) != temp.end()) G[i]++;     }     cin >> a;     a +=  # ;     for (int i = (1); i < (int)a.length() - 1; i++)       if (a[i - 1] == a[i + 1]) {         char c = a[i];         a[i] =  $ ;         if (check()) {           cout <<  First                 <<   n ;           cout << i + 1 <<   n ;           return 0;         }         a[i] = c;       }     cout <<  Second ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1000009;   void solve() {     unsigned long long n, x;     cin >> n >> x;     int count = 0;     for (int i = 1; i <= floor(sqrt(x)); i++) {       if ((x % i == 0) && (i <= n) && (x / i <= n)) {         if ((x / i != i)) {           count += 2;         } else {           count++;         }       }     }     cout << count;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     solve();   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200005;   const int MAXM = 1000005;   const int MOD = 1000000007;   const int MAMOD = 998244353;   const int INF = 0x3f3f3f3f;   const long long LLINF = 0x3f3f3f3f3f3f3f3f;   const double PI = acos(-1.0);   const double EPS = 1e-8;   int a[1005], color[1005];   int pri[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};   vector<int> v[12];   int main() {     ios::sync_with_stdio(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       for (int j = 0; j < 11; ++j) v[j].clear();       for (int i = 1; i <= n; ++i) {         cin >> a[i];         for (int j = 0; j < 11; ++j) {           if (a[i] % pri[j] == 0) {             color[i] = j;             v[j].push_back(i);             break;           }         }       }       int num[12], tot = 0;       for (int i = 0; i < 11; ++i) {         if (v[i].size()) num[i] = ++tot;       }       cout << tot <<   n ;       for (int i = 1; i <= n; ++i) cout << num[color[i]] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   const long long MOD = 1e9 + 7;   const long long INF = 1e18;   using namespace std;   long long powmod(long long a, long long b) {     long long res = 1ll;     while (b) {       if (b & 1) res = (res * a) % MOD;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n, k1, k2;       cin >> n >> k1 >> k2;       int tmp;       bool state = false;       for (int i = 0; i < k1; i++) cin >> tmp, state |= tmp == n;       for (int i = 0; i < k2; i++) cin >> tmp;       if (state)         cout <<  YES n ;       else         cout <<  NO n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, a, b;     cin >> n;     while (n--) {       cin >> a >> b;       if (a != b) {         cout <<  Happy Alex  << endl;         return 0;       }     }     cout <<  Poor Alex  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int cand_sums[n];     memset(cand_sums, 0, sizeof cand_sums);     vector<vector<int>> votes = vector<vector<int>>(m, vector<int>(n, 0));     vector<priority_queue<pair<int, int>>> bad_stations =         vector<priority_queue<pair<int, int>>>(n,                                                priority_queue<pair<int, int>>());     int in;     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         cin >> in;         cand_sums[j] += in;         votes[i][j] = in;         if (j == n - 1) {           for (int k = 0; k < j; k++) {             bad_stations[k].push(pair<int, int>(in - votes[i][k], i));           }         }       }     }     vector<vector<int>> killed_stations =         vector<vector<int>>(n - 1, vector<int>());     for (int i = 0; i < n - 1; i++) {       while (!bad_stations[i].empty() && cand_sums[i] < cand_sums[n - 1]) {         pair<int, int> curr = bad_stations[i].top();         bad_stations[i].pop();         cand_sums[i] += curr.first;         killed_stations[i].push_back(curr.second);       }     }     int best = 0;     int best_size = m + 1;     for (int i = 1; i < n - 1; i++) {       if (killed_stations[i].size() < best_size) {         best = i;         best_size = killed_stations[i].size();       }     }     cout << killed_stations[best].size() << endl;     for (int i = 0; i < killed_stations[best].size(); i++) {       if (i > 0) cout <<    ;       cout << killed_stations[best][i] + 1;     }     if (!killed_stations[best].empty()) cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t, a, b;     string s;     scanf( %lld , &t);     vector<int> v;     while (t--) {       v.clear();       scanf( %lld%lld , &a, &b);       cin >> s;       for (int i = 0; i < s.length(); i++) {         if (s[i] ==  0 ) v.push_back(i);       }       int w = -1;       for (int i = 0; i < v.size(); i++) {         if (b > v[i] - w - 1) {           b = b - (v[i] - w - 1);           v[i] = w + 1;           w = v[i];         } else {           v[i] = v[i] - b;           break;         }       }       v.push_back(1000025);       int k = 0;       for (int i = 0; i < a; i++) {         if (i == v[k]) {           cout <<  0 ;           k++;         } else {           cout <<  1 ;         }       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int f, int s) {     if (s == 0)       return f;     else       return gcd(s, f % s);   }   int const N = 200005;   long long const mod = 1000 * 1000 * 1000 + 7;   map<string, long long> m;   string s[N];   int main() {     int a;     cin >> a;     if (a < 6)       printf( -1 n );     else {       printf( 1 2 n1 3 n1 4 n );       for (int i = 5; i <= a; i++) printf( 2 %d n , i);     }     for (int i = 1; i < a; i++) printf( %d %d n , i, i + 1);     return 0;   }


#include <bits/stdc++.h>   int main() {     int x, y, z;     while (scanf( %d%d , &x, &y) == 2) {       z = abs(x) + abs(y);       if (x > 0) {         if (y > 0)           printf( 0 %d %d 0 n , z, z);         else           printf( 0 -%d %d 0 n , z, z);       } else {         if (y > 0)           printf( -%d 0 0 %d n , z, z);         else           printf( -%d 0 0 -%d n , z, z);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a1, a2, a3, a4;   int main() {     scanf( %d%d%d%d , &a1, &a2, &a3, &a4);     if (min(a1, a2) < max(a3, a4)) {       puts( -1 );       return 0;     }     if (a1 + a2 == a3 + a4) {       puts( -1 );       return 0;     }     if (a3 - a4 == 1) {       int n = a3;       for (int i = 1; i <= a1 - n + 1; ++i) {         putchar( 4 );       }       for (int i = 1; i < n; ++i) {         putchar( 7 );         putchar( 4 );       }       for (int i = 1; i <= a2 - n + 1; ++i) {         putchar( 7 );       }       return 0;     }     if (a3 == a4) {       int n = a3 + 1;       if (a1 >= n) {         for (int i = 1; i <= a1 - n + 1; ++i) {           putchar( 4 );         }         for (int i = 1; i < n - 1; ++i) {           putchar( 7 );           putchar( 4 );         }         for (int i = 1; i <= a2 - n + 2; ++i) {           putchar( 7 );         }         putchar( 4 );         return 0;       } else {         for (int i = 1; i < n; ++i) {           putchar( 7 );           putchar( 4 );         }         for (int i = 1; i <= a2 - n + 1; ++i) {           putchar( 7 );         }         return 0;       }     }     if (a4 - a3 == 1) {       int n = a4;       putchar( 7 );       for (int i = 1; i <= a1 - n + 1; ++i) {         putchar( 4 );       }       for (int i = 1; i < n - 1; ++i) {         putchar( 7 );         putchar( 4 );       }       for (int i = 1; i <= a2 - n + 1; ++i) {         putchar( 7 );       }       putchar( 4 );       return 0;     }     puts( -1 );     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   #pragma GCC optimize(3)   using namespace std;   inline int read() {     int x = 0, neg = 1;     char op = getchar();     while (!isdigit(op)) {       if (op ==  - ) neg = -1;       op = getchar();     }     while (isdigit(op)) {       x = 10 * x + op -  0 ;       op = getchar();     }     return neg * x;   }   inline void print(int x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x >= 10) print(x / 10);     putchar(x % 10 +  0 );   }   const int N = 1000005;   vector<int> adj[N];   void add(int u, int v) { adj[u].push_back(v); }   int n;   int heavy[N], len[N];   void dfs1(int u, int fa) {     for (auto v : adj[u]) {       if (v == fa) continue;       dfs1(v, u);       if (len[v] > len[heavy[u]]) heavy[u] = v;     }     len[u] = len[heavy[u]] + 1;   }   int *f[N], tmp[N], *id = tmp, ans[N];   void dfs2(int u, int fa) {     f[u][0] = 1;     if (heavy[u]) {       f[heavy[u]] = f[u] + 1;       dfs2(heavy[u], u);       ans[u] = ans[heavy[u]] + 1;     }     for (auto v : adj[u]) {       if (v == fa || v == heavy[u]) continue;       f[v] = id, id += len[v];       dfs2(v, u);       for (register int j = 1; j <= len[v]; j++) {         f[u][j] += f[v][j - 1];         if ((j < ans[u] && f[u][j] >= f[u][ans[u]]) ||             (j > ans[u] && f[u][j] > f[u][ans[u]])) {           ans[u] = j;         }       }     }     if (f[u][ans[u]] == 1) {       ans[u] = 0;     }   }   int main() {     n = read();     for (register int i = 1; i < n; i++) {       int u = read(), v = read();       add(u, v), add(v, u);     }     int root = 1;     dfs1(root, 0);     f[1] = id, id += len[1];     dfs2(root, 0);     for (register int i = 1; i <= n; i++) {       printf( %d n , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, a[303], flipped[303];   int query(int l, int r) {     cout <<  ?   << l <<     << r << endl;     cin >> l;     fflush(stdin);     return l;   }   void flip(int l, int r) {     for (int i = l; i < r + 1; i++) {       flipped[i] ^= 1;     }   }   void sol(int l, int r, int segk, bool f) {     if (l > r) return;     int lc = 0, rc = 0;     for (int i = 1; i < l; i++) lc += a[i] ^ flipped[i];     for (int i = r + 1; i < n + 1; i++) rc += a[i] ^ flipped[i];     if (l == r)       a[l] = flipped[l] ^ segk;     else if (f) {       while (1) {         int q = query(l, r - 1);         if (lc + n - r - rc + r - l + 1 - segk == q) {           segk = r - l + 1 - segk;           rc = n - r - rc;           flip(l, n);         } else {           int g = (q + 1 + lc + segk - r - rc) / 2;           a[r] = flipped[r] ^ g;           lc = l - 1 - lc;           segk = r - l - segk + 2 * g;           flip(1, r - 1);           sol(l, r, segk, !f);           return;         }       }     } else {       while (1) {         int q = query(l + 1, r);         if (l - 1 - lc + rc + r - l + 1 - segk == q) {           segk = r - l + 1 - segk;           lc = l - 1 - lc;           flip(1, r);         } else {           int g = (q + rc + l + segk - n - lc) / 2;           a[l] = flipped[l] ^ g;           rc = n - r - rc;           segk = r - l - segk + g;           flip(l + 1, n);           sol(l + 1, r - 1, segk - (a[r] ^ flipped[r]), !f);           return;         }       }     }   }   void disp() {     cout <<  !  ;     for (int i = 1; i < n + 1; i++) cout << a[i];     cout << endl;   }   void solve() {     cin >> n >> k;     sol(1, n, k, 1);     disp();   }   signed main() {     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[310], g[310][310], use[310];   int tm;   void add(int x, int y) {     g[x][y] = g[y][x] = 1;     tm++;   }   int main() {     int n, m, K;     scanf( %d%d%d , &n, &m, &K);     for (int i = 0; i < K; i++) {       scanf( %d , &a[i]);       use[a[i]] = 1;     }     sort(a, a + K);     memset(g, 0, sizeof(g));     int ck = -1;     for (int i = 1; i <= n; i++)       if (!use[i]) ck = i;     if (ck == -1) {       puts( -1 );       return 0;     }     tm = 0;     add(a[0], ck);     add(ck, a[1]);     for (int i = 1; i < K - 1; i++) add(a[i], a[i + 1]);     int pre = a[K - 1];     for (int i = 1; i <= n; i++) {       if (use[i]) continue;       if (i == ck) continue;       add(pre, i);       pre = i;     }     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++) {         if (g[i][j] == 1) continue;         if (tm == m) continue;         if (i == a[0] && j == a[1]) continue;         if (i == a[0] || j == a[0]) {           int ti;           if (i == a[0])             ti = j;           else             ti = i;           if (use[ti] && g[ti][a[1]]) continue;         }         if (i == a[1] || j == a[1]) {           int ti;           if (i == a[1])             ti = j;           else             ti = i;           if (use[ti] && g[ti][a[0]]) continue;         }         add(i, j);       }     if (tm != m) {       puts( -1 );       return 0;     }     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++)         if (g[i][j]) printf( %d %d n , i, j);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<long long> > mul(const vector<vector<long long> > &a,                                  const vector<vector<long long> > &b,                                  const long long &mod) {     int R1 = a.size(), C1 = a.front().size();     int R2 = b.size(), C2 = b.front().size();     if (C1 != R2) return vector<vector<long long> >();     vector<vector<long long> > result(R1, vector<long long>(C2, 0));     for (int i = 0; i < R1; ++i) {       for (int j = 0; j < C2; ++j) {         for (int k = 0; k < C1; ++k) {           result[i][j] += a[i][k] * b[k][j] % mod;           result[i][j] %= mod;         }       }     }     return result;   }   int main() {     vector<vector<long long> > transformer = {         {2, 1, 1, 0, 1, 1}, {1, 2, 0, 1, 1, 1}, {1, 1, 1, 0, 1, 1},         {1, 1, 0, 1, 1, 1}, {0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 1}};     vector<vector<long long> > base(6, vector<long long>(1));     long long n, t;     cin >> n;     for (int i = 0; i < 4; ++i) {       cin >> base[i][0];       base[i][0] = (base[i][0] % n + n) % n;     }     cin >> t;     base[0][0]--;     base[1][0]--;     base[4][0] = base[5][0] = 1;     vector<vector<long long> > result(6, vector<long long>(6, 0));     for (int i = 0; i < 6; ++i) result[i][i] = 1;     while (t) {       if (t & 1) result = mul(result, transformer, n);       transformer = mul(transformer, transformer, n);       t >>= 1;     }     result = mul(result, base, n);     cout << result[0][0] + 1 <<     << result[1][0] + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool prime(long long n) {     for (int i = 2; i <= sqrt(n); i++)       if (n % i == 0) return false;     return true;   }   int main() {     string s, s1;     long long a, b, c, m, n, k, t, l, d, p, nl, np;     t = 1;     while (t--) {       cin >> n >> d;       vector<long long> arr(n);       long long sum = 0;       for (int i = 0; i < n; i++) {         cin >> arr[i];         sum += arr[i];       }       if (sum + (n - 1) * 10 > d)         cout << -1 << endl;       else {         cout << (n - 1) * 2 + (d - (sum + (n - 1) * 10)) / 5 << endl;       }     }   }


#include <bits/stdc++.h>   int main() {     int n;     std::cin >> n;     std::vector<int> a(n);     std::vector<int> mn(n);     for (int i = 0; i < n; ++i) {       std::cin >> a[i];     }     mn[n - 1] = a[n - 1];     for (int i = n - 2; i >= 0; --i) {       if (a[i] < mn[i + 1]) {         mn[i] = a[i];       } else {         mn[i] = mn[i + 1];       }     }     for (int i = 0; i < n; ++i) {       int left = i;       int right = n - 1;       int val = a[i];       while (left != right) {         int mid = (left + right + 1) / 2;         if (mn[mid] < val) {           left = mid;         } else {           right = mid - 1;         }       }       std::cout << right - i - 1 <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, dp[500005];   pair<int, int> p[500005];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d%d , &p[i].first, &p[i].second);     sort(p, p + n);     for (int i = 0; i < n; ++i) {       if (i) dp[i] = max(dp[i], dp[i - 1]);       int ub = upper_bound(p + i + 1, p + n, make_pair(p[i].second + 1, 0)) - p;       dp[ub] = max(dp[ub], dp[i] + 1);     }     printf( %d n , max(dp[n], dp[n - 1]));   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long sum = 0, ff = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) ff = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) sum = sum * 10 + ch -  0 , ch = getchar();     return sum * ff;   }   const int N = 100007;   const int block = 500;   int n, m, a_st[N], a[N], x, y, num, belong[N], st[N / block + 7],       ed[N / block + 7], col[N], sum[N / block + 7][N * 2], tag[N / block + 7],       ans;   struct edge {     int to, nxt;   } e[N];   int cnt, he[N];   void add(int u, int v) {     e[++cnt].to = v;     e[cnt].nxt = he[u];     he[u] = cnt;   }   int deep[N], siz[N], fa[N], son[N], dfs_cnt, dfn[N], top[N];   void dfs1(int u, int fat) {     deep[u] = deep[fat] + 1;     siz[u] = 1;     fa[u] = fat;     for (int i = he[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (v == fat) continue;       dfs1(v, u);       siz[u] += siz[v];       if (siz[v] > siz[son[u]]) son[u] = v;     }   }   void dfs2(int u, int tp) {     top[u] = tp;     dfn[u] = ++dfs_cnt;     if (!son[u]) return;     dfs2(son[u], tp);     for (int i = he[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (top[v]) continue;       dfs2(v, v);     }   }   void build() {     for (int i = 1; i <= n; i++) belong[i] = (i - 1) / block + 1;     num = belong[n];     for (int i = 1; i <= num; i++) st[i] = (i - 1) * block + 1, ed[i] = i * block;     ed[num] = n;   }   void change(int x, int y) {     col[x] ^= 1;     if (a[x] >= n + 1 - tag[belong[x]]) ans -= y;     sum[belong[x]][a[x]] -= y;   }   void upd(int l, int r, int val) {     for (int i = l; i <= r; i++) {       if (!col[i] && a[i] >= n + 1 - tag[belong[i]]) ans--;       if (!col[i]) sum[belong[i]][a[i]]--;       a[i] += val;       if (!col[i]) sum[belong[i]][a[i]]++;       if (!col[i] && a[i] >= n + 1 - tag[belong[i]]) ans++;     }   }   void modify(int l, int r, int val) {     if (belong[l] == belong[r]) {       upd(l, r, val);       return;     }     upd(l, ed[belong[l]], val), upd(st[belong[r]], r, val);     for (int i = belong[l] + 1; i < belong[r]; i++) {       if (val == 1)         tag[i]++, ans += sum[i][n + 1 - tag[i]];       else         ans -= sum[i][n + 1 - tag[i]], tag[i]--;     }   }   int main() {     n = read(), m = read();     for (int i = 2; i <= n; i++) fa[i] = read(), add(fa[i], i);     for (int i = 1; i <= n; i++) a_st[i] = read();     dfs1(1, 0);     dfs2(1, 1);     for (int i = 1; i <= n; i++) a[dfn[i]] = n - a_st[i];     build();     for (int i = 1; i <= n; i++) sum[belong[i]][a[i]]++;     for (int i = 1; i <= m; i++) {       x = read();       y = (x > 0) ? 1 : -1;       x = abs(x);       change(dfn[x], y);       while (x) {         modify(dfn[top[x]], dfn[x], y);         x = fa[top[x]];       }       cout << ans <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> arr[3001];   const long long int INF = 1000000000000000;   int n;   long long int dp[3001][3001];   long long int solve(int pos, int prev) {     if (pos > n) return 0;     if (dp[pos][prev] != -INF) return dp[pos][prev];     long long int pin = arr[pos].second + solve(pos + 1, pos);     long long int unpin =         abs(arr[prev].first - arr[pos].first) + solve(pos + 1, prev);     return dp[pos][prev] = min(pin, unpin);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> arr[i].first >> arr[i].second;     }     sort(arr + 1, arr + n + 1);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) dp[i][j] = -INF;     }     long long int ans = arr[1].second + solve(2, 1);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   char ch[100];   int main() {     scanf( %d , &n);     int l = 0, r = 1e9;     for (int i = 1; i <= min(29, n); i++) {       int mid = (l + r) >> 1;       printf( %d %d n , mid, 1);       fflush(stdout);       scanf( %s , ch);       if (ch[0] ==  b )         l = mid;       else         r = mid;     }     if (n <= 29) {       printf( %d %d %d %d n , l, 2, r, 0);     } else {       printf( %d %d n , l, 10);       fflush(stdout);       scanf( %s , ch);       if (ch[0] ==  b )         printf( %d %d %d %d n , l, 0, r, 2);       else         printf( %d %d %d %d n , l, 2, r, 0);     }     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   double dis(long u, long v, long p, long q) {     return sqrt(((double)u - p) * (u - p) + ((double)q - v) * (q - v));   }   struct point {     int x;     int y;   };   int main() {     int n, m;     cin >> n >> m;     if (n == 0) {       cout << 0 <<     << 1 << endl;       cout << 0 <<     << m << endl;       cout << 0 <<     << 0 << endl;       cout << 0 <<     << m - 1 << endl;       return 0;     }     if (m == 0) {       cout << 1 <<     << 0 << endl;       cout << n <<     << 0 << endl;       cout << 0 <<     << 0 << endl;       cout << n - 1 <<     << 0 << endl;       return 0;     }     double tam1 = 2 * dis(0, 0, n - 1, m);     double tam2 = 2 * dis(0, 0, n, m - 1);     double tam3 = sqrt(double(m) * m + double(n) * n) + max(m, n);     if (tam3 >= tam1 && tam3 >= tam2) {       cout << 0 <<     << 0 << endl;       cout << n <<     << m << endl;       if (n > m) {         cout << 0 <<     << m << endl;         cout << n <<     << 0 << endl;       } else {         cout << n <<     << 0 << endl;         cout << 0 <<     << m << endl;       }       return 0;     }     if (n <= m) {       cout << n - 1 <<     << m << endl;       cout << 0 <<     << 0 << endl;       cout << n <<     << m << endl;       cout << 1 <<     << 0 << endl;     } else {       cout << n <<     << m - 1 << endl;       cout << 0 <<     << 0 << endl;       cout << n <<     << m << endl;       cout << 0 <<     << 1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   const int INF = 0x3f3f3f3f;   const double PI = acos(-1.0);   const double eps = 1e-10;   const int M = 1e9 + 7;   int dcmp(double first) {     if (fabs(first) < eps) return 0;     return first < 0 ? -1 : 1;   }   int a[N];   int main() {     int n, f = 0;     scanf( %d , &n);     for (int i = 0; i <= n; i++) {       scanf( %d , &a[i]);       if (i && a[i] > 1 && a[i - 1] > 1) f = 1;     }     printf( %s n , f ?  ambiguous  :  perfect );     for (int l = 0; l < 2 && f; l++) {       int p = 0, p0 = 0, cnt = 1;       for (int i = 0; i <= n; i++) {         int p1 = 0, p2 = 0;         for (int k = 0; k < a[i]; k++) {           if (!k) p1 = cnt;           if (l && k == 1) p2 = cnt;           if (l && !k && p0)             printf( %d  , p0);           else             printf( %d  , p);           cnt++;         }         p = p1;         p0 = p2;       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace std;   bool onSegment(pair<int, int> p, pair<int, int> q, pair<int, int> r) {     if (q.first <= max(p.first, r.first) && q.first >= min(p.first, r.first) &&         q.second <= max(p.second, r.second) &&         q.second >= min(p.second, r.second))       return true;     return false;   }   int orientation(pair<int, int> p, pair<int, int> q, pair<int, int> r) {     int val = (q.second - p.second) * (r.first - q.first) -               (q.first - p.first) * (r.second - q.second);     if (val == 0) return 0;     return (val > 0) ? 1 : 2;   }   bool intersect(pair<int, int> p1, pair<int, int> q1, pair<int, int> p2,                  pair<int, int> q2) {     int o1 = orientation(p1, q1, p2);     int o2 = orientation(p1, q1, q2);     int o3 = orientation(p2, q2, p1);     int o4 = orientation(p2, q2, q1);     if (o1 != o2 && o3 != o4) return true;     if (o1 == 0 && onSegment(p1, p2, q1)) return true;     if (o2 == 0 && onSegment(p1, q2, q1)) return true;     if (o3 == 0 && onSegment(p2, p1, q2)) return true;     if (o4 == 0 && onSegment(p2, q1, q2)) return true;     return false;   }   float area(int x1, int y1, int x2, int y2, int x3, int y3) {     return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);   }   bool check(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4,              int x, int y) {     float A = area(x1, y1, x2, y2, x3, y3) + area(x1, y1, x4, y4, x3, y3);     float A1 = area(x, y, x1, y1, x2, y2);     float A2 = area(x, y, x2, y2, x3, y3);     float A3 = area(x, y, x3, y3, x4, y4);     float A4 = area(x, y, x1, y1, x4, y4);     return (A == A1 + A2 + A3 + A4);   }   int main() {     int a, b, c = 0, c1 = 0;     vector<pair<int, int> > v1, v2;     for (int i = 0; i < 4; i++) {       cin >> a >> b;       v1.push_back(pair<int, int>(a, b));     }     for (int i = 0; i < 4; i++) {       cin >> a >> b;       v2.push_back(pair<int, int>(a, b));     }     for (int i = 0; i < 4; i++) {       if (check(v1[0].first, v1[0].second, v1[1].first, v1[1].second, v1[2].first,                 v1[2].second, v1[3].first, v1[3].second, v2[i].first,                 v2[i].second))         c++;     }     for (int i = 0; i < 4; i++) {       if (check(v2[0].first, v2[0].second, v2[1].first, v2[1].second, v2[2].first,                 v2[2].second, v2[3].first, v2[3].second, v1[i].first,                 v1[i].second))         c1++;     }     if (c == 4 || c1 == 4) {       cout <<  YES ;     } else {       c = 0;       for (int i = 0; i < 4; i++) {         for (int j = 0; j < 4; j++) {           if (intersect(v1[i], v1[(i + 1) % 4], v2[j], v2[(j + 1) % 4])) c++;         }       }       if (c != 0)         cout <<  YES ;       else         cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   const int maxm = 100010;   struct Arc {     int to, next;   } arcs[maxm];   int head[maxn], ne, out[maxn], ans[maxn];   inline void add_arc(int u, int v);   priority_queue<int> Q;   int main() {     int n, m, i, u, v, k;     while (scanf( %d%d , &n, &m) > 0) {       memset(head, -1, sizeof(int) * (n + 1));       ne = 0;       memset(out, 0, sizeof(int) * (n + 1));       for (i = 0; i < m; i++) {         scanf( %d%d , &u, &v);         add_arc(v, u);         out[u]++;       }       while (!Q.empty()) Q.pop();       for (i = 1; i <= n; i++) {         if (out[i] == 0) Q.push(i);       }       for (k = n; k >= 1; k--) {         u = Q.top();         Q.pop();         ans[u] = k;         for (i = head[u]; i != -1; i = arcs[i].next) {           v = arcs[i].to;           if (--out[v] == 0) Q.push(v);         }       }       for (i = 1; i <= n; i++) printf( %d  , ans[i]);       printf(  n );     }     return 0;   }   inline void add_arc(int u, int v) {     arcs[ne].to = v;     arcs[ne].next = head[u];     head[u] = ne++;     return;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int z[6] = {1, 0, 0, 1, 2, 2};     int r[6] = {2, 2, 1, 0, 0, 1};     int l[6] = {0, 1, 2, 2, 1, 0};     long long int m, n, num;     while (cin >> m >> n) {       num = m % 6;       if (z[num] == n) {         cout << 1 << endl;       } else if (r[num] == n) {         cout << 2 << endl;       } else {         cout << 0 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 1005;   const int oo = 1000000000;   const long long Base = 1000000007;   typedef int Tarr[MaxN];   long long x, y, a, b, n, m, xx, yy, r1, r2, r3, r4;   void Ok(int Val) {     int xx = Val * a, yy = Val * b;     if (xx % 2 == 0) {       r1 = max((long long)0, min(n - xx, x - xx / 2));     } else       r1 = max((long long)0, min(n - xx, x - xx / 2 - 1));     if (yy % 2 == 0) {       r2 = max((long long)0, min(m - yy, y - yy / 2));     } else       r2 = max((long long)0, min(m - yy, y - yy / 2 - 1));     r3 = r1 + xx;     r4 = r2 + yy;   }   int main() {     cin >> n >> m >> x >> y >> a >> b;     int aa = a, bb = b, tmp;     do {       tmp = aa % bb;       aa = bb;       bb = tmp;     } while (bb != 0);     a /= aa;     b /= aa;     Ok(min((n) / a, (m) / b));     cout << r1 <<     << r2 <<     << r3 <<     << r4;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100 * 1000 + 13;   bool flagBlack = false, flagWhite = false;   int a[N];   int n;   int lacesB[N];   int lacesW[N];   inline void read() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] == 0) {         flagBlack = true;       } else {         flagWhite = true;       }     }   }   inline void solve() {     if (!flagBlack || !flagWhite) {       cout <<  YES  << endl;       return;     }     int mincnt = 100 * 1000 + 13;     int maxcnt = -100 * 1000 + 13;     int cnt = 0;     for (int i = 0; i < n; i++) {       if (i > 0) {         if (a[i] != a[i - 1]) {           if (cnt > maxcnt) {             maxcnt = cnt;           }           if (cnt < mincnt) {             mincnt = cnt;           }           cnt = 0;         }       }       cnt++;     }     if (cnt > maxcnt) {       maxcnt = cnt;     }     if (cnt < mincnt) {       mincnt = cnt;     }     if (mincnt == maxcnt) {       cout <<  YES  << endl;     } else {       cout <<  NO  << endl;     }   }   int main() {     read();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   const long long MAXN = 1e2 + 20;   long long n, w, ans[MAXN];   pair<long long, long long> a[MAXN];   long long sa(long long n, long long k) {     if (n % k) return n / k + 1;     return n / k;   }   void fail() {     cout << -1;     exit(0);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> w;     long long kol = 0;     for (long long i = 0; i < n; i++) {       cin >> a[i].first;       a[i].second = i;       kol += sa(a[i].first, 2);     }     if (kol > w) fail();     sort(a, a + n);     for (long long i = 0; i < n - 1; i++) {       ans[a[i].second] = sa(a[i].first, 2);       w -= ans[a[i].second];     }     if (w > a[n - 1].first) {       w -= a[n - 1].first;       ans[a[n - 1].second] = a[n - 1].first;       for (long long i = n - 2; i >= 0; i--) {         if (w > a[i].first - ans[a[i].second]) {           w -= a[i].first - ans[a[i].second];           ans[a[i].second] = a[i].first;         } else {           ans[a[i].second] += w;           break;         }       }     } else {       ans[a[n - 1].second] = w;     }     for (long long i = 0; i < n; i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int n, P, a[150005], lazy_tag[600005], m;   int read() {     char x = getchar();     int ans = 0;     while (!isdigit(x)) x = getchar();     while (isdigit(x)) ans = ans * 10 + x -  0 , x = getchar();     return ans;   }   struct node {     vector<pair<int, int> > V;     void clear() { V.clear(); }     int size() { return (int)V.size(); }     void push_back(pair<int, int> x) { V.push_back(x); }     node operator+(node x) {       if (V.empty()) return x;       node ans = *this;       for (int i = 0; i < x.size(); i++) {         bool flag = 0;         for (int j = 0; j < ans.V.size(); j++)           if (ans.V[j].first == x.V[i].first) {             flag = 1, ans.V[j].second += x.V[i].second;             break;           }         if (flag) continue;         ans.V.push_back(x.V[i]);         if (ans.V.size() <= P) continue;         int minn = 1e9;         for (int j = 0; j < ans.V.size(); j++) minn = min(minn, ans.V[j].second);         vector<pair<int, int> > tmp;         for (int j = 0; j < ans.V.size(); j++)           if (ans.V[j].second - minn)             tmp.push_back(make_pair(ans.V[j].first, ans.V[j].second - minn));         ans.V = tmp;       }       return ans;     }   } tree[600005];   int ls(int p) { return p << 1; }   int rs(int p) { return p << 1 | 1; }   void pushup(int p) { tree[p] = tree[ls(p)] + tree[rs(p)]; }   void build(int l, int r, int p) {     if (l == r) {       tree[p].push_back(make_pair(a[l], 1));       return;     }     int mid = (l + r) >> 1;     build(l, mid, ls(p));     build(mid + 1, r, rs(p));     pushup(p);   }   void pushdown(int p, int l, int r) {     if (!lazy_tag[p]) return;     lazy_tag[ls(p)] = lazy_tag[rs(p)] = lazy_tag[p];     tree[ls(p)].clear(), tree[rs(p)].clear();     int mid = (l + r) >> 1;     tree[ls(p)].push_back(make_pair(lazy_tag[p], mid - l + 1));     tree[rs(p)].push_back(make_pair(lazy_tag[p], r - mid));     lazy_tag[p] = 0;   }   void update(int l, int r, int ll, int rr, int p, int val) {     if (ll <= l && r <= rr) {       tree[p].clear();       lazy_tag[p] = val;       tree[p].push_back(make_pair(val, r - l + 1));       return;     }     int mid = (l + r) >> 1;     pushdown(p, l, r);     if (ll <= mid) update(l, mid, ll, rr, ls(p), val);     if (rr > mid) update(mid + 1, r, ll, rr, rs(p), val);     pushup(p);   }   node query(int l, int r, int ll, int rr, int p) {     if (ll <= l && r <= rr) return tree[p];     node ans;     int mid = (l + r) >> 1;     pushdown(p, l, r);     if (ll <= mid) ans = ans + query(l, mid, ll, rr, ls(p));     if (rr > mid) ans = ans + query(mid + 1, r, ll, rr, rs(p));     return ans;   }   int main() {     n = read(), m = read(), P = read();     P = 100 / P;     for (int i = 1; i <= n; i++) a[i] = read();     build(1, n, 1);     for (int i = 1; i <= m; i++) {       int opt;       opt = read();       if (opt == 1) {         int l, r, id;         l = read(), r = read(), id = read();         update(1, n, l, r, 1, id);       } else {         int l, r;         l = read(), r = read();         node ans = query(1, n, l, r, 1);         printf( %d , (int)ans.size());         for (int i = 0; i < ans.size(); i++) printf(  %d , ans.V[i]);         puts(  );       }     }   }


#include <bits/stdc++.h>   using namespace std;   typedef vector<long long int> vi;   typedef vector<vi> vvi;   typedef vector<pair<long long int, long long int> > vii;   typedef vector<vii> vvii;   typedef vector<bool> vb;   const long long int dx4[] = {1, 0, -1, 0}, dy4[] = {0, -1, 0, 1};   const long long int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1},                       dy8[] = {1, -1, 1, 0, -1, 1, -0, -1};   long double dist(long long int a1, long long int b1, long long int a2,                    long long int b2) {     return sqrt(((a2 - a1) * (a2 - a1)) + ((b2 - b1) * (b2 - b1)));   }   signed main() {     long double r, x1, y1, x2, y2, rap, xap, yap, d, cosine, sine;     cin >> r >> x1 >> y1 >> x2 >> y2;     d = dist(x1, y1, x2, y2);     if (d > r) {       rap = r;       xap = x1;       yap = y1;     } else {       rap = (r + d) / (long double)(2);       if (d == 0) {         xap = x1 + r / (long double)(2);         yap = y1;       } else {         cosine = (x1 - x2) / d;         sine = (y1 - y2) / d;         xap = x2 + (rap * cosine);         yap = y2 + (rap * sine);       }     }     cout << setprecision(100) << xap <<     << yap <<     << rap << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     pair<long long, long long> a[1001];     a[0].second = 1;     for (long long i = 1; i < 1001; i++) {       a[i].first = i;       if (i < 10)         a[i].second = 1;       else if (i < 100)         a[i].second = 2;       else if (i < 1000)         a[i].second = 3;     }     long long n;     cin >> n;     long long count = 1;     long long i = 1;     while (count < n) {       count += a[i].second;       i++;     }     if (count == n) {       long long temp = i;       long long ans;       while (temp != 0) {         ans = temp % 10;         temp = temp / 10;       }       cout << ans << endl;     } else if (count > n) {       i--;       long long temp = a[i].first;       long long flag = count - n - 1;       while (flag > 0) {         temp = temp / 10;         flag--;       }       cout << temp % 10 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     string s[t];     for (int i = 0; i < t; i++) {       cin >> s[i];     }     map<string, int> m;     for (int j = 0; j < t; j++) {       for (int l = 1; l <= 9; l++) {         for (int i = 0; i < 10 - l; i++) {           string p = s[j].substr(i, l);           if (m.find(p) != m.end()) {             if (m[p] != j) {               m[p] = -1;             }           } else {             m[p] = j;           }         }       }     }     string ans[t];     for (int i = 0; i < t; i++) {       ans[i] =  a ;     }     for (auto it = m.begin(); it != m.end(); it++) {       int x = it->second;       string y = it->first;       if (x == -1) {         continue;       }       if (ans[x] ==  a  || ans[x].size() > y.size()) {         ans[x] = y;       }     }     for (int i = 0; i < t; i++) {       cout << ans[i] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long ceil(long long a, long long b) {     long long res = a / b;     if (a % b != 0) res++;     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n, m, q;     cin >> n >> m >> q;     vector<int> a(n), b(m);     long long total = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (i % 2 == 0) {         total += a[i];       } else {         total -= a[i];       }     }     for (int i = 0; i < m; i++) {       cin >> b[i];     }     set<long long> pos_sum;     long long sum_even = 0, sum_odd = 0;     for (int i = 0; i < n; i++) {       if (i % 2 == 0) {         sum_even += b[i];       } else {         sum_odd += b[i];       }     }     for (int j = 0; j < m - n + 1; j++) {       if (j != 0) {         if ((j - 1) % 2 == 0) {           sum_even -= b[j - 1];         } else {           sum_odd -= b[j - 1];         }         if ((j + n - 1) % 2 == 0) {           sum_even += b[j + n - 1];         } else {           sum_odd += b[j + n - 1];         }       }       if (j % 2 == 0) {         pos_sum.insert(-sum_odd + sum_even);       } else {         pos_sum.insert(-sum_even + sum_odd);       }     }     auto find = [&]() {       auto ub = pos_sum.upper_bound(total);       long long res = LONG_LONG_MAX;       if (ub != pos_sum.end()) {         res = min(res, abs(total - *ub));       }       if (ub != pos_sum.begin()) {         ub--;         res = min(res, abs(total - *ub));       }       return res;     };     cout << find() <<   n ;     while (q--) {       int l, r, x;       cin >> l >> r >> x;       l--, r--;       long long len = r - l + 1;       if (l % 2 == 0) {         total += ceil(len, 2) * x;         total -= (len / 2) * x;       } else {         total -= ceil(len, 2) * x;         total += (len / 2) * x;       }       cout << find() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string k;     cin >> k;     int st = 0;     while (st < (int)k.length() && k[st] ==  a  && 1) {       st++;     }     if (st == (int)k.length()) {       k[k.length() - 1] =  z ;       cout << k << endl;       return 0;     }     int se = st + 1;     while (se < (int)k.length() && k[se] !=  a  && 1) {       se++;     }     se--;     for (int i = st; i <= se; i++) {       k[i]--;     }     cout << k << endl;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, x, y;     cin >> n >> x >> y;     vector<int> nums(n);     vector<int> a(n);     vector<vector<int>> freq(n + 1);     vector<vector<int>> ind(n + 1 + 1);     vector<bool> mis(n);     for (int i = 0; i < n; i++) {       cin >> nums[i];       mis[i] = false;       ind[nums[i]].push_back(i);     }     for (int i = 1; i <= n + 1; i++) {       freq[ind[i].size()].push_back(i);     }     int f = n;     for (int k = 0; k < x; k++) {       while (freq[f].empty()) {         f--;       }       int col = freq[f].back();       int idx = ind[col].back();       a[idx] = col;       ind[col].pop_back();       freq[f].pop_back();       freq[f - 1].push_back(col);     }     while (f > 0 && freq[f].empty()) f--;     if (2 * f > 2 * n - x - y) {       cout <<  NO n ;       return;     }     vector<int> ve;     for (int i = 1; i <= f; i++) {       for (int color : freq[i]) {         ve.insert(ve.end(), ind[color].begin(), ind[color].end());       }     }     int mismatch = n - y;     auto makemismatch = [&](int i) {       a[i] = freq[0][0];       mismatch--;       mis[i] = true;     };     for (int i = 0; i < n - x; i++) {       a[ve[i]] = nums[ve[(i + (n - x) / 2) % (n - x)]];       if (a[ve[i]] == nums[ve[i]]) {         makemismatch(ve[i]);       }     }     for (int i = 0; mismatch > 0; i++) {       if (!mis[ve[i]]) makemismatch(ve[i]);     }     cout <<  YES n ;     for (int i = 0; i < n; i++) {       cout << a[i] <<    ;     }     cout <<   n ;   }   int main() {     int t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     n--;     int a[n];     for (int i = 0; i < n; ++i) {       cin >> a[i];       if (a[i] == 0) --i;     }     int principi;     int b[n];     for (int i = 0; i < n; ++i) {       cin >> b[i];       if (b[i] == 0) --i;       if (b[i] == a[0]) principi = i;     }     bool bo = true;     for (int i = 1; bo and i < n; ++i) {       if (a[i] != b[(principi + i) % n]) bo = false;     }     if (bo)       cout <<  YES  << endl;     else       cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int x, y, z, id;   } e[200005];   vector<int> to[200005];   int fa[200005], f[200005][18], deep[200005], g[200005][18], id[200005],       ans[200005];   bool flag[200005];   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   void dfs(int x, int fa) {     f[x][0] = fa, deep[x] = deep[fa] + 1;     for (auto tar : to[x])       if (tar != fa) dfs(tar, x);   }   pair<int, int> lca(int x, int y) {     int res = 0;     if (deep[x] < deep[y]) swap(x, y);     for (int k = 17; k >= 0; k--)       if (deep[f[x][k]] >= deep[y]) res = max(res, g[x][k]), x = f[x][k];     if (x == y) return {x, res};     for (int k = 17; k >= 0; k--)       if (f[x][k] != f[y][k])         res = max(res, max(g[x][k], g[y][k])), x = f[x][k], y = f[y][k];     res = max(res, max(g[x][0], g[y][0]));     return {f[x][0], res};   }   void merge(int from, int to, int v) {     from = find(from);     while (deep[from] > deep[to]) {       ans[id[from]] = v;       fa[from] = find(f[from][0]);       from = find(from);     }   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++)       scanf( %d%d%d , &e[i].x, &e[i].y, &e[i].z), e[i].id = i;     sort(e + 1, e + m + 1, [&](edge a, edge b) { return a.z < b.z; });     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++) {       int fx = find(e[i].x), fy = find(e[i].y);       if (fx != fy)         to[e[i].x].push_back(e[i].y), to[e[i].y].push_back(e[i].x), fa[fx] = fy;       else         flag[i] = 1;     }     dfs(1, 0);     for (int i = 1; i <= m; i++)       if (!flag[i])         g[deep[e[i].x] < deep[e[i].y] ? e[i].y : e[i].x]          [0] = e[i].z,             id[deep[e[i].x] < deep[e[i].y] ? e[i].y : e[i].x] = e[i].id;     for (int k = 1; k <= 17; k++)       for (int i = 1; i <= n; i++)         f[i][k] = f[f[i][k - 1]][k - 1],         g[i][k] = max(g[i][k - 1], g[f[i][k - 1]][k - 1]);     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++)       if (flag[i]) {         auto g = lca(e[i].x, e[i].y);         ans[e[i].id] = g.second;         merge(e[i].x, g.first, e[i].z), merge(e[i].y, g.first, e[i].z);       }     for (int i = 1; i <= m; i++) printf( %d  , ans[i] ? ans[i] - 1 : -1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int to, next;   } e[400005];   int n, k, num, head[200005], a[200005], f[200005], g[200005], h[200005],       g_node[200005], ans, maxa, size[200005];   bool ok[200005];   inline void insert(int u, int v) {     e[++num].to = v;     e[num].next = head[u];     head[u] = num;     e[++num].to = u;     e[num].next = head[v];     head[v] = num;   }   void dfs(int now, int fa) {     size[now] = 1;     int s = 0, maxc = 0, secc = 0, id = 0;     for (int i = head[now]; i; i = e[i].next)       if (e[i].to != fa) {         dfs(e[i].to, now);         size[now] += size[e[i].to];         if (ok[e[i].to]) {           int ch = f[e[i].to] + g[e[i].to] + 1;           if (ch == size[e[i].to])             s += ch;           else if (ch > maxc)             secc = maxc, maxc = ch, id = e[i].to;           else if (ch > secc)             secc = ch;         }       }     if (ok[now]) {       f[now] = s;       g[now] = maxc;       h[now] = secc;       g_node[now] = id;     }   }   void work(int now, int fa, int out) {     if (ok[now]) {       if (out == n - size[now])         f[now] += out;       else {         if (out > g[now])           h[now] = g[now], g[now] = out, g_node[now] = fa;         else if (out > h[now])           h[now] = out;       }       num = max(num, f[now] + g[now] + 1);       if (num >= k) return;     }     for (int i = head[now]; i; i = e[i].next)       if (e[i].to != fa) {         if (!ok[now])           work(e[i].to, now, 0);         else {           if (ok[e[i].to] && f[e[i].to] + g[e[i].to] + 1 == size[e[i].to])             work(e[i].to, now, f[now] - size[e[i].to] + g[now] + 1);           else if (e[i].to == g_node[now])             work(e[i].to, now, f[now] + h[now] + 1);           else             work(e[i].to, now, f[now] + g[now] + 1);         }       }   }   inline bool check(int x) {     for (int i = 1; i <= n; i++) ok[i] = a[i] >= x;     memset(f, 0, sizeof(f));     memset(g, 0, sizeof(g));     memset(h, 0, sizeof(h));     dfs(1, 0);     num = 0;     work(1, 0, 0);     return num >= k;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       maxa = max(maxa, a[i]);     }     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d%d , &u, &v);       insert(u, v);     }     int l = 1, r = maxa;     while (l <= r) {       int mid = (l + r) >> 1;       if (check(mid))         ans = mid, l = mid + 1;       else         r = mid - 1;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double Pi = acos(-1.0);   void DEBUG(string name, int* begin, int* end) {     printf( DEBUG: %s = { , name.c_str());     for (; begin != end; begin++) {       printf( %5d, , *begin);     }     printf( %c} n , (char)8);   }   void DEBUG(string name, long long* begin, long long* end) {     printf( DEBUG: %s = { , name.c_str());     for (; begin != end; begin++) {       printf( %5lld, , *begin);     }     printf( %c} n , (char)8);   }   void DEBUG(string name, double* begin, double* end) {     printf( DEBUG: %s = { , name.c_str());     for (; begin != end; begin++) {       printf( %5lf, , *begin);     }     printf( %c} n , (char)8);   }   void DEBUG(string name, vector<int> nums) {     printf( DEBUG: %s = { , name.c_str());     for (int i : nums) {       printf( %5d, , i);     }     printf( %c} n , (char)8);   }   void DEBUG(string name, set<int> nums) {     printf( DEBUG: %s = { , name.c_str());     for (int i : nums) {       printf( %5d, , i);     }     printf( %c} n , (char)8);   }   void DEBUG(string name, vector<long long> nums) {     printf( DEBUG: %s = { , name.c_str());     for (int i : nums) {       printf( %5lld, , i);     }     printf( %c} n , (char)8);   }   void DEBUG(string name, set<long long> nums) {     printf( DEBUG: %s = { , name.c_str());     for (int i : nums) {       printf( %5lld, , i);     }     printf( %c} n , (char)8);   }   void DEBUG(string name, map<int, int> nums) {     printf( DEBUG: %s = { , name.c_str());     for (auto i : nums) {       printf( (%d -> %d), , i.first, i.second);     }     printf( %c} n , (char)8);   }   void DEBUG(string name, queue<int> nums) {     if (nums.size() == 0) {       printf( DEBUG: %s = [] n , name.c_str());       return;     }     printf( DEBUG: %s = [ , name.c_str());     while (!nums.empty()) {       printf( %5d, , nums.front());       nums.pop();     }     printf( %c] n , (char)8);   }   void DEBUG(string name, int num) {     printf( DEBUG: %s = %d n , name.c_str(), num);   }   void DEBUG(string name, long long num) {     printf( DEBUG: %s = %lld n , name.c_str(), num);   }   void DEBUG(string name, double num) {     printf( DEBUG: %s = %lf n , name.c_str(), num);   }   int cross(int a1, int b1, int a2, int b2) { return (a1 * b2 - a2 * b1); }   int ccw(pair<int, int> a, pair<int, int> b, pair<int, int> c) {     return cross(b.first - a.first, b.second - a.second, c.first - a.first,                  c.second - a.second);   }   void hi() { printf( hi n ); }   long long n;   long long times[100010];   long long t[100010], k[100010];   vector<pair<int, int> > adj[100010];   bool visited[100010];   void dfs(int node) {     k[node] = 1;     for (pair<int, int> i : adj[node]) {       if (!visited[i.first]) {         visited[i.first] = true;         dfs(i.first);         k[node] += k[i.first];         t[node] += t[i.first] + 2 * i.second;       }     }   }   bool comp(pair<int, int> a, pair<int, int> b) {     return (t[a.first] + 2 * a.second) * (k[b.first]) <            (t[b.first] + 2 * b.second) * (k[a.first]);   }   long long curr_time;   void dfs2(int node) {     times[node] = curr_time;     for (pair<int, int> i : adj[node]) {       if (!visited[i.first]) {         visited[i.first] = true;         curr_time += i.second;         dfs2(i.first);         curr_time += i.second;       }     }   }   int main() {     cin >> n;     for (int i = 0; i < n - 1; i++) {       int a, b, c;       cin >> a >> b >> c;       a--;       b--;       adj[a].push_back(pair<int, int>(b, c));       adj[b].push_back(pair<int, int>(a, c));     }     visited[0] = true;     dfs(0);     for (int i = 0; i < n; i++) {       sort(adj[i].begin(), adj[i].end(), comp);     }     memset(visited, 0, sizeof(visited));     visited[0] = true;     dfs2(0);     double sum = 0;     for (int i = 0; i < n; i++) {       sum += times[i];     }     printf( %.41lf , sum / (n - 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 6;   int n, k;   int Pow10[10], F[10];   long long f[N];   long long cal(int n) {     int t = 0;     long long s = 0;     while (n > 0) {       int x = n % 10;       if (x % 3 == 0) s += 1LL * F[t] * x / 3;       n /= 10;       t++;     }     return s;   }   int main() {     ios ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     if (fopen( main.in ,  r )) freopen( main.in ,  r , stdin);     cin >> k;     Pow10[0] = 1;     for (int i = 0; i < 6; i++) {       cin >> F[i];       if (i) Pow10[i] = Pow10[i - 1] * 10;     }     int _, n;     cin >> _ >> n;     for (int i = 0; i <= n; i++) f[i] = -1e15;     f[0] = 0;     for (int t = 0; t < 6; t++) {       int s = 0, p = 1;       while (s + p <= 3 * (k - 1)) {         s += p;         for (int i = n; i >= 3LL * p * Pow10[t]; i--)           f[i] = max(f[i], f[i - 3 * p * Pow10[t]] + 1LL * p * F[t]);         p *= 2;       }       s = 3 * (k - 1) - s;       for (int i = n; i >= 3LL * s * Pow10[t]; i--)         f[i] = max(f[i], f[i - 3 * s * Pow10[t]] + 1LL * s * F[t]);     }     long long res = 0;     for (int i = 0; i <= n; i++) {       res = max(res, cal(n - i) + f[i]);     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   int a, b, c;   int t, x = 0;   int ans = 0;   void find(int x) {     for (t = x; t <= c + x; t++)       if (b * x + (b - a) * (t - x) < 0) {         x++;         ans++;         find(x);         break;       }   }   int main() {     scanf( %d%d%d , &a, &b, &c);     find(x);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     sort(a.begin(), a.end());     bool ok = true;     int cur = -1;     int q = 0;     for (int i = 0; i < n; ++i) {       if (q == 0) {         if (cur != -1 && a[i] != a[i - 1]) {           ok = false;           break;         }         q = 1;         cur = a[i];       } else {         if (cur == a[i]) {           ++q;         } else {           if (cur == a[i] - 1) {             --q;           } else {             ok = false;             break;           }         }       }     }     ok &= q == 0;     if (ok) {       cout <<  YES n ;     } else {       cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x;     cin >> x;     x = abs(x);     int pos = 0;     int i;     for (i = 1; pos < x; i++) pos += i;     int diff = pos - x;     if (diff % 2 == 0)       cout << (i - 1) << endl;     else {       if (i % 2 != 0)         cout << i << endl;       else         cout << i + 1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   int node[101], h[101], perm[101], fav[101];   int getP(int c) {     int p = node[c];     if (p < 0) return c;     while (node[p] > 0) p = node[p];     return node[c] = p;   }   void join(int a, int b) {     if (0) printf( join %d %d n , a, b);     int pa = getP(a), pb = getP(b);     if (pa == pb) {       return;     }     if (h[pa] < h[pb]) {       node[pa] = pb;       h[pb] += h[pa];     } else {       node[pb] = pa;       h[pa] += h[pb];     }   }   void getNum() {     int i;     for (i = 0; i <= n; i++) {       node[i] = -1;       h[i] = 1;     }     for (i = 1; i <= n; i++) {       if (i + fav[i] <= n) join(i, i + fav[i]);       if (i - fav[i] > 0) join(i, i - fav[i]);     }     for (i = 1; i <= n; i++)       if (getP(i) != getP(perm[i])) {         if (0) printf( %d != %d n , i, perm[i]);         printf( NO );         return;       }     printf( YES );     return;   }   int main() {     int a, b, c;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &perm[i]);     }     for (int i = 1; i <= n; i++) scanf( %d , &fav[i]);     getNum();     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 100, mod = 998244353, inf = 2e9;   int n, p[N], v[N], x[N], cnt, ans;   int qpow(int x, int y) {     int ret = 1;     while (y) {       if (y & 1) ret = 1ll * ret * x % mod;       x = 1ll * x * x % mod, y >>= 1;     }     return ret;   }   struct node {     int x, y, p, q, tp;     bool operator<(const node &tp) const {       return 1ll * p * tp.q < 1ll * q * tp.p;     }   } res[N * 3];   struct matrix {     int s[2][2];     void init(int o) {       if (o == 1)         for (int i = 0; i < 2; ++i)           for (int j = 0; j < 2; ++j) s[i][j] = (i == j);       else {         for (int i = 0; i < 2; ++i)           for (int j = 0; j < 2; ++j) s[i][j] = 0;         s[0][0] = 1;       }     }     matrix operator*(matrix b) {       matrix ret;       for (int i = 0; i < 2; ++i)         for (int j = 0; j < 2; ++j) {           ret.s[i][j] = 0;           for (int k = 0; k < 2; ++k)             ret.s[i][j] = (ret.s[i][j] + 1ll * s[i][k] * b.s[k][j]) % mod;         }       return ret;     }   } trans[N], I, H;   struct SegmentTree {     int L[N << 2], R[N << 2];     matrix F[N << 2];     void build(int k, int l, int r) {       L[k] = l, R[k] = r;       if (l == r) {         F[k] = trans[l];         return;       }       int mid = l + r >> 1;       build(k << 1, l, mid);       build(k << 1 | 1, mid + 1, r);       F[k] = F[k << 1] * F[k << 1 | 1];     }     matrix Query(int k, int l, int r) {       if (r < l) return I;       if (l <= L[k] && R[k] <= r) {         return F[k];       }       int mid = L[k] + R[k] >> 1;       matrix ret = I;       if (l <= mid) ret = Query(k << 1, l, r);       if (r > mid) ret = ret * Query(k << 1 | 1, l, r);       return ret;     }     void Modify(int k, int x) {       if (L[k] == R[k]) {         F[k] = trans[x];         return;       }       int mid = L[k] + R[k] >> 1;       if (x <= mid)         Modify(k << 1, x);       else         Modify(k << 1 | 1, x);       F[k] = F[k << 1] * F[k << 1 | 1];     }   } Tr;   int main() {     scanf( %d , &n);     for (int i = 1, c = qpow(100, mod - 2); i <= n; ++i)       scanf( %d%d%d , &x[i], &v[i], &p[i]), p[i] = mod + 1 - 1ll * c * p[i] % mod,                                             p[i] %= mod;     if (n == 1) {       puts( 0 );       return 0;     }     for (int i = 2; i <= n; ++i) {       if (v[i] > v[i - 1])         res[++cnt] = (node){i - 1, i, x[i] - x[i - 1], v[i] - v[i - 1], 0};       res[++cnt] = (node){i - 1, i, x[i] - x[i - 1], v[i] + v[i - 1], 1};       if (v[i - 1] > v[i])         res[++cnt] = (node){i - 1, i, x[i] - x[i - 1], v[i - 1] - v[i], 2};     }     sort(res + 1, res + cnt + 1);     I.init(1);     H.init(2);     for (int i = 1; i <= n; ++i)       trans[i].s[0][0] = trans[i].s[1][0] = p[i],       trans[i].s[0][1] = trans[i].s[1][1] = (mod + 1 - p[i]) % mod;     Tr.build(1, 1, n);     for (int i = 1; i <= cnt; ++i) {       int x = res[i].x, y = res[i].y;       matrix pre = Tr.Query(1, 1, x), suf = Tr.Query(1, y + 1, n), Y;       Y.init(2);       Y.s[0][0] = 0;       if (res[i].tp == 0) Y.s[0][0] = p[y];       if (res[i].tp == 1) Y.s[1][0] = p[y];       if (res[i].tp == 2) Y.s[1][1] = (1 + mod - p[y]) % mod;       matrix tmp = pre * Y * suf;       ans = (ans + 1ll * (tmp.s[0][1] + tmp.s[0][0]) * res[i].p % mod *                        qpow(res[i].q, mod - 2) % mod) %             mod;       if (res[i].tp == 0) trans[y].s[0][0] = 0;       if (res[i].tp == 1) trans[y].s[1][0] = 0;       if (res[i].tp == 2) trans[y].s[1][1] = 0;       Tr.Modify(1, y);     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     long long x, q, b;     for (int i = k - 1; i >= 1; i--) {       x = n / i * k + i;       q = x % k;       b = x / k;       if (q * b == n) return cout << x, 0;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     cout << (n - 1) * 2 + 1 <<     << 2 << endl;     cout << 1 <<     << 2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> vec(n);     for (int i = 0; i < n; ++i) cin >> vec[i];     int sum = accumulate(vec.begin(), vec.end(), 0);     if ((sum / 100) % 2 != 0) {       cout <<  NO n ;       return 0;     }     sum /= 2;     int num_one = count(vec.begin(), vec.end(), 100);     int num_two = count(vec.begin(), vec.end(), 200);     int curr = 0;     while (curr <= sum && num_two > 0) {       curr += 200;       num_two--;     }     if (curr == sum) {       cout <<  YES n ;       return 0;     }     if (curr > sum) curr -= 200;     while (curr != sum && num_one > 0) {       curr += 100;       num_one--;     }     if (curr == sum)       cout <<  YES n ;     else       cout <<  NO n ;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   template <typename T>   inline void umax(T &a, T b) {     a = max(a, b);   }   template <typename T>   inline void umin(T &a, T b) {     a = min(a, b);   }   const long long N = 1e5 + 10;   const long long INF = 1e16 + 10;   const pair<long long, long long> NINF = make_pair(INF, INF);   const long double EPS = 1e-6;   const long long II = 1e9;   const long long MOD = 1e9 + 7;   long long dp[1 << 22];   long long cnt[50][50];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m;     cin >> n >> m;     string second;     cin >> second;     for (long long i = 0; i < (n - 1); i++) {       cnt[second[i] -  a ][second[i + 1] -  a ]++;       cnt[second[i + 1] -  a ][second[i] -  a ]++;     }     for (long long i = 0; i < (1 << m); i++) dp[i] = INF;     for (long long i = 0; i < (m); i++) dp[1 << i] = 0;     for (long long msk = 0; msk < (1 << m); msk++) {       long long cur = dp[msk];       for (long long i = 0; i < (m); i++)         if (msk & (1 << i))           for (long long j = 0; j < (m); j++)             if ((msk & (1 << j)) == 0) cur += cnt[i][j];       for (long long j = 0; j < (m); j++)         if ((msk & (1 << j)) == 0) umin(dp[msk | (1 << j)], cur);     }     cout << dp[(1 << m) - 1];   }


#include <bits/stdc++.h>   using namespace std;   bool myfunction(int i, int j) { return (i < j); }   istream &in = cin;   int n;   bool adj[1501][1501];   int vtx[1501];   vector<bool> chk;   vector<pair<pair<int, int>, int> > points;   vector<pair<int, int> > res;   int dfs(int node) {     int cnt = 0;     chk[node] = true;     for (int i = 1; i <= n; ++i)       if (!chk[i] && adj[node][i]) cnt += dfs(i);     vtx[node] = cnt + 1;     return vtx[node];   }   void go(int root, vector<pair<pair<int, int>, int> > ps) {     int min_y = INT_MAX, min_i = -1;     for (int i = 1; i <= ps.size() - 1; ++i)       if (ps[i].first.second < min_y) {         min_y = ps[i].first.second;         min_i = i;       }     res.push_back(make_pair(ps[min_i].second, root));     vector<pair<long double, int> > tps(1);     for (int i = 1; i <= ps.size() - 1; ++i)       if (i != min_i)         tps.push_back(             make_pair(atan2l(ps[i].first.first - ps[min_i].first.first,                              ps[i].first.second - ps[min_i].first.second),                       i));     sort(tps.begin() + 1, tps.end());     int s = 1;     for (int i = 1; i <= n; ++i)       if (adj[root][i]) {         vector<pair<pair<int, int>, int> > nps(1);         while (nps.size() < vtx[i] + 1) {           nps.push_back(ps[tps[s].second]);           ++s;         }         adj[i][root] = false;         go(i, nps);       }   }   int main() {     in >> n;     for (int i = (0); i < (n + 1); ++i)       for (int j = (0); j < (n + 1); ++j) adj[i][j] = false;     points.resize(n + 1);     chk.resize(n + 1, false);     memset(vtx, 0, sizeof(vtx));     for (int i = (0); i < (n - 1); ++i) {       int a, b;       in >> a >> b;       adj[a][b] = adj[b][a] = true;     }     for (int i = (0); i < (n); ++i) {       int x, y;       in >> x >> y;       points[i + 1] = make_pair(make_pair(x, y), i + 1);     }     dfs(1);     sort(points.begin() + 1, points.end());     for (int i = (0); i < (n + 1); ++i) chk[i] = false;     go(1, points);     sort(res.begin(), res.end());     for (int i = 0; i < res.size(); ++i) cout << res[i].second <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e3 + 500;   const double eps = 1e-8;   int sgn(double x) {     if (fabs(x) < eps) return 0;     if (x < 0)       return -1;     else       return 1;   }   struct Point {     double x, y;     Point() {}     Point(double _x, double _y) : x(_x), y(_y) {}     void input() { scanf( %lf%lf , &x, &y); }     void output() { printf( %.0lf %.0lf n , x, y); }     Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); }     bool operator==(const Point &b) const {       return sgn(x - b.x) == 0 && sgn(y - b.y) == 0;     }     bool operator<(const Point &b) const {       return sgn(x - b.x) == 0 ? sgn(y - b.y) < 0 : x < b.x;     }     double operator^(const Point &b) const { return x * b.y - y * b.x; }     double operator*(const Point &b) const { return x * b.x + y * b.y; }     double distance(Point b) { return hypot(x - b.x, y - b.y); }   };   struct polygon {     int n;     Point p[maxn], p1, p2, p3;     void input(int _n) {       n = _n;       for (int i = 0; i < n; ++i) p[i].input();     }     struct cmp {       Point p;       cmp(const Point &p0) { p = p0; }       bool operator()(const Point &aa, const Point &bb) {         Point a = aa, b = bb;         int d = sgn((a - p) ^ (b - p));         if (d == 0) return sgn(a.distance(p) - b.distance(p)) < 0;         return d > 0;       }     };     void norm() {       Point mi = p[0];       for (int i = 1; i < n; ++i) mi = min(mi, p[i]);       sort(p, p + n, cmp(mi));     }     void Graham(polygon &convex) {       norm();       int &top = convex.n;       top = 0;       if (n == 1) {         top = 1;         convex.p[0] = p[0];         return;       }       if (n == 2) {         top = 2;         convex.p[0] = p[0];         convex.p[1] = p[1];         if (convex.p[0] == convex.p[1]) top--;         return;       }       convex.p[0] = p[0];       convex.p[1] = p[1];       top = 2;       for (int i = 2; i < n; ++i) {         while (top > 1 && sgn((convex.p[top - 1] - convex.p[top - 2]) ^                               (p[i] - convex.p[top - 2])) <= 0)           top--;         convex.p[top++] = p[i];       }       if (convex.n == 2 && (convex.p[0] == convex.p[1])) convex.n--;     }     void rotating_calipers() {       double ans = 0;       for (int i = 0; i < n; ++i) {         int k = i + 1;         for (int j = i + 1; j < n; ++j) {           while (sgn(fabs((p[j] - p[i]) ^ (p[(k + 1) % n] - p[i])) -                      fabs((p[j] - p[i]) ^ (p[k] - p[i]))) > 0)             k = (k + 1) % n;           if (ans < 0.5 * fabs((p[j] - p[i]) ^ (p[k] - p[i]))) {             p1 = p[i];             p2 = p[j];             p3 = p[k];             ans = 0.5 * fabs((p[j] - p[i]) ^ (p[k] - p[i]));           }         }       }     }   } A, B;   int n;   long long s;   int main() {     scanf( %d%lld , &n, &s);     A.input(n);     A.Graham(B);     B.rotating_calipers();     printf( %.0lf %.0lf n , B.p1.x + B.p2.x - B.p3.x, B.p1.y + B.p2.y - B.p3.y);     printf( %.0lf %.0lf n , B.p1.x + B.p3.x - B.p2.x, B.p1.y + B.p3.y - B.p2.y);     printf( %.0lf %.0lf n , B.p3.x + B.p2.x - B.p1.x, B.p3.y + B.p2.y - B.p1.y);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int A[(1 << 17)];   int A0[(1 << 17)];   int A1[(1 << 17)];   int C[(1 << 17)];   int RMQ[2 << 20];   int I[32];   void Add(int a, int b) {     int i;     for (i = (0); i < (20); ++i) RMQ[I[i] + (a >> i)] += b;   }   int Sum(int a) {     int res = 0;     int i;     for (i = (0); i < (20); ++i) {       if (a & 1) res += RMQ[I[i] + a - 1];       a >>= 1;     }     return res;   }   int main() {     int N, M;     scanf( %d%d , &N, &M);     vector<int> v;     int i;     for (i = (0); i < (M); ++i) {       scanf( %d , &A[i]);       if (A[i] == 0)         scanf( %d%d , &A0[i], &A1[i]);       else         v.push_back(A[i]);     }     sort((v).begin(), (v).end());     v.resize(unique((v).begin(), (v).end()) - v.begin());     for (i = (0); i < (M); ++i)       if (A[i] != 0)         A[i] = int(lower_bound((v).begin(), (v).end(), A[i]) - v.begin()) + 1;     set<pair<int, int> > bl, lb;     bl.insert(pair<int, int>(-1, -N));     lb.insert(pair<int, int>(-N, -1));     v.clear();     memset(C, -1, sizeof(C));     for (i = (0); i < (M); ++i)       if (A[i] != 0)         if (C[A[i]] == -1) {           pair<int, int> t = *lb.begin();           lb.erase(lb.begin());           bl.erase(pair<int, int>(t.second, t.first));           int l = -t.second;           int r = -t.second - t.first;           int m = (l + r) / 2;           bl.insert(pair<int, int>(-l, l - m));           lb.insert(pair<int, int>(l - m, -l));           bl.insert(pair<int, int>(-(m + 1), (m + 1) - r));           lb.insert(pair<int, int>((m + 1) - r, -(m + 1)));           A0[i] = m;           C[A[i]] = m;           v.push_back(m);         } else {           pair<int, int> t(-C[A[i]], 0);           set<pair<int, int> >::iterator it0 = bl.lower_bound(t);           set<pair<int, int> >::iterator it1 = it0;           --it1;           pair<int, int> r = *it1;           pair<int, int> l = *it0;           bl.erase(l);           bl.erase(r);           lb.erase(pair<int, int>(l.second, l.first));           lb.erase(pair<int, int>(r.second, r.first));           l.second += r.second - 1;           bl.insert(l);           lb.insert(pair<int, int>(l.second, l.first));           A0[i] = -C[A[i]];           C[A[i]] = -1;         }     for (i = (0); i < (M); ++i)       if (A[i] == 0) {         ++A1[i];         v.push_back(A0[i]);         v.push_back(A1[i]);       }     sort((v).begin(), (v).end());     v.resize(unique((v).begin(), (v).end()) - v.begin());     memset(RMQ, 0, sizeof(RMQ));     I[0] = 0;     for (i = (1); i < (20); ++i) I[i] = I[i - 1] + (1 << (21 - i));     for (i = (0); i < (M); ++i)       if (A[i] == 0) {         A0[i] = lower_bound((v).begin(), (v).end(), A0[i]) - v.begin();         A1[i] = lower_bound((v).begin(), (v).end(), A1[i]) - v.begin();         printf( %d n , Sum(A1[i]) - Sum(A0[i]));       } else if (A0[i] < 0) {         A0[i] = lower_bound((v).begin(), (v).end(), -A0[i]) - v.begin();         Add(A0[i], -1);       } else {         A0[i] = lower_bound((v).begin(), (v).end(), A0[i]) - v.begin();         Add(A0[i], 1);       }     return 0;   };


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const int INF = 213456789;   const double eps = 1e-7;   int flag1 = 2, flag2 = 2, n, m;   template <class T>   T gcd(T a, T b) {     if (!b) return a;     return gcd(b, a % b);   }   int a[110][110];   int dy[] = {1, -1, 0, 0};   int dx[] = {0, 0, -1, 1};   int prex[110][110];   int prey[110][110];   int cost[110][110];   int dd[110][110];   bool over;   int edx, edy;   void print(int nx, int ny) {     if (nx == edx && ny == edy) return;     print(prex[nx][ny], prey[nx][ny]);     if (flag1 && dd[nx][ny] == 0) printf( L n );     if (!flag1 && dd[nx][ny] == 0) printf( R n );     if (flag1 && dd[nx][ny] == 1) printf( R n );     if (!flag1 && dd[nx][ny] == 1) printf( L n );     if (flag2 && dd[nx][ny] == 2) printf( D n );     if (!flag2 && dd[nx][ny] == 2) printf( U n );     if (flag2 && dd[nx][ny] == 3) printf( U n );     if (!flag2 && dd[nx][ny] == 3) printf( D n );     fflush(stdout);     int l, r;     scanf( %d%d , &l, &r);   }   void bfs(int x, int y) {     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cost[i][j] = INF;       }     }     edx = x, edy = y;     cost[x][y] = 0;     queue<pair<int, int> > q;     q.push(make_pair(x, y));     while (!q.empty()) {       pair<int, int> tmp = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         int nx = dx[i] + tmp.first, ny = dy[i] + tmp.second;         if (nx > 0 && nx <= n && ny > 0 && ny <= m && cost[nx][ny] == INF &&             a[nx][ny] != 3) {           if (a[nx][ny] == 2) {             dd[nx][ny] = i;             prex[nx][ny] = tmp.first;             prey[nx][ny] = tmp.second;             cost[nx][ny] = cost[tmp.first][tmp.second] + 1;             print(nx, ny);             over = 1;             return;           }           dd[nx][ny] = i;           prex[nx][ny] = tmp.first;           prey[nx][ny] = tmp.second;           cost[nx][ny] = cost[tmp.first][tmp.second] + 1;           q.push(make_pair(nx, ny));         }       }     }   }   void check1() {     int i = 1;     while (a[i][2] == 3) {       if (flag2)         printf( U n );       else         printf( D n );       fflush(stdout);       int l, r;       scanf( %d%d , &l, &r);       if (a[l][r] == 2) {         over = 1;         return;       }       i++;     }     printf( L n );     fflush(stdout);     int l, r;     scanf( %d%d , &l, &r);     if (a[l][r] == 2) {       over = 1;       return;     }     if (l == i && r == 2) {       flag1 = 1;       printf( R n );       fflush(stdout);       scanf( %d%d , &l, &r);       if (a[l][r] == 2) {         over = 1;         return;       }       bfs(l, r);     } else {       flag1 = 0;       bfs(l, r);     }   }   void check2() {     int i = 1;     while (a[2][i] == 3) {       if (flag1)         printf( L n );       else         printf( R n );       fflush(stdout);       int l, r;       scanf( %d%d , &l, &r);       if (a[l][r] == 2) {         over = 1;         return;       }       i++;     }     printf( U n );     fflush(stdout);     int l, r;     scanf( %d%d , &l, &r);     if (a[l][r] == 2) {       over = 1;       return;     }     if (l == 2 && r == i) {       flag2 = 1;       printf( D n );       fflush(stdout);       scanf( %d%d , &l, &r);       if (a[l][r] == 2) {         over = 1;         return;       }       bfs(l, r);     } else {       flag2 = 0;       bfs(l, r);     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         char c;         cin >> c;         if (c ==  . )           a[i][j] = 1;         else if (c ==  F )           a[i][j] = 2;         else if (c ==  * )           a[i][j] = 3;       }     }     if (a[1][2] == 1 || a[1][2] == 2) {       printf( L n );       fflush(stdout);       int l, r;       scanf( %d%d , &l, &r);       if (a[l][r] == 2) return 0;       if (l == 1 && r == 1) {         flag1 = 0;       } else {         flag1 = 1;         printf( R n );         fflush(stdout);         scanf( %d%d , &l, &r);         if (a[l][r] == 2) return 0;       }     }     if (a[2][1] == 1 || a[2][1] == 2) {       printf( U n );       fflush(stdout);       int l, r;       scanf( %d%d , &l, &r);       if (a[l][r] == 2) return 0;       if (l == 1 && r == 1) {         flag2 = 0;       } else {         flag2 = 1;         printf( D n );         fflush(stdout);         scanf( %d%d , &l, &r);         if (a[l][r] == 2) return 0;       }     }     if (flag1 == 2) check1();     if (over) return 0;     if (flag2 == 2) check2();     if (over) return 0;     if (flag1 != 2 && flag2 != 2) bfs(1, 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   queue<int> q;   vector<int> v[1999];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (int i = 2; i <= n; i++) {       int a;       cin >> a;       v[a].push_back(i);     }     q.push(1);     int ok = 1;     while (!q.empty()) {       int cou = 0, sep = 0;       int k = q.front();       q.pop();       for (auto x : v[k]) {         sep = 1;         if (v[x].empty()) {           cou++;         }         q.push(x);       }       if (sep) {         if (cou < 3) {           ok = 0;           break;         }       }     }     if (ok) {       cout <<  YES ;     } else {       cout <<  NO ;     }   }


#include <bits/stdc++.h>   using namespace std;   int ans = -1;   map<pair<int, int>, bool> visited, mp;   void bfs(int x1, int y1, int x2, int y2) {     queue<pair<pair<int, int>, int>> q;     q.push({{x1, y1}, 0});     while (!q.empty()) {       auto z = q.front();       q.pop();       visited[z.first] = 1;       if (z.first.first == x2 && z.first.second == y2) {         ans = z.second;         break;       }       int x = z.first.first, y = z.first.second;       if (x - 1 >= 1 && !visited[{x - 1, y}] && mp[{x - 1, y}]) {         visited[{x - 1, y}] = true;         q.push({{x - 1, y}, z.second + 1});       }       if (x + 1 <= 1e9 && !visited[{x + 1, y}] && mp[{x + 1, y}]) {         visited[{x + 1, y}] = true;         q.push({{x + 1, y}, z.second + 1});       }       if (y - 1 >= 1 && !visited[{x, y - 1}] && mp[{x, y - 1}]) {         visited[{x, y - 1}] = true;         q.push({{x, y - 1}, z.second + 1});       }       if (y + 1 <= 1e9 && !visited[{x, y + 1}] && mp[{x, y + 1}]) {         visited[{x, y + 1}] = true;         q.push({{x, y + 1}, z.second + 1});       }       if (x + 1 <= 1e9 && y + 1 <= 1e9 && !visited[{x + 1, y + 1}] &&           mp[{x + 1, y + 1}]) {         visited[{x + 1, y + 1}] = true;         q.push({{x + 1, y + 1}, z.second + 1});       }       if (x + 1 <= 1e9 && y - 1 >= 1 && !visited[{x + 1, y - 1}] &&           mp[{x + 1, y - 1}]) {         visited[{x + 1, y - 1}] = true;         q.push({{x + 1, y - 1}, z.second + 1});       }       if (x - 1 >= 1 && y + 1 <= 1e9 && !visited[{x - 1, y + 1}] &&           mp[{x - 1, y + 1}]) {         visited[{x - 1, y + 1}] = true;         q.push({{x - 1, y + 1}, z.second + 1});       }       if (x - 1 >= 1 && y - 1 >= 1 && !visited[{x - 1, y - 1}] &&           mp[{x - 1, y - 1}]) {         visited[{x - 1, y - 1}] = true;         q.push({{x - 1, y - 1}, z.second + 1});       }     }   }   int main() {     ;     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int x1, y1, x2, y2;     cin >> x1 >> y1 >> x2 >> y2;     int n;     cin >> n;     set<pair<int, pair<int, int>>> s;     for (int i = 0; i < n; i++) {       int r, a, b;       cin >> r >> a >> b;       s.insert({r, {a, b}});     }     for (auto it : s) {       for (int i = it.second.first; i <= it.second.second; i++) {         mp[{it.first, i}] = true;       }     }     mp[{x1, y1}] = true;     mp[{x2, y2}] = true;     bfs(x1, y1, x2, y2);     cout << ans;     return 0;   }