#include <bits/stdc++.h> n using namespace std; n template <class T> n void debug(T a, T b) { n   for (; a != b; ++a) cerr << *a <<    ; n   cerr << endl; n } n template <class T> n void chmin(T& a, const T& b) { n   if (a > b) a = b; n } n template <class T> n void chmax(T& a, const T& b) { n   if (a < b) a = b; n } n namespace std { n template <class S, class T> n ostream& operator<<(ostream& out, const pair<S, T>& a) { n   out <<  (  << a.first <<  ,  << a.second <<  ) ; n   return out; n } n }  // namespace std n bool valid[200005]; n struct DomTree { n   static const int MAX_V = 200000; n   int semi[MAX_V], U[MAX_V], idom[MAX_V]; n   int puf[MAX_V], par[MAX_V]; n   int mn[MAX_V]; n   void init() { n     memset(puf, -1, sizeof(puf)); n     for (int i = 0; i < (MAX_V); ++i) mn[i] = i; n   } n   int root(int a) { n     if (puf[a] == -1) return a; n     int r = root(puf[a]); n     if (semi[mn[puf[a]]] < semi[mn[a]]) mn[a] = mn[puf[a]]; n     return puf[a] = r; n   } n   void unite(int a, int b) { puf[b] = a; } n   int eval(int v) { n     root(v); n     return mn[v]; n   } n   int cnt; n   int vs[MAX_V]; n   vector<int> bucket[MAX_V]; n   vector<int> g[MAX_V], rg[MAX_V]; n   void dfs(int v) { n     semi[v] = cnt; n     vs[cnt++] = v; n     for (int u : g[v]) n       if (semi[u] < 0) { n         par[u] = v; n         dfs(u); n       } n   } n   vector<int> g3[200005]; n   int size[200005]; n   int dfs3(int v) { n     if (valid[v]) size[v] = 1; n     for (auto to : g3[v]) { n       size[v] += dfs3(to); n     } n     return size[v]; n   } n   int solve(int n, int r, vector<int>* g_) { n     init(); n     for (int i = 0; i < (n); ++i) { n       g[i] = g_[i]; n       rg[i].clear(); n     } n     for (int i = 0; i < (n); ++i) n       for (auto to : g[i]) { n         rg[to].push_back(i); n       } n     for (int i = 0; i < (n); ++i) semi[i] = -1; n     cnt = 0; n     dfs(r); n     for (int i = n - 1; i > 0; i--) { n       int w = vs[i]; n       for (int v : rg[w]) { n         int u = eval(v); n         chmin(semi[w], semi[u]); n       } n       bucket[vs[semi[w]]].push_back(w); n       for (int v : bucket[par[w]]) U[v] = eval(v); n       bucket[par[w]].clear(); n       unite(par[w], w); n     } n     for (int i = 1; i < n; i++) { n       int w = vs[i]; n       int u = U[w]; n       if (semi[w] == semi[u]) n         idom[w] = semi[w]; n       else n         idom[w] = idom[u]; n     } n     for (int i = 1; i < n; i++) { n       int w = vs[i]; n       idom[w] = vs[idom[w]]; n     } n     idom[r] = -1; n     for (int i = 0; i < (n); ++i) n       if (i != r) g3[idom[i]].push_back(i); n     dfs3(r); n     int res = 0; n     for (int i = 0; i < (n); ++i) n       if (i != r) chmax(res, size[i]); n     return res; n   } n }; n DomTree D; n int n, m, s; n vector<pair<int, int> > g[200005]; n const long long int INF = 1e18; n long long int cost[200005]; n vector<int> g2[200005]; n int main() { n   cin >> n >> m >> s; n   --s; n   for (int i = 0; i < (m); ++i) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     --a; n     --b; n     g[a].push_back({b, c}); n     g[b].push_back({a, c}); n   } n   for (int i = 0; i < (n); ++i) cost[i] = INF; n   priority_queue<pair<long long int, int>, vector<pair<long long int, int> >, n                  greater<pair<long long int, int> > > n       pq; n   pq.push({0, s}); n   cost[s] = 0; n   while (!pq.empty()) { n     pair<long long int, int> cur = pq.top(); n     pq.pop(); n     if (cost[cur.second] < cur.first) continue; n     for (auto e : g[cur.second]) { n       if (cost[e.first] > cur.first + e.second) { n         cost[e.first] = cur.first + e.second; n         pq.push({cost[e.first], e.first}); n       } n     } n   } n   for (int i = 0; i < (n); ++i) n     if (cost[i] < INF) n       for (auto e : g[i]) { n         if (cost[e.first] == cost[i] + e.second) g2[i].push_back(e.first); n       } n   for (int i = 0; i < (n); ++i) n     if (cost[i] < INF) valid[i] = 1; n   for (int i = 0; i < (n); ++i) n     if (cost[i] == INF) g2[s].push_back(i); n   int res = D.solve(n, s, g2); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int n, m, h, t, maxv; n int e[maxn][2]; n set<int> mp[maxn]; n vector<int> g[maxn]; n vector<int> xv, yv; n set<int> comm; n bool check(int x, int y, vector<int> &xv, vector<int> &yv, set<int> &comm) { n   int lx = max(0, (int)(h - xv.size())); n   int ly = max(0, (int)(t - yv.size())); n   if (comm.size() >= lx + ly) { n     printf( YES n ); n     printf( %d %d n , x, y); n     set<int>::iterator it = comm.begin(); n     int mx = min((int)xv.size(), h); n     for (int i = 0; i < mx; i++) printf( %d  , xv[i]); n     for (int i = 0; i < lx; i++) { n       printf( %d  , *it); n       it++; n     } n     printf(  n ); n     int my = min((int)yv.size(), t); n     for (int i = 0; i < my; i++) printf( %d  , yv[i]); n     for (int i = 0; i < ly; i++) { n       printf( %d  , *it); n       it++; n     } n     printf(  n ); n     return true; n   } n   return false; n } n bool solve(int x, int y) { n   if (mp[x].size() <= h || mp[y].size() <= t) return false; n   int cnt = mp[x].size() - 1; n   for (int i = 0; i < g[y].size() && cnt < h + t; i++) { n     int tx = g[y][i]; n     if (tx == x) continue; n     if (mp[x].count(tx) == 0) cnt++; n   } n   if (cnt < h + t) return false; n   xv.clear(); n   yv.clear(); n   comm.clear(); n   for (set<int>::iterator it = mp[x].begin(); n        it != mp[x].end() && xv.size() <= h && comm.size() <= maxv; it++) { n     int ty = *it; n     if (ty == y) continue; n     if (mp[y].find(ty) != mp[y].end()) n       comm.insert(ty); n     else n       xv.push_back(ty); n   } n   for (set<int>::iterator it = mp[y].begin(); n        it != mp[y].end() && yv.size() <= t && comm.size() <= maxv; it++) { n     int tx = *it; n     if (tx == x) continue; n     if (mp[x].find(tx) != mp[x].end()) n       comm.insert(tx); n     else n       yv.push_back(tx); n   } n   if (check(x, y, xv, yv, comm)) return true; n   return false; n } n int main() { n   scanf( %d%d%d%d , &n, &m, &h, &t); n   maxv = h + t; n   for (int i = 1; i <= n; i++) mp[i].clear(); n   for (int i = 0; i < m; i++) { n     int tx, ty; n     scanf( %d%d , &tx, &ty); n     mp[tx].insert(ty); n     mp[ty].insert(tx); n     g[tx].push_back(ty); n     g[ty].push_back(tx); n   } n   int flags = false; n   for (int i = 1; i <= n; i++) n     if (flags == false) n       for (int j = 0; j < g[i].size(); j++) { n         if (solve(i, g[i][j])) { n           flags = true; n           break; n         } n       } n   if (!flags) printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, G, R, d[100127], w[100127], rt, tot = 0, mi[100127 * 20], n                                           c[100127 * 20][2]; n long long f[100127], s[100127], ans; n void change(int &p, int l, int r, int x, int v) { n   if (!p) p = ++tot; n   mi[p] = v; n   if (l == r) return; n   int mid = (1ll * l + r) >> 1; n   (x <= mid) ? change(c[p][0], l, mid, x, v) n              : change(c[p][1], mid + 1, r, x, v); n } n int Ask(int p, int l, int r, int L, int R) { n   if (R < L) return n + 1; n   if ((!p) || (R < l) || (r < L)) return n + 1; n   if ((L <= l) && (r <= R)) return mi[p]; n   int mid = (1ll * l + r) >> 1; n   return min(Ask(c[p][0], l, mid, L, R), Ask(c[p][1], mid + 1, r, L, R)); n } n inline int Get(int v) { n   if (v >= R) return Ask(rt, 0, R + G - 1, (v + G) % (R + G), v - 1); n   return min(Ask(rt, 0, R + G - 1, v + G, R + G - 1), n              Ask(rt, 0, R + G - 1, 0, v - 1)); n } n inline long long calc(int x, int y, int v) { n   if (y == n + 1) return 0; n   v = (v + s[y] - s[x]) % (R + G); n   return R + G - v; n } n int main() { n   scanf( %d%d%d , &n, &G, &R); n   int i, p, x; n   for (i = 1; i <= n + 1; i++) scanf( %d , &d[i]); n   w[n + 1] = n + 1; n   f[n + 1] = 0; n   for (i = 2; i <= n + 1; i++) s[i] = s[i - 1] + d[i]; n   for (i = n; i >= 1; i--) { n     w[i] = Get(s[i] % (R + G)); n     change(rt, 0, R + G - 1, s[i] % (R + G), i); n   } n   for (i = n; i >= 1; i--) f[i] = f[w[i]] + s[w[i]] - s[i] + calc(i, w[i], 0); n   scanf( %d , &m); n   while (m--) { n     scanf( %d , &x); n     ans = d[1] + x; n     x = (x + d[1]) % (G + R); n     if (x >= G) { n       ans += R + G - x; n       x = 0; n     } n     p = Get((R + G - x) % (R + G)); n     ans += f[p] + s[p] + calc(1, p, x); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gcd(T a, T b) { n   while (b ^= a ^= b ^= a %= b) n     ; n   return a; n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd<T>(a, b) * b); n } n template <class T> n inline int countbit(T n) { n   return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); n } n template <class T> n inline bool isPrimeNumber(T n) { n   if (n <= 1) return false; n   for (T i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n template <class T> n inline T powerMod(T p, int e, T m) { n   if (e == 0) n     return 1 % m; n   else if (e % 2 == 0) { n     T t = powerMod(p, e / 2, m); n     return multiplyMod(t, t, m); n   } else n     return multiplyMod(powerMod(p, e - 1, m), p, m); n } n bool isUpperCase(char c) { return c >=  A  && c <=  Z ; } n bool isLowerCase(char c) { return c >=  a  && c <=  z ; } n char toLowerCase(char c) { return (isUpperCase(c)) ? (c + 32) : c; } n char toUpperCase(char c) { return (isLowerCase(c)) ? (c - 32) : c; } n template <class T> n string toString(T n) { n   ostringstream ost; n   ost << n; n   ost.flush(); n   return ost.str(); n } n int toInt(string s) { n   int r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n double toDouble(string s) { n   double r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n const double pi = acos(-1.0); n int main() { n   string s; n   cin >> s; n   int pos = -14; n   vector<string> v = { 01:10 ,   02:20 ,  03:30 ,  04:40 ,  05:50 , n                        10:01 ,   11:11 ,  12:21 ,  13:31 ,  14:41 , n                         15:51 ,  20:02 ,  21:12 ,  22:22 ,  23:32 }; n   for (int i = 0; i < v.size(); i++) { n     if (s < v[i]) { n       pos = i; n       break; n     } n   } n   if (pos == -14) n     cout <<  00:00 ; n   else n     cout << v[pos]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, t, tbl[510][510], star[510][510], sum[510]; n int main() { n   scanf( %d%d%d , &n, &m, &t); n   if (t == 0) { n     long long ans = 0; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) ans += i * j; n     cout << ans << endl; n     return 0; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %1d , tbl[i] + j); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       star[i][j] = (tbl[i][j] && tbl[i - 1][j] && tbl[i + 1][j] && n                     tbl[i][j - 1] && tbl[i][j + 1]); n   long long ans = 0; n   for (int i = 2; i < n; i++) { n     memset(sum, 0, sizeof(sum)); n     for (int j = i; j < n; j++) { n       for (int k = 2; k < m; k++) sum[k] += star[j][k]; n       for (int k = 2, l = 2, now = 0; k < m; k++) { n         while (l < m && (l <= k || now < t)) now += sum[l++]; n         if (now >= t) ans += m - l + 1; n         now -= sum[k]; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, d; n   cin >> k >> d; n   if (k > 1 && d == 0) n     cout <<  No solution ; n   else { n     cout << d; n     for (int i = 0; i < k - 1; i++) cout <<  0 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> a; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   while (n--) { n     int temp_val; n     string temp_s; n     cin >> temp_val >> temp_s; n     sort(temp_s.begin(), temp_s.end()); n     if (a[temp_s] < 1) n       a[temp_s] = temp_val; n     else n       a[temp_s] = min(a[temp_s], temp_val); n   } n   int ans = INT_MAX; n   if (a[ A ] >= 1 && a[ B ] >= 1 && a[ C ] >= 1) ans = a[ A ] + a[ B ] + a[ C ]; n   if (a[ A ] >= 1 && a[ BC ] >= 1) ans = min(ans, a[ A ] + a[ BC ]); n   if (a[ C ] >= 1 && a[ AB ] >= 1) ans = min(ans, a[ C ] + a[ AB ]); n   if (a[ B ] >= 1 && a[ AC ] >= 1) ans = min(ans, a[ B ] + a[ AC ]); n   if (a[ AB ] >= 1 && a[ BC ] >= 1) ans = min(ans, a[ AB ] + a[ BC ]); n   if (a[ AC ] >= 1 && a[ AB ] >= 1) ans = min(ans, a[ AC ] + a[ AB ]); n   if (a[ BC ] >= 1 && a[ AC ] >= 1) ans = min(ans, a[ BC ] + a[ AC ]); n   if (a[ ABC ] >= 1) ans = min(ans, a[ ABC ]); n   if (ans == INT_MAX) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2020 + 1; n int vis[MAXN], par[MAXN], orig[MAXN], match[MAXN], aux[MAXN], t, N; n vector<int> conn[MAXN]; n queue<int> Q; n void addEdge(int u, int v) { n   conn[u].push_back(v); n   conn[v].push_back(u); n } n void init(int n) { n   N = n; n   t = 0; n   for (int i = 0; i <= n; ++i) { n     conn[i].clear(); n     match[i] = aux[i] = par[i] = 0; n   } n } n void augment(int u, int v) { n   int pv = v, nv; n   do { n     pv = par[v]; n     nv = match[pv]; n     match[v] = pv; n     match[pv] = v; n     v = nv; n   } while (u != pv); n } n int lca(int v, int w) { n   ++t; n   while (true) { n     if (v) { n       if (aux[v] == t) n         return v; n       else n         aux[v] = t; n       v = orig[par[match[v]]]; n     } n     swap(v, w); n   } n } n void blossom(int v, int w, int a) { n   while (orig[v] != a) { n     par[v] = w; n     w = match[v]; n     if (vis[w] == 1) Q.push(w), vis[w] = 0; n     orig[v] = orig[w] = a; n     v = par[w]; n   } n } n bool bfs(int u) { n   fill(vis + 1, vis + 1 + N, -1); n   iota(orig + 1, orig + N + 1, 1); n   Q = queue<int>(); n   Q.push(u); n   vis[u] = 0; n   while (!Q.empty()) { n     int v = Q.front(); n     Q.pop(); n     for (int x : conn[v]) { n       if (vis[x] == -1) { n         par[x] = v; n         vis[x] = 1; n         if (!match[x]) return augment(u, x), true; n         Q.push(match[x]); n         vis[match[x]] = 0; n       } else if (vis[x] == 0 && orig[v] != orig[x]) { n         int a = lca(orig[v], orig[x]); n         blossom(x, v, a); n         blossom(v, x, a); n       } n     } n   } n   return false; n } n int Match() { n   int ans = 0; n   vector<int> V(N - 1); n   iota(V.begin(), V.end(), 1); n   shuffle(V.begin(), V.end(), mt19937(0x94949)); n   for (auto x : V) n     if (!match[x]) { n       for (auto y : conn[x]) n         if (!match[y]) { n           match[x] = y, match[y] = x; n           ++ans; n           break; n         } n     } n   for (int i = 1; i <= N; ++i) n     if (!match[i] && bfs(i)) ++ans; n   return ans; n } n struct UFD { n   vector<int> ufd; n   UFD(int n) : ufd(n) { iota(ufd.begin(), ufd.end(), 0); } n   int Find(int x) { n     if (x == ufd[x]) return x; n     return ufd[x] = Find(ufd[x]); n   } n   void Union(int a, int b) { ufd[Find(a)] = Find(b); } n }; n int main() { n   int N; n   cin >> N; n   vector<int> V(N); n   for (int& x : V) cin >> x; n   vector<pair<int, int>> ft; n   int from = -1; n   for (int i = 0; i < N; ++i) { n     if (V[i] == 0) { n       if (from == -1) from = i; n     } else { n       if (from != -1) ft.emplace_back(from, i - 1); n       from = -1; n     } n   } n   if (from != -1) ft.emplace_back(from, N - 1); n   if (ft.size() == 1 && ft[0].first == 0 && ft[0].second == N - 1) { n     for (int i = 0; i < N; ++i) cout << (i / 2) + 1 <<    ; n     cout << endl; n     return 0; n   } n   vector<pair<int, int>> ov, av; n   for (auto [f, t] : ft) { n     if (f == 0) { n       for (int i = 0; i + 1 <= t; i += 2) V[i] = V[i + 1] = -1; n       if (t % 2 == 0) V[t] = V[t + 1]; n     } else if (t == N - 1) { n       for (int i = t; i - 1 >= f; i -= 2) V[i] = V[i - 1] = -1; n       if ((t - f) % 2 == 0) V[f] = V[f - 1]; n     } else { n       if ((t - f + 1) % 2 == 0) n         av.emplace_back(f, t); n       else n         ov.emplace_back(f, t); n     } n   } n   int K = 600; n   UFD ufd(K + 1); n   init(K); n   vector<bool> used(K + 1); n   vector<int> cooked(K + 1); n   for (int i = 0; i < N - 1; ++i) n     if (V[i] == V[i + 1] && V[i] > 0) used[V[i]] = 1; n   vector<pair<int, int>> raw; n   for (auto [f, t] : ov) { n     int s = V[f - 1], e = V[t + 1]; n     if (ufd.Find(s) == ufd.Find(e)) { n       cooked[s] = s; n       used[s] = 1; n       V[f] = V[f - 1]; n       for (int i = f + 1; i <= t; ++i) V[i] = -1; n     } else n       raw.emplace_back(f, t); n     ufd.Union(s, e); n   } n   for (int i = 1; i <= K; ++i) n     if (cooked[i]) cooked[ufd.Find(i)] = cooked[i]; n   for (int i = 1; i <= K; ++i) n     if (cooked[ufd.Find(i)]) used[i] = 1; n   vector<vector<int>> added(K + 1, vector<int>(K + 1, -1)); n   for (int i = 0; i < (int)av.size(); ++i) { n     auto [f, t] = av[i]; n     int s = ufd.Find(V[f - 1]), e = ufd.Find(V[t + 1]); n     if (used[s] || used[e]) continue; n     if (added[s][e] != -1) continue; n     if (s == e) continue; n     added[s][e] = added[e][s] = i; n     addEdge(s, e); n   } n   Match(); n   vector<int> root(K + 1); n   for (int i = 1; i <= K; ++i) n     if (match[i] && match[i] > i) { n       int s = i, e = match[i]; n       auto [f, t] = av[added[s][e]]; n       V[f] = V[f - 1]; n       V[t] = V[t + 1]; n       used[V[f - 1]] = used[V[t + 1]] = 1; n       for (int j = f + 1; j <= t - 1; ++j) V[j] = -1; n     } n   for (auto [f, t] : av) n     if (V[f] == 0) n       for (int j = f; j <= t; ++j) V[j] = -1; n   vector<vector<tuple<int, int, int>>> conn(K + 1); n   for (auto [f, t] : raw) { n     int s = V[f - 1], e = V[t + 1]; n     conn[s].emplace_back(e, f, t); n     conn[e].emplace_back(s, f, t); n   } n   function<void(int, int)> dfs = [&](int a, int p) { n     for (auto [b, f, t] : conn[a]) n       if (b != p) { n         if (V[f] == 0) { n           for (int i = f; i <= t; ++i) V[i] = -1; n           if (V[f - 1] == b) n             V[f] = b; n           else n             V[t] = b; n         } n         dfs(b, a); n       } n   }; n   for (int i = 1; i <= K; ++i) n     if (ufd.Find(i) == i) { n       int p = i; n       if (cooked[p]) n         p = cooked[p]; n       else n         for (int i = 1; i <= K; ++i) n           if (used[i] && ufd.Find(i) == p) p = i; n       dfs(p, -1); n     } n   for (int i = 0; i < N - 1; ++i) n     if (V[i] == V[i + 1] && V[i] > 0) used[V[i]] = 1; n   int tp = 1; n   for (int i = 0; i < N - 1; ++i) n     if (V[i] == -1) { n       while (tp <= K && used[tp]) ++tp; n       assert(V[i + 1] == -1); n       V[i] = V[i + 1] = tp; n       if (tp <= K) used[tp] = 1; n       ++tp; n     } n   for (auto x : V) cout << x <<    , assert(x > 0); n   cout << endl; n } n


#include <bits/stdc++.h> n long long i, j, r, c, fre[1000], ans; n char na, name1[1000][1000]; n using namespace std; n int main() { n   scanf( %lld %lld %c , &r, &c, &na); n   for (i = 0; i < r; i++) { n     for (j = 0; j < c; j++) { n       scanf(  %c , &name1[i][j]); n     } n   } n   for (i = 0; i < r; i++) { n     for (j = 0; j < c; j++) { n       if (name1[i][j] == na) { n         if (i - 1 >= 0) { n           fre[(int)name1[i - 1][j]]++; n         } n         if (i + 1 < r) { n           fre[(int)name1[i + 1][j]]++; n         } n         if (j + 1 < c) { n           fre[(int)name1[i][j + 1]]++; n         } n         if (j - 1 >= 0) { n           fre[(int)name1[i][j - 1]]++; n         } n       } n     } n   } n   for (i = 65; i <= 90; i++) { n     if (fre[i] > 0) { n       ans++; n     } n   } n   if (fre[(int)na] > 0) { n     ans--; n   } n   printf( %lld n , ans); n } n


#include <iostream> n #include <iomanip> n #include<iterator> // for iterators n #include<vector> // for vectors n #include <utility> n #include <algorithm> n #include <set> n #include <map> n #include <stack> n #include <unordered_map> n using namespace std; n  n int main() n { n     int t, n, temp; scanf( %d , &t); n     vector<int> v; n     while (t --) n     { n         scanf( %d , &n); n         for (int i = 0; i < n; ++i) n         { n             scanf( %d , &temp); n             v.emplace_back(temp); n         } n         sort(v.begin() , v.end()); n         int counter = 0; n         for (int i = 1; i < v.size(); ++i) n         { n             if (v[i] > v[0]) n                 counter ++; n         } n         printf( %d n  , counter); n         v.clear(); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b) { n   long long res = 1; n   a %= 998244353; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 998244353 % 998244353; n     a = a * a % 998244353; n   } n   return res; n } n long long gcd(long long a, long long b) { n   if (a < b) return gcd(b, a); n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long n, m; n long long flg = 0; n vector<long long> ad[500005]; n long long t[500005]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ; n   long long i, j, k, x; n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     cin >> j >> k; n     ad[j].push_back(k); n     ad[k].push_back(j); n   } n   vector<pair<long long, long long> > ans; n   for (i = 1; i <= n; i++) { n     cin >> t[i]; n     ans.push_back(make_pair(t[i], i)); n   } n   sort(ans.begin(), ans.end()); n   for (i = 1; i <= n; i++) { n     set<long long> tp; n     for (auto x : ad[i]) { n       tp.insert(t[x]); n     } n     for (j = 1; j < t[i]; j++) { n       auto it = tp.find(j); n       if (it == tp.end()) { n         cout << -1 <<   n ; n         return 0; n       } else { n         tp.erase(j); n       } n     } n     auto it = tp.find(t[i]); n     if (it != tp.end()) { n       cout << -1 <<   n ; n       return 0; n     } n   } n   for (i = 0; i < ans.size(); i++) { n     cout << ans[i].second <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int MAX = 2e5 + 5; n const long long MAX2 = 11; n const long long MOD = 998244353; n const long long MOD2 = 1000005329; n const long long INF = 2e18; n const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; n const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; n const double pi = acos(-1); n const double EPS = 1e-9; n const int block = 2000; n int n, m, a, b, c, v[205][205], ans; n pair<double, pair<int, int> > e[205 * 205]; n double le, ri, m1, m2, n1, n2, a1, a2, u[205][205], nx; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) n       if (i != j) v[i][j] = MOD; n   for (int i = 1; i <= m; ++i) { n     cin >> a >> b >> c; n     e[i] = {c, {a, b}}; n     v[a][b] = v[b][a] = min(v[a][b], c); n   } n   for (int k = 1; k <= n; ++k) n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= n; ++j) v[i][j] = min(v[i][j], v[i][k] + v[k][j]); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) u[i][j] = v[i][j]; n   ans = 1e9; n   for (int i = 1; i <= m; ++i) { n     a = e[i].second.first, b = e[i].second.second; n     le = 0.0, ri = e[i].first; n     for (int k = 1; k <= 40; ++k) { n       m1 = (le + le + ri) / 3.0, n1 = e[i].first - m1; n       m2 = (le + ri + ri) / 3.0, n2 = e[i].first - m2; n       a1 = a2 = 0.0; n       for (int j = 1; j <= n; ++j) { n         a1 = max(a1, min(u[a][j] + m1, u[b][j] + n1)); n         a2 = max(a2, min(u[a][j] + m2, u[b][j] + n2)); n       } n       if (a1 < a2) n         ri = m2; n       else n         le = m1; n     } n     ans = min(ans, (int)round(a1 * 2.0)); n     nx = m1; n     le = 0.0, ri = nx; n     for (int k = 1; k <= 40; ++k) { n       m1 = (le + le + ri) / 3.0, n1 = e[i].first - m1; n       m2 = (le + ri + ri) / 3.0, n2 = e[i].first - m2; n       a1 = a2 = 0.0; n       for (int j = 1; j <= n; ++j) { n         a1 = max(a1, min(u[a][j] + m1, u[b][j] + n1)); n         a2 = max(a2, min(u[a][j] + m2, u[b][j] + n2)); n       } n       if (a1 < a2) n         ri = m2; n       else n         le = m1; n     } n     ans = min(ans, (int)round(a1 * 2.0)); n     le = nx, ri = e[i].first; n     for (int k = 1; k <= 40; ++k) { n       m1 = (le + le + ri) / 3.0, n1 = e[i].first - m1; n       m2 = (le + ri + ri) / 3.0, n2 = e[i].first - m2; n       a1 = a2 = 0.0; n       for (int j = 1; j <= n; ++j) { n         a1 = max(a1, min(u[a][j] + m1, u[b][j] + n1)); n         a2 = max(a2, min(u[a][j] + m2, u[b][j] + n2)); n       } n       if (a1 < a2) n         ri = m2; n       else n         le = m1; n     } n     ans = min(ans, (int)round(a1 * 2.0)); n   } n   if (ans & 1) n     cout << ans / 2 <<  .5 n ; n   else n     cout << ans / 2 <<  .0 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const double eps = 1e-10; n const double pi = acos(-1.0); n const int maxn = 1e6 + 10; n const long long inf = 0x3f3f3f3f; n const int dir[8][2] = {{0, 1}, {1, 0},  {0, -1}, {-1, 0}, n                        {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; n int n, m; n int dis[1010][1010][4]; n char g[1010][1010]; n queue<pair<int, int> > q; n void bfs(int x) { n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (g[i][j] ==  0  + x) q.push(make_pair(i, j)), dis[i][j][x] = 0; n   while (!q.empty()) { n     pair<int, int> p = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int dx = p.first + dir[i][0]; n       int dy = p.second + dir[i][1]; n       if (dx < 1 || dx > n || dy < 1 || dy > m || g[dx][dy] ==  # ) continue; n       int v = (g[dx][dy] ==  . ); n       if (dis[p.first][p.second][x] + v < dis[dx][dy][x]) { n         dis[dx][dy][x] = dis[p.first][p.second][x] + v; n         q.push(make_pair(dx, dy)); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> g[i] + 1; n   memset(dis, inf, sizeof dis); n   bfs(1), bfs(2), bfs(3); n   long long ans = inf; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       long long tmp = 0; n       for (int k = 1; k <= 3; k++) tmp += dis[i][j][k]; n       if (g[i][j] ==  . ) tmp -= 2; n       ans = min(ans, tmp); n     } n   if (ans == inf) n     cout << -1; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000011]; n int find(long long v) { n   int i, ret = -1; n   for (i = 1; i <= 2 * sqrt(v) + 1; ++i) { n     if (1ll * i * (i - 1) / 2 >= v) { n       ret = i; n       break; n     } n   } n   if (ret == -1 || 1ll * ret * (ret - 1) / 2 != v) return -1; n   return ret; n } n int main() { n   long long a00, a01, a10, a11; n   cin >> a00 >> a01 >> a10 >> a11; n   if (a00 + a01 + a10 + a11 == 0) { n     printf( 0 n ); n     return 0; n   } n   long long n = find(a00 + a01 + a10 + a11); n   long long n0 = find(a00), n1 = find(a11); n   if (a00 == 0 && a01 == 0 && a10 == 0) { n     n0 = 0; n   } n   if (a11 == 0 && a01 == 0 && a10 == 0) { n     n1 = 0; n   } n   if (n0 == -1 || n1 == -1 || n == -1 || n0 + n1 != n) { n     printf( Impossible n ); n     return 0; n   } n   int i = 1; n   while (n0 > 0 && n1 > 0) { n     if (a01 >= n1) { n       a[i++] = 0; n       a01 -= n1; n       n0++; n     } else { n       a[i++] = 1; n       n1--; n     } n   } n   if (a01 != 0) { n     printf( Impossible n ); n     return 0; n   } n   while (n0 > 0) { n     a[i++] = 0; n     n0--; n   } n   while (n1 > 0) { n     a[i++] = 1; n     n1--; n   } n   for (i = 1; i <= (int)n; ++i) { n     printf( %d , a[i]); n   } n   printf(  n ); n   return 0; n } n


//Code By CXY07 n #include<bits/stdc++.h> n using namespace std; n  n //#define FILE n //#define int long long n #define debug(x) cout << #x <<   =   << x << endl n #define file(FILENAME) freopen(FILENAME .in ,  r , stdin), freopen(FILENAME .out ,  w , stdout) n #define LINE() cout <<  LINE =   << __LINE__ << endl n #define LL long long n #define ull unsigned long long n #define pii pair<int,int> n #define mp make_pair n #define pb push_back n #define fst first n #define scd second n #define inv(x) qpow((x),mod - 2) n #define lowbit(x) ((x) & (-(x))) n #define abs(x) ((x) < 0 ? (-(x)) : (x)) n #define randint(l, r) (rand() % ((r) - (l) + 1) + (l)) n #define vec vector n  n const int MAXN = 1e6 + 10; n const int INF = 2e9; n const double PI = acos(-1); n const double eps = 1e-6; n //const int mod = 1e9 + 7; n //const int mod = 998244353; n //const int G = 3; n //const int base = 131; n  n int T, n, m; n int _L[MAXN], _R[MAXN]; n int *L = _L + (int)5e5, *R = _R + (int)5e5; n char s[MAXN]; n  n template<typename T> inline bool read(T &a) { n  a = 0; char c = getchar(); int f = 1; n  while(c <  0  || c >  9 ) {if(c ==  - ) f = -1; c = getchar();} n  while(c >=  0  && c <=  9 ) {a = a * 10 + (c ^ 48); c = getchar();} n  a *= f; n  return 1; n } n  n template<typename A, typename ...B> n inline bool read(A &x, B &...y) {return read(x) && read(y...);} n  n void solve() { n  scanf( %s , s + 1); n  n = strlen(s + 1); n  for(int i = -n; i <= n; ++i) L[i] = R[i] = 0; n  int now = 0; n  for(int i = 1; i <= n; ++i) { n   if(s[i] ==  1 ) R[now++]++; n   else L[now--]++; n  } n  now = 0; n  for(int i = 1; i <= n; ++i) { n   if(L[now] && (!R[now] || R[now - 1])) L[now--]--, putchar( 0 ); n   else R[now++]--, putchar( 1 ); n  } n  putchar(  n ); n } n  n signed main () { n #ifdef FILE n  freopen( .in , r ,stdin); n  freopen( .out , w ,stdout); n #endif n  read(T); n  while(T--) solve(); n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int u, v; n   long long c; n   bool operator<(const edge& other) const { return c < other.c; } n }; n vector<int> parent; n vector<int> size; n int get(int v) { n   if (parent[v] == v) { n     return v; n   } n   return parent[v] = get(parent[v]); n } n void unite(int a, int b) { n   a = get(a), b = get(b); n   if (size[a] < size[b]) { n     swap(a, b); n   } n   parent[b] = a; n   size[a] += size[b]; n } n const int rmax = 18; n vector<vector<int> > p; n vector<vector<long long> > maxx; n vector<vector<edge> > g; n vector<int> tin, tout; n int timer = 0; n void dfs(int v, int par) { n   tin[v] = timer++; n   p[v][0] = par; n   for (edge& e : g[v]) { n     if (e.v != par) { n       maxx[e.v][0] = e.c; n       dfs(e.v, v); n     } n   } n   tout[v] = timer++; n } n bool ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; } n int lca(int u, int v) { n   if (ancestor(u, v)) { n     return u; n   } n   for (int r = rmax - 1; r >= 0; --r) { n     if (!ancestor(p[u][r], v)) { n       u = p[u][r]; n     } n   } n   return p[u][0]; n } n long long getMax(int u, int v) { n   assert(u != v && ancestor(v, u)); n   long long ans = 0; n   for (int r = rmax - 1; r >= 0; --r) { n     if (!ancestor(p[u][r], v)) { n       ans = max(ans, maxx[u][r]); n       u = p[u][r]; n     } n   } n   return max(ans, maxx[u][0]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<edge> edges(m); n   for (int i = 0; i < m; ++i) { n     int u, v; n     long long c; n     cin >> u >> v >> c; n     --u, --v; n     edges[i] = {u, v, c}; n   } n   sort((edges).begin(), (edges).end()); n   parent.resize(n); n   size.assign(n, 1); n   for (int i = 0; i < n; ++i) { n     parent[i] = i; n   } n   long long total = 0; n   vector<edge> useful; n   for (int i = 0; i < m; ++i) { n     int u = edges[i].u, v = edges[i].v; n     u = get(u), v = get(v); n     if (u == v) { n       continue; n     } n     unite(u, v); n     total += edges[i].c; n     useful.push_back(edges[i]); n   } n   set<int> s; n   for (int i = 0; i < n; ++i) { n     s.insert(get(i)); n   } n   if ((int)(s).size() == 1) { n     p = vector<vector<int> >(n, vector<int>(rmax)); n     maxx = vector<vector<long long> >(n, vector<long long>(rmax)); n     g.resize(n); n     for (edge& e : useful) { n       int u = e.u, v = e.v; n       long long c = e.c; n       g[u].push_back({u, v, c}); n       g[v].push_back({v, u, c}); n     } n     tin.resize(n); n     tout.resize(n); n     dfs(0, 0); n     for (int r = 0; r + 1 < rmax; ++r) { n       for (int v = 0; v < n; ++v) { n         int t = p[v][r]; n         p[v][r + 1] = p[t][r]; n         maxx[v][r + 1] = max(maxx[v][r], maxx[t][r]); n       } n     } n   } n   int q; n   cin >> q; n   while (q--) { n     int a, b; n     cin >> a >> b; n     --a, --b; n     if ((int)(s).size() > 2) { n       cout <<  -1 n ; n       continue; n     } n     if ((int)(s).size() == 2) { n       a = get(a), b = get(b); n       if (a == b) { n         cout <<  -1 n ; n         continue; n       } n       cout << total <<   n ; n       continue; n     } n     int w = lca(a, b); n     long long ans = 0; n     if (a != w) { n       ans = max(ans, getMax(a, w)); n     } n     if (b != w) { n       ans = max(ans, getMax(b, w)); n     } n     cout << total - ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)1e18; n const long long mod = (long long)1e9 + 7; n const double eps = (double)1e-9; n const double pi = acos(-1.0); n const int dx[] = {0, 0, 1, 0, -1}; n const int dy[] = {0, 1, 0, -1, 0}; n const int N = 300500; n int n, dp[N]; n vector<int> g[N], l; n vector<pair<int, int> > gr, G, ga[N]; n void dfs(int v, int pr = -1) { n   dp[v] = 1; n   for (int to : g[v]) { n     if (to != pr) { n       dfs(to, v); n       dp[v] += dp[to]; n     } n   } n } n int dfs1(int v) { n   for (int to : g[v]) { n     if (dp[to] * 2 > n) { n       dp[v] -= dp[to]; n       dp[to] += dp[v]; n       return dfs1(to); n     } n   } n   return v; n } n void dfs2(int v, int pr, int gl = 0) { n   gr.push_back(make_pair(gl, v)); n   for (int to : g[v]) { n     if (to != pr) { n       dfs2(to, v, gl + 1); n     } n   } n } n void checkv(int st) { n   gr.clear(); n   dfs2(st, -1, 0); n   sort(gr.begin(), gr.end()); n   for (int i = 0; i + 1 < n; ++i) { n     if (gr[i].first == gr[i + 1].first && n         (int)g[gr[i].second].size() != (int)g[gr[i + 1].second].size()) n       return; n   } n   cout << st << endl; n   exit(0); n } n int main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(1); n   int centroid = dfs1(1); n   checkv(centroid); n   for (int i = 1; i <= n; ++i) n     if ((int)g[i].size() == 1) l.push_back(i); n   for (int x : l) { n     int last = x; n     int cur = g[x][0]; n     int dist = 1; n     while ((int)g[cur].size() == 2) { n       int nxt = g[cur][0]; n       if (nxt == last) nxt = g[cur][1]; n       dist++; n       last = cur; n       cur = nxt; n     } n     if (dist == n - 1) { n       checkv(x); n       cout << -1; n       return 0; n     } n     ga[cur].push_back(make_pair(x, dist)); n     G.push_back(make_pair(dist, x)); n   } n   for (int i = 1; i <= n; ++i) { n     if ((int)ga[i].size() == 1) { n       checkv(ga[i][0].first); n       cout << -1; n       return 0; n     } n   } n   for (int i = 0; i + 1 < (int)G.size(); ++i) n     if (G[i].first != G[i + 1].first) { n       checkv(G[i].second); n       checkv(G[i + 1].second); n       cout << -1; n       return 0; n     } n   cout << -1 << endl; n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ns, a[1111]; n void ok(long long x) { n   long long l = 0, r = min(2000000000ll, (1ll << 62) / (x + 1) + 1); n   while (l < r) { n     long long m = (l + r) / 2; n     if (m * (m - 1) / 2 + m * x >= n) n       r = m; n     else n       l = m + 1; n     if (m * (m - 1) / 2 + m * x == n && m % 2) a[++ns] = m + m * x; n   } n } n int main() { n   while (cin >> n) { n     ns = 0; n     for (int i = 0; i < 62; i++) ok((1ll << i) - 1); n     if (!ns) n       cout << -1 << endl; n     else { n       for (int i = 1; i <= ns; i++) cout << a[i] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char num[200001]; n int numLen, m, temp, answer = 0; n int stepArr = 1; n int main() { n   cin >> num >> m; n   numLen = strlen(num); n   answer += (num[numLen - 1] - 48) % m; n   for (int i = numLen - 2; i >= 0; i--) { n     stepArr = (stepArr * 10) % m; n     answer += ((num[i] - 48) * stepArr) % m; n     answer %= m; n   } n   temp = answer; n   for (int i = 0; i < numLen - 1; i++) { n     temp = ((((temp - (num[i] - 48) * stepArr) % m + m) * 10) % m + n             (num[i] - 48) % m) % n            m; n     if (num[i + 1] ^ 48) { n       answer = min(answer, temp); n     } n   } n   cout << answer; n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n T mymax(T x, T y) { n   return (x > y ? x : y); n } n using namespace std; n const int N = 100005; n const long long inf = 1e18; n vector<int> graph[N], cost[N]; n int n, m; n long long dis[N]; n struct node { n   int nd, c; n   node(int _nd, int _c) { n     nd = _nd; n     c = _c; n   } n   bool operator<(const node& p) const { return c > p.c; } n }; n void dijkstra() { n   priority_queue<node> pq; n   pq.push(node(1, 0)); n   for (int i = 0; i < N; i++) dis[i] = inf; n   dis[1] = 0; n   while (!pq.empty()) { n     node top = pq.top(); n     pq.pop(); n     int u = top.nd; n     for (int i = 0; i < (int)graph[u].size(); i++) { n       int v = graph[u][i]; n       if (dis[v] > dis[u] + cost[u][i]) { n         dis[v] = dis[u] + cost[u][i]; n         pq.push(node(v, dis[v])); n       } n     } n   } n   queue<int> q; n   q.push(n); n   vector<int> ans; n   ans.push_back(n); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i < graph[u].size(); i++) { n       int v = graph[u][i]; n       if (dis[u] - cost[u][i] == dis[v]) { n         ans.push_back(v); n         q.push(v); n         break; n       } n     } n   } n   if (ans.size() == 1) { n     printf( -1 n ); n     return; n   } n   reverse((ans).begin(), (ans).end()); n   for (int x : ans) { n     printf( %d  , x); n   } n   printf(  n ); n } n void solve() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     graph[u].push_back(v); n     graph[v].push_back(u); n     if (cost[u].size() == 0) { n       cost[u].push_back(w); n       cost[v].push_back(w); n     } else { n       int mn = 1e9; n       for (int i = 0; i < graph[u].size() - 1; i++) { n         int adj = graph[u][i]; n         if (adj == v) { n           mn = min(mn, cost[u][i]); n         } n       } n       mn = min(mn, w); n       cost[u].push_back(mn); n       cost[v].push_back(mn); n     } n   } n   dijkstra(); n   return; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 150; n int n; n double p[N]; n void init() { n   std::cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %lf , &p[i]); n     p[i] /= 100.0; n   } n } n void work() { n   static double f[N]; n   for (int i = 1; i <= n; i++) { n     f[i] = 1.0; n   } n   double answer = 1.0; n   double pr = 1.0; n   for (int t = 2; t <= 500000; t++) { n     int best = -1; n     for (int i = 1; i <= n; i++) { n       if (best == -1 || (1.0 - f[i] * (1.0 - p[i])) * (1.0 - f[best]) > n                             (1.0 - f[best] * (1.0 - p[best])) * (1.0 - f[i])) { n         best = i; n       } n     } n     f[best] *= 1.0 - p[best]; n     pr = 1.0; n     for (int i = 1; i <= n; i++) { n       pr *= 1.0 - f[i]; n     } n     answer += 1.0 - pr; n   } n   printf( %.10f n , answer); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[51]; n int main() { n   int n, g, check = 0, t = 0, q = 0; n   cin >> n; n   g = (n * (n - 1)) / 2; n   pair<int, int> can[g - 1]; n   for (int i = 0; i < g - 1; i++) { n     cin >> can[i].first >> can[i].second; n     s[can[i].first]++; n     s[can[i].second]++; n   } n   for (int i = 1; i <= n; i++) { n     if (s[i] < n - 1) { n       if (t == 0) n         t = i; n       else n         q = i; n     } n   } n   for (int i = 0; i < g; i++) { n     if (can[i].first == t) { n       check = 1; n       for (int r = 0; r < g; r++) { n         if (can[r].first == can[i].second && can[r].second == q) { n           cout << t <<     << q; n           return 0; n         } n       } n     } n   } n   if (check == 0) { n     cout << q <<     << t; n     return 0; n   } n   for (int i = 0; i < g; i++) { n     if (can[i].first == q) { n       for (int r = 0; r < g; r++) { n         if (can[r].first == can[i].second && can[r].second == t) { n           cout << q <<     << t; n           return 0; n         } n       } n     } n   } n   cout << t <<     << q; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma optimize( -O3 ) n string to_string(string s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(const bool& b) { return (b ?  true  :  false ); } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(const A& v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) res +=  ,  ; n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n template <typename T> n inline void _read(T& x) { n   cin >> x; n } n template <typename A, typename B> n inline void _read(pair<A, B>& x) { n   _read(x.first); n   _read(x.second); n } n template <typename T> n inline void _read(vector<T>& x) { n   for (auto& v : x) _read(v); n } n void R() {} n template <typename T, typename... U> n void R(T& head, U&... tail) { n   _read(head); n   R(tail...); n } n template <typename T> n inline void _write(const T& x) { n   cout << x <<    ; n } n template <typename A, typename B> n inline void _write(const pair<A, B>& x) { n   _write(x.first); n   _write(x.second); n } n template <typename T> n inline void _write(const vector<T>& in) { n   for (const auto& x : in) _write(x); n } n void W() { cout <<   n ; } n template <typename T, typename... U> n void W(const T& head, const U&... tail) { n   _write(head); n   W(tail...); n } n void go(); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   go(); n   return 0; n } n int solve(int a, int b) { n   if (a == 1 && b == 2) return 3; n   if (a == 1 && b == 3) return 4; n   if (a == 2 && b == 1) return 3; n   if (a == 2 && b == 3) { n     W( Infinite ); n     exit(0); n   } n   if (a == 3 && b == 1) return 4; n   if (a == 3 && b == 2) { n     W( Infinite ); n     exit(0); n   } n } n void go() { n   int n; n   R(n); n   vector<int> a(n); n   R(a); n   int ans = 0; n   for (int i = 1; i < n; i++) { n     ans += solve(a[i - 1], a[i]); n   } n   for (int i = 2; i < n; i++) n     if (a[i - 2] == 3 && a[i - 1] == 1 && a[i] == 2) ans--; n   W( Finite ); n   W(ans); n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n int S, n, tot; n int fa[2100], a[2100][2100], p[2100 * 2100 / 2]; n vector<int> E[2100]; n void EXT() { n   printf( NO n ); n   exit(0); n } n int get(int x) { n   if (fa[x] == x) return x; n   return fa[x] = get(fa[x]); n } n void dfs(int x, int ff, int dep) { n   if (a[S][x] != dep) EXT(); n   for (int i : E[x]) n     if (i != ff) dfs(i, x, dep + a[x][i]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) scanf( %d , &a[i][j]); n   for (int i = 1; i <= n; ++i) { n     if (a[i][i] != 0) EXT(); n     for (int j = i + 1; j <= n; ++j) { n       if (a[i][j] != a[j][i] || a[i][j] == 0) EXT(); n       p[++tot] = (i << 20) + j; n     } n   } n   sort(p + 1, p + tot + 1, [](int x, int y) { n     return a[x >> 20][x & 0xffff] < a[y >> 20][y & 0xffff]; n   }); n   for (int i = 1; i <= n; ++i) fa[i] = i; n   for (int i = 1; i <= tot; ++i) { n     int x = p[i] >> 20, y = p[i] & 0xffff; n     if (get(x) == get(y)) continue; n     E[x].push_back(y); n     E[y].push_back(x); n     fa[get(x)] = get(y); n   } n   for (int i = 1; i <= n; ++i) { n     S = i; n     dfs(i, 0, 0); n   } n   printf( YES n ); n } n


#include <bits/stdc++.h> n using namespace std; n int top; n int t[10000]; n long long sum[100000]; n int ans[5002][5002]; n const int MOD = 1e9 + 7; n int main() { n   int n, a, b, k; n   scanf( %d%d%d%d , &n, &a, &b, &k); n   if (a < b) n     top = b - 1; n   else n     top = abs(n - b); n   for (int i = 1; i <= top; i++) { n     int far = i * 2 - 1; n     if (far <= top) n       ans[i][1] = 2 * (i - 1); n     else n       ans[i][1] = i - 1 + (top - i); n   } n   for (int i = 1; i <= top; i++) sum[i] = (sum[i - 1] + t[i]) % MOD; n   for (int j = 2; j <= k; j++) { n     for (int q = 1; q <= top; q++) sum[q] = (sum[q - 1] + ans[q][j - 1]) % MOD; n     for (int i = 1; i <= top; i++) { n       int far = i * 2 - 1; n       if (far <= top) n         ans[i][j] = (sum[i - 1] + sum[far]) % MOD - sum[i]; n       else n         ans[i][j] = (sum[i - 1] + sum[top]) % MOD - sum[i]; n       ans[i][j] = ans[i][j] % MOD; n     } n   } n   printf( %d n , (ans[abs(a - b)][k] + MOD) % MOD); n } n


#include <bits/stdc++.h> n using namespace std; n int level(int n, int sum[]) { n   int i; n   for (i = 1; i < 11; i++) { n     if (n > sum[i - 1] && n <= sum[i]) { n       return i; n     } n   } n } n int number(int n, int l, int sum[]) { n   int st = sum[l - 1] + 1; n   return n - st + 1; n } n int main() { n   int sum[11] = {0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55}; n   int n, t, i, j, k; n   cin >> n >> t; n   int ans = 0; n   vector<int> adj[60]; n   for (i = 1; i < 56; i++) { n     int l = level(i, sum); n     int num = number(i, l, sum); n     adj[i].push_back(sum[l] + num); n     adj[i].push_back(sum[l] + num + 1); n   } n   double w[100] = {0}; n   double one = 1.00; n   for (j = 1; j <= t; j++) { n     w[1] += one; n     for (i = 1; i < 56; i++) { n       if (w[i] >= one) { n         double rem = (w[i] - one) / 2.00; n         w[i] = one; n         for (k = 0; k < adj[i].size(); k++) { n           w[adj[i][k]] += rem; n         } n       } n     } n   } n   for (i = 1; i <= sum[n]; i++) { n     if (w[i] == one) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double p[110], S; n int main() { n   scanf( %d , &n); n   for (int i = 0; i <= n; i++) scanf( %lf , p + i); n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j <= n; j++) n       if (i * 2 <= n && n <= j * 2) n         S = max( n             S, (n * 0.5 - j) / (i - j) * p[i] + (n * 0.5 - i) / (j - i) * p[j]); n   printf( %.9lf n , S); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 51; n const int tot = maxn * maxn; n long long input[maxn]; n long long dp[maxn][tot][maxn]; n long long dp2[maxn][tot][maxn]; n long long comb[maxn + 1][maxn + 1]; n int N, M; n void init() { n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> input[i]; n   } n   cin >> M; n   comb[0][0] = 1; n   for (int i = 1; i <= maxn; i++) { n     comb[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1]; n     } n   } n } n long long factorial[maxn]; n bool vst[maxn]; n void run() { n   int total = 0; n   for (int i = 0; i < N; i++) { n     total += input[i]; n   } n   if (total <= M) { n     cout << N << endl; n     return; n   } n   for (int i = 0; i < N; i++) { n     memset(vst, 0, sizeof(vst)); n     vst[i] = 1; n     dp[i][0][0] = 1; n     int cnt = 0; n     for (int j = 0; j < N; j++) { n       if (vst[j] == 0) { n         cnt++; n         for (int k = 0; k < tot; k++) { n           for (int t = 0; t <= cnt; t++) { n             dp2[i][k][t] = dp[i][k][t]; n           } n         } n         for (int k = 0; k < tot; k++) { n           for (int t = 0; t <= cnt; t++) { n             if (dp[i][k][t] && (k + input[j] < tot)) { n               dp2[i][k + input[j]][t + 1] += dp[i][k][t]; n             } n           } n         } n         for (int k = 0; k < tot; k++) { n           for (int t = 0; t <= cnt; t++) { n             dp[i][k][t] = dp2[i][k][t]; n           } n         } n       } n     } n   } n   double ans = 0; n   for (int i = M; i >= 0; i--) { n     for (int j = 0; j < N; j++) { n       if (i + input[j] > M) { n         for (int k = 0; k < N; k++) { n           double tmp = dp[j][i][k] * 1.0 / comb[N][k]; n           tmp /= (N - k); n           tmp *= k; n           ans += tmp; n         } n       } n     } n   } n   printf( %.8lf n , ans); n } n void baoli() { n   const int n = 50; n   int tmp[n]; n   cout << n <<    ; n   for (int i = 0; i < n; i++) { n     tmp[i] = i + 1; n     cout << tmp[i] <<    ; n   } n   int m = 100; n   cout << m << endl; n   double ans = 0; n   for (int test = 0; test <= 10000; test++) { n     random_shuffle(tmp, tmp + n); n     int sum = 0; n     for (int i = 0; i < n; i++) { n       sum += tmp[i]; n       if (sum > m) { n         ans += i; n         break; n       } n     } n   } n   cout << ans / 10000 << endl; n } n int main() { n   init(); n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int a, b, c; n   cin >> a >> b >> c; n   a = abs(a) + abs(b); n   if (a > c) n     cout <<  NO  << endl; n   else { n     if ((c - a) % 2 == 0) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a; n   for (int i = 0; i <= 9; i++) { n     cout << i << endl; n     cout << flush; n     a =   ; n     getline(cin, a); n     if (a ==  no ) { n       continue; n     } else { n       if (a ==  don t think so  || a ==  great!  || a ==  don t touch me!  || n           a ==  not bad  || a ==  cool ) { n         cout <<  normal ; n       } else { n         if (a ==  are you serious  || a ==  worse  || a ==  terrible  || n             a ==  don t even  || a ==  no way  || a ==  go die in a hole ) { n           cout <<  grumpy ; n         } else { n           cout <<  normal ; n         } n       } n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n vector<int> v[N]; n int num[N]; n int q[N]; n int cnt; n int check(int mid) { n   int t = num[0] + mid, res = 0, tot = 0; n   for (int i = 1; i <= 100000; i++) { n     for (int j = 0; j <= max(-1, num[i] - t); j++) tot++, res += v[i][j]; n   } n   if (tot > mid) return 1 << 30; n   cnt = 0; n   for (int i = 1; i <= 100000; i++) { n     for (int j = max(0, num[i] - t + 1); j <= num[i] - 1; j++) n       q[++cnt] = v[i][j]; n   } n   sort(q + 1, q + cnt + 1); n   for (int i = 1; i <= mid - tot; i++) res += q[i]; n   return res; n } n int main() { n   int x, y; n   int n; n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     if (x == 0 || y == 0) n       num[0]++; n     else n       v[x].push_back(y), num[x]++; n   } n   for (int i = 1; i <= 100000; i++) { n     sort(v[i].begin(), v[i].end()); n   } n   int f = 1; n   for (int i = 1; i <= n; i++) { n     if (num[i] >= num[0]) f = false; n   } n   if (f) { n     puts( 0 ); n     return 0; n   } n   int l = 1, r = n; n   int ans = 1 << 30; n   while (l <= r) { n     int mid1 = l + (r - l) / 3; n     int f1 = check(mid1); n     int mid2 = r - (r - l) / 3; n     int f2 = check(mid2); n     if (f1 <= f2) n       r = mid2 - 1, ans = min(ans, f1); n     else n       l = mid1 + 1, ans = min(ans, f2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const long long INF = 1e17 + 7; n const int N = 1e4 + 7; n const int M = 5e6 + 7; n long long x[N], y[N]; n long long ax, ay, bx, by, a, b, t; n signed main() { n   ios::sync_with_stdio(false); n   cin >> x[0] >> y[0] >> ax >> ay >> bx >> by; n   int n = 0; n   for (; x[n] < INF && y[n] < INF; n++) { n     x[n + 1] = x[n] * ax + bx; n     y[n + 1] = y[n] * ay + by; n     if (x[n + 1] >= INF || y[n + 1] >= INF) break; n   } n   cin >> a >> b >> t; n   long long ans = 0; n   for (int l = 0; l <= n; l++) { n     for (int r = l; r <= n; r++) { n       unsigned long long to = n           min(abs(a - x[l]) + abs(b - y[l]), abs(a - x[r]) + abs(b - y[r])); n       to += abs(x[l] - x[r]) + abs(y[l] - y[r]); n       if (to <= t) ans = max(ans, r - l + 1ll); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastio() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   fastio(); n   long long int a, b; n   long long int mx = INT_MIN; n   long long int n, mn = INT_MAX; n   cin >> n; n   long long int ar[n]; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n     mn = min(mn, ar[i]); n     mx = max(ar[i], mx); n   } n   long long int res = mx - mn + 1; n   cout << res - n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n int len(pair<int, int> xy) { return xy.first + xy.second; } n pair<int, int> jump(pair<int, int> xy, int len) { n   if (len == 0) return xy; n   auto [x, y] = xy; n   if (len > x + y) return {0, 0}; n   int bit = 31 - __builtin_clz(len); n   int cur = x ^ y; n   int take = cur & ((1 << bit) - 1); n   assert(take <= len); n   x &= ~take; n   y &= ~take; n   len -= take; n   while (len > 0) { n     int bit = __builtin_ctz(x ^ y); n     int take = min(1 << bit, len); n     if ((x >> bit) & 1) n       x -= take; n     else n       y -= take; n     len -= take; n   } n   return {x, y}; n } n pair<int, int> lca(pair<int, int> u, pair<int, int> v) { n   int lu = len(u), lv = len(v); n   if (lu > lv) { n     swap(lu, lv); n     swap(u, v); n   } n   v = jump(v, lv - lu); n   if (u == v) return u; n   for (int bit = 29; bit >= 0; bit--) { n     auto nu = jump(u, 1 << bit), nv = jump(v, 1 << bit); n     if (nu != nv) tie(u, v) = {nu, nv}; n   } n   return jump(u, 1); n } n bool cmp(pair<int, int> u, pair<int, int> v) { n   if (u == v) return false; n   bool swp = false; n   int lu = len(u), lv = len(v); n   if (lu > lv) { n     swap(lu, lv); n     swap(u, v); n     swp = true; n   } n   v = jump(v, lv - lu); n   if (u == v) return true ^ swp; n   return (u > v) ^ swp; n } n using event = pair<pair<int, int>, int>; n vector<event> evt; n struct node { n   int val, depth; n   node *left, *right; n }; n node *build(int from, int to) { n   if (from >= to) return nullptr; n   int val = 0; n   auto w = lca(evt[from].first, evt[to - 1].first); n   while (from < to && evt[from].first == w) { n     val += evt[from].second; n     ++from; n   } n   auto [wx, wy] = w; n   if ((wx + 1) & wy) return new node({val, len(w), nullptr, build(from, to)}); n   if (wx & (wy + 1)) return new node({val, len(w), build(from, to), nullptr}); n   pair<int, int> rchild(wx, wy + 1); n   int lo = from - 1, hi = to; n   while (lo + 1 < hi) { n     auto mi = (lo + hi) / 2; n     if (cmp(evt[mi].first, rchild)) n       lo = mi; n     else n       hi = mi; n   } n   return new node({val, len(w), build(from, hi), build(hi, to)}); n } n vector<int> change; n void add(int l, int r) { n   change.push_back(l); n   change.push_back(r); n } n void simplify() { n   sort((change).begin(), (change).end()); n   vector<int> st; n   for (auto x : change) { n     if (!st.empty() && st.back() == x) n       st.pop_back(); n     else n       st.push_back(x); n   } n   change = st; n } n int dfs(node *u) { n   int ans = 0; n   if (u->left) { n     auto s = dfs(u->left); n     if (s > 0) add(u->depth + 1, u->left->depth); n     ans += s; n   } n   if (u->right) { n     auto s = dfs(u->right); n     if (s > 0) add(u->depth + 1, u->right->depth); n     ans += s; n   } n   ans += u->val; n   if (ans > 0) add(u->depth, u->depth + 1); n   return ans; n } n void print(node *u, string tab =   ) { n   if (!u) { n     cerr << tab <<  X n ; n     return; n   } n   cerr << tab << u->val <<     << u->depth << endl; n   auto ntab = tab +     ; n   print(u->left, ntab); n   print(u->right, ntab); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int m; n   cin >> m; n   while (m--) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     pair<int, int> u(x1, y1), v(x2, y2); n     pair<int, int> w = lca(u, v); n     evt.emplace_back(u, +1); n     evt.emplace_back(v, +1); n     evt.emplace_back(w, -1); n     if (len(w)) { n       w = jump(w, 1); n       evt.emplace_back(w, -1); n     } n   } n   sort((evt).begin(), (evt).end(), n        [](const event &a, const event &b) { return cmp(a.first, b.first); }); n   auto tree = build(0, ((int)(evt).size())); n   dfs(tree); n   simplify(); n   int ans = ((int)(change).size()); n   if (ans > 0 && change[0] == 0) --ans; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n vector<int> nums, fact; n int a, b, n; n void precomp() { n   fact.resize(n + 1); n   fact[0] = 1; n   for (int i = 1; i <= n; ++i) fact[i] = 1ll * fact[i - 1] * i % mod; n } n void gener() { n   int _max = log10(max(a, b) * n); n   queue<int> q; n   q.push(0); n   while (!q.empty()) { n     int top = q.front(); n     q.pop(); n     nums.push_back(top); n     if ((int)log10(top) == _max) continue; n     for (int i : {a, b}) q.push(10 * top + i); n   } n } n void egcd(int a, int b, int& x, int& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return; n   } n   int x1, y1; n   egcd(b, a % b, x1, y1); n   x = y1; n   y = x1 - (a / b) * y1; n } n int inv_mod(int a) { n   int x, y; n   egcd(a, mod, x, y); n   return (x + mod) % mod; n } n int binom(int n, int k) { n   int top = fact[n]; n   int bot = 1ll * fact[k] * fact[n - k] % mod; n   return 1ll * top * inv_mod(bot) % mod; n } n int main() { n   cin >> a >> b >> n; n   precomp(); n   gener(); n   int ans = 0; n   for (int c : nums) { n     int top = c - b * n; n     int bot = a - b; n     if (top % bot) continue; n     int res = top / bot; n     if (res >= 0 && res <= n) ans = (ans + binom(n, res)) % mod; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct num { n   static const int MA = 1e9 + 7, MB = 1e9 + 9; n   int a, b; n   num() {} n   num(int x) : a(x), b(x) {} n   num(int a, int b) : a(a), b(b) {} n   num operator+(const num& x) const { n     return num((a + x.a) % MA, (b + x.b) % MB); n   } n   num operator-(const num& x) const { n     return num((a + MA - x.a) % MA, (b + MB - x.b) % MB); n   } n   num operator*(int x) const { n     return num(((long long)a * x) % MA, ((long long)b * x) % MB); n   } n   num operator*(const num& x) const { n     return num(((long long)a * x.a) % MA, ((long long)b * x.b) % MB); n   } n   bool operator==(const num& x) const { return a == x.a && b == x.b; } n }; n struct num2 { n   static const int MB = 1e9 + 9; n   int b; n   num2() {} n   num2(int x) : b(x) {} n   num2 operator+(const num2& x) const { return num2((b + x.b) % MB); } n   num2 operator-(const num2& x) const { return num2((b + MB - x.b) % MB); } n   num2 operator*(int x) const { return num2(((long long)b * x) % MB); } n   num2 operator*(const num2& x) const { n     return num2(((long long)b * x.b) % MB); n   } n   bool operator==(const num2& x) const { return b == x.b; } n }; n const int MODP = 701; n template <class T> n struct StringOps { n   StringOps() {} n   StringOps(const string& s) : s(s) { n     n = s.length(); n     deg.resize(n + 1); n     hashes.resize(n); n     deg[0] = T(1); n     for (int i = 1; i <= n; ++i) { n       deg[i] = deg[i - 1] * T(MODP); n     } n     T curhash = T(0); n     for (int i = 0; i < n; ++i) { n       curhash = curhash * T(MODP) + T(s[i]); n       hashes[i] = curhash; n     } n   } n   char get(int idx) { return s[idx]; } n   T get_hash(int l, int r) { n     T cur(0); n     if (l > 0) { n       cur = hashes[l - 1]; n     } n     return hashes[r] - cur * deg[r - l + 1]; n   } n   int string_lcp(int l, int r, int lim = 1e9) { n     int cleft = 1, cright = min(n - r, n - l); n     cright = min(lim, cright); n     int res = 0; n     while (cleft <= cright) { n       int key = (cleft + cright) / 2; n       if (s[l + key - 1] != s[r + key - 1]) { n         cright = key - 1; n       } else if (get_hash(l, l + key - 1) == get_hash(r, r + key - 1)) { n         res = key; n         cleft = key + 1; n       } else { n         cright = key - 1; n       } n     } n     return res; n   } n   bool substring_cmp(int l1, int r1, int l2, int r2) { n     int lcp = string_lcp(l1, l2); n     int len1 = r1 - l1 + 1, len2 = r2 - l2 + 1; n     if (lcp >= len1 && lcp >= len2) { n       return len1 < len2; n     } n     if (lcp >= len1) { n       return true; n     } n     if (lcp >= len2) { n       return false; n     } n     return s[l1 + lcp] < s[l2 + lcp]; n   } n   vector<T> hashes; n   vector<T> deg; n   string s; n   int n; n }; n const int maxN = 1000100; n const int MOD = 1e9 + 7; n int len1, len2; n int u, v; n StringOps<num2> sops; n int get_nlength(const pair<int, int>& a) { n   if (a.first == u) { n     if (a.second != len1) { n       return len1 - 1; n     } else { n       return len1; n     } n   } n   if (a.second != len2) { n     return len2 - 1; n   } else { n     return len2; n   } n } n void fill_segs(int& l1, int& r1, int& l2, int& r2, const pair<int, int>& a) { n   if (a.first == u) { n     l2 = r2 = -1; n     if (a.second == 0) { n       l1 = 1, r1 = len1 - 1; n     } else if (a.second >= len1 - 1) { n       l1 = 0, r1 = a.second - 1; n     } else { n       l1 = 0, r1 = a.second - 1; n       l2 = a.second + 1, r2 = len1 - 1; n     } n     return; n   } n   l2 = r2 = -1; n   if (a.second == 0) { n     l1 = 1, r1 = len2 - 1; n   } else if (a.second >= len2 - 1) { n     l1 = 0, r1 = a.second - 1; n   } else { n     l1 = 0, r1 = a.second - 1; n     l2 = a.second + 1, r2 = len2 - 1; n   } n   l1 += len1, r1 += len1; n   if (l2 != -1) { n     l2 += len1, r2 += len1; n   } n } n int cmp2(vector<pair<int, int>>& a, vector<pair<int, int>>& b, int p1 = 0, n          int p2 = 0) { n   if (p1 == a.size() && p2 < b.size()) { n     return -1; n   } n   if (p1 == a.size() && p2 == b.size()) { n     return 0; n   } n   if (p2 == b.size() && p1 < a.size()) { n     return 1; n   } n   if (p1 + 1 == a.size() && p2 + 1 == b.size() && a[0] == b[0]) { n     return 0; n   } n   if (p1 + 2 == a.size() && p2 + 2 == b.size() && a[0] == b[0] && n       a[1] == b[1]) { n     return 0; n   } n   if (sops.get(a[p1].first) != sops.get(b[p2].first)) { n     char c1 = sops.get(a[p1].first); n     char c2 = sops.get(b[p2].first); n     if (c1 < c2) { n       return -1; n     } n     return 1; n   } n   int lcp = sops.string_lcp( n       a[p1].first, b[p2].first, n       min(a[p1].second - a[p1].first + 1, b[p2].second - b[p2].first + 1)); n   lcp = min( n       lcp, min(a[p1].second - a[p1].first + 1, b[p2].second - b[p2].first + 1)); n   int nx = a[p1].first + lcp - 1, ny = b[p2].first + lcp - 1; n   if (nx >= a[p1].second) { n     ++p1; n   } else { n     a[p1].first = nx + 1; n   } n   if (ny >= b[p2].second) { n     ++p2; n   } else { n     b[p2].first = ny + 1; n   } n   return cmp2(a, b, p1, p2); n } n bool cmp(const pair<int, int>& a, const pair<int, int>& b) { n   int nlen1 = get_nlength(a); n   int nlen2 = get_nlength(b); n   if (nlen1 == 0 && nlen2 == 0) { n     if (a.first == u & b.first == v) { n       return true; n     } n     return false; n   } n   if (nlen1 == 0) { n     return true; n   } n   if (nlen2 == 0) { n     return false; n   } n   int l1, r1, l2, r2; n   fill_segs(l1, r1, l2, r2, a); n   int p1, q1, p2, q2; n   fill_segs(p1, q1, p2, q2, b); n   vector<pair<int, int>> na; n   vector<pair<int, int>> nb; n   na.push_back(make_pair(l1, r1)); n   if (l2 != -1) { n     na.push_back(make_pair(l2, r2)); n   } n   nb.push_back(make_pair(p1, q1)); n   if (p2 != -1) { n     nb.push_back(make_pair(p2, q2)); n   } n   int val = cmp2(na, nb); n   if (val == 0) { n     if (a.first == u && b.first == v) { n       return true; n     } n     return false; n   } n   return val == -1; n } n struct Node { n   Node() { left = right = nullptr; } n   int idxleft, idxright; n   Node* left; n   Node* right; n }; n int curorder[maxN]; n int lenorder; n void dfs(Node* root) { n   if (!root) { n     return; n   } n   if (root->left) { n     dfs(root->left); n   } n   for (int i = root->idxleft; i <= root->idxright; ++i) { n     curorder[lenorder] = i; n     ++lenorder; n   } n   if (root->right) { n     dfs(root->right); n   } n } n pair<int, int> order[2][maxN]; n int clen[2]; n void get_order(const string& s, int p) { n   int n = s.length(); n   Node* root = new Node(); n   Node* curnode = root; n   for (int i = 0; i < n;) { n     int cur = i; n     while (cur < n && s[cur] == s[i]) { n       ++cur; n     } n     curnode->idxleft = i, curnode->idxright = cur - 1; n     if (cur == n) { n       break; n     } n     if (s[i] < s[cur]) { n       Node* left = new Node(); n       curnode->left = left; n       curnode = curnode->left; n     } else { n       Node* right = new Node(); n       curnode->right = right; n       curnode = curnode->right; n     } n     i = cur; n   } n   lenorder = 0; n   dfs(root); n   int ps = n; n   for (int i = 0; i < n; ++i) { n     if (curorder[i] == n - 1) { n       ps = i + 1; n       break; n     } n   } n   vector<pair<int, int>> result; n   clen[p] = 0; n   for (int i = 0; i < ps; ++i) { n     order[p][clen[p]] = make_pair(v, curorder[i]); n     ++clen[p]; n   } n   order[p][clen[p]] = make_pair(v, n); n   ++clen[p]; n   for (int i = ps; i < n; ++i) { n     order[p][clen[p]] = make_pair(v, curorder[i]); n     ++clen[p]; n   } n } n int dp[2][maxN]; n int dp2[2][maxN]; n void gen() { n   cout << 10 << endl; n   for (int i = 0; i < 10; ++i) { n     for (int j = 0; j < 100000; ++j) { n       cout << char(rand() % 26 +  a ); n     } n     cout << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vector<string> s(n); n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n   } n   for (int i = 0; i <= s[0].length(); ++i) { n     dp[u][i] = 1; n     dp2[u][i] = 1; n   } n   sops = StringOps<num2>(s[0]); n   len1 = 0, len2 = s[0].length(); n   u = 1, v = 0; n   get_order(s[0], 0); n   swap(u, v); n   for (int i = 0; i + 1 < n; ++i) { n     string& s1 = s[i]; n     string& s2 = s[i + 1]; n     string nw = s1 + s2; n     len1 = s1.length(), len2 = s2.length(); n     sops = StringOps<num2>(nw); n     get_order(s[i + 1], v); n     int cadd = 0; n     int head1 = 0, head2 = 0; n     while (head1 < clen[u] || head2 < clen[v]) { n       if (head1 == clen[u]) { n         dp[v][order[v][head2].second] = cadd; n         ++head2; n       } else if (head2 == clen[v]) { n         break; n       } else { n         if (cmp(order[u][head1], order[v][head2])) { n           int nhead1 = head1; n           while (nhead1 < clen[u] && n                  s1[order[u][nhead1].second] == s1[order[u][head1].second] && n                  order[u][nhead1].second - order[u][head1].second == n                      nhead1 - head1) { n             ++nhead1; n           } n           for (int j = head1; j < nhead1; ++j) { n             cadd += dp[u][order[u][j].second]; n             if (cadd >= MOD) { n               cadd -= MOD; n             } n           } n           head1 = nhead1; n         } else { n           int nhead2 = head2; n           while (nhead2 < clen[v] && n                  s2[order[v][nhead2].second] == s2[order[v][head2].second] && n                  order[v][nhead2].second - order[v][head2].second == n                      nhead2 - head2) { n             ++nhead2; n           } n           for (int j = head2; j < nhead2; ++j) { n             dp[v][order[v][j].second] = cadd; n           } n           head2 = nhead2; n         } n       } n     } n     swap(u, v); n   } n   int res = 0; n   int res2 = 0; n   for (int i = 0; i <= s.back().length(); ++i) { n     res += dp[u][i]; n     if (res >= MOD) { n       res -= MOD; n     } n     res2 += dp2[u][i]; n     if (res2 >= MOD) { n       res2 -= MOD; n     } n   } n   cout << res << endl; n   cerr << (double)clock() / CLOCKS_PER_SEC << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, h, count = 0; n   cin >> n >> h; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     if (a[i] > h) n       count += 2; n     else n       count += 1; n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   cin >> a; n   vector<int> s(6); n   for (int i = 0; i < 6; i++) { n     if ((1 << i) & a) s[i]++; n   } n   int res = 0; n   res += (16 * s[0]); n   res += (2 * s[1]); n   res += (8 * s[2]); n   res += (4 * s[3]); n   res += s[4]; n   res += (32 * s[5]); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int l[6]; n int main() { n   int n, m, a; n   cin >> m >> n; n   for (int i = 0; i < m; i++) { n     for (int i = 1; i <= n; i++) { n       cin >> a; n       l[i] = max(l[i - 1], l[i]) + a; n     } n     cout << l[n] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3fll; n const int qwq = 1 << 19; n int N, Q; n int a[qwq]; n struct Query { n   int r1, c1, r2, c2, id; n   Query(int r1, int c1, int r2, int c2, int id) n       : r1(r1), c1(c1), r2(r2), c2(c2), id(id) {} n   bool inline operator<(const Query& a) const { return r2 < a.r2; } n }; n struct Segtree { n   int dt[qwq << 1]; n   void inline init() { memset(dt, Inf, sizeof(dt)); } n   void inline add(int a, int x) { n     a += qwq; n     dt[a] = x; n     while (a >>= 1) dt[a] = min(dt[a << 1], dt[a << 1 | 1]); n   } n   int inline get(int l, int r) { n     int res = Inf; n     for (l += qwq, r += qwq + 1; l < r; l >>= 1, r >>= 1) { n       if (l & 1) res = min(res, dt[l++]); n       if (r & 1) res = min(res, dt[--r]); n     } n     return res; n   } n } sl, sr, qj; n vector<Query> q1, q2; n int ans[qwq]; n vector<int> dd; n int inline mianx(int a, int l, int r) { return max(min(a, r), l); } n int inline Ubval(int val) { n   int l = -1, r = ((int)(dd).size()); n   while (r - l > 1) { n     int m = (l + r) >> 1; n     if (a[dd[m]] > val) n       r = m; n     else n       l = m; n   } n   return r; n } n int inline Lbpos(int val) { n   return lower_bound((dd).begin(), (dd).end(), val) - dd.begin(); n } n int inline FLbpos(int val) { n   return upper_bound((dd).begin(), (dd).end(), val, greater<int>()) - n          dd.begin(); n } n void inline solve() { n   int qid = 0; n   sl.init(); n   sr.init(); n   qj.init(); n   for (int i = 1; i <= (N); ++i) qj.add(i, a[i]); n   for (int i = 1; i <= (N); ++i) { n     while (((int)(dd).size()) && a[dd.back()] >= a[i]) dd.pop_back(); n     sl.add(((int)(dd).size()), a[i] - 2 * i); n     sr.add(((int)(dd).size()), -a[i] - 2 * i); n     int rp = ((int)(dd).size()); n     dd.push_back(i); n     while (qid < ((int)(q1).size()) && q1[qid].r2 == i) { n       int id = q1[qid].id; n       int r1 = q1[qid].r1, c1 = q1[qid].c1; n       int r2 = q1[qid].r2, c2 = q1[qid].c2; n       int lp = Lbpos(r1); n       ans[id] = min(ans[id], r2 - r1 + c2 + 1); n       ans[id] = min(ans[id], r2 - r1 + abs(min(c1, a[dd[lp]]) - c2)); n       int rpos = Ubval(c2), lpos = rpos - 1, pos; n       rpos = mianx(rpos, lp, rp); n       lpos = mianx(lpos, lp, rp); n       if (abs(a[dd[lpos]] - c2) < abs(a[dd[rpos]] - c2)) n         pos = lpos; n       else n         pos = rpos; n       ans[id] = min(ans[id], r2 - r1 + abs(a[dd[pos]] - c2) + 1); n       rpos = Ubval(c2), lpos = rpos - 1; n       rpos = mianx(rpos, 0, lp); n       lpos = mianx(lpos, -1, lp - 1); n       ans[id] = min(ans[id], r2 + r1 + sr.get(0, lpos) + c2 + 1); n       ans[id] = min(ans[id], r2 + r1 + sl.get(rpos, lp - 1) - c2 + 1); n       int sp = min(Ubval(min(c1, a[dd[lp]])), lp); n       rpos = Ubval(c2), lpos = rpos - 1; n       rpos = mianx(rpos, 0, sp); n       lpos = mianx(lpos, -1, sp - 1); n       ans[id] = min(ans[id], r2 + r1 + sr.get(0, lpos) + c2); n       ans[id] = min(ans[id], r2 + r1 + sl.get(rpos, sp - 1) - c2); n       ++qid; n     } n   } n   dd.clear(); n   sl.init(); n   sr.init(); n   --qid; n   for (int i = (N); ((-1) > 0 ? i <= (1) : i >= (1)); i += (-1)) { n     while (((int)(dd).size()) && a[dd.back()] >= a[i]) dd.pop_back(); n     sl.add(((int)(dd).size()), a[i] + 2 * i); n     sr.add(((int)(dd).size()), -a[i] + 2 * i); n     int rp = ((int)(dd).size()); n     dd.push_back(i); n     while (~qid && q1[qid].r2 == i) { n       int id = q1[qid].id; n       int r1 = q1[qid].r1, c1 = q1[qid].c1; n       int r2 = q1[qid].r2, c2 = q1[qid].c2; n       int rpos = Ubval(c2), lpos = rpos - 1; n       rpos = mianx(rpos, 0, rp + 1); n       lpos = mianx(lpos, -1, rp); n       ans[id] = min(ans[id], sr.get(0, lpos) + c2 - r2 - r1 + 1); n       ans[id] = min(ans[id], sl.get(rpos, rp) - c2 - r2 - r1 + 1); n       int sp = min(Ubval(min(qj.get(r1, r2), c1)), rp); n       rpos = Ubval(c2), lpos = rpos - 1; n       rpos = mianx(rpos, 0, sp); n       lpos = mianx(lpos, -1, sp - 1); n       ans[id] = min(ans[id], sr.get(0, lpos) + c2 - r2 - r1); n       ans[id] = min(ans[id], sl.get(rpos, sp - 1) - c2 - r2 - r1); n       --qid; n     } n   } n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= (N); ++i) scanf( %d , a + i); n   scanf( %d , &Q); n   memset(ans, Inf, sizeof(ans)); n   for (int i = 1; i <= (Q); ++i) { n     int r1, c1, r2, c2; n     scanf( %d%d%d%d , &r1, &c1, &r2, &c2); n     if (r1 <= r2) n       q1.push_back(Query(r1, c1, r2, c2, i)); n     else n       q2.push_back(Query(N - r1 + 1, c1, N - r2 + 1, c2, i)); n   } n   sort((q1).begin(), (q1).end()); n   solve(); n   reverse(a + 1, a + N + 1); n   sort((q2).begin(), (q2).end()); n   q2.swap(q1); n   solve(); n   for (int i = 1; i <= (Q); ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   float h, l; n   scanf( %f %f , &h, &l); n   float x = (l * l + h * h) / (2 * h) - h; n   printf( %f n , x); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   std::ios_base::sync_with_stdio(false); n   int n; n   std::cin >> n; n   std::vector<std::vector<bool> > g(n, std::vector<bool>(n, 0)); n   for (int row = 0; row < n; row++) { n     std::string s; n     std::cin >> s; n     for (int col = 0; col < s.size(); col++) { n       g[row][col] = s[col] -  0 ; n     } n   } n   std::vector<int> a(n); n   for (int p = 0; p < n; p++) { n     std::cin >> a[p]; n   } n   bool done(false); n   std::vector<int> guests; n   while (!done) { n     done = true; n     for (int p = 0; p < n; p++) { n       if (a[p] != 0) { n         continue; n       } n       done = false; n       guests.push_back(p); n       for (int q = 0; q < n; q++) { n         a[q] -= g[p][q]; n       } n       break; n     } n   } n   std::cout << guests.size() << std::endl; n   for (int p = 0; p < guests.size(); p++) { n     std::cout << (1 + guests[p]) << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int a[111][111][111]; n int color[111][111][111], cc; n char second[111]; n void visit(queue<pair<pair<int, int>, int> >* q, int x, int y, int z) { n   if (a[x][y][z] == 1 && color[x][y][z] != cc) { n     color[x][y][z] = cc; n     q->push(make_pair(make_pair(x, y), z)); n   } n } n void go(int x, int y, int z, int mx, int my, int mz) { n   assert(a[x][y][z] == 1); n   if (mx > n) mx = n; n   if (my > m) my = m; n   if (mz > k) mz = k; n   ++cc; n   queue<pair<pair<int, int>, int> > q; n   color[x][y][z] = cc; n   q.push(make_pair(make_pair(x, y), z)); n   while (!q.empty()) { n     pair<pair<int, int>, int> fr = q.front(); n     q.pop(); n     int x = fr.first.first; n     int y = fr.first.second; n     int z = fr.second; n     if (x + 1 <= mx) visit(&q, x + 1, y, z); n     if (y + 1 <= my) visit(&q, x, y + 1, z); n     if (z + 1 <= mz) visit(&q, x, y, z + 1); n   } n } n bool reachable(int x, int y, int z) { n   if (x + 1 < n && a[x + 1][y][z] == 1 && color[x + 1][y][z] != cc) { n     return false; n   } n   if (y + 1 < m && a[x][y + 1][z] == 1 && color[x][y + 1][z] != cc) { n     return false; n   } n   if (z + 1 < k && a[x][y][z + 1] == 1 && color[x][y][z + 1] != cc) { n     return false; n   } n   return true; n } n bool critical(int x, int y, int z) { n   if (x > 0 && a[x - 1][y][z] == 1) { n     go(x - 1, y, z, x + 1, y + 1, z + 1); n     if (!reachable(x, y, z)) { n       return true; n     } n   } n   if (y > 0 && a[x][y - 1][z] == 1) { n     go(x, y - 1, z, x + 1, y + 1, z + 1); n     if (!reachable(x, y, z)) { n       return true; n     } n   } n   if (z > 0 && a[x][y][z - 1] == 1) { n     go(x, y, z - 1, x + 1, y + 1, z + 1); n     if (!reachable(x, y, z)) { n       return true; n     } n   } n   return false; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       scanf( %s , second); n       for (int t = 0; t < k; t++) { n         a[i][j][t] = (second[t] ==  1 ); n       } n     } n   } n   int res = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       for (int t = 0; t < k; t++) { n         if (a[i][j][t] == 1) { n           a[i][j][t] = 0; n           res += critical(i, j, t); n           a[i][j][t] = 1; n         } n       } n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1005; n long long n, m, q, ans; n bool vizitat[N][N]; n long long dp[N][N][2]; n bool verifica(long long i, long long j) { n   if (i < 1 || j < 1 || i > n || j > m) return false; n   return true; n } n long long cnt(long long i, long long j) { n   long long answer = 0; n   for (long long it = 0; it <= 1; it++) { n     long long up = 0; n     long long lin = i; n     long long col = j; n     while (verifica(lin, col) && vizitat[lin][col] == false) { n       if (up % 2 == it) { n         lin--; n       } else { n         col--; n       } n       up++; n     } n     long long down = 0; n     lin = i; n     col = j; n     while (verifica(lin, col) && vizitat[lin][col] == false) { n       if (down % 2 == it) { n         col++; n       } else { n         lin++; n       } n       down++; n     } n     answer += up * down; n   } n   answer--; n   return answer; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> q; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= m; j++) { n       dp[i][j][1] = dp[i][j - 1][0] + 1; n       dp[i][j][0] = dp[i - 1][j][1] + 1; n       ans += dp[i][j][1]; n       ans += dp[i][j][0]; n     } n   } n   ans -= n * m; n   while (q--) { n     long long lin, col; n     cin >> lin >> col; n     if (vizitat[lin][col] == true) { n       vizitat[lin][col] = false; n       ans += cnt(lin, col); n     } else { n       ans -= cnt(lin, col); n       vizitat[lin][col] = true; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define pb push_back n #define ll long long n #define ff first n #define ss second n  n void solve() n {  n    int n; n    cin>>n; n  n    int sum=0; n    int cnt=1; n  n    while(sum<n) n    { n      sum+=cnt; n      cnt+=1; n    } n    if(sum-1==n) n     cout<<cnt; n   else n     cout<<cnt-1; n   cout<<  n ; n    n  n } n    n   n int main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     #ifndef ONLINE_JUDGE n        freopen( input.txt , r ,stdin); n        freopen( output.txt , w ,stdout); n      #endif n     int q=1; n     cin>>q; n    n   while(q--) n   { n        solve(); n   n   } n   n   return 0; n           n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int ar[n + 5]; n   int br[n + 5]; n   int mark[200005]; n   memset(mark, 0, sizeof mark); n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n     br[i] = ar[i]; n   } n   sort(br, br + n, greater<int>()); n   int sum = 0; n   int cr[n + 5]; n   int c = 0; n   for (int i = 0; i < k; i++) { n     sum += br[i]; n     for (int j = 0; j < n; j++) { n       if (br[i] == ar[j]) { n         ar[j] = 0; n         cr[c++] = j; n       } n     } n   } n   sort(cr, cr + k); n   cout << sum << endl; n   if (k == 1) n     cout << n << endl; n   else { n     for (int i = 0; i < k; i++) { n       if (i == 0) { n         cout << cr[i] + 1 <<    ; n       } else if (i == k - 1) { n         int p = cr[i] - cr[i - 1]; n         p += n - cr[i] - 1; n         cout << p << endl; n       } else { n         cout << cr[i] - cr[i - 1] <<    ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long rdtsc() { n   long long tmp; n   asm( rdtsc  :  =A (tmp)); n   return tmp; n } n inline int myrand() { return abs((rand() << 15) ^ rand()); } n inline int rnd(int x) { return myrand() % x; } n void precalc() {} n const int inf = (int)1.01e9; n const int n = 4; n int xs[n][2]; n bool read() { n   for (int i = 0; i < 4; ++i) { n     if (scanf( %d%d , &xs[i][0], &xs[i][1]) < 2) { n       return false; n     } n   } n   return true; n } n int res; n int axs[n][2]; n int perm[4]; n int calcans(int x[2], int y[2]) { n   for (int i = 0; i < n; ++i) { n     perm[i] = i; n   } n   int cres = inf; n   do { n     int cur = 0; n     for (int i = 0; i < n; ++i) { n       int cx = xs[perm[i]][0], cy = xs[perm[i]][1]; n       int nx = x[i / 2], ny = y[i % 2]; n       if (cx != nx && cy != ny) { n         cur = inf; n         break; n       } n       cur = max(cur, max(abs(cx - nx), abs(cy - ny))); n     } n     if (res > cur) { n       res = cur; n       for (int i = 0; i < n; ++i) { n         axs[perm[i]][0] = x[i / 2]; n         axs[perm[i]][1] = y[i % 2]; n       } n     } n     cres = min(cres, cur); n   } while (next_permutation(perm, perm + 4)); n   return cres; n } n int dir[n]; n map<int, vector<int> *> o[2]; n void updans() { n   o[0].clear(); n   o[1].clear(); n   for (int i = 0; i < n; ++i) { n     int x = xs[i][dir[i]]; n     auto it = o[dir[i]].find(x); n     vector<int> *v; n     if (it != o[dir[i]].end()) { n       v = it->second; n     } else { n       v = new vector<int>(); n       o[dir[i]][x] = v; n     } n     v->push_back(i); n   } n   int mx = max(((int)(o[0]).size()), ((int)(o[1]).size())); n   int mn = min(((int)(o[0]).size()), ((int)(o[1]).size())); n   if (mx != 2) { n     return; n   } n   int x[2], y[2]; n   int cntx = 0, cnty = 0; n   for (auto it : o[0]) { n     x[cntx++] = it.first; n   } n   for (auto it : o[1]) { n     y[cnty++] = it.first; n   } n   if (mn == 2) { n     if (abs(x[1] - x[0]) != abs(y[1] - y[0])) { n       return; n     } n     calcans(x, y); n     return; n   } n   if (mn == 1) { n     if (cntx == 1) { n       int dist = abs(y[1] - y[0]); n       for (int it = 0; it < 2; ++it) { n         x[1] = x[0] + (it ? (-dist) : dist); n         calcans(x, y); n       } n     } else { n       int dist = abs(x[1] - x[0]); n       for (int it = 0; it < 2; ++it) { n         y[1] = y[0] + (it ? (-dist) : dist); n         calcans(x, y); n       } n     } n     return; n   } n   if (mn == 0) { n     if (cntx == 0) { n       int dist = abs(y[1] - y[0]); n       int L = -inf, R = inf; n       while (R - L > 1) { n         int M = (L + R) / 2; n         x[0] = M, x[1] = M + dist; n         int curl = calcans(x, y); n         x[0] = M + 1, x[1] = M + 1 + dist; n         int curr = calcans(x, y); n         if (curl >= curr) { n           L = M; n         } else { n           R = M; n         } n       } n       x[0] = L, x[1] = L + dist; n       calcans(x, y); n       x[0] = R, x[1] = R + dist; n       calcans(x, y); n     } else { n       int dist = abs(x[1] - x[0]); n       int L = -inf, R = inf; n       while (R - L > 1) { n         int M = (L + R) / 2; n         y[0] = M, y[1] = M + dist; n         int curl = calcans(x, y); n         y[0] = M + 1, y[1] = M + 1 + dist; n         int curr = calcans(x, y); n         if (curl >= curr) { n           L = M; n         } else { n           R = M; n         } n       } n       y[0] = L, y[1] = L + dist; n       calcans(x, y); n       y[0] = R, y[1] = R + dist; n       calcans(x, y); n     } n   } n } n void gen(int i) { n   if (i == 4) { n     updans(); n     return; n   } n   dir[i] = 0; n   gen(i + 1); n   dir[i] = 1; n   gen(i + 1); n } n void solve() { n   res = inf; n   gen(0); n   if (res == inf) { n     printf( -1 n ); n     return; n   } n   printf( %d n , res); n   for (int i = 0; i < 4; ++i) { n     printf( %d %d n , axs[i][0], axs[i][1]); n   } n } n int main() { n   srand(rdtsc()); n   precalc(); n   int T = 0; n   scanf( %d , &T); n   for (int tn = 0; tn < T; ++tn) { n     assert(read()); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long MAX_N = 450 + 100; n const long long MOD = 1e9 + 7; n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   cout << 3 * n + min(k - 1, n - k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int m, q; n int a[3]; n string dday; n int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n string tostring(int x) { n   string temp =   ; n   for (int i = 0; i < 2; i++) { n     temp = (char)( 0  + x % 10) + temp; n     x /= 10; n   } n   return temp; n } n bool solve() { n   sort(a, a + 3); n   do { n     if (a[1] > 12) continue; n     if (a[2] % 4 == 0) { n       if (a[1] == 2 && a[0] > 29) continue; n     } else if (a[1] == 2 && a[0] > 28) n       continue; n     if (a[1] != 2 && a[0] > days[a[1] - 1]) continue; n     a[2] += 18; n     if (a[2] > 100) continue; n     string temp = tostring(a[2]); n     temp.push_back( . ); n     temp += tostring(a[1]); n     temp.push_back( . ); n     temp += tostring(a[0]); n     if (temp <= dday) { n       return true; n     } n     a[2] -= 18; n   } while (next_permutation(a, a + 3)); n   return false; n } n int main() { n   scanf( %d.%d.%d , &a[0], &a[1], &a[2]); n   dday = tostring(a[2]); n   dday.push_back( . ); n   dday += tostring(a[1]); n   dday.push_back( . ); n   dday += tostring(a[0]); n   scanf( %d.%d.%d , &a[0], &a[1], &a[2]); n   if (solve()) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   if (n == 0) { n     printf( 1 ); n   } else if (n % 2 == 0) { n     if (n % 4 == 0) { n       printf( 6 ); n     } else { n       printf( 4 ); n     } n   } else if (n % 2 != 0) { n     if (n % 4 == 1) { n       printf( 8 ); n     } else { n       printf( 2 ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n  n #define fi first n #define se second n #define mp make_pair n  n using namespace std; n using ll = long long; n using pii = pair<int, int>; n  n constexpr double eps = 1e-8; n constexpr int NINF = 0xc0c0c0c0; n constexpr int INF = 0x3f3f3f3f; n constexpr ll mod = 1e9 + 7; n constexpr ll N = 1e6 + 5; n  n int n, a[N], f[N], l[N], r[N], cnt[N]; n  n int main() { n     ios::sync_with_stdio(false); n     cin.tie(nullptr); n  n     cin >> n; n     for (int i = 1; i <= n; i++) { n         cin >> a[i]; n         if (!l[a[i]]) l[a[i]] = i; n     } n     for (int i = n; i; i--) { n         if (!r[a[i]]) r[a[i]] = i; n     } n     for (int i = n; i; i--) { n         cnt[a[i]]++; n         if (i == l[a[i]]) { n             f[i] = max(cnt[a[i]] + f[r[a[i]] + 1], f[i + 1]); n         } else { n             f[i] = max(cnt[a[i]], f[i + 1]); n         } n     } n     cout << n - f[1] <<   n ; n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int oo = 1e9 + 5; n const int N = 1e6 + 5; n long long ans = 0; n int main() { n   int n; n   cin >> n; n   int a[n + 1]; n   for (int(i) = (0); (i) < (n); i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   int x; n   x = (n - 1) / 2; n   cout << a[x]; 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; n     cin >> n; n     int a[n]; n     bool ok = true; n     bool f = true; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (!ok) continue; n       if (f && a[i] < i) { n         f = false; n       } n       if (!f && a[i] < n - i - 1) ok = false; n     } n     if (n % 2 == 0 && a[n / 2] == (n / 2 - 1) && a[n / 2 - 1] == (n / 2 - 1)) n       ok = false; n     if (ok) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<char, int> mp1; n int main() { n   ios_base::sync_with_stdio(0); n   string s, p, q; n   long long int a, b, c, d, i, j, k, u, v, w = 0, x, y, z; n   cin >> p; n   for (i = 0; i < p.length(); i++) mp1[p[i]] = 1; n   cin >> s; n   cin >> k; n   while (k--) { n     w = 0; n     cin >> q; n     if (s.length() > q.length() + 1) { n       cout <<  NO  << endl; n       continue; n     } n     for (i = 0; i < q.length() && i < s.length(); i++) { n       if (q[i] == s[i]) continue; n       if (s[i] ==  ?  && mp1[q[i]] == 1) continue; n       if (s[i] ==  * ) { n         w = 1; n         v = s.length() - 1; n         for (u = q.length() - 1; v > i && u >= i; u--, v--) { n           if (q[u] == s[v]) continue; n           if (s[v] ==  ?  && mp1[q[u]] == 1) continue; n           break; n         } n       } else { n         cout <<  NO  << endl; n         w = -1; n         break; n       } n       if (v != i) { n         w = -1; n         cout <<  NO  << endl; n         break; n       } n       for (j = u; j >= i; j--) { n         if (mp1[q[j]] == 0) continue; n         break; n       } n       if (j >= i) { n         w = -1; n         cout <<  NO  << endl; n         break; n       } n       i = q.length() - 1; n     } n     if (w == -1) continue; n     if (i != q.length()) { n       cout <<  NO  << endl; n       continue; n     } n     if (s.length() == q.length() + 1 && w == 0 && s[q.length()] !=  * ) { n       cout <<  NO  << endl; n       continue; n     } n     cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 7; n const long long MOD = 1e9 + 7; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umod(T& a, T b) { n   a += b; n   while (a < 0) a += MOD; n   a %= MOD; n   return 1; n } n int las, n, cur, cost[N], m, cst; n vector<int> v[N]; n vector<long long> par[N]; n long long ans; n void merge(int x) { n   v[x].push_back(0); n   int l = x * 2, r = x * 2 + 1; n   int a = 0, b = 0; n   while (a < int(v[l].size()) && b < int(v[r].size())) { n     if (v[l][a] + cost[l] <= v[r][b] + cost[r]) n       v[x].push_back(v[l][a] + cost[l]), a++; n     else n       v[x].push_back(v[r][b] + cost[r]), b++; n   } n   while (a < int(v[l].size())) v[x].push_back(v[l][a] + cost[l]), a++; n   while (b < int(v[r].size())) v[x].push_back(v[r][b] + cost[r]), b++; n   par[x].push_back(0LL); n   for (int i = 1; i < int(v[x].size()); i++) n     par[x].push_back(par[x][i - 1] + v[x][i]); n } n void dfs(int x) { n   if (x > n) return; n   dfs(x * 2); n   dfs(x * 2 + 1); n   merge(x); n } n long long get(int x, int y) { n   if (x > n || y <= 0) return 0; n   int ind = upper_bound(v[x].begin(), v[x].end(), y) - v[x].begin(); n   return 0LL + (1LL * ind * y) - par[x][ind - 1]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 2; i <= n; i++) scanf( %d , &cost[i]); n   dfs(1); n   while (m--) { n     int aa, bb; n     scanf( %d%d , &aa, &bb); n     las = -1, cur = aa, ans = 0, cst = bb; n     while (las != 1 && cst > 0) { n       if (las != cur * 2) ans += get(cur * 2, cst - cost[cur * 2]); n       if (las != cur * 2 + 1) ans += get(cur * 2 + 1, cst - cost[cur * 2 + 1]); n       ans += cst; n       cst -= cost[cur]; n       las = cur; n       cur /= 2; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int a[maxn], x[maxn], y[maxn], sk[maxn], top, cnt; n int main() { n   int n, P, ly = -1, fl = 0; n   scanf( %d , &n), P = n; n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = n; i >= 1; i--) { n     if (a[i] == 1) { n       sk[++top] = x[++cnt] = P--; n       y[cnt] = i; n       if (!fl) ly = i; n     } else if (a[i] == 2) { n       if (top <= 0) return puts( -1 ), 0; n       x[++cnt] = sk[top--]; n       y[cnt] = i; n       if (!fl) ly = i; n       P = min(P, x[cnt] - 1); n     } else if (a[i] == 3) { n       if (ly == -1) return puts( -1 ), 0; n       x[++cnt] = P; n       y[cnt] = ly; n       x[++cnt] = P; n       y[cnt] = ly = i; n       if (!fl) top--; n       fl = 1; n       P--; n     } n   } n   printf( %d n , cnt); n   for (int i = 1; i <= cnt; i++) printf( %d %d n , x[i], y[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   double x, y; n }; n int main() { n   point p; n   double a, b, c, d; n   point v; n   scanf( %lf%lf%lf%lf%lf%lf%lf%lf , &p.x, &p.y, &v.x, &v.y, &a, &b, &c, &d); n   double lenth = sqrt(v.x * v.x + v.y * v.y); n   v.x /= lenth; n   v.y /= lenth; n   a /= 2; n   c /= 2; n   point q; n   q.x = p.x + v.x * b; n   q.y = p.y + v.y * b; n   printf( %.10f %.10f n , q.x, q.y); n   q.x = p.x - v.y * a; n   q.y = p.y + v.x * a; n   printf( %.10f %.10f n , q.x, q.y); n   q.x = p.x - v.y * c; n   q.y = p.y + v.x * c; n   printf( %.10f %.10f n , q.x, q.y); n   point w; n   w.x = q.x - v.x * d; n   w.y = q.y - v.y * d; n   printf( %.10f %.10f n , w.x, w.y); n   q.x = w.x + v.y * c * 2; n   q.y = w.y - v.x * c * 2; n   printf( %.10f %.10f n , q.x, q.y); n   q.x = p.x + v.y * c; n   q.y = p.y - v.x * c; n   printf( %.10f %.10f n , q.x, q.y); n   q.x = p.x + v.y * a; n   q.y = p.y - v.x * a; n   printf( %.10f %.10f n , q.x, q.y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, M, K, R, l, r, A, B, X, Y, Z, C, D, Q, a, b, c, d, i, j, k, first, n     mx, ans, cnt, T, P; n void solve() { n   cin >> N >> K; n   long long A[N]; n   long long lar = INT_MIN, sma = INT_MAX; n   for (long long int i = 0; i < N; i++) { n     cin >> A[i]; n     lar = max(lar, A[i]); n     sma = min(sma, A[i]); n   } n   for (long long int i = 0; i < N; i++) { n     A[i] = lar - A[i]; n   } n   if (K % 2 == 0) { n     for (long long int i = 0; i < N; i++) { n       A[i] = lar - sma - A[i]; n     } n   } n   for (long long int i = 0; i < N; i++) { n     cout << A[i] <<    ; n   } n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mini(long long a, long long b) { n   if (a < b) return a; n   return b; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   vector<long long> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   sort(a.begin(), a.end()); n   reverse(a.begin(), a.end()); n   for (int i = 1; i < n; ++i) a[i] += a[i - 1]; n   vector<long long> odp(n, 0); n   for (int i = 1; i <= n - 1; ++i) { n     for (long long il = 1, pos = 0, w = 0; pos < n; pos += il, il *= i, ++w) { n       odp[i] += w * a[mini(n - 1, pos + il - 1)]; n       if (pos != 0) odp[i] -= w * a[pos - 1]; n     } n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     int k; n     cin >> k; n     k = mini(k, n - 1); n     cout << odp[k] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const long long INFLL = 2e18; n const int MOD = 1e8 + 7; n const int N = 4e5 + 10; n int fx[8] = {+0, +0, +1, -1, +1, +1, -1, -1}; n int fy[8] = {+1, -1, +0, +0, +1, -1, +1, -1}; n int timer; n int used[N]; n int tin[N], low[N]; n char bridge; n vector<int> g[N]; n void dfsAP(int v, int p = -1) { n   if (bridge) return; n   used[v] = 1; n   tin[v] = low[v] = ++timer; n   for (int to : g[v]) { n     if (p == to) continue; n     if (!used[to]) { n       dfsAP(to, v); n       low[v] = min(low[v], low[to]); n       if (tin[v] < low[to]) bridge = 1; n     } else { n       low[v] = min(low[v], tin[to]); n     } n   } n   used[v] = 2; n } n void dfsCC(int v, int p = -1) { n   used[v] = 1; n   for (int to : g[v]) { n     if (p == to) continue; n     if (!used[to]) { n       dfsCC(to, v); n       printf( %d %d n , v, to); n     } else if (used[to] == 1) { n       printf( %d %d n , v, to); n     } n   } n   used[v] = 2; n } n void solve(int n) { n   dfsAP(1); n   if (bridge) { n     puts( 0 ); n     return; n   } n   fill(used, used + n + 1, 0); n   dfsCC(1); n } n int main() { n   int n, m, u, v; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; ++i) { n     scanf( %d %d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   solve(n); n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char** argv) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int a, b, c; n     cin >> a >> b >> c; n     int sum1 = a + b; n     int cnt = 0; n     while (sum1 <= c) { n       if (a > b) swap(a, b); n       a += b; n       cnt++; n       sum1 += b; n     } n     cout << cnt + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int g[100000]; n int res[100000]; n int main() { n   int N; n   scanf( %d , &N); n   ; n   g[0] = 0; n   g[1] = 0; n   res[0] = res[1] = -1; n   for (int(n) = 2; n < (int)N + 1; n++) { n     set<int> s; n     vector<int> v; n     for (int i = 2; i * i <= 2 * n; i++) { n       if ((2 * n) % i == 0) { n         v.push_back(i); n         v.push_back((2 * n) / i); n       } n     } n     res[n] = -1; n     for (__typeof(v).begin() it = v.begin(); it != v.end(); it++) { n       int help = 2 * n / (*it) + *it - 1; n       if (help % 2 == 0 && help / 2 >= *it && help / 2 < n) { n         int num = 0; n         for (int m = 0; m < *it; m++) { n           num = num ^ g[help / 2 - m]; n         } n         if (num == 0) { n           res[n] = res[n] == -1 ? *it : min(res[n], *it); n         } n         s.insert(num); n       } n     } n     int sol = 0; n     while (s.find(sol) != s.end()) { n       sol++; n     } n     g[n] = sol; n   } n   if (g[N] == 0) { n     puts( -1 ); n   } else { n     printf( %d n , res[N]); n   } n } n