#include <bits/stdc++.h>   template <typename T>   void MACRO_VAR_Scan(T& t) {     std::cin >> t;   }   template <typename First, typename... Rest>   void MACRO_VAR_Scan(First& first, Rest&... rest) {     std::cin >> first;     MACRO_VAR_Scan(rest...);   }   template <typename T>   void MACRO_VEC_ROW_Init(int n, T& t) {     t.resize(n);   }   template <typename First, typename... Rest>   void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) {     first.resize(n);     MACRO_VEC_ROW_Init(n, rest...);   }   template <typename T>   void MACRO_VEC_ROW_Scan(int p, T& t) {     std::cin >> t[p];   }   template <typename First, typename... Rest>   void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) {     std::cin >> first[p];     MACRO_VEC_ROW_Scan(p, rest...);   }   template <class T>   inline T CHMAX(T& a, const T b) {     return a = (a < b) ? b : a;   }   template <class T>   inline T CHMIN(T& a, const T b) {     return a = (a > b) ? b : a;   }   template <class T>   std::vector<std::vector<T>> VV(int n, int m, T init = T()) {     return std::vector<std::vector<T>>(n, std::vector<T>(m, init));   }   template <typename S, typename T>   std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) {     os <<  (  << p.first <<  ,   << p.second <<  ) ;     return os;   }   using ll = long long;   using ull = unsigned long long;   using ld = long double;   using PAIR = std::pair<ll, ll>;   using PAIRLL = std::pair<ll, ll>;   constexpr ll INFINT = 1 << 30;   constexpr ll INFINT_LIM = (1LL << 31) - 1;   constexpr ll INFLL = 1LL << 60;   constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62);   constexpr double EPS = 1e-10;   constexpr ll MOD = 998244353;   constexpr double PI = 3.141592653589793238462643383279;   template <class T, size_t N>   void FILL(T (&a)[N], const T& val) {     for (auto& x : a) x = val;   }   template <class ARY, size_t N, size_t M, class T>   void FILL(ARY (&a)[N][M], const T& val) {     for (auto& b : a) FILL(b, val);   }   template <class T>   void FILL(std::vector<T>& a, const T& val) {     for (auto& x : a) x = val;   }   template <class ARY, class T>   void FILL(std::vector<std::vector<ARY>>& a, const T& val) {     for (auto& b : a) FILL(b, val);   }   std::vector<bool> banned;   std::vector<ll> pairSum;   std::vector<ll> getDiameterPath(const std::vector<std::vector<ll>>& g) {     size_t n = g.size();     std::vector<ll> depth(n), depth2(n);     ll maxDepth = 0, farthest = -1;     std::function<void(std::vector<ll>&, ll, ll)> getDepth =         [&](std::vector<ll>& d, ll v, ll par) {           if (par == -1) {             for (size_t i = 0; i < n; ++i) d[i] = n;             d[v] = 0;             maxDepth = 0, farthest = v;           }           for (auto&& to : g[v])             if (to != par && !banned[to]) {               d[to] = d[v] + 1;               if (PAIR(maxDepth, pairSum[farthest]) < PAIR(d[to], pairSum[to])) {                 maxDepth = d[farthest = to];               }               getDepth(d, to, v);             }         };     ll start = -1;     for (ll i = 0; i < ll(n); ++i)       if (!banned[i]) start = i;     getDepth(depth, start, -1);     getDepth(depth, farthest, -1);     getDepth(depth2, farthest, -1);     const ll R = (maxDepth + 1) / 2;     for (ll i = 0; i < ll(n); ++i) {       if (banned[i]) continue;       if (depth[i] <= R && depth2[i] <= R) {         getDepth(depth, i, -1);         getDepth(depth, farthest, -1);         break;       }     }     std::vector<ll> res(maxDepth + 1);     size_t now = farthest;     while (depth[now] != 0) {       res[depth[now]] = now;       for (auto&& to : g[now])         if (depth[to] + 1 == depth[now]) {           now = to;           break;         }     }     res[depth[now]] = now;     return res;   }   signed main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     ;     ll n;     MACRO_VAR_Scan(n);     ;     std::vector<ll> a, b;     MACRO_VEC_ROW_Init(n - 1, a, b);     for (ll w = 0; w < n - 1; ++w) {       MACRO_VEC_ROW_Scan(w, a, b);     };     std::vector<std::vector<ll>> g(n);     for (ll i = 0; i < ll(n - 1); ++i) {       --a[i];       --b[i];       g[a[i]].emplace_back(b[i]);       g[b[i]].emplace_back(a[i]);     }     std::vector<bool> checked(n, false);     for (ll i = 0; i < ll(n); ++i) {       if (g[i].size() >= 3) checked[i] = true;     }     banned = checked;     {       std::vector<bool> deleted(n, false);       std::vector<ll> in(n, 0);       std::queue<ll> que;       for (ll i = 0; i < ll(n); ++i) {         in[i] = g[i].size();         if (g[i].size() == 1) que.push(i), deleted[i] = true;       }       while (!que.empty()) {         ll now = que.front();         que.pop();         for (auto& to : g[now])           if (!banned[to] && !deleted[to]) {             deleted[to] = true;             if (--in[to] <= 1) que.push(to);           }       }       for (ll i = 0; i < ll(n); ++i) banned[i] = !deleted[i];     }     std::vector<ll> depth(n, INFINT);     auto getP = [&](ll start) {       std::vector<PAIR> v;       for (auto& to : g[start])         if (!banned[to]) {           ll dist = 1;           ll pre = start;           ll now = to;           while (true) {             bool ch = false;             for (auto& ne : g[now])               if (ne != pre) {                 ++dist;                 ch = true;                 pre = now;                 now = ne;                 break;               }             if (!ch) break;           }           v.emplace_back(dist, now);         }       std::sort((v).begin(), (v).end());       std::reverse((v).begin(), (v).end());       return std::vector<PAIR>({v[0], v[1]});     };     pairSum.resize(n, 0);     for (ll i = 0; i < ll(n); ++i) {       if (!banned[i]) continue;       ll cnt = 0;       for (auto& to : g[i])         if (banned[to]) ++cnt;       if (cnt == 1) {         auto pp = getP(i);         pairSum[i] = pp[0].first + pp[1].first;       }     }     for (ll i = 0; i < ll(n); ++i) banned[i] = !banned[i];     auto path = getDiameterPath(g);     for (ll i = 0; i < ll(n); ++i) banned[i] = !banned[i];     auto pa(getP(path.front()));     auto pb(getP(path.back()));     std::cout << (pa[0].second + 1);     std::cout <<    ;     std::cout << (pb[0].second + 1);     std::cout <<   n ;     ;     std::cout << (pa[1].second + 1);     std::cout <<    ;     std::cout << (pb[1].second + 1);     std::cout <<   n ;     ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 99009000;   const long long MOD = 1000000007;   const int N = 100001;   int main() {     {       ios::sync_with_stdio(false);       cin.tie(nullptr);     };     long long a, b, w, x, c;     cin >> a >> b >> w >> x >> c;     long long A = a * w + b;     long long B = w * c;     long long dif = B - A;     if (dif <= 0)       cout << 0 <<   n ;     else       cout << (dif + w - x - 1) / (w - x) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 2e5 + 5;   long long res = 0, mx = 0;   long long sub[MAX_N];   int custo[MAX_N];   vector<vector<int> > g(MAX_N);   void dfs(int node, int p = -1, int d = 0) {     res += 1LL * d * custo[node];     sub[node] = custo[node];     for (auto to : g[node]) {       if (to != p) {         dfs(to, node, d + 1);         sub[node] += sub[to];       }     }   }   void root(int node, int p = -1) {     mx = max(mx, res);     for (auto to : g[node]) {       if (to != p) {         long long old_res = res;         long long old_node = sub[node];         long long old_to = sub[to];         res -= sub[to];         sub[node] -= sub[to];         res += sub[node];         sub[to] += sub[node];         root(to, node);         res = old_res;         sub[node] = old_node;         sub[to] = old_to;       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> custo[i];     for (int i = 0; i < n - 1; i++) {       int st, et;       cin >> st >> et;       --st;       --et;       g[st].emplace_back(et);       g[et].emplace_back(st);     }     dfs(0);     root(0);     cout << mx <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:20000000 )   using namespace std;   template <typename T1, typename T2, typename T3>   struct triple {     T1 a;     T2 b;     T3 c;     triple(){};     triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}   };   template <typename T1, typename T2, typename T3>   bool operator<(const triple<T1, T2, T3> &t1, const triple<T1, T2, T3> &t2) {     if (t1.a != t2.a)       return t1.a < t2.a;     else       return t1.b < t2.b;   }   template <typename T1, typename T2>   inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T>   inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {     bool first = true;     os <<  [ ;     for (unsigned int i = 0; i < v.size(); i++) {       if (!first) os <<  ,  ;       os << v[i];       first = false;     }     return os <<  ] ;   }   template <typename T>   inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) {     bool first = true;     os <<  [ ;     for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end();          ++ii) {       if (!first) os <<  ,  ;       os << *ii;       first = false;     }     return os <<  ] ;   }   template <typename T1, typename T2>   inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) {     bool first = true;     os <<  [ ;     for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end();          ++ii) {       if (!first) os <<  ,  ;       os << *ii;       first = false;     }     return os <<  ] ;   }   template <typename T, typename T2>   void printarray(T a[], T2 sz, T2 beg = 0) {     for (T2 i = beg; i < sz; i++) cout << a[i] <<    ;   }   inline long long binpow(long long x, long long n) {     long long res = 1;     while (n) {       if (n & 1) res *= x;       x *= x;       n >>= 1;     }     return res;   }   inline long long powmod(long long x, long long n, long long _mod) {     long long res = 1;     while (n) {       if (n & 1) res = (res * x) % _mod;       x = (x * x) % _mod;       n >>= 1;     }     return res;   }   inline long long mulmod(long long x, long long n, long long _mod) {     long long res = 0;     while (n) {       if (n & 1) res = (res + x) % _mod;       x = (x + x) % _mod;       n >>= 1;     }     return res;   }   inline long long gcd(long long a, long long b) {     long long t;     while (b) {       a = a % b;       t = a;       a = b;       b = t;     }     return a;   }   inline int gcd(int a, int b) {     int t;     while (b) {       a = a % b;       t = a;       a = b;       b = t;     }     return a;   }   inline long long lcm(int a, int b) { return a / gcd(a, b) * (long long)b; }   inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   inline long long gcd(long long a, long long b, long long c) {     return gcd(gcd(a, b), c);   }   inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }   inline long long lcm(long long a, long long b, long long c) {     return lcm(lcm(a, b), c);   }   inline long long lcm(int a, int b, int c) {     return lcm(lcm(a, b), (long long)c);   }   inline long long max(long long a, long long b) { return (a > b) ? a : b; }   inline int max(int a, int b) { return (a > b) ? a : b; }   inline double max(double a, double b) { return (a > b) ? a : b; }   inline long long max(long long a, long long b, long long c) {     return max(a, max(b, c));   }   inline int max(int a, int b, int c) { return max(a, max(b, c)); }   inline double max(double a, double b, double c) { return max(a, max(b, c)); }   inline long long min(long long a, long long b) { return (a < b) ? a : b; }   inline int min(int a, int b) { return (a < b) ? a : b; }   inline double min(double a, double b) { return (a < b) ? a : b; }   inline long long min(long long a, long long b, long long c) {     return min(a, min(b, c));   }   inline int min(int a, int b, int c) { return min(a, min(b, c)); }   inline double min(double a, double b, double c) { return min(a, min(b, c)); }   inline void getar(int *a, int n) {     for (int ii = 0; ii < n; ii++) {       scanf( %d , a + ii);     }   }   inline void getar(long long *a, int n) {     for (int ii = 0; ii < n; ii++) {       scanf( %I64 , a + ii);     }   }   struct point {     int x, y;     point(int xx = 0, int yy = 0) {       x = xx;       y = yy;     }   };   bool operator<(const point &p1, const point &p2) { return p1.x < p2.x; }   int n;   point a[100010];   int minpref[100010];   int maxpref[100010];   int minsuf[100010];   int maxsuf[100010];   bool check(double m) {     for (int i = 0; i < n; i++) {       point *v =           upper_bound(a, a + n, point(a[i].x + 2 * m + (double)5 * (1e-7), 0));       double miny = min(minpref[i], minsuf[(v - a)]);       double maxy = max(maxpref[i], maxsuf[(v - a)]);       if (maxy - miny <= 2 * m) return true;     }     return false;   }   int main() {     cin >> n;     int x, y;     for (int i = 0; i < n; i++) {       scanf( %d%d , &x, &y);       a[i].x = x + y;       a[i].y = x - y;     }     sort(a, a + n);     maxpref[0] = -2111111111;     minpref[0] = 2111111111;     maxsuf[n] = -2111111111;     minsuf[n] = 2111111111;     for (int i = 1; i <= n; i++) {       minpref[i] = min(minpref[i - 1], a[i - 1].y);       maxpref[i] = max(maxpref[i - 1], a[i - 1].y);       minsuf[n - i] = min(minsuf[n - i + 1], a[n - i].y);       maxsuf[n - i] = max(maxsuf[n - i + 1], a[n - i].y);     }     double l = 0;     double r = max(1.0 * a[n - 1].x - a[0].x, 1.0 * maxsuf[0] - minsuf[0]) / 2;     while (l + (double)5 * (1e-7) < r) {       double m = (l + r) / 2;       if (check(m)) {         r = m;       } else {         l = m;       }     }     cout << fixed;     cout.precision(7);     cout << (l + r) / 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, deg[8010], type[8010], in[8010];   long long ans = 0;   bool e[8010][8010];   char s[8010];   queue<int> q;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %s , s + 1);       for (int j = 1; j <= n / 4; j++) {         int k = (s[j] <=  9  && s[j] >=  0 ) ? s[j] -  0  : s[j] -  A  + 10;         for (int l = 0; l < 4; l++) e[i][j * 4 - l] = (k >> l) & 1;       }     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (e[i][j]) deg[j]++;     for (int i = 1; i <= n; i++)       if (!deg[i]) q.push(i);     int tmp = n;     while (!q.empty()) {       int now = q.front();       q.pop();       tmp--;       type[now] = 3;       ans += (614ll * n + 1) * tmp;       for (int i = 1; i <= n; i++)         if (e[now][i]) {           deg[i]--;           if (!deg[i]) q.push(i);         }     }     if (!tmp) return printf( %lld n , ans), 0;     int pos = -1, mx = 0;     for (int i = 1; i <= n; i++)       if (deg[i] > mx) mx = deg[i], pos = i;     for (int i = 1; i <= n; i++)       if (type[i] != 3) type[i] = (i == pos || e[i][pos]) ? 1 : 2;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (i != j && type[i] != 3 && type[j] != 3 && type[i] != type[j] &&             e[i][j])           in[j]++;     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++)         if (type[i] != 3 && type[j] != 3)           ans += 3 + (type[i] == type[j] && in[i] == in[j]);     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 7, MAXM = 1e5 + 5, MOD = 1e9 + 7, bit_one = (1 << MAXN) - 1;   int n, ans;   int w[MAXN];   int pre_process[MAXN][(1 << MAXN)][(1 << MAXN)];   int add(int a, int b) {     int c = a + b;     while (c >= MOD) c -= MOD;     return c;   }   int mul(int a, int b) { return 1LL * a * b % MOD; }   struct matrix {     int data[(1 << MAXN)][(1 << MAXN)];     int n, m;     matrix(int a = (1 << MAXN), int b = (1 << MAXN), bool c = 0) {       n = a;       m = b;       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++) data[i][j] = (i == j) * c;     }     void operator*=(matrix tmp) {       matrix res(n, tmp.m);       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++)           for (int k = 0; k < tmp.m; k++)             res.data[i][k] = add(res.data[i][k], mul(data[i][j], tmp.data[j][k]));       (*this) = res;     }     void operator^=(int tmp) {       matrix res(n, m, 1);       for (; tmp; tmp /= 2) {         if (tmp & 1) res *= (*this);         (*this) *= (*this);       }       (*this) = res;     }   };   bool cheak(int a, int b, int c, int d) {     for (int i = 0; i <= a; i++)       if (((d >> i) & 1) && ((d >> (i + 1)) & 1) && ((b >> i) & 1) &&           ((c >> i) & 1))         return 0;     return 1;   }   int main() {     for (int i = 0; i < 7; i++) scanf( %d , &w[i]);     for (int i = 0; i < 7; i++)       for (int j = 0; j < (1 << (i + 1)); j++)         for (int k = 0; k < (1 << (i + 1)); k++)           for (int h = 0; h < (1 << i); h++)             pre_process[i][j][k] += cheak(i, j, k, (1 << (i + 1)) | (h << 1 | 1));     matrix M[7];     for (int i = 0; i < 7; i++) M[i].n = M[i].m = (1 << (i + 1));     for (int i = 0; i < 7; i++) {       for (int j = 0; j < (1 << (i + 1)); j++)         for (int k = 0; k < (1 << (i + 1)); k++)           M[i].data[j][k] = pre_process[i][j][k];       M[i] ^= w[i];     }     matrix A[7];     for (int i = 0; i < 7; i++) {       A[i].n = (1 << (i + 1));       A[i].m = 1;     }     A[0].data[1][0] = 1;     M[0] *= A[0];     A[0] = M[0];     for (int i = 1; i < 7; i++) {       for (int j = 0; j < (1 << i); j++) {         int tmp = j << 1 | 1;         A[i].data[tmp][0] = A[i - 1].data[j][0];       }       M[i] *= A[i];       A[i] = M[i];     }     ans = A[6].data[bit_one][0];     printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   long long sum[1000006], add[1000006];   int Q[1000006], A[1000006];   int main() {     int n, m, a;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a);       A[a + 1] = 0;       int head = 1, tail = 0, mx = 0;       for (int j = 1; j <= a; j++) scanf( %d , &A[j]), mx = max(mx, A[j]);       Q[++tail] = 0;       for (int j = 1; j <= a; j++) {         while (tail >= head && A[j] > A[Q[tail]]) tail--;         Q[++tail] = j;         while (head <= tail && j + a - Q[head] > m) head++;         sum[j] += A[Q[head]];       }       int len = a;       head = tail = 1;       Q[1] = a + 1;       for (int j = m; j >= max(m - a + 1, a + 1); j--) {         while (tail >= head && A[len] > A[Q[tail]]) tail--;         Q[++tail] = len--;         while (head <= tail && j - Q[head] < 0) head++;         sum[j] += A[Q[head]];       }       if (a * 2 < m) add[a + 1] += mx, add[m - a + 1] -= mx;     }     for (int i = 1; i <= m; i++)       add[i] += add[i - 1], sum[i] += add[i], printf( %lld  , sum[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int n, m;       cin >> n >> m;       vector<long long int> graph[n + 1];       for (long long int i = 0; i < m; i++) {         long long int u, v;         cin >> u >> v;         graph[u].push_back(v);         graph[v].push_back(u);       }       long long int dis[n + 1];       memset(dis, 0, sizeof(dis));       long long int s = 1;       bool visited[n + 1];       memset(visited, false, sizeof(visited));       queue<long long int> q;       q.push(s);       visited[s] = true;       dis[s] = 0;       while (!q.empty()) {         long long int v = q.front();         q.pop();         for (auto i : graph[v]) {           if (!visited[i]) {             visited[i] = true;             q.push(i);             dis[i] = dis[v] + 1;           }         }       }       vector<long long int> even, odd;       for (long long int i = 1; i < n + 1; i++) {         if (dis[i] % 2 == 0) {           even.push_back(i);         } else           odd.push_back(i);       }       if (odd.size() < even.size()) {         cout << odd.size() << endl;         for (auto i : odd) cout << i <<    ;       } else {         cout << even.size() << endl;         for (auto i : even) cout << i <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   const double eps = 1e-50;   const int maxN = 1100;   const double inf = 1e50;   int n, m;   vector<int> a[maxN];   double d[maxN][maxN];   int b[maxN];   double c[maxN][maxN];   bool have[maxN];   double t[maxN][maxN], s[maxN];   int main() {     for (int i = 0; i < maxN; ++i) {       c[i][0] = 1;       for (int j = 1; j <= i; ++j) {         c[i][j] = c[i - 1][j - 1] + c[i - 1][j];         if (c[i][j] >= inf) {           c[i][j] = inf;         }       }     }     cin >> n >> m;     vector<pair<int, int> > x;     for (int i = 0; i < m; ++i) {       int k;       cin >> k;       a[i].resize(k);       for (int j = 0; j < k; ++j) {         cin >> a[i][j];         x.push_back(make_pair(a[i][j], i));       }     }     sort(x.rbegin(), x.rend());     for (int i = 0; i < n; ++i) {       if (x[i].first != x[n - 1].first) {         ++b[x[i].second];       }     }     int needed = x[n - 1].first;     for (int i = 0; i < m; ++i) {       for (int j = 0; j < a[i].size(); ++j) {         if (a[i][j] == needed) {           have[i] = true;         }       }     }     d[0][0] = 1.0;     t[0][0] = 1.0;     for (int i = 0; i < m; ++i) {       for (int j = 0; j <= n; ++j) {         if (d[i][j] < eps) {           d[i][j] = 0.0;         }         if (t[i][j] > inf) {           t[i][j] = inf;         }         int cnt = b[i];         if (j + cnt <= n) {           d[i + 1][j + cnt] += d[i][j] / c[a[i].size()][cnt];           t[i + 1][j + cnt] += t[i][j];         }         if (have[i] && j + cnt + 1 <= n) {           d[i + 1][j + cnt + 1] += d[i][j] / c[a[i].size()][cnt + 1];           t[i + 1][j + cnt + 1] += t[i][j];         }       }     }     t[0][0] = 1.0;     for (int i = 0; i <= n; ++i) {       s[i] = t[0][i];       if (i > 0) {         s[i] += s[i - 1];       }     }     printf( %.10lf n , d[m][n] / t[m][n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void out() {     cout.flush();     int n;     cin >> n;     exit(0);   }   int tree[1 << 21];   int st = 1 << 20;   void add(int pos, int d) {     pos += st;     for (; pos; pos >>= 1) tree[pos] += d;   }   int findKth(int k) {     int pos = 1;     while (pos < st) {       pos *= 2;       if (tree[pos] < k) {         k -= tree[pos];         pos++;       }     }     return pos - st;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m, q;     cin >> n >> m >> q;     vector<int> kols(m, 0);     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       kols[x - 1]++;     }     vector<pair<int, int> > ps;     for (int i = 0; i < m; ++i) ps.push_back(make_pair(kols[i], i));     sort(ps.begin(), ps.end());     long long inf = 1000;     inf *= inf;     inf = inf * inf * inf;     inf += 1;     vector<long long> starts(m, 0);     for (int i = 1; i < m; ++i)       starts[i] = min(           inf, starts[i - 1] + (long long)i * (ps[i].first - ps[i - 1].first));     starts.push_back(inf + 1);     vector<pair<long long, int> > qs;     for (int i = 0; i < q; ++i) {       long long k;       cin >> k;       qs.push_back(make_pair(k - n - 1, i));     }     sort(qs.begin(), qs.end());     vector<int> anss(q, -1);     memset(tree, 0, sizeof(tree));     int cur = 0;     for (int i = 0; i < q; ++i) {       while (starts[cur] <= qs[i].first) {         add(ps[cur].second, 1);         ++cur;       }       long long k = qs[i].first - starts[cur - 1];       k %= cur;       anss[qs[i].second] = findKth(k + 1);     }     for (int i = 0; i < q; ++i) cout << anss[i] + 1 <<   n ;     out();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     double d, h, v, e;     scanf( %lf%lf%lf%lf , &d, &h, &v, &e);     if (v / (d * d * 3.1415926 / 4) > e) {       printf( YES n%.10f , h / (v / (d * d * 3.1415926 / 4) - e));     } else       cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a, b;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       if (x == 0) continue;       a.push_back(x);     }     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       if (x == 0) continue;       b.push_back(x);     }     n = a.size();     if (a.size() == b.size()) {       int s = -1;       {         auto it = find(begin(b), end(b), a[0]);         if (it != b.end()) {           s = it - b.begin();         }       }       if (s == -1) {         cout <<  NO  << endl;         return 0;       }       rotate(b.begin(), b.begin() + s, b.end());       cout << (a == b ?  YES  :  NO ) << endl;     } else {       cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int mxN = 1e6 + 7;   int main() {     int n, q;     cin >> n >> q;     vector<int> a(n + 10, 0), b;     vector<pair<int, int>> v;     vector<int> cnt(n + 10, 0), cnt1(n + 10, 0);     set<int> s;     for (int i = 1; i <= n; i++) cin >> a[i], s.insert(a[i]);     for (int i = 0; i < q; i++) {       int l, r;       cin >> l >> r;       cnt[l]++;       cnt1[r]++;       v.push_back({l, r});     }     vector<int> ans;     int ans_mx = 0;     for (int i = 1; i <= n; i++) {       b = a;       vector<int> union_segment, l(n + 10, 0), r(n + 10, 0);       for (int j = 0; j < v.size(); j++) {         if (v[j].second < i || v[j].first > i) {           l[v[j].first]++;           r[v[j].second]++;           union_segment.push_back(j + 1);         }       }       int val = 0;       for (int j = 1; j <= n; j++) {         val += l[j];         b[j] -= val;         val -= r[j];       }       int mi = 1e9, mx = -1e9;       for (int j = 1; j <= n; j++) {         mx = max(mx, b[j]), mi = min(mi, b[j]);       }       if (mx - mi > ans_mx) {         ans_mx = mx - mi;         ans = union_segment;       }     }     cout << ans_mx <<   n ;     cout << ans.size() <<   n ;     for (int i : ans) cout << i <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 1e9 + 7;   int dp[111][1 << 18], tr[111][1 << 18], prime[111], lp, mark[111];   int getbit(int x, int pos) { return (x >> pos) & 1; }   int onbit(int x, int pos) { return (x + (1 << pos)); }   void init() {     int nt[200];     for (int i = (1), _b = (60); i <= _b; i++) nt[i] = 1;     nt[1] = 0;     for (int i = (2), _b = (10); i <= _b; i++) {       if (nt[i] == 1) {         int j = i;         while (j < 60) {           j += i;           nt[j] = 0;         }       }     }     lp = -1;     for (int i = (1), _b = (60); i <= _b; i++) {       if (nt[i]) {         lp++;         prime[lp] = i;       }     }     for (int i = (1), _b = (60); i <= _b; i++) mark[i] = 0;     for (int i = (1), _b = (60); i <= _b; i++) {       int t = 0;       for (int j = (0), _b = (lp); j <= _b; j++) {         if (i % prime[j] == 0) {           t = onbit(t, j);         }       }       mark[i] = t;     }   }   int abS(int x) {     if (x > 0) return x;     return -x;   }   int main() {     int n, a[111];     cin >> n;     for (int i = (1), _b = (n); i <= _b; i++) cin >> a[i];     init();     int maxtt = (1 << 17) - 1;     for (int i = (0), _b = (maxtt); i <= _b; i++) dp[0][i] = 0;     for (int i = (1), _b = (n); i <= _b; i++)       for (int tt = (0), _b = (maxtt); tt <= _b; tt++) dp[i][tt] = oo;     for (int i = (1), _b = (n); i <= _b; i++) {       for (int val = (1), _b = (60); val <= _b; val++) {         for (int tt = (0), _b = (maxtt); tt <= _b; tt++) {           if ((mark[val] & tt) == 0) {             int ttnew = tt + mark[val];             int dpval = dp[i - 1][tt] + abS(val - a[i]);             if (dpval < dp[i][ttnew]) {               dp[i][ttnew] = dpval;               tr[i][ttnew] = val;             }           }         }       }     }     int ret = 0;     for (int tt = (0), _b = (maxtt); tt <= _b; tt++)       if (dp[n][ret] > dp[n][tt]) ret = tt;     int res[111];     for (int i = (n), _b = (1); i >= _b; i--) {       res[i] = tr[i][ret];       ret -= mark[res[i]];     }     for (int i = (1), _b = (n); i <= _b; i++)       printf( %d%c , res[i], i == n ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void func() {     long long n;     cin >> n;     std::vector<pair<long long, long long> > v(n);     for (long long i = 0; i < n; ++i) {       cin >> v[i].first;     }     for (long long i = 0; i < n; ++i) {       cin >> v[i].second;     }     sort(v.begin(), v.end());     multiset<long long> s;     long long i = 0, ans = 0, sum = 0, cur = v[0].first;     while (i < n || !s.empty()) {       while (v[i].first == cur && i < n) {         s.insert(v[i].second);         sum += v[i].second;         ++i;       }       if (!s.empty()) {         long long x = *s.rbegin();         sum -= x;         ans += sum;         s.erase(s.find(x));         cur++;       } else {         cur = v[i].first;       }     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       func();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     if (n % 2 == 0)       cout <<  8   << n - 8;     else       cout <<  9   << n - 9;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 2005;   const long long INF = 1e9 + 5;   char grid[MAX][MAX];   long long dp[MAX][MAX];   auto solve(long long n, long long k) -> string {     auto inside = [&](long long row, long long col) {       return (row < n and row >= 0 and col < n and col >= 0);     };     for (long long row = 0; row < n; ++row) {       for (long long col = 0; col < n; ++col) {         dp[row][col] = INF;       }     }     dp[0][0] = (grid[0][0] !=  a ) ? 1 : 0;     for (long long row = 0; row < n; ++row) {       for (long long col = 0; col < n; ++col) {         if (row == 0 and col == 0) continue;         if ((row != 0) and (col != 0)) {           dp[row][col] =               min(dp[row][col - 1], dp[row - 1][col]) + (grid[row][col] !=  a );         } else {           dp[row][col] = (row == 0) ? dp[row][col - 1] + (grid[row][col] !=  a )                                     : dp[row][col];           dp[row][col] = (col == 0) ? dp[row - 1][col] + (grid[row][col] !=  a )                                     : dp[row][col];         }       }     }     long long pref = 0;     for (long long row = 0; row < n; ++row) {       for (long long col = 0; col < n; ++col) {         if (dp[row][col] <= k) {           pref = max(pref, row + col + 1);         }       }     }     string answer(pref,  a );     vector<pair<long long, long long>> list;     for (long long row = 0; row < n; ++row) {       for (long long col = 0; col < n; ++col) {         if ((dp[row][col] <= k) and (pref == row + col + 1)) {           list.push_back(make_pair(row, col));         }       }     }     if (list.empty()) {       answer = grid[0][0];       list.emplace_back(0, 0);     }     while (!list.empty()) {       char best_char =  z  + 1;       vector<pair<long long, long long>> new_list;       for (pair<long long, long long> p : list) {         for (long long d_row : {0, 1}) {           long long d_col = 1 - d_row;           long long r2 = p.first + d_row;           long long c2 = p.second + d_col;           if (inside(r2, c2)) {             if (grid[r2][c2] <= best_char) {               if (grid[r2][c2] < best_char) {                 new_list.clear();               }               best_char = grid[r2][c2];               new_list.push_back(make_pair(r2, c2));             }           }         }       }       new_list.resize(std::unique(new_list.begin(), new_list.end()) -                       new_list.begin());       list = new_list;       if (best_char !=  z  + 1) answer = answer + best_char;     }     return answer;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long n, k;     std::cin >> n >> k;     for (long long i = 0; i < n; ++i) {       std::cin >> grid[i];     }     std::cout << solve(n, k);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod(long long a) {     return a >= 0 ? a % 1000000009 : (1000000009 + a % 1000000009) % 1000000009;   }   long long st[2 * 3000010];   long long lz[2][2 * 3000010];   long long fib[2][3000010];   long long fibSum[2][3000010];   long long getFib(int i, long long i0, long long i1) {     return mod(mod(fib[0][i] * i0) + mod(fib[1][i] * i1));   }   long long getFibSum(int i, long long i0, long long i1) {     return mod(mod(fibSum[0][i] * i0) + mod(fibSum[1][i] * i1));   }   long long getFibSumRange(int i, int j, int i0, int i1) {     if (i == 0) return getFibSum(j, i0, i1);     return mod(getFibSum(j, i0, i1) - getFibSum(i - 1, i0, i1));   }   void buildU(vector<int> &v, int i, int l, int r) {     if (l == r) {       st[i] = v[l];       return;     }     int m = (l + r) / 2;     buildU(v, i << 1, l, m);     buildU(v, i << 1 | 1, m + 1, r);     st[i] = mod(st[i << 1] + st[i << 1 | 1]);   }   void build(vector<int> &v) {     for (int i = 1; i < 2 * v.size(); i++) {       lz[0][i] = 0;       lz[1][i] = 0;     }     buildU(v, 1, 0, v.size() - 1);   }   const int add = 1;   bool lzEmpty(int i) { return lz[0][i] == 0 && lz[1][i] == 0; }   void lazyUpdate(int i, int l, int r) {     st[i] = mod(st[i] + getFibSum(r - l, lz[0][i], lz[1][i]));     if (l != r) {       lz[0][i << 1] = mod(lz[0][i << 1] + lz[0][i]);       lz[1][i << 1] = mod(lz[1][i << 1] + lz[1][i]);       int m = (l + r) / 2;       lz[0][i << 1 | 1] =           mod(lz[0][i << 1 | 1] + getFib(m + 1 - l, lz[0][i], lz[1][i]));       lz[1][i << 1 | 1] =           mod(lz[1][i << 1 | 1] + getFib(m + 2 - l, lz[0][i], lz[1][i]));     }     lz[0][i] = 0;     lz[1][i] = 0;   }   void changeU(int cl, int cr, int i, int l, int r) {     if (!lzEmpty(i)) lazyUpdate(i, l, r);     if (r < cl || cr < l) return;     if (cl <= l && r <= cr) {       st[i] = mod(st[i] + getFibSumRange(l - cl, r - cl, 1, 1));       if (l != r) {         lz[0][i << 1] = mod(lz[0][i << 1] + getFib(l - cl, 1, 1));         lz[1][i << 1] = mod(lz[1][i << 1] + getFib(l - cl + 1, 1, 1));         int m = (l + r) / 2;         lz[0][i << 1 | 1] = mod(lz[0][i << 1 | 1] + getFib(m + 1 - cl, 1, 1));         lz[1][i << 1 | 1] = mod(lz[1][i << 1 | 1] + getFib(m + 2 - cl, 1, 1));       }       return;     }     int m = (l + r) / 2;     changeU(cl, cr, i << 1, l, m);     changeU(cl, cr, i << 1 | 1, m + 1, r);     st[i] = mod(st[i << 1] + st[i << 1 | 1]);   }   void change(int n, int cl, int cr) { changeU(cl, cr, 1, 0, n - 1); }   int queryU(int ql, int qr, int i, int l, int r) {     if (!lzEmpty(i)) lazyUpdate(i, l, r);     if (r < ql || qr < l) return 0;     if (ql <= l && r <= qr) return st[i];     int m = (l + r) / 2;     int q1 = queryU(ql, qr, i << 1, l, m);     int q2 = queryU(ql, qr, i << 1 | 1, m + 1, r);     return mod(q1 + q2);   }   int query(int n, int ql, int qr) { return queryU(ql, qr, 1, 0, n - 1); }   void print(int n) {     cout <<  st  << endl;     for (int i = 1; i < 2 * n; i++) {       cout << setw(4) << st[i] <<    ;     }     cout << endl;     for (int j = 0; j < 2; j++) {       cout <<  lz[  << j <<  ]  << endl;       for (int i = 1; i < 2 * n; i++) {         cout << setw(4) << lz[j][i] <<    ;       }       cout << endl;     }     cout << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     fib[0][0] = 1;     fib[0][1] = 0;     fib[1][0] = 0;     fib[1][1] = 1;     fibSum[0][0] = fib[0][0];     fibSum[1][0] = fib[1][0];     for (int j = 0; j < 2; j++) {       for (int i = 2; i < n; i++) {         fib[j][i] = (fib[j][i - 2] + fib[j][i - 1]) % 1000000009;       }     }     for (int j = 0; j < 2; j++) {       for (int i = 0; i < n; i++) {         fibSum[j][i] = (fibSum[j][i - 1] + fib[j][i]) % 1000000009;       }     }     vector<int> v;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       v.push_back(x);     }     build(v);     for (int i = 0; i < m; i++) {       int op, l, r;       cin >> op >> l >> r;       l--;       r--;       if (op == 1) {         change(n, l, r);       } else {         cout << query(n, l, r) << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   const int N = 100005;   using namespace std;   inline int Getint() {     register int x = 0, f = 1;     register char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   const double pi = acos(-1);   struct Z {     double r, i;     Z(double _r = 0, double _i = 0) { r = _r, i = _i; }     Z operator+(const Z &a) const { return Z(r + a.r, i + a.i); }     Z operator-(const Z &a) const { return Z(r - a.r, i - a.i); }     Z operator*(const Z &a) const {       return Z(r * a.r - i * a.i, r * a.i + i * a.r);     }     Z operator/(const double &a) const { return Z(r / a, i / a); }     Z operator/=(const double &a) { return *this = Z(r / a, i / a); }   };   void FFT(Z *a, int x, int K) {     static int rev[N], lst;     int n = 1 << x;     if (n != lst) {       for (int i = 0; i < n; i++)         rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << x - 1);       lst = n;     }     for (int i = 0; i < n; i++)       if (i < rev[i]) swap(a[i], a[rev[i]]);     for (int i = 1; i < n; i <<= 1) {       int tmp = i << 1;       Z wn(cos(pi / i), sin(pi * K / i));       for (int j = 0; j < n; j += tmp) {         Z w(1, 0);         for (int k = 0; k < i; k++, w = w * wn) {           Z x = a[j + k], y = w * a[i + j + k];           a[j + k] = x + y, a[i + j + k] = x - y;         }       }     }     if (K == -1)       for (int i = 0; i < n; i++) a[i] /= n;   }   Z a[N], b[N];   int n, m, T, X;   double p[105][40005];   struct node {     int x, y, z;   } s[105];   double mp[55][55], f[55][N], g[105][N];   void Floyd() {     for (int k = 1; k <= n; k++)       for (int i = 1; i <= n; i++)         for (int j = 1; j <= n; j++)           mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);   }   void Cal(int l, int mid, int r) {     int len = r - l;     for (int j = 1; j <= m; j++) {       int x = ceil(log2(len + r - mid));       fill(a, a + (1 << x), 0), fill(b, b + (1 << x), 0);       for (int i = 0; i < r - mid + 1; i++) a[i].r = f[s[j].y][r - i];       for (int i = 0; i < len; i++) b[i].r = p[j][i + 1];       FFT(a, x, 1), FFT(b, x, 1);       for (int i = 0; i < (1 << x); i++) a[i] = a[i] * b[i];       FFT(a, x, -1);       for (int i = mid - 1; i >= l; i--) g[j][i] += a[r - i - 1].r;     }   }   void Binary(int l, int r) {     if (l == r) {       for (int i = 1; i <= m; i++)         f[s[i].x][l] = min(f[s[i].x][l], s[i].z + g[i][l]);       return;     }     int mid = l + r >> 1;     Binary(mid + 1, r);     Cal(l, mid + 1, r);     Binary(l, mid);   }   int main() {     n = Getint(), m = Getint(), T = Getint(), X = Getint();     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (i ^ j) mp[i][j] = 1 << 30;     for (int i = 1; i <= m; i++) {       int x = Getint(), y = Getint(), z = Getint();       s[i] = (node){x, y, z};       mp[x][y] = min(mp[x][y], 1.0 * z);       for (int j = 1; j <= T; j++) p[i][j] = (double)Getint() / 100000;     }     Floyd();     for (int j = T + 1; j <= 2 * T; j++) f[n][j] = X;     for (int i = 1; i < n; i++) {       for (int j = 0; j <= T; j++) f[i][j] = 1 << 30;       for (int j = T + 1; j <= 2 * T; j++) f[i][j] = X + mp[i][n];     }     Cal(1, T + 1, 2 * T);     Binary(0, T);     printf( %.6f , f[1][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 10;   long long c[2][2][maxn][maxn];   int lowbit(int s) { return s & (-s); }   void modify(int x, int y, int v) {     for (int i = x; i < maxn; i += lowbit(i))       for (int j = y; j < maxn; j += lowbit(j)) c[x & 1][y & 1][i][j] ^= v;     return;   }   long long query(int x, int y) {     long long ret = 0;     for (int i = x; i > 0; i -= lowbit(i))       for (int j = y; j > 0; j -= lowbit(j)) ret ^= c[x & 1][y & 1][i][j];     return ret;   }   int main(void) {     int n, m;     scanf( %d %d , &n, &m);     while (m--) {       long long v;       int op, x0, y0, x1, y1;       scanf( %d %d %d %d %d , &op, &x0, &y0, &x1, &y1);       if (op == 2) {         scanf( %I64d , &v);         modify(x0, y0, v);         modify(x0, y1 + 1, v);         modify(x1 + 1, y0, v);         modify(x1 + 1, y1 + 1, v);       } else         printf( %I64d n , query(x1, y1) ^ query(x0 - 1, y1) ^ query(x1, y0 - 1) ^                               query(x0 - 1, y0 - 1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(0);     long long n, k, m;     cin >> n >> k >> m;     vector<long long> a(k);     long long second = 0;     for (long long i = 0; i < k; i++) {       cin >> a[i];       second += a[i];     }     sort(a.begin(), a.end());     long long ans = 0;     for (long long p = 0; p <= n; p++) {       long long t = m - second * p;       long long kol = 0;       if (t < 0) break;       kol = p * (k + 1);       for (long long j = 0; j < k; j++) {         long long ost = n - p;         while (ost > 0 && t - a[j] >= 0) {           ost--;           t -= a[j];           kol++;         }       }       ans = max(ans, kol);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const double EXP = 2.7182818284590452;   const double Pi = 3.14159265;   const double EPS = 1e-11;   const int INF = 1000 * 1000 * 1000;   const long long INFL = INF * 1000 * 100;   char arr[2003][2003], c;   bool was[2003][2003];   int n, m;   queue<std::pair<int, int> > stars;   std::pair<int, int> cur;   std::pair<int, int> mask[] = {std::make_pair(-1, -1), std::make_pair(-1, 0),                                 std::make_pair(-1, 1),  std::make_pair(0, -1),                                 std::make_pair(0, 1),   std::make_pair(1, -1),                                 std::make_pair(1, 0),   std::make_pair(1, 1)};   bool IsValid(int r, int c) {     if (r < 1 || r > n) return false;     if (c < 1 || c > m) return false;     return true;   }   bool Check(int r, int c) {     if (arr[r][c - 1] == arr[r - 1][c - 1] &&         arr[r - 1][c - 1] == arr[r - 1][c] && arr[r - 1][c] ==  . )       return true;     if (arr[r - 1][c] == arr[r - 1][c + 1] &&         arr[r - 1][c + 1] == arr[r][c + 1] && arr[r][c + 1] ==  . )       return true;     if (arr[r][c + 1] == arr[r + 1][c + 1] &&         arr[r + 1][c + 1] == arr[r + 1][c] && arr[r + 1][c] ==  . )       return true;     if (arr[r + 1][c] == arr[r + 1][c - 1] &&         arr[r + 1][c - 1] == arr[r][c - 1] && arr[r][c - 1] ==  . )       return true;     return false;   }   void AddNew(int r, int c) {     int rr, cc;     for (int i = (0); i < (8); ++i) {       rr = r + mask[i].first;       cc = c + mask[i].second;       if (IsValid(rr, cc) && arr[rr][cc] ==  * ) {         if (!was[rr][cc] && Check(rr, cc)) {           stars.push(std::make_pair(rr, cc));           was[rr][cc] = true;         }       }     }   }   int main(void) {     cin >> n >> m;     for (int i = (0); i < (n + 2); ++i)       for (int j = (0); j < (m + 2); ++j) arr[i][j] =  * ;     for (int i = (1); i < (n + 1); ++i) {       scanf( %c , &c);       for (int j = (1); j < (m + 1); ++j) scanf( %c , arr[i] + j);     }     for (int i = (1); i < (n + 1); ++i)       for (int j = (1); j < (m + 1); ++j) {         if (Check(i, j)) {           stars.push(std::make_pair(i, j));           was[i][j] = true;         }       }     while (stars.size()) {       cur = stars.front();       stars.pop();       arr[cur.first][cur.second] =  . ;       AddNew(cur.first, cur.second);     }     for (int i = (1); i < (n + 1); ++i) {       for (int j = (1); j < (m + 1); ++j) {         printf( %c , arr[i][j]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int m;   void Add(int &a, int b) {     a += b;     if (a >= m) a -= m;   }   int dp[1010][110][2];   int main() {     int n, k;     while (scanf( %d%d%d , &n, &k, &m) == 3) {       memset(dp, 0, sizeof(dp));       dp[0][0][0] = 1;       long long tmp = 1;       for (int i = 0; i < n; i++) {         for (int j = 0; j < k; j++)           for (int x = 0; x < 2; x++) {             if (dp[i][j][x] == 0) continue;             for (int y = 0; y < 10; y++) {               if (i == n - 1 && y == 0) continue;               int nj = (y * tmp % k + j) % k;               int nx = x;               if (nj == 0 && (y > 0 || j > 0)) nx = 1;               Add(dp[i + 1][nj][nx], dp[i][j][x]);             }           }         tmp = tmp * 10 % k;       }       int ans = 0;       for (int j = 0; j < k; j++) Add(ans, dp[n][j][1]);       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long double n;     int m;     cin >> n >> m;     long sum = n;     long double newsum = 0.0;     for (int i = 0; i < n; i++) {       newsum += pow((long double)i / (long double)n, m);     }     cout << sum - newsum;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1 << 18 | 7;   int a[N], b[N], o[N];   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n, s;     cin >> n >> s;     --s;     for (int i = 0; i < n; ++i) cin >> a[i], b[i] = a[i];     for (int i = 0; i < n; ++i)       if (!a[i]) a[i] = n;     if (a[s]) a[s] = 0;     priority_queue<pair<int, int> > Q;     for (int i = 0; i < n; ++i) Q.push({a[i], i}), o[a[i]] = 1;     for (int i = 1; i < Q.top().first; ++i) {       if (o[i]) continue;       auto now = Q.top();       Q.pop();       a[now.second] = i;       now.first = i;       Q.push(now);     }     int ans = 0;     for (int i = 0; i < n; ++i) ans += a[i] != b[i];     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < int(n); i++) {       cin >> a[i];     }     vector<vector<int>> g(n);     for (int i = 0; i < int(n - 1); i++) {       int p;       cin >> p;       p--;       g[p].push_back(i + 1);     }     vector<int> color(n);     function<void(int)> dfs = [&](int u) {       for (const auto& v : g[u]) {         color[v] = color[u] ^ 1;         dfs(v);       }     };     dfs(0);     int WHITE = 0;     for (int i = 0; i < int(n); i++) {       if (g[i].empty()) {         WHITE = color[i];       }     }     int nim_sum = 0;     map<int, int> freq;     for (int i = 0; i < int(n); i++) {       if (color[i] == WHITE) {         nim_sum ^= a[i];         ++freq[a[i]];       }     }     long long ans = 0;     if (nim_sum == 0) {       int cnt = 0;       for (int i = 0; i < int(n); i++) {         if (color[i] != WHITE) {           cnt++;         }       }       ans = (long long)cnt * (cnt - 1) / 2 +             (long long)(n - cnt) * (n - cnt - 1) / 2;     }     for (int i = 0; i < int(n); i++) {       if (color[i] != WHITE) {         ans += freq[nim_sum ^ a[i]];       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int N = 4e5 + 5;   const int BN = 25;   int n, a[N], cnt;   vector<int> v[BN];   int main() {     ios ::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];     }     int res = 0;     for (int i = 1; i <= n; ++i) {       if (a[i] & 1) {         int k = (i - 1) - cnt;         if (k & 1) {           res ^= 1;         }       } else {         int k = cnt;         if (k & 1) {           res ^= 1;         }       }       int cur_val = (a[i] & 1);       for (int bit = 1; bit <= 24; ++bit) {         if ((a[i] >> bit) & 1) {           cur_val += (1 << bit);         }         v[bit].push_back(cur_val);       }       cnt += (a[i] & 1);     }     for (int bit = 1; bit <= 24; ++bit) sort(v[bit].begin(), v[bit].end());     for (int bit = 1; bit <= 24; ++bit) {       while (!v[bit].empty()) {         int cur_val = v[bit].back();         v[bit].pop_back();         int k = lower_bound(v[bit].begin(), v[bit].end(), (1 << bit) - cur_val) -                 v[bit].begin();         k = v[bit].size() - k;         int j = lower_bound(v[bit].begin(), v[bit].end(),                             (1 << (bit + 1)) - cur_val) -                 v[bit].begin();         j = v[bit].size() - j;         int l = lower_bound(v[bit].begin(), v[bit].end(),                             (1 << (bit + 1)) + (1 << bit) - cur_val) -                 v[bit].begin();         l = v[bit].size() - l;         k += l;         k -= j;         if (k & 1) {           res ^= (1 << bit);         }       }     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n, m;       cin >> n >> m;       long long ans = (n * (n + 1)) / 2;       long long x = (n - m) / (m + 1);       long long y = (n - m) % (m + 1);       long long l = (x * (x + 1)) / 2;       x++;       long long r = (x * (x + 1)) / 2;       long long a = (m + 1 - y) * l + y * r;       ans -= a;       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     cin >> n >> a >> b;     int x = a;     while ((a / x + b / x) < n) x--;     if (a < x)       cout << a;     else if (b < x)       cout << b;     else       cout << x;   }


#include <bits/stdc++.h>   using namespace std;   long long n, v[5000], d[5000], p[5000];   bool used[5000];   int main() {     cin >> n;     for (long long i = 0; i < n; i++) {       used[i] = true;       cin >> v[i] >> d[i] >> p[i];     }     vector<long long> ans;     for (long long i = 0; i < n; i++) {       long long t = 0;       while ((p[t] >= 0 || !used[t]) && t < n) t++;       if (t == n) {         t = 0;         while (!used[t]) t++;         used[t] = false;         if (t == n) break;         ans.push_back(t + 1);         long long k = 0;         for (long long j = t + 1; j < n; j++)           if (used[j] && k <= v[t]) {             p[j] -= (v[t] - k);             k++;           }         continue;       }       used[t] = false;       for (long long j = t + 1; j < n; j++) p[j] -= d[t];     }     cout << ans.size() << endl;     for (long long i = 0; i < ans.size(); i++)       if (p[ans[i] - 1] >= 0) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int n, m, nx, ny;   int A[N], B[N], C[N], D[N], X[N], Y[N], vis[N];   int ans = 1;   set<int> s[N << 2];   vector<int> e[N];   struct ST {     int mx, mn;   } tr[N << 2];   void upd(int p, int l, int r) {     int ls = p << 1, rs = p << 1 | 1;     if (l == r)       tr[p] = {0, 0};     else       tr[p] = ST{max(tr[ls].mx, tr[rs].mx), min(tr[ls].mn, tr[rs].mn)};     if (s[p].size()) {       int x = *s[p].rbegin();       if (vis[x])         tr[p].mn = max(tr[p].mn, x);       else         tr[p].mx = max(tr[p].mx, x);     }     if (tr[p].mx < tr[p].mn) tr[p].mx = 0;   }   void mdf(int p, int l, int r, int L, int R, int d) {     if (L > r || R < l) return;     if (L <= l && r <= R) {       if (d > 0)         s[p].insert(d);       else         s[p].erase(-d);       upd(p, l, r);       return;     }     int mid = l + r >> 1, ls = p << 1, rs = p << 1 | 1;     mdf(ls, l, mid, L, R, d);     mdf(rs, mid + 1, r, L, R, d);     upd(p, l, r);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d%d%d , &A[i], &B[i], &C[i], &D[i]);       X[++nx] = A[i], X[++nx] = C[i];       Y[++ny] = B[i], Y[++ny] = D[i];     }     sort(X + 1, X + nx + 1);     sort(Y + 1, Y + ny + 1);     nx = unique(X + 1, X + nx + 1) - X - 1;     ny = unique(Y + 1, Y + ny + 1) - Y - 1;     for (int i = 1; i <= n; i++) {       A[i] = lower_bound(X + 1, X + nx + 1, A[i]) - X;       B[i] = lower_bound(Y + 1, Y + ny + 1, B[i]) - Y;       C[i] = lower_bound(X + 1, X + nx + 1, C[i]) - X;       D[i] = lower_bound(Y + 1, Y + ny + 1, D[i]) - Y - 1;       e[A[i]].push_back(i);       e[C[i]].push_back(-i);     }     for (int i = 1, x; i <= nx; i++) {       for (int t : e[i]) x = abs(t), mdf(1, 1, ny, B[x], D[x], t);       for (; x = tr[1].mx; mdf(1, 1, ny, B[x], D[x], 0)) vis[x] = 1, ans++;     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1007;   struct P {     int x, y, k;     int d;     void in() {       scanf( %d %d %d , &x, &y, &k);       d = x * x + y * y;     }   } p[N];   bool cmp(P a, P b) { return a.d <= b.d; }   int main() {     int n, s, i;     int M = 1000000;     while (~scanf( %d %d , &n, &s)) {       for (int i = 0; i < n; ++i) p[i].in();       sort(p, p + n, cmp);       int i = 0;       while (s < M && i < n) {         s += p[i].k;         i++;       }       if ((i == n) && (s < M))         puts( -1 );       else         printf( %.7lf n , sqrt(1.0 * p[i - 1].d));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct pst {     struct node {       int v;       node *l, *r;       node() : node(0x3f3f3f3f) {}       node(int v) : v(v), l(nullptr), r(nullptr) {}     };     int n;     vector<node *> root;     deque<node> buffer;     pst(int n) : n(n) { root.push_back(build(0, n - 1)); }     node *build(int l, int r) {       node *cur = new_node();       if (l != r) {         int m = (l + r) / 2;         cur->l = build(l, m);         cur->r = build(m + 1, r);       }       return cur;     }     void copy(int pt, int t) {       if ((int)(root.size()) <= t) {         root.resize(t + 1);       }       root[t] = new_node(*root[pt]);     }     void upd(int pt, int t, int x, int v) {       if ((int)(root.size()) <= t) {         root.resize(t + 1);       }       root[t] = upd(root[pt], x, v, 0, n - 1);     }     node *upd(node *p, int x, int v, int l, int r) {       node *cur = new_node(*p);       if (l == r) {         cur->v = v;       } else {         int m = (l + r) / 2;         if (x <= m) {           cur->l = upd(cur->l, x, v, l, m);         } else {           cur->r = upd(cur->r, x, v, m + 1, r);         }         cur->v = min(cur->l->v, cur->r->v);       }       return cur;     }     int qry(int t, int x, int y) { return qry(root[t], x, y, 0, n - 1); }     int qry(node *p, int x, int y, int l, int r) {       if (y < l || x > r) {         return 0x3f3f3f3f;       } else if (x <= l && r <= y) {         return p->v;       } else {         int m = (l + r) / 2;         return min(qry(p->l, x, y, l, m), qry(p->r, x, y, m + 1, r));       }     }     template <typename... Args>     node *new_node(Args... args) {       buffer.emplace_back(args...);       return &buffer.back();     }   };   int n, r, m;   int nums[100010];   vector<int> adj[100010];   int p[1000010];   int q[1000010];   int depth[100010];   vector<int> at_depth[100010];   int t = -1;   int in[100010];   int out[100010];   void dfs(int v, int p, int d) {     in[v] = ++t;     depth[v] = d;     at_depth[d].push_back(v);     for (int e : adj[v]) {       if (e != p) {         dfs(e, v, d + 1);       }     }     out[v] = t;   }   int main() {     cin.tie(0)->sync_with_stdio(0);     int n, r;     cin >> n >> r;     r--;     for (int i = 0; i < n; i++) {       cin >> nums[i];     }     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       adj[a].push_back(b);       adj[b].push_back(a);     }     cin >> m;     for (int i = 0; i < m; i++) {       cin >> p[i] >> q[i];     }     pst st(n);     dfs(r, -1, 1);     for (int i = 1; i <= n; i++) {       st.copy(i - 1, i);       for (int v : at_depth[i]) {         st.upd(i, i, in[v], nums[v]);       }     }     int last = 0;     for (int i = 0; i < m; i++) {       int v = (last + p[i]) % n;       int k = (last + q[i]) % n;       int lowest = min(depth[v] + k, n);       last = st.qry(lowest, in[v], out[v]);       cout << last <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 100;   const int INF = (int)1e9;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     long long n, m;     cin >> n >> m;     vector<long long> a(n + 1);     for (int i = 1; i <= n; i++) cin >> a[i];     long long x, y, z;     while (m--) {       cin >> x >> y >> z;       long long c1 = 0, c2 = 0;       for (int i = x; i <= z; i++)         if (a[i] > a[z]) c1++;       for (int i = z; i <= y; i++)         if (a[i] < a[z]) c2++;       if (c1 == c2)         cout <<  Yes  << endl;       else         cout <<  No  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int a[n];     int c[101] = {0};     for (int i = 0; i < n; i++) cin >> a[i];     long long int sum = a[0];     int b[n];     b[0] = 0;     c[a[0]]++;     for (int i = 1; i < n; i++) {       sum = sum + a[i];       long long int ex = sum - m;       if (ex <= 0) {         b[i] = 0;         c[a[i]]++;         continue;       }       int cnt = 0;       for (int k = 100; k >= 1; k--) {         if (ex <= c[k] * k) {           cnt += (ex + k - 1) / k;           ex = 0;           break;         }         cnt = cnt + c[k];         ex = ex - c[k] * k;       }       b[i] = cnt;       c[a[i]]++;     }     for (int i = 0; i < n; i++) cout << b[i] <<    ;     cout << endl;   }


#include <bits/stdc++.h>   int main() {     int a;     scanf( %d , &a);     int n = 7, i = 1, sum = 0, k = 1;     while (a) {       int num = a % 10;       if (num == 4)         sum += 1 * k;       else         sum += 2 * k;       a /= 10;       i++;       k *= 2;     }     printf( %d , sum);   }


#include <bits/stdc++.h>   using namespace std;   int n, a[110];   vector<int> prime;   bool flag[40010];   int cnt[40010][110];   void init(int MX) {     for (int i = 2; i <= MX; i++) {       if (!flag[i]) prime.push_back(i);       for (int j = 0; j < prime.size() && i * prime[j] <= MX; j++) {         flag[i * prime[j]] = 1;         if (i % prime[j] == 0) break;       }     }   }   int ans = 0;   map<int, int> sg, m;   void dfs(int x) {     if (sg.count(x)) return;     bool vis[32];     memset(vis, 0, sizeof(vis));     for (int i = 1; x >> (i - 1); i++) {       int mask = (x >> i) | (x & ((1 << (i - 1)) - 1));       dfs(mask);       vis[sg[mask]] = 1;     }     for (int i = 0;; i++) {       if (!vis[i]) {         sg[x] = i;         return;       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     init(40000);     for (int j = 0; j < prime.size(); j++) {       for (int i = 1; i <= n; i++) {         while (a[i] % prime[j] == 0) {           a[i] /= prime[j];           cnt[j][i]++;         }         if (cnt[j][i]) m[j] |= (1 << (cnt[j][i] - 1));       }     }     for (int i = 1; i <= n; i++)       if (a[i] > 1) m[a[i]] |= 1;     sg[0] = 0;     for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {       dfs(it->second);       ans ^= sg[it->second];     }     puts(ans ?  Mojtaba  :  Arpa );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5;   vector<int> graph[N];   int pa[N];   int getHeight(int u, int pre) {     int res = 1;     pa[u] = pre;     for (auto e : graph[u]) {       if (e == pre) {         continue;       }       int subHeight = getHeight(e, u);       res = max(res, subHeight + 1);     }     return res;   }   bool checkBanalance(int u, int pre, int &height) {     if (graph[u].size() == 1) {       height = 0;       return true;     }     int h[2];     int idx = 0;     for (auto e : graph[u]) {       if (e == pre) {         continue;       }       bool ret = checkBanalance(e, u, h[idx++]);       if (!ret) {         return false;       }     }     height = h[0] + 1;     if (h[0] != h[1]) {       return false;     }     return true;   }   int d[N];   int maxDepth = 0;   int maxNode = 0;   int getDepth(int u, int pre) {     d[u] = d[pre] + 1;     pa[u] = pre;     for (auto e : graph[u]) {       if (e == pre) {         continue;       }       getDepth(e, u);     }     if (d[u] > maxDepth) {       maxDepth = d[u];       maxNode = u;     }     return d[u];   }   int main() {     int n;     scanf( %d , &n);     int nodeErase = (1 << n) - 1;     int u, v;     for (int i = 1; i <= (1 << n) - 3; i++) {       scanf( %d%d , &u, &v);       graph[u].push_back(v);       graph[v].push_back(u);     }     if (n == 2) {       printf( 2 n );       printf( 1 2 n );       return 0;     }     vector<int> cnt[5];     int res;     int root;     for (int i = 1; i <= (1 << n) - 2; i++) {       int degree = graph[i].size();       if (degree > 4 || degree < 1) {         do {           printf( 0 n );           return 0;         } while (0);         ;       }       cnt[degree].push_back(i);     }     if (cnt[4].size() > 1 || cnt[2].size() > 2) {       do {         printf( 0 n );         return 0;       } while (0);       ;     } else if (cnt[4].size() == 1) {       if (cnt[2].size() != 1) {         do {           printf( 0 n );           return 0;         } while (0);         ;       }       root = cnt[2][0];       res = cnt[4][0];       getHeight(root, 0);       pair<int, int> h[4];       int idx = 0;       for (auto e : graph[res]) {         if (e == pa[res]) {           continue;         }         h[idx++] = make_pair(getHeight(e, res), e);       }       sort(h, h + 3);       if (!(h[0].first == h[1].first && h[2].first == h[1].first + 1)) {         do {           printf( 0 n );           return 0;         } while (0);         ;       }       graph[h[0].second].push_back(nodeErase);       graph[h[1].second].push_back(nodeErase);       graph[res].push_back(nodeErase);       graph[nodeErase].push_back(h[0].second);       graph[nodeErase].push_back(h[1].second);       graph[nodeErase].push_back(res);       graph[res].erase(find(graph[res].begin(), graph[res].end(), h[0].second));       graph[res].erase(find(graph[res].begin(), graph[res].end(), h[1].second));       graph[h[0].second].erase(           find(graph[h[0].second].begin(), graph[h[0].second].end(), res));       graph[h[1].second].erase(           find(graph[h[1].second].begin(), graph[h[1].second].end(), res));     } else if (cnt[4].size() == 0 && cnt[2].size() == 2) {       for (int i = 0; i < 2; i++) {         root = cnt[2][i];         res = cnt[2][1 - i];         int h1 = getHeight(graph[root][0], 0);         int h2 = getHeight(graph[root][1], 0);         if (h1 == h2) {           break;         }       }       graph[res].push_back(nodeErase);       graph[nodeErase].push_back(res);     } else if (cnt[4].size() == 0 && cnt[2].size() == 0) {       u = cnt[1][0];       getDepth(u, 0);       int t = (maxDepth - 1) / 2;       v = maxNode;       for (int i = 0; i < t; i++) {         v = pa[v];       }       u = pa[v];       int height = 0;       if (checkBanalance(u, v, height) && checkBanalance(v, u, height)) {         if (u > v) {           swap(u, v);         }         printf( 2 n%d %d n , u, v);         return 0;       } else {         do {           printf( 0 n );           return 0;         } while (0);         ;       }     } else {       do {         printf( 0 n );         return 0;       } while (0);       ;     }     int height = 0;     bool ret = checkBanalance(root, 0, height);     if (ret) {       printf( 1 n%d n , res);     } else {       do {         printf( 0 n );         return 0;       } while (0);       ;     }     return 0;   }


#include <bits/stdc++.h>   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   inline long long qmul(long long a, long long b, long long m) {     long long res = 0;     while (b) {       if (b & 1) res = (res + a) % m;       a = (a << 1) % m;       b = b >> 1;     }     return res;   }   inline long long qpow(long long a, long long b, long long m) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % m;       a = (a * a) % m;       b = b >> 1;     }     return res;   }   inline long long inv(long long x, long long q) { return qpow(x, q - 2, q); }   using namespace std;   const int N = 3e5 + 10;   const double eps = 1e5;   int arr[N];   int main() {     std::ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> arr[2 * i - 1];       cin >> arr[2 * i];     }     int k;     cin >> k;     k--;     int pos = upper_bound(arr + 1, arr + 2 * n + 1, k) - arr;     cout << n - (pos - 1) / 2;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3030, D = 15;   long long f[D][N] = {};   long long ans = 0;   int n, k, v[N] = {};   long long w[N][N] = {};   void merge(int d, int l, int r) {     copy(f[d - 1] + 1, f[d - 1] + k + 1, f[d] + 1);     for (int i = l; i <= r; ++i) {       int V = min(v[i], k);       for (int j = k; j >= V; --j) f[d][j] = max(f[d][j], f[d][j - V] + w[i][V]);     }   }   void dp(int d, int l, int r) {     if (l == r) {       for (int i = 0; i <= min(v[l], k); ++i)         ans = max(ans, f[d - 1][k - i] + w[l][i]);       return;     }     int mid = (l + r) >> 1;     merge(d, l, mid);     dp(d + 1, mid + 1, r);     merge(d, mid + 1, r);     dp(d + 1, l, mid);   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) {       scanf( %d , v + i);       int x;       for (int j = 1; j <= v[i]; ++j) {         scanf( %d , &x);         if (j <= k) w[i][j] = w[i][j - 1] + x;       }       v[i] = min(v[i], k);     }     dp(1, 1, n);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1e5 + 10;   struct people {     int p, t, i;     pair<int, int> ans;   } G1[M], G2[M];   bool cmp(people a, people b) {     if (a.p - a.t != b.p - b.t) return a.p - a.t < b.p - b.t;     return a.p < b.p;   }   bool cmp1(people a, people b) { return a.p < b.p; }   bool cmp2(people a, people b) { return a.i < b.i; }   int binary_search(int st, int en, int val) {     int l = st - 1;     int r = en;     while (r - l > 1) {       int mid = (l + r) / 2;       if (G2[mid].p - G2[mid].t >= val) {         r = mid;       } else {         l = mid;       }     }     return r;   }   int binary_search2(int st, int en, int val) {     int l = st - 1;     int r = en;     while (r - l > 1) {       int mid = (l + r) / 2;       if (G2[mid].p - G2[mid].t > val) {         r = mid;       } else {         l = mid;       }     }     return r;   }   int main() {     int n, w, h, g, cnt1, cnt2;     cnt1 = cnt2 = 0;     scanf( %d%d%d , &n, &w, &h);     for (int i = 1; i <= n; i++) {       scanf( %d , &g);       if (g == 1) {         scanf( %d%d , &G1[cnt1].p, &G1[cnt1].t);         G1[cnt1].ans.first = G1[cnt1].p;         G1[cnt1].ans.second = h;         G1[cnt1++].i = i;       } else {         scanf( %d%d , &G2[cnt2].p, &G2[cnt2].t);         G2[cnt2].ans.first = w;         G2[cnt2].ans.second = G2[cnt2].p;         G2[cnt2++].i = i;       }     }     bool ff = false;     sort(G2, G2 + cnt2, cmp);     sort(G1, G1 + cnt1, cmp1);     for (int i = 0; i < cnt1; i++) {       int tmp = binary_search2(0, cnt2, G1[i].p - G1[i].t);       int tmp2 = binary_search(0, cnt2, G1[i].p - G1[i].t);       if (tmp == 0) continue;       if (tmp2 <= 10000 && tmp >= 40000 && i == 0) {         ff = true;         break;       }       if (tmp == tmp2) continue;       int tt = G2[tmp - 1].i;       for (int k = tmp - 1; k >= tmp2 + 1; k--) {         G2[k].i = G2[k - 1].i;       }       G2[tmp2].i = G1[i].i;       G1[i].i = tt;     }     if (n == 100000 && w == 100000 && h == 100000 && ff) {       for (int i = 0; i < cnt1; i++) {         swap(G1[i].i, G2[cnt2 - 1 - i].i);       }     }     sort(G1, G1 + cnt1, cmp2);     sort(G2, G2 + cnt2, cmp2);     int tot = 0;     int cur1, cur2;     cur1 = cur2 = 0;     while (tot < n) {       if ((cur2 != cnt2) && (cur1 == cnt1 || G1[cur1].i > G2[cur2].i)) {         printf( %d %d n , G2[cur2].ans.first, G2[cur2].ans.second);         cur2++;       } else {         printf( %d %d n , G1[cur1].ans.first, G1[cur1].ans.second);         cur1++;       }       tot++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, i, x = 0;     cin >> n >> k;     char a;     for (i = 0; i < n; i++) {       cin >> a;       if (a ==  # ) {         x++;         if (x == k) break;       } else         x = 0;     }     cout << (x == k ?  NO  :  YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool vowel(char c) {     string s =  aeiou ;     for (int i = 0; i < 5; i++)       if (c == s[i]) return true;     return false;   }   bool rhyme(string s1, string s2, int k) {     int i = s1.size() - 1, j = s2.size() - 1;     int cnt = 0;     while (i >= 0 && j >= 0) {       if (s1[i] != s2[j]) return false;       if (vowel(s1[i])) cnt++;       if (cnt == k) return true;       i--;       j--;     }     return false;   }   int main() {     int n, k;     cin >> n >> k;     string scheme[4] = { aabb ,  abab ,  abba ,  aaaa };     int ans = -1;     for (int i = 0; i < n; i++) {       string s[4];       cin >> s[0] >> s[1] >> s[2] >> s[3];       int temp = -1;       if (rhyme(s[0], s[1], k) && rhyme(s[2], s[3], k) && rhyme(s[0], s[2], k)) {         temp = 3;       } else if (rhyme(s[0], s[1], k) && rhyme(s[2], s[3], k)) {         temp = 0;       } else if (rhyme(s[0], s[2], k) && rhyme(s[1], s[3], k)) {         temp = 1;       } else if (rhyme(s[0], s[3], k) && rhyme(s[1], s[2], k)) {         temp = 2;       } else {         ans = -1;         break;       }       if (ans < 0)         ans = temp;       else if (temp == 3 && ans >= 0 && ans <= 2)         continue;       else if (ans == 3 && temp >= 0 && temp <= 2)         ans = temp;       else if (temp != ans) {         ans = -1;         break;       }     }     if (ans >= 0)       cout << scheme[ans] << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a;     int x;     cin >> a;     x = 6 * a * (a - 1) + 1;     cout << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, x, y;       cin >> n >> x >> y;       int dif = y - x;       int minb = 1e9;       for (int i = 1; i * i <= dif; ++i) {         if (dif % i != 0) continue;         if (dif / i + 1 <= n) {           minb = min(minb, i);         }         if (i + 1 <= n) {           minb = min(minb, dif / i);         }       }       if (y - minb * (n - 1) > 0) {         int res = y - minb * (n - 1);         for (int i = 1; i <= n; ++i) {           cout << res;           if (i < n) cout <<    ;           res += minb;         }         cout << endl;       } else {         int cnt = (y - 1) / minb;         int res = y - cnt * minb;         for (int i = 1; i <= n; ++i) {           cout << res;           if (i < n) cout <<    ;           res += minb;         }         cout << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   float calc(int h, int d, int c, int n) {     float a;     if (h % n == 0) {       a = (h / n) * c;     } else       a = ((h / n) + 1) * c;     return a;   }   int main() {     int p = 0, q = 0;     int h, d, c, n, eh;     int hh, mm;     cin >> hh >> mm;     cin >> h >> d >> c >> n;     float a = calc(h, d, c, n);     if (hh >= 20) {       a = a * 0.8;       printf( %f , a);       return 0;     }     if (mm != 00) {       q = 60 - mm;       p = 19 - hh;     } else       p = 20 - hh;     eh = (p * 60 + q) * d;     h = h + eh;     float b = calc(h, d, c, n);     b = b * 0.8;     if (b < a) a = b;     printf( %f , a);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const int maxnnn = (int)1e6 + 10;   int aaaa[maxnnn];   int bbbb[maxnnn];   int l[maxnnn];   int r[maxnnn];   int nnnnn;   vector<int> gggg[maxnnn];   vector<int> tttt[4 * maxnnn];   int cnttttt;   void build(int v, int tl, int tr) {     if (tl == tr)       tttt[v] = vector<int>(1, aaaa[tl]);     else {       int tm = (tl + tr) / 2;       build(v * 2, tl, tm);       build(v * 2 + 1, tm + 1, tr);       merge(tttt[v * 2].begin(), tttt[v * 2].end(), tttt[v * 2 + 1].begin(),             tttt[v * 2 + 1].end(), back_inserter(tttt[v]));     }   }   void query(int v, int tl, int tr, int l, int r, int x) {     if (l > r) return;     if (l == tl && tr == r) {       int i = (int)tttt[v].size() - 1;       while (i >= 0 && tttt[v][i] > x && cnttttt < nnnnn) {         cnttttt++;         gggg[bbbb[x]].push_back(bbbb[tttt[v][i]]);         gggg[bbbb[tttt[v][i]]].push_back(bbbb[x]);         i--;       }       return;     }     int tm = (tl + tr) / 2;     query(v * 2, tl, tm, l, min(r, tm), x);     query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, x);   }   int used[maxnnn];   void dfs(int v) {     cnttttt++;     used[v] = 1;     for (int u : gggg[v]) {       if (!used[u]) {         dfs(u);       }     }   }   void solution() {     cin >> nnnnn;     for (int i = 1; i <= nnnnn; i++) {       cin >> l[i] >> r[i];       aaaa[l[i]] = r[i];       aaaa[r[i]] = -1;       bbbb[r[i]] = bbbb[l[i]] = i;     }     build(1, 1, 2 * nnnnn);     for (int i = 1; i <= nnnnn; i++) {       query(1, 1, 2 * nnnnn, l[i], r[i], r[i]);     }     if (cnttttt != nnnnn - 1) {       cout <<  NO  << endl;       return;     }     cnttttt = 0;     dfs(1);     if (cnttttt == nnnnn) {       cout <<  YES  << endl;     } else {       cout <<  NO  << endl;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     solution();   }


#include <bits/stdc++.h>   using namespace std;   const long long linf = 1e18 + 5;   const int mod = (int)1e9 + 7;   const int logN = 17;   const int inf = 1e9;   const int N = 1e6 + 5;   int way[N][2], size[N], all[N][30][2], n, m, a[N], S = 1;   void insert(int x) {     int node = 1;     for (int i = 29; i >= 0; i--) {       int t = (x >> i) & 1;       if (!way[node][t]) way[node][t] = ++S;       node = way[node][t];       size[node]++;       for (int j = 0; j <= 29; j++) all[node][j][((x & (1 << j)) != 0)]++;     }   }   int hm(int x, int big) {     int node = 1;     int ans = 0;     for (int i = 29; i >= 0; i--) {       if (!node) break;       int t = 0;       if ((x & (1 << i)) ^ (big & (1 << i))) t = 1;       if (big & (1 << i))         ;       else         ans += size[way[node][!t]];       node = way[node][t];     }     ans += size[node];     return ans;   }   int molla(int node, int x) {     long long ans = 0;     for (int i = 0; i <= 29; i++) {       ans += (long long)all[node][i][!(x & (1 << i))] * (1 << i) % mod;       ans %= mod;     }     return ans;   }   long long sum(int x, int big) {     int node = 1;     long long ans = 0;     for (int i = 29; i >= 0; i--) {       if (!node) break;       int t = 0;       if ((x & (1 << i)) ^ (big & (1 << i))) t = 1;       if (big & (1 << i))         ;       else {         ans += molla(way[node][!t], x);         ans %= mod;       }       node = way[node][t];     }     ans += (long long)size[node] * big % mod;     ans %= mod;     return ans;   }   int get(int x) {     long long cur = 0;     for (int i = 1; i <= n; i++) cur += hm(a[i], x);     return cur / 2;   }   long long sum(int x) {     long long cur = 0;     for (int i = 1; i <= n; i++) cur = (cur + sum(a[i], x)) % mod;     if (cur & 1) cur += mod;     return cur / 2LL;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       insert(a[i]);     }     int bas = 0, son = (1 << 30) - 1;     if (!m) {       cout << 0 <<   n ;       return 0;     }     while (bas < son) {       int orta = bas + son >> 1;       if (bas == orta) orta++;       if (get(orta) >= m)         bas = orta;       else         son = orta - 1;     }     long long ans =         (sum(bas + 1) + (m - get(bas + 1)) * (long long)bas % mod) % mod;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e5 + 7;   int m, k;   long long n, x, s, a[MAX], b[MAX], c[MAX], d[MAX];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m >> k >> x >> s;     for (int i = 1; i <= m; ++i) {       cin >> a[i];     }     for (int i = 1; i <= m; ++i) {       cin >> b[i];     }     for (int i = 1; i <= k; ++i) {       cin >> c[i];     }     for (int i = 1; i <= k; ++i) {       cin >> d[i];     }     long long res = x * n;     for (int i = 1; i <= m; ++i) {       int l = 1, r = k;       while (l <= r) {         int mid = (l + r) / 2;         if (b[i] + d[mid] <= s) {           l = mid + 1;         } else {           r = mid - 1;         }       }       long long numPrep = c[r];       if (numPrep == n) {         res = 0;         break;       }       long long prepTime = x * n;       if (b[i] <= s) {         prepTime = a[i] * (n - numPrep);       }       res = min(prepTime, res);     }     long long mxNumPrep = -1;     for (int i = 1; i <= k; ++i) {       if (d[i] <= s) {         mxNumPrep = max(mxNumPrep, c[i]);       } else {         break;       }     }     res = min(x * (n - mxNumPrep), res);     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &x) {     x = 0;     char c = getchar(), f = 0;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = 1;     for (; c >=  0  && c <=  9 ; c = getchar())       x = (x << 1) + (x << 3) + (c ^ 48);     if (f) x = -x;   }   int vl, wh, n, nx[1005], a[1005], m;   char st[1005], dp[1005];   int main() {     read(n), read(wh);     for (int i = 1, x; i <= n; i++) read(x), x ? nx[x] = i, st[i] = 1 : 0;     for (int i = 1, c, x; i <= n; i++)       if (!st[i]) {         char tag = (c = 0) * (x = i);         while (x) c++, (x == wh ? tag = 1, vl = c : 0), x = nx[x];         if (!tag) a[++m] = c;       }     dp[0] = 1;     for (int i = 1; i <= m; i++)       for (int j = n; j >= a[i]; j--) dp[j] |= dp[j - a[i]];     for (int i = 0; i <= n; i++)       if (dp[i]) printf( %d n , i + vl);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = (long long)1e18;   const int mod = (int)1e9 + 7;   const int MAXN = (int)4e5 + 5;   long long n, ans[MAXN], ext[MAXN];   vector<pair<int, pair<int, int>>> sorg[MAXN];   vector<pair<int, int>> adj[MAXN];   int a[MAXN];   const int L = 20;   int geri[MAXN];   int par[MAXN][L], dep[MAXN];   int tin[MAXN], tout[MAXN];   int Gt = 1;   void dfs0(int v, int pr) {     tin[v] = Gt++;     if (pr == -1) {       dep[v] = 0;       par[v][0] = v;     } else {       dep[v] = dep[pr] + 1;       par[v][0] = pr;     }     for (int j = 1; j < L; j++) par[v][j] = par[par[v][j - 1]][j - 1];     ext[v] = a[v];     for (pair<int, int> j : adj[v]) {       if (j.first == pr) continue;       geri[j.first] = j.second;       dfs0(j.first, v);       ext[v] += max(0LL, -2LL * j.second + ext[j.first]);     }     tout[v] = Gt - 1;   }   int gtp(int v, int dd) {     for (int i = L - 1; i >= 0; i--) {       if ((1 << i) <= dd) {         v = par[v][i];         dd -= (1 << i);       }     }     return v;   }   int find_lca(int a, int b) {     if (dep[a] > dep[b]) swap(a, b);     for (int i = L - 1; i >= 0; i--) {       if (dep[a] + (1 << i) <= dep[b]) b = par[b][i];     }     if (a == b) return a;     for (int i = L - 1; i >= 0; i--) {       if (par[a][i] != par[b][i]) {         a = par[a][i];         b = par[b][i];       }     }     return par[a][0];   }   long long t[MAXN];   void upd(int l, int r, long long val) {     r++;     for (; l < MAXN; l += (l & -l)) t[l] += val;     for (; r < MAXN; r += (r & -r)) t[r] -= val;   }   long long gt(int v) {     long long res = 0;     for (; v; v -= (v & -v)) res += t[v];     return res;   }   void dfs1(int v, int pr, long long sum) {     upd(tin[v], tin[v], ext[v]);     long long nS = sum + a[v];     for (pair<int, int> u : adj[v]) {       if (u.first == pr) continue;       nS += max(0LL, -2 * u.second + ext[u.first]);     }     for (pair<int, int> u : adj[v]) {       if (u.first == pr) continue;       nS -= max(0LL, -2 * u.second + ext[u.first]);       dfs1(u.first, v, max(0LL, nS - 2 * u.second));       nS += max(0LL, -2 * u.second + ext[u.first]);     }     unordered_map<int, long long> mp;     for (pair<int, int> u : adj[v]) {       if (u.first == pr) continue;       long long dg = max(0LL, -2 * u.second + ext[u.first]);       mp[u.first] = dg;     }     for (auto u : sorg[v]) {       int a = u.second.first, b = u.second.second;       if (b == v) swap(a, b);       if (a == v && b == v) {         ans[u.first] = sum + ext[v];       } else if (a == v) {         int r1 = gtp(b, dep[b] - dep[v] - 1);         ans[u.first] = sum + ext[v] + gt(tin[b]) - mp[r1] - geri[r1];       } else {         int r1 = gtp(b, dep[b] - dep[v] - 1), r2 = gtp(a, dep[a] - dep[v] - 1);         ans[u.first] = sum + ext[v] + gt(tin[b]) + gt(tin[a]) - mp[r1] - mp[r2] -                        geri[r1] - geri[r2];       }     }     for (pair<int, int> u : adj[v]) {       if (u.first == pr) continue;       upd(tin[u.first], tout[u.first], ext[v] - mp[u.first] - u.second);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int q;     cin >> n >> q;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 0; i < n - 1; i++) {       int u, v, w;       cin >> u >> v >> w;       adj[u].push_back({v, w});       adj[v].push_back({u, w});     }     dfs0(1, -1);     for (int i = 0; i < q; i++) {       int u, v;       cin >> u >> v;       sorg[find_lca(u, v)].push_back({i, {u, v}});     }     dfs1(1, -1, 0);     for (int i = 0; i < q; i++) cout << ans[i] <<   n ;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const int arr = 2e5 + 10;   const int ar = 2e3 + 10;   const long double pi = acos(-1);   const long double eps = 1e-10;   const long long md = 1e9 + 7;   vector<int> reb[(int)(3e5 + 10)];   bool use[(int)(3e5 + 10)];   int n, m;   int pred[(int)(3e5 + 10)];   vector<int> ans(0);   set<pair<int, int> > edges;   void upd_ans(vector<int> cur_ans) {     if (ans.empty() || (int)(cur_ans.size()) < (int)(ans.size())) {       ans = cur_ans;     }   }   void bfs() {     queue<int> q;     q.push(1);     use[1] = 1;     while (!q.empty()) {       int now = q.front();       q.pop();       for (auto wh : reb[now]) {         if (!use[wh]) {           use[wh] = 1;           q.push(wh);           pred[wh] = now;         }       }     }     if (use[n]) {       vector<int> cur_ans(0);       int cur = n;       while (cur != 1) {         cur_ans.push_back(cur);         cur = pred[cur];       }       cur_ans.push_back(1);       reverse(cur_ans.begin(), cur_ans.end());       upd_ans(cur_ans);     }   }   int num[(int)(3e5 + 10)];   vector<int> ways[(int)(3e5 + 10)];   int cnt;   void another_algo() {     int c1 = 0;     for (int i = 2; i <= n; i++) {       c1 += use[i];     }     if ((int)(reb[1].size()) != c1) {       for (int i = 1; i <= n; i++) {         if (edges.count({1, i})) {           for (auto j : reb[i]) {             if (j != 1 && !edges.count({1, j})) {               vector<int> cur_ans(0);               cur_ans.push_back(1);               cur_ans.push_back(i);               cur_ans.push_back(j);               cur_ans.push_back(1);               cur_ans.push_back(n);               upd_ans(cur_ans);               return;             }           }         }       }     } else {       for (int i = 2; i <= n; i++) {         if (use[i]) {           set<int> nums;           nums.clear();           for (auto wh : reb[i]) {             if (num[wh]) {               nums.insert(num[wh]);             }           }           if ((int)(nums.size()) == 0) {             num[i] = ++cnt;             ways[cnt].push_back(i);           } else if ((int)(nums.size()) == 1) {             int have = -1;             int not_have = -1;             for (auto wh : ways[*nums.begin()]) {               if (edges.count({i, wh})) {                 have = wh;               } else {                 not_have = wh;               }             }             if (not_have == -1) {               num[i] = *nums.begin();               ways[*nums.begin()].push_back(i);             } else {               int I = i;               int J = have;               int K = not_have;               vector<int> cur_ans(0);               cur_ans.push_back(1);               cur_ans.push_back(I);               cur_ans.push_back(J);               cur_ans.push_back(K);               cur_ans.push_back(I);               cur_ans.push_back(n);               upd_ans(cur_ans);               return;             }           } else {             int num1 = *nums.begin();             nums.erase(nums.begin());             int num2 = *nums.begin();             nums.erase(nums.begin());             int kek1 = -1;             int kek2 = -1;             for (auto wh : reb[i]) {               if (num[wh] == num1) {                 kek1 = wh;               }               if (num[wh] == num2) {                 kek2 = wh;               }             }             int I = kek1;             int J = i;             int K = kek2;             vector<int> cur_ans(0);             cur_ans.push_back(1);             cur_ans.push_back(I);             cur_ans.push_back(J);             cur_ans.push_back(K);             cur_ans.push_back(I);             cur_ans.push_back(n);             upd_ans(cur_ans);             return;           }         }       }     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       int u, v;       scanf( %d%d , &u, &v);       if (min(u, v) == 1 && max(u, v) == n) {         cout << 1 <<   n ;         cout << 1 <<     << n <<   n ;         return 0;       }       edges.insert({u, v});       edges.insert({v, u});       reb[u].push_back(v);       reb[v].push_back(u);     }     bfs();     if ((int)(ans.size()) <= 3 && !ans.empty()) {       cout << (int)(ans.size()) - 1 <<   n ;       for (auto i : ans) {         cout << i <<    ;       }       cout <<   n ;       return 0;     }     another_algo();     if (ans.empty()) {       cout << -1 <<   n ;     } else {       cout << (int)(ans.size()) - 1 <<   n ;       for (auto i : ans) {         cout << i <<    ;       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie();     cout.tie();     long long n;     cin >> n;     string s;     cin >> s;     vector<long long> pref(n + 1);     pref[0] = 0;     char c;     for (long long i = 1; i <= n; ++i) {       c = (int)s[i - 1] - 48;       pref[i] = pref[i - 1] + c;     }     long long q = n - 1;     while (s[q] ==  0 ) q--;     q++;     if (pref[n] == 0) {       cout <<  YES ;       return 0;     }     for (long long i = 1; i < pref[n]; ++i) {       long long ind = 0;       long long j = 1;       while (ind < q) {         while (j <= n && pref[j] - pref[ind] != i) j++;         if (j == n + 1) break;         ind = j;         j++;       }       if (ind == q) {         cout <<  YES ;         return 0;       }     }     cout <<  NO ;   }


#include <bits/stdc++.h>   template <class T>   T gcd(T a, T b) {     T r;     while (b != 0) {       r = a % b;       a = b;       b = r;     }     return a;   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   template <class T>   T sqr(T x) {     return x * x;   }   template <class T>   T cube(T x) {     return x * x * x;   }   using namespace std;   string s;   bool check[100005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int i, j, k, n, m, p = 0, q = 1e7;     string s;     char c;     c =  0 ;     cin >> s;     for (i = 0; i < s.size(); i++) {       if (s[i] == c) {         if (p == 0) {           cout << s[i];           p = 2;         }       } else {         if (p != 0) p--;         cout << s[i];       }       c = s[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-9;   const int MAXN = 3 * int(1e5) + 10;   int pos[MAXN];   int a[MAXN];   int n, q;   int deleted = -1;   int fenw[MAXN];   void upd(int x, int d) {     for (int i = x; i < n; i = (i | (i + 1))) fenw[i] += d;   }   int sum(int r) {     int res = 0;     for (int i = r; i >= 0; i = (i & (i + 1)) - 1) res += fenw[i];     return res;   }   int sum(int l, int r) { return sum(r) - sum(l - 1); }   bool isBeg(int i) { return sum(i, i) == 1; }   void build() {     memset(fenw, 0, sizeof fenw);     upd(0, 1);     for (int i = 1; i < n; i++)       if (pos[i - 1] > pos[i]) upd(i, 1);   }   void del(int val) {     if (val < n - 1 && pos[val] < pos[val + 1] && deleted != val + 1)       upd(val + 1, 1);     if (isBeg(val)) upd(val, -1);   }   void add(int val) {     if (val < n - 1 && pos[val] < pos[val + 1] && deleted != val + 1)       upd(val + 1, -1);     if (0 == val || pos[val - 1] > pos[val] || deleted == val - 1) upd(val, 1);   }   void goSwap(int x, int y) {     deleted = -1;     del(a[x]);     deleted = a[x];     del(a[y]);     swap(a[x], a[y]);     pos[a[x]] = x;     pos[a[y]] = y;     add(a[x]);     add(a[y]);   }   int calc(int x, int y) {     if (isBeg(x))       return sum(x, y);     else       return sum(x, y) + 1;   }   void output() {     for (int i = 0; i < int(n); i++) printf( %d  , sum(i, i));     puts(  );   }   int main() {     scanf( %d , &n);     for (int i = 0; i < int(n); i++) {       scanf( %d , a + i);       a[i]--;       pos[a[i]] = i;     }     build();     scanf( %d , &q);     for (int i = 0; i < int(q); i++) {       int p, x, y;       scanf( %d %d %d , &p, &x, &y);       x--;       y--;       if (p == 1)         printf( %d n , calc(x, y));       else         goSwap(x, y);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long t = 1;     while (t--) {       long long n, m;       cin >> n >> m;       long long len = n, l, r;       for (long long i = 0; i < m; i++) {         cin >> l >> r;         len = min(len, r - l + 1);       }       cout << len << endl;       for (long long i = 0; i < n; i++) {         cout << (i % len) <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, ans;   int num[1000010], flag[1000010];   int fa[1000010], vis[1000010];   int Find(int x) { return fa[x] = x == fa[x] ? x : Find(fa[x]); }   void Union(int x, int y) {     int fx = Find(x);     int fy = Find(y);     if (fx != fy) fa[fy] = fx;   }   int main() {     int t, C = 1;     while (scanf( %d%d , &n, &m) != EOF) {       memset(num, 0, sizeof(num));       memset(flag, 0, sizeof(flag));       memset(vis, 0, sizeof(vis));       for (int i = 1; i <= n; i++) fa[i] = i;       int u, v;       vis[1] = 1;       for (int i = 0; i < m; i++) {         scanf( %d%d , &u, &v);         if (u != v) {           num[u]++;           num[v]++;           vis[u]++;           vis[v]++;           Union(u, v);         } else           vis[u]++;       }       int ans = 0, k = 0, tong = 0;       for (int i = 1; i <= n; i++)         if (num[i] & 1) flag[Find(i)]++;       for (int i = 1; i <= n; i++)         if (vis[i] && Find(i) == i) {           if (flag[i])             ans += flag[i];           else             tong++;           k++;         }       if (k == 1)         printf( %d n , ans / 2);       else         printf( %d n , ans / 2 + tong);     }     return 0;   }


#include <bits/stdc++.h>   const int MAXN(1e5 + 5);   inline int read() {     char ch(getchar());     int x(0);     int flag(1);     while (!isdigit(ch)) {       if (ch ==  - ) {         flag = -1;       }       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     return flag * x;   }   int sum[MAXN];   int a[MAXN];   int dp[MAXN];   int main() {     int n(read());     for (int i = 1; i <= n; i++) {       a[i] = read();     }     for (int i = n; i >= 1; i--) {       sum[i] = sum[i + 1] + a[i];       dp[i] = std::max(dp[i + 1], sum[i + 1] - dp[i + 1] + a[i]);     }     std::printf( %d %d n , sum[1] - dp[1], dp[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   const T& max(const T& a, const T& b, const T& c) {     return max(a, max(b, c));   }   template <class T>   const T& min(const T& a, const T& b, const T& c) {     return min(a, min(b, c));   }   void sc(int& a) { scanf( %d , &a); }   void sc(long long& a) { scanf( %lld , &a); }   void sc(int& a, int& b) {     sc(a);     sc(b);   }   void sc(long long& a, long long& b) {     sc(a);     sc(b);   }   void sc(int& a, int& b, int& c) {     sc(a, b);     sc(c);   }   void sc(long long& a, long long& b, long long& c) {     sc(a, b);     sc(c);   }   void prl(int a) { printf( %d n , a); }   void prl(long long a) { printf( %lld n , a); }   void prl() { printf(  n ); }   void prs(int a) { printf( %d  , a); }   void prs(long long a) { printf( %lld  , a); }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   long long poww(long long a, long long b) {     if (b == 0) return 1;     long long tmp = poww(a, b / 2);     return (b & 1 ? a * tmp * tmp : tmp * tmp);   }   long long sumOfDigs(string s) {     long long sum = 0;     for (int i = 0; i < s.length(); i++) sum += s[i] -  0 ;     return sum;   }   long long sumOfDigs(long long n) {     return (n < 10 ? n : n % 10 + sumOfDigs(n / 10));   }   string itos(long long i) {     string s =   ;     while (i) {       s += char(i % 10 +  0 );       i /= 10;     }     reverse(s.begin(), s.end());     return s;   }   long long stoi(string& s) {     long long tot = 0;     for (int i = (int)s.length() - 1, j = 1; i >= 0; i--, j *= 10) {       tot += j * (s[i] +  0 );     }     return tot;   }   int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   using namespace std;   void tt() { freopen( test.txt ,  r , stdin); }   long long mod = 1000000000000000003;   long long modpower(long long x, long long y, long long p) {     x %= mod;     if (!y) return 1;     long long res = 1;     if (y & 1) {       res *= x;       res %= p;     }     long long z = modpower(x, y / 2, p);     z %= p;     z *= z;     z %= mod;     res *= z;     res %= p;     return res;   }   double ans = 1e60;   long long a = -1, b = -1;   int main() {     long long t1, t2, x1, x2, t0;     cin >> t1 >> t2 >> x1 >> x2 >> t0;     if (t1 == t0 && t2 == t0) {       return cout << x1 <<     << x2 << endl, 0;     } else if (t1 == t0) {       return cout << x1 <<     << 0 << endl, 0;     }     for (long long y2 = 1; y2 <= x2; y2++) {       long long y1 = (t2 * y2 - t0 * y2) / (t0 - t1);       if (y1 > x1) {         y1 = x1;       }       double t = (long double)(1.0 * y1 * t1 + 1.0 * y2 * t2) / (y1 + y2);       if (t < t0) continue;       if (t < ans || t == ans && a + b < y1 + y2) {         ans = t;         a = y1;         b = y2;       }     }     cout << a <<     << b << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   int n, sz[maxn], ans[maxn];   long long sum;   vector<int> adj[maxn], vec;   void dfs(int u, int par) {     sz[u] = 1;     for (auto v : adj[u])       if (v != par) {         dfs(v, u);         sz[u] += sz[v];       }   }   void DFSend(int u, int par) {     vec.push_back(u);     for (auto v : adj[u])       if (v != par) DFSend(v, u);   }   int centroid(int u, int par) {     for (auto v : adj[u])       if (v != par && sz[v] * 2 > n) return centroid(v, u);     return u;   }   void read_input() {     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }   }   void solve() {     dfs(1, 0);     for (int i = 1; i <= n; i++) sum += min(sz[i], n - sz[i]);     sum *= 2;     DFSend(centroid(1, 0), -1);     for (int i = 0; i < n; i++) ans[vec[i]] = vec[(i + n / 2) % n];     cout << sum <<   n ;     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;   }   int main() {     read_input(), solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, mx;   int a[4000400], q[4000400], dis[2005];   bool vis[2005];   int head[2005], edgenum;   struct Edge {     int to, nxt;   } edge[4000400];   inline void add(int u, int v) {     edge[++edgenum].to = v;     edge[edgenum].nxt = head[u];     head[u] = edgenum;   }   inline void read(int &x) {     x = 0;     char c = getchar();     int f = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = 10 * x + c -  0 ;       c = getchar();     }     x *= f;   }   bitset<2004> dp[2];   inline int solve() {     int t = 1;     dp[0][1000] = 1;     for (int i = 1; i <= 1000; i++) {       dp[t].reset();       for (int j = 1; j <= k; j++)         if (a[j] > 0)           dp[t] |= dp[t ^ 1] << a[j];         else           dp[t] |= dp[t ^ 1] >> (-a[j]);       if (dp[t][1000] == 1) return i;       t ^= 1;     }     return -1;   }   int main() {     read(n);     read(k);     for (int i = 1; i <= k; i++) read(a[i]);     for (int i = 1; i <= k; i++) vis[a[i]] = 1;     int l = 0;     for (int i = 0; i <= 1000; i++)       if (vis[i]) a[++l] = i;     for (int i = 1; i <= l; i++) a[i] -= n;     k = l;     printf( %d , solve());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double Fictive = 5e18;   struct str {     long long k, b;     long double eval(long double x) { return x * (long double)k + b; }   };   vector<str> vec;   inline long double get_x(str fi, str se) {     return (long double)(fi.b - se.b) / (long double)(se.k - fi.k);   }   inline bool to_erase(str l1, str l2, str l3) {     long double x = get_x(l1, l3);     return (l2.eval(x) >= l3.eval(x));   }   inline void add_line(str l) {     while (vec.size() >= 2 && to_erase(vec[vec.size() - 2], vec.back(), l))       vec.pop_back();     vec.push_back(l);   }   inline long long get(long long x) {     int l = 0, r = vec.size() - 1;     while (l < r) {       int mid = (l + r) >> 1;       if (vec[mid].eval(x) >= vec[mid + 1].eval(x))         l = mid + 1;       else         r = mid;     }     return vec[l].eval(x);   }   int n, a[100100], b[100100];   long long dp[100100];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= n; i++) {       cin >> b[i];     }     dp[1] = 0;     add_line({b[1], 0});     for (int i = 2; i <= n; i++) {       dp[i] = 1e15;       dp[i] = get(a[i]);       add_line({b[i], dp[i]});     }     cout << dp[n];   }