#include <bits/stdc++.h> n using namespace std; n map<long long int, vector<long long int>> v; n map<long long int, long long int> vis; n void dfs(long long int n) { n   long long int i; n   cout << n <<    ; n   vis[n] = 1; n   for (i = 0; i < v[n].size(); i++) { n     if (!vis[v[n][i]]) { n       vis[v[n][i]] = 1; n       dfs(v[n][i]); n     } n   } n } n int main() { n   long long int n, i, j, k, p, q, ans = 0; n   cin >> n; n   map<long long int, long long int> make_pair; n   for (i = 1; i <= n; i++) { n     cin >> p >> q; n     v[p].push_back(q); n     v[q].push_back(p); n     make_pair[p]++; n     make_pair[q]++; n   } n   for (auto it : make_pair) { n     if (it.second == 1) { n       ans = it.first; n       break; n     } n   } n   dfs(ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y; n int p[100010], q[100010]; n vector<int> t[100010], v[100010]; n int mn[100010]; n bool flag[100010]; n inline int dist(int x, int y) { n   if (p[x] - q[y] >= 0) n     return p[x] - q[y]; n   else n     return q[y] - p[x]; n } n int main() { n   scanf( %d%d%d%d , &n, &m, &x, &y); n   memset(mn, 63, sizeof(mn)); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   for (int i = 1; i <= m; i++) scanf( %d , &q[i]); n   q[m + 1] = 1e9; n   q[0] = -1e9; n   int pos = 1; n   for (int i = 1; i <= n; i++) { n     while (pos <= m && q[pos] <= p[i]) pos++; n     pos--; n     if (dist(i, pos) == dist(i, pos + 1)) { n       t[i].push_back(pos); n       t[i].push_back(pos + 1); n     } else if (dist(i, pos) < dist(i, pos + 1)) { n       t[i].push_back(pos); n     } else { n       t[i].push_back(pos + 1); n     } n   } n   for (int i = 1; i <= n; i++) n     if (t[i].size() == 1) { n       int t1 = t[i][0]; n       v[t1].push_back(i); n       mn[t1] = min(mn[t1], dist(i, t1)); n     } n   for (int i = 1; i <= n; i++) n     if (t[i].size() == 1) { n       if (dist(i, t[i][0]) > mn[t[i][0]]) flag[i] = 1; n     } n   for (int i = 1; i <= n; i++) n     if (t[i].size() == 2) { n       bool flag1 = 0, flag2 = 0; n       int cnt1 = 0, cnt2 = 0; n       int t1 = t[i][0], t2 = t[i][1], d = dist(i, t1); n       for (int j = 0; j < v[t1].size(); j++) { n         if (dist(v[t1][j], t1) < d) flag1 = 1; n         if (d < dist(v[t1][j], t1)) n           if (flag[v[t1][j]] == 0) cnt1++; n       } n       for (int j = 0; j < v[t2].size(); j++) { n         if (dist(v[t2][j], t2) < d) flag2 = 1; n         if (d < dist(v[t2][j], t2)) n           if (flag[v[t2][j]] == 0) cnt2++; n       } n       if (cnt1 + flag1 <= cnt2 + flag2) { n         if (flag1) flag[i] = 1; n         for (int j = 0; j < v[t1].size(); j++) { n           if (d < dist(v[t1][j], t1)) n             if (flag[v[t1][j]] == 0) flag[v[t1][j]] = 1; n         } n         mn[t1] = min(mn[t1], dist(i, t1)); n         v[t1].push_back(i); n       } else { n         if (flag2) flag[i] = 1; n         for (int j = 0; j < v[t2].size(); j++) { n           if (d < dist(v[t2][j], t2)) n             if (flag[v[t2][j]] == 0) flag[v[t2][j]] = 1; n         } n         mn[t2] = min(mn[t2], dist(i, t2)); n         v[t2].push_back(i); n       } n     } n   int ans = 0; n   for (int i = 1; i <= n; i++) ans += flag[i]; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double Pi = acos(-1.0); n const int Inf = 0x3f3f3f3f; n const int MAXN = 2e5 + 10; n inline int read() { n   register int x = 0, f = 1, ch = getchar(); n   while (!isdigit(ch)) f = ch ==  -  ? -1 : 1, ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); n   return x * f; n } n inline long long readll() { n   register long long x = 0, f = 1, ch = getchar(); n   while (!isdigit(ch)) f = ch ==  -  ? -1 : 1, ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); n   return x * f; n } n struct Edge1 { n   int u, v, d; n   Edge1(int u_ = 0, int v_ = 0, int d_ = 0) { u = u_, v = v_, d = d_; } n } e[MAXN << 1]; n struct Edge2 { n   int x, y; n   bool s; n }; n struct Node { n   int f[35]; n   inline void Insert(int x) { n     for (int i = 30; i >= 0; --i) n       if (x & (1 << i)) { n         if (!f[i]) { n           f[i] = x; n           return; n         } n         x ^= f[i]; n       } n   } n   inline int Query(int x) { n     for (int i = 30; i >= 0; --i) n       if ((x ^ f[i]) < x) x ^= f[i]; n     return x; n   } n } n1; n map<pair<int, int>, int> mp; n vector<Edge1> v[MAXN << 2]; n int n, m, c, Q, tim; n int st[MAXN << 1], ed[MAXN << 1]; n int qx[MAXN], qy[MAXN]; n int fa[MAXN], f[MAXN], dep[MAXN]; n inline int Find_fa(int x) { return fa[x] == x ? x : Find_fa(fa[x]); } n inline int Find_dis(int x) { return fa[x] == x ? 0 : f[x] ^ Find_dis(fa[x]); } n inline void Insert(int k, int l, int r, int x, int y, Edge1 e1) { n   if (x <= l && r <= y) { n     v[k].push_back(e1); n     return; n   } n   int mid = (l + r) >> 1; n   if (x <= mid) Insert(k << 1, l, mid, x, y, e1); n   if (y > mid) Insert(k << 1 | 1, mid + 1, r, x, y, e1); n } n inline void Solve(int k, int l, int r, Node G) { n   stack<Edge2> stk; n   for (int i = 0; i < (int)v[k].size(); ++i) { n     int x = v[k][i].u, y = v[k][i].v, d = v[k][i].d; n     int ex = Find_fa(x), ey = Find_fa(y); n     d ^= Find_dis(x) ^ Find_dis(y); n     if (ex == ey) n       G.Insert(d); n     else { n       if (dep[ex] > dep[ey]) swap(ex, ey), swap(x, y); n       Edge2 cur = (Edge2){ex, ey, 0}; n       fa[ex] = ey; n       f[ex] = d; n       if (dep[ex] == dep[ey]) ++dep[ey], cur.s = 1; n       stk.push(cur); n     } n   } n   if (l == r) n     printf( %d n , G.Query(Find_dis(qx[l]) ^ Find_dis(qy[l]))); n   else { n     int mid = (l + r) >> 1; n     Solve(k << 1, l, mid, G); n     Solve(k << 1 | 1, mid + 1, r, G); n   } n   while (!stk.empty()) n     f[fa[stk.top().x] = stk.top().x] = 0, dep[stk.top().y] -= stk.top().s, n                         stk.pop(); n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; ++i) fa[i] = i; n   for (int i = 1; i <= m; ++i) { n     int u = read(), v = read(), d = read(); n     mp[make_pair(u, v)] = i; n     st[i] = 1; n     ed[i] = -1; n     e[i] = Edge1(u, v, d); n   } n   c = m; n   tim = 1; n   Q = read(); n   for (int i = 1, opt, x, y, z; i <= Q; ++i) { n     opt = read(); n     x = read(); n     y = read(); n     if (opt == 1) { n       z = read(); n       mp[make_pair(x, y)] = ++c; n       st[c] = tim; n       ed[c] = -1; n       e[c] = Edge1(x, y, z); n     } else if (opt == 2) n       ed[mp[make_pair(x, y)]] = tim - 1; n     else n       qx[tim] = x, qy[tim] = y, ++tim; n   } n   --tim; n   for (int i = 1; i <= c; ++i) { n     if (ed[i] == -1) ed[i] = tim; n     if (st[i] <= ed[i]) Insert(1, 1, tim, st[i], ed[i], e[i]); n   } n   Solve(1, 1, tim, n1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mx[4] = {0, 0, 1, -1}; n int my[4] = {-1, 1, 0, 0}; n int n, m, cnt, cou; n char a[55][55]; n bool used[55][55]; n bool good(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); } n void dfs(int i, int j) { n   used[i][j] = true; n   cou++; n   for (int k = 0; k < 4; k++) n     if (good(i + mx[k], j + my[k]) && a[i + mx[k]][j + my[k]] ==  #  && n         !used[i + mx[k]][j + my[k]]) n       dfs(i + mx[k], j + my[k]); n } n int main() { n   scanf( %d%d n , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %s n , &a[i]); n     for (int j = 0; j < m; j++) n       if (a[i][j] ==  # ) cnt++; n   } n   if (cnt <= 2) n     printf( -1 n ); n   else { n     int ans = 2; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) n         if (a[i][j] ==  # ) { n           a[i][j] =  . ; n           cou = 0; n           memset(used, 0, sizeof(used)); n           for (int si = 0; si < n; si++) n             for (int sj = 0; sj < m; sj++) n               if (a[si][sj] ==  # ) dfs(si, sj), si = n, sj = m; n           if (cou != cnt - 1) ans = 1; n           a[i][j] =  # ; n         } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9 + 1; n const long long INFLL = 1e18 + 1; n struct Graph { n   struct edge { n     int c, f, link, to, ind; n     int c2 = 0; n     edge() {} n     edge(int c, int f, int link, int to, int ind) n         : c(c), f(f), link(link), to(to), ind(ind) {} n   }; n   vector<vector<edge>> g; n   int s, t, n; n   Graph(int N, int S, int T) { n     n = N; n     s = S, t = T; n     g.resize(n); n   } n   vector<int> usd; n   int dfs(int v, int now) { n     usd[v] = 1; n     if (v == t) return now; n     for (auto &e : g[v]) { n       if (usd[e.to] || e.f == e.c) continue; n       int x = dfs(e.to, min(now, e.c - e.f)); n       if (x == 0) continue; n       e.f += x; n       g[e.to][e.link].f -= x; n       return x; n     } n     return 0; n   } n   int Ford_Fulkerson() { n     int ans = 0; n     while (1) { n       usd.assign(n, 0); n       int d = dfs(s, INF); n       ans += d; n       if (d == 0) break; n     } n     return ans; n   } n   void add_not_dir(int a, int b, int c, int i) { n     g[a].emplace_back(c, 0, g[b].size(), b, i); n     g[b].emplace_back(c, 0, g[a].size() - 1, a, i); n   } n   void add_dir(int a, int b, int c, int i) { n     g[a].emplace_back(c, 0, g[b].size(), b, i); n     g[b].emplace_back(0, 0, g[a].size() - 1, a, i); n   } n   int Edmonds_Karp() { n     int ans = 0; n     while (1) { n       queue<int> q; n       vector<int> p(n, -1), dist(n, INF), mn(n, INF); n       q.push(s); n       dist[s] = 0; n       while (!q.empty()) { n         int v = q.front(); n         q.pop(); n         for (auto &e : g[v]) { n           if (e.f == e.c || dist[e.to] != INF) continue; n           p[e.to] = v; n           q.push(e.to); n           dist[e.to] = dist[v] + 1; n           mn[e.to] = min(mn[v], e.c - e.f); n         } n       } n       if (dist[t] == INF) break; n       vector<int> path; n       int now = t; n       while (now != -1) { n         path.emplace_back(now); n         now = p[now]; n       } n       reverse((path).begin(), (path).end()); n       int F = mn[t]; n       ans += F; n       for (int i = 0; i < (int)path.size() - 1; i++) { n         int v = path[i]; n         for (auto &e : g[v]) { n           if (e.to == path[i + 1] && e.c - e.f >= F) { n             e.f += F; n             g[e.to][e.link].f -= F; n             break; n           } n         } n       } n     } n     return ans; n   } n   vector<int> d, uk; n   int dfs_Dinic(int v, int now) { n     if (v == t || now == 0) return now; n     for (int i = uk[v]; i < (int)g[v].size(); i++) { n       if (d[g[v][i].to] == d[v] + 1) { n         int can = g[v][i].c2 - g[v][i].f; n         int x = dfs_Dinic(g[v][i].to, min(can, now)); n         if (x != 0) { n           g[v][i].f += x; n           g[g[v][i].to][g[v][i].link].f -= x; n           return x; n         } n       } n       uk[v]++; n     } n     return 0; n   } n   bool bfs() { n     d.assign(n, INF); n     d[s] = 0; n     deque<int> q; n     q.push_back(s); n     while (!q.empty()) { n       int v = q.front(); n       q.pop_front(); n       for (auto &e : g[v]) { n         if (e.f < e.c2 && d[e.to] == INF) { n           d[e.to] = d[v] + 1; n           q.push_back(e.to); n         } n       } n     } n     return d[t] != INF; n   } n   long long Dinic() { n     long long ans = 0; n     while (bfs()) { n       uk.assign(n, 0); n       while (1) { n         int d = dfs_Dinic(s, INF); n         if (d == 0) break; n         ans += d; n       } n     } n     return ans; n   } n   long long Fast_Dinic() { n     long long ans = 0; n     for (int j = 29; j >= 0; j--) { n       for (int i = 0; i < n; i++) { n         for (int k = 0; k < (int)g[i].size(); k++) { n           if (g[i][k].c & (1 << j)) g[i][k].c2 += (1 << j); n         } n       } n       ans += Dinic(); n     } n     return ans; n   } n }; n bool is_prime(int x) { n   for (int i = 2; i * i <= x; i++) { n     if (x % i == 0) return false; n   } n   return true; n } n vector<int> usd; n vector<vector<int>> gr; n void dfs_ans(int v, vector<int> &comp) { n   comp.emplace_back(v); n   usd[v] = 1; n   for (auto &to : gr[v]) { n     if (!usd[to]) dfs_ans(to, comp); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   int cnt0 = 0, cnt1 = 0; n   for (auto &c : a) { n     cin >> c; n     if (c % 2 == 0) n       cnt0++; n     else n       cnt1++; n   } n   if (cnt0 != cnt1) { n     cout <<  Impossible ; n     return 0; n   } n   Graph g(n + 2, n, n + 1); n   for (int i = 0; i < n; i++) { n     if (a[i] % 2 == 1) { n       g.add_dir(n, i, 2, -1); n     } else { n       g.add_dir(i, n + 1, 2, i); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (a[i] % 2 == 1 && a[j] % 2 == 0) { n         if (is_prime(a[i] + a[j])) { n           g.add_dir(i, j, 1, -1); n         } n       } n     } n   } n   int x = g.Edmonds_Karp(); n   if (x != 2 * cnt0) { n     cout <<  Impossible ; n     return 0; n   } n   gr.resize(n); n   vector<vector<int>> ans; n   for (int i = 0; i < n; i++) { n     for (auto &e : g.g[i]) { n       if (e.f == 1) { n         gr[i].emplace_back(e.to); n         gr[e.to].emplace_back(i); n       } n     } n   } n   usd.assign(n, 0); n   for (int i = 0; i < n; i++) { n     if (!usd[i]) { n       ans.emplace_back(vector<int>()); n       dfs_ans(i, ans.back()); n     } n   } n   cout << ans.size() <<   n ; n   for (auto &c : ans) { n     cout << c.size() <<    ; n     for (auto &l : c) { n       cout << l + 1 <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T abs(T a) { n   return a > 0 ? a : -a; n } n int n; n int m; n int main() { n   int last; n   cin >> n >> last; n   if (n == 1) { n     if (last == 1) { n       cout << 0 << endl <<  T  << endl; n     } else { n       cout <<  IMPOSSIBLE  << endl; n     } n     return 0; n   } n   int ans = -1; n   int errans = 1e9; n   for (int i = 1; i <= last; i++) { n     int a = last, b = i; n     int err = 0, len = 0; n     while (b) { n       err += (a / b) - 1; n       len += a / b; n       a %= b; n       swap(b, a); n     } n     if (a != 1) continue; n     err--; n     if (len == n) { n       if (errans > err) { n         errans = err; n         ans = i; n       } n     } n   } n   if (ans < 0) { n     cout <<  IMPOSSIBLE  << endl; n     return 0; n   } n   vector<int> res; n   int a = last, b = ans; n   while (b) { n     res.push_back(a / b); n     a %= b; n     swap(b, a); n   } n   reverse((res).begin(), (res).end()); n   res[0]--; n   cout << errans << endl; n   cout <<  T ; n   for (int i = 0; i < (((int)(res).size())); i++) n     for (int j = 0; j < (res[i]); j++) printf( %c , ((i + 1) % 2 ?  B  :  T )); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int RLEN = 1 << 18 | 1; n inline char nc() { n   static char ibuf[RLEN], *ib, *ob; n   (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin)); n   return (ib == ob) ? -1 : *ib++; n } n inline int rd() { n   char ch = nc(); n   int i = 0, f = 1; n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = nc(); n   } n   while (isdigit(ch)) { n     i = (i << 1) + (i << 3) + ch -  0 ; n     ch = nc(); n   } n   return i * f; n } n inline void W(long long x) { n   static int buf[50]; n   if (!x) { n     putchar( 0 ); n     return; n   } n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   while (x) { n     buf[++buf[0]] = x % 10; n     x /= 10; n   } n   while (buf[0]) putchar(buf[buf[0]--] +  0 ); n } n const int N = 4e5 + 50, M = 1e7 + 50; n int n, a[N], b[N], li[N], ri[N]; n int rt[N], lc[M], rc[M], c1[M], tot; n long long s1[M], s2[M], ans; n inline int ga(int *anc, int x) { n   return (anc[x] == x) ? x : (anc[x] = ga(anc, anc[x])); n } n inline void upt(int k) { n   c1[k] = c1[lc[k]] + c1[rc[k]]; n   s1[k] = s1[rc[k]] + s1[lc[k]] + c1[rc[k]] * s2[lc[k]]; n   s2[k] = s2[lc[k]] + s2[rc[k]]; n } n inline void inc(int &k, int l, int r, int p) { n   if (!k) k = ++tot; n   if (l == r) { n     c1[k] = 1; n     s1[k] = s2[k] = p; n     return; n   } n   int mid = (l + r) >> 1; n   if (p <= mid) n     inc(lc[k], l, mid, p); n   else n     inc(rc[k], mid + 1, r, p); n   upt(k); n } n inline int merge(int x, int y) { n   if (!x || !y) return (x + y); n   lc[x] = merge(lc[x], lc[y]); n   rc[x] = merge(rc[x], rc[y]); n   upt(x); n   return x; n } n inline void inc(int x, int y) { n   int r = ga(ri, x); n   li[r + 1] = r; n   ri[r] = ri[r + 1] = r + 1; n   ans -= s1[rt[x]] + (long long)(x - 1) * s2[rt[x]]; n   inc(rt[x], 1, n, y); n   ans += s1[rt[x]] + (long long)(x - 1) * s2[rt[x]]; n } n inline void _merge(int x, int y) { n   ans -= s1[rt[x]] + (long long)(x - 1) * s2[rt[x]]; n   ans -= s1[rt[y]] + (long long)(y - 1) * s2[rt[y]]; n   rt[x] = merge(rt[x], rt[y]); n   ans += s1[rt[x]] + (long long)(x - 1) * s2[rt[x]]; n   ri[y - 1] = y; n   li[y] = y - 1; n } n int main() { n   n = rd(); n   for (int i = 1; i <= n; i++) { n     a[i] = rd(), b[i] = rd(); n     ans -= (long long)a[i] * b[i]; n     if (li[a[i] - 1]) { n       inc(ga(li, a[i] - 1), b[i]); n     } else { n       if (!li[a[i]]) { n         li[a[i]] = ri[a[i]] = a[i]; n         inc(rt[a[i]], 1, n, b[i]); n         ans += s1[rt[a[i]]] + (a[i] - 1) * s2[rt[a[i]]]; n       } else n         inc(a[i], b[i]); n     } n     while (li[ga(ri, a[i]) + 1]) _merge(ga(li, a[i]), ga(ri, a[i]) + 1); n     W(ans); n     putchar(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 24; n int f[1 << MAXN], a[MAXN], N; n vector<pair<int, int> > p[MAXN]; n int dp(int state, int pos) { n   if (f[state] != -1) return f[state]; n   if (state == 1) return 1; n   if (p[pos].size() == 0) { n     f[state] = 0x3F3F3F3F; n     return f[state]; n   } n   int tot = 0, flag = 0; n   for (int i = 0; i <= pos; i++) n     if ((state >> i) & 1) tot++; n   int ret = 0x3F3F3F3F; n   for (int i = 0; i < p[pos].size(); i++) { n     int nxtstate = ((state & (~(1 << pos))) | (1 << pos - 1) | n                     (1 << p[pos][i].first) | (1 << p[pos][i].second)); n     ret = min(dp(nxtstate, pos - 1), ret); n   } n   return f[state] = max(ret, tot); n } n int main() { n   scanf( %d , &N); n   memset(f, -1, sizeof(f)); n   for (int i = 0; i < N; i++) scanf( %d , a + i); n   for (int i = 1; i < N; i++) { n     for (int j = 0; j < i; j++) n       for (int k = j; k < i; k++) n         if (a[j] + a[k] == a[i]) p[i].push_back(make_pair(j, k)); n   } n   int ans = dp(1 << (N - 1), N - 1); n   if (ans == 0x3F3F3F3F) n     printf( -1 n ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string A, B; n int has[10]; n string res; n void Create(int from) { n   for (int i = 0; i < 10; i++) { n     for (int j = 0; j < has[i]; j++) { n       res[from++] = i +  0 ; n     } n   } n } n int main() { n   cin >> A >> B; n   int st = 0; n   for (int i = 0; i < A.length(); i++) has[A[i] -  0 ]++; n   while (A.length() < B.length()) { n     A =  0  + A; n     st++; n   } n   res = A; n   for (int i = st; i < A.length(); i++) { n     for (int j = 10 - 1; j >= 0; j--) { n       if (has[j] > 0) { n         has[j]--; n         res[i] = j +  0 ; n         Create(i + 1); n         if (res <= B) break; n         has[j]++; n       } n     } n   } n   int pos = 0; n   while (res[pos] ==  0 ) pos++; n   cout << res.substr(pos) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::system_clock::now().time_since_epoch().count()); n const int MAXN = 200100, MAXK = 25; n const long long MOD = 998244353; n int st[MAXN][MAXK]; n int n, q; n int a[MAXN]; n pair<int, int> lastP[MAXN]; n int lg[MAXN]; n void buildST() { n   for (int j = (1); j <= (MAXK); j++) n     for (int i = 0; i + (1 << j) <= q; i++) n       st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); n } n int getMax(int l, int r) { n   int p = lg[r - l + 1]; n   return max(st[l][p], st[r - (1 << p) + 1][p]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   cin >> n; n   for (int i = (1); i <= (n); i++) cin >> a[i]; n   for (int i = (1); i <= (n); i++) lastP[i] = {a[i], -1}; n   cin >> q; n   for (int i = (2); i <= (q); i++) lg[i] = lg[i / 2] + 1; n   for (int i = (0); i <= (q - 1); i++) { n     int t; n     cin >> t; n     if (t == 1) { n       int p, x; n       cin >> p >> x; n       lastP[p] = {x, i}; n     } else { n       int x; n       cin >> x; n       st[i][0] = x; n     } n   } n   buildST(); n   for (int i = (1); i <= (n); i++) { n     int cur = lastP[i].first, wh = lastP[i].second; n     cur = max(cur, getMax(wh + 1, q - 1)); n     cout << cur <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 1; n long long n, a[maxn], b[maxn], f1, f2; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 0; i < n; ++i) cin >> a[i]; n   for (long long i = 0; i < n; ++i) cin >> b[i]; n   long long max1 = a[0], max2 = b[0]; n   f1 = a[0]; n   f2 = b[0]; n   for (long long i = 1; i < n; ++i) { n     f1 = max2 + a[i]; n     f2 = max1 + b[i]; n     max1 = max(max1, f1); n     max2 = max(max2, f2); n   } n   cout << max(max1, max2) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2010; n int n, m, k, p, q, d[maxn][maxn]; n bool vis[maxn][maxn]; n bool solve(int x, int y, int r) { n   for (int i = max(1, x - r); i <= min(n, x + r); i++) { n     int tmp = r - abs(i - x); n     if (y - tmp >= 1 && !vis[i][y - tmp]) { n       p = i, q = y - tmp; n       return 1; n     } n     if (y + tmp <= m && !vis[i][y + tmp]) { n       p = i, q = y + tmp; n       return 1; n     } n   } n   return 0; n } n int main() { n   scanf( %d %d %d , &n, &m, &k); n   for (int x, y, i = 1; i <= k; i++) { n     scanf( %d %d , &x, &y); n     for (int j = -1; j <= 1; j++) n       for (int k = -1; k <= 1; k++) { n         if (x + j < 1 || x + j > n || y + j < 1 || y + j > m) continue; n         d[x][y] = max(d[x][y], d[x + j][y + k] - abs(j) - abs(k)); n       } n     while (!solve(x, y, d[x][y])) d[x][y]++; n     printf( %d %d n , p, q); n     vis[p][q] = 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pro(int *a, int *b, int t, int s) { n   int k = 0; n   for (int i = 1; i < t; i++) { n     if (b[i] != s) { n       return 2; n     } n     if (b[i] == 0) { n       k++; n     } n   } n   if (k == (t - 1)) { n     return 3; n   } n   return 1; n } n int main() { n   int t, a[105], b[105]; n   a[0] = 0; n   cin >> t; n   for (int i = 1; i <= t; i++) { n     cin >> a[i]; n     b[i - 1] = a[i] - a[i - 1]; n   } n   if (t == 1) { n     cout << a[1]; n   } n   int c = pro(a, b, t, b[1]); n   if (c == 3) { n     cout << a[1]; n   } else if (c == 1) { n     cout << a[t] + b[1]; n   } else { n     cout << a[t]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int maxn = 1e6 + 5; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const double eps = 1e-8; n const double PI = acos(-1.0); n long long dp[1005][105]; n long long d, res, tot = 9, tmp = 1; n int main() { n   int n, k, m; n   scanf( %d%d%d , &n, &k, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < k; j++) { n       for (int t = 0 + (i == n); t <= 9; t++) { n         d = (t * tmp + j) % k; n         if (d) dp[i][d] = (dp[i][d] + dp[i - 1][j] + (j == 0)) % m; n       } n     } n     tmp = (tmp * 10) % k; n   } n   for (int i = 1; i < n; i++) tot = tot * 10 % m; n   for (int i = 1; i < k; i++) res = (res + dp[n][i]) % m; n   printf( %lld n , (m + tot - res) % m); n   return 0; n } n


#include <bits/stdc++.h> n   n #define PI 3.141592653589793238462643383279 n #define EXP 2.71828182846 n   n #define ll long long n  n using namespace std; n  n double log2(double n) n { n  return log(n)/log(2); n } n  n double ln(double n) n { n  return log(n)/log(EXP); n } n   n void Heap(signed long long int A[],signed long long int n,signed long long int l,signed long long int r) n { n  signed long long int x,i,j; n  x=A[l]; n  i=l; n  while(1) n  { n   j=2*i; n   if(j>r)break; n   if(j<r&&A[j+1]>=A[j]) n   j++; n   if(x>=A[j]) break; n   A[i]=A[j]; n   i=j; n  } n  A[i]=x; n } n   n void HeapSort(signed long long int A[],signed long long int n) n { n  signed long long int l,r,temp; n  l=n/2; n  while(l>=0) n  { n   Heap(A,n,l,n-1); n   l--; n  } n  r=n-1; n  while(r>=1) n  { n   temp=A[0]; n   A[0]=A[r]; n   A[r]=temp; n   r--; n   Heap(A,n,0,r); n  } n } n   n  bool issimple(unsigned long long n){  n  for(unsigned long long i=2;i<=sqrt(n);i++) n   if(n%i==0) n    return false; n  return true; n } n   n long long nod(long long a,long long b) n { n     long long temp; n     while(b) n     { n         a%=b; n         temp=a; n         a=b; n         b=temp; n     } n     return a; n } n   n long long nok(long long a,long long b,long long nod) n { n     return a/nod*b; n } n   n unsigned long long fac(unsigned long long n) n { n  unsigned long long f=1; n  if(n==0||n==1) return f; n  for(int i=2;i<=n;i++) n  { n   f*=i; n  } n  return f; n } n   n int bSearch(int A[],int n,int x) n { n  int l=0,r=n-1,m,found=0,c=0; n  while(l<=r)  n  { n   m=(l+r)/2; n   c++; n   if(A[m]==x)  n   { n    found=1; n    break; n   } n   //c++; n   if(A[m]<x) l=m+1; n   else r=m-1; n  } n  if(found) n   return c; n  return 0; n } n  n map<long long int,long long int> factorize(long long x) { n     map<long long int,long long int> factors; n   n     for (int i = 2; i <= sqrt(x); i++) { n         while (x % i == 0) { n          if(factors.find(i)==factors.end()) n              factors.insert({i,1}); n             else n              factors[i]++; n             x /= i; n         } n     } n   n     if (x != 1) { n         if(factors.find(x)==factors.end()) n              factors.insert({x,1}); n             else n              factors[x]++; n     } n   n     return factors; n } n  n void answer(bool yes) n { n  yes? cout<< YES n :cout<< NO n ; n } n  n long long ab(long long val) n { n  if(val<0) return -val; n  return val; n } n  n bool even(long long number) n { n  if (number & 1) n      return 0; //íå÷åòíîå n  else  n      return 1; //÷åòíîå n } n  n int main() n { n     long long n,k,i,count,j,t; n     bool yes,flag; n  cin>>t; n  while(t--) n  { n   cin>>n; n   count=0; n    n   count=0; n    n   for(i=1+n;!count;i++) n   { n    if(issimple(i)) n    { n     for(j=i+n;;j++) n     { n      if(issimple(j)) n      { n       count=(i)*(j); n       break; n      } n     } n      n    } n   } n    n   cout<<count<<endl; n   //answer(yes); n  } n      n     return 0; n } n  n  n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int ncr(unsigned long long int a, unsigned long long int b) { n   unsigned long long int ans = 1; n   for (unsigned long long int i = 1; i <= b; i++) { n     ans *= (a - i + 1); n   } n   for (unsigned long long int i = 1; i <= b; i++) { n     ans /= i; n   } n   return ans; n } n unsigned long long int power(unsigned long long int a, n                              unsigned long long int b) { n   unsigned long long int ans = 1; n   for (int i = 1; i <= b; i++) { n     ans *= a; n   } n   return ans; n } n unsigned long long int solvelessthansingle(int numofdigit) { n   unsigned long long int ans = ncr(numofdigit - 1, 1); n   ans *= 9; n   return ans; n } n unsigned long long int solvelessthandouble(int numofdigit) { n   unsigned long long int ans = ncr(numofdigit - 1, 2); n   ans *= 81; n   return ans; n } n unsigned long long int solvelessthantriple(unsigned long long int numofdigit) { n   unsigned long long int ans = ncr(numofdigit - 1, 3); n   ans *= 729; n   return ans; n } n unsigned long long int solvelessthannone(unsigned long long int numofdigit) { n   return 1; n } n unsigned long long int solve(unsigned long long int n, n                              unsigned long long int rem) { n   if (rem == 0) { n     return 0; n   } n   if (n < 10) { n     return n; n   } n   int numofdigit = 0; n   unsigned long long int n2 = n; n   while (n2) { n     numofdigit++; n     n2 /= 10; n   } n   unsigned long long int ans = 0; n   if (rem >= 1) ans += solvelessthansingle(numofdigit); n   if (rem >= 2) ans += solvelessthandouble(numofdigit); n   if (rem >= 3) ans += solvelessthantriple(numofdigit); n   int lastdigit; n   unsigned long long int n1 = n; n   while (n1) { n     int temp = n1 % 10; n     lastdigit = temp; n     n1 /= 10; n   } n   if (rem >= 1) ans += (lastdigit)*solvelessthannone(numofdigit); n   if (rem >= 2) ans += (lastdigit - 1) * solvelessthansingle(numofdigit); n   if (rem >= 3) ans += (lastdigit - 1) * solvelessthandouble(numofdigit); n   unsigned long long int mul = n       power((unsigned long long int)10, (unsigned long long int)numofdigit - 1); n   mul *= lastdigit; n   n -= mul; n   ans += solve(n, rem - 1); n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) { n     unsigned long long int n, m; n     cin >> n >> m; n     unsigned long long int ans = solve(m, 3) - solve(n - 1, 3); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int block_size = 320; n const long long mod = 1e9 + 7; n const long long inf = 1e9 + 7; n const long double eps = 1e-9; n const double PI = atan(1) * 4; n template <typename T> n inline int sign(const T &a) { n   if (a < 0) return -1; n   if (a > 0) return 1; n   return 0; n } n template <typename T, typename S> n inline bool upmin(T &a, const S &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T, typename S> n inline bool upmax(T &a, const S &b) { n   return a < b ? a = b, 1 : 0; n } n template <typename T> n inline void in(T &x) { n   x = 0; n   T f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   x *= f; n } n long long twop(int x) { return 1LL << x; } n template <typename A, typename B> n inline void in(A &x, B &y) { n   in(x); n   in(y); n } n template <typename A, typename B, typename C> n inline void in(A &x, B &y, C &z) { n   in(x); n   in(y); n   in(z); n } n template <typename A, typename B, typename C, typename D> n inline void in(A &x, B &y, C &z, D &d) { n   in(x); n   in(y); n   in(z); n   in(d); n } n int c[500010], tsize[500010], hv[500010], ans[500010]; n vector<int> son[500010]; n int mp[4194304 + 10]; n int global; n int addLen; n int opt[4194304], cnt = 0; n void change(int id, int to) { n   upmax(mp[id], to); n   opt[cnt++] = id; n } n void rollback() { n   while (cnt > 0) { n     mp[opt[--cnt]] = -inf; n   } n } n void tra(int cur, int len, int mask, int &ans) { n   for (long long i = 0; i < 22; i++) { n     ans = max(ans, mp[mask ^ twop(i) ^ global] + len + addLen); n   } n   ans = max(ans, mp[mask ^ global] + len + addLen); n   for (auto i : son[cur]) { n     tra(i, len + 1, mask ^ twop(c[i]), ans); n   } n } n void add(int cur, int len, int mask) { n   change(mask ^ global, len - addLen); n   for (auto i : son[cur]) { n     add(i, len + 1, mask ^ twop(c[i])); n   } n } n void solve(int cur, bool record) { n   for (auto s : son[cur]) { n     if (s != hv[cur]) { n       solve(s, 0); n       upmax(ans[cur], ans[s]); n     } n   } n   if (hv[cur]) { n     solve(hv[cur], 1); n     global ^= twop(c[hv[cur]]); n     addLen++; n     change(global, -addLen); n     upmax(ans[cur], ans[hv[cur]]); n     { upmax(ans[cur], mp[global] + addLen); } n     for (long long i = 0; i < 22; i++) { n       upmax(ans[cur], mp[0 ^ twop(i) ^ global] + addLen); n     } n     for (auto i : son[cur]) { n       if (i != hv[cur]) { n         tra(i, 1, twop(c[i]), ans[cur]); n         add(i, 1, twop(c[i])); n       } n     } n   } else { n     mp[0] = 0; n     global = 0; n     addLen = 0; n   } n   if (!record) { n     global = 0; n     rollback(); n     addLen = 0; n   } n } n void dfs(int cur) { n   tsize[cur] = 1; n   for (auto i : son[cur]) { n     dfs(i), tsize[cur] += tsize[i]; n     if (tsize[i] > tsize[hv[cur]]) hv[cur] = i; n   } n } n int main() { n   memset(mp, 0xcf, sizeof(mp)); n   int n; n   in(n); n   for (long long i = 2; i < n + 1; i++) { n     int p; n     in(p); n     char cc = getchar(); n     c[i] = cc -  a ; n     son[p].push_back(i); n   } n   map<int, int> mp; n   int global = 0, addLen = 0; n   dfs(1); n   solve(1, 0); n   for (long long i = 1; i < n + 1; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t = 1; n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   while (t--) { n     long long int i, j, k, m, n, ans = 0; n     cin >> n >> k; n     if (n >= k) n = k - 1; n     if (k - n >= n) { n       cout << 0; n       return 0; n     } n     long long int hi = n, lo = k - n; n     cout << (hi - lo + 1) / 2; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long sum(long long n) { return (n * (n - 1)) / 2; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int arr[n + 1]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   pair<int, int> b[n + 1]; n   for (int i = 0; i < n; i++) { n     cin >> b[i].first; n     b[i].second = i; n   } n   sort(b, b + n); n   sort(arr, arr + n, greater<int>()); n   long long a[n + 1]; n   for (int i = 0; i < n; i++) { n     a[b[i].second] = arr[i]; n   } n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool uin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n struct TPoint { n   long long x, y; n   TPoint(long long x, long long y) : x(x), y(y) {} n   TPoint operator+(const TPoint &p) const { return TPoint(x + p.x, y + p.y); } n   TPoint operator-(const TPoint &p) const { return TPoint(x - p.x, y - p.y); } n   long long operator%(const TPoint &p) const { return x * p.y - y * p.x; } n   long long operator*(const TPoint &p) const { return x * p.x + y * p.y; } n   long long operator~() const { return *this * *this; } n   bool operator<(const TPoint &p) const { n     return make_pair(x, y) < make_pair(p.x, p.y); n   } n }; n bool cmp(const TPoint &a, const TPoint &b) { n   if (a % b) return a % b > 0; n   return ~a < ~b; n } n const int maxn = 210000; n int miny[maxn], maxy[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.precision(10); n   cout << fixed; n   int n, m; n   while (1) { n     cin >> n >> m; n     if (!n) break; n     for (int i = 0; i < (int)(n + 10); ++i) miny[i] = 1e9, maxy[i] = -1e9; n     for (int i = 0; i < (int)(m); ++i) { n       int x, y; n       cin >> x >> y; n       uin(miny[x], y); n       uax(maxy[x], y); n     } n     vector<TPoint> p; n     for (int i = 0; i < (int)(n + 2); ++i) { n       if (i && miny[i] < miny[i - 1]) p.push_back(TPoint(i, miny[i])); n       if (miny[i] < miny[i + 1]) p.push_back(TPoint(i - 1, miny[i])); n       if (i && maxy[i] > maxy[i - 1]) p.push_back(TPoint(i, maxy[i] - 1)); n       if (maxy[i] > maxy[i + 1]) p.push_back(TPoint(i - 1, maxy[i] - 1)); n     } n     TPoint O = *min_element((p).begin(), (p).end()); n     for (auto &w : p) w = w - O; n     sort((p).begin(), (p).end(), cmp); n     vector<TPoint> st; n     for (auto w : p) { n       while (st.size() > 1) { n         TPoint prv = st.back(), pprv = st[st.size() - 2]; n         if ((w - pprv) % (prv - pprv) >= 0) n           st.pop_back(); n         else n           break; n       } n       st.push_back(w); n     } n     reverse((st).begin(), (st).end()); n     for (int i = 0; i < (int)(st.size()); ++i) { n       st[i] = st[i] + O; n     } n     int j = min_element((st).begin(), (st).end()) - st.begin(); n     cout << st.size() <<   n ; n     for (int i = 0; i < (int)(st.size()); ++i) { n       TPoint w = st[(i + j) % st.size()]; n       cout << w.x <<     << w.y <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nextInt() { n   char c; n   int i; n   for (c = getchar(); c <  0  || c >  9 ; c = getchar()) n     ; n   for (i = c -  0 , c = getchar(); c >=  0  && c <=  9 ; n        i = i * 10 + c -  0 , c = getchar()) n     ; n   return i; n } n const int inf = 1 << 30; n struct Edge { n   int v, w; n   Edge(int v = 0, int w = 0) : v(v), w(w) {} n }; n struct Info { n   int dis, lca; n   Info(int dis = 0, int lca = 0) : dis(dis), lca(lca) {} n   Info inc_dist(int w) const { return Info(dis + w, lca); } n }; n Info up[101000], down[101000]; n int anc[101000][20], fa[101000], dep[101000], w[101000]; n bool live[101000]; n vector<Edge> e[101000]; n int n, m; n Info merge_info(const Info& a, const Info& b, int w, int v) { n   Info info = b.inc_dist(w); n   if (info.dis > a.dis) { n     return info; n   } else if (info.dis == a.dis) { n     info.lca = v; n     return info; n   } else { n     return a; n   } n } n void calc_up(int x) { n   vector<Edge> son; n   for (Edge edge : e[x]) n     if (edge.v != fa[x]) son.push_back(edge); n   int deg = son.size(); n   vector<Info> pre_max = vector<Info>(deg); n   for (int i = 0; i < deg; ++i) { n     pre_max[i] = n         (i == 0 ? down[son[i].v].inc_dist(son[i].w) n                 : merge_info(pre_max[i - 1], down[son[i].v], son[i].w, x)); n   } n   vector<Info> suf_max = vector<Info>(deg); n   for (int i = deg - 1; i >= 0; --i) { n     suf_max[i] = (i == deg - 1 ? down[son[i].v].inc_dist(son[i].w) n                                : merge_info(suf_max[i + 1], down[son[i].v], n                                             son[i].w, x)); n   } n   for (int i = 0; i < deg; ++i) { n     int v = son[i].v, w = son[i].w; n     Info info = up[x]; n     if (i != 0) info = merge_info(info, pre_max[i - 1], 0, x); n     if (i != deg - 1) info = merge_info(info, suf_max[i + 1], 0, x); n     up[v] = merge_info(Info(live[v] ? 0 : -inf, v), info, w, v); n     calc_up(v); n   } n } n void calc_down(int x) { n   down[x] = Info(live[x] ? 0 : -inf, x); n   anc[x][0] = fa[x]; n   for (int i = 1; (1 << i) <= dep[x]; ++i) { n     anc[x][i] = anc[anc[x][i - 1]][i - 1]; n   } n   for (Edge edge : e[x]) n     if (edge.v != fa[x]) { n       int v = edge.v; n       fa[v] = x, dep[v] = dep[x] + 1, calc_down(v); n       down[x] = merge_info(down[x], down[v], edge.w, x); n     } n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   for (int i = 20 - 1; i >= 0; --i) n     if (dep[anc[x][i]] >= dep[y]) x = anc[x][i]; n   if (x == y) return x; n   for (int i = 20 - 1; i >= 0; --i) n     if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i]; n   return fa[x]; n } n void chain_inc(int x, int y) { n   int z = lca(x, y); n   ++w[x], ++w[y], --w[z], --w[fa[z]]; n } n void calc_ans(int x) { n   for (Edge edge : e[x]) n     if (edge.v != fa[x]) { n       calc_ans(edge.v), w[x] += w[edge.v]; n     } n } n int main() { n   dep[0] = -1; n   n = nextInt(); n   m = nextInt(); n   for (int i = 1; i <= m; ++i) { n     int v = nextInt(); n     live[v] = true; n   } n   for (int i = 1; i <= n - 1; ++i) { n     int u = nextInt(); n     int v = nextInt(); n     int w = nextInt(); n     e[u].push_back(Edge(v, w)); n     e[v].push_back(Edge(u, w)); n   } n   int root = 1; n   calc_down(root); n   up[root] = Info(live[root] ? 0 : -inf, root); n   calc_up(root); n   for (int i = 1; i <= n; ++i) n     if (live[i]) { n       if (up[i].dis > down[i].dis) { n         chain_inc(i, up[i].lca); n       } else if (up[i].dis < down[i].dis) { n         chain_inc(i, down[i].lca); n       } n     } n   calc_ans(1); n   int ans = 0, ans_cnt = 0; n   for (int i = 1; i <= n; ++i) n     if (!live[i]) { n       if (w[i] > ans) n         ans = w[i], ans_cnt = 1; n       else if (w[i] == ans) n         ++ans_cnt; n     } n   printf( %d %d n , ans, ans_cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000, M = 2000; n int head[N + 10], ver[M + 10], nxt[M + 10], tot = 0; n void add(int x, int y) { n   ver[++tot] = y; n   nxt[tot] = head[x]; n   head[x] = tot; n } n int col[N + 10], cnt = 0; n void dfs(int x) { n   col[x] = cnt; n   for (int i = head[x]; i; i = nxt[i]) { n     int y = ver[i]; n     if (col[y]) continue; n     dfs(y); n   } n } n vector<int> seq[N + 10]; n pair<int, int> que[N + 10]; n int h = 1, t = 0, fa[N + 10]; n bool vis[N + 10]; n pair<int, int> bfs(int x, bool f) { n   memset(vis, 0, sizeof(vis)); n   if (f) memset(fa, -1, sizeof(fa)); n   h = 1; n   t = 0; n   pair<int, int> ans = make_pair(0, 0); n   que[++t] = make_pair(0, x); n   vis[x] = 1; n   while (h <= t) { n     pair<int, int> H = que[h++]; n     ans = max(ans, H); n     for (int i = head[H.second]; i; i = nxt[i]) { n       int y = ver[i]; n       if (vis[y]) continue; n       vis[y] = 1; n       if (f) fa[y] = H.second; n       que[++t] = make_pair(H.first + 1, y); n     } n   } n   return ans; n } n int dis[N + 10], v[N + 10]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     add(x, y); n     add(y, x); n   } n   int maxx = 0, f = 0; n   for (int i = 1; i <= n; i++) { n     if (!col[i]) { n       cnt++; n       dfs(i); n       pair<int, int> tmp = bfs(bfs(i, false).second, true); n       int y = tmp.second; n       if (tmp.first >= maxx) { n         maxx = tmp.first; n         f = cnt; n       } n       while (~y) { n         seq[cnt].push_back(y); n         y = fa[y]; n       } n       v[cnt] = seq[cnt][seq[cnt].size() / 2]; n     } n   } n   for (int i = 1; i <= cnt; i++) { n     if (i == f) continue; n     add(v[i], v[f]); n     add(v[f], v[i]); n   } n   printf( %d n , bfs(bfs(1, false).second, false).first); n   for (int i = 1; i <= cnt; i++) { n     if (i == f) continue; n     printf( %d %d n , v[i], v[f]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n int n, a, b, t; n pair<int, int> m[MAXN]; n bool ans[MAXN]; n int main() { n   cin >> n >> a >> b; n   for (int i = 0; i < n; i++) { n     scanf( %d , &m[i].first); n     m[i].second = i; n     if (a < b) m[i].second *= -1; n   } n   if (a != b) sort(m, m + n); n   for (int i = 0; i < (a < b ? b : a); i++) n     if (a < b) n       ans[a < b ? -m[i].second : m[i].second] = true; n     else n       ans[a < b ? -m[i].second : m[i].second] = true; n   for (int i = 0; i < n; i++) n     printf( %d  , ((a >= b) ? (ans[i] ? 1 : 2) : (ans[i] ? 2 : 1))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T min(T a, T b, T c) { n   return min(a, min(b, c)); n } n template <class T> n T max(T a, T b, T c) { n   return max(a, max(b, c)); n } n struct matrix { n   vector<vector<long long> > a; n   int m, n; n   matrix(int _m, int _n) { n     m = _m; n     n = _n; n     a.resize(m + 1); n     for (int i = (1); i <= (m); ++i) a[i].resize(n + 1); n   } n   void read() { n     for (int i = (1); i <= (m); ++i) { n       for (int j = (1); j <= (n); ++j) scanf( %lld , &a[i][j]); n     } n   } n   bool inline operator==(const matrix &o) const { n     if (m != o.m || n != o.n) return false; n     for (int i = (1); i <= (m); ++i) { n       for (int j = (1); j <= (n); ++j) { n         if (a[i][j] != o.a[i][j]) return false; n       } n     } n     return true; n   } n   void inline operator=(const matrix &o) { n     m = o.m; n     n = o.n; n     for (int i = (1); i <= (m); ++i) { n       for (int j = (1); j <= (n); ++j) a[i][j] = o.a[i][j]; n     } n   } n   matrix inline operator*(const matrix &o) const { n     matrix c(m, o.n); n     for (int i = (1); i <= (m); ++i) { n       for (int j = (1); j <= (o.n); ++j) { n         c.a[i][j] = 0; n         for (int k = (1); k <= (n); ++k) n           c.a[i][j] = (c.a[i][j] + a[i][k] * o.a[k][j]) % 1000003; n       } n     } n     return c; n   } n   void print() { n     for (int i = (1); i <= (m); ++i) { n       for (int j = (1); j <= (n); ++j) printf( %lld  , a[i][j]); n       puts(  ); n     } n     puts(  ); n   } n }; n matrix matPow(matrix A, int n) { n   if (n == 1) return A; n   if (n % 2 == 1) return A * matPow(A, n - 1); n   matrix B = matPow(A, n / 2); n   return B * B; n } n int n, H, W; n int main() { n   scanf( %d%d%d , &n, &W, &H); n   n += 1; n   matrix A(W + 1, W + 1); n   for (int i = (1); i <= (W); ++i) A.a[i][i + 1] = 1; n   A.a[W + 1][W + 1] = 1; n   for (int j = (W); j >= (1); --j) n     A.a[W + 1][j] = (A.a[W + 1][j + 1] * H) % 1000003; n   matrix F(W + 1, 1); n   F.a[1][1] = 1; n   for (int i = (2); i <= (W + 1); ++i) { n     int p = 1; n     for (int j = (i - 1); j >= (1); --j) { n       F.a[i][1] = (F.a[i][1] + F.a[j][1] * p) % 1000003; n       p = (p * H) % 1000003; n     } n   } n   if (n <= W) n     cout << F.a[n + 1][1]; n   else n     cout << (matPow(A, n - W) * F).a[W + 1][1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int to, u, w; n } edge[200000]; n struct data { n   int x, y, w, d; n } v[200010]; n int k = 1, f[200000], low[200010], dfu[200010], n, m, ans[200010], head[200010], n     cnt, l; n bool vis[200010]; n void add(int x, int y, int z) { n   edge[k].u = y; n   edge[k].to = head[x]; n   edge[k].w = z; n   head[x] = k++; n } n int getfather(int x) { n   if (f[x] == x) return x; n   f[x] = getfather(f[x]); n   return f[x]; n } n void dfs(int now, int from) { n   low[now] = dfu[now] = cnt++; n   vis[now] = 1; n   for (int i = head[now]; i != -1; i = edge[i].to) { n     int u = edge[i].u; n     if (vis[u] == 1 && edge[i].w != from) n       low[now] = min(low[now], dfu[u]); n     else if (vis[u] != 1) { n       dfs(u, edge[i].w); n       low[now] = min(low[now], low[u]); n       if (dfu[now] < low[u]) ans[edge[i].w] = 2; n     } n   } n } n void add(int x, int y) { n   int xx = getfather(x); n   int yy = getfather(y); n   if (xx != yy) { n     dfu[x] = dfu[y] = dfu[xx] = dfu[yy] = 0; n     low[x] = low[y] = low[xx] = low[yy] = 0; n     vis[x] = vis[y] = vis[xx] = vis[yy] = 0; n     head[x] = head[y] = head[xx] = head[yy] = -1; n     f[xx] = yy; n   } n } n bool comp(data x, data y) { return x.w < y.w; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &v[i].x, &v[i].y, &v[i].w); n     v[i].d = i; n   } n   sort(v + 1, v + m + 1, comp); n   memset(head, -1, sizeof(head)); n   for (int i = 1; i <= n; i++) f[i] = i; n   l = 1; n   for (int i = 1; i <= m; i++) n     if (v[i].w != v[i + 1].w) { n       for (int j = l; j <= i; j++) { n         int xx = getfather(v[j].x); n         int yy = getfather(v[j].y); n         if (xx == yy) { n           ans[v[j].d] = 1; n         } else { n           add(xx, yy, v[j].d); n           add(yy, xx, v[j].d); n         } n       } n       for (int j = l; j <= i; j++) { n         int xx = getfather(v[j].x); n         int yy = getfather(v[j].y); n         if (xx != yy && vis[xx] == 0) dfs(xx, -1); n         if (xx != yy && vis[yy] == 0) dfs(yy, -1); n       } n       for (int j = l; j <= i; j++) { n         add(v[j].x, v[j].y); n       } n       l = i + 1; n     } n   for (int i = 1; i <= m; i++) n     if (ans[i] == 1) n       printf( %s n ,  none ); n     else if (ans[i] == 0) n       printf( %s n ,  at least one ); n     else n       printf( %s n ,  any ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, t = 0, a[30005]; n   cin >> n >> k; n   for (int i = 1; i < n; i++) cin >> a[i]; n   for (t = 1; t < k; t += a[t]) n     ; n   cout << (t == k ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n void Speed() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n struct xi { n   long long a, b; n }; n const int XS = 1e3 + 12; n const int second = 1e4 + 12; n const int M = 1e5 + 12; n const int L = 1e6 + 12; n const int XL = 1e7 + 12; n const long long INF1 = 1e9 + 7; n const long long INF2 = 1e12 + 7; n long long T, n; n void Tramp() { n   cin >> n; n   long long a[n + 12], b[n + 12]; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n; ++i) { n     if (i & 1) { n       b[i] = a[i + 1] * (-1); n       b[i + 1] = a[i]; n     } n   } n   for (int i = 1; i <= n; ++i) cout << b[i] <<    ; n   cout <<   n ; n } n int main() { n   Speed(); n   cin >> T; n   while (T--) Tramp(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000003ll; n long long npow[64]; n long long fast_pow(long long n, long long e) { n   if (e == 0) return 1ll; n   long long tmp = fast_pow(n, e / 2ll); n   tmp = (tmp * tmp) % MOD; n   if (e & 1ll) return (tmp * n) % MOD; n   return tmp; n } n int main() { n   ios::sync_with_stdio(0); n   npow[0] = 1ll; n   for (int i = 1; i < 63; i++) npow[i] = (npow[i - 1] * 2ll); n   long long n, k; n   cin >> n >> k; n   if (n < 63 && k > npow[n]) { n     cout <<  1 1 n ; n     return 0; n   } n   long long tgt = k - 1ll; n   long long pcount = 0; n   long long val = 2ll; n   while (true) { n     long long adder = tgt / val; n     if (!adder) break; n     val *= 2ll; n     pcount += adder; n   } n   long long kmult = n - (pcount / (k - 1)); n   long long den = fast_pow(2, k - 1ll); n   den = fast_pow(den, kmult - 1ll); n   den = den * fast_pow(2ll, k - 1 - (pcount % (k - 1))); n   den %= MOD; n   long long num = 0; n   if (k - 1ll < MOD) { n     long long prod = fast_pow(2ll, pcount); n     long long rem = fast_pow(2ll, n); n     num = fast_pow(prod, MOD - 2ll); n     for (int i = 1; i <= k - 1ll; i++) { n       long long mult = rem - i; n       while (mult < 0) mult += MOD; n       num = (num * mult) % MOD; n     } n   } n   num = (den - num); n   while (num < 0) num += MOD; n   cout << num <<     << den <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long su = 0, f = 1; n   char c = getchar(); n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) su = su * 10 + c -  0 ; n   return su * f; n } n inline void wr(long long x) { n   if (x < 0) n     putchar( - ), wr(-x); n   else { n     if (x >= 10) wr(x / 10); n     putchar(x % 10 +  0 ); n   } n } n inline void wri(long long x) { n   wr(x); n   putchar(   ); n } n inline void wrn(long long x) { n   wr(x); n   putchar(  n ); n } n inline void wrn(long long x, long long y) { n   wri(x); n   wrn(y); n } n inline void wrn(long long x, long long y, long long z) { n   wri(x); n   wrn(y, z); n } n int n, m, ans[600055], k, a[600055], x, y, ss[600055]; n vector<pair<long long, long long> > q[600055]; n pair<long long, long long> z[600055]; n char str[600055]; n int Next[600055 * 2], head[600055], to[600055 * 2], nedge, g[600055][4]; n void add(int a, int b) { n   Next[++nedge] = head[a]; n   head[a] = nedge; n   to[nedge] = b; n } n void add_ne(int a, int b) { n   add(a, b); n   add(b, a); n } n void ad(int x, int k) { n   for (; x <= n; x += x & -x) ss[x] += k; n } n int que(int x) { n   int su = 0; n   for (; x; x -= x & -x) su += ss[x]; n   return su; n } n signed main() { n   n = read(); n   for (int i = 1; i <= n; i++) n     a[i] = read(), z[i].first = a[i], z[i].second = n - i + 1; n   sort(z + 1, z + n + 1); n   for (int i = 1; i <= n; i++) z[i].second = n - z[i].second + 1; n   m = read(); n   for (int i = 1; i <= m; i++) { n     x = read(); n     y = read(); n     q[x].push_back(make_pair(y, i)); n   } n   for (int i = 1; i <= n; i++) { n     ad(z[n - i + 1].second, 1); n     for (int j = 0; j < int(q[i].size()); j++) { n       int l = 1, r = n, t = q[i][j].first; n       while (l < r) { n         int mid = (l + r) >> 1; n         if (que(mid) < t) n           l = mid + 1; n         else n           r = mid; n       } n       ans[q[i][j].second] = l; n     } n   } n   for (int i = 1; i <= m; i++) wrn(a[ans[i]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n int main() { n   n  int t , x , k; n   n  cin >> t; n   n  while(t--) n  { n      cin >> x; n       n      k = (int)(sqrt(8 * x + 1)) / 2; n        n        n      if((k * (k + 1) - 2 * x)!=2 && (k * (k + 1) - 2 * x) >= 0) n        cout << k <<   n ; n         n      else n        cout << k + 1 <<   n ;  n       n       n  } n   n    n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int LG = 18; n const int MX_N = 100005; n int n, q; n struct LowestCommonAncestor { n   int clk, par[MX_N][LG]; n   int st[MX_N], h[MX_N]; n   vector<int> G[MX_N]; n   void addEdge(int u, int v) { n     G[u].push_back(v); n     G[v].push_back(u); n   } n   void dfs(int u = 1, int p = 0) { n     st[u] = ++clk; n     par[u][0] = p; n     h[u] = h[p] + 1; n     for (int i = 1; i < LG; i++) par[u][i] = par[par[u][i - 1]][i - 1]; n     for (auto& v : G[u]) n       if (v != p) dfs(v, u); n   } n   int LCA(int u, int v) { n     if (h[u] < h[v]) swap(u, v); n     for (int i = LG - 1; i >= 0; i--) n       if (h[par[u][i]] >= h[v]) u = par[u][i]; n     if (u == v) return u; n     for (int i = LG - 1; i >= 0; i--) n       if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i]; n     return par[u][0]; n   } n }; n struct SegTree { n   pair<int, int> seg1[MX_N * 4], seg2[MX_N * 4]; n   void Merge(int nd, int lft, int rgt) { n     seg1[nd] = min(seg1[lft], seg1[rgt]); n     seg2[nd] = max(seg2[lft], seg2[rgt]); n   } n   void Bld(int* a, int nd = 1, int l = 1, int r = n) { n     if (l == r) { n       seg1[nd] = seg2[nd] = {a[l], l}; n       return; n     } n     int mid = l + (r - l) / 2, lft = nd << 1, rgt = lft | 1; n     Bld(a, lft, l, mid); n     Bld(a, rgt, mid + 1, r); n     Merge(nd, lft, rgt); n   } n   pair<int, int> QryMN(int L, int R, int nd = 1, int l = 1, int r = n) { n     if (l > R or r < L) return {INT_MAX, INT_MAX}; n     if (l >= L and r <= R) return seg1[nd]; n     int mid = l + (r - l) / 2, lft = nd << 1, rgt = lft | 1; n     return min(QryMN(L, R, lft, l, mid), QryMN(L, R, rgt, mid + 1, r)); n   } n   pair<int, int> QryMX(int L, int R, int nd = 1, int l = 1, int r = n) { n     if (l > R or r < L) return {INT_MIN, INT_MIN}; n     if (l >= L and r <= R) return seg2[nd]; n     int mid = l + (r - l) / 2, lft = nd << 1, rgt = lft | 1; n     return max(QryMX(L, R, lft, l, mid), QryMX(L, R, rgt, mid + 1, r)); n   } n }; n SegTree sg; n LowestCommonAncestor olca; n void calc(int lca, int bd, int& res, int& bad) { n   if (res < olca.h[lca]) res = olca.h[lca], bad = bd; n } n void solution(int l, int r, int& res, int& bad) { n   pair<int, int> mnD = sg.QryMN(l, r); n   pair<int, int> mxD = sg.QryMX(l, r); n   int L = mnD.second; n   int R = mxD.second; n   pair<int, int> tmp; n   if (L == l) { n     tmp = sg.QryMN(l + 1, r); n     calc(olca.LCA(tmp.second, R), L, res, bad); n   } else if (L == r) { n     tmp = sg.QryMN(l, r - 1); n     calc(olca.LCA(tmp.second, R), L, res, bad); n   } else { n     tmp = min(sg.QryMN(l, L - 1), sg.QryMN(L + 1, r)); n     calc(olca.LCA(tmp.second, R), L, res, bad); n   } n   if (R == l) { n     tmp = sg.QryMX(l + 1, r); n     calc(olca.LCA(tmp.second, L), R, res, bad); n   } else if (R == r) { n     tmp = sg.QryMX(l, r - 1); n     calc(olca.LCA(tmp.second, L), R, res, bad); n   } else { n     tmp = max(sg.QryMX(l, R - 1), sg.QryMX(R + 1, r)); n     calc(olca.LCA(tmp.second, L), R, res, bad); n   } n } n int main() { n   scanf( %d %d , &n, &q); n   for (int i = 2, p; i <= n; i++) { n     scanf( %d , &p); n     olca.addEdge(p, i); n   } n   olca.dfs(); n   sg.Bld(olca.st); n   while (q--) { n     int l, r; n     scanf( %d %d , &l, &r); n     if (r - l == 1) { n       int hh = max(olca.h[l], olca.h[r]); n       int nn = (hh == olca.h[l]) ? r : l; n       printf( %d %d n , nn, hh - 1); n       continue; n     } n     int res = 0, bad = 0; n     solution(l, r, res, bad); n     printf( %d %d n , bad, res - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[1007]; n struct Node { n   int pos; n   int val; n }; n Node res[1007]; n int gcd(int a, int b) { n   int m = std::max(a, b); n   int n = std::min(a, b); n   if (m % n == 0) n     return n; n   else n     return gcd(n, m % n); n } n bool is_co_prim(int a, int b) { return gcd(a, b) == 1; } n int get(int a, int b) { n   int m = std::min(a, b); n   int n = std::max(a, b); n   for (int i = m + 1;; i++) { n     if (i != n && gcd(a, i) == 1 && gcd(b, i) == 1) { n       return i; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   int cnt = 0; n   for (int i = 0; i < n - 1; i++) { n     if (is_co_prim(a[i], a[i + 1])) { n       ; n     } else { n       res[cnt].pos = i + 1; n       res[cnt].val = 1; n       cnt++; n     } n   } n   cout << cnt << endl; n   int now = 0; n   for (int i = 0; i < cnt; i++) { n     while (now != res[i].pos) { n       cout << a[now] <<    ; n       now++; n     } n     cout << res[i].val <<    ; n     now = res[i].pos; n   } n   while (now < n) { n     cout << a[now] <<    ; n     now++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, t; n bool ans = 0; n vector<int> port; n vector<bool> f; n void dfs(int i) { n   if (i == t - 1) { n     ans = 1; n     return; n   } n   f[i] = 1; n   if (i != n - 1 && !f[i + port[i]]) dfs(i + port[i]); n } n int main() { n   cin >> n >> t; n   port.resize(n - 1); n   f.resize(n); n   for (int i = 0; i < n - 1; ++i) { n     cin >> port[i]; n   } n   dfs(0); n   cout << (ans ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( omit-frame-pointer ) n #pragma GCC optimize( unroll-loops ) n const int MAXINT = 2147483640; n const long long MAXLL = 9223372036854775800LL; n const long long MAXN = 1000000; n const double pi = 3.1415926535897932384626433832795; n using namespace std; n int kol[300000]; n int x1[300000]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   srand(time(0)); n   int n, mn = 1e9; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x1[i]; n     mn = min(x1[i], mn); n   } n   for (int i = 1; i <= n; i++) { n     x1[i] -= mn; n     kol[x1[i]]++; n   } n   if (kol[2] == 0) { n     cout << n << endl; n     for (int i = 1; i <= n; i++) cout << x1[i] + mn <<    ; n     cout << endl; n     return 0; n   } n   int kol1 = kol[0]; n   int kol2 = kol[1]; n   int kol3 = kol[2]; n   int ans = n; n   if (kol2 / 2 * 2 >= min(kol1, kol3) * 2) { n     ans -= kol2 / 2 * 2; n     kol1 += kol2 / 2; n     kol3 += kol2 / 2; n     kol2 -= kol2 / 2 * 2; n   } else { n     int kek = min(kol1, kol3); n     ans -= kek * 2; n     kol2 += kek * 2; n     kol1 -= kek; n     kol3 -= kek; n   } n   cout << ans << endl; n   for (int i = 1; i <= kol1; i++) cout << mn <<    ; n   for (int i = 1; i <= kol2; i++) cout << mn + 1 <<    ; n   for (int i = 1; i <= kol3; i++) cout << mn + 2 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base ::sync_with_stdio(false); n   int n; n   cin >> n; n   int pass = 0, ans = 0; n   while (n) { n     ans += n; n     ans += (pass * (n - 1)); n     pass++; n     --n; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   string a; n   cin >> n >> a; n   stack<char> q; n   int m = 1; n   int len = 0; n   int he = -1; n   for (int i = 0; i < n; i++) { n     if (q.empty() || q.top() == a[i]) { n       q.push(a[i]); n       len++; n       m += 2; n     } else { n       if (a[i] != a[i - 1]) len += 3; n       if (a[i] ==  [  && a[i - 1] ==  ] ) len += 4; n       q.pop(); n       len++; n       m -= 2; n     } n     he = max(he, m); n   } n   vector<vector<char>> res(len, vector<char>(he,    )); n   m = 0; n   int cur = 0; n   for (int i = 0; i < n; i++) { n     if (i != 0 && a[i] == a[i - 1] && a[i] ==  [ ) n       m++; n     else if (i != 0 && a[i] == a[i - 1] && a[i] ==  ] ) n       m--; n     if (a[i] ==  [ ) { n       res[cur][m] =  + ; n       res[cur][he - m - 1] =  + ; n       res[cur + 1][m] =  - ; n       res[cur + 1][he - m - 1] =  - ; n       for (int j = m + 1; j < he - m - 1; j++) res[cur][j] =  | ; n     } else { n       if (a[i] ==  ]  && a[i - 1] ==  [ ) cur += 3; n       res[cur - 1][m] =  - ; n       res[cur - 1][he - m - 1] =  - ; n       res[cur][m] =  + ; n       res[cur][he - m - 1] =  + ; n       for (int j = m + 1; j < he - m - 1; j++) res[cur][j] =  | ; n     } n     cur++; n   } n   for (int i = 0; i < he; i++) { n     for (int j = 0; j < len; j++) cout << res[j][i]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2000 + 10; n const int MOD = (int)(1e9) + 7; n vector<int> adj[MAXN]; n int a[MAXN], f[MAXN]; n bool visited[MAXN]; n int d, n; n void DFS(int u, int root) { n   visited[u] = true; n   f[u] = 1; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (!visited[v]) { n       if ((a[v] < a[root]) || (a[v] > a[root] + d)) continue; n       if ((a[v] == a[root]) && (v < root)) continue; n       DFS(v, root); n       f[u] = ((long long)(f[u]) * (f[v] + 1)) % MOD; n     } n   } n } n int main() { n   cin >> d >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n - 1; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   int result = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) f[j] = 0, visited[j] = false; n     DFS(i, i); n     result = (result + f[i]) % MOD; n   } n   cout << result << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const int INF = 1000000007; n const int MOD = 1000000007; n const int MAXN = 100055; n int dx[] = {2, 1, -1, -2, -1, 1, 2, 1, -1}; n int dy[] = {0, 2, 2, 0, -2, -2, 0, 2, 2}; n int main() { n   long long n; n   while (cin >> n) { n     n++; n     if (n == 1) { n       cout <<  0 0  << endl; n       continue; n     } n     long long L = (n - 1 + 2) / 3; n     L = sqrt(L + 0.5); n     while (3 * L * (L + 1) + 1 < n) ++L; n     n -= 3 * L * (L - 1) + 1; n     int side = (n - 1) / L; n     n -= L * side; n     long long ansx = dx[side] * L + dx[side + 2] * n; n     long long ansy = dy[side] * L + dy[side + 2] * n; n     cout << ansx <<     << ansy << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int s, e, d; n }; n int a[1010]; n int in[1010]; n int out[1010]; n int vis[1010]; n int d[1010][1010]; n int n, p; n int cnt = 0; n int ans; n Node node[1010]; n void dfs(int x) { n   if (!out[x]) { n     node[cnt].e = x; n     node[cnt].d = ans; n     return; n   } else { n     ans = min(ans, d[x][a[x]]); n     dfs(a[x]); n   } n } n int cmp(Node a, Node b) { return a.s < b.s; } n int main() { n   scanf( %d %d , &n, &p); n   cnt = 0; n   for (int i = 0; i < p; i++) { n     int ta, tb, tc; n     scanf( %d %d %d , &ta, &tb, &tc); n     a[ta] = tb; n     out[ta]++; n     in[tb]++; n     d[ta][tb] = tc; n     vis[ta] = vis[tb] = 1; n   } n   for (int i = 1; i <= n && p != 0; i++) { n     if (in[i] == 0 && vis[i]) { n       ans = 1 << 30; n       node[cnt].s = i; n       dfs(i); n       cnt++; n     } n   } n   sort(node, node + cnt, cmp); n   printf( %d n , cnt); n   for (int i = 0; i < cnt; i++) { n     printf( %d %d %d n , node[i].s, node[i].e, node[i].d); n   } n } n


#include <bits/stdc++.h> n template <class T> n bool setmax(T &_a, T _b) { n   if (_b > _a) { n     _a = _b; n     return true; n   } n   return false; n } n template <class T> n bool setmin(T &_a, T _b) { n   if (_b < _a) { n     _a = _b; n     return true; n   } n   return false; n } n const int N = 200010; n int a[N], ox[N], oy[N], ans[N]; n int n, m; n int dfs(int k, int l1, int l2) { n   setmin(l2, ox[k]); n   int r1 = std::max(l1 + 1, oy[k]), r2 = l2 + 4; n   if (l1 > l2 || r1 > r2) return -1; n   int p = dfs(k + 1, r1 + 1, r2 + 1) - 1; n   if (p > 0) { n     int q = std::max(l1, p - 4); n     for (int i = int(q); i <= int(p); ++i) ans[i] = k; n     return q; n   } n   return (l1 <= n + 1 && l2 >= n + 1 && k > m ? n + 1 : -1); n } n bool solve() { n   std::fill(ox, ox + N, n + 1); n   std::fill(oy, oy + N, -1); n   m = 0; n   for (int i = int(1); i <= int(n); ++i) n     if (a[i] > 0) { n       setmin(ox[a[i]], i); n       setmax(oy[a[i]], i); n       setmax(m, a[i]); n     } n   return dfs(1, 1, 1) > 0; n } n int main() { n   scanf( %d , &n); n   for (int i = int(1); i <= int(n); ++i) scanf( %d , &a[i]); n   if (solve()) { n     printf( %d n , ans[n]); n     for (int i = int(1); i <= int(n); ++i) printf( %d  , ans[i]); n     printf(  n ); n   } else { n     printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 2e5 + 5; n using namespace std; n struct info { n   int coord; n   bool taxi; n } v[MAXN]; n int n, m, dist[MAXN], man[MAXN], fr[MAXN]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n + m; i++) { n     cin >> v[i].coord; n     dist[i] = 2e9; n   } n   for (int i = 1; i <= n + m; i++) cin >> v[i].taxi; n   int cur = -1; n   for (int i = 1; i <= n + m; i++) { n     if (v[i].taxi) cur = i; n     if (!v[i].taxi && cur != -1) { n       dist[i] = v[i].coord - v[cur].coord; n       man[i] = cur; n     } n   } n   cur = -1; n   for (int i = n + m; i >= 1; i--) { n     if (v[i].taxi) cur = i; n     if (!v[i].taxi && cur != -1) { n       if (v[cur].coord - v[i].coord < dist[i]) { n         dist[i] = v[cur].coord - v[i].coord; n         man[i] = cur; n       } n     } n   } n   for (int i = 1; i <= n + m; i++) n     if (!v[i].taxi) fr[man[i]]++; n   for (int i = 1; i <= n + m; i++) { n     if (v[i].taxi) cout << fr[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAX = 1000010; n const long long MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n const long long LLINF = 0x3f3f3f3f3f3f3f3f; n const long double EPS = 1e-6; n const long double PI = acos(-1.0); n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, x, y; n   cin >> n; n   cout <<  YES n ; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     cin >> x >> y; n     if (x & 1) { n       if (y & 1) n         cout << 1 <<   n ; n       else n         cout << 2 <<   n ; n     } else { n       if (y & 1) n         cout << 3 <<   n ; n       else n         cout << 4 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int rd() { n   register int ans = 0, flag = 1; n   register char c = getchar(); n   for (; c >  9  || c <  0 ; c = getchar()) n     if (c ==  - ) flag = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) ans = ans * 10 -  0  + c; n   return ans * flag; n } n const int MAXN = 1e5 + 10; n int a[MAXN]; n int ans[MAXN]; n int tmp[MAXN]; n int _[20][MAXN]; n void dfs(int now, int n) { n   if (n < 2 && _[now][1] == 0) { n     if (tmp[0] < ans[0]) { n       ans[0] = tmp[0]; n       for (register int i = 1; i <= tmp[0]; ++i) { n         ans[i] = tmp[i]; n       } n     } n     return; n   } n   if (now > 18 || tmp[0] >= ans[0]) { n     return; n   } n   for (register int I = 1; I <= n; ++I) { n     if (_[now][I] & 1) { n       for (register int i = 1; i <= n; ++i) { n         _[now + 1][i] = (_[now][i] - (_[now][i] & 1)) >> 1; n       } n       tmp[++tmp[0]] = (1 << now); n       register int nn = n           unique(_[now + 1] + 1, _[now + 1] + n + 1) - _[now + 1] - 1; n       dfs(now + 1, nn); n       --tmp[0]; n       for (register int i = 1; i <= n; ++i) { n         _[now + 1][i] = (_[now][i] + (_[now][i] & 1)) >> 1; n       } n       tmp[++tmp[0]] = -(1 << now); n       nn = unique(_[now + 1] + 1, _[now + 1] + n + 1) - _[now + 1] - 1; n       dfs(now + 1, nn); n       --tmp[0]; n       return; n     } n   } n   for (register int i = 1; i <= n; ++i) { n     _[now + 1][i] = _[now][i] >> 1; n   } n   n = unique(_[now + 1] + 1, _[now + 1] + n + 1) - _[now + 1] - 1; n   dfs(now + 1, n); n } n int main() { n   register int n = rd(); n   for (register int i = 1; i <= n; ++i) { n     a[i] = rd(); n   } n   sort(a + 1, a + n + 1); n   n = unique(a + 1, a + n + 1) - a - 1; n   ans[0] = 0x3f3f3f3f; n   for (register int i = 1; i <= n; ++i) { n     _[0][i] = a[i]; n   } n   dfs(0, n); n   printf( %d n , ans[0]); n   for (register int i = 1; i <= ans[0]; ++i) { n     printf( %d  , ans[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.1415926535897932384626433832795; n const long double EPS = 1e-11; n int n, m, first, second; n bool a[110][110]; n pair<long double, int> find_k(int s, int e) { n   queue<int> q; n   int ur[110]; n   long double kr[110]; n   memset(ur, 0xFF, sizeof(ur)); n   memset(kr, 0, sizeof(kr)); n   q.push(s); n   ur[s] = 1; n   kr[s] = 1.0; n   int p; n   while (!q.empty()) { n     p = q.front(); n     q.pop(); n     for (int i = 0; i < (n); i++) n       if ((a[p][i]) && (ur[i] + 1 == ur[p])) kr[p] += kr[i]; n     for (int i = 0; i < (n); i++) n       if ((a[p][i]) && (ur[i] == -1)) { n         ur[i] = ur[p] + 1; n         q.push(i); n       } n   } n   return make_pair(kr[e], ur[e] - 1); n } n int main() { n   cout << setiosflags(ios::fixed) << setprecision(10); n   cin >> n >> m; n   for (int i = 0; i < (m); i++) { n     cin >> first >> second; n     first--; n     second--; n     a[first][second] = 1; n     a[second][first] = 1; n   } n   pair<long double, int> k = find_k(0, n - 1); n   if (k.first == 0) cout <<  LOLO ; n   long double ans = 1.0; n   for (int i = 1; i < n - 1; i++) { n     pair<long double, int> k1 = find_k(0, i), k2 = find_k(i, n - 1); n     if (k1.second + k2.second == k.second) n       ans = max(ans, 2.0 * k1.first * k2.first / k.first); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long d[40000]; n char s[4005]; n void input() { n   int i, j, len, a, sum; n   long long res; n   scanf( %d %s , &a, &s); n   len = strlen(s); n   for ((i) = 0; (i) < (len); (i)++) { n     sum = 0; n     for ((j) = (i); (j) <= (len - 1); (j)++) { n       sum += s[j] -  0 ; n       d[sum]++; n     } n   } n   if (a == 0) { n     res = d[0] * len * (len + 1) - d[0] * d[0]; n     printf( %I64d n , res); n     return; n   } n   res = 0; n   for ((i) = (9 * len); (i) >= (1); (i)--) n     if (a % i == 0 && a / i <= 36000) res += d[i] * d[a / i]; n   printf( %I64d , res); n } n int main() { n   input(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1000005], m, M, N, sum; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i), M = max(a[i], M); n   for (int i = 1; i <= n; ++i) sum += M - a[i]; n   if (m <= sum) n     printf( %d %d n , M, M + m); n   else n     printf( %d %d n , (m - sum + n - 1) / n + M, M + m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const long long INF = 1000 * 1000 * 1000 + 7; n const long long LINF = INF * (long long)INF; n const int MAX = 100 + 47; n int A[MAX]; n vector<int> B; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = (0); i < (n); i++) cin >> A[i]; n   int cnt = 0; n   int vid = 0; n   for (int i = (0); i < (n + 1); i++) { n     if ((A[i] < 0 && vid == 2) || i == n) { n       B.push_back(cnt); n       cnt = 1; n       vid = 1; n       continue; n     } n     cnt++; n     if (A[i] < 0) vid++; n   } n   cout << (int)B.size() << endl; n   for (int i = (0); i < ((int)B.size()); i++) { n     cout << B[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   std::cout.tie(nullptr); n   int tc; n   std::cin >> tc; n   while (tc--) { n     long long x, y; n     std::cin >> x >> y; n     if ((x == 1 || x == 3) && y > x) { n       std::cout <<  NO n ; n     } else if (x == 2 && y > 3) { n       std::cout <<  NO n ; n     } else { n       std::cout <<  YES n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int n; n   int k; n   int M; n   scanf( %d%d%d , &n, &k, &M); n   int number[45][45] = {0}; n   for (int i = 0; i < k; i++) { n     int temp; n     scanf( %d , &temp); n     for (int j = 0; j < n; j++) { n       number[j][i] = temp; n     } n   } n   for (int i = 0; i < n; i++) { n     std::sort(number[i], number[i] + k); n   } n   int ptrAddR = 0; n   int ptrAddL = 0; n   int ptrDelR = 0; n   int ptrDelL = -1; n   int lockR = 0; n   int time = M; n   int score = 0; n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < n; j++) { n       if (time >= number[j][i]) { n         time -= number[j][i]; n         ptrDelL = j; n         ptrDelR = i; n       } else { n         goto FAST_JUMP; n       } n     } n   } n FAST_JUMP: n   if (ptrDelL == n) { n     score = (ptrDelR - 1 + 1) * n; n   } else { n     score = (ptrDelR - 1 + 1) * n + ptrDelL + 1; n   } n   if (ptrDelR == k - 1) { n     lockR = ptrDelL + 1; n     ptrAddL = lockR; n     ptrAddR = k - 1; n     score += lockR; n   } else { n     ptrAddR = ptrDelR + 1; n     ptrAddL = 0; n   } n   if (ptrDelL <= lockR) { n     ptrDelL = n - 1; n     ptrDelR--; n   } n   int warning = 0; n   int tptrAddR = ptrAddR; n   int tptrAddL = ptrAddL; n   int tptrDelR = ptrDelR; n   int tptrDelL = ptrDelL; n   int tlockR = lockR; n   int flag = 0; n   while (!(lockR >= n - 1 || ptrDelR < 0 || warning >= 2)) { n     if (tptrDelL <= tlockR) { n       tptrDelL = n - 1; n       tptrDelR--; n     } n     time += number[tptrDelL][tptrDelR]; n     tptrDelL--; n     if (tptrDelL <= tlockR) { n       tptrDelL = n - 1; n       tptrDelR--; n     } n     flag = -1; n     if (tptrAddR > k - 1) { n       tptrAddR = tptrDelR + 1; n       tptrAddL++; n       tlockR++; n     } n     if (tptrAddR > k - 1 || tptrAddL > n - 1) { n       break; n     } n     if (time >= number[tptrAddL][tptrAddR]) { n       time -= number[tptrAddL][tptrAddR]; n       flag = 0; n       tptrAddR++; n       if (tptrAddR > k - 1) { n         tptrAddR = tptrDelR + 1; n         tptrAddL++; n         tlockR++; n         flag = 1; n       } n     } else { n     } n     warning -= flag; n     if (warning <= 0) { n       score -= warning; n       warning = 0; n       ptrAddL = tptrAddL; n       ptrAddR = tptrAddR; n       ptrDelL = tptrDelL; n       ptrDelR = tptrDelR; n       lockR = tlockR; n     } n   } n   printf( %d , score); 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, m; n     cin >> n >> m; n     if (n % m == 0) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<bool> hero(n); n   vector<long long> v(n); n   vector<int> pos_num(n); n   for (int i = 0; i < m; i++) { n     int s, h; n     cin >> s >> h; n     hero[--s] = 1; n     pos_num[s] = i; n     v[s] = h; n   } n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (!hero[i]) v[i] = x; n   } n   for (int cell = 0; cell < n; cell++) { n     vector<int> order; n     vector<long long> v_; n     vector<bool> hero_; n     for (bool x : hero) hero_.push_back(x); n     for (long long x : v) v_.push_back(x); n     for (int dum = 0; dum < m; dum++) { n       vector<bool> can(n); n       for (int i = 0; i < n; i++) n         if (hero_[i]) { n           int inc = i <= cell ? 1 : -1; n           long long sum = 0; n           int idx = i; n           bool ok = 1; n           while (1) { n             if (idx == i || !hero_[idx]) sum += v_[idx]; n             if (sum < 0) ok = 0; n             if (idx == cell) break; n             idx += inc; n           } n           if (ok) can[i] = 1; n         } n       int idx = -1; n       for (int i = 0; i < cell + 1; i++) { n         if (can[i]) { n           idx = i; n           break; n         } n       } n       for (int i = n - 1; i >= cell; i--) n         if (can[i]) { n           idx = i; n           break; n         } n       if (idx == -1) break; n       int inc = idx <= cell ? 1 : -1; n       order.push_back(pos_num[idx]); n       hero_[idx] = 0; n       while (1) { n         v_[idx] = 0; n         if (idx == cell) break; n         idx += inc; n       } n     } n     if (order.size() == m) { n       cout << cell + 1 <<   n ; n       for (int i = 0; i < m; i++) { n         if (i) cout <<    ; n         cout << order[i] + 1; n       } n       cout <<   n ; n       return 0; n     } n   } n   cout << -1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, INF = 1e9 + 7; n int n, a[N]; n bool used[N]; n int main() { n   int q; n   cin >> q; n   while (q--) { n     cin >> n; n     for (int i = 1; i <= n * 2; i++) { n       cin >> a[i]; n       used[i] = 0; n     } n     vector<pair<int, int> > vec; n     for (int i = 1; i <= n * 2; i++) { n       if (used[i]) continue; n       for (int j = i + 1; j <= n * 2; j++) { n         if ((a[i] + a[j]) % 2 == 0) { n           used[j] = 1; n           used[i] = 1; n           vec.push_back({i, j}); n           break; n         } n       } n     } n     bool cond = 1; n     for (int i = 1; i <= n * 2; i++) { n       if (!used[i]) { n         cond = 0; n       } n     } n     for (int i = 0; i < vec.size() - cond; i++) { n       cout << vec[i].first <<     << vec[i].second << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, q, m; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   cin >> q; n   while (q--) { n     cin >> m; n     cout << upper_bound(a, a + n, m) - a << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long t; n   cin >> t; n   while (t--) { n     long long a, b; n     cin >> a >> b; n     cout << min(max(2 * a, b) * max(2 * a, b), max(a, 2 * b) * max(a, 2 * b)) n          << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b; n int k; n int main() { n   cin >> a >> b; n   if (b > a) { n     cout <<  -1 n ; n     return 0; n   } n   if (a == b) { n     cout << a << endl; n     return 0; n   } n   k = a / b; n   if (k & 1) k++; n   double ans = (a + b) * 1.0 / k; n   printf( %.10f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cas = 1, T; n int n, a[10010], b[10010]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + 1 + n); n   int l = 1, r = (n + 1) / 2 + 1; n   for (int i = 1; i <= n; i++) { n     if (i % 2 == 1) n       printf( %d  , a[l++]); n     else n       printf( %d  , a[r++]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Mod = 1e9 + 7; n int add(int a, int b) { return a + b >= Mod ? a + b - Mod : a + b; } n int dec(int a, int b) { return a - b < 0 ? a - b + Mod : a - b; } n int mul(int a, int b) { return 1ll * a * b % Mod; } n void Add(int &a, int b) { a = add(a, b); } n void Dec(int &a, int b) { a = dec(a, b); } n void Mul(int &a, int b) { a = mul(a, b); } n const int N = 1e5 + 50; n int n, a[N], pr[N], sf[N]; n map<int, int> bin; n struct lll { n   int a, b, c, bc, ac; n }; n lll operator+(const lll &x, const lll &y) { n   return (lll){add(x.a, y.a), add(x.b, y.b), add(x.c, y.c), add(x.bc, y.bc), n                add(x.ac, y.ac)}; n } n namespace SGT { n const int N = ::N << 2; n lll t[N]; n int tb[N]; n int tc[N]; n int tbc[N]; n int len[N]; n void up(int x) { t[x] = t[x << 1] + t[x << 1 | 1]; } n void build(int x, int l, int r) { n   len[x] = r - l + 1; n   if (l == r) return t[x].a = l - 1, void(); n   build(x << 1, l, ((l + r) >> 1)); n   build(x << 1 | 1, ((l + r) >> 1) + 1, r); n   up(x); n } n void B(int x, int v) { n   Add(tb[x], v); n   Add(t[x].b, mul(len[x], v)); n   Add(t[x].bc, mul(v, t[x].c)); n   Add(tbc[x], mul(v, tc[x])); n } n void C(int x, int v) { n   Add(tc[x], v); n   Add(t[x].c, mul(len[x], v)); n   Add(t[x].bc, mul(v, t[x].b)); n   Add(tbc[x], mul(v, tb[x])); n   Add(t[x].ac, mul(v, t[x].a)); n } n void BC(int x, int v) { n   Add(t[x].bc, v); n   Add(tbc[x], v); n } n void down(int x) { n   if (tb[x]) B(x << 1, tb[x]), B(x << 1 | 1, tb[x]), tb[x] = 0; n   if (tc[x]) C(x << 1, tc[x]), C(x << 1 | 1, tc[x]), tc[x] = 0; n   if (tbc[x]) BC(x << 1, tbc[x]), BC(x << 1 | 1, tbc[x]), tbc[x] = 0; n } n void mdfb(int x, int l, int r, int L, int R, int v) { n   if (L <= l && r <= R) return B(x, v); n   down(x); n   if (L <= ((l + r) >> 1)) mdfb(x << 1, l, ((l + r) >> 1), L, R, v); n   if (R > ((l + r) >> 1)) mdfb(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R, v); n   up(x); n } n void mdfc(int x, int l, int r, int L, int R, int v) { n   if (L <= l && r <= R) return C(x, v); n   down(x); n   if (L <= ((l + r) >> 1)) mdfc(x << 1, l, ((l + r) >> 1), L, R, v); n   if (R > ((l + r) >> 1)) mdfc(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R, v); n   up(x); n } n lll ask(int x, int l, int r, int L, int R) { n   if (L > R) return (lll){0}; n   if (L <= l && r <= R) return t[x]; n   down(x); n   if (R <= ((l + r) >> 1)) return ask(x << 1, l, ((l + r) >> 1), L, R); n   if (L > ((l + r) >> 1)) return ask(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R); n   return ask(x << 1, l, ((l + r) >> 1), L, R) + n          ask(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R); n } n }  // namespace SGT n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) { n     pr[i] = bin[a[i]]; n     bin[a[i]] = i; n   } n   bin.clear(); n   for (int i = n; i; i--) { n     sf[i] = bin[a[i]]; n     bin[a[i]] = i; n     if (!sf[i]) sf[i] = n + 1; n   } n   SGT ::build(1, 1, n); n   static int s1[N], s2[N]; n   int t1 = 0, t2 = 0, mx = 0, ans = 0; n   for (int i = 1; i <= n; i++) { n     mx = max(mx, pr[i]); n     while (t1 && pr[s1[t1]] <= pr[i]) { n       int h = dec(pr[i], pr[s1[t1]]); n       SGT ::mdfb(1, 1, n, s1[t1 - 1] + 1, s1[t1], h); n       --t1; n     } n     while (t2 && sf[s2[t2]] >= sf[i]) { n       int h = dec(sf[i], sf[s2[t2]]); n       SGT ::mdfc(1, 1, n, s2[t2 - 1] + 1, s2[t2], h); n       --t2; n     } n     s1[++t1] = i, s2[++t2] = i; n     SGT ::mdfb(1, 1, n, i, i, pr[i]); n     SGT ::mdfc(1, 1, n, i, i, sf[i]); n     lll z = SGT ::ask(1, 1, n, mx + 1, i); n     Dec(ans, z.bc); n     Dec(ans, mul(i + 1, z.a)); n     Add(ans, mul(i + 1, z.b)); n     Add(ans, z.ac); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void showvec(vector<long long> &arr); n inline void solve() { n   long long n; n   cin >> n; n   vector<long long> arr(n); n   for (long long i = 0; i < n; i++) { n     cin >> arr[i]; n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   vector<long long> arr(200005); n   long long t = 1; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     fill(arr.begin(), arr.end(), -1); n     long long ans = INT_MAX; n     for (long long i = 0; i < n; i++) { n       long long ele; n       cin >> ele; n       if (arr[ele] != -1) { n         ans = min(ans, i - arr[ele] + 1); n       } n       arr[ele] = i; n     } n     cout << (ans == INT_MAX ? -1 : ans); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long int i, l, n, m, t; n   cin >> n >> m; n   long int a[m + 1]; n   for (i = 0; i < m; i++) cin >> a[i]; n   sort(a, a + m); n   bool flag = true; n   if (a[0] == 1) flag = false; n   if (a[m - 1] == n) flag = false; n   for (i = 0; i < m - 2; i++) n     if (a[i] == a[i + 1] - 1 && a[i + 1] + 1 == a[i + 2]) flag = false; n   if (!flag) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct my { n   int num; n   int dmg; n } dmg[10010]; n int mx, reg, rg[10010], n; n int flag[10010]; n vector<pair<int, int> > v; n bool operator<(my a, my b) { n   if (a.dmg > b.dmg) return 1; n   return 0; n } n int main() { n   cin >> n >> mx >> reg; n   for (int i = 0; i < n; i++) { n     cin >> rg[i] >> dmg[i].dmg; n     dmg[i].num = i; n   } n   int time = 0, inds, kols = 0, fl = 0; n   int total_dmg = 0, cur = mx; n   sort(dmg, dmg + n); n   while (1) { n     cur += (reg - total_dmg); n     if (cur > mx) cur = mx; n     if (cur <= 0) { n       cout <<  YES n ; n       cout << time <<     << kols <<   n ; n       for (int i = 0; i < (int)v.size(); i++) n         cout << v[i].first <<     << v[i].second <<   n ; n       return 0; n     } n     inds = -1; n     for (int i = 0; i < n; i++) { n       if (!flag[i]) { n         if (cur * 100 <= rg[dmg[i].num] * mx) { n           inds = i; n           break; n         } n       } n     } n     if (inds != -1) { n       total_dmg += dmg[inds].dmg; n       flag[inds] = 1; n       kols++; n       v.push_back(make_pair(time, dmg[inds].num + 1)); n     } else { n       if (reg >= total_dmg) { n         cout <<  NO n ; n         return 0; n       } n     } n     time++; n   } n   return 0; n } n


#include <bits/stdc++.h> n bool isvowel(char s) { n   if (s ==  a  || s ==  A  || s ==  e  || s ==  E  || s ==  i  || s ==  I  || n       s ==  o  || s ==  O  || s ==  u  || s ==  U ) n     return true; n   else n     return false; n } n long long fact(long long n) { n   long long mul = 1; n   for (long long i = 1; i <= n; i++) mul = mul * i; n   return mul; n } n long long gcd(long long a, long long b) { n   return b == 0 ? a : gcd(b % 1000000007, (a % b) % 1000000007); n } n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n & 1) n     cout <<  -1  n          <<   n ; n   else { n     for (int i = 1; i <= n; i += 2) { n       cout << i + 1 <<     << i <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 mt_rand( n     chrono::high_resolution_clock::now().time_since_epoch().count()); n const int INF = 2e9; n const long long ML = 4e18; n long long Pow(long long x, int p) { n   long long res = 1; n   while (p) { n     if (p & 1) res = (res * x) % 998244353; n     x = (x * x) % 998244353; n     p >>= 1; n   } n   return res; n } n void solve() { n   int n, q; n   cin >> n >> q; n   vector<int> ax(n); n   for (int i = (0); i < (n); ++i) cin >> ax[i]; n   sort((ax).begin(), (ax).end()); n   vector<long long> sum(n + 1, 0); n   for (int i = (1); i < (n + 1); ++i) { n     sum[i] = sum[i - 1] + ax[i - 1]; n     sum[i] %= 998244353; n   } n   long long inv = Pow(n, 998244353 - 2); n   for (int i = (0); i < (q); ++i) { n     int a, b; n     cin >> a >> b; n     int p = lower_bound((ax).begin(), (ax).end(), b) - ax.begin(); n     int big = n - p, small = p; n     if (big < a) { n       cout << 0 << endl; n       continue; n     } else { n       long long bs = (sum[n] - sum[p] + 998244353) % 998244353, sm = sum[p]; n       long long ans = n           bs * (big - a) % 998244353 * Pow(big, 998244353 - 2) % 998244353; n       ans = (ans + sm * (big + 1 - a) % 998244353 * n                        Pow(big + 1, 998244353 - 2) % 998244353) % n             998244353; n       cout << ans << endl; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n const long long MOD = 1e9 + 7; n const long long MAXN = 1e6 + 1; n using namespace std; n long long readInt() { n   bool minus1 = false; n   long long result = 0; n   char ch; n   ch = getchar(); n   while (true) { n     if (ch ==  - ) break; n     if (ch >=  0  && ch <=  9 ) break; n     ch = getchar(); n   } n   if (ch ==  - ) n     minus1 = true; n   else n     result = ch -  0 ; n   while (true) { n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n     result = result * 10 + (ch -  0 ); n   } n   if (minus1) n     return -result; n   else n     return result; n } n const int N = 3333; n int dx[] = {+1, -1, 0, 0}; n int dy[] = {0, 0, +1, -1}; n string dirs =  UDRL ; n int directiony[N]; n int directionx[N]; n long long curx = 5000000001LL, cury = 5000000001LL; n vector<long long> xler; n vector<long long> yler; n void add() { n   xler.push_back(curx); n   xler.push_back(curx - 1); n   xler.push_back(curx + 1); n   yler.push_back(cury); n   yler.push_back(cury - 1); n   yler.push_back(cury + 1); n } n int used[N][N]; n int H, W; n void dfs(int x, int y) { n   used[x][y] = 2; n   for (int i = 0; i < 4; i++) { n     int tmpx = x + dx[i]; n     int tmpy = y + dy[i]; n     if ((0 <= tmpx && tmpx < H) && (0 <= tmpy && tmpy < W) && n         used[tmpx][tmpy] == 0) { n       dfs(tmpx, tmpy); n     } n   } n } n int main() { n   int n = readInt(); n   add(); n   for (int i = 0; i < n; i++) { n     char x; n     int y; n     cin >> x >> y; n     for (int j = 0; j < 4; j++) { n       if (dirs[j] == x) { n         directionx[i] = dx[j] * y; n         directiony[i] = dy[j] * y; n         curx += directionx[i]; n         cury += directiony[i]; n         add(); n       } n     } n   } n   sort(xler.begin(), xler.end()); n   sort(yler.begin(), yler.end()); n   xler.resize(unique(xler.begin(), xler.end()) - xler.begin()); n   yler.resize(unique(yler.begin(), yler.end()) - yler.begin()); n   H = xler.size(); n   W = yler.size(); n   curx = 5000000001LL; n   cury = 5000000001LL; n   long long compx = lower_bound(xler.begin(), xler.end(), curx) - xler.begin(); n   long long compy = lower_bound(yler.begin(), yler.end(), cury) - yler.begin(); n   for (int i = 0; i < n; i++) { n     long long tmpx = curx + directionx[i]; n     long long tmpy = cury + directiony[i]; n     long long tmpcompx = n         lower_bound((xler).begin(), (xler).end(), tmpx) - xler.begin(); n     long long tmpcompy = n         lower_bound((yler).begin(), (yler).end(), tmpy) - yler.begin(); n     for (int j = min(tmpcompx, compx); j <= max(tmpcompx, compx); j++) { n       for (int k = min(tmpcompy, compy); k <= max(tmpcompy, compy); k++) { n         used[j][k] = 1; n       } n     } n     curx = tmpx; n     cury = tmpy; n     compx = tmpcompx; n     compy = tmpcompy; n   } n   dfs(0, 0); n   long long res = 0; n   for (int i = 0; i < H; i++) { n     for (int j = 0; j < W; j++) { n       if (used[i][j] != 2) { n         res += (xler[i + 1] - xler[i]) * (yler[j + 1] - yler[j]); n       } n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n const long long MAX_N = 3e5 + 10; n const long long MOD = 1e9 + 9; n using namespace std; n vector<int> g1[MAX_N]; n vector<int> g[MAX_N]; n vector<int> current; n int p[MAX_N]; n int mark[MAX_N]; n int d[MAX_N]; n int h[MAX_N]; n int n, m, k; n void rt(int v) { n   mark[v]++; n   for (auto x : g1[v]) { n     if (!mark[x]) { n       p[x] = v; n       h[x] = h[v] + 1; n       g[x].push_back(v); n       g[v].push_back(x); n       rt(x); n     } n   } n } n void solve() { n   cout <<  CYCLES n ; n   int ct = 0; n   for (int i = 2; i <= n; ++i) { n     int v = i; n     if (g[v].size() > 1) continue; n     ct++; n     pair<int, int> pr; n     int res = 1e9; n     vector<pair<int, int>> vc; n     for (auto x : g1[v]) { n       if (x == p[v]) continue; n       if ((h[v] - h[x]) % 3 != 2 and h[v] - h[x] + 1 < res) { n         res = h[v] - h[x] + 1; n         pr.second = v; n         pr.first = x; n       } n       vc.push_back({h[x], x}); n     } n     sort(vc.begin(), vc.end()); n     for (int i = 0; i < vc.size() - 1; ++i) { n       int len = vc[i + 1].first - vc[i].first + 2; n       if (len % 3 and res > len) { n         res = len; n         pr = {vc[i].second, vc[i + 1].second}; n       } n     } n     int now = pr.second; n     cout << res <<   n ; n     if (pr.second != v) cout << v <<   n ; n     while (1) { n       cout << now <<    ; n       if (now == pr.first) break; n       now = p[now]; n     } n     cout <<   n ; n     if (ct == k) exit(0); n   } n   exit(0); n } n void dfs(int v, int f) { n   for (auto x : g[v]) { n     if (x != f) { n       d[x] = d[v] + 1; n       dfs(x, v); n     } n   } n } n void sfd(int v, int f, int dest) { n   current.push_back(v); n   if (v == dest) { n     cout << current.size() <<   n ; n     for (auto x : current) cout << x <<    ; n     exit(0); n   } n   for (auto x : g[v]) { n     if (x != f) sfd(x, v, dest); n   } n   current.pop_back(); n } n int main() { n   cin >> n >> m >> k; n   if (n == 1) return cout <<  PATH n1 n1 , 0; n   if (n == 2) return cout <<  PATH n2 n1 2 , 0; n   for (int i = 0; i < m; ++i) { n     int u, v; n     scanf( %d%d , &v, &u); n     g1[u].push_back(v); n     g1[v].push_back(u); n   } n   rt(1); n   int now = 0, v = 1; n   for (int i = 1; i <= n; ++i) { n     if (h[i] > now) { n       now = h[i]; n       v = i; n     } n   } n   dfs(v, 0); n   now = 0; n   int best = 0; n   for (int i = 1; i <= n; ++i) { n     if (d[i] > now) { n       now = d[i]; n       best = i; n     } n   } n   if (n / (double)k <= now) { n     cout <<  PATH n ; n     sfd(v, 0, best); n   } else n     solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const int N = 100005; n int a[N * 2], ans[N]; n stack<int> s; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= 2 * n; i++) { n     char op; n     scanf(  %c , &op); n     if (op ==  + ) { n       a[i] = -1; n     } else { n       scanf( %d , &a[i]); n     } n   } n   int t = n; n   for (int i = 2 * n; i >= 1; i--) { n     if (a[i] == -1) { n       if (s.empty()) return puts( NO ), 0; n       ans[t--] = s.top(); n       s.pop(); n     } else { n       if (!s.empty() && a[i] > s.top()) return puts( NO ), 0; n       s.push(a[i]); n     } n   } n   puts( YES ); n   for (int i = 1; i <= n; i++) printf( %d%c , ans[i],    n [i == n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const int N = 1e5 + 10; n int n, m; n int main() { n   scanf( %d%d , &n, &m); n   long long sum1 = 0, sum2 = 0; n   int t = n - n / 2; n   for (int i = 1; i <= n; i++) { n     sum1 += i - 1; n     sum2 += abs(t - i); n   } n   int x, d; n   long long sum3 = 0; n   long long z1 = 0; n   long long z2 = 0; n   while (m--) { n     scanf( %d%d , &x, &d); n     sum3 += x; n     if (d < 0) n       z1 += d * sum2; n     else n       z2 += d * sum1; n   } n   double ans = sum3; n   ans += 1.0 * (z1 + z2) / n; n   printf( %.12f , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n int w[55]; n int n; n int num_teams(int s) { n   map<int, int> cnt; n   for (int i = 0; i < n; ++i) { n     ++cnt[w[i]]; n   } n   int teams = 0; n   for (int i = 0; i < n; ++i) { n     int wt = w[i]; n     if (cnt[wt] == 0) continue; n     if (wt >= s) continue; n     if (cnt[s - wt] == 0) continue; n     if (wt == (s - wt) && cnt[wt] < 2) continue; n     --cnt[wt]; n     --cnt[s - wt]; n     ++teams; n   } n   return teams; n } n void solve() { n   int max_teams = 0; n   for (int s = 2; s <= 100; ++s) { n     max_teams = max(max_teams, num_teams(s)); n   } n   cout << max_teams <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     cin >> n; n     for (int i = 0; i < n; ++i) { n       cin >> w[i]; n     } n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int q = 0; q < t; q++) { n     long long n, m; n     cin >> n >> m; n     if (n % m == 0) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n