#include <bits/stdc++.h> n using namespace std; n vector<int> vec[1000006], V; n struct Node { n   int x, y, w; n   bool operator<(const Node& a) const { return y < a.y; } n } node[500006]; n int n; n struct Info { n   long long len, mx, w; n   int p; n   Info() {} n   Info(long long _len, long long _mx, long long _w, int _p) { n     len = _len; n     mx = _mx; n     w = _w; n     p = _p; n   } n   Info operator+(const Info& a) { n     Info ret; n     ret.len = len + a.len; n     ret.w = w + a.w; n     if (a.mx >= a.w + mx - a.len) { n       ret.mx = a.mx; n       ret.p = a.p; n     } else { n       ret.mx = a.w + mx - a.len; n       ret.p = p; n     } n     return ret; n   } n   void pr() { printf( %lld %lld %lld %d n , len, mx, w, p); } n } info[1000006 << 2]; n void build(int p, int l, int r) { n   if (l == r) { n     int len = 0; n     if (l > 1) len = V[l - 1] - V[l - 2]; n     info[p] = Info(len, 0, 0, l); n     return; n   } n   int mid = (l + r) >> 1; n   build(p << 1, l, mid); n   build(p << 1 | 1, mid + 1, r); n   info[p] = info[p << 1] + info[p << 1 | 1]; n } n void update(int p, int l, int r, int aim, long long w) { n   if (l == r) { n     info[p].w += w; n     info[p].mx += w; n     return; n   } n   int mid = (l + r) >> 1; n   if (aim <= mid) n     update(p << 1, l, mid, aim, w); n   else n     update(p << 1 | 1, mid + 1, r, aim, w); n   info[p] = info[p << 1] + info[p << 1 | 1]; n } n Info query(int p, int l, int r, int L, int R) { n   if (L <= l && r <= R) { n     return info[p]; n   } n   int mid = (l + r) >> 1; n   if (L <= mid && R > mid) n     return query(p << 1, l, mid, L, R) + query(p << 1 | 1, mid + 1, r, L, R); n   else if (L <= mid) n     return query(p << 1, l, mid, L, R); n   else if (R > mid) n     return query(p << 1 | 1, mid + 1, r, L, R); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &node[i].x, &node[i].y, &node[i].w); n     if (node[i].x > node[i].y) swap(node[i].x, node[i].y); n     V.push_back(node[i].x); n     V.push_back(node[i].y); n   } n   sort(node + 1, node + n + 1); n   sort(V.begin(), V.end()); n   V.resize(unique(V.begin(), V.end()) - V.begin()); n   for (int i = 1; i <= n; i++) { n     node[i].x = lower_bound(V.begin(), V.end(), node[i].x) - V.begin(); n     node[i].y = lower_bound(V.begin(), V.end(), node[i].y) - V.begin(); n     vec[node[i].y].push_back(i); n   } n   int End = V.size(); n   build(1, 1, End); n   long long ans = -1e15; n   int ansl, ansr; n   for (int i = 0; i < End; i++) { n     int siz = (int)vec[i].size(); n     for (int j = 0; j < siz; j++) n       update(1, 1, End, node[vec[i][j]].x + 1, node[vec[i][j]].w); n     Info tmp = query(1, 1, End, 1, i + 1); n     if (tmp.mx > ans) { n       ans = tmp.mx; n       ansl = V[tmp.p - 1]; n       ansr = V[i]; n     } n   } n   if (ans < 0) ans = 0, ansl = ansr = 2e9; n   printf( %lld n%d %d %d %d , ans, ansl, ansl, ansr, ansr); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t, arr[110], i, l, s, m, n, j; n   scanf( %d , &t); n   l = -1; n   s = 1001; n   for (i = 0; i < t; i++) scanf( %d , &arr[i]); n   for (i = 0; i < t; i++) { n     for (j = i + 1; j < t; j++) { n       if (arr[j] < arr[i]) { n         s = arr[j]; n         arr[j] = arr[i]; n         arr[i] = s; n       } n     } n   } n   printf( %d  , arr[t - 1]); n   for (i = 1; i < t - 1; i++) { n     printf( %d  , arr[i]); n   } n   printf( %d n , arr[0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[3010][6010], fa[18010000]; n int dx[9] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[9] = {-1, 0, 1, -1, 1, -1, 0, 1}; n inline int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f = ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = (x << 3) + (x << 1) + ch - 48, ch = getchar(); n   return f ? -x : x; n } n inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n inline int calc(int x, int y) { return (x - 1) * m * 2 + y; } n int main() { n   int ans = 0; n   n = read(), m = read(); n   int Q = read(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= 2 * m; j++) fa[calc(i, j)] = calc(i, j); n   if (m == 1) { n     puts( 0 ); n     return 0; n   } n   while (Q--) { n     int x = read(), y = read(); n     int flag = 0; n     for (int i = 0; i < 8; i++) { n       int xx = x + dx[i], yy = y + dy[i]; n       if (yy == 0) yy = 2 * m; n       if (xx < 1 || xx > n || !a[xx][yy]) continue; n       for (int j = 0; j < 8; j++) { n         int xxx = x + dx[j], yyy = y + m + dy[j]; n         if (yyy > 2 * m) yyy = 1; n         if (xxx < 1 || xxx > n || !a[xxx][yyy]) continue; n         if (find(calc(xx, yy)) == find(calc(xxx, yyy))) { n           flag = 1; n           break; n         } n       } n     } n     if (flag) continue; n     a[x][y] = a[x][y + m] = 1; n     ans++; n     int fx1 = find(calc(x, y)), fx2 = find(calc(x, y + m)); n     for (int i = 0; i < 8; i++) { n       int xx = x + dx[i], yy = y + dy[i]; n       if (yy > m) { n         if (a[xx][yy]) fa[find(calc(xx, yy))] = fx1; n         if (a[xx][1]) fa[find(calc(xx, 1))] = fx2; n       } else if (yy < 1) { n         if (a[xx][yy + m]) fa[find(calc(xx, yy + m))] = fx2; n         if (a[xx][2 * m]) fa[find(calc(xx, 2 * m))] = fx1; n       } else { n         if (a[xx][yy]) fa[find(calc(xx, yy))] = fx1; n         if (a[xx][yy + m]) fa[find(calc(xx, yy + m))] = fx2; n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n], i; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (n == 1) { n     cout << 1; n     return 0; n   } n   int lmarr[n + 1], rmarr[n + 1]; n   lmarr[0] = INT_MIN; n   rmarr[n] = INT_MAX; n   for (i = 0; i < n; i++) { n     lmarr[i + 1] = max(a[i], lmarr[i]); n   } n   for (i = n - 1; i >= 0; i--) { n     rmarr[i] = min(rmarr[i + 1], a[i]); n   } n   int count = 1; n   for (i = 1; i < n; i++) { n     if (lmarr[i] <= rmarr[i]) { n       count++; n     } n   } n   cout << count; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n template <typename T, size_t N> n int SIZE(const T (&t)[N]) { n   return N; n } n template <typename T> n int SIZE(const T &t) { n   return t.size(); n } n string to_string(const string s, int x1 = 0, int x2 = 1e9) { n   return     + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) :   ) +    ; n } n string to_string(const char *s) { return to_string((string)s); } n string to_string(const bool b) { return (b ?  true  :  false ); } n string to_string(const char c) { return string({c}); } n template <size_t N> n string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { n   string t =   ; n   for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); n        __iii__ <= __jjj__; ++__iii__) { n     t += b[__iii__] +  0 ; n   } n   return     + t +    ; n } n template <typename A, typename... C> n string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords); n int l_v_l_v_l = 0, t_a_b_s = 0; n template <typename A, typename B> n string to_string(const pair<A, B> &p) { n   l_v_l_v_l++; n   string res =  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n   l_v_l_v_l--; n   return res; n } n template <typename A, typename... C> n string to_string(const A(&v), int x1, int x2, C... coords) { n   int rnk = rank<A>::value; n   string tab(t_a_b_s,    ); n   string res =   ; n   bool first = true; n   if (l_v_l_v_l == 0) res +=   n ; n   res += tab +  [ ; n   x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); n   auto l = begin(v); n   advance(l, x1); n   auto r = l; n   advance(r, (x2 - x1) + (x2 < SIZE(v))); n   for (auto e = l; e != r; e = next(e)) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     l_v_l_v_l++; n     if (e != l) { n       if (rnk > 1) { n         res +=   n ; n         t_a_b_s = l_v_l_v_l; n       }; n     } else { n       t_a_b_s = 0; n     } n     res += to_string(*e, coords...); n     l_v_l_v_l--; n   } n   res +=  ] ; n   if (l_v_l_v_l == 0) res +=   n ; n   return res; n } n void dbgm() { ; } n template <typename Heads, typename... Tails> n void dbgm(Heads H, Tails... T) { n   cout << to_string(H) <<   |  ; n   dbgm(T...); n } n const long long MOD = (int)1e9 + 7; n const long long INF = 2e18 + 5; n struct hashLL { n   size_t operator()(long long x) const { n     x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); n     x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb); n     x = x ^ (x >> 31); n     return x; n   } n }; n long long inv(long long a, long long b) { n   return 1 < a ? b - inv(b % a, a) * b / a : 1; n } n long long gcd(long long a, long long b, long long &x, long long &y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   long long x1, y1; n   long long d = gcd(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n long long distsq(long long x1, long long y1, long long x2, long long y2) { n   return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); n } n const int N = 100001; n long long n, m, k, x, y, w; n unordered_map<int, unordered_map<int, int>> g; n string s; n pair<long long, long long> solve(long long curr, long long par) { n   long long returnwt = 0; n   long long endwt = 0; n   vector<long long> v; n   for (auto p : g[curr]) { n     long long child = p.first; n     long long wt = p.second; n     if (child == par) continue; n     returnwt += wt; n     pair<long long, long long> x = solve(child, curr); n     returnwt += x.first; n     returnwt += wt; n     if (-wt - wt - x.first + x.second + wt < endwt) { n       endwt = -wt - wt - x.first + x.second + wt; n     } n   } n   return {returnwt, returnwt + endwt}; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < (n - 1); i++) { n     cin >> x >> y >> w; n     g[x][y] = w; n     g[y][x] = w; n   } n   long long res = solve(1, 0).second; n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l, r; n vector<pair<int, int> > G[100005]; n int seq[100005], cnts, siz[100005], rch[100005], num[100005]; n bool vis[100005]; n int rt, Min; n void dfs(int now, int ff, int tsz) { n   int Max = 0; n   siz[now] = 1; n   rch[now] = 0; n   for (int i = 0, v, sz = G[now].size(); i < sz; i++) n     if ((v = G[now][i].first) != ff && !vis[v]) { n       dfs(v, now, tsz); n       rch[now] = max(rch[now], rch[v] + 1); n       siz[now] += siz[v]; n       Max = max(Max, siz[v]); n     } n   Max = max(Max, tsz - siz[now]); n   if (Max < Min) Min = Max, rt = now; n } n int Gert(int now, int tsz) { n   Min = 0x3f3f3f3f; n   dfs(now, -1, tsz); n   return rt; n } n bool cmp(const pair<int, int> &a, const pair<int, int> &b) { n   return rch[a.first] < rch[b.first]; n } n void setsiz(int now, int ff) { n   siz[now] = 1, rch[now] = 0; n   for (int i = 0, sz = G[now].size(), v; i < sz; i++) n     if ((v = G[now][i].first) != ff && !vis[v]) { n       setsiz(v, now); n       siz[now] += siz[v]; n       rch[now] = max(rch[now], rch[v] + 1); n     } n } n void Solve(int now) { n   seq[++cnts] = now; n   setsiz(now, -1); n   vis[now] = 1; n   rch[now] = 0; n   sort(G[now].begin(), G[now].end(), cmp); n   for (int v, i = 0, sz = G[now].size(); i < sz; i++) n     if (!vis[(v = G[now][i].first)]) Solve(Gert(v, siz[v])); n } n int f[100005], g[100005], floc[100005], gloc[100005], ansx, ansy; n int Maxdep; n void ser(int now, int ff, int dist, int cost, int Lim) { n   Maxdep = max(Maxdep, dist); n   if (cost > g[dist]) g[dist] = cost, gloc[dist] = now; n   for (int i = 0, sz = G[now].size(), v; i < sz; i++) n     if ((v = G[now][i].first) != ff && !vis[v]) n       ser(v, now, dist + 1, cost + (G[now][i].second >= Lim ? 1 : -1), Lim); n } n int Q[100005], L, R; n bool check(int Lim) { n   memset(vis, 0, sizeof vis); n   memset(f, -0x3f, sizeof f); n   memset(g, -0x3f, sizeof g); n   f[0] = 0; n   int premaxdep = 0; n   for (int u, id = 1; id <= n; id++) { n     for (int i = 1; i <= premaxdep; i++) f[i] = -0x3f3f3f3f, g[i] = -0x3f3f3f3f; n     premaxdep = 0; n     vis[u = seq[id]] = 1; n     floc[0] = u; n     for (int predep = 0, i = 0, v, sz = G[u].size(); i < sz; i++) n       if (!vis[v = G[u][i].first]) { n         for (int j = 1; j <= predep; j++) g[j] = -0x3f3f3f3f; n         Maxdep = 0; n         ser(v, -1, 1, G[u][i].second >= Lim ? 1 : -1, Lim); n         L = R = 0; n         for (int j = 0, k = min(r, Maxdep); j <= predep; j++) { n           for (; j + k >= l && k >= 0; k--) { n             for (; L < R && g[k] >= g[Q[R - 1]]; R--) n               ; n             Q[R++] = k; n           } n           for (; L < R && Q[L] + j > r; L++) n             ; n           if (L < R && f[j] + g[Q[L]] >= 0) { n             ansx = floc[j]; n             ansy = gloc[Q[L]]; n             return 1; n           } n         } n         for (int j = 1; j <= Maxdep; j++) n           if (f[j] < g[j]) f[j] = g[j], floc[j] = gloc[j]; n         predep = Maxdep; n         premaxdep = max(premaxdep, Maxdep); n       } n   } n   return 0; n } n int main() { n   int u, v, w; n   scanf( %d%d%d , &n, &l, &r); n   for (int i = 1; i < n; i++) { n     scanf( %d%d%d , &u, &v, &w); n     G[u].push_back(make_pair(v, w)); n     G[v].push_back(make_pair(u, w)); n   } n   Solve(Gert(1, n)); n   int L = 0, R = 1000000000, Mid; n   while (L < R) { n     Mid = (L + R + 1) >> 1; n     if (check(Mid)) n       L = Mid; n     else n       R = Mid - 1; n   } n   check(L); n   printf( %d %d n , ansx, ansy); n } n


#include <bits/stdc++.h> n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); n } n using namespace std; n int n, m; n struct Edge { n   int u, v, w, id; n   Edge() {} n   Edge(int jzp, int zzz, int lsr, int chr) { n     u = jzp, v = zzz, w = lsr, id = chr; n   } n } E[301000], Ev[301000]; n bool cmp(const Edge &a, const Edge &b) { return a.w < b.w; } n int stkx[301000], stky[301000], stop, siz[301000], oddcnt, fath[301000]; n int find(int cur) { return fath[cur] == cur ? cur : find(fath[cur]); } n inline void merge(int x, int y) { n   x = find(x), y = find(y); n   if (x == y) return; n   if (siz[x] > siz[y]) swap(x, y); n   stkx[++stop] = x, stky[stop] = y; n   if (siz[x] & 1) --oddcnt; n   if (siz[y] & 1) --oddcnt; n   siz[y] += siz[x]; n   fath[x] = y; n   if (siz[y] & 1) ++oddcnt; n } n inline void cancl(int x, int y) { n   fath[x] = x; n   if (siz[y] & 1) --oddcnt; n   siz[y] -= siz[x]; n   if (siz[x] & 1) ++oddcnt; n   if (siz[y] & 1) ++oddcnt; n } n int st[301000], ed[301000]; n int ans[301000]; n int h[301000], ltot; n void sol(int l, int r, int d, int u) { n   if (l > r) return; n   int memo = stop, mid = (l + r) >> 1; n   for (int i = l; i <= mid; ++i) { n     if (E[i].w <= d) merge(E[i].u, E[i].v); n   } n   int memo_ = stop; n   if (!oddcnt) { n     ans[mid] = d; n   } else { n     for (int i = d + 1; i <= u; ++i) { n       for (int j = st[i]; j <= ed[i]; ++j) { n         if (Ev[j].id <= mid) merge(Ev[j].u, Ev[j].v); n       } n       if (!oddcnt) { n         ans[mid] = i; n         break; n       } n     } n   } n   if (!ans[mid]) { n     while (stop != memo_) cancl(stkx[stop], stky[stop]), --stop; n     sol(mid + 1, r, d, u); n     while (stop != memo) cancl(stkx[stop], stky[stop]), --stop; n     return; n   } else { n     while (stop != memo_) cancl(stkx[stop], stky[stop]), --stop; n     sol(mid + 1, r, d, ans[mid]); n     while (stop != memo) cancl(stkx[stop], stky[stop]), --stop; n     for (int i = d + 1; i <= ans[mid]; ++i) { n       for (int j = st[i]; j <= ed[i]; ++j) { n         if (Ev[j].id < l) merge(Ev[j].u, Ev[j].v); n       } n     } n     sol(l, mid - 1, ans[mid], u); n     while (stop != memo) cancl(stkx[stop], stky[stop]), --stop; n     return; n   } n } n int main() { n   read(n), read(m); n   for (int i = 1; i <= n; ++i) siz[i] = 1, fath[i] = i; n   oddcnt = n; n   for (int i = 1; i <= m; ++i) { n     int u, v, w; n     read(u), read(v), read(w); n     h[i] = w; n     Ev[i] = Edge(u, v, w, i); n   } n   sort(h + 1, h + 1 + m); n   ltot = unique(h + 1, h + 1 + m) - h - 1; n   for (int i = 1; i <= m; ++i) n     Ev[i].w = lower_bound(h + 1, h + 1 + ltot, Ev[i].w) - h; n   memcpy(E, Ev, sizeof(Ev)); n   sort(Ev + 1, Ev + 1 + m, cmp); n   for (int i = 1; i <= m; ++i) n     if (Ev[i].w != Ev[i - 1].w) ed[Ev[i - 1].w] = i - 1, st[Ev[i].w] = i; n   ed[Ev[m].w] = m; n   sol(1, m, 1, ltot); n   h[0] = -1; n   for (int i = 1; i <= m; ++i) printf( %d n , h[ans[i]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long a, long long b, n                  long long mod = (long long)(1e9 + 7)) { n   if (!b) return 1; n   a %= mod; n   return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int DEBUG = 1; n const int mxn = 2e5 + 7; n int n, k; n int dsu[mxn], sz[mxn], mini[mxn], maxi[mxn]; n bool ac[mxn]; n string ans; n int a[mxn], b[mxn]; n void init() { n   for (int i = 0; i < mxn; i++) n     ac[i] = 0, dsu[i] = i, sz[i] = 1, mini[i] = i, maxi[i] = i; n } n int root(int i) { return dsu[i] = dsu[i] == i ? i : root(dsu[i]); } n void merge(int i, int j) { n   if (!(0 <= i && i <= n - 1) || !(0 <= j && j <= n - 1)) return; n   i = root(i); n   j = root(j); n   if (i == j) return; n   if (sz[i] < sz[j]) swap(i, j); n   dsu[j] = i; n   sz[i] += sz[j]; n   (mini[i]) = min((mini[i]), (mini[j])); n   (maxi[i]) = max((maxi[i]), (maxi[j])); n } n void activate(int i) { n   ac[i] = 1; n   if (i && ac[i - 1]) merge(i, i - 1); n   if ((0 <= i + 1 && i + 1 <= n - 1) && ac[i + 1]) merge(i, i + 1); n } n void solve() { n   scanf( %d , &n); n   scanf( %d , &k); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   init(); n   ans = string(n,    ); n   for (int i = 0; i < n; i++) b[--a[i]] = i; n   int t = 0; n   for (int i = n - 1; i >= 0; i--) n     if (!ac[b[i]]) { n       activate(b[i]); n       ans[b[i]] =  1  + t; n       int cnt = k, j = b[i]; n       while (cnt-- && j < n) { n         if (ac[j]) j = maxi[root(j)] + 1; n         if (j >= n) break; n         activate(j); n         ans[j++] =  1  + t; n       } n       cnt = k; n       j = b[i]; n       while (cnt-- && j >= 0) { n         if (ac[j]) j = mini[root(j)] - 1; n         if (j < 0) break; n         activate(j); n         ans[j--] =  1  + t; n       } n       t ^= 1; n     } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int Abs(int x) { return x > 0 ? x : -x; } n set<int> peo; n set<int>::iterator p1, p2; n bool vis[200050], boy[200050]; n char s[200050]; n int a[200050]; n struct node { n   int l, r; n   int val; n   node(int a = 0, int b = 0, int c = 0) : l(a), r(b), val(c) {} n   bool operator<(const node &x) const { n     if (val != x.val) return x.val > val; n     if (x.l != l) return x.l > l; n     return x.r > r; n   } n }; n set<node> myset; n set<node>::iterator it; n vector<node> ans; n void work() { n   while (myset.size()) { n     it = myset.begin(); n     node u = *it; n     myset.erase(it); n     if (vis[u.l] || vis[u.r]) continue; n     vis[u.l] = vis[u.r] = 1; n     ans.push_back(u); n     p1 = peo.find(u.l); n     if (p1 == peo.begin()) { n       peo.erase(p1); n       peo.erase(u.r); n       continue; n     } n     p2 = peo.find(u.r); n     p2++; n     if (p2 == peo.end()) { n       peo.erase(p1); n       peo.erase(u.r); n       continue; n     } n     p1--; n     peo.erase(u.l); n     peo.erase(u.r); n     u.l = *p1; n     u.r = *p2; n     if (boy[u.l] == boy[u.r]) continue; n     u.val = Abs(a[u.l] - a[u.r]); n     myset.insert(u); n   } n } n int n; n int main() { n   int i, j; n   while (cin >> n) { n     myset.clear(); n     peo.clear(); n     scanf( %s , s + 1); n     for (i = 1; i <= n; i++) scanf( %d , &a[i]); n     boy[1] = (s[1] ==  B ); n     for (i = 1; i <= n; i++) { n       peo.insert(i); n       boy[i + 1] = (s[i + 1] ==  B ); n       if (i < n && boy[i] != boy[i + 1]) n         myset.insert(node(i, i + 1, Abs(a[i] - a[i + 1]))); n     } n     memset(vis, 0, sizeof vis); n     ans.clear(); n     work(); n     cout << ans.size() << endl; n     for (i = 0; i < ans.size(); i++) printf( %d %d n , ans[i].l, ans[i].r); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n const long long MOD = 1e9 + 7; n const long long INF = 1e9 + 8; n const double pi = 3.14159265359; n long long binpow(long long a, long long b, long long m) { n   a %= m; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n long long inverse(long long x) { return binpow(x, MOD - 2, MOD); } n void solve() { n   long long n, x; n   cin >> n >> x; n   if (n <= 2) { n     cout << 1 << endl; n     return; n   } n   cout << (n - 3) / x + 2 << endl; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   long long t; n   cin >> t; n   while (t--) solve(); n   cerr <<  Time elapsed :   << 1.0 * clock() / CLOCKS_PER_SEC <<   sec  n  ; n } n


#include <bits/stdc++.h> n using namespace std; n long long num[100010]; n int main() { n   int n, q, req; n   while (~scanf( %d%d%d , &n, &q, &req)) { n     bool res[100010]; n     res[0] = true; n     res[n + 1] = true; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &num[i]); n       if (num[i] <= req) n         res[i] = true; n       else n         res[i] = false; n     } n     int qiu = 0; n     bool flag = true; n     for (int i = 1; i <= n; i++) { n       if (flag == true && res[i] == false) { n         qiu++; n         flag = false; n       } n       if (res[i] == true) flag = true; n     } n     for (int i = 0; i < q; i++) { n       int cur; n       scanf( %d , &cur); n       if (cur == 0) { n         cout << qiu << endl; n       } else { n         int a; n         long long b; n         scanf( %d%lld , &a, &b); n         num[a] += b; n         if (res[a] == true && num[a] > req) { n           res[a] = false; n           if (res[a - 1] == false && res[a + 1] == false) qiu--; n           if (res[a - 1] == true && res[a + 1] == true) qiu++; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n long long int t, n, m; n set<long long int> vis; n queue<long long int> q; n vector<long long int> temp, v[500005], g[500005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int i, j, k, x, y; n   cin >> n >> m; n   for (i = 1; i <= m; i++) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (i = 1; i <= n; i++) { n     sort(g[i].begin(), g[i].end()); n     vis.insert(i); n   } n   long long int comp = 0; n   for (i = 1; i <= n; i++) { n     if (vis.find(i) == vis.end()) n       continue; n     else n       x = i; n     vis.erase(x); n     q.push(x); n     comp++; n     while (!q.empty()) { n       x = q.front(); n       q.pop(); n       v[comp].push_back(x); n       temp.clear(); n       for (auto it : vis) { n         if (!binary_search(g[x].begin(), g[x].end(), it)) { n           temp.push_back(it); n           q.push(it); n         } n       } n       for (auto it : temp) vis.erase(it); n     } n   } n   cout << comp <<   n ; n   for (i = 1; i <= comp; i++) { n     cout << v[i].size() <<    ; n     for (auto it : v[i]) cout << it <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n const int N = (int)1e6 + 7, mod = 1000000007, M = 2e9, lvl = 21; n char s[N]; n int n, c[10], x[4] = {1, 6, 8, 9}; n vector<int> base, a; n void push(int x) { n   a.push_back(x / 100); n   x %= 100; n   a.push_back(x / 10); n   x %= 10; n   a.push_back(x); n } n bool div7() { n   if (a.size() <= 4) { n     int ret = a.back(); n     a.pop_back(); n     if (a.empty()) return ret % 7 == 0; n     ret += a.back() * 10; n     a.pop_back(); n     if (a.empty()) return ret % 7 == 0; n     ret += a.back() * 100; n     a.pop_back(); n     if (a.empty()) return ret % 7 == 0; n     ret += a.back() * 1000; n     a.pop_back(); n     if (a.empty()) return ret % 7 == 0; n   } n   int t = a.back() * 2, bg; n   a.pop_back(); n   bg = a.back(); n   a.pop_back(); n   bg += a.back() * 10; n   a.pop_back(); n   bg += a.back() * 100; n   a.pop_back(); n   push(bg - t); n   return div7(); n } n void print() { n   for (int i : base) printf( %d , i); n   for (int i = 0; i < 4; i++) printf( %d , x[i]); n   while (c[0]--) printf( 0 ); n   exit(0); n } n int main() { n   scanf( %s , s); n   n = strlen(s); n   for (int i = 0; i < n; i++) c[s[i] -  0 ]++; n   c[1]--; n   c[6]--; n   c[8]--; n   c[9]--; n   for (int i = 1; i <= 9; i++) { n     while (c[i]--) base.push_back(i); n   } n   do { n     a.clear(); n     for (int i : base) a.push_back(i); n     for (int i = 0; i < 4; i++) a.push_back(x[i]); n     if (div7()) print(); n     n++; n   } while (next_permutation(x, x + 4)); n   puts( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, num; n   scanf( %d , &n); n   int f = 0; n   int pre = 0; n   bool ans = 1; n   for (int i = 0; i < n; i++) { n     scanf( %d , &num); n     if ((f == 1 && num > pre) || (f == 2 && num >= pre)) ans = 0; n     if (num == pre) f = 1; n     if (num < pre) f = 2; n     pre = num; n   } n   if (ans) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     vector<int> a(n, 0); n     for (int i = 0; i < n; i++) cin >> a[i]; n     vector<int> prev(n, 0); n     prev[0] = 0; n     int m = -1; n     int s = 0; n     int l = 0; n     int r = 0; n     for (int i = 1; i < n - 1; i++) { n       if (a[i] > a[i + 1] && a[i - 1] < a[i]) { n         s++; n       } n       prev[i] = s; n     } n     for (int i = n - 1; i >= k - 1; i--) { n       if (prev[i - 1] - prev[i - k + 1] >= m) { n         m = prev[i - 1] - prev[i - k + 1]; n         l = i - k + 1; n       } n     } n     cout << m + 1 <<     << l + 1 << endl; n   } n } n


#include <bits/stdc++.h> n long long mod = 1e9 + 7, mod2 = 998244353; n const int N = int(3e5) + 999; n using namespace std; n long long fac[10000000]; n long long gcd(long long x, long long y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n long long fexp(long long a, long long b, long long m) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % m; n     b /= 2; n     a = a * a % m; n   } n   return ans; n } n long long inverse(long long a, long long p) { return fexp(a, p - 2, p); } n long long ncr(long long n, long long r, long long p) { n   if (r == 0) return 1; n   return (fac[n] * inverse(fac[n - r], p) % p * inverse(fac[r], p) % p) % p; n } n vector<long long> za(string s) { n   long long n = s.size(); n   vector<long long> z(n); n   long long x = 0, y = 0, p = 0; n   for (long long i = 1; i < n; i++) { n     z[i] = max(p, min(z[i - x], y - i + 1)); n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) { n       x = i; n       y = i + z[i]; n       z[i]++; n     } n   } n   return z; n } n void subset(long long a[], long long k) { n   for (int i = 1; i < pow(2, k); i++) { n     for (int j = 0; j < k; j++) { n       if (i & 1 << j) { n         cout << a[j] <<    ; n       } n     } n     cout << endl; n   } n } n vector<long long> pr(string s) { n   long long n = s.length(); n   vector<long long> pi(n); n   for (int i = 1; i < n; i++) { n     int j = pi[i - 1]; n     while (j > 0 && s[i] != s[j]) j = pi[j - 1]; n     if (s[i] == s[j]) j++; n     pi[i] = j; n   } n   return pi; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n, val; n   cin >> n; n   long long a[n], c1 = 0, c2 = 0, ans = 0, t = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     if (t % 2 == 0) n       c1++; n     else n       c2++; n     if (a[i] < 0) t++; n     if (t % 2 == 0) n       ans += c1; n     else n       ans += c2; n   } n   cout << n * (n + 1) / 2 - ans <<     << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void chkmax(T& x, U y) { n   if (x < y) x = y; n } n template <typename T, typename U> n inline void chkmin(T& x, U y) { n   if (y < x) x = y; n } n int main() { n   int h, m, s, A, B; n   scanf( %d%d%d%d%d , &h, &m, &s, &A, &B); n   double a = h % 12 + 1.0 * m / 60 + 1.0 * s / 3600; n   double b = 0.2 * m + 1.0 * s / 300; n   double c = 0.2 * s; n   for (int i = 0; i < 2; i++) { n     bool flg = 1; n     for (int x = A; x != B; x = x % 12 + 1) { n       if (x % 12 <= a && a <= x % 12 + 1) flg = 0; n       if (x % 12 <= b && b <= x % 12 + 1) flg = 0; n       if (x % 12 <= c && c <= x % 12 + 1) flg = 0; n     } n     if (flg) return puts( yes ), 0; n     swap(A, B); n   } n   puts( no ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n int main() { n  ios_base::sync_with_stdio(false); n  cin.tie(NULL); n   n  long long int T; n  cin>>T; n  while(T--) n  { n   string S; n   cin>>S; n    n   long long int A[100]; n   long long int x=0; n   long long int val=0; n   long long int N=S.length(); n    n   long long int first,mid,last; n   A[0]=0;A[1]=0;A[2]=0; n   string str=  ; n    n   for(long long int i=0;i<N;i++) n   { n       char c; n       c=S[i]; n       if(c== ? ) n       A[x]++; n       else  n       { n           str+=c; n           x++; n       } n        n   } n    n   n  // cout<<A[0]<<   <<A[1]<<   <<A[2]<<endl; n   if(str== () ) n   { n      val=1; n        n        n   } n   else if(str== )( ) n   { n       if((A[0]>=1)&&(A[2]>=1)) n       { n           val=1; n       } n   } n    n   if(val==0) n   cout<< NO <<endl; n   else if(val==1&&(N%2==0)) n   cout<< YES <<endl; n   else n   cout<< NO <<endl; n    n       n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n const int inf = 1000 * 1000 * 1000; n const int mod = 1000 * 1000 * 1000 + 7; n mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count()); n int n, s, d; n bitset<N> a[(1 << 20) + 2]; n bitset<N> b[(1 << 10) + 2]; n bitset<N> mas[33]; n bitset<N> A; n int cnt[(1 << 20) + 2]; n unordered_map<bitset<N>, int> mp; n int main() { n   cin >> n >> s >> d; n   for (int i = 1; i <= s; i++) { n     int c; n     scanf( %d , &c); n     mas[i] = 0; n     while (c--) { n       int a; n       scanf( %d , &a); n       mas[i][a] = 1; n     } n   } n   int x = min(s, 20); n   int y = s - x; n   cnt[0] = 0; n   for (int i = 1; i < (1 << 20); i++) { n     for (int j = 0; j < 20; j++) { n       if ((i >> j) & 1) { n         cnt[i]++; n       } n     } n   } n   b[0] = 0; n   mp[b[0]] = 0; n   for (int i = 1; i < (1 << x); i++) { n     int pos; n     for (int j = 0; j < x; j++) { n       if ((i >> j) & 1) { n         pos = j; n         break; n       } n     } n     b[i] = b[i ^ (1 << pos)] ^ mas[pos + 1]; n     if (mp.find(b[i]) == mp.end()) { n       mp[b[i]] = cnt[i]; n     } else { n       mp[b[i]] = min(mp[b[i]], cnt[i]); n     } n   } n   a[0] = 0; n   for (int i = 1; i < (1 << y); i++) { n     int pos; n     for (int j = 0; j < y; j++) { n       if ((i >> j) & 1) { n         pos = j; n         break; n       } n     } n     a[i] = a[i ^ (1 << pos)] ^ mas[pos + 1 + x]; n   } n   while (d--) { n     int c; n     scanf( %d , &c); n     A = 0; n     while (c--) { n       int a; n       scanf( %d , &a); n       A[a] = 1; n     } n     int answ = inf; n     for (int i = 0; i < (1 << y); i++) { n       if (mp.find(A ^ a[i]) != mp.end()) { n         answ = min(answ, cnt[i] + mp[A ^ a[i]]); n       } n     } n     if (answ == inf) answ = -1; n     printf( %d n , answ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, i, k = 0; n     cin >> n; n     int a[n]; n     map<int, int> m; n     set<int> s; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n       m[a[i]]++; n       k = max(k, m[a[i]]); n     } n     for (i = 0; i < n; i++) s.insert(a[i]); n     int mx = s.size(); n     int ans = max(min(mx - 1, k), min(mx, k - 1)); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, t; n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 0; i < n; i++) { n       cout << n - i <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n struct help { n   long int money; n   long int friends; n }; n void fusion_sort(struct help *p, long int n); n void bablesort(struct help *p, long int n); n int main() { n   long int n, m; n   scanf( %ld %ld , &n, &m); n   struct help *p = (struct help *)malloc(sizeof(struct help) * n); n   for (int i = 0; i < n; i++) scanf( %ld %ld , &p[i].money, &p[i].friends); n   if (n == 1) { n     printf( %ld , p[0].friends); n     return 0; n   } n   if (n > 1000) n     fusion_sort(p, n); n   else n     bablesort(p, n); n   unsigned long long max_friend = 0; n   unsigned long long cur_sum = 0; n   for (int b = 0, e = 0; e < n;) n     if (p[e].money - p[b].money >= m) { n       if (cur_sum > max_friend) { n         max_friend = cur_sum; n       } n       cur_sum -= p[b].friends; n       b++; n     } else { n       cur_sum += p[e].friends; n       e++; n     } n   if (cur_sum > max_friend) max_friend = cur_sum; n   printf( %lld , max_friend); n   return 0; n } n void fusion_sort(struct help *p, long int n) { n   if (n > 2) { n     fusion_sort(p, n / 2); n     fusion_sort((p + n / 2), n / 2 + n % 2); n   } n   struct help temp; n   for (int i = 0, j = n / 2; i < j && j < n; i++) n     if (p[i].money > p[j].money) { n       temp.friends = p[j].friends; n       temp.money = p[j].money; n       for (int k = j; k > i; k--) { n         p[k].friends = p[k - 1].friends; n         p[k].money = p[k - 1].money; n       } n       p[i].friends = temp.friends; n       p[i].money = temp.money; n       j++; n     } n } n void bablesort(struct help *p, long int n) { n   long int fr; n   long int mo; n   while (n > 1) { n     for (int i = 0; i < n - 1; i++) n       if (p[i].money > p[i + 1].money) { n         fr = p[i].friends; n         mo = p[i].money; n         p[i].friends = p[i + 1].friends; n         p[i].money = p[i + 1].money; n         p[i + 1].friends = fr; n         p[i + 1].money = mo; n       } n     n--; n   } n } n


#include <bits/stdc++.h> n using LL = long long; n const LL Inf = (LL)1e18; n const int N = 5000 + 5; n int s, e, n; n LL x[N], a[N], b[N], c[N], d[N]; n LL dp[2][N]; n inline LL lo(int p) { return d[p] - x[p]; } n inline LL ro(int p) { return x[p] + c[p]; } n inline LL li(int p) { return b[p] - x[p]; } n inline LL ri(int p) { return x[p] + a[p]; } n inline void _min(LL &a, LL b) { n   if (a > b) a = b; n } n LL work() { n   if (s > e) { n     for (int i = 1; i <= n; ++i) { n       x[i] = -x[i]; n       std::swap(a[i], b[i]); n       std::swap(c[i], d[i]); n     } n     for (int i = 1; i < n + 1 - i; ++i) { n       std::swap(x[i], x[n + 1 - i]); n       std::swap(a[i], a[n + 1 - i]); n       std::swap(b[i], b[n + 1 - i]); n       std::swap(c[i], c[n + 1 - i]); n       std::swap(d[i], d[n + 1 - i]); n     } n     s = n + 1 - s; n     e = n + 1 - e; n   } n   for (int i = 0; i < 2; ++i) { n     for (int j = 0; j < N; ++j) { n       dp[i][j] = Inf; n     } n   } n   int cur = 0, nex = 1; n   dp[cur][1] = li(1) + lo(1); n   for (int i = 2; i <= n; ++i) { n     for (int left = 1; left <= i - 1; ++left) { n       if (dp[cur][left] == Inf) continue; n       for (int q = 0; q < 2; ++q) { n         for (int w = 0; w < 2; ++w) { n           if (i == s && w == 0) continue; n           if (i == e && q == 1) continue; n           if (s < i && i < e) { n             if (left == 1 && q == 0) continue; n           } n           LL cost = 0; n           cost += q == 0 ? ro(i) : lo(i); n           cost += w == 0 ? ri(i) : li(i); n           int n_left = left; n           if (q == w) { n             if (q == 0) n               n_left--; n             else n               n_left++; n           } n           _min(dp[nex][n_left], dp[cur][left] + cost); n         } n       } n     } n     std::swap(cur, nex); n     std::fill(dp[nex], dp[nex] + N, Inf); n   } n   LL ret = dp[cur][0]; n   if (s < e) { n     ret -= li(s) + ro(e); n   } else { n     ret -= ri(s) + lo(e); n   } n   return ret; n } n int main() { n   std::ios::sync_with_stdio(false); n   std::cin >> n >> s >> e; n   for (int i = 1; i <= n; ++i) std::cin >> x[i]; n   for (int i = 1; i <= n; ++i) std::cin >> a[i]; n   for (int i = 1; i <= n; ++i) std::cin >> b[i]; n   for (int i = 1; i <= n; ++i) std::cin >> c[i]; n   for (int i = 1; i <= n; ++i) std::cin >> d[i]; n   std::cout << work() << std::endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > g; n bitset<(int)1e6> us; n bool isCyclic(int v, int p = -1) { n   us.set(v); n   for (int i = 0; i < g[v].size(); i++) { n     if (us.test(g[v][i]) && g[v][i] != p) return 0; n     if (us.test(g[v][i])) continue; n     bool cures = isCyclic(g[v][i], v); n     if (!cures) return 0; n   } n   return 1; n } n int main() { n   int n, m; n   cin >> n >> m; n   g.resize(n + 1); n   for (int i = 0; i < m; i++) { n     int v, to; n     cin >> v >> to; n     g[to].push_back(v); n     g[v].push_back(to); n   } n   int sum = 0; n   for (int i = 1; i <= n; i++) { n     if (us.test(i)) continue; n     sum += isCyclic(i); n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n long long num[5005], sum[5005], dp[5005][5005]; n int main() { n   int i, j; n   while (scanf( %d%d%d , &n, &m, &k) != EOF) { n     for (i = 0; i < n; i++) { n       scanf( %I64d , &num[i]); n     } n     memset(sum, 0, sizeof(sum)); n     for (i = 0; i <= m - 1; i++) { n       sum[0] += num[i]; n     } n     for (i = 1; i + m <= n; i++) { n       sum[i] = sum[i - 1]; n       sum[i] -= num[i - 1]; n       sum[i] += num[i + m - 1]; n     } n     memset(dp, 0, sizeof(dp)); n     for (j = 1; j <= k; j++) { n       for (i = n - m * j; i >= 0; i--) { n         dp[i][j] = max(dp[i + 1][j], dp[i + m][j - 1] + sum[i]); n       } n     } n     long long res = 0; n     for (i = 0; i <= n - m * k; i++) { n       res = max(dp[i][k], res); n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 20001; n const int MAX_LOG = 20; n using ll = long long; n using pii = pair<int, int>; n using pil = pair<int, ll>; n using pll = pair<ll, ll>; n const double delta = 1e-6; n const long long mod = 1e9 + 7; n const int magic_const = 1e9; n const ll INF = 2e18 + 2; n const ll BIG_DT = 1e12; n mt19937 gen(54323244234); n inline ll modoper(ll x) { return ((x % mod) + mod) % mod; } n ll gcd(ll x, ll y) { return y == 0 ? x : gcd(y, x % y); } n inline ll get_rand(ll x) { return (gen() % x + x) % x; } n template <typename T> n inline void sort(vector<T>& x) { n   sort(x.begin(), x.end()); n } n template <typename T> n inline void reverse(vector<T>& x) { n   reverse(x.begin(), x.end()); n } n template <typename T> n inline void set_max(T& x, T y) { n   x = max(x, y); n } n template <typename T> n inline void set_min(T& x, T y) { n   x = min(x, y); n } n ll bin_pow(ll x, ll pow) { n   if (pow == 0) return 1; n   if ((pow & 2) == 1) { n     return (bin_pow(x, pow - 1) * x) % mod; n   } else { n     auto tmp = bin_pow(x, pow / 2); n     return (tmp * tmp) % mod; n   } n } n struct fenwick_tree { n   vector<int> a; n   fenwick_tree(int n) : a(n + 2) {} n   void add(int pos, int delta) { n     for (pos++; pos < a.size(); pos += pos & -pos) a[pos] += delta; n   } n   int get_sum(int pos) { n     int sum = 0; n     for (pos++; pos > 0; pos -= pos & -pos) sum += a[pos]; n     return sum; n   } n }; n vector<double> ys(MAX_N); n vector<vector<double>> matrix(3, vector<double>(3)); n double find_det() { n   double answer = 0.0; n   for (int i = 0; i < 3; i++) { n     double pr = 1.0; n     for (int j = 0; j < 3; j++) { n       pr *= matrix[j][(i + j) % 3]; n     } n     answer += pr; n   } n   for (int i = 0; i < 3; i++) { n     double pr = 1.0; n     for (int j = 0; j < 3; j++) { n       pr *= matrix[j][(i - j + 3) % 3]; n     } n     answer -= pr; n   } n   return answer; n } n void set_point_in_vector(vector<double>& a, int x) { n   a[0] = x * x; n   a[1] = x; n   a[2] = 1.0; n } n void nullify(int x1, int x2, int x3) { n   set_point_in_vector(matrix[0], x1); n   set_point_in_vector(matrix[1], x2); n   set_point_in_vector(matrix[2], x3); n } n void set_st_r(int x1, int x2, int x3, int column) { n   nullify(x1, x2, x3); n   matrix[0][column] = ys[x1]; n   matrix[1][column] = ys[x2]; n   matrix[2][column] = ys[x3]; n } n pair<double, pair<double, double>> find_eq(int x1, int x2, int x3) { n   nullify(x1, x2, x3); n   double det = find_det(); n   set_st_r(x1, x2, x3, 0); n   double det_1 = find_det(); n   set_st_r(x1, x2, x3, 1); n   double det_2 = find_det(); n   set_st_r(x1, x2, x3, 2); n   double det_3 = find_det(); n   return {det_1 / det, {det_2 / det, det_3 / det}}; n } n void solve() { n   int n, d; n   cin >> n >> d; n   vector<int> a(n, 1); n   int sum_now = (n * (n - 1)) / 2 - d; n   for (int l = 1, r = a.size() - 1; l < r; r--) { n     while (l < r && (sum_now - r + l < 0 || a[l] == a[l - 1] * 2)) { n       l++; n     } n     if (l >= r) break; n     sum_now -= r; n     sum_now += l; n     a[l]++; n     a[r]--; n   } n   if (sum_now != 0) { n     cout <<  NO n ; n     return; n   } n   cout <<  YES n ; n   a.insert(a.begin(), 0); n   vector<int> pref_sums = a; n   for (int i = 1; i < pref_sums.size(); i++) { n     pref_sums[i] += pref_sums[i - 1]; n   } n   for (int i = 2; i < pref_sums.size(); i++) { n     for (int k = 0; k < a[i]; k++) { n       cout << pref_sums[i - 2] + 1 + k / 2 <<    ; n     } n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   srand(23321); n   int t = 1; n   cin >> t; n   for (int i = 0; i < t; i++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const int N = 200005; n int vis[N], tab[N], i, a, b, sum, n, m, vis0[N], vis1[N]; n vector<int> graf0[N], graf1[N]; n string s; n bool cyc(int w) { n   vis[w] = 1; n   for (auto& w2 : graf0[w]) n     if (vis[w2] == 1) n       return true; n     else if (vis[w2] == 0 && cyc(w2)) n       return true; n   vis[w] = 2; n   return false; n } n void go(int w, vector<int> graf[N], int* vis) { n   tab[w] = 1; n   vis[w] = 1; n   for (auto& w2 : graf[w]) n     if (!vis[w2]) go(w2, graf, vis); n } n int main() { n   scanf( %d %d , &n, &m); n   while (m--) { n     scanf( %d %d , &a, &b); n     graf0[a].push_back(b); n     graf1[b].push_back(a); n   } n   for (i = 1; i <= n; i++) n     if (vis[i] == 0 && cyc(i)) { n       printf( -1 n ); n       return 0; n     } n   sum = 0; n   for (i = 1; i <= n; i++) n     if (tab[i] == 0) { n       go(i, graf0, vis0); n       go(i, graf1, vis1); n       s +=  A ; n       sum++; n     } else { n       s +=  E ; n       go(i, graf0, vis0); n       go(i, graf1, vis1); n     } n   printf( %d n%s n , sum, s.c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long; n using ld = long double; n using namespace std; n const int MAXN = 2000228; n const ll INF = (1ll << 50); n set<pair<ll, ll>> s; n void ins(ll x, ll y) {} n int main() { n   ios_base::sync_with_stdio(false); n   cout.setf(ios::fixed); n   cout.precision(20); n   cout.tie(nullptr); n   cin.tie(nullptr); n   int n; n   cin >> n; n   ll p = INF; n   s.emplace(p - 1, 0); n   for (int i = 0; i < n; ++i) { n     ll x; n     cin >> x; n     if (x >= p) { n       continue; n     } n     p = x; n     vector<pair<ll, ll>> rmed; n     while (!s.empty()) { n       auto val = *s.rbegin(); n       if (val.first < x - 1) { n         break; n       } n       s.erase(val); n       rmed.push_back(val); n     } n     pair<ll, ll> tmp{x, -1}; n     s.emplace(tmp); n     ll last = 0; n     for (auto& val : rmed) { n       ll dv = val.first / x; n       last = max(last, val.second + x * (dv - 1) * i); n       ll md = val.first % x; n       ll nvl = x * dv * i + val.second; n       auto it = s.lower_bound(pair<ll, ll>{md, nvl}); n       if (it->second >= nvl) { n         continue; n       } n       auto lit = it; n       while (lit != s.begin()) { n         auto nit = lit; n         --nit; n         if (nit->second > nvl) { n           break; n         } n         lit = nit; n       } n       s.erase(lit, it); n       s.emplace(md, nvl); n     } n     while (!s.empty() && s.rbegin()->second <= last) { n       auto it = s.end(); n       --it; n       s.erase(it); n     } n     s.emplace(x - 1, last); n   } n   ll ans = 0; n   for (auto& val : s) { n     ans = max(ans, val.first * n + val.second); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long ll; n typedef long double ld; n typedef string str; n  n #define pb push_back n #define ppp pop_back n #define pii pair<int,int> n #define fi first n #define se second n #define stie std::tie n #define vec vector n #define forn(i, l, r) for (int i=l; i<=r; i++) n #define forb(i, r, l) for (int i=r; i>=l; i--) n #define emp empty n #define beg begin n #define ins insert n #define cle clear n #define era erase n #define que queue n #define pque priority_queue n #define mset multiset n #define deq deque n #define sta stack n #define con const n #define rsz resize n #define ass assign n #define lowb lower_bound n #define uppb upper_bound n  n template<class T> void mini(T& a, T b) { a = min(a, b); } n template<class T> void maxi(T& a, T b) { a = max(a, b); } n template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> a) { return out << a.first <<     << a.second; } n template<class T> ostream& operator<<(ostream& out, vector<T> a) { for (auto& i : a) out << i <<    ; return out; } n template<class T> ostream& operator<<(ostream& out, set<T> a) { for (auto& i : a) out << i <<    ; return out; } n template<class T> ostream& operator<<(ostream& out, multiset<T> a) { for (auto& i : a) out << i <<    ; return out; } n template<class T1, class T2> ostream& operator<<(ostream& out, map<T1, T2> a) { for (auto& i : a) out << i <<   n ; return out; } n template<class T1, class T2> ostream& operator<<(ostream& out, unordered_map<T1, T2> a) { for (auto& i : a) out << i <<   n ; return out; } n template<class T> ostream& operator<<(ostream& out, queue<T> a) { while (!a.empty()) { out << a.front() <<    ; a.pop(); } return out; } n template<class T> ostream& operator<<(ostream& out, deque<T> a) { while (!a.empty()) { out << a.front() <<    ; a.pop_front(); } return out; } n template<class T> ostream& operator<<(ostream& out, priority_queue<T> a) { while (!a.empty()) { out << a.top() <<   n ; a.pop(); } return out; } n template<class T> void out(T a) { cout << a << endl; } n template<class T1, class T2> void out(T1 a, T2 b) { cout << a <<     << b << endl; } n template<class T1, class T2, class T3> void out(T1 a, T2 b, T3 c) { cout << a <<     << b <<     << c << endl; } n template<class T1, class T2, class T3, class T4> void out(T1 a, T2 b, T3 c, T4 d) { cout << a <<     << b <<     << c <<     << d << endl; } n template<class T> void out(vector<vector<T>> a) { for (vector<T> i : a) out(i); } n template<class T> void out_(T a[], int l, int r) { for (int i = l; i <= r; i++) cout << a[i] <<    ; cout << endl; } n void out() { cout <<  OK  << endl; } n template<class T> void sort(vector<T>& a) { sort(a.begin(), a.end()); } n template<class T> void uniq(vector<T>& a) { sort(a); a.erase(unique(a.begin(), a.end()), a.end()); } n template<class T> vector<T> set_int(vector<T> a, vector<T> b) { sort(a); sort(b); vector<T> res; set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res)); return res; } n clock_t start_time; void start_timer() { start_time = clock(); } n double get_time() { return (double)(clock() - start_time) / CLOCKS_PER_SEC; } n #if __SIZEOF_INT128__ >= 16 n typedef __int128 LL; n istream& operator>>(istream& in, __int128& a) { int64_t b; cin >> b; a = b; return in; } n ostream& operator<<(ostream& out, const __int128 a) { n  unsigned __int128 b = a < 0 ? -a : a; char buf[128]; char* c = end(buf); do { --c; *c =  0123456789 [b % 10]; b /= 10; } while (b); n  if (a < 0) { --c; *c =  - ; } int len = end(buf) - c; out.rdbuf()->sputn(c, len); return out; n } n #endif n  n con int N = 1e5 + 5; n con int K = 25; n  n int a[N]; n vec<int> g[N]; n int dp[N][2 * K]; n bool ans[N]; n int k; n  n void dfs(int v = 1, int p = 0) { n  dp[v][0] = a[v]; n  for (int u : g[v]) { n   if (u == p) continue; n   dfs(u, v); n   forn(i, 1, k-1) { n    dp[v][i] ^= dp[u][i-1]; n   } n   dp[v][0] ^= dp[u][k-1]; n  } n } n  n void dfs2(int v = 1, int p = 0, vec<int> cur = vec<int>(2 * K, 0)) { n  forn(i, 0, k-1) { n   cur[i] ^= dp[v][i]; n  } n  int odd = 0; n  forn(i, k/2, k-1) { n   odd ^= cur[i]; n  } n  ans[v] = odd; n  for (int u : g[v]) { n   if (u == p) continue; n   vec<int> nxt = cur; n   forn(i, 1, k-1) { n    nxt[i] ^= dp[u][i-1]; n   } n   nxt[0] ^= dp[u][k-1]; n   int last = nxt[k-1]; n   forb(i, k-1, 1) { n    nxt[i] = nxt[i-1]; n   } n   nxt[0] = last; n   dfs2(u, v, nxt); n  } n } n  n void Solve() { n  int n; n  cin >> n >> k; n  k *= 2; n  forn(i, 1, n-1) { n   int u, v; n   cin >> u >> v; n   g[u].pb(v); n   g[v].pb(u); n  } n  forn(i, 1, n) cin >> a[i]; n  dfs(); n  dfs2(); n  forn(i, 1, n) { n   cout << ans[i] <<    ; n  } n  cout <<   n ; n } n  n int main() { n  ios_base::sync_with_stdio(0); cin.tie(0); n #ifdef LOCAL n  freopen( input.txt ,  r , stdin); n #endif n  start_timer(); n  Solve(); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t-- > 0) { n     int n; n     cin >> n; n     vector<vector<int>> a; n     int b[n + 1]; n     for (int i = 0; i <= n; i++) b[i] = 0; n     int count = 0; n     int s = 0; n     for (int i = 0; i < n; i++) { n       vector<int> x; n       int k; n       cin >> k; n       int c = 0; n       for (int j = 0; j < k; j++) { n         int q; n         cin >> q; n         x.push_back(q); n         if (c == 0) { n           if (b[q] == 0) { n             b[q]++; n             count++; n             c = 1; n           } n         } n       } n       if (c == 0) s = i; n       a.push_back(x); n     } n     if (count == n) { n       cout <<  OPTIMAL  << endl; n     } else { n       s++; n       int f = 0; n       for (int i = 0; i < n; i++) { n         if (b[i + 1] == 0) { n           f = i; n         } n       } n       f++; n       cout <<  IMPROVE  << endl; n       cout << s <<     << f << endl; n     } n   } n   return 0; n } n


//Hare Krishna n //author: Dipjoy Basak n //dip_10 n  n #include <bits/stdc++.h> n using namespace std; n  n #define endl   n  n #define ll long long n #define int ll n #define ld long double n #define pb push_back n #define mp make_pair n #define ff first n #define ss ssecond n #define vi vector<int> n #define pi pair<int, int> n #define vpi vector<pair<int, int>> n #define rep(i, l, r) for (int i = l; i <= r; i++) n #define rrep(i, r, l) for (int i = r; i >= l; i--) n #ifndef ONLINE_JUDGE n #define debug(x)         n     cerr << #x <<    ;   n     _print(x);           n     cerr << endl; n #else n #define debug(x) ; n #endif n #define maxn 1000005 n #define INF INT_MAX n const int mod = 1000000007; n  n void _print(int t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(ld t) { cerr << t; } n void _print(double t) { cerr << t; } n  n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) n { n     cerr <<  { ; n     _print(p.ff); n     cerr <<  , ; n     _print(p.ss); n     cerr <<  } ; n } n template <class T> n void _print(vector<T> v) n { n     cerr <<  [  ; n     for (T i : v) n     { n         _print(i); n         cerr <<    ; n     } n     cerr <<  ] ; n } n template <class T> n void _print(set<T> v) n { n     cerr <<  [  ; n     for (T i : v) n     { n         _print(i); n         cerr <<    ; n     } n     cerr <<  ] ; n } n template <class T> n void _print(multiset<T> v) n { n     cerr <<  [  ; n     for (T i : v) n     { n         _print(i); n         cerr <<    ; n     } n     cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) n { n     cerr <<  [  ; n     for (auto i : v) n     { n         _print(i); n         cerr <<    ; n     } n     cerr <<  ] ; n } n  n //--------------------------------------------------------------------------------------------------------------------------------------- n  n void solve() n { n     int n; n     cin >> n; n     int a[n]; n     int idx = -1, mn = INT_MAX; n     rep(i, 0, n - 1) n     { n         cin >> a[i]; n         if (a[i] < mn) n         { n             mn = a[i]; n             idx = i; n         } n     } n     cout << n - 1 << endl; n     int cnt = 1; n     rep(i, idx + 1, n - 1) n     { n         cout << idx + 1 <<     << i + 1 <<     << mn <<     << mn + cnt << endl; n         cnt++; n     } n     cnt = 1; n     rrep(i, idx - 1, 0) n     { n         cout << idx + 1 <<     << i + 1 <<     << mn <<     << mn + cnt << endl; n         cnt++; n     } n } n int32_t main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n     cout << setprecision(15) << fixed; n     int t; n     t = 1; n     cin >> t; n     while (t--) n     { n         solve(); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dp[15][15]; n int n, t, i, j, ans = 0; n void work() { n   double t; n   dp[1][1] += 1; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= i; j++) { n       if (dp[i][j] > 1) { n         t = dp[i][j] - 1; n         dp[i][j] = 1; n         dp[i + 1][j] += t / 2; n         dp[i + 1][j + 1] += t / 2; n       } n     } n   } n } n int main() { n   scanf( %d%d , &n, &t); n   while (t--) { n     work(); n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= i; j++) { n       if (abs(1 - dp[i][j]) < 0.0000001) { n         ans++; n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n const double pi = 3.14159265358979323846; n using namespace std; n long long fastexp(long long a, long long b) { n   long long ans = 1; n   long long temp = a; n   while (b > 0) { n     if (b & 1) ans *= (temp); n     temp *= temp; n     b >>= 1; n   } n   return ans; n } n long long GCD(long long a, long long b) { n   if (a == 0) return b; n   return GCD(b % a, a); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   long long* A = new long long[n]; n   for (long long i = 0; i < n; i++) cin >> A[i]; n   long long m; n   cin >> m; n   long long* B = new long long[m]; n   for (long long i = 0; i < m; i++) cin >> B[i]; n   long long count = 0; n   long long i = 0; n   long long j = 0; n   bool f = true; n   while (i < n && j < m) { n     long long cum1 = A[i++]; n     long long cum2 = B[j++]; n     bool flag = false; n     while (i < n && j < m) { n       if (cum1 == cum2) { n         flag = true; n         break; n       } n       if (cum1 > cum2) { n         cum2 += B[j]; n         j++; n       } else { n         cum1 += A[i]; n         i++; n       } n     } n     if (i == n) { n       while (j < m) cum2 += B[j++]; n     } n     if (j == m) { n       while (i < n) cum1 += A[i++]; n     } n     if (cum1 == cum2) flag = true; n     if (!flag) { n       f = false; n       break; n     } n     count++; n   } n   if (!f) n     cout <<  -1  n          <<   n ; n   else n     cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long int x = 0; n   for (int i = 2; i <= n; ++i) { n     if (i & 1) n       x = (x * 3 - 3) % 1000000007; n     else n       x = (x * 3 + 3) % 1000000007; n   } n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n char s[500010]; n int c[30][500010]; n int then(int x) { return x & -x; } n void in(int o, int x, int k) { n   while (x <= N) { n     c[o][x] += k; n     x += then(x); n   } n } n int out(int o, int x) { n   int re = 0; n   while (x > 0) { n     re += c[o][x]; n     x -= then(x); n   } n   return re; n } n int main() { n   int Q; n   scanf( %s%d , s + 1, &Q); n   N = strlen(s + 1); n   for (int i = 1; i <= N; i++) in(s[i] -  a , i, 1); n   for (int i = 1; i <= Q; i++) { n     int o; n     scanf( %d , &o); n     if (o == 1) { n       int k; n       char m; n       scanf( %d %c , &k, &m); n       in(s[k] -  a , k, -1); n       in(m -  a , k, 1); n       s[k] = m; n     } else { n       int x, y; n       scanf( %d%d , &x, &y); n       int give = 0; n       for (int i = 0; i <= 25; i++) n         if (out(i, y) - out(i, x - 1)) give++; n       printf( %d n , give); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s =   ; n   vector<int> fib; n   fib.push_back(1); n   fib.push_back(1); n   for (int i = 2; i < 17; ++i) { n     fib.push_back(fib[i - 1] + fib[i - 2]); n   } n   for (int i = 1; i <= n; ++i) { n     if (binary_search(fib.begin(), fib.end(), i) == 1) { n       s.append(1u,  O ); n     } else { n       s.append(1u,  o ); n     } n   } n   cout << s <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   int l = b.size(); n   int st = a.size() - 1; n   int ar[l + 3]; n   memset(ar, -1, sizeof ar); n   ar[l] = st + 1; n   for (int i = l - 1; i >= 0; i--) { n     while (st >= 0 && a[st] != b[i]) { n       st--; n     } n     if (st >= 0) { n       ar[i] = st; n       st--; n     } else n       break; n   } n   int ans = 0; n   int pre = 0; n   for (int i = 0; i < a.size(); i++) { n     int la = ar[pre]; n     if (la != -1) ans = max(ans, la - i); n     if (pre < l && a[i] == b[pre]) { n       pre++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 60 + 10, M = (1 << 16) + 10, MOD = 1e9 + 7, n                     INF = 2e9 + 10; n const long double PI = acos(-1); n int n, a[N], in[N], fac[N], msk[N], all; n long long int dp[M][N], ans = 1; n vector<int> adj[N], S, T; n bool mrk[N], ad[N][N]; n long long int Power(long long int x, long long int y) { n   if (y == 0) { n     return 1ll; n   } n   return ((y % 2 ? x : 1) * Power((x * x) % MOD, y / 2)) % MOD; n } n void Dfs(int u) { n   mrk[u] = 1; n   if (in[u]) n     T.push_back(u); n   else n     S.push_back(u); n   for (int v : adj[u]) { n     if (!mrk[v]) { n       Dfs(v); n     } n   } n   return; n } n void Solve() { n   memset(dp, 0, sizeof(dp)); n   int t = T.size(), s = S.size(); n   if (t < 2) return; n   for (int i = 0; i < t; i++) { n     msk[i] = 0; n     for (int j = 0; j < s; j++) { n       if ((a[T[i]] % a[S[j]]) == 0) { n         msk[i] |= (1 << j); n       } n     } n     dp[msk[i]][1]++; n   } n   for (int k = 1; k < t; k++) { n     for (int mask = 1; mask < (1 << s); mask++) { n       int cnt = 0; n       for (int i = 0; i < t; i++) { n         if ((msk[i] | mask) == mask) { n           cnt++; n         } else if (0 < (msk[i] & mask)) { n           dp[mask | msk[i]][k + 1] += dp[mask][k]; n           dp[mask | msk[i]][k + 1] %= MOD; n         } n       } n       if (k < cnt) { n         dp[mask][k + 1] += (cnt - k) * dp[mask][k]; n         dp[mask][k + 1] %= MOD; n       } n     } n   } n   ans = (ans * dp[(1 << s) - 1][t]) % MOD; n   ans = (ans * Power(fac[t - 1], MOD - 2)) % MOD; n   all += (t - 1); n   return; n } n void Dfs_All() { n   for (int i = 0; i < n; i++) { n     if (!mrk[i]) { n       Dfs(i); n       Solve(); n       S.clear(); n       T.clear(); n     } n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   fac[0] = 1; n   for (int i = 1; i < N; i++) { n     fac[i] = (fac[i - 1] * i) % MOD; n   } n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     for (int j = 0; j < i; j++) { n       int u = i, v = j; n       if (a[v] < a[u]) swap(u, v); n       if ((a[v] % a[u]) == 0) { n         adj[u].push_back(v); n         adj[v].push_back(u); n         in[v]++; n       } n     } n   } n   Dfs_All(); n   ans = (ans * fac[all]) % MOD; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int MAXN = 3e6 + 9; n long long n, k; n long long two(long long x) { n   long long base = 1, ans = 0; n   while (base * x <= n) { n     long long f = min(n, base * (x + 1) - 1); n     ans += f - base * x + 1; n     base *= 2; n   } n   return ans; n } n long long one(long long x) { n   if (x % 2 == 0) return two(x + 1) + two(x); n   return two(x); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int t = 1; n   while (t--) { n     cin >> n >> k; n     long long N = n, ans = 1; n     while (N) { n       for (long long i = 0; i <= 10; i++) { n         if (i >= N) break; n         if (one(N - i) >= k) ans = max(ans, N - i); n       } n       N /= 2; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isused[26]; n string s, t; n vector<pair<int, int> > v; n int abs(int num) { n   if (num < 0) n     return -num; n   else n     return num; n } n int main(void) { n   cin >> s >> t; n   int slen = s.size(); n   for (int i = 0; i < slen; i++) isused[(s[i] -  a )] = true; n   int tlen = t.size(); n   for (int i = 0; i < tlen; i++) { n     if (isused[(t[i] -  a )] == false) { n       cout << -1 << endl; n       return 0; n     } n   } n   for (int i = 0; i < tlen;) { n     pair<int, int> temp = make_pair(-1, -1); n     for (int j = 0; j < slen; j++) { n       if (t[i] == s[j]) { n         int k1 = 0; n         while (i + k1 + 1 < tlen && j + k1 + 1 < slen && n                t[i + k1 + 1] == s[j + k1 + 1]) n           k1++; n         int k2 = 0; n         while (i + k2 + 1 < tlen && j - k2 - 1 >= 0 && n                t[i + k2 + 1] == s[j - k2 - 1]) n           k2++; n         if (k1 >= k2) { n           if (temp.first == -1) n             temp = make_pair(j, j + k1); n           else if (abs(temp.second - temp.first) < k1) n             temp = make_pair(j, j + k1); n         } else { n           if (temp.first == -1) n             temp = make_pair(j, j - k2); n           else if (abs(temp.first - temp.second) < k2) n             temp = make_pair(j, j - k2); n         } n       } n     } n     if (temp.first <= temp.second) n       i += (temp.second - temp.first) + 1; n     else n       i += (temp.first - temp.second) + 1; n     v.push_back(make_pair(temp.first + 1, temp.second + 1)); n   } n   cout << v.size() << endl; n   for (int i = 0; i < v.size(); i++) n     cout << v[i].first <<     << v[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, totx, tot, hangx[2005], liex[2005]; n double ans; n char s[2005]; n long long func(int n, int m, int a[]) { n   long long ret = 0; n   for (int i = 1; i <= n; i++) { n     long long sum = 0, cnt = m - a[i]; n     for (int j = 1; j <= n; j++) { n       if (a[j]) n         sum += (m - 1) * abs(i - j); n       else n         sum += m * abs(i - j); n     } n     if (a[i]) n       ret += (m - 1) * sum; n     else n       ret += m * sum; n     if (a[i]) { n       int l = i - 1, r = i + 1; n       while (a[l + 1] < a[l]) { n         cnt += m - a[l]; n         l--; n       } n       while (a[r - 1] < a[r]) { n         cnt += m - a[r]; n         r++; n       } n       ret += 4 * cnt * (a[i] - 1); n     } n   } n   return ret; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> s; n     for (int j = 1; j <= m; j++) { n       if (s[j - 1] ==  X ) { n         hangx[i] = j; n         liex[j] = i; n         totx++; n       } n     } n   } n   tot = n * m - totx; n   ans = (func(n, m, hangx) + func(m, n, liex)) * 1.0 / tot / tot; n   cout << fixed << setprecision(12) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a[1000005], ans = 0; n inline long long read() { n   long long red = 0, f_f = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) f_f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) red = red * 10 + ch -  0 , ch = getchar(); n   return red * f_f; n } n signed main() { n   n = (1ll << read()), m = read(); n   for (long long i = 0; i < n; i++) a[i] = read(), ans += a[i]; n   printf( %.6lf n , 1.0 * ans / n); n   while (m--) { n     long long x = read(), y = read(); n     ans -= a[x]; n     a[x] = y; n     ans += a[x]; n     printf( %.6lf n , 1.0 * ans / n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false), cin.tie(nullptr); n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   int res = 0; n   for (int i = 0; i < k; i++) { n     int x = 0, y = 0; n     for (int j = i; j < n; j += k) { n       if (a[j] == 1) n         x++; n       else n         y++; n     } n     res += min(x, y); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n char s[100011]; n pair<int, int> dp[100011]; n int cnt[100011], mx[100011][2]; n int main() { n   int n, m; n   cin >> n >> s + 1 >> m; n   for (int i = 1; i <= n; i++) { n     if (s[i] !=  b ) mx[i][0] = mx[i - 1][1] + 1; n     if (s[i] !=  a ) mx[i][1] = mx[i - 1][0] + 1; n     cnt[i] = cnt[i - 1] + (s[i] ==  ? ); n     dp[i] = dp[i - 1]; n     if (mx[i][!(m & 1)] >= m) { n       if (dp[i - m].first + 1 == dp[i].first) { n         smin(dp[i].second, dp[i - m].second + cnt[i] - cnt[i - m]); n       } n       if (dp[i - m].first + 1 > dp[i].first) { n         dp[i].first = dp[i - m].first + 1; n         dp[i].second = dp[i - m].second + cnt[i] - cnt[i - m]; n       } n     } n   } n   cout << dp[n].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-10; n using namespace std; n using ll = long long; n using ul = unsigned long long; n using PII = pair<int, int>; n const int NN = 1011101; n const int NZ = 511100; n const int MM = 151; n const int need = (1 << 30) - 1; n int n, m, s, x, i, j, t, a, b, k, c, r, col[NN]; n int l; n ll in[NN]; n int comp[NN]; n PII gr[NN]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &a, &b); n     a--, b--; n     gr[i + i] = {a, b}; n     gr[i + i + 1] = {b, a}; n   } n   set<int> sv; n   for (int i = 0; i < n; i++) sv.insert(i); n   m += m; n   sort(gr, gr + m); n   vector<vector<int>> answww; n   while (!sv.empty()) { n     int ff = *sv.begin(); n     sv.erase(ff); n     queue<int> qq; n     qq.push(ff); n     vector<int> _comp; n     while (!qq.empty()) { n       int v = qq.front(); n       qq.pop(); n       _comp.push_back(v); n       vector<int> rv; n       for (auto it : sv) { n         int vv = it; n         if (!binary_search(gr, gr + m, make_pair(v, it))) { n           qq.push(vv); n           rv.push_back(vv); n         } n       } n       for (int i = 0; i < rv.size(); i++) { n         sv.erase(rv[i]); n       } n     } n     answww.push_back(_comp); n   } n   printf( %d n , answww.size()); n   for (int i = 0; i < answww.size(); i++) { n     printf( %d  , answww[i].size()); n     for (int j = 0; j < answww[i].size(); j++) { n       printf(  %d , answww[i][j] + 1); n     } n     puts(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (b == 0) { n     return a; n   } n   gcd(b, a % b); n } n int n, m, a, b, t; n bool eo = 0; n int main() { n   cin >> n >> m; n   n++; n   m++; n   for (int i = 1; i <= n; i++) { n     if (i == 1) n       cin >> a; n     else n       cin >> t; n   } n   for (int i = 1; i <= m; i++) { n     if (i == 1) n       cin >> b; n     else n       cin >> t; n   } n   if (a * b < 0) eo = 1; n   if (n > m) { n     if (eo) cout <<  - ; n     cout <<  Infinity  << endl; n   } n   if (n < m) { n     cout <<  0/1 ; n   } n   if (n == m) { n     a = abs(a), b = abs(b); n     int t = gcd(a, b); n     if (eo) cout <<  - ; n     a /= t, b /= t; n     cout << a <<  /  << b; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool test_cases; n void init(bool k) { n   test_cases = k; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n struct test_cases { n   void prepare_left(long long *great_left, long long n, long long *a) { n     stack<long long> st; n     great_left[0] = -1; n     st.push(a[0]); n     for (long long i = 1; i < n; i++) { n       while (!st.empty() and st.top() < a[i]) { n         st.pop(); n       } n       if (st.empty()) { n         great_left[i] = -1; n       } else { n         great_left[i] = st.top(); n       } n       st.push(a[i]); n     } n   } n   void prepare_right(long long *great_right, long long n, long long *a) { n     stack<long long> st; n     great_right[n - 1] = -1; n     st.push(a[n - 1]); n     for (long long i = n - 2; i >= 0; i--) { n       while (!st.empty() and st.top() < a[i]) { n         st.pop(); n       } n       if (st.empty()) { n         great_right[i] = -1; n       } else { n         great_right[i] = st.top(); n       } n       st.push(a[i]); n     } n   } n   void test_case(long long test) { n     long long n; n     cin >> n; n     long long a[n]; n     long long great_left[n]; n     long long great_right[n]; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     long long ans = 0; n     for (long long i = 0; i < n - 1; i++) { n       ans = max(ans, a[i] ^ a[i + 1]); n     } n     prepare_left(great_left, n, a); n     prepare_right(great_right, n, a); n     for (long long i = 0; i < n; i++) { n       long long left = great_left[i]; n       long long right = great_right[i]; n       if (left != -1) { n         ans = max(ans, left ^ a[i]); n       } n       if (right != -1) { n         ans = max(ans, right ^ a[i]); n       } n       if (left != -1 and right != -1) { n         ans = max(ans, left ^ right); n       } n     } n     cout << ans <<   n ; n   } n }; n int32_t main() { n   init(false); n   long long tc; n   if (test_cases) { n     cin >> tc; n   } else { n     tc = 1; n   } n   for (long long i = 0; i < tc; i++) { n     struct test_cases o; n     o.test_case(i + 1); n   } n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n bool solve(vector<int>& arr, int m, int n, int k) { n   vector<int> dp(n + 1, 0); n   int last = 0; n   for (int i = k; i <= n; i++) { n     int l = dp[i - k] + 1; n     if (arr[i] - arr[l] <= m) last = i; n     dp[i] = last; n   } n   return dp[n] == n; n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> arr(n + 1); n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   sort(arr.begin() + 1, arr.end()); n   int s = 0; n   int g = 1e9 + 1; n   int ans = -1; n   while (s != g) { n     int m = (s + g) / 2; n     if (solve(arr, m, n, k)) { n       g = m; n       ans = m; n     } else { n       if (s == m) { n         break; n       } n       s = m; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, INF = N; n int n, L, R, ANS_U, ANS_V, ANS; n struct st { n   int nxt, to, w, dis; n } ed[N << 1]; n int head[N], num; n void add(int from, int to, int w) { n   ed[++num].to = to, ed[num].w = w; n   ed[num].nxt = head[from]; n   head[from] = num; n } n int sum, rt, siz[N], mx[N]; n bool vis[N]; n void getrt(int x, int fa) { n   mx[x] = 0; n   for (int u, i = head[x]; i; i = ed[i].nxt) { n     u = ed[i].to; n     if (u == fa || vis[u]) continue; n     getrt(u, x), mx[x] = max(mx[x], siz[u]); n   } n   mx[x] = max(mx[x], sum - siz[x]); n   if (mx[x] < mx[rt]) rt = x; n } n int mxd[N], V[N]; n vector<int> E[N]; n void dfs_init(int x, int fa) { n   mxd[x] = 0, siz[x] = 1; n   for (int u, i = head[x]; i; i = ed[i].nxt) { n     u = ed[i].to; n     if (vis[u] || u == fa) continue; n     dfs_init(u, x), mxd[x] = max(mxd[x], mxd[u] + 1), siz[x] += siz[u]; n   } n } n bool cmp(int a, int b) { return mxd[a] < mxd[b]; } n void work(int x) { n   dfs_init(x, 0); n   int h = 0; n   for (int i = head[x]; i; i = ed[i].nxt) n     if (!vis[ed[i].to]) V[++h] = ed[i].to; n   sort(V + 1, V + 1 + h, cmp); n   for (int i = 1; i <= h; i++) E[x].push_back(V[i]); n } n void solve_init(int x) { n   work(x), vis[x] = 1; n   for (int u, i = head[x]; i; i = ed[i].nxt) { n     u = ed[i].to; n     if (vis[u]) continue; n     rt = 0, sum = siz[u], getrt(u, x), solve_init(rt); n   } n } n int f[N], a[N], s[N], id_a[N], id_s[N], ans_u, ans_v, ans; n void dfs(int x, int fa, int dep) { n   if (f[x] > a[dep]) a[dep] = f[x], id_a[dep] = x; n   for (int u, i = head[x]; i; i = ed[i].nxt) { n     u = ed[i].to; n     if (vis[u] || u == fa) continue; n     f[u] = f[x] + ed[i].dis, dfs(u, x, dep + 1); n   } n } n int q[N], hd, tail; n void calc(int x) { n   dfs_init(x, 0); n   int len = 0; n   for (int i = head[x]; i; i = ed[i].nxt) n     if (!vis[ed[i].to]) f[ed[i].to] = ed[i].dis, ++mxd[ed[i].to]; n   for (int i = 0; i <= mxd[x]; i++) s[i] = -N; n   s[0] = 0, id_s[0] = x; n   for (auto u : E[x]) { n     if (vis[u]) continue; n     for (int i = 0; i <= mxd[u]; i++) a[i] = -N; n     a[0] = 0, id_a[0] = x; n     dfs(u, x, 1), hd = 1, tail = 0; n     if (L <= len) n       for (int i = min(R, len); i > L; i--) { n         while (hd <= tail && s[q[tail]] <= s[i]) --tail; n         q[++tail] = i; n       } n     for (int i = 0; i <= mxd[u]; i++) { n       while (hd <= tail && q[hd] > R - i) ++hd; n       if (len >= L - i && L - i >= 0) { n         while (hd <= tail && s[L - i] >= s[q[tail]]) --tail; n         q[++tail] = L - i; n       } n       if (hd <= tail && s[q[hd]] + a[i] > ans) n         ans = s[q[hd]] + a[i], ans_u = id_a[i], ans_v = id_s[q[hd]]; n     } n     for (int i = 0; i <= mxd[u]; i++) n       if (a[i] > s[i]) s[i] = a[i], id_s[i] = id_a[i]; n     len = max(len, mxd[u]); n   } n } n void solve(int x) { n   calc(x), vis[x] = 1; n   for (int u, i = head[x]; i; i = ed[i].nxt) { n     u = ed[i].to; n     if (vis[u]) continue; n     rt = 0, sum = siz[u], getrt(u, x), solve(rt); n   } n } n int main() { n   int u, v, w, l = 0, r = 0; n   scanf( %d%d%d , &n, &L, &R); n   for (int i = 1; i < n; i++) n     scanf( %d%d%d , &u, &v, &w), add(u, v, w), add(v, u, w), r = max(r, w); n   mx[0] = N, rt = 0, sum = n, getrt(1, 0), solve_init(rt); n   while (l <= r) { n     int mid = (l + r) >> 1; n     for (int i = 1; i <= num; i++) ed[i].dis = (ed[i].w >= mid) ? 1 : -1; n     for (int i = 1; i <= n; i++) vis[i] = 0; n     mx[0] = N, ans = -N, rt = 0, sum = n, getrt(1, 0), solve(rt); n     if (ans >= 0) n       ANS_U = ans_u, ANS_V = ans_v, ANS = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   printf( %d %d n , ANS_U, ANS_V); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int light[3000]; n int agr[3000]; n int main() { n   int n; n   scanf( %d , &n); n   int m = pow(2, n + 1) - 1; n   for (int i = 2; i <= m; i++) { n     scanf( %d , &light[i]); n   } n   int ans = 0; n   for (int i = m; i >= 3; i--) { n     if (i % 2 == 0) continue; n     int a = light[i] + agr[i]; n     int b = light[i - 1] + agr[i - 1]; n     int c = a; n     if (a != b) { n       c = max(a, b); n       ans += abs(b - a); n     } n     agr[i / 2] = c; n     agr[(i - 1) / 2] = c; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long k, wyn; n long long tab[27][27]; n map<long long, long long> odp[27][27]; n void back1(int, int, long long); n void back2(int, int, long long); n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) cin >> tab[i][j]; n   } n   back1(1, 1, tab[1][1]); n   back2(n, m, k); n   if (n == 1 && m == 1 && tab[1][1] == k) n     cout << 1; n   else n     cout << wyn; n   return 0; n } n void back1(int x, int y, long long akt) { n   if (x + y == n + 1) { n     odp[x][y][akt]++; n     return; n   } n   if (x + 1 <= n) back1(x + 1, y, (akt ^ tab[x + 1][y])); n   if (y + 1 <= m) back1(x, y + 1, (akt ^ tab[x][y + 1])); n } n void back2(int x, int y, long long akt) { n   if (x + y == n + 1) { n     wyn += odp[x][y][akt]; n     return; n   } n   if (x - 1 >= 1) back2(x - 1, y, (akt ^ tab[x][y])); n   if (y - 1 >= 1) back2(x, y - 1, (akt ^ tab[x][y])); n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int calc(int a, int x, int n) { return max(0, min(x - (a + 1) / 2, n - a)); } n void solve(istream &in, ostream &out) { n   int n, m, x, y, a, b; n   in >> n >> m >> x >> y >> a >> b; n   int d = gcd(a, b); n   a /= d; n   b /= d; n   int t = min(n / a, m / b); n   a *= t; n   b *= t; n   int x1 = calc(a, x, n); n   int y1 = calc(b, y, m); n   int x2 = x1 + a; n   int y2 = y1 + b; n   out << x1 <<     << y1 <<     << x2 <<     << y2 << endl; n } n int main() { n   solve(cin, cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string a, b; n   cin >> a >> b; n   n *= 2; n   int n1 = 0, n2 = 0, q = 0; n   for (int i = 0; i < n; ++i) n     if (a[i] ==  1 ) n       if (b[i] ==  1 ) n         q = !q; n       else n         ++n1; n     else if (b[i] ==  1 ) n       ++n2; n   n1 += q; n   if (n1 == n2 - 1 || n1 == n2) n     cout <<  Draw ; n   else if (n1 > n2) n     cout <<  First ; n   else n     cout <<  Second ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool a[18][18]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) a[i][j] = 1; n   } n   map<string, int> mp; n   string name; n   vector<string> names(n); n   for (int i = 0; i < n; i++) { n     cin >> name; n     mp[name] = i; n     names[i] = name; n   } n   string s1, s2; n   for (int i = 0; i < m; i++) { n     cin >> s1 >> s2; n     a[mp[s1]][mp[s2]] = 0; n     a[mp[s2]][mp[s1]] = 0; n   } n   int res = 0; n   vector<int> v; n   vector<int> vres; n   for (int i = 1; i < (1 << n); i++) { n     v.clear(); n     for (int j = 0; j < n; j++) { n       if ((1 << j) & i) { n         v.push_back(j); n       } n     } n     bool b = true; n     for (int j = 0; j < v.size(); j++) { n       for (int k = j + 1; k < v.size(); k++) { n         if (!a[v[j]][v[k]]) { n           b = false; n           break; n         } n       } n     } n     if (b) { n       if (v.size() > res) { n         res = v.size(); n         vres = v; n       } n     } n   } n   cout << res << endl; n   vector<string> team; n   for (int i = 0; i < vres.size(); i++) { n     team.push_back(names[vres[i]]); n   } n   sort(team.begin(), team.end()); n   for (int i = 0; i < team.size(); i++) { n     if (i != 0) cout << endl; n     cout << team[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[2010], b[2010]; n unordered_map<int, int> mp; n int main() { n   int n; n   scanf( %d , &n); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     mp[a[i]] = 1; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &b[i]); n     mp[b[i]] = 1; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (mp[a[i] ^ b[j]] == 1) ans++; n     } n   } n   if (ans % 2 == 1) { n     puts( Koyomi ); n   } else { n     puts( Karen ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int n, sum; n int cnt[100005], from[100005], to[100005], c[100005]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) scanf( %d%d , &from[i], &to[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &c[i]); n   for (int i = 1; i < n; i++) { n     if (c[from[i]] != c[to[i]]) { n       cnt[from[i]]++; n       cnt[to[i]]++; n       sum++; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (cnt[i] == sum) { n       printf( YES n%d n , i); n       return 0; n     } n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve(int t) { n   int a, b, c, rem = 0, sum = 0; n   while (t != 0) { n     rem = t % 10; n     sum += rem; n     t /= 10; n   } n   if (sum % 4 == 0) return 1; n   return 0; n } n int main() { n   int t, n, flag = 0; n   cin >> t; n   while (1) { n     flag = solve(t); n     if (flag == 1) { n       cout << t <<   n ; n       break; n     } else n       t++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace IO { n const int __S = (1 << 20) + 5; n char __buf[__S], *__H, *__T; n inline char getc() { n   if (__H == __T) __T = (__H = __buf) + fread(__buf, 1, __S, stdin); n   if (__H == __T) return -1; n   return *__H++; n } n template <class __I> n inline void read(__I &__x) { n   __x = 0; n   int __fg = 1; n   char __c = getc(); n   while (!isdigit(__c) && __c !=  - ) __c = getc(); n   if (__c ==  - ) __fg = -1, __c = getc(); n   while (isdigit(__c)) __x = __x * 10 + __c -  0 , __c = getc(); n   __x *= __fg; n } n inline void readd(double &__x) { n   __x = 0; n   double __fg = 1.0; n   char __c = getc(); n   while (!isdigit(__c) && __c !=  - ) __c = getc(); n   if (__c ==  - ) __fg = -1.0, __c = getc(); n   while (isdigit(__c)) __x = __x * 10.0 + __c -  0 , __c = getc(); n   if (__c !=  . ) { n     __x = __x * __fg; n     return; n   } else n     while (!isdigit(__c)) __c = getc(); n   double __t = 1e-1; n   while (isdigit(__c)) n     __x = __x + 1.0 * (__c -  0 ) * __t, __t = __t * 0.1, __c = getc(); n   __x = __x * __fg; n } n inline void reads(char *__s, int __x) { n   char __c = getc(); n   int __tot = __x - 1; n   while (__c <  !  || __c >  ~ ) __c = getc(); n   while (__c >=  !  && __c <=  ~ ) __s[++__tot] = __c, __c = getc(); n   __s[++__tot] =   0 ; n } n char __obuf[__S], *__oS = __obuf, *__oT = __oS + __S - 1, __c, __qu[55]; n int __qr; n inline void flush() { n   fwrite(__obuf, 1, __oS - __obuf, stdout); n   __oS = __obuf; n } n inline void putc(char __x) { n   *__oS++ = __x; n   if (__oS == __oT) flush(); n } n template <class __I> n inline void print(__I __x) { n   if (!__x) putc( 0 ); n   if (__x < 0) putc( - ), __x = -__x; n   while (__x) __qu[++__qr] = __x % 10 +  0 , __x /= 10; n   while (__qr) putc(__qu[__qr--]); n } n inline void prints(const char *__s, const int __x) { n   int __len = strlen(__s + __x); n   for (int __i = __x; __i < __len + __x; __i++) putc(__s[__i]); n } n inline void printd(double __x, int __d) { n   long long __t = (long long)floor(__x); n   print(__t); n   putc( . ); n   __x -= (double)__t; n   while (__d--) { n     double __y = __x * 10.0; n     __x *= 10.0; n     int __c = (int)floor(__y); n     putc(__c +  0 ); n     __x -= floor(__y); n   } n } n inline void el() { putc(  n ); } n inline void sp() { putc(   ); } n }  // namespace IO n using namespace IO; n int cnt, sz[400005], usz[400005], n, x, y, g[400005], to[800005], he[400005], n     ne[800005], fm[400005], um[400005], sm[400005], ans[400005]; n void dfs1(int x, int fa) { n   sz[x] = 1; n   for (int i = he[x], y; ~i; i = ne[i]) n     if ((y = to[i]) != fa) { n       dfs1(y, x), sz[x] += sz[y]; n       if (sz[y] > n / 2) n         g[x] = y, sm[x] = max(sm[x], fm[y]), n         sm[x] > fm[x] ? swap(sm[x], fm[x]) : void(0); n       else n         sm[x] = max(sm[x], sz[y]), sm[x] > fm[x] ? swap(sm[x], fm[x]) : void(0); n     } n   usz[x] = n - sz[x]; n   if (usz[x] > n / 2) g[x] = -1; n } n void dfs2(int x, int fa) { n   if (usz[x] <= n / 2) n     um[x] = usz[x]; n   else { n     if (fm[fa] == sz[x]) n       um[x] = sm[fa]; n     else n       um[x] = fm[fa]; n     um[x] = max(um[x], um[fa]); n   } n   for (int i = he[x], y; ~i; i = ne[i]) n     if ((y = to[i]) != fa) dfs2(y, x); n } n void add(int x, int y) { n   to[cnt] = y; n   ne[cnt] = he[x]; n   he[x] = cnt++; n } n int main() { n   memset(he, -1, sizeof(he)); n   read(n); n   for (int i = 1; i < n; i++) read(x), read(y), add(x, y), add(y, x); n   dfs1(1, 0); n   dfs2(1, 0); n   for (int i = 1; i <= n; i++) { n     if (!g[i]) n       ans[i] = 1; n     else if (g[i] == -1 && usz[i] - um[i] <= n / 2) n       ans[i] = 1; n     else if (g[i] != -1 && sz[g[i]] - fm[g[i]] <= n / 2) n       ans[i] = 1; n   } n   for (int i = 1; i <= n; i++) print(ans[i]), sp(); n   flush(); n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a, long long int b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   long long int r = power(a, b / 2) % 1000000007; n   if (b % 2 == 0) n     return (r * r) % 1000000007; n   else n     return (((r * r) % 1000000007) * a) % 1000000007; n } n int prime[1000009]; n int sumprime[1000009]; n void sieve(int n) { n   prime[0] = prime[1] = 1; n   for (int i = 2; i <= sqrt(n); i++) { n     if (!prime[i]) { n       for (int j = 2 * i; j <= n; j = j + i) { n         prime[j]++; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!prime[i]) { n       sumprime[i] = sumprime[i - 1] + 1; n     } else { n       sumprime[i] = sumprime[i - 1]; n     } n   } n } n bool comp(const pair<long long int, long long int> &a, n           const pair<long long int, long long int> &b) { n   return (a.second < b.second); n } n const int N = 5 * 1e5 + 9; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   string s; n   cin >> s; n   int n = s.length(); n   int flag = 0; n   map<char, int> mp; n   mp[ A ]++; n   mp[ H ]++; n   mp[ I ]++; n   mp[ M ]++; n   mp[ O ]++; n   mp[ o ]++; n   mp[ T ]++; n   mp[ U ]++; n   mp[ V ]++; n   mp[ v ]++; n   mp[ W ]++; n   mp[ w ]++; n   mp[ X ]++; n   mp[ x ]++; n   mp[ Y ]++; n   mp[ b ]++; n   mp[ d ]++; n   mp[ p ]++; n   mp[ q ]++; n   if (n % 2) { n     char c = s[n / 2]; n     if (c ==  p  || c ==  q  || c ==  d  || c ==  b ) { n       flag++; n     } n   } n   for (int i = 0; i < n; i++) { n     if (mp.find(s[i]) == mp.end()) { n       flag++; n       break; n     } n     if (s[i] ==  b ) { n       if (s[n - i - 1] !=  d ) { n         flag++; n         break; n       } n       continue; n     } n     if (s[i] ==  d ) { n       if (s[n - i - 1] !=  b ) { n         flag++; n         break; n       } n       continue; n     } n     if (s[i] ==  p ) { n       if (s[n - i - 1] !=  q ) { n         flag++; n         break; n       } n       continue; n     } n     if (s[i] ==  q ) { n       if (s[n - i - 1] !=  p ) { n         flag++; n         break; n       } n       continue; n     } n     if (s[i] != s[n - i - 1]) { n       flag++; n       break; n     } n   } n   if (flag) { n     cout <<  NIE n ; n     return 0; n   } n   cout <<  TAK n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::string a; n   std::getline(cin, a); n   ; n   std::string b; n   std::getline(cin, b); n   ; n   int team1 = 0; n   int team2 = 0; n   for (int i = 0; i < a.size(); i = i + 2) { n     if (a[i] == b[i]) { n     } else if ((a[i] ==  8  && b[i] ==  [ ) || (a[i] ==  [  && b[i] ==  ( ) || n                (a[i] ==  (  && b[i] ==  8 )) { n       team1++; n     } else { n       team2++; n     } n   } n   if (team1 == team2) { n     cout <<  TIE ; n   } else if (team1 > team2) { n     cout <<  TEAM 1 WINS ; n   } else { n     cout <<  TEAM 2 WINS ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, x, y, z; n   cin >> a >> b >> c >> x >> y >> z; n   int a1, b1, c1, a2, b2, c2, m = 0, n = 0; n   if (a > x) { n     if (abs(a - x) >= 2) { n       if ((a - x) % 2 == 0) n         m = m + a - x; n       else n         m = m + a - x - 1; n     } n   } else n     n = n + a - x; n   if (b > y) { n     if (abs(b - y) >= 2) { n       if ((b - y) % 2 == 0) n         m = m + b - y; n       else n         m = m + b - y - 1; n     } n   } else n     n = n + b - y; n   if (c > z) { n     if (abs(c - z) >= 2) { n       if ((c - z) % 2 == 0) n         m = m + c - z; n       else n         m = m + c - z - 1; n     } n   } else n     n = n + c - z; n   if (m >= n * -2) n     cout <<  Yes  << endl; n   else n     cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n int R, C; n string t[2222]; n int s1[2222][2222]; n int main() { n   memset(s1, 0, sizeof(s1)); n   cin >> R >> C; n   for (int r = 0; r < (int)R; r++) { n     cin >> t[r]; n     for (int c = 0; c < (int)C; c++) { n       if (t[r][c] ==  w ) { n         s1[r + 1][c + 1]++; n       } n     } n   } n   for (int r = 0; r < (int)2221; r++) { n     for (int c = 0; c < (int)2221; c++) { n       s1[r + 1][c + 1] += (s1[r + 1][c] + s1[r][c + 1] - s1[r][c]); n     } n   } n   int lo = 0; n   int hi = max(R, C); n   while (lo < hi - 1) { n     int len = (lo + hi) / 2; n     bool isSome = false; n     for (int r = 0; r + len - 1 < max(R, C); r++) { n       for (int c = 0; c + len - 1 < max(R, C); c++) { n         if (s1[r + len][c + len] - s1[r + len][c] - s1[r][c + len] + s1[r][c] == n             s1[2221][2221]) { n           isSome = true; n           break; n         } n       } n       if (isSome) break; n     } n     if (isSome) n       hi = len; n     else n       lo = len; n   } n   cerr << hi << endl; n   bool done = false; n   for (int r = 0; r + hi - 1 < R; r++) { n     for (int c = 0; c + hi - 1 < C; c++) { n       int big = s1[r + hi][c + hi] - s1[r][c + hi] - s1[r + hi][c] + s1[r][c]; n       int small = 0; n       if (hi != 1 && hi != 2) { n         small = s1[r + hi - 1][c + hi - 1] - s1[r + 1][c + hi - 1] - n                 s1[r + hi - 1][c + 1] + s1[r + 1][c + 1]; n       } n       if (big - small == s1[2221][2221]) { n         for (int i = 0; i < hi; i++) { n           if (t[r][c + i] ==  . ) t[r][c + i] =  + ; n           if (t[r + hi - 1][c + i] ==  . ) t[r + hi - 1][c + i] =  + ; n           if (t[r + i][c] ==  . ) t[r + i][c] =  + ; n           if (t[r + i][c + hi - 1] ==  . ) t[r + i][c + hi - 1] =  + ; n         } n         done = true; n         break; n       } n       if (done) break; n     } n     if (done) break; n   } n   if (done) { n     for (int r = 0; r < (int)R; r++) { n       for (int c = 0; c < (int)C; c++) { n         cout << t[r][c]; n       } n       cout << endl; n     } n     cout << endl; n   } else { n     cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double EPS = 1e-9; n int N, K; n double L, V1, V2; n bool f(double t) { n   if (L / V1 < t - EPS) return true; n   int nGroups = N / K; n   if (N % K != 0) nGroups++; n   double distByBus = (t * V1 * V2 - L * V2) / (V1 - V2); n   double tByBus = distByBus / V2; n   double posBus = distByBus; n   double tElapsed = tByBus; n   nGroups--; n   while (nGroups > 0) { n     double posGroup = tElapsed * V1; n     if (posGroup > L - EPS) break; n     double tMeet = (posBus - posGroup) / (V1 + V2); n     double posMeet = posGroup + tMeet * V1; n     tElapsed += tMeet; n     if (L - posMeet < distByBus - EPS) { n       tElapsed += (L - posMeet) / V2; n       posBus = L; n     } else { n       tElapsed += tByBus; n       posBus = posMeet + distByBus; n     } n     nGroups--; n   } n   return tElapsed < t + EPS; n } n int main() { n   cin >> N >> L >> V1 >> V2 >> K; n   double lo = 0, hi = 1e9 + 5; n   int i = 0; n   while (hi - lo > EPS && i++ < 100) { n     double mid = (lo + hi) / 2; n     if (f(mid)) { n       hi = mid; n     } else n       lo = mid; n   } n   cout << fixed << setprecision(10) << hi << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1e9 + 7; n int w[1007]; n int n, x; n int d[107]; n int N = 101; n int mat[107][107]; n void mNoz(int (&a)[107][107], int (&b)[107][107], int (&c)[107][107]) { n   long long res[107][107]; n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= N; ++j) res[i][j] = 0; n   for (int i = 1; i <= N; ++i) n     for (int k = 1; k <= N; ++k) n       for (int j = 1; j <= N; ++j) n         res[i][j] += ((long long)a[i][k] * b[k][j]) % mod; n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= N; ++j) c[i][j] = res[i][j] % mod; n } n void pot(int (&a)[107][107], int (&c)[107][107], int k) { n   int res[107][107]; n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= N; ++j) res[i][j] = i == j; n   while (k > 0) { n     if (k & 1) mNoz(res, a, res); n     mNoz(a, a, a); n     k /= 2; n   } n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= N; ++j) c[i][j] = res[i][j]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> x; n   for (int i = 1; i <= n; ++i) { n     int a; n     cin >> a; n     mat[1][a]++; n   } n   mat[1][N] = mat[N][N] = 1; n   for (int i = 1; i + 1 < N; ++i) mat[i + 1][i] = 1; n   pot(mat, mat, x); n   int wyn = mat[1][1] + mat[1][N]; n   wyn %= mod; n   cout << wyn << endl; n   return 0; n } n


#include <bits/stdc++.h> n inline char nc() { n   static char buf[1000000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline void read(int &x) { n   static char c; n   for (c = getchar(); !( 0  <= c && c <=  9 ); c = getchar()) n     ; n   for (x = 0; ( 0  <= c && c <=  9 ); c = getchar()) x = x * 10 + c - 48; n } n void write(int x) { n   if (x < 10) { n     putchar( 0  + x); n     return; n   } n   write(x / 10); n   putchar( 0  + x % 10); n } n inline void writeln(int x) { n   if (x < 0) putchar( - ), x *= -1; n   write(x); n   putchar(  n ); n } n inline void writel(int x) { n   if (x < 0) putchar( - ), x *= -1; n   write(x); n   putchar(   ); n } n using namespace std; n int T, a[1007], sb, ans[1007 * 1007], n, tot; n void play(int x) { n   ans[++tot] = x; n   a[x] ^= a[x + 2], a[x + 2] ^= a[x], a[x] ^= a[x + 2]; n   a[x + 1] ^= a[x + 2], a[x + 2] ^= a[x + 1], a[x + 1] ^= a[x + 2]; n } n signed main() { n   read(T); n   while (T--) { n     read(n); n     tot = 0; n     for (int i = 1; i <= n; i++) read(a[i]); n     for (int i = 1; i <= n - 2; i++) { n       sb = i; n       for (int j = i + 1; j <= n; j++) n         if (a[j] < a[sb]) sb = j; n       while (sb >= i + 2) play(sb - 2), sb -= 2; n       if (sb == i + 1) { n         play(i), play(i); n         continue; n       } n     } n     for (int i = n; i >= 3; i--) { n       sb = i; n       for (int j = i - 1; j; j--) n         if (a[j] > a[sb]) sb = j; n       while (sb <= i - 2) play(sb), play(sb), sb += 2; n       if (sb == i - 1) { n         play(sb - 1); n         continue; n       } n     } n     for (int i = 1; i < n; i++) n       if (a[i] > a[i + 1]) tot = -1; n     writeln(tot); n     if (tot <= 0) continue; n     for (int i = 1; i < tot; i++) writel(ans[i]); n     writeln(ans[tot]); n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n   n #define ll long long n #define mod 1000000007   n #define pb push_back n #define ff first n #define ss second n #define all(a) (a).begin(),(a).end() n #define fr(i,a,b) for(int i=a;i<b;i++) n #define mapit(i,mp) for(auto i = mp.begin(); i!=mp.end(); i++) n #define pii pair<int,int> n  n int cte[100001] ; n int cto[100001] ; n   n void solve(){ n     int n; n        cin>>n; n        int a[n]; n        fr(i,0,n){ n             cin>>a[i]; n        } n   n        int b[n]; n   n        fr(i,0,n){ n             b[i] = a[i]; n        } n   n        sort(b,b+n); n   n   n        for(int i = 0; i<n; i++){ n             if(i%2 == 0){ n                 cte[a[i]]++; n             }else{ n                 cto[a[i]]++; n             } n        } n   n        bool flag = true; n   n        for(int i = 0; i<n; i++){ n             if(i%2 == 0){ n                 if(cte[b[i]] == 0){ n                     flag = false; n                     break; n                 } n                 cte[b[i]]--; n             }else{ n                 if(cto[b[i]] == 0){ n                     flag = false; n                     break; n                 } n                 cto[b[i]]--; n             } n        } n   n        if(flag){ n             cout<< YES <<  n ; n        }else{ n             cout<< NO <<  n ; n        } n } n   n int main(){ n     #ifndef ONLINE_JUDGE n         freopen( input.txt ,  r , stdin); n         freopen( output.txt ,  w , stdout); n     #endif n   n     int t = 1; n     cin>>t;     n    while(t--) n    { n         fr(i,0,100001){ n             cte[i] = 0; n         } n  n          fr(i,0,100001){ n             cto[i] = 0; n         } n  n         solve(); n    } n    return 0; n }